xref: /titanic_50/usr/src/cmd/print/printer-info/printer-info.c (revision b11ac39f7d50211a3de081489d8d964e4cfeb0f9)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*b11ac39fSjacobs  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <unistd.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
337c478bd9Sstevel@tonic-gate #include <sys/prnio.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #define	COMMAND_SET_MAX	16	/* more than 16 command sets is not likely */
377c478bd9Sstevel@tonic-gate #define	NP(x)	(x ? x : "")
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate typedef struct {
407c478bd9Sstevel@tonic-gate 	char *manufacturer;
417c478bd9Sstevel@tonic-gate 	char *model;
427c478bd9Sstevel@tonic-gate 	char *description;
437c478bd9Sstevel@tonic-gate 	char *class;
447c478bd9Sstevel@tonic-gate 	char *command_set[COMMAND_SET_MAX];
457c478bd9Sstevel@tonic-gate } printer_description_t;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate int
get_printer_description(char * path,printer_description_t * info)487c478bd9Sstevel@tonic-gate get_printer_description(char *path, printer_description_t *info)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	int fd, rc;
517c478bd9Sstevel@tonic-gate 	struct prn_1284_device_id id;
527c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
537c478bd9Sstevel@tonic-gate 	char *s, *iter = NULL;
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	/* open the device */
567c478bd9Sstevel@tonic-gate 	if ((fd = open(path, O_RDWR)) < 0)
577c478bd9Sstevel@tonic-gate 		return (fd);
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/* get the 1284 device id */
607c478bd9Sstevel@tonic-gate 	memset(&id, 0, sizeof (id));
617c478bd9Sstevel@tonic-gate 	memset(&buf, 0, sizeof (buf));
627c478bd9Sstevel@tonic-gate 	id.id_len = sizeof (buf);
637c478bd9Sstevel@tonic-gate 	id.id_data = buf;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	rc = ioctl(fd, PRNIOC_GET_1284_DEVID, &id);
667c478bd9Sstevel@tonic-gate 	/* close(fd); */
677c478bd9Sstevel@tonic-gate 	if (rc < 0)
687c478bd9Sstevel@tonic-gate 		return (rc);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	memset(info, 0, sizeof (*info));
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/* parse the 1284 device id string */
737c478bd9Sstevel@tonic-gate 	for (s = (char *)strtok_r(buf, ";\n", &iter); s != NULL;
747c478bd9Sstevel@tonic-gate 			s = (char *)strtok_r(NULL, ";\n", &iter)) {
757c478bd9Sstevel@tonic-gate 		char *t, *u, *iter2 = NULL;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 		if ((t = (char *)strtok_r(s, ":\n", &iter2)) == NULL)
787c478bd9Sstevel@tonic-gate 			continue;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 		if ((u = (char *)strtok_r(NULL, ":\n", &iter2)) == NULL)
817c478bd9Sstevel@tonic-gate 			continue;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 		if ((strcasecmp(t, "MFG") == 0) ||
847c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "MANUFACTURER") == 0))
857c478bd9Sstevel@tonic-gate 			info->manufacturer = strdup(u);
867c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "MDL") == 0) ||
877c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "MODEL") == 0))
887c478bd9Sstevel@tonic-gate 			info->model = strdup(u);
897c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "DES") == 0) ||
907c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "DESCRIPTION") == 0))
917c478bd9Sstevel@tonic-gate 			info->description = strdup(u);
927c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "CLS") == 0) ||
937c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "CLASS") == 0))
947c478bd9Sstevel@tonic-gate 			info->class = strdup(u);
957c478bd9Sstevel@tonic-gate 		else if ((strcasecmp(t, "CMD") == 0) ||
967c478bd9Sstevel@tonic-gate 		    (strcasecmp(t, "COMMAND SET") == 0)) {
977c478bd9Sstevel@tonic-gate 			/* this should be more dynamic, I got lazy */
987c478bd9Sstevel@tonic-gate 			char *v, *iter3 = NULL;
997c478bd9Sstevel@tonic-gate 			int i = 0;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 			for (v = (char *)strtok_r(u, ",\n", &iter3);
1027c478bd9Sstevel@tonic-gate 				((v != NULL) && (i < COMMAND_SET_MAX));
1037c478bd9Sstevel@tonic-gate 			v = (char *)strtok_r(NULL, ",\n", &iter3))
1047c478bd9Sstevel@tonic-gate 				info->command_set[i++] = strdup(v);
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	return (0);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static void
usage(char * name)1127c478bd9Sstevel@tonic-gate usage(char *name)
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate 	char *program;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	if ((program = strrchr(name, '/')) == NULL)
1177c478bd9Sstevel@tonic-gate 		program = name;
1187c478bd9Sstevel@tonic-gate 	else
1197c478bd9Sstevel@tonic-gate 		program++;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	printf("Usage: %s [-aMmdCc] (path) ...\n", program);
1227c478bd9Sstevel@tonic-gate }
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])1257c478bd9Sstevel@tonic-gate main(int ac, char *av[])
1267c478bd9Sstevel@tonic-gate {
1277c478bd9Sstevel@tonic-gate 	int rc;
1287c478bd9Sstevel@tonic-gate 	int manufacturer = 0, model = 0, description = 0, command_set = 0,
1297c478bd9Sstevel@tonic-gate 	    class = 0;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	while ((rc = getopt(ac, av, "aMmdCc")) != EOF)
1327c478bd9Sstevel@tonic-gate 		switch (rc) {
1337c478bd9Sstevel@tonic-gate 		case 'a':
1347c478bd9Sstevel@tonic-gate 			manufacturer++;
1357c478bd9Sstevel@tonic-gate 			model++;
1367c478bd9Sstevel@tonic-gate 			description++;
1377c478bd9Sstevel@tonic-gate 			command_set++;
1387c478bd9Sstevel@tonic-gate 			class++;
1397c478bd9Sstevel@tonic-gate 			break;
1407c478bd9Sstevel@tonic-gate 		case 'M':
1417c478bd9Sstevel@tonic-gate 			manufacturer++;
1427c478bd9Sstevel@tonic-gate 			break;
1437c478bd9Sstevel@tonic-gate 		case 'm':
1447c478bd9Sstevel@tonic-gate 			model++;
1457c478bd9Sstevel@tonic-gate 			break;
1467c478bd9Sstevel@tonic-gate 		case 'd':
1477c478bd9Sstevel@tonic-gate 			description++;
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 		case 'C':
1507c478bd9Sstevel@tonic-gate 			command_set++;
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case 'c':
1537c478bd9Sstevel@tonic-gate 			class++;
1547c478bd9Sstevel@tonic-gate 			break;
1557c478bd9Sstevel@tonic-gate 		default:
1567c478bd9Sstevel@tonic-gate 			usage(av[0]);
1577c478bd9Sstevel@tonic-gate 			exit(1);
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (optind >= ac) {
1617c478bd9Sstevel@tonic-gate 		usage(av[0]);
1627c478bd9Sstevel@tonic-gate 		exit(1);
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	while (optind < ac) {
1667c478bd9Sstevel@tonic-gate 		char *path = av[optind++];
1677c478bd9Sstevel@tonic-gate 		printer_description_t info;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 		rc = get_printer_description(path, &info);
1707c478bd9Sstevel@tonic-gate 		if (rc == 0) {
1717c478bd9Sstevel@tonic-gate 			printf("%s:\n", path);
1727c478bd9Sstevel@tonic-gate 			if (manufacturer != 0)
1737c478bd9Sstevel@tonic-gate 				printf("\tManufacturer: %s\n",
1747c478bd9Sstevel@tonic-gate 						NP(info.manufacturer));
1757c478bd9Sstevel@tonic-gate 			if (model != 0)
1767c478bd9Sstevel@tonic-gate 				printf("\tModel:        %s\n",
1777c478bd9Sstevel@tonic-gate 						NP(info.model));
1787c478bd9Sstevel@tonic-gate 			if (description != 0)
1797c478bd9Sstevel@tonic-gate 				printf("\tDescription:  %s\n",
1807c478bd9Sstevel@tonic-gate 						NP(info.description));
1817c478bd9Sstevel@tonic-gate 			if (class != 0)
1827c478bd9Sstevel@tonic-gate 				printf("\tClass:        %s\n",
1837c478bd9Sstevel@tonic-gate 						NP(info.class));
1847c478bd9Sstevel@tonic-gate 			if (command_set != 0) {
185*b11ac39fSjacobs 				int i;
186*b11ac39fSjacobs 
1877c478bd9Sstevel@tonic-gate 				printf("\tCommand set:\n");
188*b11ac39fSjacobs 				for (i = 0; info.command_set[i] != NULL; i++)
1897c478bd9Sstevel@tonic-gate 					printf("\t\tcmd[%d]: %s\n", i,
190*b11ac39fSjacobs 						info.command_set[i]);
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 		} else
1937c478bd9Sstevel@tonic-gate 			perror(path);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	return (rc);
1967c478bd9Sstevel@tonic-gate }
197