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 /* 23324104abSsonam gupta - Sun Microsystems - Bangalore India * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 24355b4669Sjacobs * 25355b4669Sjacobs */ 26355b4669Sjacobs 27179184d3Sjacobs /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */ 28355b4669Sjacobs 29355b4669Sjacobs #include <stdio.h> 30355b4669Sjacobs #include <stdlib.h> 31355b4669Sjacobs #include <unistd.h> 32355b4669Sjacobs #include <string.h> 33355b4669Sjacobs #include <locale.h> 34355b4669Sjacobs #include <libintl.h> 35355b4669Sjacobs #include <papi.h> 36355b4669Sjacobs #include "common.h" 3705f9a513SRalph Turner - Sun UK - Contractor #include <pwd.h> 3805f9a513SRalph Turner - Sun UK - Contractor #include <grp.h> 3905f9a513SRalph Turner - Sun UK - Contractor #include <sys/types.h> 40355b4669Sjacobs #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */ 41355b4669Sjacobs #include <magic.h> 42355b4669Sjacobs #endif /* HAVE_LIBMAGIC */ 43355b4669Sjacobs 44355b4669Sjacobs static void 45355b4669Sjacobs usage(char *program) 46355b4669Sjacobs { 47355b4669Sjacobs char *name; 48355b4669Sjacobs 49355b4669Sjacobs if ((name = strrchr(program, '/')) == NULL) 50355b4669Sjacobs name = program; 51355b4669Sjacobs else 52355b4669Sjacobs name++; 53355b4669Sjacobs 54355b4669Sjacobs fprintf(stdout, 55355b4669Sjacobs gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] " 56355b4669Sjacobs "[-f form-name] [-H special-handling] [-n number] " 57355b4669Sjacobs "[-o option] [-P page-list] [-q priority-level] " 58355b4669Sjacobs "[-S character-set | print-wheel] [-t title] [-v] " 59355b4669Sjacobs "[-T content-type [-r]] [-y mode-list] [file...]\n"), 60355b4669Sjacobs name); 61355b4669Sjacobs exit(1); 62355b4669Sjacobs } 63355b4669Sjacobs 64355b4669Sjacobs int 65355b4669Sjacobs main(int ac, char *av[]) 66355b4669Sjacobs { 67355b4669Sjacobs papi_status_t status; 68355b4669Sjacobs papi_service_t svc = NULL; 69355b4669Sjacobs papi_attribute_t **list = NULL; 70355b4669Sjacobs papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 71355b4669Sjacobs papi_job_t job = NULL; 72c1ecd8b9Sjacobs char prefetch[3]; 73c1ecd8b9Sjacobs int prefetch_len = sizeof (prefetch); 74355b4669Sjacobs char *printer = NULL; 75355b4669Sjacobs char b = PAPI_TRUE; 76355b4669Sjacobs int copy = 0; 77355b4669Sjacobs int silent = 0; 78355b4669Sjacobs int dump = 0; 79355b4669Sjacobs int validate = 0; 80355b4669Sjacobs int modify = -1; 81355b4669Sjacobs int c; 8205f9a513SRalph Turner - Sun UK - Contractor uid_t ruid; 8305f9a513SRalph Turner - Sun UK - Contractor struct passwd *pw; 84355b4669Sjacobs 85355b4669Sjacobs (void) setlocale(LC_ALL, ""); 86355b4669Sjacobs (void) textdomain("SUNW_OST_OSCMD"); 87355b4669Sjacobs 8805f9a513SRalph Turner - Sun UK - Contractor ruid = getuid(); 8905f9a513SRalph Turner - Sun UK - Contractor if ((pw = getpwuid(ruid)) != NULL) 9005f9a513SRalph Turner - Sun UK - Contractor (void) initgroups(pw->pw_name, pw->pw_gid); 9105f9a513SRalph Turner - Sun UK - Contractor (void) setuid(ruid); 9205f9a513SRalph Turner - Sun UK - Contractor 9305f9a513SRalph Turner - Sun UK - Contractor 94355b4669Sjacobs while ((c = getopt(ac, av, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF) 95355b4669Sjacobs switch (c) { 96355b4669Sjacobs case 'H': /* handling */ 97355b4669Sjacobs if (strcasecmp(optarg, "hold") == 0) 98355b4669Sjacobs papiAttributeListAddString(&list, 99355b4669Sjacobs PAPI_ATTR_EXCL, 100355b4669Sjacobs "job-hold-until", "indefinite"); 10143b9c050Sjacobs else if (strcasecmp(optarg, "immediate") == 0) 102355b4669Sjacobs papiAttributeListAddString(&list, 103355b4669Sjacobs PAPI_ATTR_EXCL, 104355b4669Sjacobs "job-hold-until", "no-hold"); 105355b4669Sjacobs else 106355b4669Sjacobs papiAttributeListAddString(&list, 107355b4669Sjacobs PAPI_ATTR_EXCL, 108355b4669Sjacobs "job-hold-until", optarg); 109355b4669Sjacobs break; 11043b9c050Sjacobs case 'P': { /* page list */ 11143b9c050Sjacobs char buf[BUFSIZ]; 11243b9c050Sjacobs 11343b9c050Sjacobs snprintf(buf, sizeof (buf), "page-ranges=%s", optarg); 11443b9c050Sjacobs papiAttributeListFromString(&list, 11543b9c050Sjacobs PAPI_ATTR_EXCL, buf); 11643b9c050Sjacobs } 117355b4669Sjacobs break; 118355b4669Sjacobs case 'S': /* charset */ 119355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 120355b4669Sjacobs "lp-charset", optarg); 121355b4669Sjacobs break; 122355b4669Sjacobs case 'T': /* type */ 123355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 124355b4669Sjacobs "document-format", 125355b4669Sjacobs lp_type_to_mime_type(optarg)); 126355b4669Sjacobs break; 127355b4669Sjacobs case 'D': /* dump */ 128355b4669Sjacobs dump = 1; 129355b4669Sjacobs break; 130355b4669Sjacobs case 'c': /* copy */ 131355b4669Sjacobs copy = 1; 132355b4669Sjacobs break; 133355b4669Sjacobs case 'd': /* destination */ 134355b4669Sjacobs printer = optarg; 135355b4669Sjacobs break; 136355b4669Sjacobs case 'f': /* form */ 137355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 1380a44ef6dSjacobs "form", optarg); 139355b4669Sjacobs break; 140355b4669Sjacobs case 'i': /* modify job */ 141355b4669Sjacobs if ((get_printer_id(optarg, &printer, &modify) < 0) || 142355b4669Sjacobs (modify < 0)) { 143355b4669Sjacobs fprintf(stderr, 144355b4669Sjacobs gettext("invalid request id: %s\n"), 145355b4669Sjacobs optarg); 146355b4669Sjacobs exit(1); 147355b4669Sjacobs } 148355b4669Sjacobs break; 149355b4669Sjacobs case 'm': /* mail when complete */ 150355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 151355b4669Sjacobs "rfc-1179-mail", 1); 152355b4669Sjacobs break; 153355b4669Sjacobs case 'n': /* copies */ 154355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 155355b4669Sjacobs "copies", atoi(optarg)); 156355b4669Sjacobs break; 157355b4669Sjacobs case 'o': /* lp "options" */ 158355b4669Sjacobs papiAttributeListFromString(&list, 159355b4669Sjacobs PAPI_ATTR_REPLACE, optarg); 160355b4669Sjacobs break; 161355b4669Sjacobs case 'p': /* Solaris - notification */ 162355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 163355b4669Sjacobs "rfc-1179-mail", 1); 164355b4669Sjacobs break; 165355b4669Sjacobs case 'q': { /* priority */ 166355b4669Sjacobs int i = atoi(optarg); 167355b4669Sjacobs 16843b9c050Sjacobs i = 100 - (i * 2.5); 169355b4669Sjacobs if ((i < 1) || (i > 100)) { 170*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("UX:lp: ")); 171*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("ERROR: ")); 172*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("Bad priority" 173*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India " value \"%s\"."), optarg); 174*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("\n ")); 175*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("TO FIX")); 176*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext(": ")); 177*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("Use an integer value" 178*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India " from 0 to 39.")); 179*a1a4c722Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("\n")); 180355b4669Sjacobs exit(1); 181355b4669Sjacobs } 182355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, 18343b9c050Sjacobs "job-priority", i); 184355b4669Sjacobs } 185355b4669Sjacobs break; 186355b4669Sjacobs case 'r': /* "raw" mode */ 187355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 188355b4669Sjacobs "document-format", 189355b4669Sjacobs "application/octet-stream"); 190355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 191355b4669Sjacobs "stty", "raw"); 192355b4669Sjacobs break; 193355b4669Sjacobs case 's': /* suppress message */ 194355b4669Sjacobs silent = 1; 195355b4669Sjacobs break; 196355b4669Sjacobs case 't': /* title */ 197355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 198355b4669Sjacobs "job-name", optarg); 199355b4669Sjacobs break; 200355b4669Sjacobs case 'V': /* validate */ 201355b4669Sjacobs validate = 1; 202355b4669Sjacobs break; 203355b4669Sjacobs case 'w': 204355b4669Sjacobs papiAttributeListAddBoolean(&list, PAPI_ATTR_EXCL, 205355b4669Sjacobs "rfc-1179-mail", 1); 206355b4669Sjacobs break; 207355b4669Sjacobs case 'y': /* lp "modes" */ 208355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_APPEND, 209355b4669Sjacobs "lp-modes", optarg); 210355b4669Sjacobs break; 211355b4669Sjacobs case 'E': 212355b4669Sjacobs encryption = PAPI_ENCRYPT_REQUIRED; 213355b4669Sjacobs break; 214355b4669Sjacobs default: 215355b4669Sjacobs usage(av[0]); 216355b4669Sjacobs } 217355b4669Sjacobs 218355b4669Sjacobs /* convert "banner", "nobanner" to "job-sheet" */ 219355b4669Sjacobs if (papiAttributeListGetBoolean(list, NULL, "banner", &b) == PAPI_OK) { 220355b4669Sjacobs (void) papiAttributeListDelete(&list, "banner"); 221355b4669Sjacobs if (b == PAPI_FALSE) 222355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 223355b4669Sjacobs "job-sheets", "none"); 224355b4669Sjacobs } 225355b4669Sjacobs 226355b4669Sjacobs if ((printer == NULL) && 227355b4669Sjacobs ((printer = getenv("PRINTER")) == NULL) && 228355b4669Sjacobs ((printer = getenv("LPDEST")) == NULL)) 229355b4669Sjacobs printer = DEFAULT_DEST; 230355b4669Sjacobs 231e2738c5eSjacobs if (((optind + 1) == ac) && (strcmp(av[optind], "-") == 0)) 232e2738c5eSjacobs optind = ac; 233e2738c5eSjacobs 234355b4669Sjacobs if (modify == -1) { 2350a44ef6dSjacobs char *document_format = "text/plain"; 236355b4669Sjacobs 237355b4669Sjacobs if (optind != ac) { 238355b4669Sjacobs /* get the mime type of the file data */ 2390a44ef6dSjacobs #ifdef MAGIC_MIME 240355b4669Sjacobs magic_t ms = NULL; 241355b4669Sjacobs 242355b4669Sjacobs if ((ms = magic_open(MAGIC_MIME)) != NULL) { 243355b4669Sjacobs document_format = magic_file(ms, av[optind]); 244355b4669Sjacobs magic_close(ms); 245355b4669Sjacobs } 2460a44ef6dSjacobs #else 2470a44ef6dSjacobs if (is_postscript(av[optind]) == 1) 2480a44ef6dSjacobs document_format = "application/postscript"; 249355b4669Sjacobs #endif 250c1ecd8b9Sjacobs } else { 251c1ecd8b9Sjacobs if (is_postscript_stream(0, prefetch, &prefetch_len) 252c1ecd8b9Sjacobs == 1) 253c1ecd8b9Sjacobs document_format = "application/postscript"; 2540a44ef6dSjacobs } 255355b4669Sjacobs 256355b4669Sjacobs papiAttributeListAddInteger(&list, PAPI_ATTR_EXCL, "copies", 1); 257355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 258355b4669Sjacobs "document-format", document_format); 259355b4669Sjacobs papiAttributeListAddString(&list, PAPI_ATTR_EXCL, 260355b4669Sjacobs "job-sheets", "standard"); 261355b4669Sjacobs } 262355b4669Sjacobs 263355b4669Sjacobs status = papiServiceCreate(&svc, printer, NULL, NULL, cli_auth_callback, 264355b4669Sjacobs encryption, NULL); 265355b4669Sjacobs if (status != PAPI_OK) { 266355b4669Sjacobs fprintf(stderr, gettext( 267355b4669Sjacobs "Failed to contact service for %s: %s\n"), printer, 268355b4669Sjacobs verbose_papi_message(svc, status)); 269355b4669Sjacobs exit(1); 270355b4669Sjacobs } 271355b4669Sjacobs 2720a44ef6dSjacobs if (dump != 0) { 2730a44ef6dSjacobs printf("requesting attributes:\n"); 2740a44ef6dSjacobs papiAttributeListPrint(stdout, list, "\t"); 2750a44ef6dSjacobs printf("\n"); 2760a44ef6dSjacobs } 2770a44ef6dSjacobs 278355b4669Sjacobs if (modify != -1) 279355b4669Sjacobs status = papiJobModify(svc, printer, modify, list, &job); 280355b4669Sjacobs else if (optind == ac) /* no file list, use stdin */ 281c1ecd8b9Sjacobs status = jobSubmitSTDIN(svc, printer, prefetch, prefetch_len, 282c1ecd8b9Sjacobs list, &job); 283355b4669Sjacobs else if (validate == 1) /* validate the request can be processed */ 284355b4669Sjacobs status = papiJobValidate(svc, printer, list, 285355b4669Sjacobs NULL, &av[optind], &job); 286355b4669Sjacobs else if (copy == 0) /* reference the files in the job, default */ 287355b4669Sjacobs status = papiJobSubmitByReference(svc, printer, list, 288355b4669Sjacobs NULL, &av[optind], &job); 289355b4669Sjacobs else /* copy the files before return, -c */ 290355b4669Sjacobs status = papiJobSubmit(svc, printer, list, 291355b4669Sjacobs NULL, &av[optind], &job); 292355b4669Sjacobs 293355b4669Sjacobs papiAttributeListFree(list); 294355b4669Sjacobs 295355b4669Sjacobs if (status != PAPI_OK) { 296355b4669Sjacobs fprintf(stderr, gettext("%s: %s\n"), printer, 297355b4669Sjacobs verbose_papi_message(svc, status)); 298355b4669Sjacobs papiJobFree(job); 299355b4669Sjacobs papiServiceDestroy(svc); 300355b4669Sjacobs exit(1); 301355b4669Sjacobs } 302355b4669Sjacobs 303355b4669Sjacobs if (((silent == 0) || (dump != 0)) && 304355b4669Sjacobs ((list = papiJobGetAttributeList(job)) != NULL)) { 305324104abSsonam gupta - Sun Microsystems - Bangalore India int32_t id = -1; 306355b4669Sjacobs 30735a603adSsonam gupta - Sun Microsystems - Bangalore India if (printer == NULL) 308179184d3Sjacobs papiAttributeListGetString(list, NULL, 309179184d3Sjacobs "printer-name", &printer); 31035a603adSsonam gupta - Sun Microsystems - Bangalore India 311324104abSsonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 312324104abSsonam gupta - Sun Microsystems - Bangalore India "job-id-requested", &id); 313324104abSsonam gupta - Sun Microsystems - Bangalore India if (id == -1) { 314355b4669Sjacobs papiAttributeListGetInteger(list, NULL, "job-id", &id); 315324104abSsonam gupta - Sun Microsystems - Bangalore India } 316324104abSsonam gupta - Sun Microsystems - Bangalore India 317179184d3Sjacobs printf(gettext("request id is %s-%d "), printer, id); 318355b4669Sjacobs if (ac != optind) 319355b4669Sjacobs printf("(%d file(s))\n", ac - optind); 320355b4669Sjacobs else 321355b4669Sjacobs printf("(standard input)\n"); 322355b4669Sjacobs 323355b4669Sjacobs if (dump != 0) { 324355b4669Sjacobs printf("job attributes:\n"); 325355b4669Sjacobs papiAttributeListPrint(stdout, list, "\t"); 326355b4669Sjacobs printf("\n"); 327355b4669Sjacobs } 328355b4669Sjacobs } 329355b4669Sjacobs 330355b4669Sjacobs papiJobFree(job); 331355b4669Sjacobs papiServiceDestroy(svc); 332355b4669Sjacobs 333355b4669Sjacobs return (0); 334355b4669Sjacobs } 335