xref: /illumos-gate/usr/src/cmd/hal/utils/printer.c (revision defc4c8acfa01dba1ef3c13ca0cafccfcede51c0)
1 /*
2  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  *
5  * Licensed under the Academic Free License version 2.1
6  */
7 
8 #pragma ident	"%Z%%M%	%I%	%E% SMI"
9 
10 #ifdef HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13 
14 #include <errno.h>
15 #include <strings.h>
16 #include <ctype.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include <ctype.h>
21 
22 #include <libhal.h>
23 #include <logger.h>
24 
25 #include "printer.h"
26 
27 static char *
28 strip_ws(char *s)
29 {
30 	if (s != NULL) {
31 		char *p;
32 
33 		/* skip the leading whitespace */
34 		for (; ((*s != NULL) && (isspace(*s) != 0)); s++);
35 
36 		/* drop the trailing whitespace */
37 		for (p = s + strlen(s) - 1; ((p > s) && (isspace(*p) != 0));
38 		     p--);
39 		*(++p) = '\0';
40 	}
41 
42 	return (s);
43 }
44 
45 int
46 ieee1284_devid_to_printer_info(char *devid_string, char **manufacturer,
47 		char **model, char **description, char **class,
48 		char **serial_no, char ***command_set)
49 {
50 	char *iter = NULL;
51 	char *s;
52 
53 	if (devid_string == NULL)
54 		return (-1);
55 
56 	/* parse the 1284 device id string */
57 	for (s = (char *)strtok_r(devid_string, ";\n", &iter); s != NULL;
58 			s = (char *)strtok_r(NULL, ";\n", &iter)) {
59 		char *t, *u, *iter2 = NULL;
60 
61 		if ((t = (char *)strtok_r(s, ":\n", &iter2)) == NULL)
62 			continue;
63 
64 		if ((u = (char *)strtok_r(NULL, ":\n", &iter2)) == NULL)
65 			continue;
66 
67 		if (((strcasecmp(t, "MFG") == 0) ||
68 		     (strcasecmp(t, "MANUFACTURER") == 0)) &&
69 		    (manufacturer != NULL))
70 				*manufacturer = strdup(strip_ws(u));
71 		else if (((strcasecmp(t, "MDL") == 0) ||
72 			  (strcasecmp(t, "MODEL") == 0)) &&
73 			 (model != NULL))
74 				*model = strdup(strip_ws(u));
75 		else if (((strcasecmp(t, "DES") == 0) ||
76 			  (strcasecmp(t, "DESCRIPTION") == 0)) &&
77 			 (description != NULL))
78 				*description = strdup(strip_ws(u));
79 		else if (((strcasecmp(t, "CLS") == 0) ||
80 			  (strcasecmp(t, "CLASS") == 0)) &&
81 			 (class != NULL))
82 				*class = strdup(strip_ws(u));
83 		else if (((strcasecmp(t, "SER") == 0) ||
84 			  (strcasecmp(t, "SERNO") == 0)) &&
85 			 (serial_no != NULL))
86 				*serial_no = strdup(strip_ws(u));
87 		else if (((strcasecmp(t, "CMD") == 0) ||
88 			  (strcasecmp(t, "COMMAND SET") == 0)) &&
89 			 (command_set != NULL)) {
90 			/* this should be more dynamic, I got lazy */
91 			char *v, *iter3 = NULL;
92 			char *cmds[32];
93 			int i = 0;
94 
95 			memset(&cmds, 0, sizeof (cmds));
96 #define	NELEM(a)	(sizeof (a) / sizeof (*(a)))
97 			for (v = strtok_r(u, ",\n", &iter3);
98 			     ((v != NULL) && (i < NELEM(cmds)));
99 			     v = strtok_r(NULL, ",\n", &iter3)) {
100 				cmds[i++] = strdup(strip_ws(v));
101 			}
102 #undef NELEM
103 			*command_set = calloc(++i, sizeof (char *));
104 			for (i = 0; (cmds)[i] != NULL; i++)
105 				(*command_set)[i] = cmds[i];
106 		}
107 	}
108 
109 	return (0);
110 }
111 
112 
113 int
114 add_printer_info(LibHalChangeSet *cs, char *udi, char *manufacturer,
115 		char *model, char *description, char *serial_number,
116 		char **command_set, char *device)
117 {
118 #define	NP(x)   (x?x:"")
119 	HAL_DEBUG(("udi: %s, snmp data: vendor=%s, product=%s, "
120 		    "description=%s, serial=%s, device=%s\n",
121 		    NP(udi), NP(manufacturer), NP(model), NP(description),
122 		    NP(serial_number), NP(device)));
123 #undef NP
124 
125 	if (model != NULL)
126 		libhal_changeset_set_property_string(cs,
127 					"info.product", model);
128 	if (manufacturer != NULL)
129 		libhal_changeset_set_property_string(cs,
130 					"printer.vendor", manufacturer);
131 	if (model != NULL)
132 		libhal_changeset_set_property_string(cs,
133 					"printer.product", model);
134 	if (serial_number != NULL)
135 		libhal_changeset_set_property_string(cs,
136 					"printer.serial", serial_number);
137 	if (description != NULL)
138 		libhal_changeset_set_property_string(cs,
139 					"printer.description", description);
140 	if (command_set != NULL)
141 		libhal_changeset_set_property_strlist(cs, "printer.commandset",
142 					(const char **)command_set);
143 	if (device != NULL)
144 		libhal_changeset_set_property_string(cs,
145 					"printer.device", device);
146 
147 	return (0);
148 }
149