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 /* 23355b4669Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24355b4669Sjacobs * Use is subject to license terms. 25355b4669Sjacobs * 26355b4669Sjacobs */ 27355b4669Sjacobs 28355b4669Sjacobs /* $Id: common.c 162 2006-05-08 14:17:44Z njacobs $ */ 29355b4669Sjacobs 30355b4669Sjacobs #pragma ident "%Z%%M% %I% %E% SMI" 31355b4669Sjacobs 32355b4669Sjacobs #include <stdio.h> 33355b4669Sjacobs #include <stdlib.h> 34355b4669Sjacobs #include <unistd.h> 35*0a44ef6dSjacobs #include <sys/types.h> 36*0a44ef6dSjacobs #include <sys/stat.h> 37*0a44ef6dSjacobs #include <fcntl.h> 38355b4669Sjacobs #include <alloca.h> 39355b4669Sjacobs #include <string.h> 40355b4669Sjacobs #include <libintl.h> 41355b4669Sjacobs #include <ctype.h> 42355b4669Sjacobs #include <pwd.h> 43355b4669Sjacobs #include <papi.h> 44355b4669Sjacobs #include "common.h" 45355b4669Sjacobs 46355b4669Sjacobs #ifndef HAVE_GETPASSPHRASE /* some systems don't have getpassphrase() */ 47355b4669Sjacobs #define getpassphrase getpass 48355b4669Sjacobs #endif 49355b4669Sjacobs 50355b4669Sjacobs /* give the most verbose error message to the caller */ 51355b4669Sjacobs char * 52355b4669Sjacobs verbose_papi_message(papi_service_t svc, papi_status_t status) 53355b4669Sjacobs { 54355b4669Sjacobs char *mesg; 55355b4669Sjacobs 56355b4669Sjacobs mesg = papiServiceGetStatusMessage(svc); 57355b4669Sjacobs 58355b4669Sjacobs if (mesg == NULL) 59355b4669Sjacobs mesg = papiStatusString(status); 60355b4669Sjacobs 61355b4669Sjacobs return (mesg); 62355b4669Sjacobs } 63355b4669Sjacobs 64355b4669Sjacobs static int 65355b4669Sjacobs match_job(int id, char *user, int ac, char *av[]) 66355b4669Sjacobs { 67355b4669Sjacobs int i; 68355b4669Sjacobs 69355b4669Sjacobs for (i = 0; i < ac; i++) 70355b4669Sjacobs if (strcmp("-", av[i]) == 0) 71355b4669Sjacobs return (0); /* "current" user match */ 72355b4669Sjacobs else if ((isdigit(av[i][0]) != 0) && (id == atoi(av[i]))) 73355b4669Sjacobs return (0); /* job-id match */ 74355b4669Sjacobs else if (strcmp(user, av[i]) == 0) 75355b4669Sjacobs return (0); /* user match */ 76355b4669Sjacobs 77355b4669Sjacobs return (-1); 78355b4669Sjacobs } 79355b4669Sjacobs 80*0a44ef6dSjacobs static struct { 81*0a44ef6dSjacobs char *mime_type; 82*0a44ef6dSjacobs char *lp_type; 83*0a44ef6dSjacobs } type_map[] = { 84*0a44ef6dSjacobs { "text/plain", "simple" }, 85*0a44ef6dSjacobs { "application/octet-stream", "raw" }, 86*0a44ef6dSjacobs { "application/octet-stream", "any" }, 87*0a44ef6dSjacobs { "application/postscript", "postscript" }, 88*0a44ef6dSjacobs { "application/postscript", "ps" }, 89*0a44ef6dSjacobs { "application/x-cif", "cif" }, 90*0a44ef6dSjacobs { "application/x-dvi", "dvi" }, 91*0a44ef6dSjacobs { "application/x-plot", "plot" }, 92*0a44ef6dSjacobs { "application/x-ditroff", "troff" }, 93*0a44ef6dSjacobs { "application/x-troff", "otroff" }, 94*0a44ef6dSjacobs { "application/x-pr", "pr" }, 95*0a44ef6dSjacobs { "application/x-fortran", "fortran" }, 96*0a44ef6dSjacobs { "application/x-raster", "raster" }, 97*0a44ef6dSjacobs { NULL, NULL} 98*0a44ef6dSjacobs }; 99*0a44ef6dSjacobs 100*0a44ef6dSjacobs char * 101*0a44ef6dSjacobs lp_type_to_mime_type(char *lp_type) 102*0a44ef6dSjacobs { 103*0a44ef6dSjacobs int i; 104*0a44ef6dSjacobs 105*0a44ef6dSjacobs if (lp_type == NULL) 106*0a44ef6dSjacobs return ("application/octet-stream"); 107*0a44ef6dSjacobs 108*0a44ef6dSjacobs for (i = 0; type_map[i].lp_type != NULL; i++) 109*0a44ef6dSjacobs if (strcasecmp(type_map[i].lp_type, lp_type) == 0) 110*0a44ef6dSjacobs return (type_map[i].mime_type); 111*0a44ef6dSjacobs 112*0a44ef6dSjacobs return (lp_type); 113*0a44ef6dSjacobs } 114*0a44ef6dSjacobs 115355b4669Sjacobs /* 116355b4669Sjacobs * to support job/printer status 117355b4669Sjacobs */ 118355b4669Sjacobs static char * 119355b4669Sjacobs state_string(int state) 120355b4669Sjacobs { 121355b4669Sjacobs switch (state) { 122355b4669Sjacobs case 3: 123355b4669Sjacobs return (gettext("idle")); 124355b4669Sjacobs case 4: 125355b4669Sjacobs return (gettext("processing")); 126355b4669Sjacobs case 5: 127355b4669Sjacobs return (gettext("stopped")); 128355b4669Sjacobs default: 129355b4669Sjacobs return (gettext("unknown")); 130355b4669Sjacobs } 131355b4669Sjacobs } 132355b4669Sjacobs 133355b4669Sjacobs static char *_rank_suffixes[] = { 134355b4669Sjacobs "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" 135355b4669Sjacobs }; 136355b4669Sjacobs 137355b4669Sjacobs static char * 138355b4669Sjacobs rank_string(const int rank) 139355b4669Sjacobs { 140355b4669Sjacobs static char buf[12]; 141355b4669Sjacobs 142355b4669Sjacobs if (rank < 0) 143355b4669Sjacobs snprintf(buf, sizeof (buf), gettext("invalid")); 144355b4669Sjacobs else if (rank == 0) 145355b4669Sjacobs snprintf(buf, sizeof (buf), gettext("active")); 146355b4669Sjacobs else if ((rank > 10) && (rank < 14)) 147355b4669Sjacobs sprintf(buf, "%dth", rank); 148355b4669Sjacobs else 149355b4669Sjacobs sprintf(buf, "%d%s", rank, _rank_suffixes[rank % 10]); 150355b4669Sjacobs 151355b4669Sjacobs return (buf); 152355b4669Sjacobs } 153355b4669Sjacobs 154355b4669Sjacobs static void 155355b4669Sjacobs printer_state_line(FILE *fp, papi_printer_t p, int num_jobs, char *name) 156355b4669Sjacobs { 157355b4669Sjacobs papi_attribute_t **list = papiPrinterGetAttributeList(p); 158355b4669Sjacobs int state = 0; 159355b4669Sjacobs char *reason = ""; 160355b4669Sjacobs 161355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 162355b4669Sjacobs "printer-state", &state); 163355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 164355b4669Sjacobs "printer-state-reasons", &reason); 165355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 166355b4669Sjacobs "printer-name", &name); 167355b4669Sjacobs 168355b4669Sjacobs if ((state != 0x03) || (num_jobs != 0)) { 169355b4669Sjacobs fprintf(fp, "%s: %s", name, state_string(state)); 170355b4669Sjacobs if (state == 0x05) /* stopped */ 171355b4669Sjacobs fprintf(fp, ": %s\n", reason); 172355b4669Sjacobs else 173355b4669Sjacobs fprintf(fp, "\n"); 174355b4669Sjacobs } else 175355b4669Sjacobs fprintf(fp, "no entries\n"); 176355b4669Sjacobs } 177355b4669Sjacobs 178355b4669Sjacobs static void 179355b4669Sjacobs print_header(FILE *fp) 180355b4669Sjacobs { 181355b4669Sjacobs fprintf(fp, gettext("Rank\tOwner\t Job\tFile(s)\t\t\t\tTotal Size\n")); 182355b4669Sjacobs } 183355b4669Sjacobs 184355b4669Sjacobs static void 185355b4669Sjacobs print_job_line(FILE *fp, int count, papi_job_t job, int fmt, int ac, char *av[]) 186355b4669Sjacobs { 187355b4669Sjacobs papi_attribute_t **list = papiJobGetAttributeList(job); 188355b4669Sjacobs int copies = 1, id = 0, rank = count, size = 0; 189355b4669Sjacobs char *name = "print job"; 190355b4669Sjacobs char *user = "nobody"; 191355b4669Sjacobs char *host = "localhost"; 192355b4669Sjacobs char *suffix = "k"; 193355b4669Sjacobs 194355b4669Sjacobs (void) papiAttributeListGetInteger(list, NULL, 195355b4669Sjacobs "job-id", &id); 196355b4669Sjacobs (void) papiAttributeListGetString(list, NULL, 197355b4669Sjacobs "job-originating-user-name", &user); 198*0a44ef6dSjacobs (void) papiAttributeListGetString(list, NULL, 199*0a44ef6dSjacobs "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; 241355b4669Sjacobs char *user = ""; 242355b4669Sjacobs char *mesg = gettext("cancelled"); 243355b4669Sjacobs 244355b4669Sjacobs papiAttributeListGetInteger(list, NULL, 245355b4669Sjacobs "job-id", &id); 246355b4669Sjacobs papiAttributeListGetString(list, NULL, 247355b4669Sjacobs "job-originating-user-name", &user); 248355b4669Sjacobs 249355b4669Sjacobs /* if we are looking and it doesn't match, return early */ 250355b4669Sjacobs if ((ac > 0) && (match_job(id, user, ac, av) < 0)) 251355b4669Sjacobs return; 252355b4669Sjacobs 253355b4669Sjacobs status = papiJobCancel(svc, printer, id); 254355b4669Sjacobs if (status != PAPI_OK) 255355b4669Sjacobs mesg = papiStatusString(status); 256355b4669Sjacobs 257355b4669Sjacobs fprintf(fp, "%s-%d: %s\n", printer, id, mesg); 258355b4669Sjacobs } 259355b4669Sjacobs 260355b4669Sjacobs int 261355b4669Sjacobs berkeley_queue_report(papi_service_t svc, FILE *fp, char *dest, int fmt, 262355b4669Sjacobs int ac, char *av[]) 263355b4669Sjacobs { 264355b4669Sjacobs papi_status_t status; 265355b4669Sjacobs papi_printer_t p = NULL; 266355b4669Sjacobs papi_job_t *jobs = NULL; 267355b4669Sjacobs char *pattrs[] = { "printer-name", "printer-state", 268355b4669Sjacobs "printer-state-reasons", NULL }; 269*0a44ef6dSjacobs char *jattrs[] = { "job-name", "job-octets", "job-k-octets", "job-id", 270*0a44ef6dSjacobs "job-originating-user-name", 271*0a44ef6dSjacobs "job-originating-host-name", 272355b4669Sjacobs "number-of-intervening-jobs", NULL }; 273355b4669Sjacobs int num_jobs = 0; 274355b4669Sjacobs 275355b4669Sjacobs status = papiPrinterQuery(svc, dest, pattrs, NULL, &p); 276355b4669Sjacobs if (status != PAPI_OK) { 277355b4669Sjacobs fprintf(fp, gettext( 278355b4669Sjacobs "Failed to query service for state of %s: %s\n"), 279355b4669Sjacobs dest, verbose_papi_message(svc, status)); 280355b4669Sjacobs return (-1); 281355b4669Sjacobs } 282355b4669Sjacobs 283355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 284355b4669Sjacobs 0, &jobs); 285355b4669Sjacobs if (status != PAPI_OK) { 286355b4669Sjacobs fprintf(fp, gettext( 287355b4669Sjacobs "Failed to query service for jobs on %s: %s\n"), 288355b4669Sjacobs dest, verbose_papi_message(svc, status)); 289355b4669Sjacobs return (-1); 290355b4669Sjacobs } 291355b4669Sjacobs if (jobs != NULL) { 292355b4669Sjacobs while (jobs[num_jobs] != NULL) 293355b4669Sjacobs num_jobs++; 294355b4669Sjacobs } 295355b4669Sjacobs 296355b4669Sjacobs printer_state_line(fp, p, num_jobs, dest); 297355b4669Sjacobs if (num_jobs > 0) { 298355b4669Sjacobs int i; 299355b4669Sjacobs 300355b4669Sjacobs if (fmt == 3) 301355b4669Sjacobs print_header(fp); 302355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 303355b4669Sjacobs print_job_line(fp, i, jobs[i], fmt, ac, av); 304355b4669Sjacobs } 305355b4669Sjacobs 306355b4669Sjacobs papiPrinterFree(p); 307355b4669Sjacobs papiJobListFree(jobs); 308355b4669Sjacobs 309355b4669Sjacobs return (num_jobs); 310355b4669Sjacobs } 311355b4669Sjacobs 312355b4669Sjacobs int 313355b4669Sjacobs berkeley_cancel_request(papi_service_t svc, FILE *fp, char *dest, 314355b4669Sjacobs int ac, char *av[]) 315355b4669Sjacobs { 316355b4669Sjacobs papi_status_t status; 317355b4669Sjacobs papi_job_t *jobs = NULL; 318355b4669Sjacobs char *jattrs[] = { "job-originating-user-name", "job-id", NULL }; 319355b4669Sjacobs 320355b4669Sjacobs status = papiPrinterListJobs(svc, dest, jattrs, PAPI_LIST_JOBS_ALL, 321355b4669Sjacobs 0, &jobs); 322355b4669Sjacobs 323355b4669Sjacobs if (status != PAPI_OK) { 324355b4669Sjacobs fprintf(fp, gettext("Failed to query service for %s: %s\n"), 325355b4669Sjacobs dest, verbose_papi_message(svc, status)); 326355b4669Sjacobs return (-1); 327355b4669Sjacobs } 328355b4669Sjacobs 329355b4669Sjacobs /* cancel the job(s) */ 330355b4669Sjacobs if (jobs != NULL) { 331355b4669Sjacobs int i; 332355b4669Sjacobs 333355b4669Sjacobs for (i = 0; jobs[i] != NULL; i++) 334355b4669Sjacobs cancel_job(svc, fp, dest, jobs[i], ac, av); 335355b4669Sjacobs } 336355b4669Sjacobs 337355b4669Sjacobs papiJobListFree(jobs); 338355b4669Sjacobs 339355b4669Sjacobs return (0); 340355b4669Sjacobs } 341355b4669Sjacobs 342355b4669Sjacobs int 343355b4669Sjacobs get_printer_id(char *name, char **printer, int *id) 344355b4669Sjacobs { 345355b4669Sjacobs int result = -1; 346355b4669Sjacobs 347355b4669Sjacobs if (name != NULL) { 348355b4669Sjacobs char *p = strrchr(name, '-'); 349355b4669Sjacobs 350355b4669Sjacobs *printer = name; 351355b4669Sjacobs if (p != NULL) { 352355b4669Sjacobs char *s = NULL; 353355b4669Sjacobs 354355b4669Sjacobs *id = strtol(p + 1, &s, 10); 355355b4669Sjacobs if (s[0] != '\0') 356355b4669Sjacobs *id = -1; 357355b4669Sjacobs else 358355b4669Sjacobs *p = '\0'; 359355b4669Sjacobs result = 0; 360355b4669Sjacobs } else 361355b4669Sjacobs *id = -1; 362355b4669Sjacobs } 363355b4669Sjacobs 364355b4669Sjacobs return (result); 365355b4669Sjacobs } 366355b4669Sjacobs 367355b4669Sjacobs /* 368355b4669Sjacobs * strsplit() splits a string into a NULL terminated array of substrings 369355b4669Sjacobs * determined by a seperator. The original string is modified, and newly 370355b4669Sjacobs * allocated space is only returned for the array itself. If more than 371355b4669Sjacobs * 1024 substrings exist, they will be ignored. 372355b4669Sjacobs */ 373355b4669Sjacobs char ** 374355b4669Sjacobs strsplit(char *string, const char *seperators) 375355b4669Sjacobs { 376355b4669Sjacobs char *list[BUFSIZ], 377355b4669Sjacobs **result; 378355b4669Sjacobs int length = 0; 379355b4669Sjacobs 380355b4669Sjacobs if ((string == NULL) || (seperators == NULL)) 381355b4669Sjacobs return (NULL); 382355b4669Sjacobs 383355b4669Sjacobs (void) memset(list, 0, sizeof (list)); 384355b4669Sjacobs for (list[length] = strtok(string, seperators); 385355b4669Sjacobs (list[length] != NULL) && (length < (BUFSIZ - 2)); 386355b4669Sjacobs list[length] = strtok(NULL, seperators)) 387355b4669Sjacobs length++; 388355b4669Sjacobs 389355b4669Sjacobs if ((result = (char **)calloc(length+1, sizeof (char *))) != NULL) 390355b4669Sjacobs (void) memcpy(result, list, length * sizeof (char *)); 391355b4669Sjacobs 392355b4669Sjacobs return (result); 393355b4669Sjacobs } 394355b4669Sjacobs 395355b4669Sjacobs papi_status_t 396355b4669Sjacobs jobSubmitSTDIN(papi_service_t svc, char *printer, papi_attribute_t **list, 397355b4669Sjacobs papi_job_t *job) 398355b4669Sjacobs { 399355b4669Sjacobs papi_status_t status; 400355b4669Sjacobs papi_stream_t stream = NULL; 401355b4669Sjacobs int rc; 402355b4669Sjacobs char buf[BUFSIZ]; 403355b4669Sjacobs 404355b4669Sjacobs status = papiJobStreamOpen(svc, printer, list, NULL, &stream); 405355b4669Sjacobs while ((status == PAPI_OK) && ((rc = read(0, buf, sizeof (buf))) > 0)) 406355b4669Sjacobs status = papiJobStreamWrite(svc, stream, buf, rc); 407355b4669Sjacobs 408355b4669Sjacobs if (status == PAPI_OK) 409355b4669Sjacobs status = papiJobStreamClose(svc, stream, job); 410355b4669Sjacobs 411355b4669Sjacobs return (status); 412355b4669Sjacobs } 413355b4669Sjacobs 414*0a44ef6dSjacobs /* 415*0a44ef6dSjacobs * is_postscript() will detect if the file passed in contains postscript 416*0a44ef6dSjacobs * data. A one is returned if the file contains postscript, zero is returned 417*0a44ef6dSjacobs * if the file is not postscript, and -1 is returned if an error occurs 418*0a44ef6dSjacobs */ 419*0a44ef6dSjacobs #define PS_MAGIC "%!" 420*0a44ef6dSjacobs #define PC_PS_MAGIC "^D%!" 421*0a44ef6dSjacobs int 422*0a44ef6dSjacobs is_postscript(const char *file) 423*0a44ef6dSjacobs { 424*0a44ef6dSjacobs char buf[3]; 425*0a44ef6dSjacobs int fd; 426*0a44ef6dSjacobs 427*0a44ef6dSjacobs if ((fd = open(file, O_RDONLY)) < 0) 428*0a44ef6dSjacobs return (-1); 429*0a44ef6dSjacobs 430*0a44ef6dSjacobs if (read(fd, buf, sizeof (buf)) < 0) { 431*0a44ef6dSjacobs close(fd); 432*0a44ef6dSjacobs return (-1); 433*0a44ef6dSjacobs } 434*0a44ef6dSjacobs close(fd); 435*0a44ef6dSjacobs 436*0a44ef6dSjacobs if ((strncmp(buf, PS_MAGIC, sizeof (PS_MAGIC) - 1) == 0) || 437*0a44ef6dSjacobs (strncmp(buf, PC_PS_MAGIC, sizeof (PC_PS_MAGIC) - 1) == 0)) 438*0a44ef6dSjacobs return (1); 439*0a44ef6dSjacobs else 440*0a44ef6dSjacobs return (0); 441*0a44ef6dSjacobs } 442*0a44ef6dSjacobs 443355b4669Sjacobs static char ** 444355b4669Sjacobs all_list(papi_service_t svc) 445355b4669Sjacobs { 446355b4669Sjacobs papi_status_t status; 447355b4669Sjacobs papi_printer_t printer = NULL; 448355b4669Sjacobs char *list[] = { "member-names", NULL }; 449355b4669Sjacobs char **result = NULL; 450355b4669Sjacobs 451355b4669Sjacobs status = papiPrinterQuery(svc, "_all", list, NULL, &printer); 452355b4669Sjacobs if ((status == PAPI_OK) && (printer != NULL)) { 453355b4669Sjacobs papi_attribute_t **attributes = 454355b4669Sjacobs papiPrinterGetAttributeList(printer); 455355b4669Sjacobs if (attributes != NULL) { 456355b4669Sjacobs void *iter = NULL; 457355b4669Sjacobs char *value = NULL; 458355b4669Sjacobs 459355b4669Sjacobs for (status = papiAttributeListGetString(attributes, 460355b4669Sjacobs &iter, "member-names", &value); 461355b4669Sjacobs status == PAPI_OK; 462355b4669Sjacobs status = papiAttributeListGetString(attributes, 463355b4669Sjacobs &iter, NULL, &value)) 464355b4669Sjacobs list_append(&result, strdup(value)); 465355b4669Sjacobs } 466355b4669Sjacobs papiPrinterFree(printer); 467355b4669Sjacobs } 468355b4669Sjacobs 469355b4669Sjacobs return (result); 470355b4669Sjacobs } 471355b4669Sjacobs 472355b4669Sjacobs static char ** 473355b4669Sjacobs printers_list(papi_service_t svc) 474355b4669Sjacobs { 475355b4669Sjacobs papi_status_t status; 476355b4669Sjacobs papi_printer_t *printers = NULL; 477355b4669Sjacobs char *keys[] = { "printer-name", NULL }; 478355b4669Sjacobs char **result = NULL; 479355b4669Sjacobs 480355b4669Sjacobs status = papiPrintersList(svc, keys, NULL, &printers); 481355b4669Sjacobs if ((status == PAPI_OK) && (printers != NULL)) { 482355b4669Sjacobs int i; 483355b4669Sjacobs 484355b4669Sjacobs for (i = 0; printers[i] != NULL; i++) { 485355b4669Sjacobs papi_attribute_t **attributes = 486355b4669Sjacobs papiPrinterGetAttributeList(printers[i]); 487355b4669Sjacobs char *name = NULL; 488355b4669Sjacobs 489355b4669Sjacobs (void) papiAttributeListGetString(attributes, NULL, 490355b4669Sjacobs "printer-name", &name); 491355b4669Sjacobs if ((name != NULL) && (strcmp(name, "_default") != 0)) 492355b4669Sjacobs list_append(&result, strdup(name)); 493355b4669Sjacobs } 494355b4669Sjacobs papiPrinterListFree(printers); 495355b4669Sjacobs } 496355b4669Sjacobs 497355b4669Sjacobs return (result); 498355b4669Sjacobs } 499355b4669Sjacobs 500355b4669Sjacobs char ** 501355b4669Sjacobs interest_list(papi_service_t svc) 502355b4669Sjacobs { 503355b4669Sjacobs static char been_here; 504355b4669Sjacobs static char **result; 505355b4669Sjacobs 506355b4669Sjacobs if (been_here == 0) { /* only do this once */ 507355b4669Sjacobs been_here = 1; 508355b4669Sjacobs 509355b4669Sjacobs if ((result = all_list(svc)) == NULL) 510355b4669Sjacobs result = printers_list(svc); 511355b4669Sjacobs } 512355b4669Sjacobs 513355b4669Sjacobs return (result); 514355b4669Sjacobs } 515355b4669Sjacobs 516355b4669Sjacobs char * 517355b4669Sjacobs localhostname() 518355b4669Sjacobs { 519355b4669Sjacobs static char *result; 520355b4669Sjacobs 521355b4669Sjacobs if (result == NULL) { 522355b4669Sjacobs static char buf[256]; 523355b4669Sjacobs 524355b4669Sjacobs if (gethostname(buf, sizeof (buf)) == 0) 525355b4669Sjacobs result = buf; 526355b4669Sjacobs } 527355b4669Sjacobs 528355b4669Sjacobs return (result); 529355b4669Sjacobs } 530355b4669Sjacobs 531355b4669Sjacobs int 532355b4669Sjacobs cli_auth_callback(papi_service_t svc, void *app_data) 533355b4669Sjacobs { 534355b4669Sjacobs char prompt[BUFSIZ]; 535355b4669Sjacobs char *user, *svc_name, *passphrase; 536355b4669Sjacobs 537355b4669Sjacobs /* get the name of the service we are contacting */ 538355b4669Sjacobs if ((svc_name = papiServiceGetServiceName(svc)) == NULL) 539355b4669Sjacobs return (-1); 540355b4669Sjacobs 541355b4669Sjacobs /* find our who we are supposed to be */ 542355b4669Sjacobs if ((user = papiServiceGetUserName(svc)) == NULL) { 543355b4669Sjacobs struct passwd *pw; 544355b4669Sjacobs 545355b4669Sjacobs if ((pw = getpwuid(getuid())) != NULL) 546355b4669Sjacobs user = pw->pw_name; 547355b4669Sjacobs else 548355b4669Sjacobs user = "nobody"; 549355b4669Sjacobs } 550355b4669Sjacobs 551355b4669Sjacobs /* build the prompt string */ 552355b4669Sjacobs snprintf(prompt, sizeof (prompt), 553355b4669Sjacobs gettext("passphrase for %s to access %s: "), user, svc_name); 554355b4669Sjacobs 555355b4669Sjacobs /* ask for the passphrase */ 556355b4669Sjacobs if ((passphrase = getpassphrase(prompt)) != NULL) 557355b4669Sjacobs papiServiceSetPassword(svc, passphrase); 558355b4669Sjacobs 559355b4669Sjacobs return (0); 560355b4669Sjacobs } 561