xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-misc.c (revision 39a8b5b32a0cae36489b82cb7b22348736870f03)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*LINTLIBRARY*/
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
33 
34 #include <papi_impl.h>
35 
36 
37 papi_status_t
38 papiAttributeListAddLPString(papi_attribute_t ***list, int flags, char *name,
39 		char *value)
40 {
41 	papi_status_t result = PAPI_BAD_ARGUMENT;
42 
43 	if ((list != NULL) && (name != NULL) && (value != NULL) &&
44 	    (value[0] != NULL))
45 		result = papiAttributeListAddString(list, flags, name, value);
46 	return (result);
47 }
48 
49 papi_status_t
50 papiAttributeListAddLPStrings(papi_attribute_t ***list, int flags, char *name,
51 				char **values)
52 {
53 	papi_status_t result = PAPI_OK;
54 	int i, flgs = flags;
55 
56 	if ((list == NULL) || (name == NULL) || (values == NULL))
57 		result = PAPI_BAD_ARGUMENT;
58 
59 	for (i = 0; ((result == PAPI_OK) && (values[i] != NULL));
60 		i++, flgs = PAPI_ATTR_APPEND)
61 		result = papiAttributeListAddString(list, flgs, name,
62 							values[i]);
63 
64 	return (result);
65 }
66 
67 void
68 papiAttributeListGetLPString(papi_attribute_t **attributes, char *key,
69 				char **string)
70 {
71 	char *value = NULL;
72 
73 	papiAttributeListGetString(attributes, NULL, key,  &value);
74 	if (value != NULL) {
75 		if (*string != NULL)
76 			free(*string);
77 		*string = strdup(value);
78 	}
79 }
80 
81 void
82 papiAttributeListGetLPStrings(papi_attribute_t **attributes, char *key,
83 				char ***strings)
84 {
85 	papi_status_t status;
86 	char **values = NULL;
87 	char *value = NULL;
88 	void *iter = NULL;
89 
90 	for (status = papiAttributeListGetString(attributes, &iter,
91 				key, &value);
92 	    status == PAPI_OK;
93 	    status = papiAttributeListGetString(attributes, &iter,
94 				NULL, &value))
95 		addlist(&values, value);
96 
97 	if (values != NULL) {
98 		if (*strings != NULL)
99 			freelist(*strings);
100 		*strings = values;
101 	}
102 }
103 
104 char *
105 printer_name_from_uri_id(char *uri, int32_t id)
106 {
107 	REQUEST *request = NULL;
108 	char *result = "";
109 
110 	if (uri != NULL) {
111 		if ((result = strrchr(uri, '/')) != NULL) {
112 			result += 1;
113 		} else
114 			result = (char *)uri;
115 
116 		if ((strcmp(result, "jobs") == 0) ||
117 		    (strcmp(result, "any") == 0) ||
118 		    (strcmp(result, "all") == 0))
119 			result = "";
120 	}
121 
122 	if ((result[0] == NULL) && (id != -1)) {
123 		char path[32];
124 
125 		snprintf(path, sizeof (path), "%d-0", id);
126 		if ((request = getrequest(path)) != NULL)
127 			result = request->destination;
128 	}
129 
130 	result = strdup(result);
131 
132 	if (request != NULL)
133 		freerequest(request);
134 
135 	return (result);
136 }
137 
138 /*
139  * LP content type <-> MIME type conversion table. (order dependent)
140  */
141 static struct {
142 	char *mime_type;
143 	char *lp_type;
144 } type_map[] = {
145 	{ "text/plain", "simple" },
146 	{ "application/octet-stream", "raw" },
147 	{ "application/octet-stream", "any" },
148 	{ "application/postscript", "postscript" },
149 	{ "application/postscript", "ps" },
150 	{ "application/x-cif", "cif" },
151 	{ "application/x-dvi", "dvi" },
152 	{ "application/x-plot", "plot" },
153 	{ "application/x-ditroff", "troff" },
154 	{ "application/x-troff", "otroff" },
155 	{ "application/x-pr", "pr" },
156 	{ "application/x-fortran", "fortran" },
157 	{ "application/x-raster", "raster" },
158 	{ NULL, NULL}
159 };
160 
161 char *
162 mime_type_to_lp_type(char *mime_type)
163 {
164 	int i;
165 
166 	if (mime_type == NULL)
167 		return ("simple");
168 
169 	for (i = 0; type_map[i].mime_type != NULL; i++)
170 		if (strcasecmp(type_map[i].mime_type, mime_type) == 0)
171 			return (type_map[i].lp_type);
172 
173 	return (mime_type);
174 }
175 
176 char *
177 lp_type_to_mime_type(char *lp_type)
178 {
179 	int i;
180 
181 	if (lp_type == NULL)
182 		return ("text/plain");
183 
184 	for (i = 0; type_map[i].lp_type != NULL; i++)
185 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
186 			return (type_map[i].mime_type);
187 
188 	return (lp_type);
189 }
190