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