lp.c revision 179184d3e9bc6ab146407a62a8461338687b1908
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A *
2N/A */
2N/A
2N/A/* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A#include <string.h>
2N/A#include <locale.h>
2N/A#include <libintl.h>
2N/A#include <papi.h>
2N/A#include "common.h"
2N/A
2N/A#ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */
2N/A#include <magic.h>
2N/A#endif /* HAVE_LIBMAGIC */
2N/A
2N/Astatic void
2N/Ausage(char *program)
2N/A{
2N/A char *name;
2N/A
2N/A if ((name = strrchr(program, '/')) == NULL)
2N/A name = program;
2N/A else
2N/A name++;
2N/A
2N/A fprintf(stdout,
2N/A gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] "
2N/A "[-f form-name] [-H special-handling] [-n number] "
2N/A "[-o option] [-P page-list] [-q priority-level] "
2N/A "[-S character-set | print-wheel] [-t title] [-v] "
2N/A "[-T content-type [-r]] [-y mode-list] [file...]\n"),
2N/A name);
2N/A exit(1);
2N/A}
2N/A
2N/Astatic struct {
2N/A char *mime_type;
2N/A char *lp_type;
2N/A} type_map[] = {
2N/A { "text/plain", "simple" },
2N/A { "application/octet-stream", "raw" },
2N/A { "application/octet-stream", "any" },
2N/A { "application/postscript", "postscript" },
2N/A { "application/postscript", "ps" },
2N/A { "application/x-cif", "cif" },
2N/A { "application/x-dvi", "dvi" },
2N/A { "application/x-plot", "plot" },
2N/A { "application/x-ditroff", "troff" },
2N/A { "application/x-troff", "otroff" },
2N/A { "application/x-pr", "pr" },
2N/A { "application/x-fortran", "fortran" },
2N/A { "application/x-raster", "raster" },
2N/A { NULL, NULL}
2N/A};
2N/A
2N/Astatic char *
2N/Alp_type_to_mime_type(char *lp_type)
2N/A{
2N/A int i;
2N/A
2N/A if (lp_type == NULL)
2N/A return ("application/octet-stream");
2N/A
2N/A for (i = 0; type_map[i].lp_type != NULL; i++)
2N/A if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
2N/A return (type_map[i].mime_type);
2N/A
2N/A return (lp_type);
2N/A}
2N/A
2N/Aint
2N/Amain(int ac, char *av[])
2N/A{
2N/A papi_status_t status;
2N/A papi_service_t svc = NULL;
2N/A papi_attribute_t **list = NULL;
2N/A papi_encryption_t encryption = PAPI_ENCRYPT_NEVER;
2N/A papi_job_t job = NULL;
2N/A char *printer = NULL;
2N/A char b = PAPI_TRUE;
2N/A int copy = 0;
2N/A int silent = 0;
2N/A int dump = 0;
2N/A int validate = 0;
2N/A int modify = -1;
2N/A int c;
2N/A
2N/A (void) setlocale(LC_ALL, "");
2N/A (void) textdomain("SUNW_OST_OSCMD");
2N/A
2N/A while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF)
2N/A switch (c) {
2N/A case 'H': /* handling */
2N/A if (strcasecmp(optarg, "hold") == 0)
2N/A papiAttributeListAddString(&list,
2N/A PAPI_ATTR_EXCL,
2N/A "job-hold-until", "indefinite");
2N/A else if (strcasecmp(optarg, "release") == 0)
2N/A papiAttributeListAddString(&list,
2N/A PAPI_ATTR_EXCL,
2N/A "job-hold-until", "no-hold");
2N/A else if (strcasecmp(optarg, "immediate") == 0)
2N/A papiAttributeListAddInteger(&list,
2N/A PAPI_ATTR_EXCL,
2N/A "job-priority", 100);
2N/A else
2N/A papiAttributeListAddString(&list,
2N/A PAPI_ATTR_EXCL,
2N/A "job-hold-until", optarg);
2N/A break;
2N/A case 'P': /* page list */
2N/A papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2N/A "page-ranges", optarg);
2N/A break;
2N/A case 'S': /* charset */
2N/A papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2N/A "lp-charset", optarg);
2N/A break;
2N/A case 'T': /* type */
2N/A papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2N/A "document-format",
2N/A lp_type_to_mime_type(optarg));
2N/A break;
2N/A case 'D': /* dump */
2N/A dump = 1;
2N/A break;
2N/A case 'c': /* copy */
2N/A copy = 1;
2N/A break;
2N/A case 'd': /* destination */
2N/A printer = optarg;
2N/A break;
2N/A case 'f': /* form */
2N/A papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
2N/A "media", optarg);
2N/A break;
2N/A case 'i': /* modify job */
2N/A if ((get_printer_id(optarg, &printer, &modify) < 0) ||
2N/A (modify < 0)) {
2N/A fprintf(stderr,
2N/A gettext("invalid request id: %s\n"),
2N/A optarg);
2N/A exit(1);
2N/A }
2N/A break;
2N/A case 'm': /* mail when complete */
2N/A papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
2N/A "rfc-1179-mail", 1);
2N/A break;
2N/A case 'n': /* copies */
2N/A papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
2N/A "copies", atoi(optarg));
2N/A break;
2N/A case 'o': /* lp "options" */
2N/A papiAttributeListFromString(&list,
2N/A PAPI_ATTR_REPLACE, optarg);
2N/A break;
case 'p': /* Solaris - notification */
papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
"rfc-1179-mail", 1);
break;
case 'q': { /* priority */
int i = atoi(optarg);
i = 99 * (39 - i) / 39 + 1;
if ((i < 1) || (i > 100)) {
fprintf(stderr, gettext(
"priority must be between 0 and 39.\n"));
exit(1);
}
papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL,
"priority", i);
}
break;
case 'r': /* "raw" mode */
papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
"document-format",
"application/octet-stream");
papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
"stty", "raw");
break;
case 's': /* suppress message */
silent = 1;
break;
case 't': /* title */
papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
"job-name", optarg);
break;
case 'V': /* validate */
validate = 1;
break;
case 'w':
papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL,
"rfc-1179-mail", 1);
break;
case 'y': /* lp "modes" */
papiAttributeListAddString(&list, PAPI_ATTR_APPEND,
"lp-modes", optarg);
break;
case 'E':
encryption = PAPI_ENCRYPT_REQUIRED;
break;
default:
usage(av[0]);
}
/* convert "banner", "nobanner" to "job-sheet" */
if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) {
(void) papiAttributeListDelete(&list, "banner");
if (b == PAPI_FALSE)
papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
"job-sheets", "none");
}
if ((printer == NULL) &&
((printer = getenv("PRINTER")) == NULL) &&
((printer = getenv("LPDEST")) == NULL))
printer = DEFAULT_DEST;
if (modify == -1) {
char *document_format = "application/octet-stream";
#ifdef MAGIC_MIME
if (optind != ac) {
/* get the mime type of the file data */
magic_t ms = NULL;
if ((ms = magic_open(MAGIC_MIME)) != NULL) {
document_format = magic_file(ms, av[optind]);
magic_close(ms);
}
}
#endif
papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1);
papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
"document-format", document_format);
papiAttributeListAddString(&list, PAPI_ATTR_EXCL,
"job-sheets", "standard");
}
status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback,
encryption, NULL);
if (status != PAPI_OK) {
fprintf(stderr, gettext(
"Failed to contact service for %s: %s\n"), printer,
verbose_papi_message(svc, status));
exit(1);
}
if (modify != -1)
status = papiJobModify(svc, printer, modify, list, &job);
else if (optind == ac) /* no file list, use stdin */
status = jobSubmitSTDIN(svc, printer, list, &job);
else if (validate == 1) /* validate the request can be processed */
status = papiJobValidate(svc, printer, list,
NULL, &av[optind], &job);
else if (copy == 0) /* reference the files in the job, default */
status = papiJobSubmitByReference(svc, printer, list,
NULL, &av[optind], &job);
else /* copy the files before return, -c */
status = papiJobSubmit(svc, printer, list,
NULL, &av[optind], &job);
papiAttributeListFree(list);
if (status != PAPI_OK) {
fprintf(stderr, gettext("%s: %s\n"), printer,
verbose_papi_message(svc, status));
papiJobFree(job);
papiServiceDestroy(svc);
exit(1);
}
if (((silent == 0) || (dump != 0)) &&
((list = papiJobGetAttributeList(job)) != NULL)) {
int32_t id = 0;
papiAttributeListGetString(list, NULL,
"printer-name", &printer);
papiAttributeListGetInteger(list, NULL, "job-id", &id);
printf(gettext("request id is %s-%d "), printer, id);
if (ac != optind)
printf("(%d file(s))\n", ac - optind);
else
printf("(standard input)\n");
if (dump != 0) {
printf("job attributes:\n");
papiAttributeListPrint(stdout, list, "\t");
printf("\n");
}
}
papiJobFree(job);
papiServiceDestroy(svc);
return (0);
}