xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-misc.c (revision 85f4cb87104c72587029a6e0f1663332c85ba118)
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 /*LINTLIBRARY*/
27 
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 
32 #include <papi_impl.h>
33 
34 
35 papi_status_t
36 papiAttributeListAddLPString(papi_attribute_t ***list, int flags, char *name,
37 		char *value)
38 {
39 	papi_status_t result = PAPI_BAD_ARGUMENT;
40 
41 	if ((list != NULL) && (name != NULL) && (value != NULL) &&
42 	    (value[0] != '\0'))
43 		result = papiAttributeListAddString(list, flags, name, value);
44 	return (result);
45 }
46 
47 papi_status_t
48 papiAttributeListAddLPStrings(papi_attribute_t ***list, int flags, char *name,
49 				char **values)
50 {
51 	papi_status_t result = PAPI_OK;
52 	int i, flgs = flags;
53 
54 	if ((list == NULL) || (name == NULL) || (values == NULL))
55 		result = PAPI_BAD_ARGUMENT;
56 
57 	for (i = 0; ((result == PAPI_OK) && (values[i] != NULL));
58 		i++, flgs = PAPI_ATTR_APPEND)
59 		result = papiAttributeListAddString(list, flgs, name,
60 							values[i]);
61 
62 	return (result);
63 }
64 
65 void
66 papiAttributeListGetLPString(papi_attribute_t **attributes, char *key,
67 				char **string)
68 {
69 	char *value = NULL;
70 
71 	papiAttributeListGetString(attributes, NULL, key,  &value);
72 	if (value != NULL) {
73 		if (*string != NULL)
74 			free(*string);
75 		*string = strdup(value);
76 	}
77 }
78 
79 void
80 papiAttributeListGetLPStrings(papi_attribute_t **attributes, char *key,
81 				char ***strings)
82 {
83 	papi_status_t status;
84 	char **values = NULL;
85 	char *value = NULL;
86 	void *iter = NULL;
87 
88 	for (status = papiAttributeListGetString(attributes, &iter,
89 				key, &value);
90 	    status == PAPI_OK;
91 	    status = papiAttributeListGetString(attributes, &iter,
92 				NULL, &value))
93 		addlist(&values, value);
94 
95 	if (values != NULL) {
96 		if (*strings != NULL)
97 			freelist(*strings);
98 		*strings = values;
99 	}
100 }
101 
102 char *
103 printer_name_from_uri_id(char *uri, int32_t id)
104 {
105 	REQUEST *request = NULL;
106 	char *result = "";
107 
108 	if (uri != NULL) {
109 		if ((result = strrchr(uri, '/')) != NULL) {
110 			result += 1;
111 		} else
112 			result = (char *)uri;
113 
114 		if ((strcmp(result, "jobs") == 0) ||
115 		    (strcmp(result, "any") == 0) ||
116 		    (strcmp(result, "all") == 0))
117 			result = "";
118 	}
119 
120 	if ((result[0] == '\0') && (id != -1)) {
121 		char path[32];
122 
123 		snprintf(path, sizeof (path), "%d-0", id);
124 		if ((request = getrequest(path)) != NULL)
125 			result = request->destination;
126 	}
127 
128 	result = strdup(result);
129 
130 	if (request != NULL)
131 		freerequest(request);
132 
133 	return (result);
134 }
135 
136 /*
137  * LP content type <-> MIME type conversion table. (order dependent)
138  */
139 static struct {
140 	char *mime_type;
141 	char *lp_type;
142 } type_map[] = {
143 	{ "text/plain", "simple" },
144 	{ "application/octet-stream", "raw" },
145 	{ "application/octet-stream", "any" },
146 	{ "application/postscript", "postscript" },
147 	{ "application/postscript", "ps" },
148 	{ "application/x-cif", "cif" },
149 	{ "application/x-dvi", "dvi" },
150 	{ "application/x-plot", "plot" },
151 	{ "application/x-ditroff", "troff" },
152 	{ "application/x-troff", "otroff" },
153 	{ "application/x-pr", "pr" },
154 	{ "application/x-fortran", "fortran" },
155 	{ "application/x-raster", "raster" },
156 	{ NULL, NULL}
157 };
158 
159 char *
160 mime_type_to_lp_type(char *mime_type)
161 {
162 	int i;
163 
164 	if (mime_type == NULL)
165 		return ("simple");
166 
167 	for (i = 0; type_map[i].mime_type != NULL; i++)
168 		if (strcasecmp(type_map[i].mime_type, mime_type) == 0)
169 			return (type_map[i].lp_type);
170 
171 	return (mime_type);
172 }
173 
174 char *
175 lp_type_to_mime_type(char *lp_type)
176 {
177 	int i;
178 
179 	if (lp_type == NULL)
180 		return ("text/plain");
181 
182 	for (i = 0; type_map[i].lp_type != NULL; i++)
183 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
184 			return (type_map[i].mime_type);
185 
186 	return (lp_type);
187 }
188