1355b4669Sjacobs /*
2355b4669Sjacobs * CDDL HEADER START
3355b4669Sjacobs *
4355b4669Sjacobs * The contents of this file are subject to the terms of the
5355b4669Sjacobs * Common Development and Distribution License (the "License").
6355b4669Sjacobs * You may not use this file except in compliance with the License.
7355b4669Sjacobs *
8355b4669Sjacobs * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9355b4669Sjacobs * or http://www.opensolaris.org/os/licensing.
10355b4669Sjacobs * See the License for the specific language governing permissions
11355b4669Sjacobs * and limitations under the License.
12355b4669Sjacobs *
13355b4669Sjacobs * When distributing Covered Code, include this CDDL HEADER in each
14355b4669Sjacobs * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15355b4669Sjacobs * If applicable, add the following below this CDDL HEADER, with the
16355b4669Sjacobs * fields enclosed by brackets "[]" replaced with your own identifying
17355b4669Sjacobs * information: Portions Copyright [yyyy] [name of copyright owner]
18355b4669Sjacobs *
19355b4669Sjacobs * CDDL HEADER END
20355b4669Sjacobs */
21355b4669Sjacobs /*
22d7c57852SGary Mills * Copyright 2017 Gary Mills
23355b4669Sjacobs * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24355b4669Sjacobs * Use is subject to license terms.
25355b4669Sjacobs */
26355b4669Sjacobs
27355b4669Sjacobs /*
28355b4669Sjacobs * This file contains an extremely rudimentary implementation of PPD file
29355b4669Sjacobs * parsing support. The parsing done here converts the contents of a PPD
30355b4669Sjacobs * file into a set of PAPI attributes that applications can use to build
31355b4669Sjacobs * print panels.
32355b4669Sjacobs */
33355b4669Sjacobs
34355b4669Sjacobs #include <stdio.h>
35355b4669Sjacobs #include <ctype.h>
36355b4669Sjacobs #include <string.h>
37355b4669Sjacobs #include <papi.h>
38355b4669Sjacobs
39355b4669Sjacobs static void
process_line(char * line,char ** key,char ** value,char ** comment)40355b4669Sjacobs process_line(char *line, char **key, char **value, char **comment)
41355b4669Sjacobs {
42355b4669Sjacobs char *ptr, *ptr2;
43355b4669Sjacobs
44355b4669Sjacobs *key = &line[1];
45355b4669Sjacobs *value = NULL;
46355b4669Sjacobs *comment = NULL;
47355b4669Sjacobs
48355b4669Sjacobs if ((ptr = strchr(line, ':')) == NULL)
49355b4669Sjacobs return;
50355b4669Sjacobs
51355b4669Sjacobs /*
52355b4669Sjacobs * line is in the form:
53355b4669Sjacobs * *key: value/comment
54355b4669Sjacobs * or
55355b4669Sjacobs * *key value/comment: data
56355b4669Sjacobs */
57*e4fb8a5fSToomas Soome *ptr++ = '\0';
58355b4669Sjacobs while (isspace(*ptr) != 0)
59355b4669Sjacobs ptr++;
60355b4669Sjacobs
61355b4669Sjacobs if ((ptr2 = strchr(line, ' ')) != NULL) {
62355b4669Sjacobs ptr = ptr2;
63355b4669Sjacobs /*
64355b4669Sjacobs * line is in the form:
65355b4669Sjacobs * *key value/comment: data
66355b4669Sjacobs */
67*e4fb8a5fSToomas Soome *ptr++ = '\0';
68355b4669Sjacobs while (*ptr == ' ')
69355b4669Sjacobs ptr++;
70355b4669Sjacobs }
71355b4669Sjacobs
72355b4669Sjacobs if (*ptr == '*')
73355b4669Sjacobs ptr++;
74355b4669Sjacobs
75355b4669Sjacobs *value = ptr;
76355b4669Sjacobs
77355b4669Sjacobs if ((ptr = strchr(ptr, '/')) != NULL) {
78*e4fb8a5fSToomas Soome *ptr++ = '\0';
79355b4669Sjacobs *comment = ptr;
80355b4669Sjacobs }
81355b4669Sjacobs }
82355b4669Sjacobs
83355b4669Sjacobs papi_status_t
PPDFileToAttributesList(papi_attribute_t *** attributes,char * filename)84355b4669Sjacobs PPDFileToAttributesList(papi_attribute_t ***attributes, char *filename)
85355b4669Sjacobs {
86355b4669Sjacobs papi_status_t status = PAPI_OK;
87355b4669Sjacobs FILE *fp;
88355b4669Sjacobs char line[256];
89355b4669Sjacobs char capability[256];
90355b4669Sjacobs char def[256];
91355b4669Sjacobs char supported[256];
92355b4669Sjacobs
93355b4669Sjacobs int ui = 0;
94355b4669Sjacobs
95355b4669Sjacobs if ((fp = fopen(filename, "r")) == NULL)
96355b4669Sjacobs return (PAPI_NOT_POSSIBLE);
97355b4669Sjacobs
98355b4669Sjacobs while ((status == PAPI_OK) &&
99355b4669Sjacobs (fgets(line, sizeof (line), fp) != NULL)) {
100355b4669Sjacobs char *key = NULL, *value = NULL, *text = NULL;
101355b4669Sjacobs
102355b4669Sjacobs /* we want *key...: "value" */
103355b4669Sjacobs if (line[0] != '*')
104355b4669Sjacobs continue;
105355b4669Sjacobs
106355b4669Sjacobs if (strchr(line, ':') == NULL)
107355b4669Sjacobs continue;
108355b4669Sjacobs
109355b4669Sjacobs if ((text = strrchr(line, '\n')) != NULL)
110*e4fb8a5fSToomas Soome *text = '\0';
111355b4669Sjacobs
112355b4669Sjacobs process_line(line, &key, &value, &text);
113355b4669Sjacobs
114355b4669Sjacobs if ((strcasecmp(key, "PageSize") == 0) ||
115355b4669Sjacobs (strcasecmp(key, "InputSlot") == 0))
116355b4669Sjacobs key = "media";
117355b4669Sjacobs
118355b4669Sjacobs if (strcasecmp(key, "OpenGroup") == 0) {
119355b4669Sjacobs if (value == NULL)
120355b4669Sjacobs value = "unknown";
121355b4669Sjacobs } else if (strcasecmp(key, "OpenUI") == 0) {
122355b4669Sjacobs if ((strcasecmp(value, "PageSize") == 0) ||
123355b4669Sjacobs (strcasecmp(value, "InputSlot") == 0))
124355b4669Sjacobs value = "media";
125355b4669Sjacobs snprintf(capability, sizeof (capability), "%s", value);
126355b4669Sjacobs snprintf(def, sizeof (def),
127355b4669Sjacobs "%s-default", value);
128355b4669Sjacobs snprintf(supported, sizeof (supported),
129355b4669Sjacobs "%s-supported", value);
130355b4669Sjacobs ui = 1;
131355b4669Sjacobs } else if (strcasecmp(key, "CloseGroup") == 0) {
132355b4669Sjacobs /* do nothing */
133355b4669Sjacobs } else if (strcasecmp(key, "CloseUI") == 0) {
134355b4669Sjacobs ui = 0;
135355b4669Sjacobs /* do nothing */
136355b4669Sjacobs } else if (strcasecmp(key, "Manufacturer") == 0) {
137355b4669Sjacobs status = papiAttributeListAddString(attributes,
138355b4669Sjacobs PAPI_ATTR_EXCL,
139355b4669Sjacobs "printer-make", value);
140355b4669Sjacobs } else if (strcasecmp(key, "ModelName") == 0) {
141355b4669Sjacobs status = papiAttributeListAddString(attributes,
142355b4669Sjacobs PAPI_ATTR_EXCL,
143355b4669Sjacobs "printer-model", value);
144355b4669Sjacobs } else if (strcasecmp(key, "ShortNickName") == 0) {
145355b4669Sjacobs status = papiAttributeListAddString(attributes,
146355b4669Sjacobs PAPI_ATTR_EXCL,
147355b4669Sjacobs "printer-make-and-model", value);
148355b4669Sjacobs } else if ((strncasecmp(key, "Default", 7) == 0) && ui) {
149355b4669Sjacobs status = papiAttributeListAddString(attributes,
150355b4669Sjacobs PAPI_ATTR_EXCL,
151355b4669Sjacobs def, value);
152355b4669Sjacobs } else if ((strcasecmp(key, capability) == 0) && ui) {
153355b4669Sjacobs status = papiAttributeListAddString(attributes,
154355b4669Sjacobs PAPI_ATTR_APPEND,
155355b4669Sjacobs supported, value);
156355b4669Sjacobs }
157355b4669Sjacobs }
158355b4669Sjacobs fclose(fp);
159355b4669Sjacobs
160355b4669Sjacobs return (status);
161355b4669Sjacobs }
162