4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
27 /* $Id: lp.c 179 2006-07-17 18:24:07Z njacobs $ */
39 #include <sys/types.h>
40 #ifdef HAVE_LIBMAGIC /* for mimetype auto-detection */
42 #endif /* HAVE_LIBMAGIC */
49 if ((name
= strrchr(program
, '/')) == NULL
)
55 gettext("Usage: %s [-c] [-m] [-p] [-s] [-w] [-d destination] "
56 "[-f form-name] [-H special-handling] [-n number] "
57 "[-o option] [-P page-list] [-q priority-level] "
58 "[-S character-set | print-wheel] [-t title] [-v] "
59 "[-T content-type [-r]] [-y mode-list] [file...]\n"),
65 main(int ac
, char *av
[])
68 papi_service_t svc
= NULL
;
69 papi_attribute_t
**list
= NULL
;
70 papi_encryption_t encryption
= PAPI_ENCRYPT_NEVER
;
71 papi_job_t job
= NULL
;
73 int prefetch_len
= sizeof (prefetch
);
85 (void) setlocale(LC_ALL
, "");
86 (void) textdomain("SUNW_OST_OSCMD");
89 if ((pw
= getpwuid(ruid
)) != NULL
)
90 (void) initgroups(pw
->pw_name
, pw
->pw_gid
);
94 while ((c
= getopt(ac
, av
, "DEH:P:S:T:cd:f:i:mn:o:pq:rst:Vwy:")) != EOF
)
96 case 'H': /* handling */
97 if (strcasecmp(optarg
, "hold") == 0)
98 papiAttributeListAddString(&list
,
100 "job-hold-until", "indefinite");
101 else if (strcasecmp(optarg
, "immediate") == 0)
102 papiAttributeListAddString(&list
,
104 "job-hold-until", "no-hold");
106 papiAttributeListAddString(&list
,
108 "job-hold-until", optarg
);
110 case 'P': { /* page list */
113 snprintf(buf
, sizeof (buf
), "page-ranges=%s", optarg
);
114 papiAttributeListFromString(&list
,
115 PAPI_ATTR_EXCL
, buf
);
118 case 'S': /* charset */
119 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
120 "lp-charset", optarg
);
123 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
125 lp_type_to_mime_type(optarg
));
133 case 'd': /* destination */
137 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
140 case 'i': /* modify job */
141 if ((get_printer_id(optarg
, &printer
, &modify
) < 0) ||
144 gettext("invalid request id: %s\n"),
149 case 'm': /* mail when complete */
150 papiAttributeListAddBoolean(&list
, PAPI_ATTR_EXCL
,
153 case 'n': /* copies */
154 papiAttributeListAddInteger(&list
, PAPI_ATTR_EXCL
,
155 "copies", atoi(optarg
));
157 case 'o': /* lp "options" */
158 papiAttributeListFromString(&list
,
159 PAPI_ATTR_REPLACE
, optarg
);
161 case 'p': /* Solaris - notification */
162 papiAttributeListAddBoolean(&list
, PAPI_ATTR_EXCL
,
165 case 'q': { /* priority */
166 int i
= atoi(optarg
);
169 if ((i
< 1) || (i
> 100)) {
170 fprintf(stderr
, gettext("UX:lp: "));
171 fprintf(stderr
, gettext("ERROR: "));
172 fprintf(stderr
, gettext("Bad priority"
173 " value \"%s\"."), optarg
);
174 fprintf(stderr
, gettext("\n "));
175 fprintf(stderr
, gettext("TO FIX"));
176 fprintf(stderr
, gettext(": "));
177 fprintf(stderr
, gettext("Use an integer value"
179 fprintf(stderr
, gettext("\n"));
182 papiAttributeListAddInteger(&list
, PAPI_ATTR_EXCL
,
186 case 'r': /* "raw" mode */
187 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
189 "application/octet-stream");
190 papiAttributeListAddString(&list
, PAPI_ATTR_APPEND
,
193 case 's': /* suppress message */
196 case 't': /* title */
197 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
200 case 'V': /* validate */
204 papiAttributeListAddBoolean(&list
, PAPI_ATTR_EXCL
,
207 case 'y': /* lp "modes" */
208 papiAttributeListAddString(&list
, PAPI_ATTR_APPEND
,
212 encryption
= PAPI_ENCRYPT_REQUIRED
;
218 /* convert "banner", "nobanner" to "job-sheet" */
219 if (papiAttributeListGetBoolean(list
, NULL
, "banner", &b
) == PAPI_OK
) {
220 (void) papiAttributeListDelete(&list
, "banner");
222 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
223 "job-sheets", "none");
226 if ((printer
== NULL
) &&
227 ((printer
= getenv("PRINTER")) == NULL
) &&
228 ((printer
= getenv("LPDEST")) == NULL
))
229 printer
= DEFAULT_DEST
;
231 if (((optind
+ 1) == ac
) && (strcmp(av
[optind
], "-") == 0))
235 char *document_format
= "text/plain";
238 /* get the mime type of the file data */
242 if ((ms
= magic_open(MAGIC_MIME
)) != NULL
) {
243 document_format
= magic_file(ms
, av
[optind
]);
247 if (is_postscript(av
[optind
]) == 1)
248 document_format
= "application/postscript";
251 if (is_postscript_stream(0, prefetch
, &prefetch_len
)
253 document_format
= "application/postscript";
256 papiAttributeListAddInteger(&list
, PAPI_ATTR_EXCL
, "copies", 1);
257 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
258 "document-format", document_format
);
259 papiAttributeListAddString(&list
, PAPI_ATTR_EXCL
,
260 "job-sheets", "standard");
263 status
= papiServiceCreate(&svc
, printer
, NULL
, NULL
, cli_auth_callback
,
265 if (status
!= PAPI_OK
) {
266 fprintf(stderr
, gettext(
267 "Failed to contact service for %s: %s\n"), printer
,
268 verbose_papi_message(svc
, status
));
273 printf("requesting attributes:\n");
274 papiAttributeListPrint(stdout
, list
, "\t");
279 status
= papiJobModify(svc
, printer
, modify
, list
, &job
);
280 else if (optind
== ac
) /* no file list, use stdin */
281 status
= jobSubmitSTDIN(svc
, printer
, prefetch
, prefetch_len
,
283 else if (validate
== 1) /* validate the request can be processed */
284 status
= papiJobValidate(svc
, printer
, list
,
285 NULL
, &av
[optind
], &job
);
286 else if (copy
== 0) /* reference the files in the job, default */
287 status
= papiJobSubmitByReference(svc
, printer
, list
,
288 NULL
, &av
[optind
], &job
);
289 else /* copy the files before return, -c */
290 status
= papiJobSubmit(svc
, printer
, list
,
291 NULL
, &av
[optind
], &job
);
293 papiAttributeListFree(list
);
295 if (status
!= PAPI_OK
) {
296 fprintf(stderr
, gettext("%s: %s\n"), printer
,
297 verbose_papi_message(svc
, status
));
299 papiServiceDestroy(svc
);
303 if (((silent
== 0) || (dump
!= 0)) &&
304 ((list
= papiJobGetAttributeList(job
)) != NULL
)) {
308 papiAttributeListGetString(list
, NULL
,
309 "printer-name", &printer
);
311 papiAttributeListGetInteger(list
, NULL
,
312 "job-id-requested", &id
);
314 papiAttributeListGetInteger(list
, NULL
, "job-id", &id
);
317 printf(gettext("request id is %s-%d "), printer
, id
);
319 printf("(%d file(s))\n", ac
- optind
);
321 printf("(standard input)\n");
324 printf("job attributes:\n");
325 papiAttributeListPrint(stdout
, list
, "\t");
331 papiServiceDestroy(svc
);