1355b4669Sjacobs /* 2355b4669Sjacobs * CDDL HEADER START 3355b4669Sjacobs * 4355b4669Sjacobs * The contents of this file are subject to the terms of the 5355b4669Sjacobs * Common Development and Distribution License (the "License"). 6355b4669Sjacobs * You may not use this file except in compliance with the License. 7355b4669Sjacobs * 8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing. 10355b4669Sjacobs * See the License for the specific language governing permissions 11355b4669Sjacobs * and limitations under the License. 12355b4669Sjacobs * 13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each 14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the 16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying 17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner] 18355b4669Sjacobs * 19355b4669Sjacobs * CDDL HEADER END 20355b4669Sjacobs */ 21355b4669Sjacobs 22355b4669Sjacobs /* 23355b4669Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24355b4669Sjacobs * Use is subject to license terms. 25355b4669Sjacobs * 26355b4669Sjacobs */ 27355b4669Sjacobs 28179184d3Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */ 29355b4669Sjacobs 30355b4669Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 31355b4669Sjacobs 32355b4669Sjacobs #include <stdio.h> 33355b4669Sjacobs #include <stdlib.h> 34355b4669Sjacobs #include <unistd.h> 35355b4669Sjacobs #include <string.h> 36355b4669Sjacobs #include <locale.h> 37355b4669Sjacobs #include <libintl.h> 38355b4669Sjacobs #include <papi.h> 39355b4669Sjacobs #include "common.h" 40355b4669Sjacobs 41355b4669Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 42355b4669Sjacobs #include <magic.h> 43355b4669Sjacobs #endif /* HAVE_LIBMAGIC */ 44355b4669Sjacobs 45355b4669Sjacobs static void 46355b4669Sjacobs usage(char *program) 47355b4669Sjacobs { 48355b4669Sjacobs char *name; 49355b4669Sjacobs 50355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL) 51355b4669Sjacobs name = program; 52355b4669Sjacobs else 53355b4669Sjacobs name++; 54355b4669Sjacobs 55355b4669Sjacobs fprintf(stdout, 56355b4669Sjacobs gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 57355b4669Sjacobs "[-f form-name] [-H special-handling] [-n number] " 58355b4669Sjacobs "[-o option] [-P page-list] [-q priority-level] " 59355b4669Sjacobs "[-S character-set | print-wheel] [-t title] [-v] " 60355b4669Sjacobs "[-T content-type [-r]] [-y mode-list] [file...]\n"), 61355b4669Sjacobs name); 62355b4669Sjacobs exit(1); 63355b4669Sjacobs } 64355b4669Sjacobs 65355b4669Sjacobs int 66355b4669Sjacobs main(int ac, char *av[]) 67355b4669Sjacobs { 68355b4669Sjacobs papi_status_t status; 69355b4669Sjacobs papi_service_t svc = NULL; 70355b4669Sjacobs papi_attribute_t **list = NULL; 71355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 72355b4669Sjacobs papi_job_t job = NULL; 73355b4669Sjacobs char *printer = NULL; 74355b4669Sjacobs char b = PAPI_TRUE; 75355b4669Sjacobs int copy = 0; 76355b4669Sjacobs int silent = 0; 77355b4669Sjacobs int dump = 0; 78355b4669Sjacobs int validate = 0; 79355b4669Sjacobs int modify = -1; 80355b4669Sjacobs int c; 81355b4669Sjacobs 82355b4669Sjacobs (void) setlocale(LC_ALL, ""); 83355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 84355b4669Sjacobs 85355b4669Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 86355b4669Sjacobs switch (c) { 87355b4669Sjacobs case 'H': /* handling */ 88355b4669Sjacobs if (strcasecmp(optarg, "hold") == 0) 89355b4669Sjacobs papiAttributeListAddString(&list, 90355b4669Sjacobs PAPI_ATTR_EXCL, 91355b4669Sjacobs "job-hold-until", "indefinite"); 92355b4669Sjacobs else if (strcasecmp(optarg, "release") == 0) 93355b4669Sjacobs papiAttributeListAddString(&list, 94355b4669Sjacobs PAPI_ATTR_EXCL, 95355b4669Sjacobs "job-hold-until", "no-hold"); 96355b4669Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 97355b4669Sjacobs papiAttributeListAddInteger(&list, 98355b4669Sjacobs PAPI_ATTR_EXCL, 99355b4669Sjacobs "job-priority", 100); 100355b4669Sjacobs else 101355b4669Sjacobs papiAttributeListAddString(&list, 102355b4669Sjacobs PAPI_ATTR_EXCL, 103355b4669Sjacobs "job-hold-until", optarg); 104355b4669Sjacobs break; 105355b4669Sjacobs case 'P': /* page list */ 106355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 107355b4669Sjacobs "page-ranges", optarg); 108355b4669Sjacobs break; 109355b4669Sjacobs case 'S': /* charset */ 110355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 111355b4669Sjacobs "lp-charset", optarg); 112355b4669Sjacobs break; 113355b4669Sjacobs case 'T': /* type */ 114355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 115355b4669Sjacobs "document-format", 116355b4669Sjacobs lp_type_to_mime_type(optarg)); 117355b4669Sjacobs break; 118355b4669Sjacobs case 'D': /* dump */ 119355b4669Sjacobs dump = 1; 120355b4669Sjacobs break; 121355b4669Sjacobs case 'c': /* copy */ 122355b4669Sjacobs copy = 1; 123355b4669Sjacobs break; 124355b4669Sjacobs case 'd': /* destination */ 125355b4669Sjacobs printer = optarg; 126355b4669Sjacobs break; 127355b4669Sjacobs case 'f': /* form */ 128355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 129*0a44ef6dSjacobs "form", optarg); 130355b4669Sjacobs break; 131355b4669Sjacobs case 'i': /* modify job */ 132355b4669Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 133355b4669Sjacobs (modify < 0)) { 134355b4669Sjacobs fprintf(stderr, 135355b4669Sjacobs gettext("invalid request id: %s\n"), 136355b4669Sjacobs optarg); 137355b4669Sjacobs exit(1); 138355b4669Sjacobs } 139355b4669Sjacobs break; 140355b4669Sjacobs case 'm': /* mail when complete */ 141355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 142355b4669Sjacobs "rfc-1179-mail", 1); 143355b4669Sjacobs break; 144355b4669Sjacobs case 'n': /* copies */ 145355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 146355b4669Sjacobs "copies", atoi(optarg)); 147355b4669Sjacobs break; 148355b4669Sjacobs case 'o': /* lp "options" */ 149355b4669Sjacobs papiAttributeListFromString(&list, 150355b4669Sjacobs PAPI_ATTR_REPLACE, optarg); 151355b4669Sjacobs break; 152355b4669Sjacobs case 'p': /* Solaris - notification */ 153355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 154355b4669Sjacobs "rfc-1179-mail", 1); 155355b4669Sjacobs break; 156355b4669Sjacobs case 'q': { /* priority */ 157355b4669Sjacobs int i = atoi(optarg); 158355b4669Sjacobs 159355b4669Sjacobs i = 99 * (39 - i) / 39 + 1; 160355b4669Sjacobs if ((i < 1) || (i > 100)) { 161355b4669Sjacobs fprintf(stderr, gettext( 162355b4669Sjacobs "priority must be between 0 and 39.\n")); 163355b4669Sjacobs exit(1); 164355b4669Sjacobs } 165355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 166355b4669Sjacobs "priority", i); 167355b4669Sjacobs } 168355b4669Sjacobs break; 169355b4669Sjacobs case 'r': /* "raw" mode */ 170355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 171355b4669Sjacobs "document-format", 172355b4669Sjacobs "application/octet-stream"); 173355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 174355b4669Sjacobs "stty", "raw"); 175355b4669Sjacobs break; 176355b4669Sjacobs case 's': /* suppress message */ 177355b4669Sjacobs silent = 1; 178355b4669Sjacobs break; 179355b4669Sjacobs case 't': /* title */ 180355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 181355b4669Sjacobs "job-name", optarg); 182355b4669Sjacobs break; 183355b4669Sjacobs case 'V': /* validate */ 184355b4669Sjacobs validate = 1; 185355b4669Sjacobs break; 186355b4669Sjacobs case 'w': 187355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 188355b4669Sjacobs "rfc-1179-mail", 1); 189355b4669Sjacobs break; 190355b4669Sjacobs case 'y': /* lp "modes" */ 191355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 192355b4669Sjacobs "lp-modes", optarg); 193355b4669Sjacobs break; 194355b4669Sjacobs case 'E': 195355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 196355b4669Sjacobs break; 197355b4669Sjacobs default: 198355b4669Sjacobs usage(av[0]); 199355b4669Sjacobs } 200355b4669Sjacobs 201355b4669Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 202355b4669Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 203355b4669Sjacobs (void) papiAttributeListDelete(&list, "banner"); 204355b4669Sjacobs if (b == PAPI_FALSE) 205355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 206355b4669Sjacobs "job-sheets", "none"); 207355b4669Sjacobs } 208355b4669Sjacobs 209355b4669Sjacobs if ((printer == NULL) && 210355b4669Sjacobs ((printer = getenv("PRINTER")) == NULL) && 211355b4669Sjacobs ((printer = getenv("LPDEST")) == NULL)) 212355b4669Sjacobs printer = DEFAULT_DEST; 213355b4669Sjacobs 214e2738c5eSjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 215e2738c5eSjacobs optind = ac; 216e2738c5eSjacobs 217355b4669Sjacobs if (modify == -1) { 218*0a44ef6dSjacobs char *document_format = "text/plain"; 219355b4669Sjacobs 220355b4669Sjacobs if (optind != ac) { 221355b4669Sjacobs /* get the mime type of the file data */ 222*0a44ef6dSjacobs #ifdef MAGIC_MIME 223355b4669Sjacobs magic_t ms = NULL; 224355b4669Sjacobs 225355b4669Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 226355b4669Sjacobs document_format = magic_file(ms, av[optind]); 227355b4669Sjacobs magic_close(ms); 228355b4669Sjacobs } 229*0a44ef6dSjacobs #else 230*0a44ef6dSjacobs if (is_postscript(av[optind]) == 1) 231*0a44ef6dSjacobs document_format = "application/postscript"; 232355b4669Sjacobs #endif 233*0a44ef6dSjacobs } 234355b4669Sjacobs 235355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 236355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 237355b4669Sjacobs "document-format", document_format); 238355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 239355b4669Sjacobs "job-sheets", "standard"); 240355b4669Sjacobs } 241355b4669Sjacobs 242355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 243355b4669Sjacobs encryption, NULL); 244355b4669Sjacobs if (status != PAPI_OK) { 245355b4669Sjacobs fprintf(stderr, gettext( 246355b4669Sjacobs "Failed to contact service for %s: %s\n"), printer, 247355b4669Sjacobs verbose_papi_message(svc, status)); 248355b4669Sjacobs exit(1); 249355b4669Sjacobs } 250355b4669Sjacobs 251*0a44ef6dSjacobs if (dump != 0) { 252*0a44ef6dSjacobs printf("requesting attributes:\n"); 253*0a44ef6dSjacobs papiAttributeListPrint(stdout, list, "\t"); 254*0a44ef6dSjacobs printf("\n"); 255*0a44ef6dSjacobs } 256*0a44ef6dSjacobs 257355b4669Sjacobs if (modify != -1) 258355b4669Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 259355b4669Sjacobs else if (optind == ac) /* no file list, use stdin */ 260355b4669Sjacobs status = jobSubmitSTDIN(svc, printer, list, &job); 261355b4669Sjacobs else if (validate == 1) /* validate the request can be processed */ 262355b4669Sjacobs status = papiJobValidate(svc, printer, list, 263355b4669Sjacobs NULL, &av[optind], &job); 264355b4669Sjacobs else if (copy == 0) /* reference the files in the job, default */ 265355b4669Sjacobs status = papiJobSubmitByReference(svc, printer, list, 266355b4669Sjacobs NULL, &av[optind], &job); 267355b4669Sjacobs else /* copy the files before return, -c */ 268355b4669Sjacobs status = papiJobSubmit(svc, printer, list, 269355b4669Sjacobs NULL, &av[optind], &job); 270355b4669Sjacobs 271355b4669Sjacobs papiAttributeListFree(list); 272355b4669Sjacobs 273355b4669Sjacobs if (status != PAPI_OK) { 274355b4669Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 275355b4669Sjacobs verbose_papi_message(svc, status)); 276355b4669Sjacobs papiJobFree(job); 277355b4669Sjacobs papiServiceDestroy(svc); 278355b4669Sjacobs exit(1); 279355b4669Sjacobs } 280355b4669Sjacobs 281355b4669Sjacobs if (((silent == 0) || (dump != 0)) && 282355b4669Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 283355b4669Sjacobs int32_t id = 0; 284355b4669Sjacobs 285179184d3Sjacobs papiAttributeListGetString(list, NULL, 286179184d3Sjacobs "printer-name", &printer); 287355b4669Sjacobs papiAttributeListGetInteger(list, NULL, "job-id", &id); 288179184d3Sjacobs printf(gettext("request id is %s-%d "), printer, id); 289355b4669Sjacobs if (ac != optind) 290355b4669Sjacobs printf("(%d file(s))\n", ac - optind); 291355b4669Sjacobs else 292355b4669Sjacobs printf("(standard input)\n"); 293355b4669Sjacobs 294355b4669Sjacobs if (dump != 0) { 295355b4669Sjacobs printf("job attributes:\n"); 296355b4669Sjacobs papiAttributeListPrint(stdout, list, "\t"); 297355b4669Sjacobs printf("\n"); 298355b4669Sjacobs } 299355b4669Sjacobs } 300355b4669Sjacobs 301355b4669Sjacobs papiJobFree(job); 302355b4669Sjacobs papiServiceDestroy(svc); 303355b4669Sjacobs 304355b4669Sjacobs return (0); 305355b4669Sjacobs } 306