xref: /onnv-gate/usr/src/cmd/print/printer-info/printer-info.c (revision 245:b644fbc06615)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*245Sjacobs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <sys/ioctl.h>
330Sstevel@tonic-gate #include <sys/prnio.h>
340Sstevel@tonic-gate #include <fcntl.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	COMMAND_SET_MAX	16	/* more than 16 command sets is not likely */
370Sstevel@tonic-gate #define	NP(x)	(x ? x : "")
380Sstevel@tonic-gate 
390Sstevel@tonic-gate typedef struct {
400Sstevel@tonic-gate 	char *manufacturer;
410Sstevel@tonic-gate 	char *model;
420Sstevel@tonic-gate 	char *description;
430Sstevel@tonic-gate 	char *class;
440Sstevel@tonic-gate 	char *command_set[COMMAND_SET_MAX];
450Sstevel@tonic-gate } printer_description_t;
460Sstevel@tonic-gate 
470Sstevel@tonic-gate int
get_printer_description(char * path,printer_description_t * info)480Sstevel@tonic-gate get_printer_description(char *path, printer_description_t *info)
490Sstevel@tonic-gate {
500Sstevel@tonic-gate 	int fd, rc;
510Sstevel@tonic-gate 	struct prn_1284_device_id id;
520Sstevel@tonic-gate 	char buf[BUFSIZ];
530Sstevel@tonic-gate 	char *s, *iter = NULL;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	/* open the device */
560Sstevel@tonic-gate 	if ((fd = open(path, O_RDWR)) < 0)
570Sstevel@tonic-gate 		return (fd);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	/* get the 1284 device id */
600Sstevel@tonic-gate 	memset(&id, 0, sizeof (id));
610Sstevel@tonic-gate 	memset(&buf, 0, sizeof (buf));
620Sstevel@tonic-gate 	id.id_len = sizeof (buf);
630Sstevel@tonic-gate 	id.id_data = buf;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	rc = ioctl(fd, PRNIOC_GET_1284_DEVID, &id);
660Sstevel@tonic-gate 	/* close(fd); */
670Sstevel@tonic-gate 	if (rc < 0)
680Sstevel@tonic-gate 		return (rc);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	memset(info, 0, sizeof (*info));
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	/* parse the 1284 device id string */
730Sstevel@tonic-gate 	for (s = (char *)strtok_r(buf, ";\n", &iter); s != NULL;
740Sstevel@tonic-gate 			s = (char *)strtok_r(NULL, ";\n", &iter)) {
750Sstevel@tonic-gate 		char *t, *u, *iter2 = NULL;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 		if ((t = (char *)strtok_r(s, ":\n", &iter2)) == NULL)
780Sstevel@tonic-gate 			continue;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 		if ((u = (char *)strtok_r(NULL, ":\n", &iter2)) == NULL)
810Sstevel@tonic-gate 			continue;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 		if ((strcasecmp(t, "MFG") == 0) ||
840Sstevel@tonic-gate 		    (strcasecmp(t, "MANUFACTURER") == 0))
850Sstevel@tonic-gate 			info->manufacturer = strdup(u);
860Sstevel@tonic-gate 		else if ((strcasecmp(t, "MDL") == 0) ||
870Sstevel@tonic-gate 		    (strcasecmp(t, "MODEL") == 0))
880Sstevel@tonic-gate 			info->model = strdup(u);
890Sstevel@tonic-gate 		else if ((strcasecmp(t, "DES") == 0) ||
900Sstevel@tonic-gate 		    (strcasecmp(t, "DESCRIPTION") == 0))
910Sstevel@tonic-gate 			info->description = strdup(u);
920Sstevel@tonic-gate 		else if ((strcasecmp(t, "CLS") == 0) ||
930Sstevel@tonic-gate 		    (strcasecmp(t, "CLASS") == 0))
940Sstevel@tonic-gate 			info->class = strdup(u);
950Sstevel@tonic-gate 		else if ((strcasecmp(t, "CMD") == 0) ||
960Sstevel@tonic-gate 		    (strcasecmp(t, "COMMAND SET") == 0)) {
970Sstevel@tonic-gate 			/* this should be more dynamic, I got lazy */
980Sstevel@tonic-gate 			char *v, *iter3 = NULL;
990Sstevel@tonic-gate 			int i = 0;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 			for (v = (char *)strtok_r(u, ",\n", &iter3);
1020Sstevel@tonic-gate 				((v != NULL) && (i < COMMAND_SET_MAX));
1030Sstevel@tonic-gate 			v = (char *)strtok_r(NULL, ",\n", &iter3))
1040Sstevel@tonic-gate 				info->command_set[i++] = strdup(v);
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	return (0);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static void
usage(char * name)1120Sstevel@tonic-gate usage(char *name)
1130Sstevel@tonic-gate {
1140Sstevel@tonic-gate 	char *program;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if ((program = strrchr(name, '/')) == NULL)
1170Sstevel@tonic-gate 		program = name;
1180Sstevel@tonic-gate 	else
1190Sstevel@tonic-gate 		program++;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	printf("Usage: %s [-aMmdCc] (path) ...\n", program);
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate int
main(int ac,char * av[])1250Sstevel@tonic-gate main(int ac, char *av[])
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	int rc;
1280Sstevel@tonic-gate 	int manufacturer = 0, model = 0, description = 0, command_set = 0,
1290Sstevel@tonic-gate 	    class = 0;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	while ((rc = getopt(ac, av, "aMmdCc")) != EOF)
1320Sstevel@tonic-gate 		switch (rc) {
1330Sstevel@tonic-gate 		case 'a':
1340Sstevel@tonic-gate 			manufacturer++;
1350Sstevel@tonic-gate 			model++;
1360Sstevel@tonic-gate 			description++;
1370Sstevel@tonic-gate 			command_set++;
1380Sstevel@tonic-gate 			class++;
1390Sstevel@tonic-gate 			break;
1400Sstevel@tonic-gate 		case 'M':
1410Sstevel@tonic-gate 			manufacturer++;
1420Sstevel@tonic-gate 			break;
1430Sstevel@tonic-gate 		case 'm':
1440Sstevel@tonic-gate 			model++;
1450Sstevel@tonic-gate 			break;
1460Sstevel@tonic-gate 		case 'd':
1470Sstevel@tonic-gate 			description++;
1480Sstevel@tonic-gate 			break;
1490Sstevel@tonic-gate 		case 'C':
1500Sstevel@tonic-gate 			command_set++;
1510Sstevel@tonic-gate 			break;
1520Sstevel@tonic-gate 		case 'c':
1530Sstevel@tonic-gate 			class++;
1540Sstevel@tonic-gate 			break;
1550Sstevel@tonic-gate 		default:
1560Sstevel@tonic-gate 			usage(av[0]);
1570Sstevel@tonic-gate 			exit(1);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	if (optind >= ac) {
1610Sstevel@tonic-gate 		usage(av[0]);
1620Sstevel@tonic-gate 		exit(1);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	while (optind < ac) {
1660Sstevel@tonic-gate 		char *path = av[optind++];
1670Sstevel@tonic-gate 		printer_description_t info;
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 		rc = get_printer_description(path, &info);
1700Sstevel@tonic-gate 		if (rc == 0) {
1710Sstevel@tonic-gate 			printf("%s:\n", path);
1720Sstevel@tonic-gate 			if (manufacturer != 0)
1730Sstevel@tonic-gate 				printf("\tManufacturer: %s\n",
1740Sstevel@tonic-gate 						NP(info.manufacturer));
1750Sstevel@tonic-gate 			if (model != 0)
1760Sstevel@tonic-gate 				printf("\tModel:        %s\n",
1770Sstevel@tonic-gate 						NP(info.model));
1780Sstevel@tonic-gate 			if (description != 0)
1790Sstevel@tonic-gate 				printf("\tDescription:  %s\n",
1800Sstevel@tonic-gate 						NP(info.description));
1810Sstevel@tonic-gate 			if (class != 0)
1820Sstevel@tonic-gate 				printf("\tClass:        %s\n",
1830Sstevel@tonic-gate 						NP(info.class));
1840Sstevel@tonic-gate 			if (command_set != 0) {
185*245Sjacobs 				int i;
186*245Sjacobs 
1870Sstevel@tonic-gate 				printf("\tCommand set:\n");
188*245Sjacobs 				for (i = 0; info.command_set[i] != NULL; i++)
1890Sstevel@tonic-gate 					printf("\t\tcmd[%d]: %s\n", i,
190*245Sjacobs 						info.command_set[i]);
1910Sstevel@tonic-gate 			}
1920Sstevel@tonic-gate 		} else
1930Sstevel@tonic-gate 			perror(path);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 	return (rc);
1960Sstevel@tonic-gate }
197