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 /* 236725Sjacobs * Copyright 2008 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 #pragma ident "%Z%%M% %I% %E% SMI" 312264Sjacobs 322264Sjacobs #include <stdio.h> 332264Sjacobs #include <stdlib.h> 342264Sjacobs #include <unistd.h> 352264Sjacobs #include <string.h> 362264Sjacobs #include <locale.h> 372264Sjacobs #include <libintl.h> 382264Sjacobs #include <papi.h> 392264Sjacobs #include "common.h" 402264Sjacobs 412264Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 422264Sjacobs #include <magic.h> 432264Sjacobs #endif /* HAVE_LIBMAGIC */ 442264Sjacobs 452264Sjacobs static void 462264Sjacobs usage(char *program) 472264Sjacobs { 482264Sjacobs char *name; 492264Sjacobs 502264Sjacobs if ((name = strrchr(program, '/')) == NULL) 512264Sjacobs name = program; 522264Sjacobs else 532264Sjacobs name++; 542264Sjacobs 552264Sjacobs fprintf(stdout, 562264Sjacobs gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 572264Sjacobs "[-f form-name] [-H special-handling] [-n number] " 582264Sjacobs "[-o option] [-P page-list] [-q priority-level] " 592264Sjacobs "[-S character-set | print-wheel] [-t title] [-v] " 602264Sjacobs "[-T content-type [-r]] [-y mode-list] [file...]\n"), 612264Sjacobs name); 622264Sjacobs exit(1); 632264Sjacobs } 642264Sjacobs 652264Sjacobs int 662264Sjacobs main(int ac, char *av[]) 672264Sjacobs { 682264Sjacobs papi_status_t status; 692264Sjacobs papi_service_t svc = NULL; 702264Sjacobs papi_attribute_t **list = NULL; 712264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 722264Sjacobs papi_job_t job = NULL; 73*7253Sjacobs char prefetch[3]; 74*7253Sjacobs int prefetch_len = sizeof (prefetch); 752264Sjacobs char *printer = NULL; 762264Sjacobs char b = PAPI_TRUE; 772264Sjacobs int copy = 0; 782264Sjacobs int silent = 0; 792264Sjacobs int dump = 0; 802264Sjacobs int validate = 0; 812264Sjacobs int modify = -1; 822264Sjacobs int c; 832264Sjacobs 842264Sjacobs (void) setlocale(LC_ALL, ""); 852264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 862264Sjacobs 872264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 882264Sjacobs switch (c) { 892264Sjacobs case 'H': /* handling */ 902264Sjacobs if (strcasecmp(optarg, "hold") == 0) 912264Sjacobs papiAttributeListAddString(&list, 922264Sjacobs PAPI_ATTR_EXCL, 932264Sjacobs "job-hold-until", "indefinite"); 946725Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 952264Sjacobs papiAttributeListAddString(&list, 962264Sjacobs PAPI_ATTR_EXCL, 972264Sjacobs "job-hold-until", "no-hold"); 982264Sjacobs else 992264Sjacobs papiAttributeListAddString(&list, 1002264Sjacobs PAPI_ATTR_EXCL, 1012264Sjacobs "job-hold-until", optarg); 1022264Sjacobs break; 1036725Sjacobs case 'P': { /* page list */ 1046725Sjacobs char buf[BUFSIZ]; 1056725Sjacobs 1066725Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 1076725Sjacobs papiAttributeListFromString(&list, 1086725Sjacobs PAPI_ATTR_EXCL, buf); 1096725Sjacobs } 1102264Sjacobs break; 1112264Sjacobs case 'S': /* charset */ 1122264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1132264Sjacobs "lp-charset", optarg); 1142264Sjacobs break; 1152264Sjacobs case 'T': /* type */ 1162264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1172264Sjacobs "document-format", 1182264Sjacobs lp_type_to_mime_type(optarg)); 1192264Sjacobs break; 1202264Sjacobs case 'D': /* dump */ 1212264Sjacobs dump = 1; 1222264Sjacobs break; 1232264Sjacobs case 'c': /* copy */ 1242264Sjacobs copy = 1; 1252264Sjacobs break; 1262264Sjacobs case 'd': /* destination */ 1272264Sjacobs printer = optarg; 1282264Sjacobs break; 1292264Sjacobs case 'f': /* form */ 1302264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1313125Sjacobs "form", optarg); 1322264Sjacobs break; 1332264Sjacobs case 'i': /* modify job */ 1342264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1352264Sjacobs (modify < 0)) { 1362264Sjacobs fprintf(stderr, 1372264Sjacobs gettext("invalid request id: %s\n"), 1382264Sjacobs optarg); 1392264Sjacobs exit(1); 1402264Sjacobs } 1412264Sjacobs break; 1422264Sjacobs case 'm': /* mail when complete */ 1432264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1442264Sjacobs "rfc-1179-mail", 1); 1452264Sjacobs break; 1462264Sjacobs case 'n': /* copies */ 1472264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1482264Sjacobs "copies", atoi(optarg)); 1492264Sjacobs break; 1502264Sjacobs case 'o': /* lp "options" */ 1512264Sjacobs papiAttributeListFromString(&list, 1522264Sjacobs PAPI_ATTR_REPLACE, optarg); 1532264Sjacobs break; 1542264Sjacobs case 'p': /* Solaris - notification */ 1552264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1562264Sjacobs "rfc-1179-mail", 1); 1572264Sjacobs break; 1582264Sjacobs case 'q': { /* priority */ 1592264Sjacobs int i = atoi(optarg); 1602264Sjacobs 1616725Sjacobs i = 100 - (i * 2.5); 1622264Sjacobs if ((i < 1) || (i > 100)) { 1632264Sjacobs fprintf(stderr, gettext( 1642264Sjacobs "priority must be between 0 and 39.\n")); 1652264Sjacobs exit(1); 1662264Sjacobs } 1672264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1686725Sjacobs "job-priority", i); 1692264Sjacobs } 1702264Sjacobs break; 1712264Sjacobs case 'r': /* "raw" mode */ 1722264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1732264Sjacobs "document-format", 1742264Sjacobs "application/octet-stream"); 1752264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1762264Sjacobs "stty", "raw"); 1772264Sjacobs break; 1782264Sjacobs case 's': /* suppress message */ 1792264Sjacobs silent = 1; 1802264Sjacobs break; 1812264Sjacobs case 't': /* title */ 1822264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1832264Sjacobs "job-name", optarg); 1842264Sjacobs break; 1852264Sjacobs case 'V': /* validate */ 1862264Sjacobs validate = 1; 1872264Sjacobs break; 1882264Sjacobs case 'w': 1892264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1902264Sjacobs "rfc-1179-mail", 1); 1912264Sjacobs break; 1922264Sjacobs case 'y': /* lp "modes" */ 1932264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1942264Sjacobs "lp-modes", optarg); 1952264Sjacobs break; 1962264Sjacobs case 'E': 1972264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1982264Sjacobs break; 1992264Sjacobs default: 2002264Sjacobs usage(av[0]); 2012264Sjacobs } 2022264Sjacobs 2032264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2042264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2052264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2062264Sjacobs if (b == PAPI_FALSE) 2072264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2082264Sjacobs "job-sheets", "none"); 2092264Sjacobs } 2102264Sjacobs 2112264Sjacobs if ((printer == NULL) && 2122264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2132264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2142264Sjacobs printer = DEFAULT_DEST; 2152264Sjacobs 2162660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 2172660Sjacobs optind = ac; 2182660Sjacobs 2192264Sjacobs if (modify == -1) { 2203125Sjacobs char *document_format = "text/plain"; 2212264Sjacobs 2222264Sjacobs if (optind != ac) { 2232264Sjacobs /* get the mime type of the file data */ 2243125Sjacobs #ifdef MAGIC_MIME 2252264Sjacobs magic_t ms = NULL; 2262264Sjacobs 2272264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2282264Sjacobs document_format = magic_file(ms, av[optind]); 2292264Sjacobs magic_close(ms); 2302264Sjacobs } 2313125Sjacobs #else 2323125Sjacobs if (is_postscript(av[optind]) == 1) 2333125Sjacobs document_format = "application/postscript"; 2343125Sjacobs #endif 235*7253Sjacobs } else { 236*7253Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 237*7253Sjacobs == 1) 238*7253Sjacobs document_format = "application/postscript"; 2392264Sjacobs } 2402264Sjacobs 2412264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2422264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2432264Sjacobs "document-format", document_format); 2442264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2452264Sjacobs "job-sheets", "standard"); 2462264Sjacobs } 2472264Sjacobs 2482264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2492264Sjacobs encryption, NULL); 2502264Sjacobs if (status != PAPI_OK) { 2512264Sjacobs fprintf(stderr, gettext( 2522264Sjacobs "Failed to contact service for %s: %s\n"), printer, 2532264Sjacobs verbose_papi_message(svc, status)); 2542264Sjacobs exit(1); 2552264Sjacobs } 2562264Sjacobs 2573125Sjacobs if (dump != 0) { 2583125Sjacobs printf("requesting attributes:\n"); 2593125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2603125Sjacobs printf("\n"); 2613125Sjacobs } 2623125Sjacobs 2632264Sjacobs if (modify != -1) 2642264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2652264Sjacobs else if (optind == ac) /* no file list, use stdin */ 266*7253Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 267*7253Sjacobs list, &job); 2682264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2692264Sjacobs status = papiJobValidate(svc, printer, list, 2702264Sjacobs NULL, &av[optind], &job); 2712264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2722264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2732264Sjacobs NULL, &av[optind], &job); 2742264Sjacobs else /* copy the files before return, -c */ 2752264Sjacobs status = papiJobSubmit(svc, printer, list, 2762264Sjacobs NULL, &av[optind], &job); 2772264Sjacobs 2782264Sjacobs papiAttributeListFree(list); 2792264Sjacobs 2802264Sjacobs if (status != PAPI_OK) { 2812264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 2822264Sjacobs verbose_papi_message(svc, status)); 2832264Sjacobs papiJobFree(job); 2842264Sjacobs papiServiceDestroy(svc); 2852264Sjacobs exit(1); 2862264Sjacobs } 2872264Sjacobs 2882264Sjacobs if (((silent == 0) || (dump != 0)) && 2892264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 2902264Sjacobs int32_t id = 0; 2912264Sjacobs 2922383Sjacobs papiAttributeListGetString(list, NULL, 2932383Sjacobs "printer-name", &printer); 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