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 /*
23*12028SSonam.Gupta@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
242264Sjacobs * Use is subject to license terms.
252264Sjacobs *
262264Sjacobs */
272264Sjacobs
282264Sjacobs /* $Id: common.c 162 2006-05-08 14:17:44Z njacobs $ */
292264Sjacobs
302264Sjacobs #include <stdio.h>
312264Sjacobs #include <stdlib.h>
322264Sjacobs #include <unistd.h>
333125Sjacobs #include <sys/types.h>
343125Sjacobs #include <sys/stat.h>
353125Sjacobs #include <fcntl.h>
362264Sjacobs #include <alloca.h>
372264Sjacobs #include <string.h>
382264Sjacobs #include <libintl.h>
392264Sjacobs #include <ctype.h>
402264Sjacobs #include <pwd.h>
412264Sjacobs #include <papi.h>
422264Sjacobs #include "common.h"
432264Sjacobs
442264Sjacobs #ifndef HAVE_GETPASSPHRASE /* some systems don't have getpassphrase() */
452264Sjacobs #define getpassphrase getpass
462264Sjacobs #endif
472264Sjacobs
482264Sjacobs /* give the most verbose error message to the caller */
492264Sjacobs char *
verbose_papi_message(papi_service_t svc,papi_status_t status)502264Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status)
512264Sjacobs {
522264Sjacobs char *mesg;
532264Sjacobs
542264Sjacobs mesg = papiServiceGetStatusMessage(svc);
552264Sjacobs
562264Sjacobs if (mesg == NULL)
572264Sjacobs mesg = papiStatusString(status);
582264Sjacobs
592264Sjacobs return (mesg);
602264Sjacobs }
612264Sjacobs
622264Sjacobs static int
match_job(int id,char * user,int ac,char * av[])632264Sjacobs match_job(int id, char *user, int ac, char *av[])
642264Sjacobs {
652264Sjacobs int i;
662264Sjacobs
672264Sjacobs for (i = 0; i < ac; i++)
682264Sjacobs if (strcmp("-", av[i]) == 0)
692264Sjacobs return (0); /* "current" user match */
702264Sjacobs else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i])))
712264Sjacobs return (0); /* job-id match */
722264Sjacobs else if (strcmp(user, av[i]) == 0)
732264Sjacobs return (0); /* user match */
742264Sjacobs
752264Sjacobs return (-1);
762264Sjacobs }
772264Sjacobs
7811397SSonam.Gupta@Sun.COM /*
7911397SSonam.Gupta@Sun.COM * return 0 : argument passed is job-id && job-id matches
8011397SSonam.Gupta@Sun.COM * or argument passed is user
8111397SSonam.Gupta@Sun.COM */
8211397SSonam.Gupta@Sun.COM static int
match_job_rid(int id,int ac,char * av[])8311397SSonam.Gupta@Sun.COM match_job_rid(int id, int ac, char *av[])
8411397SSonam.Gupta@Sun.COM {
8511397SSonam.Gupta@Sun.COM int i;
8611397SSonam.Gupta@Sun.COM
8711397SSonam.Gupta@Sun.COM for (i = 0; i < ac; i++)
8811397SSonam.Gupta@Sun.COM if (isdigit(av[i][0]) != 0) {
8911397SSonam.Gupta@Sun.COM if (id == atoi(av[i]))
9011397SSonam.Gupta@Sun.COM /* job-id match */
9111397SSonam.Gupta@Sun.COM return (0);
9211397SSonam.Gupta@Sun.COM } else
9311397SSonam.Gupta@Sun.COM /* argument passed is user */
9411397SSonam.Gupta@Sun.COM return (0);
9511397SSonam.Gupta@Sun.COM return (-1);
9611397SSonam.Gupta@Sun.COM }
9711397SSonam.Gupta@Sun.COM
983125Sjacobs static struct {
993125Sjacobs char *mime_type;
1003125Sjacobs char *lp_type;
1013125Sjacobs } type_map[] = {
1023125Sjacobs { "text/plain", "simple" },
1033125Sjacobs { "application/octet-stream", "raw" },
1043125Sjacobs { "application/octet-stream", "any" },
1053125Sjacobs { "application/postscript", "postscript" },
1063125Sjacobs { "application/postscript", "ps" },
1073125Sjacobs { "application/x-cif", "cif" },
1083125Sjacobs { "application/x-dvi", "dvi" },
1093125Sjacobs { "application/x-plot", "plot" },
1103125Sjacobs { "application/x-ditroff", "troff" },
1113125Sjacobs { "application/x-troff", "otroff" },
1123125Sjacobs { "application/x-pr", "pr" },
1133125Sjacobs { "application/x-fortran", "fortran" },
1143125Sjacobs { "application/x-raster", "raster" },
1153125Sjacobs { NULL, NULL}
1163125Sjacobs };
1173125Sjacobs
1183125Sjacobs char *
lp_type_to_mime_type(char * lp_type)1193125Sjacobs lp_type_to_mime_type(char *lp_type)
1203125Sjacobs {
1213125Sjacobs int i;
1223125Sjacobs
1233125Sjacobs if (lp_type == NULL)
1243125Sjacobs return ("application/octet-stream");
1253125Sjacobs
1263125Sjacobs for (i = 0; type_map[i].lp_type != NULL; i++)
1273125Sjacobs if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1283125Sjacobs return (type_map[i].mime_type);
1293125Sjacobs
1303125Sjacobs return (lp_type);
1313125Sjacobs }
1323125Sjacobs
1332264Sjacobs /*
1342264Sjacobs * to support job/printer status
1352264Sjacobs */
1362264Sjacobs static char *
state_string(int state)1372264Sjacobs state_string(int state)
1382264Sjacobs {
1392264Sjacobs switch (state) {
1402264Sjacobs case 3:
1412264Sjacobs return (gettext("idle"));
1422264Sjacobs case 4:
1432264Sjacobs return (gettext("processing"));
1442264Sjacobs case 5:
1452264Sjacobs return (gettext("stopped"));
1462264Sjacobs default:
1472264Sjacobs return (gettext("unknown"));
1482264Sjacobs }
1492264Sjacobs }
1502264Sjacobs
1512264Sjacobs static char *_rank_suffixes[] = {
1522264Sjacobs "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"
1532264Sjacobs };
1542264Sjacobs
1552264Sjacobs static char *
rank_string(const int rank)1562264Sjacobs rank_string(const int rank)
1572264Sjacobs {
1582264Sjacobs static char buf[12];
1592264Sjacobs
1602264Sjacobs if (rank < 0)
1612264Sjacobs snprintf(buf, sizeof (buf), gettext("invalid"));
1622264Sjacobs else if (rank == 0)
1632264Sjacobs snprintf(buf, sizeof (buf), gettext("active"));
1642264Sjacobs else if ((rank > 10) && (rank < 14))
1652264Sjacobs sprintf(buf, "%dth", rank);
1662264Sjacobs else
1672264Sjacobs sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]);
1682264Sjacobs
1692264Sjacobs return (buf);
1702264Sjacobs }
1712264Sjacobs
1722264Sjacobs static void
printer_state_line(FILE * fp,papi_printer_t p,int num_jobs,char * name)1732264Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name)
1742264Sjacobs {
1752264Sjacobs papi_attribute_t **list = papiPrinterGetAttributeList(p);
1762264Sjacobs int state = 0;
1772264Sjacobs char *reason = "";
1782264Sjacobs
1792264Sjacobs (void) papiAttributeListGetInteger(list, NULL,
1808966SSonam.Gupta@Sun.COM "printer-state", &state);
1812264Sjacobs (void) papiAttributeListGetString(list, NULL,
1828966SSonam.Gupta@Sun.COM "printer-state-reasons", &reason);
1832264Sjacobs (void) papiAttributeListGetString(list, NULL,
1848966SSonam.Gupta@Sun.COM "printer-name", &name);
1852264Sjacobs
1862264Sjacobs if ((state != 0x03) || (num_jobs != 0)) {
1872264Sjacobs fprintf(fp, "%s: %s", name, state_string(state));
188*12028SSonam.Gupta@Sun.COM if ((state == 0x05) ||
189*12028SSonam.Gupta@Sun.COM (state == 0x06) ||
190*12028SSonam.Gupta@Sun.COM (state == 0x07) ||
191*12028SSonam.Gupta@Sun.COM (state == 0x08)) /* stopped */
1922264Sjacobs fprintf(fp, ": %s\n", reason);
1932264Sjacobs else
1942264Sjacobs fprintf(fp, "\n");
1952264Sjacobs } else
1962264Sjacobs fprintf(fp, "no entries\n");
1972264Sjacobs }
1982264Sjacobs
1992264Sjacobs static void
print_header(FILE * fp)2002264Sjacobs print_header(FILE *fp)
2012264Sjacobs {
2022264Sjacobs fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n"));
2032264Sjacobs }
2042264Sjacobs
2052264Sjacobs static void
print_job_line(FILE * fp,int count,papi_job_t job,int fmt,int ac,char * av[])2062264Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[])
2072264Sjacobs {
2082264Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job);
2092264Sjacobs int copies = 1, id = 0, rank = count, size = 0;
2102264Sjacobs char *name = "print job";
2112264Sjacobs char *user = "nobody";
2122264Sjacobs char *host = "localhost";
2132264Sjacobs char *suffix = "k";
2142264Sjacobs
2152264Sjacobs (void) papiAttributeListGetInteger(list, NULL,
2168966SSonam.Gupta@Sun.COM "job-id", &id);
2176817Sjacobs (void) papiAttributeListGetInteger(list, NULL,
2188966SSonam.Gupta@Sun.COM "job-id-requested", &id);
2192264Sjacobs (void) papiAttributeListGetString(list, NULL,
2208966SSonam.Gupta@Sun.COM "job-originating-user-name", &user);
2213125Sjacobs (void) papiAttributeListGetString(list, NULL,
2228966SSonam.Gupta@Sun.COM "job-originating-host-name", &host);
2232264Sjacobs
2242264Sjacobs /* if we are looking and it doesn't match, return early */
2252264Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0))
2262264Sjacobs return;
2272264Sjacobs
2282264Sjacobs (void) papiAttributeListGetInteger(list, NULL,
2298966SSonam.Gupta@Sun.COM "copies", &copies);
2302264Sjacobs (void) papiAttributeListGetInteger(list, NULL,
2318966SSonam.Gupta@Sun.COM "number-of-intervening-jobs", &rank);
2322264Sjacobs
2332264Sjacobs if (papiAttributeListGetInteger(list, NULL, "job-octets", &size)
2348966SSonam.Gupta@Sun.COM == PAPI_OK)
2352264Sjacobs suffix = "bytes";
2362264Sjacobs else
2372264Sjacobs (void) papiAttributeListGetInteger(list, NULL,
2388966SSonam.Gupta@Sun.COM "job-k-octets", &size);
2392264Sjacobs (void) papiAttributeListGetString(list, NULL,
2408966SSonam.Gupta@Sun.COM "job-name", &name);
2412264Sjacobs
2422264Sjacobs size *= copies;
2432264Sjacobs
2442264Sjacobs if (fmt == 3) {
2452264Sjacobs fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"),
2468966SSonam.Gupta@Sun.COM rank_string(++rank), user, id, name, size, suffix);
2472264Sjacobs } else
2482264Sjacobs fprintf(fp, gettext(
2498966SSonam.Gupta@Sun.COM "\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"),
2508966SSonam.Gupta@Sun.COM user, rank_string(++rank), id, host, name, size,
2518966SSonam.Gupta@Sun.COM suffix);
2522264Sjacobs }
2532264Sjacobs
2542264Sjacobs /*
2552264Sjacobs * to support job cancelation
2562264Sjacobs */
2572264Sjacobs static void
cancel_job(papi_service_t svc,FILE * fp,char * printer,papi_job_t job,int ac,char * av[])2582264Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job,
2592264Sjacobs int ac, char *av[])
2602264Sjacobs {
2612264Sjacobs papi_status_t status;
2622264Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job);
26311397SSonam.Gupta@Sun.COM int id = -1;
26410472SSonam.Gupta@Sun.COM int rid = -1;
2652264Sjacobs char *user = "";
2662264Sjacobs char *mesg = gettext("cancelled");
26711397SSonam.Gupta@Sun.COM int i = 0;
2682264Sjacobs
2692264Sjacobs papiAttributeListGetInteger(list, NULL,
2708966SSonam.Gupta@Sun.COM "job-id", &id);
2716817Sjacobs papiAttributeListGetInteger(list, NULL,
2728966SSonam.Gupta@Sun.COM "job-id-requested", &rid);
2732264Sjacobs papiAttributeListGetString(list, NULL,
2748966SSonam.Gupta@Sun.COM "job-originating-user-name", &user);
2752264Sjacobs
2762264Sjacobs /* if we are looking and it doesn't match, return early */
2776817Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0) &&
2786817Sjacobs (match_job(rid, user, ac, av) < 0))
2792264Sjacobs return;
2802264Sjacobs
28111397SSonam.Gupta@Sun.COM /*
28211397SSonam.Gupta@Sun.COM * A remote lpd job should be cancelled only based on
28311397SSonam.Gupta@Sun.COM * job-id-requested
28411397SSonam.Gupta@Sun.COM */
28511397SSonam.Gupta@Sun.COM if (rid != -1) {
28611397SSonam.Gupta@Sun.COM if (match_job_rid(rid, ac, av) == -1)
28711397SSonam.Gupta@Sun.COM /* job-id mismatch */
28811397SSonam.Gupta@Sun.COM return;
28911397SSonam.Gupta@Sun.COM }
29011397SSonam.Gupta@Sun.COM
2912264Sjacobs status = papiJobCancel(svc, printer, id);
2922264Sjacobs if (status != PAPI_OK)
2932264Sjacobs mesg = papiStatusString(status);
2942264Sjacobs
29510472SSonam.Gupta@Sun.COM if (rid != -1)
2969330SSonam.Gupta@Sun.COM fprintf(fp, "%s-%d: %s\n", printer, rid, mesg);
2979330SSonam.Gupta@Sun.COM else
2988966SSonam.Gupta@Sun.COM fprintf(fp, "%s-%d: %s\n", printer, id, mesg);
2992264Sjacobs }
3002264Sjacobs
3012264Sjacobs int
berkeley_queue_report(papi_service_t svc,FILE * fp,char * dest,int fmt,int ac,char * av[])3022264Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt,
3032264Sjacobs int ac, char *av[])
3042264Sjacobs {
3052264Sjacobs papi_status_t status;
3062264Sjacobs papi_printer_t p = NULL;
3072264Sjacobs papi_job_t *jobs = NULL;
3082264Sjacobs char *pattrs[] = { "printer-name", "printer-state",
3092264Sjacobs "printer-state-reasons", NULL };
3103125Sjacobs char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id",
3116817Sjacobs "job-originating-user-name", "job-id-requested",
3123125Sjacobs "job-originating-host-name",
3132264Sjacobs "number-of-intervening-jobs", NULL };
3142264Sjacobs int num_jobs = 0;
3152264Sjacobs
3162264Sjacobs status = papiPrinterQuery(svc, dest, pattrs, NULL, &p);
3172264Sjacobs if (status != PAPI_OK) {
3182264Sjacobs fprintf(fp, gettext(
3198966SSonam.Gupta@Sun.COM "Failed to query service for state of %s: %s\n"),
3208966SSonam.Gupta@Sun.COM dest, verbose_papi_message(svc, status));
3212264Sjacobs return (-1);
3222264Sjacobs }
3232264Sjacobs
3242264Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3258966SSonam.Gupta@Sun.COM 0, &jobs);
3262264Sjacobs if (status != PAPI_OK) {
3272264Sjacobs fprintf(fp, gettext(
3288966SSonam.Gupta@Sun.COM "Failed to query service for jobs on %s: %s\n"),
3298966SSonam.Gupta@Sun.COM dest, verbose_papi_message(svc, status));
3302264Sjacobs return (-1);
3312264Sjacobs }
3322264Sjacobs if (jobs != NULL) {
3332264Sjacobs while (jobs[num_jobs] != NULL)
3342264Sjacobs num_jobs++;
3352264Sjacobs }
3362264Sjacobs
3372264Sjacobs printer_state_line(fp, p, num_jobs, dest);
3382264Sjacobs if (num_jobs > 0) {
3392264Sjacobs int i;
3402264Sjacobs
3412264Sjacobs if (fmt == 3)
3422264Sjacobs print_header(fp);
3432264Sjacobs for (i = 0; jobs[i] != NULL; i++)
3442264Sjacobs print_job_line(fp, i, jobs[i], fmt, ac, av);
3452264Sjacobs }
3462264Sjacobs
3472264Sjacobs papiPrinterFree(p);
3482264Sjacobs papiJobListFree(jobs);
3492264Sjacobs
3502264Sjacobs return (num_jobs);
3512264Sjacobs }
3522264Sjacobs
3532264Sjacobs int
berkeley_cancel_request(papi_service_t svc,FILE * fp,char * dest,int ac,char * av[])3542264Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest,
3552264Sjacobs int ac, char *av[])
3562264Sjacobs {
3572264Sjacobs papi_status_t status;
3582264Sjacobs papi_job_t *jobs = NULL;
3596817Sjacobs char *jattrs[] = { "job-originating-user-name", "job-id",
3606817Sjacobs "job-id-requested", NULL };
3612264Sjacobs
3622264Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL,
3638966SSonam.Gupta@Sun.COM 0, &jobs);
3642264Sjacobs
3652264Sjacobs if (status != PAPI_OK) {
3662264Sjacobs fprintf(fp, gettext("Failed to query service for %s: %s\n"),
3678966SSonam.Gupta@Sun.COM dest, verbose_papi_message(svc, status));
3682264Sjacobs return (-1);
3692264Sjacobs }
3702264Sjacobs
3712264Sjacobs /* cancel the job(s) */
3722264Sjacobs if (jobs != NULL) {
3732264Sjacobs int i;
3742264Sjacobs
3752264Sjacobs for (i = 0; jobs[i] != NULL; i++)
3762264Sjacobs cancel_job(svc, fp, dest, jobs[i], ac, av);
3772264Sjacobs }
3782264Sjacobs
3792264Sjacobs papiJobListFree(jobs);
3802264Sjacobs
3812264Sjacobs return (0);
3822264Sjacobs }
3832264Sjacobs
3842264Sjacobs int
get_printer_id(char * name,char ** printer,int * id)3852264Sjacobs get_printer_id(char *name, char **printer, int *id)
3862264Sjacobs {
3872264Sjacobs int result = -1;
3882264Sjacobs
3892264Sjacobs if (name != NULL) {
3902264Sjacobs char *p = strrchr(name, '-');
3912264Sjacobs
3922264Sjacobs *printer = name;
3932264Sjacobs if (p != NULL) {
3942264Sjacobs char *s = NULL;
3952264Sjacobs
3962264Sjacobs *id = strtol(p + 1, &s, 10);
3972264Sjacobs if (s[0] != '\0')
3982264Sjacobs *id = -1;
3992264Sjacobs else
4002264Sjacobs *p = '\0';
4012264Sjacobs result = 0;
4022264Sjacobs } else
4032264Sjacobs *id = -1;
4042264Sjacobs }
4052264Sjacobs
4062264Sjacobs return (result);
4072264Sjacobs }
4082264Sjacobs
4092264Sjacobs /*
4102264Sjacobs * strsplit() splits a string into a NULL terminated array of substrings
4112264Sjacobs * determined by a seperator. The original string is modified, and newly
4122264Sjacobs * allocated space is only returned for the array itself. If more than
4132264Sjacobs * 1024 substrings exist, they will be ignored.
4142264Sjacobs */
4152264Sjacobs char **
strsplit(char * string,const char * seperators)4162264Sjacobs strsplit(char *string, const char *seperators)
4172264Sjacobs {
4182264Sjacobs char *list[BUFSIZ],
4198966SSonam.Gupta@Sun.COM **result;
4202264Sjacobs int length = 0;
4212264Sjacobs
4222264Sjacobs if ((string == NULL) || (seperators == NULL))
4232264Sjacobs return (NULL);
4242264Sjacobs
4252264Sjacobs (void) memset(list, 0, sizeof (list));
4262264Sjacobs for (list[length] = strtok(string, seperators);
4278966SSonam.Gupta@Sun.COM (list[length] != NULL) && (length < (BUFSIZ - 2));
4288966SSonam.Gupta@Sun.COM list[length] = strtok(NULL, seperators))
4292264Sjacobs length++;
4302264Sjacobs
4312264Sjacobs if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL)
4322264Sjacobs (void) memcpy(result, list, length * sizeof (char *));
4332264Sjacobs
4342264Sjacobs return (result);
4352264Sjacobs }
4362264Sjacobs
4372264Sjacobs papi_status_t
jobSubmitSTDIN(papi_service_t svc,char * printer,char * prefetch,int len,papi_attribute_t ** list,papi_job_t * job)4387253Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len,
4397253Sjacobs papi_attribute_t **list, papi_job_t *job)
4402264Sjacobs {
4412264Sjacobs papi_status_t status;
4422264Sjacobs papi_stream_t stream = NULL;
4432264Sjacobs int rc;
4442264Sjacobs char buf[BUFSIZ];
4452264Sjacobs
4462264Sjacobs status = papiJobStreamOpen(svc, printer, list, NULL, &stream);
4477253Sjacobs
4487253Sjacobs if (len > 0)
4497253Sjacobs status = papiJobStreamWrite(svc, stream, prefetch, len);
4507253Sjacobs
4512264Sjacobs while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0))
4522264Sjacobs status = papiJobStreamWrite(svc, stream, buf, rc);
4532264Sjacobs
4542264Sjacobs if (status == PAPI_OK)
4552264Sjacobs status = papiJobStreamClose(svc, stream, job);
4562264Sjacobs
4572264Sjacobs return (status);
4582264Sjacobs }
4592264Sjacobs
4603125Sjacobs /*
4613125Sjacobs * is_postscript() will detect if the file passed in contains postscript
4623125Sjacobs * data. A one is returned if the file contains postscript, zero is returned
4633125Sjacobs * if the file is not postscript, and -1 is returned if an error occurs
4643125Sjacobs */
4653125Sjacobs #define PS_MAGIC "%!"
4663125Sjacobs #define PC_PS_MAGIC "^D%!"
4673125Sjacobs int
is_postscript_stream(int fd,char * buf,int * len)4687253Sjacobs is_postscript_stream(int fd, char *buf, int *len)
4693125Sjacobs {
4707253Sjacobs if ((*len = read(fd, buf, *len)) < 0) {
4713125Sjacobs close(fd);
4723125Sjacobs return (-1);
4733125Sjacobs }
4743125Sjacobs
4753125Sjacobs if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) ||
4763125Sjacobs (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0))
4773125Sjacobs return (1);
4783125Sjacobs else
4793125Sjacobs return (0);
4803125Sjacobs }
4813125Sjacobs
4827253Sjacobs int
is_postscript(const char * file)4837253Sjacobs is_postscript(const char *file)
4847253Sjacobs {
4857253Sjacobs int rc = -1;
4867253Sjacobs int fd;
4877253Sjacobs
4887253Sjacobs if ((fd = open(file, O_RDONLY)) >= 0) {
4897253Sjacobs char buf[3];
4907253Sjacobs int len = sizeof (buf);
4917253Sjacobs
4927253Sjacobs rc = is_postscript_stream(fd, buf, &len);
4937253Sjacobs close(fd);
4947253Sjacobs }
4957253Sjacobs
4967253Sjacobs return (rc);
4977253Sjacobs }
4987253Sjacobs
4992264Sjacobs static char **
all_list(papi_service_t svc)5002264Sjacobs all_list(papi_service_t svc)
5012264Sjacobs {
5022264Sjacobs papi_status_t status;
5032264Sjacobs papi_printer_t printer = NULL;
5042264Sjacobs char *list[] = { "member-names", NULL };
5052264Sjacobs char **result = NULL;
5062264Sjacobs
5072264Sjacobs status = papiPrinterQuery(svc, "_all", list, NULL, &printer);
5082264Sjacobs if ((status == PAPI_OK) && (printer != NULL)) {
5092264Sjacobs papi_attribute_t **attributes =
5108966SSonam.Gupta@Sun.COM papiPrinterGetAttributeList(printer);
5112264Sjacobs if (attributes != NULL) {
5122264Sjacobs void *iter = NULL;
5132264Sjacobs char *value = NULL;
5142264Sjacobs
5152264Sjacobs for (status = papiAttributeListGetString(attributes,
5168966SSonam.Gupta@Sun.COM &iter, "member-names", &value);
5178966SSonam.Gupta@Sun.COM status == PAPI_OK;
5188966SSonam.Gupta@Sun.COM status = papiAttributeListGetString(attributes,
5198966SSonam.Gupta@Sun.COM &iter, NULL, &value))
5202264Sjacobs list_append(&result, strdup(value));
5212264Sjacobs }
5222264Sjacobs papiPrinterFree(printer);
5232264Sjacobs }
5242264Sjacobs
5252264Sjacobs return (result);
5262264Sjacobs }
5272264Sjacobs
5282264Sjacobs static char **
printers_list(papi_service_t svc)5292264Sjacobs printers_list(papi_service_t svc)
5302264Sjacobs {
5312264Sjacobs papi_status_t status;
5322264Sjacobs papi_printer_t *printers = NULL;
5332264Sjacobs char *keys[] = { "printer-name", NULL };
5342264Sjacobs char **result = NULL;
5352264Sjacobs
5362264Sjacobs status = papiPrintersList(svc, keys, NULL, &printers);
5372264Sjacobs if ((status == PAPI_OK) && (printers != NULL)) {
5382264Sjacobs int i;
5392264Sjacobs
5402264Sjacobs for (i = 0; printers[i] != NULL; i++) {
5412264Sjacobs papi_attribute_t **attributes =
5428966SSonam.Gupta@Sun.COM papiPrinterGetAttributeList(printers[i]);
5432264Sjacobs char *name = NULL;
5442264Sjacobs
5452264Sjacobs (void) papiAttributeListGetString(attributes, NULL,
5468966SSonam.Gupta@Sun.COM "printer-name", &name);
5472264Sjacobs if ((name != NULL) && (strcmp(name, "_default") != 0))
5482264Sjacobs list_append(&result, strdup(name));
5492264Sjacobs }
5502264Sjacobs papiPrinterListFree(printers);
5512264Sjacobs }
5522264Sjacobs
5532264Sjacobs return (result);
5542264Sjacobs }
5552264Sjacobs
5562264Sjacobs char **
interest_list(papi_service_t svc)5572264Sjacobs interest_list(papi_service_t svc)
5582264Sjacobs {
5592264Sjacobs static char been_here;
5602264Sjacobs static char **result;
5612264Sjacobs
5622264Sjacobs if (been_here == 0) { /* only do this once */
5632264Sjacobs been_here = 1;
5642264Sjacobs
5652264Sjacobs if ((result = all_list(svc)) == NULL)
5662264Sjacobs result = printers_list(svc);
5672264Sjacobs }
5682264Sjacobs
5692264Sjacobs return (result);
5702264Sjacobs }
5712264Sjacobs
5722264Sjacobs char *
localhostname()5732264Sjacobs localhostname()
5742264Sjacobs {
5752264Sjacobs static char *result;
5762264Sjacobs
5772264Sjacobs if (result == NULL) {
5782264Sjacobs static char buf[256];
5792264Sjacobs
5802264Sjacobs if (gethostname(buf, sizeof (buf)) == 0)
5812264Sjacobs result = buf;
5822264Sjacobs }
5832264Sjacobs
5842264Sjacobs return (result);
5852264Sjacobs }
5862264Sjacobs
5872264Sjacobs int
cli_auth_callback(papi_service_t svc,void * app_data)5882264Sjacobs cli_auth_callback(papi_service_t svc, void *app_data)
5892264Sjacobs {
5902264Sjacobs char prompt[BUFSIZ];
5912264Sjacobs char *user, *svc_name, *passphrase;
5922264Sjacobs
5932264Sjacobs /* get the name of the service we are contacting */
5942264Sjacobs if ((svc_name = papiServiceGetServiceName(svc)) == NULL)
5952264Sjacobs return (-1);
5962264Sjacobs
5972264Sjacobs /* find our who we are supposed to be */
5982264Sjacobs if ((user = papiServiceGetUserName(svc)) == NULL) {
5992264Sjacobs struct passwd *pw;
6002264Sjacobs
6012264Sjacobs if ((pw = getpwuid(getuid())) != NULL)
6022264Sjacobs user = pw->pw_name;
6032264Sjacobs else
6042264Sjacobs user = "nobody";
6052264Sjacobs }
6062264Sjacobs
6072264Sjacobs /* build the prompt string */
6082264Sjacobs snprintf(prompt, sizeof (prompt),
6098966SSonam.Gupta@Sun.COM gettext("passphrase for %s to access %s: "), user, svc_name);
6102264Sjacobs
6112264Sjacobs /* ask for the passphrase */
6122264Sjacobs if ((passphrase = getpassphrase(prompt)) != NULL)
6132264Sjacobs papiServiceSetPassword(svc, passphrase);
6142264Sjacobs
6152264Sjacobs return (0);
6162264Sjacobs }
6179331SSonam.Gupta@Sun.COM
6189331SSonam.Gupta@Sun.COM int32_t
job_to_be_queried(papi_service_t svc,char * printer,int32_t id)6199331SSonam.Gupta@Sun.COM job_to_be_queried(papi_service_t svc, char *printer, int32_t id)
6209331SSonam.Gupta@Sun.COM {
6219331SSonam.Gupta@Sun.COM papi_job_t *jobs = NULL;
6229331SSonam.Gupta@Sun.COM papi_status_t status;
62311397SSonam.Gupta@Sun.COM int ret = -1;
6249331SSonam.Gupta@Sun.COM char *jattrs[] = { "job-id",
6259331SSonam.Gupta@Sun.COM "job-id-requested", NULL };
6269331SSonam.Gupta@Sun.COM
6279331SSonam.Gupta@Sun.COM status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL,
6289331SSonam.Gupta@Sun.COM 0, &jobs);
6299331SSonam.Gupta@Sun.COM
6309331SSonam.Gupta@Sun.COM if (status != PAPI_OK) {
6319331SSonam.Gupta@Sun.COM fprintf(stderr, gettext("Failed to query service for %s: %s\n"),
6329331SSonam.Gupta@Sun.COM printer, verbose_papi_message(svc, status));
6339331SSonam.Gupta@Sun.COM return (-1);
6349331SSonam.Gupta@Sun.COM }
6359331SSonam.Gupta@Sun.COM
6369331SSonam.Gupta@Sun.COM if (jobs != NULL) {
6379331SSonam.Gupta@Sun.COM int i = 0;
6389331SSonam.Gupta@Sun.COM
6399331SSonam.Gupta@Sun.COM for (i = 0; jobs[i] != NULL; i++) {
6409331SSonam.Gupta@Sun.COM int32_t rid = -1;
64111397SSonam.Gupta@Sun.COM int32_t jid = -1;
6429331SSonam.Gupta@Sun.COM papi_attribute_t **list =
6439331SSonam.Gupta@Sun.COM papiJobGetAttributeList(jobs[i]);
6449331SSonam.Gupta@Sun.COM
6459331SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
6469331SSonam.Gupta@Sun.COM "job-id-requested", &rid);
64711397SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
64811397SSonam.Gupta@Sun.COM "job-id", &jid);
6499331SSonam.Gupta@Sun.COM
65011397SSonam.Gupta@Sun.COM /*
65111397SSonam.Gupta@Sun.COM * check if id matches with either rid or jid
65211397SSonam.Gupta@Sun.COM */
6539331SSonam.Gupta@Sun.COM if (rid == id) {
6549331SSonam.Gupta@Sun.COM /* get the actual id and return it */
6559331SSonam.Gupta@Sun.COM papiAttributeListGetInteger(list, NULL,
6569331SSonam.Gupta@Sun.COM "job-id", &id);
6579331SSonam.Gupta@Sun.COM return (id);
65811397SSonam.Gupta@Sun.COM } else if (id == jid) {
65911397SSonam.Gupta@Sun.COM if (rid != -1) {
66011397SSonam.Gupta@Sun.COM /*
66111397SSonam.Gupta@Sun.COM * It is a remote lpd job
66211397SSonam.Gupta@Sun.COM * can be cancelled only
66311397SSonam.Gupta@Sun.COM * using rid
66411397SSonam.Gupta@Sun.COM */
66511397SSonam.Gupta@Sun.COM ret = -1;
66611397SSonam.Gupta@Sun.COM } else {
66711397SSonam.Gupta@Sun.COM /*
66811397SSonam.Gupta@Sun.COM * its local or
66911397SSonam.Gupta@Sun.COM * remote ipp job
67011397SSonam.Gupta@Sun.COM */
67111397SSonam.Gupta@Sun.COM return (id);
67211397SSonam.Gupta@Sun.COM }
6739331SSonam.Gupta@Sun.COM }
6749331SSonam.Gupta@Sun.COM }
67511397SSonam.Gupta@Sun.COM return (ret);
6769331SSonam.Gupta@Sun.COM }
6779331SSonam.Gupta@Sun.COM return (id);
6789331SSonam.Gupta@Sun.COM }
679