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