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 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; 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, 902264Sjacobs PAPI_ATTR_EXCL, 912264Sjacobs "job-hold-until", "indefinite"); 922264Sjacobs else if (strcasecmp(optarg, "release") == 0) 932264Sjacobs papiAttributeListAddString(&list, 942264Sjacobs PAPI_ATTR_EXCL, 952264Sjacobs "job-hold-until", "no-hold"); 962264Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 972264Sjacobs papiAttributeListAddInteger(&list, 982264Sjacobs PAPI_ATTR_EXCL, 992264Sjacobs "job-priority", 100); 1002264Sjacobs else 1012264Sjacobs papiAttributeListAddString(&list, 1022264Sjacobs PAPI_ATTR_EXCL, 1032264Sjacobs "job-hold-until", optarg); 1042264Sjacobs break; 1052264Sjacobs case 'P': /* page list */ 1062264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1072264Sjacobs "page-ranges", optarg); 1082264Sjacobs break; 1092264Sjacobs case 'S': /* charset */ 1102264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1112264Sjacobs "lp-charset", optarg); 1122264Sjacobs break; 1132264Sjacobs case 'T': /* type */ 1142264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1152264Sjacobs "document-format", 1162264Sjacobs 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*3125Sjacobs "form", optarg); 1302264Sjacobs break; 1312264Sjacobs case 'i': /* modify job */ 1322264Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 1332264Sjacobs (modify < 0)) { 1342264Sjacobs fprintf(stderr, 1352264Sjacobs gettext("invalid request id: %s\n"), 1362264Sjacobs optarg); 1372264Sjacobs exit(1); 1382264Sjacobs } 1392264Sjacobs break; 1402264Sjacobs case 'm': /* mail when complete */ 1412264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1422264Sjacobs "rfc-1179-mail", 1); 1432264Sjacobs break; 1442264Sjacobs case 'n': /* copies */ 1452264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 1462264Sjacobs "copies", atoi(optarg)); 1472264Sjacobs break; 1482264Sjacobs case 'o': /* lp "options" */ 1492264Sjacobs papiAttributeListFromString(&list, 1502264Sjacobs PAPI_ATTR_REPLACE, optarg); 1512264Sjacobs break; 1522264Sjacobs case 'p': /* Solaris - notification */ 1532264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1542264Sjacobs "rfc-1179-mail", 1); 1552264Sjacobs break; 1562264Sjacobs case 'q': { /* priority */ 1572264Sjacobs int i = atoi(optarg); 1582264Sjacobs 1592264Sjacobs i = 99 * (39 - i) / 39 + 1; 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, 1662264Sjacobs "priority", i); 1672264Sjacobs } 1682264Sjacobs break; 1692264Sjacobs case 'r': /* "raw" mode */ 1702264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1712264Sjacobs "document-format", 1722264Sjacobs "application/octet-stream"); 1732264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1742264Sjacobs "stty", "raw"); 1752264Sjacobs break; 1762264Sjacobs case 's': /* suppress message */ 1772264Sjacobs silent = 1; 1782264Sjacobs break; 1792264Sjacobs case 't': /* title */ 1802264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1812264Sjacobs "job-name", optarg); 1822264Sjacobs break; 1832264Sjacobs case 'V': /* validate */ 1842264Sjacobs validate = 1; 1852264Sjacobs break; 1862264Sjacobs case 'w': 1872264Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 1882264Sjacobs "rfc-1179-mail", 1); 1892264Sjacobs break; 1902264Sjacobs case 'y': /* lp "modes" */ 1912264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 1922264Sjacobs "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, 2062264Sjacobs "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) { 218*3125Sjacobs char *document_format = "text/plain"; 2192264Sjacobs 2202264Sjacobs if (optind != ac) { 2212264Sjacobs /* get the mime type of the file data */ 222*3125Sjacobs #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 } 229*3125Sjacobs #else 230*3125Sjacobs if (is_postscript(av[optind]) == 1) 231*3125Sjacobs document_format = "application/postscript"; 232*3125Sjacobs #endif 2332264Sjacobs } 2342264Sjacobs 2352264Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 2362264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2372264Sjacobs "document-format", document_format); 2382264Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 2392264Sjacobs "job-sheets", "standard"); 2402264Sjacobs } 2412264Sjacobs 2422264Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 2432264Sjacobs encryption, NULL); 2442264Sjacobs if (status != PAPI_OK) { 2452264Sjacobs fprintf(stderr, gettext( 2462264Sjacobs "Failed to contact service for %s: %s\n"), printer, 2472264Sjacobs verbose_papi_message(svc, status)); 2482264Sjacobs exit(1); 2492264Sjacobs } 2502264Sjacobs 251*3125Sjacobs if (dump != 0) { 252*3125Sjacobs printf("requesting attributes:\n"); 253*3125Sjacobs papiAttributeListPrint(stdout, list, "\t"); 254*3125Sjacobs printf("\n"); 255*3125Sjacobs } 256*3125Sjacobs 2572264Sjacobs if (modify != -1) 2582264Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 2592264Sjacobs else if (optind == ac) /* no file list, use stdin */ 2602264Sjacobs status = jobSubmitSTDIN(svc, printer, list, &job); 2612264Sjacobs else if (validate == 1) /* validate the request can be processed */ 2622264Sjacobs status = papiJobValidate(svc, printer, list, 2632264Sjacobs NULL, &av[optind], &job); 2642264Sjacobs else if (copy == 0) /* reference the files in the job, default */ 2652264Sjacobs status = papiJobSubmitByReference(svc, printer, list, 2662264Sjacobs NULL, &av[optind], &job); 2672264Sjacobs else /* copy the files before return, -c */ 2682264Sjacobs status = papiJobSubmit(svc, printer, list, 2692264Sjacobs NULL, &av[optind], &job); 2702264Sjacobs 2712264Sjacobs papiAttributeListFree(list); 2722264Sjacobs 2732264Sjacobs if (status != PAPI_OK) { 2742264Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 2752264Sjacobs verbose_papi_message(svc, status)); 2762264Sjacobs papiJobFree(job); 2772264Sjacobs papiServiceDestroy(svc); 2782264Sjacobs exit(1); 2792264Sjacobs } 2802264Sjacobs 2812264Sjacobs if (((silent == 0) || (dump != 0)) && 2822264Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 2832264Sjacobs int32_t id = 0; 2842264Sjacobs 2852383Sjacobs papiAttributeListGetString(list, NULL, 2862383Sjacobs "printer-name", &printer); 2872264Sjacobs papiAttributeListGetInteger(list, NULL, "job-id", &id); 2882383Sjacobs printf(gettext("request id is %s-%d "), printer, id); 2892264Sjacobs if (ac != optind) 2902264Sjacobs printf("(%d file(s))\n", ac - optind); 2912264Sjacobs else 2922264Sjacobs printf("(standard input)\n"); 2932264Sjacobs 2942264Sjacobs if (dump != 0) { 2952264Sjacobs printf("job attributes:\n"); 2962264Sjacobs papiAttributeListPrint(stdout, list, "\t"); 2972264Sjacobs printf("\n"); 2982264Sjacobs } 2992264Sjacobs } 3002264Sjacobs 3012264Sjacobs papiJobFree(job); 3022264Sjacobs papiServiceDestroy(svc); 3032264Sjacobs 3042264Sjacobs return (0); 3052264Sjacobs } 306