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