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 78*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 79*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * return 0 : argument passed is job-id && job-id matches 80*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * or argument passed is user 81*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 82*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India static int 83*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India match_job_rid(int id, int ac, char *av[]) 84*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India { 85*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India int i; 86*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India 87*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India for (i = 0; i < ac; i++) 88*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (isdigit(av[i][0]) != 0) { 89*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (id == atoi(av[i])) 90*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* job-id match */ 91*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (0); 92*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else 93*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* argument passed is user */ 94*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (0); 95*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (-1); 96*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 97*06f61b77Ssonam 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)); 188355b4669Sjacobs if (state == 0x05) /* stopped */ 189355b4669Sjacobs fprintf(fp, ": %s\n", reason); 190355b4669Sjacobs else 191355b4669Sjacobs fprintf(fp, "\n"); 192355b4669Sjacobs } else 193355b4669Sjacobs fprintf(fp, "no entries\n"); 194355b4669Sjacobs } 195355b4669Sjacobs 196355b4669Sjacobs static void 197355b4669Sjacobs print_header(FILE *fp) 198355b4669Sjacobs { 199355b4669Sjacobs fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n")); 200355b4669Sjacobs } 201355b4669Sjacobs 202355b4669Sjacobs static void 203355b4669Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[]) 204355b4669Sjacobs { 205355b4669Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job); 206355b4669Sjacobs int copies = 1, id = 0, rank = count, size = 0; 207355b4669Sjacobs char *name = "print job"; 208355b4669Sjacobs char *user = "nobody"; 209355b4669Sjacobs char *host = "localhost"; 210355b4669Sjacobs char *suffix = "k"; 211355b4669Sjacobs 212355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 213355b4669Sjacobs "job-id", &id); 21436615d24Sjacobs (void) papiAttributeListGetInteger(list, NULL, 21536615d24Sjacobs "job-id-requested", &id); 216355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 217355b4669Sjacobs "job-originating-user-name", &user); 2180a44ef6dSjacobs (void) papiAttributeListGetString(list, NULL, 2190a44ef6dSjacobs "job-originating-host-name", &host); 220355b4669Sjacobs 221355b4669Sjacobs /* if we are looking and it doesn't match, return early */ 222355b4669Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0)) 223355b4669Sjacobs return; 224355b4669Sjacobs 225355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 226355b4669Sjacobs "copies", &copies); 227355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 228355b4669Sjacobs "number-of-intervening-jobs", &rank); 229355b4669Sjacobs 230355b4669Sjacobs if (papiAttributeListGetInteger(list, NULL, "job-octets", &size) 231355b4669Sjacobs == PAPI_OK) 232355b4669Sjacobs suffix = "bytes"; 233355b4669Sjacobs else 234355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 235355b4669Sjacobs "job-k-octets", &size); 236355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 237355b4669Sjacobs "job-name", &name); 238355b4669Sjacobs 239355b4669Sjacobs size *= copies; 240355b4669Sjacobs 241355b4669Sjacobs if (fmt == 3) { 242355b4669Sjacobs fprintf(fp, gettext("%s\t%-8.8s %d\t%-32.32s%d %s\n"), 243355b4669Sjacobs rank_string(++rank), user, id, name, size, suffix); 244355b4669Sjacobs } else 245355b4669Sjacobs fprintf(fp, gettext( 246355b4669Sjacobs "\n%s: %s\t\t\t\t[job %d %s]\n\t%-32.32s\t%d %s\n"), 247355b4669Sjacobs user, rank_string(++rank), id, host, name, size, 248355b4669Sjacobs suffix); 249355b4669Sjacobs } 250355b4669Sjacobs 251355b4669Sjacobs /* 252355b4669Sjacobs * to support job cancelation 253355b4669Sjacobs */ 254355b4669Sjacobs static void 255355b4669Sjacobs cancel_job(papi_service_t svc, FILE *fp, char *printer, papi_job_t job, 256355b4669Sjacobs int ac, char *av[]) 257355b4669Sjacobs { 258355b4669Sjacobs papi_status_t status; 259355b4669Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job); 260*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India int id = -1; 261dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India int rid = -1; 262355b4669Sjacobs char *user = ""; 263355b4669Sjacobs char *mesg = gettext("cancelled"); 264*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India int i = 0; 265355b4669Sjacobs 266355b4669Sjacobs papiAttributeListGetInteger(list, NULL, 267355b4669Sjacobs "job-id", &id); 26836615d24Sjacobs papiAttributeListGetInteger(list, NULL, 26936615d24Sjacobs "job-id-requested", &rid); 270355b4669Sjacobs papiAttributeListGetString(list, NULL, 271355b4669Sjacobs "job-originating-user-name", &user); 272355b4669Sjacobs 273355b4669Sjacobs /* if we are looking and it doesn't match, return early */ 27436615d24Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0) && 27536615d24Sjacobs (match_job(rid, user, ac, av) < 0)) 276355b4669Sjacobs return; 277355b4669Sjacobs 278*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 279*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * A remote lpd job should be cancelled only based on 280*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * job-id-requested 281*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 282*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) { 283*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (match_job_rid(rid, ac, av) == -1) 284*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* job-id mismatch */ 285*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return; 286*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 287*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India 288355b4669Sjacobs status = papiJobCancel(svc, printer, id); 289355b4669Sjacobs if (status != PAPI_OK) 290355b4669Sjacobs mesg = papiStatusString(status); 291355b4669Sjacobs 292dcf1b443Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) 2936b5764c3Ssonam gupta - Sun Microsystems - Bangalore India fprintf(fp, "%s-%d: %s\n", printer, rid, mesg); 29440e7ce05Ssonam gupta - Sun Microsystems - Bangalore India else 29540e7ce05Ssonam gupta - Sun Microsystems - Bangalore India fprintf(fp, "%s-%d: %s\n", printer, id, mesg); 296355b4669Sjacobs } 297355b4669Sjacobs 298355b4669Sjacobs int 299355b4669Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt, 300355b4669Sjacobs int ac, char *av[]) 301355b4669Sjacobs { 302355b4669Sjacobs papi_status_t status; 303355b4669Sjacobs papi_printer_t p = NULL; 304355b4669Sjacobs papi_job_t *jobs = NULL; 305355b4669Sjacobs char *pattrs[] = { "printer-name", "printer-state", 306355b4669Sjacobs "printer-state-reasons", NULL }; 3070a44ef6dSjacobs char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id", 30836615d24Sjacobs "job-originating-user-name", "job-id-requested", 3090a44ef6dSjacobs "job-originating-host-name", 310355b4669Sjacobs "number-of-intervening-jobs", NULL }; 311355b4669Sjacobs int num_jobs = 0; 312355b4669Sjacobs 313355b4669Sjacobs status = papiPrinterQuery(svc, dest, pattrs, NULL, &p); 314355b4669Sjacobs if (status != PAPI_OK) { 315355b4669Sjacobs fprintf(fp, gettext( 316355b4669Sjacobs "Failed to query service for state of %s: %s\n"), 317355b4669Sjacobs dest, verbose_papi_message(svc, status)); 318355b4669Sjacobs return (-1); 319355b4669Sjacobs } 320355b4669Sjacobs 321355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 322355b4669Sjacobs 0, &jobs); 323355b4669Sjacobs if (status != PAPI_OK) { 324355b4669Sjacobs fprintf(fp, gettext( 325355b4669Sjacobs "Failed to query service for jobs on %s: %s\n"), 326355b4669Sjacobs dest, verbose_papi_message(svc, status)); 327355b4669Sjacobs return (-1); 328355b4669Sjacobs } 329355b4669Sjacobs if (jobs != NULL) { 330355b4669Sjacobs while (jobs[num_jobs] != NULL) 331355b4669Sjacobs num_jobs++; 332355b4669Sjacobs } 333355b4669Sjacobs 334355b4669Sjacobs printer_state_line(fp, p, num_jobs, dest); 335355b4669Sjacobs if (num_jobs > 0) { 336355b4669Sjacobs int i; 337355b4669Sjacobs 338355b4669Sjacobs if (fmt == 3) 339355b4669Sjacobs print_header(fp); 340355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 341355b4669Sjacobs print_job_line(fp, i, jobs[i], fmt, ac, av); 342355b4669Sjacobs } 343355b4669Sjacobs 344355b4669Sjacobs papiPrinterFree(p); 345355b4669Sjacobs papiJobListFree(jobs); 346355b4669Sjacobs 347355b4669Sjacobs return (num_jobs); 348355b4669Sjacobs } 349355b4669Sjacobs 350355b4669Sjacobs int 351355b4669Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest, 352355b4669Sjacobs int ac, char *av[]) 353355b4669Sjacobs { 354355b4669Sjacobs papi_status_t status; 355355b4669Sjacobs papi_job_t *jobs = NULL; 35636615d24Sjacobs char *jattrs[] = { "job-originating-user-name", "job-id", 35736615d24Sjacobs "job-id-requested", NULL }; 358355b4669Sjacobs 359355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 360355b4669Sjacobs 0, &jobs); 361355b4669Sjacobs 362355b4669Sjacobs if (status != PAPI_OK) { 363355b4669Sjacobs fprintf(fp, gettext("Failed to query service for %s: %s\n"), 364355b4669Sjacobs dest, verbose_papi_message(svc, status)); 365355b4669Sjacobs return (-1); 366355b4669Sjacobs } 367355b4669Sjacobs 368355b4669Sjacobs /* cancel the job(s) */ 369355b4669Sjacobs if (jobs != NULL) { 370355b4669Sjacobs int i; 371355b4669Sjacobs 372355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 373355b4669Sjacobs cancel_job(svc, fp, dest, jobs[i], ac, av); 374355b4669Sjacobs } 375355b4669Sjacobs 376355b4669Sjacobs papiJobListFree(jobs); 377355b4669Sjacobs 378355b4669Sjacobs return (0); 379355b4669Sjacobs } 380355b4669Sjacobs 381355b4669Sjacobs int 382355b4669Sjacobs get_printer_id(char *name, char **printer, int *id) 383355b4669Sjacobs { 384355b4669Sjacobs int result = -1; 385355b4669Sjacobs 386355b4669Sjacobs if (name != NULL) { 387355b4669Sjacobs char *p = strrchr(name, '-'); 388355b4669Sjacobs 389355b4669Sjacobs *printer = name; 390355b4669Sjacobs if (p != NULL) { 391355b4669Sjacobs char *s = NULL; 392355b4669Sjacobs 393355b4669Sjacobs *id = strtol(p + 1, &s, 10); 394355b4669Sjacobs if (s[0] != '\0') 395355b4669Sjacobs *id = -1; 396355b4669Sjacobs else 397355b4669Sjacobs *p = '\0'; 398355b4669Sjacobs result = 0; 399355b4669Sjacobs } else 400355b4669Sjacobs *id = -1; 401355b4669Sjacobs } 402355b4669Sjacobs 403355b4669Sjacobs return (result); 404355b4669Sjacobs } 405355b4669Sjacobs 406355b4669Sjacobs /* 407355b4669Sjacobs * strsplit() splits a string into a NULL terminated array of substrings 408355b4669Sjacobs * determined by a seperator. The original string is modified, and newly 409355b4669Sjacobs * allocated space is only returned for the array itself. If more than 410355b4669Sjacobs * 1024 substrings exist, they will be ignored. 411355b4669Sjacobs */ 412355b4669Sjacobs char ** 413355b4669Sjacobs strsplit(char *string, const char *seperators) 414355b4669Sjacobs { 415355b4669Sjacobs char *list[BUFSIZ], 416355b4669Sjacobs **result; 417355b4669Sjacobs int length = 0; 418355b4669Sjacobs 419355b4669Sjacobs if ((string == NULL) || (seperators == NULL)) 420355b4669Sjacobs return (NULL); 421355b4669Sjacobs 422355b4669Sjacobs (void) memset(list, 0, sizeof (list)); 423355b4669Sjacobs for (list[length] = strtok(string, seperators); 424355b4669Sjacobs (list[length] != NULL) && (length < (BUFSIZ - 2)); 425355b4669Sjacobs list[length] = strtok(NULL, seperators)) 426355b4669Sjacobs length++; 427355b4669Sjacobs 428355b4669Sjacobs if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL) 429355b4669Sjacobs (void) memcpy(result, list, length * sizeof (char *)); 430355b4669Sjacobs 431355b4669Sjacobs return (result); 432355b4669Sjacobs } 433355b4669Sjacobs 434355b4669Sjacobs papi_status_t 435c1ecd8b9Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, char *prefetch, int len, 436c1ecd8b9Sjacobs papi_attribute_t **list, papi_job_t *job) 437355b4669Sjacobs { 438355b4669Sjacobs papi_status_t status; 439355b4669Sjacobs papi_stream_t stream = NULL; 440355b4669Sjacobs int rc; 441355b4669Sjacobs char buf[BUFSIZ]; 442355b4669Sjacobs 443355b4669Sjacobs status = papiJobStreamOpen(svc, printer, list, NULL, &stream); 444c1ecd8b9Sjacobs 445c1ecd8b9Sjacobs if (len > 0) 446c1ecd8b9Sjacobs status = papiJobStreamWrite(svc, stream, prefetch, len); 447c1ecd8b9Sjacobs 448355b4669Sjacobs while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0)) 449355b4669Sjacobs status = papiJobStreamWrite(svc, stream, buf, rc); 450355b4669Sjacobs 451355b4669Sjacobs if (status == PAPI_OK) 452355b4669Sjacobs status = papiJobStreamClose(svc, stream, job); 453355b4669Sjacobs 454355b4669Sjacobs return (status); 455355b4669Sjacobs } 456355b4669Sjacobs 4570a44ef6dSjacobs /* 4580a44ef6dSjacobs * is_postscript() will detect if the file passed in contains postscript 4590a44ef6dSjacobs * data. A one is returned if the file contains postscript, zero is returned 4600a44ef6dSjacobs * if the file is not postscript, and -1 is returned if an error occurs 4610a44ef6dSjacobs */ 4620a44ef6dSjacobs #define PS_MAGIC "%!" 4630a44ef6dSjacobs #define PC_PS_MAGIC "^D%!" 4640a44ef6dSjacobs int 465c1ecd8b9Sjacobs is_postscript_stream(int fd, char *buf, int *len) 4660a44ef6dSjacobs { 467c1ecd8b9Sjacobs if ((*len = read(fd, buf, *len)) < 0) { 4680a44ef6dSjacobs close(fd); 4690a44ef6dSjacobs return (-1); 4700a44ef6dSjacobs } 4710a44ef6dSjacobs 4720a44ef6dSjacobs if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) || 4730a44ef6dSjacobs (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0)) 4740a44ef6dSjacobs return (1); 4750a44ef6dSjacobs else 4760a44ef6dSjacobs return (0); 4770a44ef6dSjacobs } 4780a44ef6dSjacobs 479c1ecd8b9Sjacobs int 480c1ecd8b9Sjacobs is_postscript(const char *file) 481c1ecd8b9Sjacobs { 482c1ecd8b9Sjacobs int rc = -1; 483c1ecd8b9Sjacobs int fd; 484c1ecd8b9Sjacobs 485c1ecd8b9Sjacobs if ((fd = open(file, O_RDONLY)) >= 0) { 486c1ecd8b9Sjacobs char buf[3]; 487c1ecd8b9Sjacobs int len = sizeof (buf); 488c1ecd8b9Sjacobs 489c1ecd8b9Sjacobs rc = is_postscript_stream(fd, buf, &len); 490c1ecd8b9Sjacobs close(fd); 491c1ecd8b9Sjacobs } 492c1ecd8b9Sjacobs 493c1ecd8b9Sjacobs return (rc); 494c1ecd8b9Sjacobs } 495c1ecd8b9Sjacobs 496355b4669Sjacobs static char ** 497355b4669Sjacobs all_list(papi_service_t svc) 498355b4669Sjacobs { 499355b4669Sjacobs papi_status_t status; 500355b4669Sjacobs papi_printer_t printer = NULL; 501355b4669Sjacobs char *list[] = { "member-names", NULL }; 502355b4669Sjacobs char **result = NULL; 503355b4669Sjacobs 504355b4669Sjacobs status = papiPrinterQuery(svc, "_all", list, NULL, &printer); 505355b4669Sjacobs if ((status == PAPI_OK) && (printer != NULL)) { 506355b4669Sjacobs papi_attribute_t **attributes = 507355b4669Sjacobs papiPrinterGetAttributeList(printer); 508355b4669Sjacobs if (attributes != NULL) { 509355b4669Sjacobs void *iter = NULL; 510355b4669Sjacobs char *value = NULL; 511355b4669Sjacobs 512355b4669Sjacobs for (status = papiAttributeListGetString(attributes, 513355b4669Sjacobs &iter, "member-names", &value); 514355b4669Sjacobs status == PAPI_OK; 515355b4669Sjacobs status = papiAttributeListGetString(attributes, 516355b4669Sjacobs &iter, NULL, &value)) 517355b4669Sjacobs list_append(&result, strdup(value)); 518355b4669Sjacobs } 519355b4669Sjacobs papiPrinterFree(printer); 520355b4669Sjacobs } 521355b4669Sjacobs 522355b4669Sjacobs return (result); 523355b4669Sjacobs } 524355b4669Sjacobs 525355b4669Sjacobs static char ** 526355b4669Sjacobs printers_list(papi_service_t svc) 527355b4669Sjacobs { 528355b4669Sjacobs papi_status_t status; 529355b4669Sjacobs papi_printer_t *printers = NULL; 530355b4669Sjacobs char *keys[] = { "printer-name", NULL }; 531355b4669Sjacobs char **result = NULL; 532355b4669Sjacobs 533355b4669Sjacobs status = papiPrintersList(svc, keys, NULL, &printers); 534355b4669Sjacobs if ((status == PAPI_OK) && (printers != NULL)) { 535355b4669Sjacobs int i; 536355b4669Sjacobs 537355b4669Sjacobs for (i = 0; printers[i] != NULL; i++) { 538355b4669Sjacobs papi_attribute_t **attributes = 539355b4669Sjacobs papiPrinterGetAttributeList(printers[i]); 540355b4669Sjacobs char *name = NULL; 541355b4669Sjacobs 542355b4669Sjacobs (void) papiAttributeListGetString(attributes, NULL, 543355b4669Sjacobs "printer-name", &name); 544355b4669Sjacobs if ((name != NULL) && (strcmp(name, "_default") != 0)) 545355b4669Sjacobs list_append(&result, strdup(name)); 546355b4669Sjacobs } 547355b4669Sjacobs papiPrinterListFree(printers); 548355b4669Sjacobs } 549355b4669Sjacobs 550355b4669Sjacobs return (result); 551355b4669Sjacobs } 552355b4669Sjacobs 553355b4669Sjacobs char ** 554355b4669Sjacobs interest_list(papi_service_t svc) 555355b4669Sjacobs { 556355b4669Sjacobs static char been_here; 557355b4669Sjacobs static char **result; 558355b4669Sjacobs 559355b4669Sjacobs if (been_here == 0) { /* only do this once */ 560355b4669Sjacobs been_here = 1; 561355b4669Sjacobs 562355b4669Sjacobs if ((result = all_list(svc)) == NULL) 563355b4669Sjacobs result = printers_list(svc); 564355b4669Sjacobs } 565355b4669Sjacobs 566355b4669Sjacobs return (result); 567355b4669Sjacobs } 568355b4669Sjacobs 569355b4669Sjacobs char * 570355b4669Sjacobs localhostname() 571355b4669Sjacobs { 572355b4669Sjacobs static char *result; 573355b4669Sjacobs 574355b4669Sjacobs if (result == NULL) { 575355b4669Sjacobs static char buf[256]; 576355b4669Sjacobs 577355b4669Sjacobs if (gethostname(buf, sizeof (buf)) == 0) 578355b4669Sjacobs result = buf; 579355b4669Sjacobs } 580355b4669Sjacobs 581355b4669Sjacobs return (result); 582355b4669Sjacobs } 583355b4669Sjacobs 584355b4669Sjacobs int 585355b4669Sjacobs cli_auth_callback(papi_service_t svc, void *app_data) 586355b4669Sjacobs { 587355b4669Sjacobs char prompt[BUFSIZ]; 588355b4669Sjacobs char *user, *svc_name, *passphrase; 589355b4669Sjacobs 590355b4669Sjacobs /* get the name of the service we are contacting */ 591355b4669Sjacobs if ((svc_name = papiServiceGetServiceName(svc)) == NULL) 592355b4669Sjacobs return (-1); 593355b4669Sjacobs 594355b4669Sjacobs /* find our who we are supposed to be */ 595355b4669Sjacobs if ((user = papiServiceGetUserName(svc)) == NULL) { 596355b4669Sjacobs struct passwd *pw; 597355b4669Sjacobs 598355b4669Sjacobs if ((pw = getpwuid(getuid())) != NULL) 599355b4669Sjacobs user = pw->pw_name; 600355b4669Sjacobs else 601355b4669Sjacobs user = "nobody"; 602355b4669Sjacobs } 603355b4669Sjacobs 604355b4669Sjacobs /* build the prompt string */ 605355b4669Sjacobs snprintf(prompt, sizeof (prompt), 606355b4669Sjacobs gettext("passphrase for %s to access %s: "), user, svc_name); 607355b4669Sjacobs 608355b4669Sjacobs /* ask for the passphrase */ 609355b4669Sjacobs if ((passphrase = getpassphrase(prompt)) != NULL) 610355b4669Sjacobs papiServiceSetPassword(svc, passphrase); 611355b4669Sjacobs 612355b4669Sjacobs return (0); 613355b4669Sjacobs } 614f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 615f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int32_t 616f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India job_to_be_queried(papi_service_t svc, char *printer, int32_t id) 617f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India { 618f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_job_t *jobs = NULL; 619f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_status_t status; 620*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India int ret = -1; 621f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India char *jattrs[] = { "job-id", 622f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", NULL }; 623f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 624f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India status = papiPrinterListJobs(svc, printer, jattrs, PAPI_LIST_JOBS_ALL, 625f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 0, &jobs); 626f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 627f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (status != PAPI_OK) { 628f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India fprintf(stderr, gettext("Failed to query service for %s: %s\n"), 629f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India printer, verbose_papi_message(svc, status)); 630f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (-1); 631f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 632f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 633f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (jobs != NULL) { 634f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int i = 0; 635f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 636f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India for (i = 0; jobs[i] != NULL; i++) { 637f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India int32_t rid = -1; 638*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India int32_t jid = -1; 639f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papi_attribute_t **list = 640f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiJobGetAttributeList(jobs[i]); 641f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 642f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 643f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id-requested", &rid); 644*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 645*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India "job-id", &jid); 646f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India 647*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 648*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * check if id matches with either rid or jid 649*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 650f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India if (rid == id) { 651f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India /* get the actual id and return it */ 652f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India papiAttributeListGetInteger(list, NULL, 653f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India "job-id", &id); 654f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (id); 655*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else if (id == jid) { 656*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India if (rid != -1) { 657*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 658*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * It is a remote lpd job 659*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * can be cancelled only 660*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * using rid 661*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 662*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India ret = -1; 663*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } else { 664*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India /* 665*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * its local or 666*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India * remote ipp job 667*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India */ 668*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (id); 669f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 670f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 671f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 672*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India return (ret); 673*06f61b77Ssonam gupta - Sun Microsystems - Bangalore India } 674f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India return (id); 675f0b87b90Ssonam gupta - Sun Microsystems - Bangalore India } 676