xref: /titanic_52/usr/src/cmd/print/printer-info/printer-info.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <string.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/prnio.h>
34*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	COMMAND_SET_MAX	16	/* more than 16 command sets is not likely */
37*7c478bd9Sstevel@tonic-gate #define	NP(x)	(x ? x : "")
38*7c478bd9Sstevel@tonic-gate 
39*7c478bd9Sstevel@tonic-gate typedef struct {
40*7c478bd9Sstevel@tonic-gate 	char *manufacturer;
41*7c478bd9Sstevel@tonic-gate 	char *model;
42*7c478bd9Sstevel@tonic-gate 	char *description;
43*7c478bd9Sstevel@tonic-gate 	char *class;
44*7c478bd9Sstevel@tonic-gate 	char *command_set[COMMAND_SET_MAX];
45*7c478bd9Sstevel@tonic-gate } printer_description_t;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate int
48*7c478bd9Sstevel@tonic-gate get_printer_description(char *path, printer_description_t *info)
49*7c478bd9Sstevel@tonic-gate {
50*7c478bd9Sstevel@tonic-gate 	int fd, rc;
51*7c478bd9Sstevel@tonic-gate 	struct prn_1284_device_id id;
52*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
53*7c478bd9Sstevel@tonic-gate 	char *s, *iter = NULL;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	/* open the device */
56*7c478bd9Sstevel@tonic-gate 	if ((fd = open(path, O_RDWR)) < 0)
57*7c478bd9Sstevel@tonic-gate 		return (fd);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	/* get the 1284 device id */
60*7c478bd9Sstevel@tonic-gate 	memset(&id, 0, sizeof (id));
61*7c478bd9Sstevel@tonic-gate 	memset(&buf, 0, sizeof (buf));
62*7c478bd9Sstevel@tonic-gate 	id.id_len = sizeof (buf);
63*7c478bd9Sstevel@tonic-gate 	id.id_data = buf;
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 	rc = ioctl(fd, PRNIOC_GET_1284_DEVID, &id);
66*7c478bd9Sstevel@tonic-gate 	/* close(fd); */
67*7c478bd9Sstevel@tonic-gate 	if (rc < 0)
68*7c478bd9Sstevel@tonic-gate 		return (rc);
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate 	memset(info, 0, sizeof (*info));
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate 	/* parse the 1284 device id string */
73*7c478bd9Sstevel@tonic-gate 	for (s = (char *)strtok_r(buf, ";\n", &iter); s != NULL;
74*7c478bd9Sstevel@tonic-gate 			s = (char *)strtok_r(NULL, ";\n", &iter)) {
75*7c478bd9Sstevel@tonic-gate 		char *t, *u, *iter2 = NULL;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 		if ((t = (char *)strtok_r(s, ":\n", &iter2)) == NULL)
78*7c478bd9Sstevel@tonic-gate 			continue;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 		if ((u = (char *)strtok_r(NULL, ":\n", &iter2)) == NULL)
81*7c478bd9Sstevel@tonic-gate 			continue;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 		if ((strcasecmp(t, "MFG") == 0) ||
84*7c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "MANUFACTURER") == 0))
85*7c478bd9Sstevel@tonic-gate 			info->manufacturer = strdup(u);
86*7c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "MDL") == 0) ||
87*7c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "MODEL") == 0))
88*7c478bd9Sstevel@tonic-gate 			info->model = strdup(u);
89*7c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "DES") == 0) ||
90*7c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "DESCRIPTION") == 0))
91*7c478bd9Sstevel@tonic-gate 			info->description = strdup(u);
92*7c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "CLS") == 0) ||
93*7c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "CLASS") == 0))
94*7c478bd9Sstevel@tonic-gate 			info->class = strdup(u);
95*7c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "CMD") == 0) ||
96*7c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "COMMAND SET") == 0)) {
97*7c478bd9Sstevel@tonic-gate 			/* this should be more dynamic, I got lazy */
98*7c478bd9Sstevel@tonic-gate 			char *v, *iter3 = NULL;
99*7c478bd9Sstevel@tonic-gate 			int i = 0;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 			for (v = (char *)strtok_r(u, ",\n", &iter3);
102*7c478bd9Sstevel@tonic-gate 				((v != NULL) && (i < COMMAND_SET_MAX));
103*7c478bd9Sstevel@tonic-gate 			v = (char *)strtok_r(NULL, ",\n", &iter3))
104*7c478bd9Sstevel@tonic-gate 				info->command_set[i++] = strdup(v);
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	return (0);
109*7c478bd9Sstevel@tonic-gate }
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate static void
112*7c478bd9Sstevel@tonic-gate usage(char *name)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	char *program;
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if ((program = strrchr(name, '/')) == NULL)
117*7c478bd9Sstevel@tonic-gate 		program = name;
118*7c478bd9Sstevel@tonic-gate 	else
119*7c478bd9Sstevel@tonic-gate 		program++;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	printf("Usage: %s [-aMmdCc] (path) ...\n", program);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate int
125*7c478bd9Sstevel@tonic-gate main(int ac, char *av[])
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	int rc;
128*7c478bd9Sstevel@tonic-gate 	int manufacturer = 0, model = 0, description = 0, command_set = 0,
129*7c478bd9Sstevel@tonic-gate 	    class = 0;
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	while ((rc = getopt(ac, av, "aMmdCc")) != EOF)
132*7c478bd9Sstevel@tonic-gate 		switch (rc) {
133*7c478bd9Sstevel@tonic-gate 		case 'a':
134*7c478bd9Sstevel@tonic-gate 			manufacturer++;
135*7c478bd9Sstevel@tonic-gate 			model++;
136*7c478bd9Sstevel@tonic-gate 			description++;
137*7c478bd9Sstevel@tonic-gate 			command_set++;
138*7c478bd9Sstevel@tonic-gate 			class++;
139*7c478bd9Sstevel@tonic-gate 			break;
140*7c478bd9Sstevel@tonic-gate 		case 'M':
141*7c478bd9Sstevel@tonic-gate 			manufacturer++;
142*7c478bd9Sstevel@tonic-gate 			break;
143*7c478bd9Sstevel@tonic-gate 		case 'm':
144*7c478bd9Sstevel@tonic-gate 			model++;
145*7c478bd9Sstevel@tonic-gate 			break;
146*7c478bd9Sstevel@tonic-gate 		case 'd':
147*7c478bd9Sstevel@tonic-gate 			description++;
148*7c478bd9Sstevel@tonic-gate 			break;
149*7c478bd9Sstevel@tonic-gate 		case 'C':
150*7c478bd9Sstevel@tonic-gate 			command_set++;
151*7c478bd9Sstevel@tonic-gate 			break;
152*7c478bd9Sstevel@tonic-gate 		case 'c':
153*7c478bd9Sstevel@tonic-gate 			class++;
154*7c478bd9Sstevel@tonic-gate 			break;
155*7c478bd9Sstevel@tonic-gate 		default:
156*7c478bd9Sstevel@tonic-gate 			usage(av[0]);
157*7c478bd9Sstevel@tonic-gate 			exit(1);
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	if (optind >= ac) {
161*7c478bd9Sstevel@tonic-gate 		usage(av[0]);
162*7c478bd9Sstevel@tonic-gate 		exit(1);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	while (optind < ac) {
166*7c478bd9Sstevel@tonic-gate 		char *path = av[optind++];
167*7c478bd9Sstevel@tonic-gate 		printer_description_t info;
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 		rc = get_printer_description(path, &info);
170*7c478bd9Sstevel@tonic-gate 		if (rc == 0) {
171*7c478bd9Sstevel@tonic-gate 			int i = 0;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 			printf("%s:\n", path);
174*7c478bd9Sstevel@tonic-gate 			if (manufacturer != 0)
175*7c478bd9Sstevel@tonic-gate 				printf("\tManufacturer: %s\n",
176*7c478bd9Sstevel@tonic-gate 						NP(info.manufacturer));
177*7c478bd9Sstevel@tonic-gate 			if (model != 0)
178*7c478bd9Sstevel@tonic-gate 				printf("\tModel:        %s\n",
179*7c478bd9Sstevel@tonic-gate 						NP(info.model));
180*7c478bd9Sstevel@tonic-gate 			if (description != 0)
181*7c478bd9Sstevel@tonic-gate 				printf("\tDescription:  %s\n",
182*7c478bd9Sstevel@tonic-gate 						NP(info.description));
183*7c478bd9Sstevel@tonic-gate 			if (class != 0)
184*7c478bd9Sstevel@tonic-gate 				printf("\tClass:        %s\n",
185*7c478bd9Sstevel@tonic-gate 						NP(info.class));
186*7c478bd9Sstevel@tonic-gate 			if (command_set != 0) {
187*7c478bd9Sstevel@tonic-gate 				printf("\tCommand set:\n");
188*7c478bd9Sstevel@tonic-gate 				while (info.command_set[i] != NULL)
189*7c478bd9Sstevel@tonic-gate 					printf("\t\tcmd[%d]: %s\n", i,
190*7c478bd9Sstevel@tonic-gate 						info.command_set[i++]);
191*7c478bd9Sstevel@tonic-gate 			}
192*7c478bd9Sstevel@tonic-gate 		} else
193*7c478bd9Sstevel@tonic-gate 			perror(path);
194*7c478bd9Sstevel@tonic-gate 	}
195*7c478bd9Sstevel@tonic-gate 	return (rc);
196*7c478bd9Sstevel@tonic-gate }
197