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 /* 23*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India * Copyright 2010 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 7806f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 7906f61b77Ssonam gupta - Sun Microsystems - Bangalore India * return 0 : argument passed is job-id && job-id matches 8006f61b77Ssonam gupta - Sun Microsystems - Bangalore India * or argument passed is user 8106f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 8206f61b77Ssonam gupta - Sun Microsystems - Bangalore India static int 8306f61b77Ssonam gupta - Sun Microsystems - Bangalore India match_job_rid(int id, int ac, char *av[]) 8406f61b77Ssonam gupta - Sun Microsystems - Bangalore India { 8506f61b77Ssonam gupta - Sun Microsystems - Bangalore India int i; 8606f61b77Ssonam gupta - Sun Microsystems - Bangalore India 8706f61b77Ssonam gupta - Sun Microsystems - Bangalore India for (i = 0; i < ac; i++) 8806f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (isdigit(av[i][0]) != 0) { 8906f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (id == atoi(av[i])) 9006f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* job-id match */ 9106f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (0); 9206f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else 9306f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* argument passed is user */ 9406f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (0); 9506f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (-1); 9606f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 9706f61b77Ssonam gupta - Sun Microsystems - Bangalore India 980a44ef6dSjacobs static struct { 990a44ef6dSjacobs char *mime_type; 1000a44ef6dSjacobs char *lp_type; 1010a44ef6dSjacobs } type_map[] = { 1020a44ef6dSjacobs { "text/plain", "simple" }, 1030a44ef6dSjacobs { "application/octet-stream", "raw" }, 1040a44ef6dSjacobs { "application/octet-stream", "any" }, 1050a44ef6dSjacobs { "application/postscript", "postscript" }, 1060a44ef6dSjacobs { "application/postscript", "ps" }, 1070a44ef6dSjacobs { "application/x-cif", "cif" }, 1080a44ef6dSjacobs { "application/x-dvi", "dvi" }, 1090a44ef6dSjacobs { "application/x-plot", "plot" }, 1100a44ef6dSjacobs { "application/x-ditroff", "troff" }, 1110a44ef6dSjacobs { "application/x-troff", "otroff" }, 1120a44ef6dSjacobs { "application/x-pr", "pr" }, 1130a44ef6dSjacobs { "application/x-fortran", "fortran" }, 1140a44ef6dSjacobs { "application/x-raster", "raster" }, 1150a44ef6dSjacobs { NULL, NULL} 1160a44ef6dSjacobs }; 1170a44ef6dSjacobs 1180a44ef6dSjacobs char * 1190a44ef6dSjacobs lp_type_to_mime_type(char *lp_type) 1200a44ef6dSjacobs { 1210a44ef6dSjacobs int i; 1220a44ef6dSjacobs 1230a44ef6dSjacobs if (lp_type == NULL) 1240a44ef6dSjacobs return ("application/octet-stream"); 1250a44ef6dSjacobs 1260a44ef6dSjacobs for (i = 0; type_map[i].lp_type != NULL; i++) 1270a44ef6dSjacobs if (strcasecmp(type_map[i].lp_type, lp_type) == 0) 1280a44ef6dSjacobs return (type_map[i].mime_type); 1290a44ef6dSjacobs 1300a44ef6dSjacobs return (lp_type); 1310a44ef6dSjacobs } 1320a44ef6dSjacobs 133355b4669Sjacobs /* 134355b4669Sjacobs * to support job/printer status 135355b4669Sjacobs */ 136355b4669Sjacobs static char * 137355b4669Sjacobs state_string(int state) 138355b4669Sjacobs { 139355b4669Sjacobs switch (state) { 140355b4669Sjacobs case 3: 141355b4669Sjacobs return (gettext("idle")); 142355b4669Sjacobs case 4: 143355b4669Sjacobs return (gettext("processing")); 144355b4669Sjacobs case 5: 145355b4669Sjacobs return (gettext("stopped")); 146355b4669Sjacobs default: 147355b4669Sjacobs return (gettext("unknown")); 148355b4669Sjacobs } 149355b4669Sjacobs } 150355b4669Sjacobs 151355b4669Sjacobs static char *_rank_suffixes[] = { 152355b4669Sjacobs "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 153355b4669Sjacobs }; 154355b4669Sjacobs 155355b4669Sjacobs static char * 156355b4669Sjacobs rank_string(const int rank) 157355b4669Sjacobs { 158355b4669Sjacobs static char buf[12]; 159355b4669Sjacobs 160355b4669Sjacobs if (rank < 0) 161355b4669Sjacobs snprintf(buf, sizeof (buf), gettext("invalid")); 162355b4669Sjacobs else if (rank == 0) 163355b4669Sjacobs snprintf(buf, sizeof (buf), gettext("active")); 164355b4669Sjacobs else if ((rank > 10) && (rank < 14)) 165355b4669Sjacobs sprintf(buf, "%dth", rank); 166355b4669Sjacobs else 167355b4669Sjacobs sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]); 168355b4669Sjacobs 169355b4669Sjacobs return (buf); 170355b4669Sjacobs } 171355b4669Sjacobs 172355b4669Sjacobs static void 173355b4669Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name) 174355b4669Sjacobs { 175355b4669Sjacobs papi_attribute_t **list = papiPrinterGetAttributeList(p); 176355b4669Sjacobs int state = 0; 177355b4669Sjacobs char *reason = ""; 178355b4669Sjacobs 179355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 180355b4669Sjacobs "printer-state", &state); 181355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 182355b4669Sjacobs "printer-state-reasons", &reason); 183355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 184355b4669Sjacobs "printer-name", &name); 185355b4669Sjacobs 186355b4669Sjacobs if ((state != 0x03) || (num_jobs != 0)) { 187355b4669Sjacobs fprintf(fp, "%s: %s", name, state_string(state)); 188*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India if ((state == 0x05) || 189*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (state == 0x06) || 190*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (state == 0x07) || 191*c7f2fd24Ssonam gupta - Sun Microsystems - Bangalore India (state == 0x08)) /* stopped */ 192355b4669Sjacobs fprintf(fp, ": %s\n", reason); 193355b4669Sjacobs else 194355b4669Sjacobs fprintf(fp, "\n"); 195355b4669Sjacobs } else 196355b4669Sjacobs fprintf(fp, "no entries\n"); 197355b4669Sjacobs } 198355b4669Sjacobs 199355b4669Sjacobs static void 200355b4669Sjacobs print_header(FILE *fp) 201355b4669Sjacobs { 202355b4669Sjacobs fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n")); 203355b4669Sjacobs } 204355b4669Sjacobs 205355b4669Sjacobs static void 206355b4669Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[]) 207355b4669Sjacobs { 208355b4669Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job); 209355b4669Sjacobs int copies = 1, id = 0, rank = count, size = 0; 210355b4669Sjacobs char *name = "print job"; 211355b4669Sjacobs char *user = "nobody"; 212355b4669Sjacobs char *host = "localhost"; 213355b4669Sjacobs char *suffix = "k"; 214355b4669Sjacobs 215355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 216355b4669Sjacobs "job-id", &id); 21736615d24Sjacobs (void) papiAttributeListGetInteger(list, NULL, 21836615d24Sjacobs "job-id-requested", &id); 219355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 220355b4669Sjacobs "job-originating-user-name", &user); 2210a44ef6dSjacobs (void) papiAttributeListGetString(list, NULL, 2220a44ef6dSjacobs "job-originating-host-name", &host); 223355b4669Sjacobs 224355b4669Sjacobs /* if we are looking and it doesn't match, return early */ 225355b4669Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0)) 226355b4669Sjacobs return; 227355b4669Sjacobs 228355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 229355b4669Sjacobs "copies", &copies); 230355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 231355b4669Sjacobs "number-of-intervening-jobs", &rank); 232355b4669Sjacobs 233355b4669Sjacobs if (papiAttributeListGetInteger(list, NULL, "job-octets", &size) 234355b4669Sjacobs == PAPI_OK) 235355b4669Sjacobs suffix = "bytes"; 236355b4669Sjacobs else 237355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 238355b4669Sjacobs "job-k-octets", &size); 239355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 240355b4669Sjacobs "job-name", &name); 241355b4669Sjacobs 242355b4669Sjacobs size *= copies; 243355b4669Sjacobs 244355b4669Sjacobs if (fmt == 3) { 245355b4669Sjacobs fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"), 246355b4669Sjacobs rank_string(++rank), user, id, name, size, suffix); 247355b4669Sjacobs } else 248355b4669Sjacobs fprintf(fp, gettext( 249355b4669Sjacobs "\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"), 250355b4669Sjacobs user, rank_string(++rank), id, host, name, size, 251355b4669Sjacobs suffix); 252355b4669Sjacobs } 253355b4669Sjacobs 254355b4669Sjacobs /* 255355b4669Sjacobs * to support job cancelation 256355b4669Sjacobs */ 257355b4669Sjacobs static void 258355b4669Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job, 259355b4669Sjacobs int ac, char *av[]) 260355b4669Sjacobs { 261355b4669Sjacobs papi_status_t status; 262355b4669Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job); 26306f61b77Ssonam gupta - Sun Microsystems - Bangalore India int id = -1; 264dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India int rid = -1; 265355b4669Sjacobs char *user = ""; 266355b4669Sjacobs char *mesg = gettext("cancelled"); 26706f61b77Ssonam gupta - Sun Microsystems - Bangalore India int i = 0; 268355b4669Sjacobs 269355b4669Sjacobs papiAttributeListGetInteger(list, NULL, 270355b4669Sjacobs "job-id", &id); 27136615d24Sjacobs papiAttributeListGetInteger(list, NULL, 27236615d24Sjacobs "job-id-requested", &rid); 273355b4669Sjacobs papiAttributeListGetString(list, NULL, 274355b4669Sjacobs "job-originating-user-name", &user); 275355b4669Sjacobs 276355b4669Sjacobs /* if we are looking and it doesn't match, return early */ 27736615d24Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0) && 27836615d24Sjacobs (match_job(rid, user, ac, av) < 0)) 279355b4669Sjacobs return; 280355b4669Sjacobs 28106f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 28206f61b77Ssonam gupta - Sun Microsystems - Bangalore India * A remote lpd job should be cancelled only based on 28306f61b77Ssonam gupta - Sun Microsystems - Bangalore India * job-id-requested 28406f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 28506f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) { 28606f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (match_job_rid(rid, ac, av) == -1) 28706f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* job-id mismatch */ 28806f61b77Ssonam gupta - Sun Microsystems - Bangalore India return; 28906f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 29006f61b77Ssonam gupta - Sun Microsystems - Bangalore India 291355b4669Sjacobs status = papiJobCancel(svc, printer, id); 292355b4669Sjacobs if (status != PAPI_OK) 293355b4669Sjacobs mesg = papiStatusString(status); 294355b4669Sjacobs 295dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) 2966b5764c3Ssonam gupta - Sun Microsystems - Bangalore India fprintf(fp, "%s-%d: %s\n", printer, rid, mesg); 29740e7ce05Ssonam gupta - Sun Microsystems - Bangalore India else 29840e7ce05Ssonam gupta - Sun Microsystems - Bangalore India fprintf(fp, "%s-%d: %s\n", printer, id, mesg); 299355b4669Sjacobs } 300355b4669Sjacobs 301355b4669Sjacobs int 302355b4669Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt, 303355b4669Sjacobs int ac, char *av[]) 304355b4669Sjacobs { 305355b4669Sjacobs papi_status_t status; 306355b4669Sjacobs papi_printer_t p = NULL; 307355b4669Sjacobs papi_job_t *jobs = NULL; 308355b4669Sjacobs char *pattrs[] = { "printer-name", "printer-state", 309355b4669Sjacobs "printer-state-reasons", NULL }; 3100a44ef6dSjacobs char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id", 31136615d24Sjacobs "job-originating-user-name", "job-id-requested", 3120a44ef6dSjacobs "job-originating-host-name", 313355b4669Sjacobs "number-of-intervening-jobs", NULL }; 314355b4669Sjacobs int num_jobs = 0; 315355b4669Sjacobs 316355b4669Sjacobs status = papiPrinterQuery(svc, dest, pattrs, NULL, &p); 317355b4669Sjacobs if (status != PAPI_OK) { 318355b4669Sjacobs fprintf(fp, gettext( 319355b4669Sjacobs "Failed to query service for state of %s: %s\n"), 320355b4669Sjacobs dest, verbose_papi_message(svc, status)); 321355b4669Sjacobs return (-1); 322355b4669Sjacobs } 323355b4669Sjacobs 324355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 325355b4669Sjacobs 0, &jobs); 326355b4669Sjacobs if (status != PAPI_OK) { 327355b4669Sjacobs fprintf(fp, gettext( 328355b4669Sjacobs "Failed to query service for jobs on %s: %s\n"), 329355b4669Sjacobs dest, verbose_papi_message(svc, status)); 330355b4669Sjacobs return (-1); 331355b4669Sjacobs } 332355b4669Sjacobs if (jobs != NULL) { 333355b4669Sjacobs while (jobs[num_jobs] != NULL) 334355b4669Sjacobs num_jobs++; 335355b4669Sjacobs } 336355b4669Sjacobs 337355b4669Sjacobs printer_state_line(fp, p, num_jobs, dest); 338355b4669Sjacobs if (num_jobs > 0) { 339355b4669Sjacobs int i; 340355b4669Sjacobs 341355b4669Sjacobs if (fmt == 3) 342355b4669Sjacobs print_header(fp); 343355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 344355b4669Sjacobs print_job_line(fp, i, jobs[i], fmt, ac, av); 345355b4669Sjacobs } 346355b4669Sjacobs 347355b4669Sjacobs papiPrinterFree(p); 348355b4669Sjacobs papiJobListFree(jobs); 349355b4669Sjacobs 350355b4669Sjacobs return (num_jobs); 351355b4669Sjacobs } 352355b4669Sjacobs 353355b4669Sjacobs int 354355b4669Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest, 355355b4669Sjacobs int ac, char *av[]) 356355b4669Sjacobs { 357355b4669Sjacobs papi_status_t status; 358355b4669Sjacobs papi_job_t *jobs = NULL; 35936615d24Sjacobs char *jattrs[] = { "job-originating-user-name", "job-id", 36036615d24Sjacobs "job-id-requested", NULL }; 361355b4669Sjacobs 362355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 363355b4669Sjacobs 0, &jobs); 364355b4669Sjacobs 365355b4669Sjacobs if (status != PAPI_OK) { 366355b4669Sjacobs fprintf(fp, gettext("Failed to query service for %s: %s\n"), 367355b4669Sjacobs dest, verbose_papi_message(svc, status)); 368355b4669Sjacobs return (-1); 369355b4669Sjacobs } 370355b4669Sjacobs 371355b4669Sjacobs /* cancel the job(s) */ 372355b4669Sjacobs if (jobs != NULL) { 373355b4669Sjacobs int i; 374355b4669Sjacobs 375355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 376355b4669Sjacobs cancel_job(svc, fp, dest, jobs[i], ac, av); 377355b4669Sjacobs } 378355b4669Sjacobs 379355b4669Sjacobs papiJobListFree(jobs); 380355b4669Sjacobs 381355b4669Sjacobs return (0); 382355b4669Sjacobs } 383355b4669Sjacobs 384355b4669Sjacobs int 385355b4669Sjacobs get_printer_id(char *name, char **printer, int *id) 386355b4669Sjacobs { 387355b4669Sjacobs int result = -1; 388355b4669Sjacobs 389355b4669Sjacobs if (name != NULL) { 390355b4669Sjacobs char *p = strrchr(name, '-'); 391355b4669Sjacobs 392355b4669Sjacobs *printer = name; 393355b4669Sjacobs if (p != NULL) { 394355b4669Sjacobs char *s = NULL; 395355b4669Sjacobs 396355b4669Sjacobs *id = strtol(p + 1, &s, 10); 397355b4669Sjacobs if (s[0] != '\0') 398355b4669Sjacobs *id = -1; 399355b4669Sjacobs else 400355b4669Sjacobs *p = '\0'; 401355b4669Sjacobs result = 0; 402355b4669Sjacobs } else 403355b4669Sjacobs *id = -1; 404355b4669Sjacobs } 405355b4669Sjacobs 406355b4669Sjacobs return (result); 407355b4669Sjacobs } 408355b4669Sjacobs 409355b4669Sjacobs /* 410355b4669Sjacobs * strsplit() splits a string into a NULL terminated array of substrings 411355b4669Sjacobs * determined by a seperator. The original string is modified, and newly 412355b4669Sjacobs * allocated space is only returned for the array itself. If more than 413355b4669Sjacobs * 1024 substrings exist, they will be ignored. 414355b4669Sjacobs */ 415355b4669Sjacobs char ** 416355b4669Sjacobs strsplit(char *string, const char *seperators) 417355b4669Sjacobs { 418355b4669Sjacobs char *list[BUFSIZ], 419355b4669Sjacobs **result; 420355b4669Sjacobs int length = 0; 421355b4669Sjacobs 422355b4669Sjacobs if ((string == NULL) || (seperators == NULL)) 423355b4669Sjacobs return (NULL); 424355b4669Sjacobs 425355b4669Sjacobs (void) memset(list, 0, sizeof (list)); 426355b4669Sjacobs for (list[length] = strtok(string, seperators); 427355b4669Sjacobs (list[length] != NULL) && (length < (BUFSIZ - 2)); 428355b4669Sjacobs list[length] = strtok(NULL, seperators)) 429355b4669Sjacobs length++; 430355b4669Sjacobs 431355b4669Sjacobs if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL) 432355b4669Sjacobs (void) memcpy(result, list, length * sizeof (char *)); 433355b4669Sjacobs 434355b4669Sjacobs return (result); 435355b4669Sjacobs } 436355b4669Sjacobs 437355b4669Sjacobs papi_status_t 438c1ecd8b9Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len, 439c1ecd8b9Sjacobs papi_attribute_t **list, papi_job_t *job) 440355b4669Sjacobs { 441355b4669Sjacobs papi_status_t status; 442355b4669Sjacobs papi_stream_t stream = NULL; 443355b4669Sjacobs int rc; 444355b4669Sjacobs char buf[BUFSIZ]; 445355b4669Sjacobs 446355b4669Sjacobs status = papiJobStreamOpen(svc, printer, list, NULL, &stream); 447c1ecd8b9Sjacobs 448c1ecd8b9Sjacobs if (len > 0) 449c1ecd8b9Sjacobs status = papiJobStreamWrite(svc, stream, prefetch, len); 450c1ecd8b9Sjacobs 451355b4669Sjacobs while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0)) 452355b4669Sjacobs status = papiJobStreamWrite(svc, stream, buf, rc); 453355b4669Sjacobs 454355b4669Sjacobs if (status == PAPI_OK) 455355b4669Sjacobs status = papiJobStreamClose(svc, stream, job); 456355b4669Sjacobs 457355b4669Sjacobs return (status); 458355b4669Sjacobs } 459355b4669Sjacobs 4600a44ef6dSjacobs /* 4610a44ef6dSjacobs * is_postscript() will detect if the file passed in contains postscript 4620a44ef6dSjacobs * data. A one is returned if the file contains postscript, zero is returned 4630a44ef6dSjacobs * if the file is not postscript, and -1 is returned if an error occurs 4640a44ef6dSjacobs */ 4650a44ef6dSjacobs #define PS_MAGIC "%!" 4660a44ef6dSjacobs #define PC_PS_MAGIC "^D%!" 4670a44ef6dSjacobs int 468c1ecd8b9Sjacobs is_postscript_stream(int fd, char *buf, int *len) 4690a44ef6dSjacobs { 470c1ecd8b9Sjacobs if ((*len = read(fd, buf, *len)) < 0) { 4710a44ef6dSjacobs close(fd); 4720a44ef6dSjacobs return (-1); 4730a44ef6dSjacobs } 4740a44ef6dSjacobs 4750a44ef6dSjacobs if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) || 4760a44ef6dSjacobs (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0)) 4770a44ef6dSjacobs return (1); 4780a44ef6dSjacobs else 4790a44ef6dSjacobs return (0); 4800a44ef6dSjacobs } 4810a44ef6dSjacobs 482c1ecd8b9Sjacobs int 483c1ecd8b9Sjacobs is_postscript(const char *file) 484c1ecd8b9Sjacobs { 485c1ecd8b9Sjacobs int rc = -1; 486c1ecd8b9Sjacobs int fd; 487c1ecd8b9Sjacobs 488c1ecd8b9Sjacobs if ((fd = open(file, O_RDONLY)) >= 0) { 489c1ecd8b9Sjacobs char buf[3]; 490c1ecd8b9Sjacobs int len = sizeof (buf); 491c1ecd8b9Sjacobs 492c1ecd8b9Sjacobs rc = is_postscript_stream(fd, buf, &len); 493c1ecd8b9Sjacobs close(fd); 494c1ecd8b9Sjacobs } 495c1ecd8b9Sjacobs 496c1ecd8b9Sjacobs return (rc); 497c1ecd8b9Sjacobs } 498c1ecd8b9Sjacobs 499355b4669Sjacobs static char ** 500355b4669Sjacobs all_list(papi_service_t svc) 501355b4669Sjacobs { 502355b4669Sjacobs papi_status_t status; 503355b4669Sjacobs papi_printer_t printer = NULL; 504355b4669Sjacobs char *list[] = { "member-names", NULL }; 505355b4669Sjacobs char **result = NULL; 506355b4669Sjacobs 507355b4669Sjacobs status = papiPrinterQuery(svc, "_all", list, NULL, &printer); 508355b4669Sjacobs if ((status == PAPI_OK) && (printer != NULL)) { 509355b4669Sjacobs papi_attribute_t **attributes = 510355b4669Sjacobs papiPrinterGetAttributeList(printer); 511355b4669Sjacobs if (attributes != NULL) { 512355b4669Sjacobs void *iter = NULL; 513355b4669Sjacobs char *value = NULL; 514355b4669Sjacobs 515355b4669Sjacobs for (status = papiAttributeListGetString(attributes, 516355b4669Sjacobs &iter, "member-names", &value); 517355b4669Sjacobs status == PAPI_OK; 518355b4669Sjacobs status = papiAttributeListGetString(attributes, 519355b4669Sjacobs &iter, NULL, &value)) 520355b4669Sjacobs list_append(&result, strdup(value)); 521355b4669Sjacobs } 522355b4669Sjacobs papiPrinterFree(printer); 523355b4669Sjacobs } 524355b4669Sjacobs 525355b4669Sjacobs return (result); 526355b4669Sjacobs } 527355b4669Sjacobs 528355b4669Sjacobs static char ** 529355b4669Sjacobs printers_list(papi_service_t svc) 530355b4669Sjacobs { 531355b4669Sjacobs papi_status_t status; 532355b4669Sjacobs papi_printer_t *printers = NULL; 533355b4669Sjacobs char *keys[] = { "printer-name", NULL }; 534355b4669Sjacobs char **result = NULL; 535355b4669Sjacobs 536355b4669Sjacobs status = papiPrintersList(svc, keys, NULL, &printers); 537355b4669Sjacobs if ((status == PAPI_OK) && (printers != NULL)) { 538355b4669Sjacobs int i; 539355b4669Sjacobs 540355b4669Sjacobs for (i = 0; printers[i] != NULL; i++) { 541355b4669Sjacobs papi_attribute_t **attributes = 542355b4669Sjacobs papiPrinterGetAttributeList(printers[i]); 543355b4669Sjacobs char *name = NULL; 544355b4669Sjacobs 545355b4669Sjacobs (void) papiAttributeListGetString(attributes, NULL, 546355b4669Sjacobs "printer-name", &name); 547355b4669Sjacobs if ((name != NULL) && (strcmp(name, "_default") != 0)) 548355b4669Sjacobs list_append(&result, strdup(name)); 549355b4669Sjacobs } 550355b4669Sjacobs papiPrinterListFree(printers); 551355b4669Sjacobs } 552355b4669Sjacobs 553355b4669Sjacobs return (result); 554355b4669Sjacobs } 555355b4669Sjacobs 556355b4669Sjacobs char ** 557355b4669Sjacobs interest_list(papi_service_t svc) 558355b4669Sjacobs { 559355b4669Sjacobs static char been_here; 560355b4669Sjacobs static char **result; 561355b4669Sjacobs 562355b4669Sjacobs if (been_here == 0) { /* only do this once */ 563355b4669Sjacobs been_here = 1; 564355b4669Sjacobs 565355b4669Sjacobs if ((result = all_list(svc)) == NULL) 566355b4669Sjacobs result = printers_list(svc); 567355b4669Sjacobs } 568355b4669Sjacobs 569355b4669Sjacobs return (result); 570355b4669Sjacobs } 571355b4669Sjacobs 572355b4669Sjacobs char * 573355b4669Sjacobs localhostname() 574355b4669Sjacobs { 575355b4669Sjacobs static char *result; 576355b4669Sjacobs 577355b4669Sjacobs if (result == NULL) { 578355b4669Sjacobs static char buf[256]; 579355b4669Sjacobs 580355b4669Sjacobs if (gethostname(buf, sizeof (buf)) == 0) 581355b4669Sjacobs result = buf; 582355b4669Sjacobs } 583355b4669Sjacobs 584355b4669Sjacobs return (result); 585355b4669Sjacobs } 586355b4669Sjacobs 587355b4669Sjacobs int 588355b4669Sjacobs cli_auth_callback(papi_service_t svc, void *app_data) 589355b4669Sjacobs { 590355b4669Sjacobs char prompt[BUFSIZ]; 591355b4669Sjacobs char *user, *svc_name, *passphrase; 592355b4669Sjacobs 593355b4669Sjacobs /* get the name of the service we are contacting */ 594355b4669Sjacobs if ((svc_name = papiServiceGetServiceName(svc)) == NULL) 595355b4669Sjacobs return (-1); 596355b4669Sjacobs 597355b4669Sjacobs /* find our who we are supposed to be */ 598355b4669Sjacobs if ((user = papiServiceGetUserName(svc)) == NULL) { 599355b4669Sjacobs struct passwd *pw; 600355b4669Sjacobs 601355b4669Sjacobs if ((pw = getpwuid(getuid())) != NULL) 602355b4669Sjacobs user = pw->pw_name; 603355b4669Sjacobs else 604355b4669Sjacobs user = "nobody"; 605355b4669Sjacobs } 606355b4669Sjacobs 607355b4669Sjacobs /* build the prompt string */ 608355b4669Sjacobs snprintf(prompt, sizeof (prompt), 609355b4669Sjacobs gettext("passphrase for %s to access %s: "), user, svc_name); 610355b4669Sjacobs 611355b4669Sjacobs /* ask for the passphrase */ 612355b4669Sjacobs if ((passphrase = getpassphrase(prompt)) != NULL) 613355b4669Sjacobs papiServiceSetPassword(svc, passphrase); 614355b4669Sjacobs 615355b4669Sjacobs return (0); 616355b4669Sjacobs } 617f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 618f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int32_t 619f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India job_to_be_queried(papi_service_t svc, char *printer, int32_t id) 620f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India { 621f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_job_t *jobs = NULL; 622f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_status_t status; 62306f61b77Ssonam gupta - Sun Microsystems - Bangalore India int ret = -1; 624f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India char *jattrs[] = { "job-id", 625f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", NULL }; 626f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 627f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL, 628f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 0, &jobs); 629f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 630f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (status != PAPI_OK) { 631f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("Failed to query service for %s: %s\n"), 632f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India printer, verbose_papi_message(svc, status)); 633f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (-1); 634f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 635f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 636f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (jobs != NULL) { 637f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int i = 0; 638f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 639f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India for (i = 0; jobs[i] != NULL; i++) { 640f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int32_t rid = -1; 64106f61b77Ssonam gupta - Sun Microsystems - Bangalore India int32_t jid = -1; 642f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_attribute_t **list = 643f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiJobGetAttributeList(jobs[i]); 644f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 645f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 646f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", &rid); 64706f61b77Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 64806f61b77Ssonam gupta - Sun Microsystems - Bangalore India "job-id", &jid); 649f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 65006f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 65106f61b77Ssonam gupta - Sun Microsystems - Bangalore India * check if id matches with either rid or jid 65206f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 653f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (rid == id) { 654f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India /* get the actual id and return it */ 655f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 656f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id", &id); 657f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (id); 65806f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else if (id == jid) { 65906f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) { 66006f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 66106f61b77Ssonam gupta - Sun Microsystems - Bangalore India * It is a remote lpd job 66206f61b77Ssonam gupta - Sun Microsystems - Bangalore India * can be cancelled only 66306f61b77Ssonam gupta - Sun Microsystems - Bangalore India * using rid 66406f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 66506f61b77Ssonam gupta - Sun Microsystems - Bangalore India ret = -1; 66606f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else { 66706f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 66806f61b77Ssonam gupta - Sun Microsystems - Bangalore India * its local or 66906f61b77Ssonam gupta - Sun Microsystems - Bangalore India * remote ipp job 67006f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 67106f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (id); 672f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 673f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 674f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 67506f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (ret); 67606f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 677f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (id); 678f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 679