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 /* 2312091SSonam.Gupta@Sun.COM * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 242264Sjacobs * 252264Sjacobs */ 262264Sjacobs 272383Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */ 282264Sjacobs 292264Sjacobs #include <stdio.h> 302264Sjacobs #include <stdlib.h> 312264Sjacobs #include <unistd.h> 322264Sjacobs #include <string.h> 332264Sjacobs #include <locale.h> 342264Sjacobs #include <libintl.h> 352264Sjacobs #include <papi.h> 362264Sjacobs #include "common.h" 3712503SRalph.Turner@Sun.COM #include <pwd.h> 3812503SRalph.Turner@Sun.COM #include <grp.h> 3912503SRalph.Turner@Sun.COM #include <sys/types.h> 402264Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 412264Sjacobs #include <magic.h> 422264Sjacobs #endif /* HAVE_LIBMAGIC */ 432264Sjacobs 442264Sjacobs static void 452264Sjacobs usage(char *program) 462264Sjacobs { 472264Sjacobs char *name; 482264Sjacobs 492264Sjacobs if ((name = strrchr(program, '/')) == NULL) 502264Sjacobs name = program; 512264Sjacobs else 522264Sjacobs name++; 532264Sjacobs 542264Sjacobs fprintf(stdout, 559256SSonam.Gupta@Sun.COM gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 569256SSonam.Gupta@Sun.COM "[-f form-name] [-H special-handling] [-n number] " 579256SSonam.Gupta@Sun.COM "[-o option] [-P page-list] [-q priority-level] " 589256SSonam.Gupta@Sun.COM "[-S character-set | print-wheel] [-t title] [-v] " 599256SSonam.Gupta@Sun.COM "[-T content-type [-r]] [-y mode-list] [file...]\n"), 609256SSonam.Gupta@Sun.COM name); 612264Sjacobs exit(1); 622264Sjacobs } 632264Sjacobs 642264Sjacobs int 652264Sjacobs main(int ac, char *av[]) 662264Sjacobs { 672264Sjacobs papi_status_t status; 682264Sjacobs papi_service_t svc = NULL; 692264Sjacobs papi_attribute_t **list = NULL; 702264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 712264Sjacobs papi_job_t job = NULL; 727253Sjacobs char prefetch[3]; 737253Sjacobs int prefetch_len = sizeof (prefetch); 742264Sjacobs char *printer = NULL; 752264Sjacobs char b = PAPI_TRUE; 762264Sjacobs int copy = 0; 772264Sjacobs int silent = 0; 782264Sjacobs int dump = 0; 792264Sjacobs int validate = 0; 802264Sjacobs int modify = -1; 812264Sjacobs int c; 8212503SRalph.Turner@Sun.COM uid_t ruid; 8312503SRalph.Turner@Sun.COM struct passwd *pw; 842264Sjacobs 852264Sjacobs (void) setlocale(LC_ALL, ""); 862264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 872264Sjacobs 8812503SRalph.Turner@Sun.COM ruid = getuid(); 8912503SRalph.Turner@Sun.COM if ((pw = getpwuid(ruid)) != NULL) 9012503SRalph.Turner@Sun.COM (void) initgroups(pw->pw_name, pw->pw_gid); 9112503SRalph.Turner@Sun.COM (void) setuid(ruid); 9212503SRalph.Turner@Sun.COM 9312503SRalph.Turner@Sun.COM 942264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 952264Sjacobs switch (c) { 962264Sjacobs case 'H': /* handling */ 972264Sjacobs if (strcasecmp(optarg, "hold") == 0) 982264Sjacobs papiAttributeListAddString(&list, 999256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1009256SSonam.Gupta@Sun.COM "job-hold-until", "indefinite"); 1016725Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 1022264Sjacobs papiAttributeListAddString(&list, 1039256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1049256SSonam.Gupta@Sun.COM "job-hold-until", "no-hold"); 1052264Sjacobs else 1062264Sjacobs papiAttributeListAddString(&list, 1079256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 1089256SSonam.Gupta@Sun.COM "job-hold-until", optarg); 1092264Sjacobs break; 1106725Sjacobs case 'P': { /* page list */ 1116725Sjacobs char buf[BUFSIZ]; 1126725Sjacobs 1136725Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 1146725Sjacobs papiAttributeListFromString(&list, 1159256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, buf); 1166725Sjacobs } 1172264Sjacobs break; 1182264Sjacobs case 'S': /* charset */ 1192264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1209256SSonam.Gupta@Sun.COM "lp-charset", optarg); 1212264Sjacobs break; 1222264Sjacobs case 'T': /* type */ 1232264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1249256SSonam.Gupta@Sun.COM "document-format", 1259256SSonam.Gupta@Sun.COM lp_type_to_mime_type(optarg)); 1262264Sjacobs break; 1272264Sjacobs case 'D': /* dump */ 1282264Sjacobs dump = 1; 1292264Sjacobs break; 1302264Sjacobs case 'c': /* copy */ 1312264Sjacobs copy = 1; 1322264Sjacobs break; 1332264Sjacobs case 'd': /* destination */ 1342264Sjacobs printer = optarg; 1352264Sjacobs break; 1362264Sjacobs case 'f': /* form */ 1372264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1389256SSonam.Gupta@Sun.COM "form", optarg); 1392264Sjacobs break; 1402264Sjacobs case 'i': /* modify job */ 1412264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1422264Sjacobs (modify < 0)) { 1432264Sjacobs fprintf(stderr, 1449256SSonam.Gupta@Sun.COM gettext("invalid request id: %s\n"), 1459256SSonam.Gupta@Sun.COM optarg); 1462264Sjacobs exit(1); 1472264Sjacobs } 1482264Sjacobs break; 1492264Sjacobs case 'm': /* mail when complete */ 1502264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1519256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1522264Sjacobs break; 1532264Sjacobs case 'n': /* copies */ 1542264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1559256SSonam.Gupta@Sun.COM "copies", atoi(optarg)); 1562264Sjacobs break; 1572264Sjacobs case 'o': /* lp "options" */ 1582264Sjacobs papiAttributeListFromString(&list, 1599256SSonam.Gupta@Sun.COM PAPI_ATTR_REPLACE, optarg); 1602264Sjacobs break; 1612264Sjacobs case 'p': /* Solaris - notification */ 1622264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1639256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1642264Sjacobs break; 1652264Sjacobs case 'q': { /* priority */ 1662264Sjacobs int i = atoi(optarg); 1672264Sjacobs 1686725Sjacobs i = 100 - (i * 2.5); 1692264Sjacobs if ((i < 1) || (i > 100)) { 170*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("UX:lp: ")); 171*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("ERROR: ")); 172*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("Bad priority" 173*12928SSonam.Gupta@Sun.COM " value \"%s\"."), optarg); 174*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("\n ")); 175*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("TO FIX")); 176*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext(": ")); 177*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("Use an integer value" 178*12928SSonam.Gupta@Sun.COM " from 0 to 39.")); 179*12928SSonam.Gupta@Sun.COM fprintf(stderr, gettext("\n")); 1802264Sjacobs exit(1); 1812264Sjacobs } 1822264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1839256SSonam.Gupta@Sun.COM "job-priority", i); 1842264Sjacobs } 1852264Sjacobs break; 1862264Sjacobs case 'r': /* "raw" mode */ 1872264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1889256SSonam.Gupta@Sun.COM "document-format", 1899256SSonam.Gupta@Sun.COM "application/octet-stream"); 1902264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1919256SSonam.Gupta@Sun.COM "stty", "raw"); 1922264Sjacobs break; 1932264Sjacobs case 's': /* suppress message */ 1942264Sjacobs silent = 1; 1952264Sjacobs break; 1962264Sjacobs case 't': /* title */ 1972264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1989256SSonam.Gupta@Sun.COM "job-name", optarg); 1992264Sjacobs break; 2002264Sjacobs case 'V': /* validate */ 2012264Sjacobs validate = 1; 2022264Sjacobs break; 2032264Sjacobs case 'w': 2042264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2059256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 2062264Sjacobs break; 2072264Sjacobs case 'y': /* lp "modes" */ 2082264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 2099256SSonam.Gupta@Sun.COM "lp-modes", optarg); 2102264Sjacobs break; 2112264Sjacobs case 'E': 2122264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 2132264Sjacobs break; 2142264Sjacobs default: 2152264Sjacobs usage(av[0]); 2162264Sjacobs } 2172264Sjacobs 2182264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2192264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2202264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2212264Sjacobs if (b == PAPI_FALSE) 2222264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2239256SSonam.Gupta@Sun.COM "job-sheets", "none"); 2242264Sjacobs } 2252264Sjacobs 2262264Sjacobs if ((printer == NULL) && 2272264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2282264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2292264Sjacobs printer = DEFAULT_DEST; 2302264Sjacobs 2312660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 2322660Sjacobs optind = ac; 2332660Sjacobs 2342264Sjacobs if (modify == -1) { 2353125Sjacobs char *document_format = "text/plain"; 2362264Sjacobs 2372264Sjacobs if (optind != ac) { 2382264Sjacobs /* get the mime type of the file data */ 2393125Sjacobs #ifdef MAGIC_MIME 2402264Sjacobs magic_t ms = NULL; 2412264Sjacobs 2422264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2432264Sjacobs document_format = magic_file(ms, av[optind]); 2442264Sjacobs magic_close(ms); 2452264Sjacobs } 2463125Sjacobs #else 2473125Sjacobs if (is_postscript(av[optind]) == 1) 2483125Sjacobs document_format = "application/postscript"; 2493125Sjacobs #endif 2507253Sjacobs } else { 2517253Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 2529256SSonam.Gupta@Sun.COM == 1) 2537253Sjacobs document_format = "application/postscript"; 2542264Sjacobs } 2552264Sjacobs 2562264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2572264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2589256SSonam.Gupta@Sun.COM "document-format", document_format); 2592264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2609256SSonam.Gupta@Sun.COM "job-sheets", "standard"); 2612264Sjacobs } 2622264Sjacobs 2632264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2649256SSonam.Gupta@Sun.COM encryption, NULL); 2652264Sjacobs if (status != PAPI_OK) { 2662264Sjacobs fprintf(stderr, gettext( 2679256SSonam.Gupta@Sun.COM "Failed to contact service for %s: %s\n"), printer, 2689256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2692264Sjacobs exit(1); 2702264Sjacobs } 2712264Sjacobs 2723125Sjacobs if (dump != 0) { 2733125Sjacobs printf("requesting attributes:\n"); 2743125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2753125Sjacobs printf("\n"); 2763125Sjacobs } 2773125Sjacobs 2782264Sjacobs if (modify != -1) 2792264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2802264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2817253Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 2829256SSonam.Gupta@Sun.COM list, &job); 2832264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2842264Sjacobs status = papiJobValidate(svc, printer, list, 2859256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2862264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2872264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2889256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2892264Sjacobs else /* copy the files before return, -c */ 2902264Sjacobs status = papiJobSubmit(svc, printer, list, 2919256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2922264Sjacobs 2932264Sjacobs papiAttributeListFree(list); 2942264Sjacobs 2952264Sjacobs if (status != PAPI_OK) { 2962264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 2979256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2982264Sjacobs papiJobFree(job); 2992264Sjacobs papiServiceDestroy(svc); 3002264Sjacobs exit(1); 3012264Sjacobs } 3022264Sjacobs 3032264Sjacobs if (((silent == 0) || (dump != 0)) && 3042264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 30512091SSonam.Gupta@Sun.COM int32_t id = -1; 3062264Sjacobs 3079256SSonam.Gupta@Sun.COM if (printer == NULL) 3089256SSonam.Gupta@Sun.COM papiAttributeListGetString(list, NULL, 3099256SSonam.Gupta@Sun.COM "printer-name", &printer); 3109256SSonam.Gupta@Sun.COM 31112091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, 31212091SSonam.Gupta@Sun.COM "job-id-requested", &id); 31312091SSonam.Gupta@Sun.COM if (id == -1) { 31412091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, "job-id", &id); 31512091SSonam.Gupta@Sun.COM } 31612091SSonam.Gupta@Sun.COM 3172383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 3182264Sjacobs if (ac != optind) 3192264Sjacobs printf("(%d file(s))\n", ac - optind); 3202264Sjacobs else 3212264Sjacobs printf("(standard input)\n"); 3222264Sjacobs 3232264Sjacobs if (dump != 0) { 3242264Sjacobs printf("job attributes:\n"); 3252264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 3262264Sjacobs printf("\n"); 3272264Sjacobs } 3282264Sjacobs } 3292264Sjacobs 3302264Sjacobs papiJobFree(job); 3312264Sjacobs papiServiceDestroy(svc); 3322264Sjacobs 3332264Sjacobs return (0); 3342264Sjacobs } 335