12264Sjacobs /* 22264Sjacobs * CDDL HEADER START 32264Sjacobs * 42264Sjacobs * The contents of this file are subject to the terms of the 52264Sjacobs * Common Development and Distribution License (the "License"). 62264Sjacobs * You may not use this file except in compliance with the License. 72264Sjacobs * 82264Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 92264Sjacobs * or http://www.opensolaris.org/os/licensing. 102264Sjacobs * See the License for the specific language governing permissions 112264Sjacobs * and limitations under the License. 122264Sjacobs * 132264Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 142264Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 152264Sjacobs * If applicable, add the following below this CDDL HEADER, with the 162264Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 172264Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 182264Sjacobs * 192264Sjacobs * CDDL HEADER END 202264Sjacobs */ 212264Sjacobs 222264Sjacobs /* 23*9256SSonam.Gupta@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 242264Sjacobs * Use is subject to license terms. 252264Sjacobs * 262264Sjacobs */ 272264Sjacobs 282383Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */ 292264Sjacobs 302264Sjacobs #include <stdio.h> 312264Sjacobs #include <stdlib.h> 322264Sjacobs #include <unistd.h> 332264Sjacobs #include <string.h> 342264Sjacobs #include <locale.h> 352264Sjacobs #include <libintl.h> 362264Sjacobs #include <papi.h> 372264Sjacobs #include "common.h" 382264Sjacobs 392264Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 402264Sjacobs #include <magic.h> 412264Sjacobs #endif /* HAVE_LIBMAGIC */ 422264Sjacobs 432264Sjacobs static void 442264Sjacobs usage(char *program) 452264Sjacobs { 462264Sjacobs char *name; 472264Sjacobs 482264Sjacobs if ((name = strrchr(program, '/')) == NULL) 492264Sjacobs name = program; 502264Sjacobs else 512264Sjacobs name++; 522264Sjacobs 532264Sjacobs fprintf(stdout, 54*9256SSonam.Gupta@Sun.COM gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 55*9256SSonam.Gupta@Sun.COM "[-f form-name] [-H special-handling] [-n number] " 56*9256SSonam.Gupta@Sun.COM "[-o option] [-P page-list] [-q priority-level] " 57*9256SSonam.Gupta@Sun.COM "[-S character-set | print-wheel] [-t title] [-v] " 58*9256SSonam.Gupta@Sun.COM "[-T content-type [-r]] [-y mode-list] [file...]\n"), 59*9256SSonam.Gupta@Sun.COM name); 602264Sjacobs exit(1); 612264Sjacobs } 622264Sjacobs 632264Sjacobs int 642264Sjacobs main(int ac, char *av[]) 652264Sjacobs { 662264Sjacobs papi_status_t status; 672264Sjacobs papi_service_t svc = NULL; 682264Sjacobs papi_attribute_t **list = NULL; 692264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 702264Sjacobs papi_job_t job = NULL; 717253Sjacobs char prefetch[3]; 727253Sjacobs int prefetch_len = sizeof (prefetch); 732264Sjacobs char *printer = NULL; 742264Sjacobs char b = PAPI_TRUE; 752264Sjacobs int copy = 0; 762264Sjacobs int silent = 0; 772264Sjacobs int dump = 0; 782264Sjacobs int validate = 0; 792264Sjacobs int modify = -1; 802264Sjacobs int c; 812264Sjacobs 822264Sjacobs (void) setlocale(LC_ALL, ""); 832264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 842264Sjacobs 852264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 862264Sjacobs switch (c) { 872264Sjacobs case 'H': /* handling */ 882264Sjacobs if (strcasecmp(optarg, "hold") == 0) 892264Sjacobs papiAttributeListAddString(&list, 90*9256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 91*9256SSonam.Gupta@Sun.COM "job-hold-until", "indefinite"); 926725Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 932264Sjacobs papiAttributeListAddString(&list, 94*9256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 95*9256SSonam.Gupta@Sun.COM "job-hold-until", "no-hold"); 962264Sjacobs else 972264Sjacobs papiAttributeListAddString(&list, 98*9256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 99*9256SSonam.Gupta@Sun.COM "job-hold-until", optarg); 1002264Sjacobs break; 1016725Sjacobs case 'P': { /* page list */ 1026725Sjacobs char buf[BUFSIZ]; 1036725Sjacobs 1046725Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 1056725Sjacobs papiAttributeListFromString(&list, 106*9256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, buf); 1076725Sjacobs } 1082264Sjacobs break; 1092264Sjacobs case 'S': /* charset */ 1102264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 111*9256SSonam.Gupta@Sun.COM "lp-charset", optarg); 1122264Sjacobs break; 1132264Sjacobs case 'T': /* type */ 1142264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 115*9256SSonam.Gupta@Sun.COM "document-format", 116*9256SSonam.Gupta@Sun.COM lp_type_to_mime_type(optarg)); 1172264Sjacobs break; 1182264Sjacobs case 'D': /* dump */ 1192264Sjacobs dump = 1; 1202264Sjacobs break; 1212264Sjacobs case 'c': /* copy */ 1222264Sjacobs copy = 1; 1232264Sjacobs break; 1242264Sjacobs case 'd': /* destination */ 1252264Sjacobs printer = optarg; 1262264Sjacobs break; 1272264Sjacobs case 'f': /* form */ 1282264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 129*9256SSonam.Gupta@Sun.COM "form", optarg); 1302264Sjacobs break; 1312264Sjacobs case 'i': /* modify job */ 1322264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1332264Sjacobs (modify < 0)) { 1342264Sjacobs fprintf(stderr, 135*9256SSonam.Gupta@Sun.COM gettext("invalid request id: %s\n"), 136*9256SSonam.Gupta@Sun.COM optarg); 1372264Sjacobs exit(1); 1382264Sjacobs } 1392264Sjacobs break; 1402264Sjacobs case 'm': /* mail when complete */ 1412264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 142*9256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1432264Sjacobs break; 1442264Sjacobs case 'n': /* copies */ 1452264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 146*9256SSonam.Gupta@Sun.COM "copies", atoi(optarg)); 1472264Sjacobs break; 1482264Sjacobs case 'o': /* lp "options" */ 1492264Sjacobs papiAttributeListFromString(&list, 150*9256SSonam.Gupta@Sun.COM PAPI_ATTR_REPLACE, optarg); 1512264Sjacobs break; 1522264Sjacobs case 'p': /* Solaris - notification */ 1532264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 154*9256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1552264Sjacobs break; 1562264Sjacobs case 'q': { /* priority */ 1572264Sjacobs int i = atoi(optarg); 1582264Sjacobs 1596725Sjacobs i = 100 - (i * 2.5); 1602264Sjacobs if ((i < 1) || (i > 100)) { 1612264Sjacobs fprintf(stderr, gettext( 1622264Sjacobs "priority must be between 0 and 39.\n")); 1632264Sjacobs exit(1); 1642264Sjacobs } 1652264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 166*9256SSonam.Gupta@Sun.COM "job-priority", i); 1672264Sjacobs } 1682264Sjacobs break; 1692264Sjacobs case 'r': /* "raw" mode */ 1702264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 171*9256SSonam.Gupta@Sun.COM "document-format", 172*9256SSonam.Gupta@Sun.COM "application/octet-stream"); 1732264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 174*9256SSonam.Gupta@Sun.COM "stty", "raw"); 1752264Sjacobs break; 1762264Sjacobs case 's': /* suppress message */ 1772264Sjacobs silent = 1; 1782264Sjacobs break; 1792264Sjacobs case 't': /* title */ 1802264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 181*9256SSonam.Gupta@Sun.COM "job-name", optarg); 1822264Sjacobs break; 1832264Sjacobs case 'V': /* validate */ 1842264Sjacobs validate = 1; 1852264Sjacobs break; 1862264Sjacobs case 'w': 1872264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 188*9256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1892264Sjacobs break; 1902264Sjacobs case 'y': /* lp "modes" */ 1912264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 192*9256SSonam.Gupta@Sun.COM "lp-modes", optarg); 1932264Sjacobs break; 1942264Sjacobs case 'E': 1952264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1962264Sjacobs break; 1972264Sjacobs default: 1982264Sjacobs usage(av[0]); 1992264Sjacobs } 2002264Sjacobs 2012264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2022264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2032264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2042264Sjacobs if (b == PAPI_FALSE) 2052264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 206*9256SSonam.Gupta@Sun.COM "job-sheets", "none"); 2072264Sjacobs } 2082264Sjacobs 2092264Sjacobs if ((printer == NULL) && 2102264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2112264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2122264Sjacobs printer = DEFAULT_DEST; 2132264Sjacobs 2142660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 2152660Sjacobs optind = ac; 2162660Sjacobs 2172264Sjacobs if (modify == -1) { 2183125Sjacobs char *document_format = "text/plain"; 2192264Sjacobs 2202264Sjacobs if (optind != ac) { 2212264Sjacobs /* get the mime type of the file data */ 2223125Sjacobs #ifdef MAGIC_MIME 2232264Sjacobs magic_t ms = NULL; 2242264Sjacobs 2252264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2262264Sjacobs document_format = magic_file(ms, av[optind]); 2272264Sjacobs magic_close(ms); 2282264Sjacobs } 2293125Sjacobs #else 2303125Sjacobs if (is_postscript(av[optind]) == 1) 2313125Sjacobs document_format = "application/postscript"; 2323125Sjacobs #endif 2337253Sjacobs } else { 2347253Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 235*9256SSonam.Gupta@Sun.COM == 1) 2367253Sjacobs document_format = "application/postscript"; 2372264Sjacobs } 2382264Sjacobs 2392264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2402264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 241*9256SSonam.Gupta@Sun.COM "document-format", document_format); 2422264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 243*9256SSonam.Gupta@Sun.COM "job-sheets", "standard"); 2442264Sjacobs } 2452264Sjacobs 2462264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 247*9256SSonam.Gupta@Sun.COM encryption, NULL); 2482264Sjacobs if (status != PAPI_OK) { 2492264Sjacobs fprintf(stderr, gettext( 250*9256SSonam.Gupta@Sun.COM "Failed to contact service for %s: %s\n"), printer, 251*9256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2522264Sjacobs exit(1); 2532264Sjacobs } 2542264Sjacobs 2553125Sjacobs if (dump != 0) { 2563125Sjacobs printf("requesting attributes:\n"); 2573125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2583125Sjacobs printf("\n"); 2593125Sjacobs } 2603125Sjacobs 2612264Sjacobs if (modify != -1) 2622264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2632264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2647253Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 265*9256SSonam.Gupta@Sun.COM list, &job); 2662264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2672264Sjacobs status = papiJobValidate(svc, printer, list, 268*9256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2692264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2702264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 271*9256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2722264Sjacobs else /* copy the files before return, -c */ 2732264Sjacobs status = papiJobSubmit(svc, printer, list, 274*9256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2752264Sjacobs 2762264Sjacobs papiAttributeListFree(list); 2772264Sjacobs 2782264Sjacobs if (status != PAPI_OK) { 2792264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 280*9256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2812264Sjacobs papiJobFree(job); 2822264Sjacobs papiServiceDestroy(svc); 2832264Sjacobs exit(1); 2842264Sjacobs } 2852264Sjacobs 2862264Sjacobs if (((silent == 0) || (dump != 0)) && 2872264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 2882264Sjacobs int32_t id = 0; 2892264Sjacobs 290*9256SSonam.Gupta@Sun.COM if (printer == NULL) 291*9256SSonam.Gupta@Sun.COM papiAttributeListGetString(list, NULL, 292*9256SSonam.Gupta@Sun.COM "printer-name", &printer); 293*9256SSonam.Gupta@Sun.COM 2942264Sjacobs papiAttributeListGetInteger(list, NULL, "job-id", &id); 2952383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 2962264Sjacobs if (ac != optind) 2972264Sjacobs printf("(%d file(s))\n", ac - optind); 2982264Sjacobs else 2992264Sjacobs printf("(standard input)\n"); 3002264Sjacobs 3012264Sjacobs if (dump != 0) { 3022264Sjacobs printf("job attributes:\n"); 3032264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 3042264Sjacobs printf("\n"); 3052264Sjacobs } 3062264Sjacobs } 3072264Sjacobs 3082264Sjacobs papiJobFree(job); 3092264Sjacobs papiServiceDestroy(svc); 3102264Sjacobs 3112264Sjacobs return (0); 3122264Sjacobs } 313