xref: /titanic_44/usr/src/cmd/lp/lib/papi/lpsched-misc.c (revision ea8dc4b6d2251b437950c0056bc626b311c73c27)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*LINTLIBRARY*/
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 
35 #include <papi_impl.h>
36 
37 
38 papi_status_t
39 addLPString(papi_attribute_t ***list, int flags, char *name, 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 addLPStrings(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 char *
68 printer_name_from_uri_id(const char *uri, int32_t id)
69 {
70 	REQUEST *request = NULL;
71 	char *result = "";
72 
73 	if (uri != NULL) {
74 		if ((result = strrchr(uri, '/')) != NULL) {
75 			result += 1;
76 		} else
77 			result = (char *)uri;
78 
79 		if ((strcmp(result, "jobs") == 0) ||
80 		    (strcmp(result, "any") == 0) ||
81 		    (strcmp(result, "all") == 0))
82 			result = "";
83 	}
84 
85 	if ((result[0] == NULL) && (id != -1)) {
86 		char path[32];
87 
88 		snprintf(path, sizeof (path), "%d-0", id);
89 		if ((request = getrequest(path)) != NULL)
90 			result = request->destination;
91 	}
92 
93 	result = strdup(result);
94 
95 	if (request != NULL)
96 		freerequest(request);
97 
98 	return (result);
99 }
100 
101 /*
102  * LP content type <-> MIME type conversion table. (order dependent)
103  */
104 static struct {
105 	char *mime_type;
106 	char *lp_type;
107 } type_map[] = {
108 	{ "plain/text", "simple" },
109 	{ "application/octet-stream", "raw" },
110 	{ "application/octet-stream", "any" },
111 	{ "application/postscript", "postscript" },
112 	{ "application/postscript", "ps" },
113 	{ "application/x-cif", "cif" },
114 	{ "application/x-dvi", "dvi" },
115 	{ "application/x-plot", "plot" },
116 	{ "application/x-ditroff", "troff" },
117 	{ "application/x-troff", "otroff" },
118 	{ "application/x-pr", "pr" },
119 	{ "application/x-fortran", "fortran" },
120 	{ "application/x-raster", "raster" },
121 	{ NULL, NULL}
122 };
123 
124 char *
125 mime_type_to_lp_type(char *mime_type)
126 {
127 	int i;
128 
129 	if (mime_type == NULL)
130 		return ("simple");
131 
132 	for (i = 0; type_map[i].mime_type != NULL; i++)
133 		if (strcasecmp(type_map[i].mime_type, mime_type) == 0)
134 			return (type_map[i].lp_type);
135 
136 	return (mime_type);
137 }
138 
139 char *
140 lp_type_to_mime_type(char *lp_type)
141 {
142 	int i;
143 
144 	if (lp_type == NULL)
145 		return ("plain/text");
146 
147 	for (i = 0; type_map[i].lp_type != NULL; i++)
148 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
149 			return (type_map[i].mime_type);
150 
151 	return (lp_type);
152 }
153