xref: /titanic_54/usr/src/cmd/print/bsd-sysv-commands/common.c (revision c1ecd8b9404ee0d96d93f02e82c441b9bb149a3d)
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 /*
2336615d24Sjacobs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24355b4669Sjacobs  * Use is subject to license terms.
25355b4669Sjacobs  *
26355b4669Sjacobs  */
27355b4669Sjacobs 
28355b4669Sjacobs /* $Id: common.c 162 2006-05-08 14:17:44Z njacobs $ */
29355b4669Sjacobs 
30355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
31355b4669Sjacobs 
32355b4669Sjacobs #include <stdio.h>
33355b4669Sjacobs #include <stdlib.h>
34355b4669Sjacobs #include <unistd.h>
350a44ef6dSjacobs #include <sys/types.h>
360a44ef6dSjacobs #include <sys/stat.h>
370a44ef6dSjacobs #include <fcntl.h>
38355b4669Sjacobs #include <alloca.h>
39355b4669Sjacobs #include <string.h>
40355b4669Sjacobs #include <libintl.h>
41355b4669Sjacobs #include <ctype.h>
42355b4669Sjacobs #include <pwd.h>
43355b4669Sjacobs #include <papi.h>
44355b4669Sjacobs #include "common.h"
45355b4669Sjacobs 
46355b4669Sjacobs #ifndef HAVE_GETPASSPHRASE	/* some systems don't have getpassphrase() */
47355b4669Sjacobs #define	getpassphrase getpass
48355b4669Sjacobs #endif
49355b4669Sjacobs 
50355b4669Sjacobs /* give the most verbose error message to the caller */
51355b4669Sjacobs char *
52355b4669Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status)
53355b4669Sjacobs {
54355b4669Sjacobs 	char *mesg;
55355b4669Sjacobs 
56355b4669Sjacobs 	mesg = papiServiceGetStatusMessage(svc);
57355b4669Sjacobs 
58355b4669Sjacobs 	if (mesg == NULL)
59355b4669Sjacobs 		mesg = papiStatusString(status);
60355b4669Sjacobs 
61355b4669Sjacobs 	return (mesg);
62355b4669Sjacobs }
63355b4669Sjacobs 
64355b4669Sjacobs static int
65355b4669Sjacobs match_job(int id, char *user, int ac, char *av[])
66355b4669Sjacobs {
67355b4669Sjacobs 	int i;
68355b4669Sjacobs 
69355b4669Sjacobs 	for (i = 0; i < ac; i++)
70355b4669Sjacobs 		if (strcmp("-", av[i]) == 0)
71355b4669Sjacobs 			return (0);	/* "current" user match */
72355b4669Sjacobs 		else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i])))
73355b4669Sjacobs 			return (0);	/* job-id match */
74355b4669Sjacobs 		else if (strcmp(user, av[i]) == 0)
75355b4669Sjacobs 			return (0);	/* user match */
76355b4669Sjacobs 
77355b4669Sjacobs 	return (-1);
78355b4669Sjacobs }
79355b4669Sjacobs 
800a44ef6dSjacobs static struct {
810a44ef6dSjacobs 	char *mime_type;
820a44ef6dSjacobs 	char *lp_type;
830a44ef6dSjacobs } type_map[] = {
840a44ef6dSjacobs 	{ "text/plain", "simple" },
850a44ef6dSjacobs 	{ "application/octet-stream", "raw" },
860a44ef6dSjacobs 	{ "application/octet-stream", "any" },
870a44ef6dSjacobs 	{ "application/postscript", "postscript" },
880a44ef6dSjacobs 	{ "application/postscript", "ps" },
890a44ef6dSjacobs 	{ "application/x-cif", "cif" },
900a44ef6dSjacobs 	{ "application/x-dvi", "dvi" },
910a44ef6dSjacobs 	{ "application/x-plot", "plot" },
920a44ef6dSjacobs 	{ "application/x-ditroff", "troff" },
930a44ef6dSjacobs 	{ "application/x-troff", "otroff" },
940a44ef6dSjacobs 	{ "application/x-pr", "pr" },
950a44ef6dSjacobs 	{ "application/x-fortran", "fortran" },
960a44ef6dSjacobs 	{ "application/x-raster", "raster" },
970a44ef6dSjacobs 	{ NULL, NULL}
980a44ef6dSjacobs };
990a44ef6dSjacobs 
1000a44ef6dSjacobs char *
1010a44ef6dSjacobs lp_type_to_mime_type(char *lp_type)
1020a44ef6dSjacobs {
1030a44ef6dSjacobs 	int i;
1040a44ef6dSjacobs 
1050a44ef6dSjacobs 	if (lp_type == NULL)
1060a44ef6dSjacobs 		return ("application/octet-stream");
1070a44ef6dSjacobs 
1080a44ef6dSjacobs 	for (i = 0; type_map[i].lp_type != NULL; i++)
1090a44ef6dSjacobs 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1100a44ef6dSjacobs 			return (type_map[i].mime_type);
1110a44ef6dSjacobs 
1120a44ef6dSjacobs 	return (lp_type);
1130a44ef6dSjacobs }
1140a44ef6dSjacobs 
115355b4669Sjacobs /*
116355b4669Sjacobs  * to support job/printer status
117355b4669Sjacobs  */
118355b4669Sjacobs static char *
119355b4669Sjacobs state_string(int state)
120355b4669Sjacobs {
121355b4669Sjacobs 	switch (state) {
122355b4669Sjacobs 	case 3:
123355b4669Sjacobs 		return (gettext("idle"));
124355b4669Sjacobs 	case 4:
125355b4669Sjacobs 		return (gettext("processing"));
126355b4669Sjacobs 	case 5:
127355b4669Sjacobs 		return (gettext("stopped"));
128355b4669Sjacobs 	default:
129355b4669Sjacobs 		return (gettext("unknown"));
130355b4669Sjacobs 	}
131355b4669Sjacobs }
132355b4669Sjacobs 
133355b4669Sjacobs static char *_rank_suffixes[] = {
134355b4669Sjacobs 	"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
135355b4669Sjacobs };
136355b4669Sjacobs 
137355b4669Sjacobs static char *
138355b4669Sjacobs rank_string(const int rank)
139355b4669Sjacobs {
140355b4669Sjacobs 	static char buf[12];
141355b4669Sjacobs 
142355b4669Sjacobs 	if (rank < 0)
143355b4669Sjacobs 		snprintf(buf, sizeof (buf), gettext("invalid"));
144355b4669Sjacobs 	else if (rank == 0)
145355b4669Sjacobs 		snprintf(buf, sizeof (buf), gettext("active"));
146355b4669Sjacobs 	else if ((rank > 10) && (rank < 14))
147355b4669Sjacobs 		sprintf(buf, "%dth", rank);
148355b4669Sjacobs 	else
149355b4669Sjacobs 		sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
150355b4669Sjacobs 
151355b4669Sjacobs 	return (buf);
152355b4669Sjacobs }
153355b4669Sjacobs 
154355b4669Sjacobs static void
155355b4669Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
156355b4669Sjacobs {
157355b4669Sjacobs 	papi_attribute_t **list = papiPrinterGetAttributeList(p);
158355b4669Sjacobs 	int state = 0;
159355b4669Sjacobs 	char *reason = "";
160355b4669Sjacobs 
161355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
162355b4669Sjacobs 				"printer-state", &state);
163355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
164355b4669Sjacobs 				"printer-state-reasons", &reason);
165355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
166355b4669Sjacobs 				"printer-name", &name);
167355b4669Sjacobs 
168355b4669Sjacobs 	if ((state != 0x03) || (num_jobs != 0)) {
169355b4669Sjacobs 		fprintf(fp, "%s: %s", name, state_string(state));
170355b4669Sjacobs 		if (state == 0x05) /* stopped */
171355b4669Sjacobs 			fprintf(fp, ": %s\n", reason);
172355b4669Sjacobs 		else
173355b4669Sjacobs 			fprintf(fp, "\n");
174355b4669Sjacobs 	} else
175355b4669Sjacobs 		fprintf(fp, "no entries\n");
176355b4669Sjacobs }
177355b4669Sjacobs 
178355b4669Sjacobs static void
179355b4669Sjacobs print_header(FILE *fp)
180355b4669Sjacobs {
181355b4669Sjacobs 	fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
182355b4669Sjacobs }
183355b4669Sjacobs 
184355b4669Sjacobs static void
185355b4669Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
186355b4669Sjacobs {
187355b4669Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
188355b4669Sjacobs 	int copies = 1, id = 0, rank = count, size = 0;
189355b4669Sjacobs 	char *name = "print job";
190355b4669Sjacobs 	char *user = "nobody";
191355b4669Sjacobs 	char *host = "localhost";
192355b4669Sjacobs 	char *suffix = "k";
193355b4669Sjacobs 
194355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
195355b4669Sjacobs 					"job-id", &id);
19636615d24Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
19736615d24Sjacobs 					"job-id-requested", &id);
198355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
199355b4669Sjacobs 					"job-originating-user-name", &user);
2000a44ef6dSjacobs 	(void) papiAttributeListGetString(list, NULL,
2010a44ef6dSjacobs 					"job-originating-host-name", &host);
202355b4669Sjacobs 
203355b4669Sjacobs 	/* if we are looking and it doesn't match, return early */
204355b4669Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0))
205355b4669Sjacobs 		return;
206355b4669Sjacobs 
207355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
208355b4669Sjacobs 					"copies", &copies);
209355b4669Sjacobs 	(void) papiAttributeListGetInteger(list, NULL,
210355b4669Sjacobs 					"number-of-intervening-jobs", &rank);
211355b4669Sjacobs 
212355b4669Sjacobs 	if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
213355b4669Sjacobs 			== PAPI_OK)
214355b4669Sjacobs 		suffix = "bytes";
215355b4669Sjacobs 	else
216355b4669Sjacobs 		(void) papiAttributeListGetInteger(list, NULL,
217355b4669Sjacobs 					"job-k-octets", &size);
218355b4669Sjacobs 	(void) papiAttributeListGetString(list, NULL,
219355b4669Sjacobs 					"job-name", &name);
220355b4669Sjacobs 
221355b4669Sjacobs 	size *= copies;
222355b4669Sjacobs 
223355b4669Sjacobs 	if (fmt == 3) {
224355b4669Sjacobs 		fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
225355b4669Sjacobs 			rank_string(++rank), user, id, name, size, suffix);
226355b4669Sjacobs 	} else
227355b4669Sjacobs 		fprintf(fp, gettext(
228355b4669Sjacobs 			"\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
229355b4669Sjacobs 			user, rank_string(++rank), id, host, name, size,
230355b4669Sjacobs 			suffix);
231355b4669Sjacobs }
232355b4669Sjacobs 
233355b4669Sjacobs /*
234355b4669Sjacobs  * to support job cancelation
235355b4669Sjacobs  */
236355b4669Sjacobs static void
237355b4669Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
238355b4669Sjacobs 		int ac, char *av[])
239355b4669Sjacobs {
240355b4669Sjacobs 	papi_status_t status;
241355b4669Sjacobs 	papi_attribute_t **list = papiJobGetAttributeList(job);
242355b4669Sjacobs 	int id = 0;
24336615d24Sjacobs 	int rid = 0;
244355b4669Sjacobs 	char *user = "";
245355b4669Sjacobs 	char *mesg = gettext("cancelled");
246355b4669Sjacobs 
247355b4669Sjacobs 	papiAttributeListGetInteger(list, NULL,
248355b4669Sjacobs 					"job-id", &id);
24936615d24Sjacobs 	papiAttributeListGetInteger(list, NULL,
25036615d24Sjacobs 					"job-id-requested", &rid);
251355b4669Sjacobs 	papiAttributeListGetString(list, NULL,
252355b4669Sjacobs 					"job-originating-user-name", &user);
253355b4669Sjacobs 
254355b4669Sjacobs 	/* if we are looking and it doesn't match, return early */
25536615d24Sjacobs 	if ((ac > 0) && (match_job(id, user, ac, av) < 0) &&
25636615d24Sjacobs 	    (match_job(rid, user, ac, av) < 0))
257355b4669Sjacobs 		return;
258355b4669Sjacobs 
259355b4669Sjacobs 	status = papiJobCancel(svc, printer, id);
260355b4669Sjacobs 	if (status != PAPI_OK)
261355b4669Sjacobs 		mesg = papiStatusString(status);
262355b4669Sjacobs 
263355b4669Sjacobs 	fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
264355b4669Sjacobs }
265355b4669Sjacobs 
266355b4669Sjacobs int
267355b4669Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
268355b4669Sjacobs 		int ac, char *av[])
269355b4669Sjacobs {
270355b4669Sjacobs 	papi_status_t status;
271355b4669Sjacobs 	papi_printer_t p = NULL;
272355b4669Sjacobs 	papi_job_t *jobs = NULL;
273355b4669Sjacobs 	char *pattrs[] = { "printer-name", "printer-state",
274355b4669Sjacobs 			"printer-state-reasons", NULL };
2750a44ef6dSjacobs 	char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
27636615d24Sjacobs 			"job-originating-user-name", "job-id-requested",
2770a44ef6dSjacobs 			"job-originating-host-name",
278355b4669Sjacobs 			"number-of-intervening-jobs", NULL };
279355b4669Sjacobs 	int num_jobs = 0;
280355b4669Sjacobs 
281355b4669Sjacobs 	status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
282355b4669Sjacobs 	if (status != PAPI_OK) {
283355b4669Sjacobs 		fprintf(fp, gettext(
284355b4669Sjacobs 			"Failed to query service for state of %s: %s\n"),
285355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
286355b4669Sjacobs 		return (-1);
287355b4669Sjacobs 	}
288355b4669Sjacobs 
289355b4669Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
290355b4669Sjacobs 					0, &jobs);
291355b4669Sjacobs 	if (status != PAPI_OK) {
292355b4669Sjacobs 		fprintf(fp, gettext(
293355b4669Sjacobs 			"Failed to query service for jobs on %s: %s\n"),
294355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
295355b4669Sjacobs 		return (-1);
296355b4669Sjacobs 	}
297355b4669Sjacobs 	if (jobs != NULL) {
298355b4669Sjacobs 		while (jobs[num_jobs] != NULL)
299355b4669Sjacobs 			num_jobs++;
300355b4669Sjacobs 	}
301355b4669Sjacobs 
302355b4669Sjacobs 	printer_state_line(fp, p, num_jobs, dest);
303355b4669Sjacobs 	if (num_jobs > 0) {
304355b4669Sjacobs 		int i;
305355b4669Sjacobs 
306355b4669Sjacobs 		if (fmt == 3)
307355b4669Sjacobs 			print_header(fp);
308355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
309355b4669Sjacobs 			print_job_line(fp, i, jobs[i], fmt, ac, av);
310355b4669Sjacobs 	}
311355b4669Sjacobs 
312355b4669Sjacobs 	papiPrinterFree(p);
313355b4669Sjacobs 	papiJobListFree(jobs);
314355b4669Sjacobs 
315355b4669Sjacobs 	return (num_jobs);
316355b4669Sjacobs }
317355b4669Sjacobs 
318355b4669Sjacobs int
319355b4669Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
320355b4669Sjacobs 		int ac, char *av[])
321355b4669Sjacobs {
322355b4669Sjacobs 	papi_status_t status;
323355b4669Sjacobs 	papi_job_t *jobs = NULL;
32436615d24Sjacobs 	char *jattrs[] = { "job-originating-user-name", "job-id",
32536615d24Sjacobs 			"job-id-requested", NULL };
326355b4669Sjacobs 
327355b4669Sjacobs 	status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
328355b4669Sjacobs 					0, &jobs);
329355b4669Sjacobs 
330355b4669Sjacobs 	if (status != PAPI_OK) {
331355b4669Sjacobs 		fprintf(fp, gettext("Failed to query service for %s: %s\n"),
332355b4669Sjacobs 			dest, verbose_papi_message(svc, status));
333355b4669Sjacobs 		return (-1);
334355b4669Sjacobs 	}
335355b4669Sjacobs 
336355b4669Sjacobs 	/* cancel the job(s) */
337355b4669Sjacobs 	if (jobs != NULL) {
338355b4669Sjacobs 		int i;
339355b4669Sjacobs 
340355b4669Sjacobs 		for (i = 0; jobs[i] != NULL; i++)
341355b4669Sjacobs 			cancel_job(svc, fp, dest, jobs[i], ac, av);
342355b4669Sjacobs 	}
343355b4669Sjacobs 
344355b4669Sjacobs 	papiJobListFree(jobs);
345355b4669Sjacobs 
346355b4669Sjacobs 	return (0);
347355b4669Sjacobs }
348355b4669Sjacobs 
349355b4669Sjacobs int
350355b4669Sjacobs get_printer_id(char *name, char **printer, int *id)
351355b4669Sjacobs {
352355b4669Sjacobs 	int result = -1;
353355b4669Sjacobs 
354355b4669Sjacobs 	if (name != NULL) {
355355b4669Sjacobs 		char *p = strrchr(name, '-');
356355b4669Sjacobs 
357355b4669Sjacobs 		*printer = name;
358355b4669Sjacobs 		if (p != NULL) {
359355b4669Sjacobs 			char *s = NULL;
360355b4669Sjacobs 
361355b4669Sjacobs 			*id = strtol(p + 1, &s, 10);
362355b4669Sjacobs 			if (s[0] != '\0')
363355b4669Sjacobs 				*id = -1;
364355b4669Sjacobs 			else
365355b4669Sjacobs 				*p = '\0';
366355b4669Sjacobs 			result = 0;
367355b4669Sjacobs 		} else
368355b4669Sjacobs 			*id = -1;
369355b4669Sjacobs 	}
370355b4669Sjacobs 
371355b4669Sjacobs 	return (result);
372355b4669Sjacobs }
373355b4669Sjacobs 
374355b4669Sjacobs /*
375355b4669Sjacobs  * strsplit() splits a string into a NULL terminated array of substrings
376355b4669Sjacobs  * determined by a seperator.  The original string is modified, and newly
377355b4669Sjacobs  * allocated space is only returned for the array itself.  If more than
378355b4669Sjacobs  * 1024 substrings exist, they will be ignored.
379355b4669Sjacobs  */
380355b4669Sjacobs char **
381355b4669Sjacobs strsplit(char *string, const char *seperators)
382355b4669Sjacobs {
383355b4669Sjacobs 	char	*list[BUFSIZ],
384355b4669Sjacobs 		**result;
385355b4669Sjacobs 	int	length = 0;
386355b4669Sjacobs 
387355b4669Sjacobs 	if ((string == NULL) || (seperators == NULL))
388355b4669Sjacobs 		return (NULL);
389355b4669Sjacobs 
390355b4669Sjacobs 	(void) memset(list, 0, sizeof (list));
391355b4669Sjacobs 	for (list[length] = strtok(string, seperators);
392355b4669Sjacobs 		(list[length] != NULL) && (length < (BUFSIZ - 2));
393355b4669Sjacobs 		list[length] = strtok(NULL, seperators))
394355b4669Sjacobs 			length++;
395355b4669Sjacobs 
396355b4669Sjacobs 	if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
397355b4669Sjacobs 		(void) memcpy(result, list, length * sizeof (char *));
398355b4669Sjacobs 
399355b4669Sjacobs 	return (result);
400355b4669Sjacobs }
401355b4669Sjacobs 
402355b4669Sjacobs papi_status_t
403*c1ecd8b9Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len,
404*c1ecd8b9Sjacobs 		papi_attribute_t **list, papi_job_t *job)
405355b4669Sjacobs {
406355b4669Sjacobs 	papi_status_t status;
407355b4669Sjacobs 	papi_stream_t stream = NULL;
408355b4669Sjacobs 	int rc;
409355b4669Sjacobs 	char buf[BUFSIZ];
410355b4669Sjacobs 
411355b4669Sjacobs 	status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
412*c1ecd8b9Sjacobs 
413*c1ecd8b9Sjacobs 	if (len > 0)
414*c1ecd8b9Sjacobs 		status = papiJobStreamWrite(svc, stream, prefetch, len);
415*c1ecd8b9Sjacobs 
416355b4669Sjacobs 	while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
417355b4669Sjacobs 		status = papiJobStreamWrite(svc, stream, buf, rc);
418355b4669Sjacobs 
419355b4669Sjacobs 	if (status == PAPI_OK)
420355b4669Sjacobs 		status = papiJobStreamClose(svc, stream, job);
421355b4669Sjacobs 
422355b4669Sjacobs 	return (status);
423355b4669Sjacobs }
424355b4669Sjacobs 
4250a44ef6dSjacobs /*
4260a44ef6dSjacobs  * is_postscript() will detect if the file passed in contains postscript
4270a44ef6dSjacobs  * data.  A one is returned if the file contains postscript, zero is returned
4280a44ef6dSjacobs  * if the file is not postscript, and -1 is returned if an error occurs
4290a44ef6dSjacobs  */
4300a44ef6dSjacobs #define	PS_MAGIC	"%!"
4310a44ef6dSjacobs #define	PC_PS_MAGIC	"^D%!"
4320a44ef6dSjacobs int
433*c1ecd8b9Sjacobs is_postscript_stream(int fd, char *buf, int *len)
4340a44ef6dSjacobs {
435*c1ecd8b9Sjacobs 	if ((*len = read(fd, buf, *len)) < 0) {
4360a44ef6dSjacobs 		close(fd);
4370a44ef6dSjacobs 		return (-1);
4380a44ef6dSjacobs 	}
4390a44ef6dSjacobs 
4400a44ef6dSjacobs 	if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
4410a44ef6dSjacobs 	    (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
4420a44ef6dSjacobs 		return (1);
4430a44ef6dSjacobs 	else
4440a44ef6dSjacobs 		return (0);
4450a44ef6dSjacobs }
4460a44ef6dSjacobs 
447*c1ecd8b9Sjacobs int
448*c1ecd8b9Sjacobs is_postscript(const char *file)
449*c1ecd8b9Sjacobs {
450*c1ecd8b9Sjacobs 	int rc = -1;
451*c1ecd8b9Sjacobs 	int fd;
452*c1ecd8b9Sjacobs 
453*c1ecd8b9Sjacobs 	if ((fd = open(file, O_RDONLY)) >= 0) {
454*c1ecd8b9Sjacobs 		char buf[3];
455*c1ecd8b9Sjacobs 		int len = sizeof (buf);
456*c1ecd8b9Sjacobs 
457*c1ecd8b9Sjacobs 		rc = is_postscript_stream(fd, buf, &len);
458*c1ecd8b9Sjacobs 		close(fd);
459*c1ecd8b9Sjacobs 	}
460*c1ecd8b9Sjacobs 
461*c1ecd8b9Sjacobs 	return (rc);
462*c1ecd8b9Sjacobs }
463*c1ecd8b9Sjacobs 
464355b4669Sjacobs static char **
465355b4669Sjacobs all_list(papi_service_t svc)
466355b4669Sjacobs {
467355b4669Sjacobs 	papi_status_t status;
468355b4669Sjacobs 	papi_printer_t printer = NULL;
469355b4669Sjacobs 	char *list[] = { "member-names", NULL };
470355b4669Sjacobs 	char **result = NULL;
471355b4669Sjacobs 
472355b4669Sjacobs 	status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
473355b4669Sjacobs 	if ((status == PAPI_OK) && (printer != NULL)) {
474355b4669Sjacobs 		papi_attribute_t **attributes =
475355b4669Sjacobs 					papiPrinterGetAttributeList(printer);
476355b4669Sjacobs 		if (attributes != NULL) {
477355b4669Sjacobs 			void *iter = NULL;
478355b4669Sjacobs 			char *value = NULL;
479355b4669Sjacobs 
480355b4669Sjacobs 			for (status = papiAttributeListGetString(attributes,
481355b4669Sjacobs 						&iter, "member-names", &value);
482355b4669Sjacobs 				status == PAPI_OK;
483355b4669Sjacobs 				status = papiAttributeListGetString(attributes,
484355b4669Sjacobs 						&iter, NULL, &value))
485355b4669Sjacobs 					list_append(&result, strdup(value));
486355b4669Sjacobs 		}
487355b4669Sjacobs 		papiPrinterFree(printer);
488355b4669Sjacobs 	}
489355b4669Sjacobs 
490355b4669Sjacobs 	return (result);
491355b4669Sjacobs }
492355b4669Sjacobs 
493355b4669Sjacobs static char **
494355b4669Sjacobs printers_list(papi_service_t svc)
495355b4669Sjacobs {
496355b4669Sjacobs 	papi_status_t status;
497355b4669Sjacobs 	papi_printer_t *printers = NULL;
498355b4669Sjacobs 	char *keys[] = { "printer-name", NULL };
499355b4669Sjacobs 	char **result = NULL;
500355b4669Sjacobs 
501355b4669Sjacobs 	status = papiPrintersList(svc, keys, NULL, &printers);
502355b4669Sjacobs 	if ((status == PAPI_OK) && (printers != NULL)) {
503355b4669Sjacobs 		int i;
504355b4669Sjacobs 
505355b4669Sjacobs 		for (i = 0; printers[i] != NULL; i++) {
506355b4669Sjacobs 			papi_attribute_t **attributes =
507355b4669Sjacobs 				papiPrinterGetAttributeList(printers[i]);
508355b4669Sjacobs 			char *name = NULL;
509355b4669Sjacobs 
510355b4669Sjacobs 			(void) papiAttributeListGetString(attributes, NULL,
511355b4669Sjacobs 						"printer-name", &name);
512355b4669Sjacobs 			if ((name != NULL) && (strcmp(name, "_default") != 0))
513355b4669Sjacobs 				list_append(&result, strdup(name));
514355b4669Sjacobs 		}
515355b4669Sjacobs 		papiPrinterListFree(printers);
516355b4669Sjacobs 	}
517355b4669Sjacobs 
518355b4669Sjacobs 	return (result);
519355b4669Sjacobs }
520355b4669Sjacobs 
521355b4669Sjacobs char **
522355b4669Sjacobs interest_list(papi_service_t svc)
523355b4669Sjacobs {
524355b4669Sjacobs 	static char been_here;
525355b4669Sjacobs 	static char **result;
526355b4669Sjacobs 
527355b4669Sjacobs 	if (been_here == 0) {	/* only do this once */
528355b4669Sjacobs 		been_here = 1;
529355b4669Sjacobs 
530355b4669Sjacobs 		if ((result = all_list(svc)) == NULL)
531355b4669Sjacobs 			result = printers_list(svc);
532355b4669Sjacobs 	}
533355b4669Sjacobs 
534355b4669Sjacobs 	return (result);
535355b4669Sjacobs }
536355b4669Sjacobs 
537355b4669Sjacobs char *
538355b4669Sjacobs localhostname()
539355b4669Sjacobs {
540355b4669Sjacobs 	static char *result;
541355b4669Sjacobs 
542355b4669Sjacobs 	if (result == NULL) {
543355b4669Sjacobs 		static char buf[256];
544355b4669Sjacobs 
545355b4669Sjacobs 		if (gethostname(buf, sizeof (buf)) == 0)
546355b4669Sjacobs 			result = buf;
547355b4669Sjacobs 	}
548355b4669Sjacobs 
549355b4669Sjacobs 	return (result);
550355b4669Sjacobs }
551355b4669Sjacobs 
552355b4669Sjacobs int
553355b4669Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
554355b4669Sjacobs {
555355b4669Sjacobs 	char prompt[BUFSIZ];
556355b4669Sjacobs 	char *user, *svc_name, *passphrase;
557355b4669Sjacobs 
558355b4669Sjacobs 	/* get the name of the service we are contacting */
559355b4669Sjacobs 	if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
560355b4669Sjacobs 		return (-1);
561355b4669Sjacobs 
562355b4669Sjacobs 	/* find our who we are supposed to be */
563355b4669Sjacobs 	if ((user = papiServiceGetUserName(svc)) == NULL) {
564355b4669Sjacobs 		struct passwd *pw;
565355b4669Sjacobs 
566355b4669Sjacobs 		if ((pw = getpwuid(getuid())) != NULL)
567355b4669Sjacobs 			user = pw->pw_name;
568355b4669Sjacobs 		else
569355b4669Sjacobs 			user = "nobody";
570355b4669Sjacobs 	}
571355b4669Sjacobs 
572355b4669Sjacobs 	/* build the prompt string */
573355b4669Sjacobs 	snprintf(prompt, sizeof (prompt),
574355b4669Sjacobs 		gettext("passphrase for %s to access %s: "), user, svc_name);
575355b4669Sjacobs 
576355b4669Sjacobs 	/* ask for the passphrase */
577355b4669Sjacobs 	if ((passphrase = getpassphrase(prompt)) != NULL)
578355b4669Sjacobs 		papiServiceSetPassword(svc, passphrase);
579355b4669Sjacobs 
580355b4669Sjacobs 	return (0);
581355b4669Sjacobs }
582