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(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 snprintf(request, sizeof (request), "%s-%d", destination, id); 801 802 if (show_rank != 0) { 803 int32_t rank = -1; 804 805 (void) papiAttributeListGetInteger(attrs, NULL, 806 "number-of-intervening-jobs", &rank); 807 rank++; 808 809 printf("%3d %-21s %-14s %7ld %s", 810 rank, request, User, size, date); 811 } else 812 printf("%-23s %-14s %7ld %s", request, User, size, date); 813 814 (void) papiAttributeListGetInteger(attrs, NULL, 815 "job-state", &jstate); 816 if (jstate == 0x04) 817 printf(gettext(", being held")); 818 else if (jstate == 0x07) 819 printf(gettext(", cancelled")); 820 else if (jstate == 0x09) 821 printf(gettext(", complete")); 822 823 if (verbose == 1) { 824 char *form = NULL; 825 826 (void) papiAttributeListGetString(attrs, NULL, 827 "output-device-assigned", &destination); 828 printf("\n\t assigned %s", destination); 829 830 (void) papiAttributeListGetString(attrs, NULL, "form", &form); 831 if (form != NULL) 832 printf(", form %s", form); 833 } else if (verbose > 1) { 834 printf("\n"); 835 papiAttributeListPrint(stdout, attrs, "\t"); 836 } 837 838 printf("\n"); 839 840 return (0); 841 } 842 843 static int 844 job_query(char *request, int (*report)(papi_job_t, int, int), 845 papi_encryption_t encryption, int show_rank, int verbose) 846 { 847 int result = 0; 848 papi_status_t status; 849 papi_service_t svc = NULL; 850 char *printer = request; 851 int32_t id = -1; 852 int flag1 = 0; 853 int flag = 1; 854 int print_flag = 0; 855 856 do { 857 status = papiServiceCreate(&svc, printer, NULL, NULL, 858 cli_auth_callback, encryption, NULL); 859 860 if ((status == PAPI_OK) && (printer != NULL)) 861 print_flag = 1; 862 863 /* <name>-# printer name does not exist */ 864 if (status != PAPI_OK) { 865 /* 866 * Check if <name>-# is a request-id 867 * Once this check is done flag1 is set 868 */ 869 if (flag1 == 1) 870 break; 871 872 get_printer_id(printer, &printer, &id); 873 874 status = papiServiceCreate(&svc, printer, NULL, NULL, 875 cli_auth_callback, encryption, NULL); 876 877 if (status != PAPI_OK) { 878 fprintf(stderr, gettext( 879 "Failed to contact service for %s: %s\n"), 880 (printer ? printer : "all"), 881 verbose_papi_message(svc, status)); 882 return (-1); 883 } 884 } 885 886 if (printer == NULL) { /* all */ 887 char **interest = interest_list(svc); 888 889 if (interest != NULL) { 890 int i; 891 892 for (i = 0; interest[i] != NULL; i++) 893 result += job_query(interest[i], report, 894 encryption, show_rank, verbose); 895 } 896 } else if (id == -1) { /* a printer */ 897 papi_job_t *jobs = NULL; 898 899 status = papiPrinterListJobs(svc, printer, NULL, 900 0, 0, &jobs); 901 if (status != PAPI_OK) { 902 fprintf(stderr, gettext( 903 "Failed to get job list: %s\n"), 904 verbose_papi_message(svc, status)); 905 papiServiceDestroy(svc); 906 return (-1); 907 } 908 909 if (jobs != NULL) { 910 int i; 911 912 for (i = 0; jobs[i] != NULL; i++) 913 result += report(jobs[i], 914 show_rank, verbose); 915 } 916 917 papiJobListFree(jobs); 918 } else { /* a job */ 919 papi_job_t job = NULL; 920 921 /* Once a job has been found stop processing */ 922 flag = 0; 923 924 status = papiJobQuery(svc, printer, id, NULL, &job); 925 if (status != PAPI_OK) { 926 if (!print_flag) 927 fprintf(stderr, gettext( 928 "Failed to get job"\ 929 " info for %s: %s\n"), 930 request, 931 verbose_papi_message(svc, status)); 932 papiServiceDestroy(svc); 933 return (-1); 934 } 935 936 if (job != NULL) 937 result = report(job, show_rank, verbose); 938 939 papiJobFree(job); 940 } 941 942 if (flag) { 943 id = -1; 944 get_printer_id(printer, &printer, &id); 945 if (id == -1) 946 flag = 0; 947 else 948 flag1 = 1; 949 } 950 } while (flag); 951 952 papiServiceDestroy(svc); 953 954 return (result); 955 } 956 957 static int 958 report_form(char *name, papi_attribute_t **attrs, int verbose) 959 { 960 papi_status_t status; 961 char *form = NULL; 962 void *iter = NULL; 963 964 for (status = papiAttributeListGetString(attrs, &iter, 965 "form-supported", &form); 966 status == PAPI_OK; 967 status = papiAttributeListGetString(attrs, &iter, 968 NULL, &form)) { 969 if ((name == NULL) || (strcmp(name, form) == 0)) { 970 printf(gettext("form %s is available to you\n"), form); 971 if (verbose != 0) { 972 char *detail = NULL; 973 status = papiAttributeListGetString(attrs, NULL, 974 "form-supported-detail", &detail); 975 if (status == PAPI_OK) 976 printf("%s\n", detail); 977 } 978 } 979 } 980 981 return (0); 982 } 983 984 static int 985 report_print_wheels(char *name, papi_attribute_t **attrs, int verbose) 986 { 987 papi_status_t status; 988 char *pw = NULL; 989 void *iter = NULL; 990 991 for (status = papiAttributeListGetString(attrs, &iter, 992 "pw-supported", &pw); 993 status == PAPI_OK; 994 status = papiAttributeListGetString(attrs, &iter, NULL, &pw)) { 995 if ((name == NULL) || (strcmp(name, pw) == 0)) { 996 printf(gettext("charset %s is available\n"), pw); 997 if (verbose != 0) { 998 char *info = NULL; 999 status = papiAttributeListGetString(attrs, NULL, 1000 "pw-supported-extra", &info); 1001 if (status == PAPI_OK) 1002 printf("%s\n", info); 1003 } 1004 } 1005 } 1006 1007 return (0); 1008 } 1009 1010 static int 1011 service_query(char *name, int (*report)(char *, papi_attribute_t **, int), 1012 papi_encryption_t encryption, int verbose) 1013 { 1014 int result = 0; 1015 papi_status_t status; 1016 papi_service_t svc = NULL; 1017 papi_attribute_t **attrs = NULL; 1018 1019 status = papiServiceCreate(&svc, name, NULL, NULL, cli_auth_callback, 1020 encryption, NULL); 1021 if (status != PAPI_OK) { 1022 papiServiceDestroy(svc); 1023 return (-1); 1024 } 1025 1026 attrs = papiServiceGetAttributeList(svc); 1027 if (attrs != NULL) { 1028 result = report(name, attrs, verbose); 1029 1030 if (verbose > 1) { 1031 printf("\n"); 1032 papiAttributeListPrint(stdout, attrs, "\t"); 1033 printf("\n"); 1034 } 1035 } 1036 1037 papiServiceDestroy(svc); 1038 1039 return (result); 1040 } 1041 1042 int 1043 main(int ac, char *av[]) 1044 { 1045 int exit_code = 0; 1046 papi_encryption_t encryption = PAPI_ENCRYPT_NEVER; 1047 int rank = 0; 1048 int verbose = 0; 1049 int description = 0; 1050 int c; 1051 char **argv; 1052 1053 (void) setlocale(LC_ALL, ""); 1054 (void) textdomain("SUNW_OST_OSCMD"); 1055 1056 argv = (char **)calloc((ac + 1), sizeof (char *)); 1057 for (c = 0; c < ac; c++) { 1058 if ((av[c][0] == '-') && (av[c][1] == 'l') && 1059 (isalpha(av[c][2]) != 0)) { 1060 /* preserve old "-l[po...]" behavior */ 1061 argv[c] = &av[c][1]; 1062 argv[c][0] = '-'; 1063 verbose = 1; 1064 1065 } else 1066 argv[c] = av[c]; 1067 } 1068 1069 argv[c++] = "--"; 1070 ac = c; 1071 1072 /* preprocess argument list looking for '-l' or '-R' so it can trail */ 1073 while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) 1074 switch (c) { 1075 case 'l': 1076 if ((optarg == NULL) || (optarg[0] == '-')) 1077 optarg = "1"; 1078 verbose = atoi(optarg); 1079 break; 1080 case 'D': 1081 description = 1; 1082 break; 1083 case 'R': 1084 rank = 1; 1085 break; 1086 case 'E': 1087 encryption = PAPI_ENCRYPT_REQUIRED; 1088 break; 1089 default: 1090 break; 1091 } 1092 optind = 1; 1093 1094 /* process command line arguments */ 1095 while ((c = getopt(ac, argv, "LEDf:S:stc:p:a:drs:v:l:o:R:u:")) != EOF) { 1096 switch (c) { /* these may or may not have an option */ 1097 case 'a': 1098 case 'c': 1099 case 'p': 1100 case 'o': 1101 case 'R': 1102 case 'u': 1103 case 'v': 1104 case 'l': 1105 case 'f': 1106 case 'S': 1107 if (optarg[0] == '-') { 1108 /* this check stop a possible infinite loop */ 1109 if ((optind > 1) && (argv[optind-1][1] != c)) 1110 optind--; 1111 optarg = NULL; 1112 } else if (strcmp(optarg, "all") == 0) 1113 optarg = NULL; 1114 } 1115 1116 switch (c) { 1117 case 'a': 1118 exit_code += printer_query(optarg, report_accepting, 1119 encryption, verbose, 0); 1120 break; 1121 case 'c': 1122 exit_code += printer_query(optarg, report_class, 1123 encryption, verbose, 0); 1124 break; 1125 case 'p': 1126 exit_code += printer_query(optarg, report_printer, 1127 encryption, verbose, description); 1128 break; 1129 case 'd': 1130 exit_code += lpstat_default_printer(encryption); 1131 break; 1132 case 'r': 1133 exit_code += lpstat_service_status(encryption); 1134 break; 1135 case 'u': 1136 if (optarg != NULL) 1137 users = strsplit(optarg, ", \n"); 1138 exit_code += job_query(NULL, report_job, 1139 encryption, rank, verbose); 1140 if (users != NULL) { 1141 free(users); 1142 users = NULL; 1143 } 1144 break; 1145 case 'v': 1146 exit_code += printer_query(optarg, report_device, 1147 encryption, verbose, 0); 1148 break; 1149 case 'R': /* set "rank" flag in first pass */ 1150 case 'o': 1151 exit_code += job_query(optarg, report_job, 1152 encryption, rank, verbose); 1153 break; 1154 case 'f': 1155 exit_code += service_query(optarg, report_form, 1156 encryption, verbose); 1157 break; 1158 case 'S': 1159 exit_code += service_query(optarg, report_print_wheels, 1160 encryption, verbose); 1161 break; 1162 case 's': 1163 exit_code += lpstat_service_status(encryption); 1164 exit_code += lpstat_default_printer(encryption); 1165 exit_code += printer_query(NULL, report_class, 1166 encryption, verbose, 0); 1167 exit_code += printer_query(NULL, report_device, 1168 encryption, verbose, 0); 1169 exit_code += service_query(optarg, report_form, 1170 encryption, verbose); 1171 exit_code += service_query(optarg, report_print_wheels, 1172 encryption, verbose); 1173 break; 1174 case 't': 1175 exit_code += lpstat_service_status(encryption); 1176 exit_code += lpstat_default_printer(encryption); 1177 exit_code += printer_query(NULL, report_class, 1178 encryption, verbose, 0); 1179 exit_code += printer_query(NULL, report_device, 1180 encryption, verbose, 0); 1181 exit_code += printer_query(NULL, report_accepting, 1182 encryption, verbose, 0); 1183 exit_code += printer_query(NULL, report_printer, 1184 encryption, verbose, 0); 1185 exit_code += service_query(optarg, report_form, 1186 encryption, verbose); 1187 exit_code += service_query(optarg, report_print_wheels, 1188 encryption, verbose); 1189 exit_code += job_query(NULL, report_job, 1190 encryption, rank, verbose); 1191 break; 1192 case 'L': /* local-only, ignored */ 1193 case 'l': /* increased verbose level in first pass */ 1194 case 'D': /* set "description" flag in first pass */ 1195 case 'E': /* set encryption in the first pass */ 1196 break; 1197 default: 1198 usage(av[0]); 1199 } 1200 } 1201 ac--; 1202 1203 if (ac == 1) { /* report on my jobs */ 1204 struct passwd *pw = getpwuid(getuid()); 1205 1206 if (pw != NULL) 1207 users = strsplit(pw->pw_name, ""); 1208 exit_code += job_query(NULL, report_job, encryption, 1209 rank, verbose); 1210 if (users != NULL) { 1211 free(users); 1212 users = NULL; 1213 } 1214 } else { 1215 for (c = optind; c < ac; c++) 1216 exit_code += job_query(argv[c], report_job, encryption, 1217 rank, verbose); 1218 } 1219 1220 1221 if (exit_code != 0) 1222 exit_code = 1; 1223 1224 return (exit_code); 1225 } 1226