1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 * 26 */ 27 28 /* $Id: lpstat.c 173 2006-05-25 04:52:06Z njacobs $ */ 29 30 31 #include <stdio.h> 32 #include <stdlib.h> 33 #include <unistd.h> 34 #include <string.h> 35 #include <locale.h> 36 #include <libintl.h> 37 #include <ctype.h> 38 #include <pwd.h> 39 #include <papi.h> 40 #include <uri.h> 41 #include "common.h" 42 43 static void 44 usage(char *program) 45 { 46 char *name; 47 48 if ((name = strrchr(program, '/')) == NULL) 49 name = program; 50 else 51 name++; 52 53 fprintf(stdout, gettext("Usage: %s [-d] [-r] [-s] [-t] [-a [list]] " 54 "[-c [list]] [-o [list] [-l]] [-R [list] [-l]] " 55 "[-p [list] [-D] [-l]] [-v [list]] [-S [list] [-l]] " 56 "[-f [list] [-l]] [-u list]\n"), 57 name); 58 exit(1); 59 } 60 61 static char * 62 nctime(time_t *t) 63 { 64 static char buf[64]; 65 struct tm *tm = localtime(t); 66 67 (void) strftime(buf, sizeof (buf), "%c", tm); 68 69 return (buf); 70 } 71 72 static char * 73 printer_name(papi_printer_t printer) 74 { 75 papi_attribute_t **attributes = papiPrinterGetAttributeList(printer); 76 char *result = NULL; 77 78 if (attributes != NULL) 79 papiAttributeListGetString(attributes, NULL, 80 "printer-name", &result); 81 82 return (result); 83 } 84 85 static int 86 lpstat_default_printer(papi_encryption_t encryption) 87 { 88 papi_status_t status; 89 papi_service_t svc = NULL; 90 papi_printer_t p = NULL; 91 char *name = NULL; 92 93 status = papiServiceCreate(&svc, NULL, NULL, NULL, cli_auth_callback, 94 encryption, NULL); 95 if (status == PAPI_OK) { 96 char *req[] = { "printer-name", NULL }; 97 98 status = papiPrinterQuery(svc, DEFAULT_DEST, req, NULL, &p); 99 if (p != NULL) 100 name = printer_name(p); 101 } 102 if (name != NULL) 103 printf(gettext("system default printer: %s\n"), name); 104 else 105 printf(gettext("no system default destination\n")); 106 papiPrinterFree(p); 107 papiServiceDestroy(svc); 108 109 return (0); 110 } 111 112 static int 113 lpstat_service_status(papi_encryption_t encryption) 114 { 115 int result = 0; 116 papi_status_t status; 117 papi_service_t svc = NULL; 118 char *name = NULL; 119 120 if (((name = getenv("PAPI_SERVICE_URI")) == NULL) && 121 ((name = getenv("IPP_SERVER")) == NULL) && 122 ((name = getenv("CUPS_SERVER")) == NULL)) 123 name = DEFAULT_SERVICE_URI; 124 125 status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback, 126 encryption, NULL); 127 if (status != PAPI_OK) { 128 printf(gettext("scheduler is not running\n")); 129 result = -1; 130 } else 131 printf(gettext("scheduler is running\n")); 132 papiServiceDestroy(svc); 133 134 return (result); 135 } 136 137 static char * 138 get_device_uri(papi_service_t svc, char *name) 139 { 140 papi_status_t status; 141 papi_printer_t p = NULL; 142 char *keys[] = { "device-uri", NULL }; 143 char *result = NULL; 144 145 status = papiPrinterQuery(svc, name, keys, NULL, &p); 146 if ((status == PAPI_OK) && (p != NULL)) { 147 papi_attribute_t **attrs = papiPrinterGetAttributeList(p); 148 149 (void) papiAttributeListGetString(attrs, NULL, 150 "device-uri", &result); 151 if (result != NULL) 152 result = strdup(result); 153 154 papiPrinterFree(p); 155 } 156 157 return (result); 158 } 159 160 static void 161 print_description(papi_attribute_t **list, char *printer_name) 162 { 163 char *str = ""; 164 165 (void) papiAttributeListGetString(list, NULL, 166 "printer-info", &str); 167 168 /* 169 * If no printer-info is read then 170 * by default the printer-info is <printer-name>@<server> 171 */ 172 if (str[0] == '\0') { 173 char *uri = NULL; 174 uri_t *u = NULL; 175 176 (void) papiAttributeListGetString(list, NULL, 177 "printer-uri-supported", &uri); 178 179 if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) { 180 char *nodename = localhostname(); 181 182 if ((u->host == NULL) || 183 (strcasecmp(u->host, "localhost") == 0) || 184 (strcasecmp(u->host, nodename) == 0)) 185 printf(gettext("\tDescription:\n")); 186 else 187 printf(gettext("\tDescription: %s@%s\n"), 188 printer_name, u->host); 189 190 uri_free(u); 191 } else 192 printf(gettext("\tDescription:\n")); 193 } else 194 printf(gettext("\tDescription: %s\n"), str); 195 } 196 197 static char *report_device_keys[] = { "printer-name", "printer-uri-supported", 198 NULL }; 199 /* ARGSUSED2 */ 200 static int 201 report_device(papi_service_t svc, char *name, papi_printer_t printer, 202 int verbose, int description) 203 { 204 papi_status_t status; 205 papi_attribute_t **attrs = papiPrinterGetAttributeList(printer); 206 char *uri = NULL; 207 char *device = NULL; 208 uri_t *u = NULL; 209 210 if (name == NULL) { 211 status = papiAttributeListGetString(attrs, NULL, 212 "printer-name", &name); 213 if (status != PAPI_OK) 214 status = papiAttributeListGetString(attrs, NULL, 215 "printer-uri-supported", &name); 216 } 217 218 if (name == NULL) 219 return (-1); 220 221 (void) papiAttributeListGetString(attrs, NULL, 222 "printer-uri-supported", &uri); 223 224 if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) { 225 char *nodename = localhostname(); 226 227 if ((u->host == NULL) || 228 (strcasecmp(u->host, "localhost") == 0) || 229 (strcasecmp(u->host, nodename) == 0)) 230 device = get_device_uri(svc, name); 231 232 if (device != NULL) { 233 printf(gettext("device for %s: %s\n"), name, device); 234 return (0); 235 } else if (uri != NULL) { 236 printf(gettext("system for %s: %s (as %s)\n"), name, 237 u->host, uri); 238 return (0); 239 } 240 241 uri_free(u); 242 } 243 244 return (0); 245 } 246 247 static char *report_accepting_keys[] = { "printer-name", 248 "printer-uri-supported", "printer-is-accepting-jobs", 249 "printer-up-time", "printer-state-time", 250 "lpsched-reject-date", "lpsched-reject-reason", NULL }; 251 /* ARGSUSED2 */ 252 static int 253 report_accepting(papi_service_t svc, char *name, papi_printer_t printer, 254 int verbose, int description) 255 { 256 papi_status_t status; 257 papi_attribute_t **attrs = papiPrinterGetAttributeList(printer); 258 time_t curr; 259 char boolean = PAPI_FALSE; 260 261 if (name == NULL) { 262 status = papiAttributeListGetString(attrs, NULL, 263 "printer-name", &name); 264 if (status != PAPI_OK) 265 status = papiAttributeListGetString(attrs, NULL, 266 "printer-uri-supported", &name); 267 } 268 if (name == NULL) 269 return (-1); 270 271 (void) papiAttributeListGetBoolean(attrs, NULL, 272 "printer-is-accepting-jobs", &boolean); 273 (void) time(&curr); 274 (void) papiAttributeListGetDatetime(attrs, NULL, 275 "printer-up-time", &curr); 276 (void) papiAttributeListGetDatetime(attrs, NULL, 277 "printer-state-time", &curr); 278 (void) papiAttributeListGetDatetime(attrs, NULL, 279 "lpsched-reject-date", &curr); 280 281 if (boolean == PAPI_TRUE) { 282 printf(gettext("%s accepting requests since %s\n"), 283 name, nctime(&curr)); 284 } else { 285 char *reason = "unknown reason"; 286 287 (void) papiAttributeListGetString(attrs, NULL, 288 "lpsched-reject-reason", &reason); 289 290 printf(gettext("%s not accepting requests since %s\n\t%s\n"), 291 name, nctime(&curr), reason); 292 } 293 294 return (0); 295 } 296 297 static char *report_class_keys[] = { "printer-name", "printer-uri-supported", 298 "member-names", NULL }; 299 /* ARGSUSED2 */ 300 static int 301 report_class(papi_service_t svc, char *name, papi_printer_t printer, 302 int verbose, int description) 303 { 304 papi_status_t status; 305 papi_attribute_t **attrs = papiPrinterGetAttributeList(printer); 306 char *member = NULL; 307 void *iter = NULL; 308 309 status = papiAttributeListGetString(attrs, &iter, 310 "member-names", &member); 311 if (status == PAPI_NOT_FOUND) /* it's not a class */ 312 return (0); 313 314 if (name == NULL) { 315 status = papiAttributeListGetString(attrs, NULL, 316 "printer-name", &name); 317 if (status != PAPI_OK) 318 status = papiAttributeListGetString(attrs, NULL, 319 "printer-uri-supported", &name); 320 } 321 if (name == NULL) 322 return (-1); 323 324 printf(gettext("members of class %s:\n\t%s\n"), name, member); 325 while (papiAttributeListGetString(attrs, &iter, NULL, &member) 326 == PAPI_OK) 327 printf("\t%s\n", member); 328 329 return (0); 330 } 331 332 static char *report_printer_keys[] = { "printer-name", 333 "printer-uri-supported", "printer-state", 334 "printer-up-time", "printer-state-time", 335 "lpsched-disable-date", "printer-state-reasons", 336 "lpsched-disable-reason", NULL }; 337 /* ARGSUSED2 */ 338 static int 339 report_printer(papi_service_t svc, char *name, papi_printer_t printer, 340 int verbose, int description) 341 { 342 papi_status_t status; 343 papi_attribute_t **attrs = papiPrinterGetAttributeList(printer); 344 time_t curr; 345 int32_t pstat = 0; 346 char *member = NULL; 347 papi_job_t *j = NULL; 348 349 status = papiAttributeListGetString(attrs, NULL, 350 "member-names", &member); 351 if (status == PAPI_OK) /* it's a class */ 352 return (0); 353 354 if (name == NULL) { 355 status = papiAttributeListGetString(attrs, NULL, 356 "printer-name", &name); 357 if (status != PAPI_OK) 358 status = papiAttributeListGetString(attrs, NULL, 359 "printer-uri-supported", &name); 360 } 361 if (name == NULL) 362 return (-1); 363 364 printf(gettext("printer %s "), name); 365 366 status = papiAttributeListGetInteger(attrs, NULL, 367 "printer-state", &pstat); 368 369 switch (pstat) { 370 case 0x03: /* idle */ 371 printf(gettext("idle. enabled")); 372 break; 373 case 0x04: /* processing */ 374 status = papiPrinterListJobs(svc, name, NULL, 375 0, 0, &j); 376 377 if (status == PAPI_OK) { 378 if (j != NULL) { 379 int i = 0; 380 int32_t jobid = 0; 381 int32_t jstate = 0; 382 int flag = 0; 383 384 for (i = 0; j[i] != NULL; ++i) { 385 papi_attribute_t **attr = 386 papiJobGetAttributeList(j[i]); 387 388 papiAttributeListGetInteger(attr, 389 NULL, "job-state", &jstate); 390 papiAttributeListGetInteger(attr, 391 NULL, "job-id", &jobid); 392 393 /* 394 * If the job-state is not 395 * "held", "cancelled" or 396 * "completed" then only print. 397 */ 398 if ((jstate != 0x04) && 399 (jstate != 0x07) && 400 (jstate != 0x09)) { 401 if (flag == 0) 402 printf(gettext 403 ("now printing"\ 404 " %s-%d. enabled"), 405 name, jobid); 406 flag = 1; 407 } 408 } 409 papiJobListFree(j); 410 } 411 } 412 break; 413 case 0x05: /* stopped */ 414 printf(gettext("disabled")); 415 break; 416 default: 417 printf(gettext("unknown state(0x%x)."), pstat); 418 break; 419 } 420 421 (void) time(&curr); 422 (void) papiAttributeListGetDatetime(attrs, NULL, 423 "printer-up-time", &curr); 424 (void) papiAttributeListGetDatetime(attrs, NULL, 425 "printer-state-time", &curr); 426 (void) papiAttributeListGetDatetime(attrs, NULL, 427 "lpsched-disable-date", &curr); 428 printf(gettext(" since %s. available.\n"), nctime(&curr)); 429 430 if (pstat == 0x05) { 431 char *reason = "unknown reason"; 432 433 (void) papiAttributeListGetString(attrs, NULL, 434 "printer-state-reasons", &reason); 435 (void) papiAttributeListGetString(attrs, NULL, 436 "lpsched-disable-reason", &reason); 437 printf(gettext("\t%s\n"), reason); 438 } 439 440 if (verbose == 1) { 441 void *iter; 442 char *str; 443 444 str = ""; 445 (void) papiAttributeListGetString(attrs, NULL, 446 "form-ready", &str); 447 printf(gettext("\tForm mounted: %s\n"), str); 448 449 str = ""; 450 iter = NULL; 451 (void) papiAttributeListGetString(attrs, &iter, 452 "document-format-supported", &str); 453 printf(gettext("\tContent types: %s"), str); 454 while (papiAttributeListGetString(attrs, &iter, NULL, &str) 455 == PAPI_OK) 456 printf(", %s", str); 457 printf("\n"); 458 459 /* Display the printer description */ 460 print_description(attrs, name); 461 462 str = ""; 463 iter = NULL; 464 (void) papiAttributeListGetString(attrs, &iter, 465 "lpsched-printer-type", &str); 466 printf(gettext("\tPrinter types: %s"), str); 467 while (papiAttributeListGetString(attrs, &iter, NULL, &str) 468 == PAPI_OK) 469 printf(", %s", str); 470 printf("\n"); 471 472 str = ""; 473 (void) papiAttributeListGetString(attrs, NULL, 474 "lpsched-dial-info", &str); 475 printf(gettext("\tConnection: %s\n"), 476 ((str[0] == '\0') ? gettext("direct") : str)); 477 478 str = ""; 479 (void) papiAttributeListGetString(attrs, NULL, 480 "lpsched-interface-script", &str); 481 printf(gettext("\tInterface: %s\n"), str); 482 483 str = NULL; 484 (void) papiAttributeListGetString(attrs, NULL, 485 "ppd-file-uri", &str); 486 (void) papiAttributeListGetString(attrs, NULL, 487 "lpsched-ppd-source-path", &str); 488 if (str != NULL) 489 printf(gettext("\tPPD: %s\n"), str); 490 491 str = NULL; 492 (void) papiAttributeListGetString(attrs, NULL, 493 "lpsched-fault-alert-command", &str); 494 if (str != NULL) 495 printf(gettext("\tOn fault: %s\n"), str); 496 497 str = ""; 498 (void) papiAttributeListGetString(attrs, NULL, 499 "lpsched-fault-recovery", &str); 500 printf(gettext("\tAfter fault: %s\n"), 501 ((str[0] == '\0') ? gettext("continue") : str)); 502 503 str = "(all)"; 504 iter = NULL; 505 (void) papiAttributeListGetString(attrs, &iter, 506 "requesting-user-name-allowed", &str); 507 printf(gettext("\tUsers allowed:\n\t\t%s\n"), 508 ((str[0] == '\0') ? gettext("(none)") : str)); 509 if ((str != NULL) && (str[0] != '\0')) 510 while (papiAttributeListGetString(attrs, &iter, NULL, 511 &str) == PAPI_OK) 512 printf("\t\t%s\n", str); 513 514 str = NULL; 515 iter = NULL; 516 (void) papiAttributeListGetString(attrs, &iter, 517 "requesting-user-name-denied", &str); 518 if (str != NULL) { 519 printf(gettext("\tUsers denied:\n\t\t%s\n"), 520 ((str[0] == '\0') ? gettext("(none)") : str)); 521 if ((str != NULL) && (str[0] != '\0')) 522 while (papiAttributeListGetString(attrs, &iter, 523 NULL, &str) == PAPI_OK) 524 printf("\t\t%s\n", str); 525 } 526 527 str = "none"; 528 iter = NULL; 529 (void) papiAttributeListGetString(attrs, &iter, 530 "form-supported", &str); 531 printf(gettext("\tForms allowed:\n\t\t(%s)\n"), 532 ((str[0] == '\0') ? gettext("none") : str)); 533 if ((str != NULL) && (str[0] != '\0')) 534 while (papiAttributeListGetString(attrs, &iter, NULL, 535 &str) == PAPI_OK) 536 printf("\t\t(%s)\n", str); 537 538 str = ""; 539 iter = NULL; 540 (void) papiAttributeListGetString(attrs, &iter, 541 "media-supported", &str); 542 printf(gettext("\tMedia supported:\n\t\t%s\n"), 543 ((str[0] == '\0') ? gettext("(none)") : str)); 544 if ((str != NULL) && (str[0] != '\0')) 545 while (papiAttributeListGetString(attrs, &iter, NULL, 546 &str) == PAPI_OK) 547 printf("\t\t%s\n", str); 548 549 str = ""; 550 (void) papiAttributeListGetString(attrs, NULL, 551 "job-sheets-supported", &str); 552 if ((strcasecmp(str, "none")) == 0) 553 str = gettext("page never printed"); 554 else if (strcasecmp(str, "optional") == 0) 555 str = gettext("not required"); 556 else 557 str = gettext("required"); 558 559 printf(gettext("\tBanner %s\n"), str); 560 561 562 str = ""; 563 iter = NULL; 564 (void) papiAttributeListGetString(attrs, &iter, 565 "lpsched-print-wheels", &str); 566 printf(gettext("\tCharacter sets:\n\t\t%s\n"), 567 ((str[0] == '\0') ? gettext("(none)") : str)); 568 if ((str != NULL) && (str[0] != '\0')) 569 while (papiAttributeListGetString(attrs, &iter, NULL, 570 &str) == PAPI_OK) 571 printf("\t\t%s\n", str); 572 573 printf(gettext("\tDefault pitch:\n")); 574 printf(gettext("\tDefault page size:\n")); 575 printf(gettext("\tDefault port setting:\n")); 576 577 str = ""; 578 iter = NULL; 579 (void) papiAttributeListGetString(attrs, &iter, 580 "lpsched-options", &str); 581 if (str != NULL) { 582 printf(gettext("\tOptions: %s"), str); 583 while (papiAttributeListGetString(attrs, &iter, NULL, 584 &str) == PAPI_OK) 585 printf(", %s", str); 586 printf("\n"); 587 } 588 589 } else if (description == 1) 590 /* Display printer description */ 591 print_description(attrs, name); 592 else if (verbose > 1) 593 papiAttributeListPrint(stdout, attrs, "\t"); 594 595 if (verbose > 0) 596 printf("\n"); 597 598 return (0); 599 } 600 601 static int 602 printer_query(char *name, int (*report)(papi_service_t, char *, papi_printer_t, 603 int, int), papi_encryption_t encryption, 604 int verbose, int description) 605 { 606 int result = 0; 607 papi_status_t status; 608 papi_service_t svc = NULL; 609 610 status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback, 611 encryption, NULL); 612 if (status != PAPI_OK) { 613 if (status == PAPI_NOT_FOUND) 614 fprintf(stderr, gettext("%s: unknown printer\n"), 615 name ? name : "(NULL)"); 616 else 617 fprintf(stderr, gettext( 618 "Failed to contact service for %s: %s\n"), 619 name ? name : "(NULL)", 620 verbose_papi_message(svc, status)); 621 papiServiceDestroy(svc); 622 return (-1); 623 } 624 625 if (name == NULL) { /* all */ 626 char **interest = interest_list(svc); 627 628 if (interest != NULL) { 629 int i; 630 631 for (i = 0; interest[i] != NULL; i++) 632 result += printer_query(interest[i], report, 633 encryption, verbose, 634 description); 635 } 636 } else { 637 papi_printer_t printer = NULL; 638 char **keys = NULL; 639 640 /* 641 * Limit the query to only required data to reduce the need 642 * to go remote for information. 643 */ 644 if (report == report_device) 645 keys = report_device_keys; 646 else if (report == report_class) 647 keys = report_class_keys; 648 else if (report == report_accepting) 649 keys = report_accepting_keys; 650 else if ((report == report_printer) && (verbose == 0)) 651 keys = report_printer_keys; 652 653 status = papiPrinterQuery(svc, name, keys, NULL, &printer); 654 if (status != PAPI_OK) { 655 fprintf(stderr, gettext( 656 "Failed to get printer info for %s: %s\n"), 657 name, verbose_papi_message(svc, status)); 658 papiServiceDestroy(svc); 659 return (-1); 660 } 661 662 if (printer != NULL) 663 result = report(svc, name, printer, verbose, 664 description); 665 666 papiPrinterFree(printer); 667 } 668 669 papiServiceDestroy(svc); 670 671 return (result); 672 } 673 674 static int 675 match_user(char *user, char **list) 676 { 677 int i; 678 679 for (i = 0; list[i] != NULL; i++) { 680 if (strcmp(user, list[i]) == 0) 681 return (0); 682 } 683 684 return (-1); 685 } 686 687 static char **users = NULL; 688 689 static int 690 report_job(char *printer, papi_job_t job, int show_rank, int verbose) 691 { 692 papi_attribute_t **attrs = papiJobGetAttributeList(job); 693 time_t clock = 0; 694 char date[24]; 695 char request[26]; 696 char *user = "unknown"; 697 char *host = NULL; 698 int32_t size = 0; 699 int32_t jstate = 0; 700 char User[50]; 701 702 char *destination = "unknown"; 703 int32_t id = -1; 704 static int check = 0; 705 static char *uri = NULL; 706 707 (void) papiAttributeListGetString(attrs, NULL, 708 "job-originating-user-name", &user); 709 710 if ((users != NULL) && (match_user(user, users) < 0)) 711 return (0); 712 713 (void) papiAttributeListGetString(attrs, NULL, 714 "job-originating-host-name", &host); 715 716 if (check == 0) { 717 /* 718 * Read the attribute "job-printer-uri" 719 * just once 720 */ 721 (void) papiAttributeListGetString(attrs, NULL, 722 "job-printer-uri", &uri); 723 check = 1; 724 } 725 726 if (host) { 727 /* Check if it is local printer or remote printer */ 728 uri_t *u = NULL; 729 730 if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) { 731 char *nodename = localhostname(); 732 733 if ((u->host == NULL) || 734 (strcasecmp(u->host, "localhost") == 0) || 735 (strcasecmp(u->host, nodename) == 0)) { 736 737 if (strcasecmp(host, nodename) == 0) { 738 /* 739 * Request submitted locally 740 * for the local queue. 741 * Hostname will not be displayed 742 */ 743 snprintf(User, sizeof (User), "%s", 744 user); 745 } 746 else 747 snprintf(User, sizeof (User), "%s@%s", 748 user, host); 749 } else if (uri != NULL) { 750 /* 751 * It's a remote printer. 752 * In case of remote printers hostname is 753 * always displayed. 754 */ 755 snprintf(User, sizeof (User), "%s@%s", 756 user, host); 757 } 758 uri_free(u); 759 } else { 760 /* 761 * If attribute "job-printer-uri" 762 * cannot be read 763 * by default append the hostname 764 */ 765 snprintf(User, sizeof (User), "%s@%s", user, host); 766 } 767 } else { 768 /* 769 * When print server is s10u4 and ipp service is used 770 * "job-originating-hostname" attribute is not set 771 * So get the host information from the uri 772 */ 773 uri_t *u = NULL; 774 if ((uri != NULL) && (uri_from_string(uri, &u) == 0)) { 775 if ((u != NULL) && (u->host != NULL)) 776 snprintf(User, sizeof (User), "%s@%s", 777 user, u->host); 778 else 779 snprintf(User, sizeof (User), "%s", user); 780 781 uri_free(u); 782 } else 783 snprintf(User, sizeof (User), "%s", user); 784 } 785 (void) papiAttributeListGetInteger(attrs, NULL, "job-k-octets", &size); 786 size *= 1024; /* for the approximate byte size */ 787 (void) papiAttributeListGetInteger(attrs, NULL, "job-octets", &size); 788 789 (void) time(&clock); 790 (void) papiAttributeListGetInteger(attrs, NULL, 791 "time-at-creation", (int32_t *)&clock); 792 (void) strftime(date, sizeof (date), "%b %d %R", localtime(&clock)); 793 794 (void) papiAttributeListGetString(attrs, NULL, 795 "job-printer-uri", &destination); 796 (void) papiAttributeListGetString(attrs, NULL, 797 "printer-name", &destination); 798 (void) papiAttributeListGetInteger(attrs, NULL, 799 "job-id", &id); 800 801 snprintf(request, sizeof (request), "%s-%d", printer, id); 802 803 if (show_rank != 0) { 804 int32_t rank = -1; 805 806 (void) papiAttributeListGetInteger(attrs, NULL, 807 "number-of-intervening-jobs", &rank); 808 rank++; 809 810 printf("%3d %-21s %-14s %7ld %s", 811 rank, request, User, size, date); 812 } else 813 printf("%-23s %-14s %7ld %s", request, User, size, date); 814 815 (void) papiAttributeListGetInteger(attrs, NULL, 816 "job-state", &jstate); 817 if (jstate == 0x04) 818 printf(gettext(", being held")); 819 else if (jstate == 0x07) 820 printf(gettext(", cancelled")); 821 else if (jstate == 0x09) 822 printf(gettext(", complete")); 823 824 if (verbose == 1) { 825 char *form = NULL; 826 827 (void) papiAttributeListGetString(attrs, NULL, 828 "output-device-assigned", &destination); 829 printf("\n\t assigned %s", destination); 830 831 (void) papiAttributeListGetString(attrs, NULL, "form", &form); 832 if (form != NULL) 833 printf(", form %s", form); 834 } else if (verbose > 1) { 835 printf("\n"); 836 papiAttributeListPrint(stdout, attrs, "\t"); 837 } 838 839 printf("\n"); 840 841 return (0); 842 } 843 844 static int 845 job_query(char *request, int (*report)(char *, papi_job_t, int, int), 846 papi_encryption_t encryption, int show_rank, int verbose) 847 { 848 int result = 0; 849 papi_status_t status; 850 papi_service_t svc = NULL; 851 char *printer = request; 852 int32_t id = -1; 853 int flag1 = 0; 854 int flag = 1; 855 int print_flag = 0; 856 857 do { 858 status = papiServiceCreate(&svc, printer, NULL, NULL, 859 cli_auth_callback, encryption, NULL); 860 861 if ((status == PAPI_OK) && (printer != NULL)) 862 print_flag = 1; 863 864 /* <name>-# printer name does not exist */ 865 if (status != PAPI_OK) { 866 /* 867 * Check if <name>-# is a request-id 868 * Once this check is done flag1 is set 869 */ 870 if (flag1 == 1) 871 break; 872 873 get_printer_id(printer, &printer, &id); 874 875 status = papiServiceCreate(&svc, printer, NULL, NULL, 876 cli_auth_callback, encryption, NULL); 877 878 if (status != PAPI_OK) { 879 fprintf(stderr, gettext( 880 "Failed to contact service for %s: %s\n"), 881 (printer ? printer : "all"), 882 verbose_papi_message(svc, status)); 883 return (-1); 884 } 885 } 886 887 if (printer == NULL) { /* all */ 888 char **interest = interest_list(svc); 889 890 if (interest != NULL) { 891 int i; 892 893 for (i = 0; interest[i] != NULL; i++) 894 result += job_query(interest[i], report, 895 encryption, show_rank, verbose); 896 } 897 } else if (id == -1) { /* a printer */ 898 papi_job_t *jobs = NULL; 899 900 status = papiPrinterListJobs(svc, printer, NULL, 901 0, 0, &jobs); 902 if (status != PAPI_OK) { 903 fprintf(stderr, gettext( 904 "Failed to get job list: %s\n"), 905 verbose_papi_message(svc, status)); 906 papiServiceDestroy(svc); 907 return (-1); 908 } 909 910 if (jobs != NULL) { 911 int i; 912 913 for (i = 0; jobs[i] != NULL; i++) 914 result += report(printer, 915 jobs[i], show_rank, 916 verbose); 917 } 918 919 papiJobListFree(jobs); 920 } else { /* a job */ 921 papi_job_t job = NULL; 922 923 /* Once a job has been found stop processing */ 924 flag = 0; 925 926 status = papiJobQuery(svc, printer, id, NULL, &job); 927 if (status != PAPI_OK) { 928 if (!print_flag) 929 fprintf(stderr, gettext( 930 "Failed to get job"\ 931 " info for %s: %s\n"), 932 request, 933 verbose_papi_message(svc, status)); 934 papiServiceDestroy(svc); 935 return (-1); 936 } 937 938 if (job != NULL) 939 result = report(printer, job, 940 show_rank, verbose); 941 942 papiJobFree(job); 943 } 944 945 if (flag) { 946 id = -1; 947 get_printer_id(printer, &printer, &id); 948 if (id == -1) 949 flag = 0; 950 else 951 flag1 = 1; 952 } 953 } while (flag); 954 955 papiServiceDestroy(svc); 956 957 return (result); 958 } 959 960 static int 961 report_form(char *name, papi_attribute_t **attrs, int verbose) 962 { 963 papi_status_t status; 964 char *form = NULL; 965 void *iter = NULL; 966 967 for (status = papiAttributeListGetString(attrs, &iter, 968 "form-supported", &form); 969 status == PAPI_OK; 970 status = papiAttributeListGetString(attrs, &iter, 971 NULL, &form)) { 972 if ((name == NULL) || (strcmp(name, form) == 0)) { 973 printf(gettext("form %s is available to you\n"), form); 974 if (verbose != 0) { 975 char *detail = NULL; 976 status = papiAttributeListGetString(attrs, NULL, 977 "form-supported-detail", &detail); 978 if (status == PAPI_OK) 979 printf("%s\n", detail); 980 } 981 } 982 } 983 984 return (0); 985 } 986 987 static int 988 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose) 989 { 990 papi_status_t status; 991 char *pw = NULL; 992 void *iter = NULL; 993 994 for (status = papiAttributeListGetString(attrs, &iter, 995 "pw-supported", &pw); 996 status == PAPI_OK; 997 status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) { 998 if ((name == NULL) || (strcmp(name, pw) == 0)) { 999 printf(gettext("charset %s is available\n"), pw); 1000 if (verbose != 0) { 1001 char *info = NULL; 1002 status = papiAttributeListGetString(attrs, NULL, 1003 "pw-supported-extra", &info); 1004 if (status == PAPI_OK) 1005 printf("%s\n", info); 1006 } 1007 } 1008 } 1009 1010 return (0); 1011 } 1012 1013 static int 1014 service_query(char *name, int (*report)(char *, papi_attribute_t **, int), 1015 papi_encryption_t encryption, int verbose) 1016 { 1017 int result = 0; 1018 papi_status_t status; 1019 papi_service_t svc = NULL; 1020 papi_attribute_t **attrs = NULL; 1021 1022 status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback, 1023 encryption, NULL); 1024 if (status != PAPI_OK) { 1025 papiServiceDestroy(svc); 1026 return (-1); 1027 } 1028 1029 attrs = papiServiceGetAttributeList(svc); 1030 if (attrs != NULL) { 1031 result = report(name, attrs, verbose); 1032 1033 if (verbose > 1) { 1034 printf("\n"); 1035 papiAttributeListPrint(stdout, attrs, "\t"); 1036 printf("\n"); 1037 } 1038 } 1039 1040 papiServiceDestroy(svc); 1041 1042 return (result); 1043 } 1044 1045 int 1046 main(int ac, char *av[]) 1047 { 1048 int exit_code = 0; 1049 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1050 int rank = 0; 1051 int verbose = 0; 1052 int description = 0; 1053 int c; 1054 char **argv; 1055 1056 (void) setlocale(LC_ALL, ""); 1057 (void) textdomain("SUNW_OST_OSCMD"); 1058 1059 argv = (char **)calloc((ac + 1), sizeof (char *)); 1060 for (c = 0; c < ac; c++) { 1061 if ((av[c][0] == '-') && (av[c][1] == 'l') && 1062 (isalpha(av[c][2]) != 0)) { 1063 /* preserve old "-l[po...]" behavior */ 1064 argv[c] = &av[c][1]; 1065 argv[c][0] = '-'; 1066 verbose = 1; 1067 1068 } else 1069 argv[c] = av[c]; 1070 } 1071 1072 argv[c++] = "--"; 1073 ac = c; 1074 1075 /* preprocess argument list looking for '-l' or '-R' so it can trail */ 1076 while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) 1077 switch (c) { 1078 case 'l': 1079 if ((optarg == NULL) || (optarg[0] == '-')) 1080 optarg = "1"; 1081 verbose = atoi(optarg); 1082 break; 1083 case 'D': 1084 description = 1; 1085 break; 1086 case 'R': 1087 rank = 1; 1088 break; 1089 case 'E': 1090 encryption = PAPI_ENCRYPT_REQUIRED; 1091 break; 1092 default: 1093 break; 1094 } 1095 optind = 1; 1096 1097 /* process command line arguments */ 1098 while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) { 1099 switch (c) { /* these may or may not have an option */ 1100 case 'a': 1101 case 'c': 1102 case 'p': 1103 case 'o': 1104 case 'R': 1105 case 'u': 1106 case 'v': 1107 case 'l': 1108 case 'f': 1109 case 'S': 1110 if (optarg[0] == '-') { 1111 /* this check stop a possible infinite loop */ 1112 if ((optind > 1) && (argv[optind-1][1] != c)) 1113 optind--; 1114 optarg = NULL; 1115 } else if (strcmp(optarg, "all") == 0) 1116 optarg = NULL; 1117 } 1118 1119 switch (c) { 1120 case 'a': 1121 exit_code += printer_query(optarg, report_accepting, 1122 encryption, verbose, 0); 1123 break; 1124 case 'c': 1125 exit_code += printer_query(optarg, report_class, 1126 encryption, verbose, 0); 1127 break; 1128 case 'p': 1129 exit_code += printer_query(optarg, report_printer, 1130 encryption, verbose, description); 1131 break; 1132 case 'd': 1133 exit_code += lpstat_default_printer(encryption); 1134 break; 1135 case 'r': 1136 exit_code += lpstat_service_status(encryption); 1137 break; 1138 case 'u': 1139 if (optarg != NULL) 1140 users = strsplit(optarg, ", \n"); 1141 exit_code += job_query(NULL, report_job, 1142 encryption, rank, verbose); 1143 if (users != NULL) { 1144 free(users); 1145 users = NULL; 1146 } 1147 break; 1148 case 'v': 1149 exit_code += printer_query(optarg, report_device, 1150 encryption, verbose, 0); 1151 break; 1152 case 'R': /* set "rank" flag in first pass */ 1153 case 'o': 1154 exit_code += job_query(optarg, report_job, 1155 encryption, rank, verbose); 1156 break; 1157 case 'f': 1158 exit_code += service_query(optarg, report_form, 1159 encryption, verbose); 1160 break; 1161 case 'S': 1162 exit_code += service_query(optarg, report_print_wheels, 1163 encryption, verbose); 1164 break; 1165 case 's': 1166 exit_code += lpstat_service_status(encryption); 1167 exit_code += lpstat_default_printer(encryption); 1168 exit_code += printer_query(NULL, report_class, 1169 encryption, verbose, 0); 1170 exit_code += printer_query(NULL, report_device, 1171 encryption, verbose, 0); 1172 exit_code += service_query(optarg, report_form, 1173 encryption, verbose); 1174 exit_code += service_query(optarg, report_print_wheels, 1175 encryption, verbose); 1176 break; 1177 case 't': 1178 exit_code += lpstat_service_status(encryption); 1179 exit_code += lpstat_default_printer(encryption); 1180 exit_code += printer_query(NULL, report_class, 1181 encryption, verbose, 0); 1182 exit_code += printer_query(NULL, report_device, 1183 encryption, verbose, 0); 1184 exit_code += printer_query(NULL, report_accepting, 1185 encryption, verbose, 0); 1186 exit_code += printer_query(NULL, report_printer, 1187 encryption, verbose, 0); 1188 exit_code += service_query(optarg, report_form, 1189 encryption, verbose); 1190 exit_code += service_query(optarg, report_print_wheels, 1191 encryption, verbose); 1192 exit_code += job_query(NULL, report_job, 1193 encryption, rank, verbose); 1194 break; 1195 case 'L': /* local-only, ignored */ 1196 case 'l': /* increased verbose level in first pass */ 1197 case 'D': /* set "description" flag in first pass */ 1198 case 'E': /* set encryption in the first pass */ 1199 break; 1200 default: 1201 usage(av[0]); 1202 } 1203 } 1204 ac--; 1205 1206 if (ac == 1) { /* report on my jobs */ 1207 struct passwd *pw = getpwuid(getuid()); 1208 1209 if (pw != NULL) 1210 users = strsplit(pw->pw_name, ""); 1211 exit_code += job_query(NULL, report_job, encryption, 1212 rank, verbose); 1213 if (users != NULL) { 1214 free(users); 1215 users = NULL; 1216 } 1217 } else { 1218 for (c = optind; c < ac; c++) 1219 exit_code += job_query(argv[c], report_job, encryption, 1220 rank, verbose); 1221 } 1222 1223 1224 if (exit_code != 0) 1225 exit_code = 1; 1226 1227 return (exit_code); 1228 } 1229