9804 hal-set-property should support --direct option
[unleashed.git] / usr / src / cmd / print / lpget / lpget.c
blob5c19480ae1d7e623d07f979fc945bbda99063b16
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <locale.h>
36 #ifndef SUNOS_4
37 #include <libintl.h>
38 #endif
40 #include <ns.h>
41 #include <list.h>
43 extern char *optarg;
44 extern int optind, opterr, optopt;
45 extern char *getenv(const char *);
48 static void
49 Usage(char *name)
51 (void) fprintf(stderr,
52 gettext("Usage: %s [-k key] [list|(printer) ...]\n"),
53 name);
54 exit(1);
57 static int
58 display_kvp(char *key, char *value)
60 int rc = -1;
62 if (value != NULL) {
63 rc = 0;
64 (void) printf("\n\t%s=%s", key, value);
65 } else
66 (void) printf(gettext("\n\t%s - undefined"), key);
68 return (rc);
72 static int
73 display_value(ns_printer_t *printer, char *name, char **keys)
75 int rc = -1;
77 if (printer != NULL) {
78 rc = 0;
79 (void) printf("%s:", name);
80 if (keys != NULL) {
81 while (*keys != NULL) {
82 char *string = ns_get_value_string(*keys,
83 printer);
84 rc += display_kvp(*keys, string);
85 keys++;
87 } else {
88 ns_kvp_t **list = printer->attributes;
90 for (list = printer->attributes;
91 (list != NULL && *list != NULL); list++) {
92 char *string;
93 if (((*list)->key[0] == '\t') ||
94 ((*list)->key[0] == ' '))
95 continue;
97 string = ns_get_value_string((*list)->key,
98 printer);
99 rc += display_kvp((*list)->key, string);
102 (void) printf("\n");
103 } else
104 (void) printf(gettext("%s: Not Found\n"), name);
106 return (rc);
111 * main() calls the appropriate routine to parse the command line arguments
112 * and then calls the local remove routine, followed by the remote remove
113 * routine to remove jobs.
116 main(int ac, char *av[])
118 char *program;
119 int c;
120 char **keys = NULL;
121 char *ns = NULL;
122 int exit_code = 0;
124 (void) setlocale(LC_ALL, "");
126 #if !defined(TEXT_DOMAIN)
127 #define TEXT_DOMAIN "SYS_TEST"
128 #endif
129 (void) textdomain(TEXT_DOMAIN);
131 if ((program = strrchr(av[0], '/')) == NULL)
132 program = av[0];
133 else
134 program++;
136 openlog(program, LOG_PID, LOG_LPR);
137 while ((c = getopt(ac, av, "k:t:n:")) != EOF)
138 switch (c) {
139 case 'k':
140 case 't':
141 keys = (char **)list_append((void **)keys,
142 (void *)optarg);
143 break;
144 case 'n':
145 ns = optarg;
146 break;
147 default:
148 Usage(program);
151 if (optind >= ac)
152 Usage(program);
154 ns = normalize_ns_name(ns);
156 while (optind < ac) {
157 char *name = av[optind++];
159 if (strcmp(name, "list") == 0) {
160 ns_printer_t **printers = ns_printer_get_list(ns);
162 while (printers != NULL && *printers != NULL) {
163 exit_code += display_value(*printers,
164 (*printers)->name, keys);
165 printers++;
167 } else
168 exit_code = display_value(ns_printer_get_name(name, ns),
169 name, keys);
173 return (exit_code);