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*12091SSonam.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" 372264Sjacobs 382264Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 392264Sjacobs #include <magic.h> 402264Sjacobs #endif /* HAVE_LIBMAGIC */ 412264Sjacobs 422264Sjacobs static void 432264Sjacobs usage(char *program) 442264Sjacobs { 452264Sjacobs char *name; 462264Sjacobs 472264Sjacobs if ((name = strrchr(program, '/')) == NULL) 482264Sjacobs name = program; 492264Sjacobs else 502264Sjacobs name++; 512264Sjacobs 522264Sjacobs fprintf(stdout, 539256SSonam.Gupta@Sun.COM gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 549256SSonam.Gupta@Sun.COM "[-f form-name] [-H special-handling] [-n number] " 559256SSonam.Gupta@Sun.COM "[-o option] [-P page-list] [-q priority-level] " 569256SSonam.Gupta@Sun.COM "[-S character-set | print-wheel] [-t title] [-v] " 579256SSonam.Gupta@Sun.COM "[-T content-type [-r]] [-y mode-list] [file...]\n"), 589256SSonam.Gupta@Sun.COM name); 592264Sjacobs exit(1); 602264Sjacobs } 612264Sjacobs 622264Sjacobs int 632264Sjacobs main(int ac, char *av[]) 642264Sjacobs { 652264Sjacobs papi_status_t status; 662264Sjacobs papi_service_t svc = NULL; 672264Sjacobs papi_attribute_t **list = NULL; 682264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 692264Sjacobs papi_job_t job = NULL; 707253Sjacobs char prefetch[3]; 717253Sjacobs int prefetch_len = sizeof (prefetch); 722264Sjacobs char *printer = NULL; 732264Sjacobs char b = PAPI_TRUE; 742264Sjacobs int copy = 0; 752264Sjacobs int silent = 0; 762264Sjacobs int dump = 0; 772264Sjacobs int validate = 0; 782264Sjacobs int modify = -1; 792264Sjacobs int c; 802264Sjacobs 812264Sjacobs (void) setlocale(LC_ALL, ""); 822264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 832264Sjacobs 842264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 852264Sjacobs switch (c) { 862264Sjacobs case 'H': /* handling */ 872264Sjacobs if (strcasecmp(optarg, "hold") == 0) 882264Sjacobs papiAttributeListAddString(&list, 899256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 909256SSonam.Gupta@Sun.COM "job-hold-until", "indefinite"); 916725Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 922264Sjacobs papiAttributeListAddString(&list, 939256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 949256SSonam.Gupta@Sun.COM "job-hold-until", "no-hold"); 952264Sjacobs else 962264Sjacobs papiAttributeListAddString(&list, 979256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, 989256SSonam.Gupta@Sun.COM "job-hold-until", optarg); 992264Sjacobs break; 1006725Sjacobs case 'P': { /* page list */ 1016725Sjacobs char buf[BUFSIZ]; 1026725Sjacobs 1036725Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 1046725Sjacobs papiAttributeListFromString(&list, 1059256SSonam.Gupta@Sun.COM PAPI_ATTR_EXCL, buf); 1066725Sjacobs } 1072264Sjacobs break; 1082264Sjacobs case 'S': /* charset */ 1092264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1109256SSonam.Gupta@Sun.COM "lp-charset", optarg); 1112264Sjacobs break; 1122264Sjacobs case 'T': /* type */ 1132264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1149256SSonam.Gupta@Sun.COM "document-format", 1159256SSonam.Gupta@Sun.COM lp_type_to_mime_type(optarg)); 1162264Sjacobs break; 1172264Sjacobs case 'D': /* dump */ 1182264Sjacobs dump = 1; 1192264Sjacobs break; 1202264Sjacobs case 'c': /* copy */ 1212264Sjacobs copy = 1; 1222264Sjacobs break; 1232264Sjacobs case 'd': /* destination */ 1242264Sjacobs printer = optarg; 1252264Sjacobs break; 1262264Sjacobs case 'f': /* form */ 1272264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1289256SSonam.Gupta@Sun.COM "form", optarg); 1292264Sjacobs break; 1302264Sjacobs case 'i': /* modify job */ 1312264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1322264Sjacobs (modify < 0)) { 1332264Sjacobs fprintf(stderr, 1349256SSonam.Gupta@Sun.COM gettext("invalid request id: %s\n"), 1359256SSonam.Gupta@Sun.COM optarg); 1362264Sjacobs exit(1); 1372264Sjacobs } 1382264Sjacobs break; 1392264Sjacobs case 'm': /* mail when complete */ 1402264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1419256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1422264Sjacobs break; 1432264Sjacobs case 'n': /* copies */ 1442264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1459256SSonam.Gupta@Sun.COM "copies", atoi(optarg)); 1462264Sjacobs break; 1472264Sjacobs case 'o': /* lp "options" */ 1482264Sjacobs papiAttributeListFromString(&list, 1499256SSonam.Gupta@Sun.COM PAPI_ATTR_REPLACE, optarg); 1502264Sjacobs break; 1512264Sjacobs case 'p': /* Solaris - notification */ 1522264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1539256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1542264Sjacobs break; 1552264Sjacobs case 'q': { /* priority */ 1562264Sjacobs int i = atoi(optarg); 1572264Sjacobs 1586725Sjacobs i = 100 - (i * 2.5); 1592264Sjacobs if ((i < 1) || (i > 100)) { 1602264Sjacobs fprintf(stderr, gettext( 1612264Sjacobs "priority must be between 0 and 39.\n")); 1622264Sjacobs exit(1); 1632264Sjacobs } 1642264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1659256SSonam.Gupta@Sun.COM "job-priority", i); 1662264Sjacobs } 1672264Sjacobs break; 1682264Sjacobs case 'r': /* "raw" mode */ 1692264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1709256SSonam.Gupta@Sun.COM "document-format", 1719256SSonam.Gupta@Sun.COM "application/octet-stream"); 1722264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1739256SSonam.Gupta@Sun.COM "stty", "raw"); 1742264Sjacobs break; 1752264Sjacobs case 's': /* suppress message */ 1762264Sjacobs silent = 1; 1772264Sjacobs break; 1782264Sjacobs case 't': /* title */ 1792264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1809256SSonam.Gupta@Sun.COM "job-name", optarg); 1812264Sjacobs break; 1822264Sjacobs case 'V': /* validate */ 1832264Sjacobs validate = 1; 1842264Sjacobs break; 1852264Sjacobs case 'w': 1862264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1879256SSonam.Gupta@Sun.COM "rfc-1179-mail", 1); 1882264Sjacobs break; 1892264Sjacobs case 'y': /* lp "modes" */ 1902264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1919256SSonam.Gupta@Sun.COM "lp-modes", optarg); 1922264Sjacobs break; 1932264Sjacobs case 'E': 1942264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 1952264Sjacobs break; 1962264Sjacobs default: 1972264Sjacobs usage(av[0]); 1982264Sjacobs } 1992264Sjacobs 2002264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2012264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2022264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2032264Sjacobs if (b == PAPI_FALSE) 2042264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2059256SSonam.Gupta@Sun.COM "job-sheets", "none"); 2062264Sjacobs } 2072264Sjacobs 2082264Sjacobs if ((printer == NULL) && 2092264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2102264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2112264Sjacobs printer = DEFAULT_DEST; 2122264Sjacobs 2132660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 2142660Sjacobs optind = ac; 2152660Sjacobs 2162264Sjacobs if (modify == -1) { 2173125Sjacobs char *document_format = "text/plain"; 2182264Sjacobs 2192264Sjacobs if (optind != ac) { 2202264Sjacobs /* get the mime type of the file data */ 2213125Sjacobs #ifdef MAGIC_MIME 2222264Sjacobs magic_t ms = NULL; 2232264Sjacobs 2242264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2252264Sjacobs document_format = magic_file(ms, av[optind]); 2262264Sjacobs magic_close(ms); 2272264Sjacobs } 2283125Sjacobs #else 2293125Sjacobs if (is_postscript(av[optind]) == 1) 2303125Sjacobs document_format = "application/postscript"; 2313125Sjacobs #endif 2327253Sjacobs } else { 2337253Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 2349256SSonam.Gupta@Sun.COM == 1) 2357253Sjacobs document_format = "application/postscript"; 2362264Sjacobs } 2372264Sjacobs 2382264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2392264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2409256SSonam.Gupta@Sun.COM "document-format", document_format); 2412264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2429256SSonam.Gupta@Sun.COM "job-sheets", "standard"); 2432264Sjacobs } 2442264Sjacobs 2452264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2469256SSonam.Gupta@Sun.COM encryption, NULL); 2472264Sjacobs if (status != PAPI_OK) { 2482264Sjacobs fprintf(stderr, gettext( 2499256SSonam.Gupta@Sun.COM "Failed to contact service for %s: %s\n"), printer, 2509256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2512264Sjacobs exit(1); 2522264Sjacobs } 2532264Sjacobs 2543125Sjacobs if (dump != 0) { 2553125Sjacobs printf("requesting attributes:\n"); 2563125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2573125Sjacobs printf("\n"); 2583125Sjacobs } 2593125Sjacobs 2602264Sjacobs if (modify != -1) 2612264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2622264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2637253Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 2649256SSonam.Gupta@Sun.COM list, &job); 2652264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2662264Sjacobs status = papiJobValidate(svc, printer, list, 2679256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2682264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2692264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2709256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2712264Sjacobs else /* copy the files before return, -c */ 2722264Sjacobs status = papiJobSubmit(svc, printer, list, 2739256SSonam.Gupta@Sun.COM NULL, &av[optind], &job); 2742264Sjacobs 2752264Sjacobs papiAttributeListFree(list); 2762264Sjacobs 2772264Sjacobs if (status != PAPI_OK) { 2782264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 2799256SSonam.Gupta@Sun.COM verbose_papi_message(svc, status)); 2802264Sjacobs papiJobFree(job); 2812264Sjacobs papiServiceDestroy(svc); 2822264Sjacobs exit(1); 2832264Sjacobs } 2842264Sjacobs 2852264Sjacobs if (((silent == 0) || (dump != 0)) && 2862264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 287*12091SSonam.Gupta@Sun.COM int32_t id = -1; 2882264Sjacobs 2899256SSonam.Gupta@Sun.COM if (printer == NULL) 2909256SSonam.Gupta@Sun.COM papiAttributeListGetString(list, NULL, 2919256SSonam.Gupta@Sun.COM "printer-name", &printer); 2929256SSonam.Gupta@Sun.COM 293*12091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, 294*12091SSonam.Gupta@Sun.COM "job-id-requested", &id); 295*12091SSonam.Gupta@Sun.COM if (id == -1) { 296*12091SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL, "job-id", &id); 297*12091SSonam.Gupta@Sun.COM } 298*12091SSonam.Gupta@Sun.COM 2992383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 3002264Sjacobs if (ac != optind) 3012264Sjacobs printf("(%d file(s))\n", ac - optind); 3022264Sjacobs else 3032264Sjacobs printf("(standard input)\n"); 3042264Sjacobs 3052264Sjacobs if (dump != 0) { 3062264Sjacobs printf("job attributes:\n"); 3072264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 3082264Sjacobs printf("\n"); 3092264Sjacobs } 3102264Sjacobs } 3112264Sjacobs 3122264Sjacobs papiJobFree(job); 3132264Sjacobs papiServiceDestroy(svc); 3142264Sjacobs 3152264Sjacobs return (0); 3162264Sjacobs } 317