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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 /* $Id: lpc.c 146 2006-03-24 00:26:54Z njacobs $ */
39 typedef int (cmd_handler_t
)(papi_service_t
, char **);
41 static papi_encryption_t encryption
= PAPI_ENCRYPT_NEVER
;
45 lpc_exit(papi_service_t svc
, char **args
)
53 lpc_status(papi_service_t svc
, char **args
)
56 papi_printer_t p
= NULL
;
57 char *pattrs
[] = { "printer-state", "printer-state-reasons",
58 "printer-is-accepting-jobs", NULL
};
59 char *destination
= args
[1];
61 status
= papiPrinterQuery(svc
, destination
, pattrs
, NULL
, &p
);
62 if (status
== PAPI_OK
) {
63 papi_attribute_t
**list
= papiPrinterGetAttributeList(p
);
67 printf("%s:\n", destination
);
69 (void) papiAttributeListGetBoolean(list
, NULL
,
70 "printer-is-accepting-jobs", &accepting
);
71 printf(gettext("\tqueueing is %s\n"),
72 (accepting
? gettext("enabled") : gettext("disabled")));
74 (void) papiAttributeListGetInteger(list
, NULL
,
75 "printer-state", &state
);
76 printf("\tprinting is %s\n",
77 ((state
!= 0x05) ? gettext("enabled") :
78 gettext("disabled")));
80 if (state
!= 0x03) { /* !idle */
81 papi_job_t
*jobs
= NULL
;
84 (void) papiPrinterListJobs(svc
, destination
, NULL
,
85 PAPI_LIST_JOBS_ALL
, 0, &jobs
);
87 for (i
= 0; jobs
[i
] != NULL
; i
++);
88 papiJobListFree(jobs
);
90 printf(gettext("\t%d entries in spool area\n"), i
);
92 printf(gettext("\tno entries\n"));
95 printf(gettext("\tdaemon present\n"));
98 fprintf(stderr
, "%s: %s\n", destination
,
99 verbose_papi_message(svc
, status
));
109 lpc_abort(papi_service_t svc
, char **args
)
111 papi_status_t status
;
112 char *destination
= args
[1];
114 if (destination
== NULL
) {
115 fprintf(stderr
, gettext("Usage: abort (destination)\n"));
119 status
= papiPrinterPause(svc
, destination
, "paused via lpc abort");
120 if (status
== PAPI_OK
) {
121 printf(gettext("%s: processing disabled after current job\n"),
124 fprintf(stderr
, "%s: %s\n", destination
,
125 verbose_papi_message(svc
, status
));
132 lpc_clean(papi_service_t svc
, char **args
)
134 papi_status_t status
;
135 papi_job_t
*jobs
= NULL
;
136 char *destination
= args
[1];
138 if (destination
== NULL
) {
139 fprintf(stderr
, gettext("Usage: clean (destination)\n"));
143 status
= papiPrinterPurgeJobs(svc
, destination
, &jobs
);
144 if (status
!= PAPI_OK
) {
145 fprintf(stderr
, gettext("clean: %s: %s\n"), destination
,
146 verbose_papi_message(svc
, status
));
153 for (i
= 0; jobs
[i
] != NULL
; i
++)
154 printf(gettext("\t%s-%d: cancelled\n"), destination
,
155 papiJobGetId(jobs
[i
]));
157 papiJobListFree(jobs
);
164 lpc_disable(papi_service_t svc
, char **args
)
166 papi_status_t status
;
167 char *destination
= args
[1];
169 if (destination
== NULL
) {
170 fprintf(stderr
, gettext("Usage: disable: (destination)\n"));
174 status
= papiPrinterDisable(svc
, destination
, NULL
);
175 if (status
!= PAPI_OK
) {
176 fprintf(stderr
, gettext("disable: %s: %s\n"), destination
,
177 verbose_papi_message(svc
, status
));
185 lpc_enable(papi_service_t svc
, char **args
)
187 papi_status_t status
;
188 char *destination
= args
[1];
190 if (destination
== NULL
) {
191 fprintf(stderr
, gettext("Usage: enable: (destination)\n"));
195 status
= papiPrinterEnable(svc
, destination
);
196 if (status
!= PAPI_OK
) {
197 fprintf(stderr
, gettext("enable: %s: %s\n"), destination
,
198 verbose_papi_message(svc
, status
));
206 lpc_restart(papi_service_t svc
, char **args
)
210 rc
+= lpc_disable(svc
, args
);
211 rc
+= lpc_enable(svc
, args
);
217 lpc_start(papi_service_t svc
, char **args
)
219 papi_status_t status
;
220 char *destination
= args
[1];
222 if (destination
== NULL
) {
223 fprintf(stderr
, gettext("Usage: start (destination)\n"));
227 status
= papiPrinterResume(svc
, destination
);
228 if (status
!= PAPI_OK
) {
229 fprintf(stderr
, gettext("start: %s: %s\n"), destination
,
230 verbose_papi_message(svc
, status
));
238 lpc_stop(papi_service_t svc
, char **args
)
240 papi_status_t status
;
241 char *destination
= args
[1];
243 if (destination
== NULL
) {
244 fprintf(stderr
, gettext("Usage: stop (destination)\n"));
248 status
= papiPrinterPause(svc
, destination
, "paused via lpc");
249 if (status
!= PAPI_OK
) {
250 fprintf(stderr
, gettext("stop: %s: %s\n"), destination
,
251 verbose_papi_message(svc
, status
));
259 lpc_topq(papi_service_t svc
, char **args
)
261 papi_status_t status
;
262 char *destination
= args
[1];
263 char *idstr
= args
[2];
266 if (destination
== NULL
|| idstr
== NULL
) {
267 fprintf(stderr
, gettext("Usage: topq (destination) (id)\n"));
272 status
= papiJobPromote(svc
, destination
, id
);
273 if (status
!= PAPI_OK
) {
274 fprintf(stderr
, gettext("topq: %s-%d: %s\n"), destination
, id
,
275 verbose_papi_message(svc
, status
));
283 lpc_up(papi_service_t svc
, char **args
)
287 rc
+= lpc_enable(svc
, args
);
288 rc
+= lpc_start(svc
, args
);
294 lpc_down(papi_service_t svc
, char **args
)
298 rc
+= lpc_disable(svc
, args
);
299 rc
+= lpc_stop(svc
, args
);
304 static int lpc_help(papi_service_t svc
, char **args
); /* forward reference */
306 static char help_help
[] = "get help on commands";
307 static char help_exit
[] = "exit lpc";
308 static char help_status
[] = "show status of daemon and queue";
309 static char help_abort
[] =
310 "disable print queue terminating any active job processing";
311 static char help_clean
[] = "remove all jobs from a queue";
312 static char help_disable
[] = "turn off spooling to a queue";
313 static char help_down
[] =
314 "turn off queueing and printing for a queue and set a reason";
315 static char help_enable
[] = "turn on spooling to a queue";
316 static char help_restart
[] = "restart job processing for a queue";
317 static char help_start
[] = "turn on printing from a queue";
318 static char help_stop
[] = "turn off printing from a queue";
319 static char help_up
[] = "turn on queueing and printing for a queue";
320 static char help_topq
[] = "put a job at the top of the queue";
324 int (*handler
)(papi_service_t svc
, char **args
);
328 { "?", lpc_help
, help_help
, 0 },
329 { "help", lpc_help
, help_help
, 0 },
330 { "exit", lpc_exit
, help_exit
, 0 },
331 { "quit", lpc_exit
, help_exit
, 0 },
332 { "status", lpc_status
, help_status
, 1 },
333 { "abort", lpc_abort
, help_abort
, 1 },
334 { "clean", lpc_clean
, help_clean
, 1 },
335 { "disable", lpc_disable
, help_disable
, 1 },
336 { "down", lpc_down
, help_down
, 2 },
337 { "enable", lpc_enable
, help_enable
, 1 },
338 { "restart", lpc_restart
, help_restart
, 1 },
339 { "start", lpc_start
, help_start
, 1 },
340 { "stop", lpc_stop
, help_stop
, 1 },
341 { "up", lpc_up
, help_up
, 1 },
342 { "topq", lpc_topq
, help_topq
, 2 },
343 { NULL
, NULL
, NULL
, 0 }
347 lpc_handler(char *cmd
, cmd_handler_t
**handler
)
351 for (i
= 0; cmd_tab
[i
].cmd
!= NULL
; i
++)
352 if (strcmp(cmd
, cmd_tab
[i
].cmd
) == 0) {
353 *handler
= cmd_tab
[i
].handler
;
354 return (cmd_tab
[i
].num_args
);
360 lpc_helptext(char *cmd
)
364 for (i
= 0; cmd_tab
[i
].cmd
!= NULL
; i
++)
365 if (strcmp(cmd
, cmd_tab
[i
].cmd
) == 0)
366 return (gettext(cmd_tab
[i
].help_string
));
372 lpc_help(papi_service_t svc
, char **args
)
374 if (args
[1] == NULL
) {
377 printf(gettext("Commands are:\n\n"));
378 for (i
= 0; cmd_tab
[i
].cmd
!= NULL
; i
++) {
379 printf("\t%s", cmd_tab
[i
].cmd
);
386 char *helptext
= lpc_helptext(args
[1]);
388 if (helptext
== NULL
)
389 helptext
= gettext("no such command");
391 printf("%s: %s\n", args
[1], helptext
);
398 process_one(int (*handler
)(papi_service_t
, char **), char **av
, int expected
)
401 papi_status_t status
= PAPI_OK
;
402 papi_service_t svc
= NULL
;
403 char *printer
= av
[1];
405 if ((printer
!= NULL
) && (expected
!= 0)) {
406 status
= papiServiceCreate(&svc
, printer
, NULL
, NULL
,
407 cli_auth_callback
, encryption
, NULL
);
408 if (status
!= PAPI_OK
) {
409 fprintf(stderr
, gettext(
410 "Failed to contact service for %s: %s\n"),
411 printer
, verbose_papi_message(svc
, status
));
415 if (status
== PAPI_OK
)
416 rc
= handler(svc
, av
);
419 papiServiceDestroy(svc
);
425 process_all(int (*handler
)(papi_service_t
, char **), char **av
, int expected
)
427 papi_status_t status
;
428 papi_service_t svc
= NULL
;
432 status
= papiServiceCreate(&svc
, NULL
, NULL
, NULL
, NULL
,
434 if (status
!= PAPI_OK
) {
435 fprintf(stderr
, gettext("Failed to contact service: %s\n"),
436 verbose_papi_message(svc
, status
));
440 if ((printers
= interest_list(svc
)) != NULL
) {
443 for (i
= 0; printers
[i
] != NULL
; i
++) {
445 rc
+= process_one(handler
, av
, expected
);
449 papiServiceDestroy(svc
);
455 process(int ac
, char **av
)
457 int (*handler
)(papi_service_t
, char **) = NULL
;
460 char *printer
= av
[1];
463 if ((num_args
= lpc_handler(av
[0], &handler
)) < 0) {
464 printf(gettext("%s: invalid command\n"), av
[0]);
468 if (((ac
== 0) && (num_args
== 1)) ||
469 ((printer
!= NULL
) && strcmp(printer
, "all") == 0))
470 rc
= process_all(handler
, av
, num_args
);
471 else if (num_args
< ac
) {
475 memset(argv
, 0, sizeof (argv
));
478 if (strcmp(av
[0], "topq") == 0) {
480 for (i
= 2; i
<= ac
; i
++) {
482 process_one(handler
, argv
, num_args
);
485 for (i
= 1; i
<= ac
; i
++) {
487 process_one(handler
, argv
, num_args
);
490 rc
= process_one(handler
, av
, num_args
);
500 if ((name
= strrchr(program
, '/')) == NULL
)
506 gettext("Usage: %s [ command [ parameter...]]\n"),
520 fprintf(stdout
, "lpc> ");
524 if (fgets(line
, sizeof (line
), stdin
) == NULL
)
526 if ((av
= strsplit(line
, " \t\n")) != NULL
)
527 for (ac
= 0; av
[ac
] != NULL
; ac
++);
532 (void) process(ac
- 1, av
);
538 main(int ac
, char *av
[])
543 (void) setlocale(LC_ALL
, "");
544 (void) textdomain("SUNW_OST_OSCMD");
546 while ((c
= getopt(ac
, av
, "E")) != EOF
)
549 encryption
= PAPI_ENCRYPT_ALWAYS
;
558 result
= process(ac
- optind
- 1, &av
[optind
]);