xref: /illumos-gate/usr/src/cmd/lp/lib/papi/ppd.c (revision 355b4669)
1*355b4669Sjacobs /*
2*355b4669Sjacobs  * CDDL HEADER START
3*355b4669Sjacobs  *
4*355b4669Sjacobs  * The contents of this file are subject to the terms of the
5*355b4669Sjacobs  * Common Development and Distribution License (the "License").
6*355b4669Sjacobs  * You may not use this file except in compliance with the License.
7*355b4669Sjacobs  *
8*355b4669Sjacobs  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*355b4669Sjacobs  * or http://www.opensolaris.org/os/licensing.
10*355b4669Sjacobs  * See the License for the specific language governing permissions
11*355b4669Sjacobs  * and limitations under the License.
12*355b4669Sjacobs  *
13*355b4669Sjacobs  * When distributing Covered Code, include this CDDL HEADER in each
14*355b4669Sjacobs  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*355b4669Sjacobs  * If applicable, add the following below this CDDL HEADER, with the
16*355b4669Sjacobs  * fields enclosed by brackets "[]" replaced with your own identifying
17*355b4669Sjacobs  * information: Portions Copyright [yyyy] [name of copyright owner]
18*355b4669Sjacobs  *
19*355b4669Sjacobs  * CDDL HEADER END
20*355b4669Sjacobs  */
21*355b4669Sjacobs /*
22*355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*355b4669Sjacobs  * Use is subject to license terms.
24*355b4669Sjacobs  */
25*355b4669Sjacobs 
26*355b4669Sjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*355b4669Sjacobs 
28*355b4669Sjacobs /*
29*355b4669Sjacobs  * This file contains an extremely rudimentary implementation of PPD file
30*355b4669Sjacobs  * parsing support.  The parsing done here converts the contents of a PPD
31*355b4669Sjacobs  * file into a set of PAPI attributes that applications can use to build
32*355b4669Sjacobs  * print panels.
33*355b4669Sjacobs  */
34*355b4669Sjacobs 
35*355b4669Sjacobs #include <stdio.h>
36*355b4669Sjacobs #include <ctype.h>
37*355b4669Sjacobs #include <string.h>
38*355b4669Sjacobs #include <papi.h>
39*355b4669Sjacobs 
40*355b4669Sjacobs static void
41*355b4669Sjacobs process_line(char *line, char **key, char **value, char **comment)
42*355b4669Sjacobs {
43*355b4669Sjacobs 	char *ptr, *ptr2;
44*355b4669Sjacobs 
45*355b4669Sjacobs 	*key = &line[1];
46*355b4669Sjacobs 	*value = NULL;
47*355b4669Sjacobs 	*comment = NULL;
48*355b4669Sjacobs 
49*355b4669Sjacobs 	if ((ptr = strchr(line, ':')) == NULL)
50*355b4669Sjacobs 		return;
51*355b4669Sjacobs 
52*355b4669Sjacobs 	/*
53*355b4669Sjacobs 	 * line is in the form:
54*355b4669Sjacobs 	 *    *key: value/comment
55*355b4669Sjacobs 	 * or
56*355b4669Sjacobs 	 *    *key value/comment: data
57*355b4669Sjacobs 	 */
58*355b4669Sjacobs 	*ptr++ = NULL;
59*355b4669Sjacobs 	while (isspace(*ptr) != 0)
60*355b4669Sjacobs 		ptr++;
61*355b4669Sjacobs 
62*355b4669Sjacobs 	if ((ptr2 = strchr(line, ' ')) != NULL) {
63*355b4669Sjacobs 		ptr = ptr2;
64*355b4669Sjacobs 		/*
65*355b4669Sjacobs 		 * line is in the form:
66*355b4669Sjacobs 		 *    *key value/comment: data
67*355b4669Sjacobs 		 */
68*355b4669Sjacobs 		*ptr++ = NULL;
69*355b4669Sjacobs 		while (*ptr == ' ')
70*355b4669Sjacobs 			ptr++;
71*355b4669Sjacobs 	}
72*355b4669Sjacobs 
73*355b4669Sjacobs 	if (*ptr == '*')
74*355b4669Sjacobs 		ptr++;
75*355b4669Sjacobs 
76*355b4669Sjacobs 	*value = ptr;
77*355b4669Sjacobs 
78*355b4669Sjacobs 	if ((ptr = strchr(ptr, '/')) != NULL) {
79*355b4669Sjacobs 		*ptr++ = NULL;
80*355b4669Sjacobs 		*comment = ptr;
81*355b4669Sjacobs 	}
82*355b4669Sjacobs }
83*355b4669Sjacobs 
84*355b4669Sjacobs papi_status_t
85*355b4669Sjacobs PPDFileToAttributesList(papi_attribute_t ***attributes, char *filename)
86*355b4669Sjacobs {
87*355b4669Sjacobs 	papi_status_t status = PAPI_OK;
88*355b4669Sjacobs 	FILE *fp;
89*355b4669Sjacobs 	char line[256];
90*355b4669Sjacobs 	char capability[256];
91*355b4669Sjacobs 	char def[256];
92*355b4669Sjacobs 	char supported[256];
93*355b4669Sjacobs 	char *current_group_name = NULL;
94*355b4669Sjacobs 
95*355b4669Sjacobs 	int ui = 0;
96*355b4669Sjacobs 
97*355b4669Sjacobs 	if ((fp = fopen(filename, "r")) == NULL)
98*355b4669Sjacobs 		return (PAPI_NOT_POSSIBLE);
99*355b4669Sjacobs 
100*355b4669Sjacobs 	while ((status == PAPI_OK) &&
101*355b4669Sjacobs 			(fgets(line, sizeof (line), fp) != NULL)) {
102*355b4669Sjacobs 		char *key = NULL, *value = NULL, *text = NULL;
103*355b4669Sjacobs 
104*355b4669Sjacobs 		/* we want *key...: "value" */
105*355b4669Sjacobs 		if (line[0] != '*')
106*355b4669Sjacobs 			continue;
107*355b4669Sjacobs 
108*355b4669Sjacobs 		if (strchr(line, ':') == NULL)
109*355b4669Sjacobs 			continue;
110*355b4669Sjacobs 
111*355b4669Sjacobs 		if ((text = strrchr(line, '\n')) != NULL)
112*355b4669Sjacobs 			*text = NULL;
113*355b4669Sjacobs 
114*355b4669Sjacobs 		process_line(line, &key, &value, &text);
115*355b4669Sjacobs 
116*355b4669Sjacobs 		if ((strcasecmp(key, "PageSize") == 0) ||
117*355b4669Sjacobs 		    (strcasecmp(key, "InputSlot") == 0))
118*355b4669Sjacobs 			key = "media";
119*355b4669Sjacobs 
120*355b4669Sjacobs 		if (strcasecmp(key, "OpenGroup") == 0) {
121*355b4669Sjacobs 			if (value == NULL)
122*355b4669Sjacobs 				value = "unknown";
123*355b4669Sjacobs 			current_group_name = strdup(value);
124*355b4669Sjacobs 		} else if (strcasecmp(key, "OpenUI") == 0) {
125*355b4669Sjacobs 			if ((strcasecmp(value, "PageSize") == 0) ||
126*355b4669Sjacobs 			    (strcasecmp(value, "InputSlot") == 0))
127*355b4669Sjacobs 				value = "media";
128*355b4669Sjacobs 			snprintf(capability, sizeof (capability), "%s", value);
129*355b4669Sjacobs 			snprintf(def, sizeof (def),
130*355b4669Sjacobs 					"%s-default", value);
131*355b4669Sjacobs 			snprintf(supported, sizeof (supported),
132*355b4669Sjacobs 					"%s-supported", value);
133*355b4669Sjacobs 			ui = 1;
134*355b4669Sjacobs 		} else if (strcasecmp(key, "CloseGroup") == 0) {
135*355b4669Sjacobs 			/* do nothing */
136*355b4669Sjacobs 		} else if (strcasecmp(key, "CloseUI") == 0) {
137*355b4669Sjacobs 			ui = 0;
138*355b4669Sjacobs 			/* do nothing */
139*355b4669Sjacobs 		} else if (strcasecmp(key, "Manufacturer") == 0) {
140*355b4669Sjacobs 			status = papiAttributeListAddString(attributes,
141*355b4669Sjacobs 					PAPI_ATTR_EXCL,
142*355b4669Sjacobs 					"printer-make", value);
143*355b4669Sjacobs 		} else if (strcasecmp(key, "ModelName") == 0) {
144*355b4669Sjacobs 			status = papiAttributeListAddString(attributes,
145*355b4669Sjacobs 					PAPI_ATTR_EXCL,
146*355b4669Sjacobs 					"printer-model", value);
147*355b4669Sjacobs 		} else if (strcasecmp(key, "ShortNickName") == 0) {
148*355b4669Sjacobs 			status = papiAttributeListAddString(attributes,
149*355b4669Sjacobs 					PAPI_ATTR_EXCL,
150*355b4669Sjacobs 					"printer-make-and-model", value);
151*355b4669Sjacobs 		} else if ((strncasecmp(key, "Default", 7) == 0) && ui) {
152*355b4669Sjacobs 			status = papiAttributeListAddString(attributes,
153*355b4669Sjacobs 					PAPI_ATTR_EXCL,
154*355b4669Sjacobs 					def, value);
155*355b4669Sjacobs 		} else if ((strcasecmp(key, capability) == 0) && ui) {
156*355b4669Sjacobs 			status = papiAttributeListAddString(attributes,
157*355b4669Sjacobs 					PAPI_ATTR_APPEND,
158*355b4669Sjacobs 					supported, value);
159*355b4669Sjacobs 		}
160*355b4669Sjacobs 	}
161*355b4669Sjacobs 	fclose(fp);
162*355b4669Sjacobs 
163*355b4669Sjacobs 	return (status);
164*355b4669Sjacobs }
165