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 /* 232264Sjacobs * Copyright 2006 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 static struct { 662264Sjacobs char *mime_type; 672264Sjacobs char *lp_type; 682264Sjacobs } type_map[] = { 692264Sjacobs { "text/plain", "simple" }, 702264Sjacobs { "application/octet-stream", "raw" }, 712264Sjacobs { "application/octet-stream", "any" }, 722264Sjacobs { "application/postscript", "postscript" }, 732264Sjacobs { "application/postscript", "ps" }, 742264Sjacobs { "application/x-cif", "cif" }, 752264Sjacobs { "application/x-dvi", "dvi" }, 762264Sjacobs { "application/x-plot", "plot" }, 772264Sjacobs { "application/x-ditroff", "troff" }, 782264Sjacobs { "application/x-troff", "otroff" }, 792264Sjacobs { "application/x-pr", "pr" }, 802264Sjacobs { "application/x-fortran", "fortran" }, 812264Sjacobs { "application/x-raster", "raster" }, 822264Sjacobs { NULL, NULL} 832264Sjacobs }; 842264Sjacobs 852264Sjacobs static char * 862264Sjacobs lp_type_to_mime_type(char *lp_type) 872264Sjacobs { 882264Sjacobs int i; 892264Sjacobs 902264Sjacobs if (lp_type == NULL) 912264Sjacobs return ("application/octet-stream"); 922264Sjacobs 932264Sjacobs for (i = 0; type_map[i].lp_type != NULL; i++) 942264Sjacobs if (strcasecmp(type_map[i].lp_type, lp_type) == 0) 952264Sjacobs return (type_map[i].mime_type); 962264Sjacobs 972264Sjacobs return (lp_type); 982264Sjacobs } 992264Sjacobs 1002264Sjacobs int 1012264Sjacobs main(int ac, char *av[]) 1022264Sjacobs { 1032264Sjacobs papi_status_t status; 1042264Sjacobs papi_service_t svc = NULL; 1052264Sjacobs papi_attribute_t **list = NULL; 1062264Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1072264Sjacobs papi_job_t job = NULL; 1082264Sjacobs char *printer = NULL; 1092264Sjacobs char b = PAPI_TRUE; 1102264Sjacobs int copy = 0; 1112264Sjacobs int silent = 0; 1122264Sjacobs int dump = 0; 1132264Sjacobs int validate = 0; 1142264Sjacobs int modify = -1; 1152264Sjacobs int c; 1162264Sjacobs 1172264Sjacobs (void) setlocale(LC_ALL, ""); 1182264Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 1192264Sjacobs 1202264Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 1212264Sjacobs switch (c) { 1222264Sjacobs case 'H': /* handling */ 1232264Sjacobs if (strcasecmp(optarg, "hold") == 0) 1242264Sjacobs papiAttributeListAddString(&list, 1252264Sjacobs PAPI_ATTR_EXCL, 1262264Sjacobs "job-hold-until", "indefinite"); 1272264Sjacobs else if (strcasecmp(optarg, "release") == 0) 1282264Sjacobs papiAttributeListAddString(&list, 1292264Sjacobs PAPI_ATTR_EXCL, 1302264Sjacobs "job-hold-until", "no-hold"); 1312264Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 1322264Sjacobs papiAttributeListAddInteger(&list, 1332264Sjacobs PAPI_ATTR_EXCL, 1342264Sjacobs "job-priority", 100); 1352264Sjacobs else 1362264Sjacobs papiAttributeListAddString(&list, 1372264Sjacobs PAPI_ATTR_EXCL, 1382264Sjacobs "job-hold-until", optarg); 1392264Sjacobs break; 1402264Sjacobs case 'P': /* page list */ 1412264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1422264Sjacobs "page-ranges", optarg); 1432264Sjacobs break; 1442264Sjacobs case 'S': /* charset */ 1452264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1462264Sjacobs "lp-charset", optarg); 1472264Sjacobs break; 1482264Sjacobs case 'T': /* type */ 1492264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1502264Sjacobs "document-format", 1512264Sjacobs lp_type_to_mime_type(optarg)); 1522264Sjacobs break; 1532264Sjacobs case 'D': /* dump */ 1542264Sjacobs dump = 1; 1552264Sjacobs break; 1562264Sjacobs case 'c': /* copy */ 1572264Sjacobs copy = 1; 1582264Sjacobs break; 1592264Sjacobs case 'd': /* destination */ 1602264Sjacobs printer = optarg; 1612264Sjacobs break; 1622264Sjacobs case 'f': /* form */ 1632264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1642264Sjacobs "media", optarg); 1652264Sjacobs break; 1662264Sjacobs case 'i': /* modify job */ 1672264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1682264Sjacobs (modify < 0)) { 1692264Sjacobs fprintf(stderr, 1702264Sjacobs gettext("invalid request id: %s\n"), 1712264Sjacobs optarg); 1722264Sjacobs exit(1); 1732264Sjacobs } 1742264Sjacobs break; 1752264Sjacobs case 'm': /* mail when complete */ 1762264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1772264Sjacobs "rfc-1179-mail", 1); 1782264Sjacobs break; 1792264Sjacobs case 'n': /* copies */ 1802264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1812264Sjacobs "copies", atoi(optarg)); 1822264Sjacobs break; 1832264Sjacobs case 'o': /* lp "options" */ 1842264Sjacobs papiAttributeListFromString(&list, 1852264Sjacobs PAPI_ATTR_REPLACE, optarg); 1862264Sjacobs break; 1872264Sjacobs case 'p': /* Solaris - notification */ 1882264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1892264Sjacobs "rfc-1179-mail", 1); 1902264Sjacobs break; 1912264Sjacobs case 'q': { /* priority */ 1922264Sjacobs int i = atoi(optarg); 1932264Sjacobs 1942264Sjacobs i = 99 * (39 - i) / 39 + 1; 1952264Sjacobs if ((i < 1) || (i > 100)) { 1962264Sjacobs fprintf(stderr, gettext( 1972264Sjacobs "priority must be between 0 and 39.\n")); 1982264Sjacobs exit(1); 1992264Sjacobs } 2002264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 2012264Sjacobs "priority", i); 2022264Sjacobs } 2032264Sjacobs break; 2042264Sjacobs case 'r': /* "raw" mode */ 2052264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2062264Sjacobs "document-format", 2072264Sjacobs "application/octet-stream"); 2082264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 2092264Sjacobs "stty", "raw"); 2102264Sjacobs break; 2112264Sjacobs case 's': /* suppress message */ 2122264Sjacobs silent = 1; 2132264Sjacobs break; 2142264Sjacobs case 't': /* title */ 2152264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2162264Sjacobs "job-name", optarg); 2172264Sjacobs break; 2182264Sjacobs case 'V': /* validate */ 2192264Sjacobs validate = 1; 2202264Sjacobs break; 2212264Sjacobs case 'w': 2222264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 2232264Sjacobs "rfc-1179-mail", 1); 2242264Sjacobs break; 2252264Sjacobs case 'y': /* lp "modes" */ 2262264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 2272264Sjacobs "lp-modes", optarg); 2282264Sjacobs break; 2292264Sjacobs case 'E': 2302264Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 2312264Sjacobs break; 2322264Sjacobs default: 2332264Sjacobs usage(av[0]); 2342264Sjacobs } 2352264Sjacobs 2362264Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 2372264Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 2382264Sjacobs (void) papiAttributeListDelete(&list, "banner"); 2392264Sjacobs if (b == PAPI_FALSE) 2402264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2412264Sjacobs "job-sheets", "none"); 2422264Sjacobs } 2432264Sjacobs 2442264Sjacobs if ((printer == NULL) && 2452264Sjacobs ((printer = getenv("PRINTER")) == NULL) && 2462264Sjacobs ((printer = getenv("LPDEST")) == NULL)) 2472264Sjacobs printer = DEFAULT_DEST; 2482264Sjacobs 249*2660Sjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 250*2660Sjacobs optind = ac; 251*2660Sjacobs 2522264Sjacobs if (modify == -1) { 2532264Sjacobs char *document_format = "application/octet-stream"; 2542264Sjacobs 2552264Sjacobs #ifdef MAGIC_MIME 2562264Sjacobs if (optind != ac) { 2572264Sjacobs /* get the mime type of the file data */ 2582264Sjacobs magic_t ms = NULL; 2592264Sjacobs 2602264Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 2612264Sjacobs document_format = magic_file(ms, av[optind]); 2622264Sjacobs magic_close(ms); 2632264Sjacobs } 2642264Sjacobs } 2652264Sjacobs #endif 2662264Sjacobs 2672264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2682264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2692264Sjacobs "document-format", document_format); 2702264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2712264Sjacobs "job-sheets", "standard"); 2722264Sjacobs } 2732264Sjacobs 2742264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2752264Sjacobs encryption, NULL); 2762264Sjacobs if (status != PAPI_OK) { 2772264Sjacobs fprintf(stderr, gettext( 2782264Sjacobs "Failed to contact service for %s: %s\n"), printer, 2792264Sjacobs verbose_papi_message(svc, status)); 2802264Sjacobs exit(1); 2812264Sjacobs } 2822264Sjacobs 2832264Sjacobs if (modify != -1) 2842264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2852264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2862264Sjacobs status = jobSubmitSTDIN(svc, printer, list, &job); 2872264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2882264Sjacobs status = papiJobValidate(svc, printer, list, 2892264Sjacobs NULL, &av[optind], &job); 2902264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2912264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2922264Sjacobs NULL, &av[optind], &job); 2932264Sjacobs else /* copy the files before return, -c */ 2942264Sjacobs status = papiJobSubmit(svc, printer, list, 2952264Sjacobs NULL, &av[optind], &job); 2962264Sjacobs 2972264Sjacobs papiAttributeListFree(list); 2982264Sjacobs 2992264Sjacobs if (status != PAPI_OK) { 3002264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 3012264Sjacobs verbose_papi_message(svc, status)); 3022264Sjacobs papiJobFree(job); 3032264Sjacobs papiServiceDestroy(svc); 3042264Sjacobs exit(1); 3052264Sjacobs } 3062264Sjacobs 3072264Sjacobs if (((silent == 0) || (dump != 0)) && 3082264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 3092264Sjacobs int32_t id = 0; 3102264Sjacobs 3112383Sjacobs papiAttributeListGetString(list, NULL, 3122383Sjacobs "printer-name", &printer); 3132264Sjacobs papiAttributeListGetInteger(list, NULL, "job-id", &id); 3142383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 3152264Sjacobs if (ac != optind) 3162264Sjacobs printf("(%d file(s))\n", ac - optind); 3172264Sjacobs else 3182264Sjacobs printf("(standard input)\n"); 3192264Sjacobs 3202264Sjacobs if (dump != 0) { 3212264Sjacobs printf("job attributes:\n"); 3222264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 3232264Sjacobs printf("\n"); 3242264Sjacobs } 3252264Sjacobs } 3262264Sjacobs 3272264Sjacobs papiJobFree(job); 3282264Sjacobs papiServiceDestroy(svc); 3292264Sjacobs 3302264Sjacobs return (0); 3312264Sjacobs } 332