xref: /illumos-gate/usr/src/cmd/lp/lib/papi/lpsched-misc.c (revision e4fb8a5f)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * 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.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22355b4669Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <stdio.h>
297c478bd9Sstevel@tonic-gate #include <string.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <papi_impl.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate papi_status_t
papiAttributeListAddLPString(papi_attribute_t *** list,int flags,char * name,char * value)36355b4669Sjacobs papiAttributeListAddLPString(papi_attribute_t ***list, int flags, char *name,
37355b4669Sjacobs 		char *value)
387c478bd9Sstevel@tonic-gate {
397c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_BAD_ARGUMENT;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	if ((list != NULL) && (name != NULL) && (value != NULL) &&
42*e4fb8a5fSToomas Soome 	    (value[0] != '\0'))
437c478bd9Sstevel@tonic-gate 		result = papiAttributeListAddString(list, flags, name, value);
447c478bd9Sstevel@tonic-gate 	return (result);
457c478bd9Sstevel@tonic-gate }
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate papi_status_t
papiAttributeListAddLPStrings(papi_attribute_t *** list,int flags,char * name,char ** values)48355b4669Sjacobs papiAttributeListAddLPStrings(papi_attribute_t ***list, int flags, char *name,
497c478bd9Sstevel@tonic-gate 				char **values)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
527c478bd9Sstevel@tonic-gate 	int i, flgs = flags;
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate 	if ((list == NULL) || (name == NULL) || (values == NULL))
557c478bd9Sstevel@tonic-gate 		result = PAPI_BAD_ARGUMENT;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	for (i = 0; ((result == PAPI_OK) && (values[i] != NULL));
587c478bd9Sstevel@tonic-gate 		i++, flgs = PAPI_ATTR_APPEND)
597c478bd9Sstevel@tonic-gate 		result = papiAttributeListAddString(list, flgs, name,
607c478bd9Sstevel@tonic-gate 							values[i]);
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	return (result);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
65355b4669Sjacobs void
papiAttributeListGetLPString(papi_attribute_t ** attributes,char * key,char ** string)66355b4669Sjacobs papiAttributeListGetLPString(papi_attribute_t **attributes, char *key,
67355b4669Sjacobs 				char **string)
68355b4669Sjacobs {
69355b4669Sjacobs 	char *value = NULL;
70355b4669Sjacobs 
71355b4669Sjacobs 	papiAttributeListGetString(attributes, NULL, key,  &value);
72355b4669Sjacobs 	if (value != NULL) {
73355b4669Sjacobs 		if (*string != NULL)
74355b4669Sjacobs 			free(*string);
75355b4669Sjacobs 		*string = strdup(value);
76355b4669Sjacobs 	}
77355b4669Sjacobs }
78355b4669Sjacobs 
79355b4669Sjacobs void
papiAttributeListGetLPStrings(papi_attribute_t ** attributes,char * key,char *** strings)80355b4669Sjacobs papiAttributeListGetLPStrings(papi_attribute_t **attributes, char *key,
81355b4669Sjacobs 				char ***strings)
82355b4669Sjacobs {
83355b4669Sjacobs 	papi_status_t status;
84355b4669Sjacobs 	char **values = NULL;
85355b4669Sjacobs 	char *value = NULL;
86355b4669Sjacobs 	void *iter = NULL;
87355b4669Sjacobs 
88355b4669Sjacobs 	for (status = papiAttributeListGetString(attributes, &iter,
89355b4669Sjacobs 				key, &value);
90355b4669Sjacobs 	    status == PAPI_OK;
91355b4669Sjacobs 	    status = papiAttributeListGetString(attributes, &iter,
92355b4669Sjacobs 				NULL, &value))
93355b4669Sjacobs 		addlist(&values, value);
94355b4669Sjacobs 
95355b4669Sjacobs 	if (values != NULL) {
96355b4669Sjacobs 		if (*strings != NULL)
97355b4669Sjacobs 			freelist(*strings);
98355b4669Sjacobs 		*strings = values;
99355b4669Sjacobs 	}
100355b4669Sjacobs }
101355b4669Sjacobs 
1027c478bd9Sstevel@tonic-gate char *
printer_name_from_uri_id(char * uri,int32_t id)103355b4669Sjacobs printer_name_from_uri_id(char *uri, int32_t id)
1047c478bd9Sstevel@tonic-gate {
1057c478bd9Sstevel@tonic-gate 	REQUEST *request = NULL;
1067c478bd9Sstevel@tonic-gate 	char *result = "";
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	if (uri != NULL) {
1097c478bd9Sstevel@tonic-gate 		if ((result = strrchr(uri, '/')) != NULL) {
1107c478bd9Sstevel@tonic-gate 			result += 1;
1117c478bd9Sstevel@tonic-gate 		} else
1127c478bd9Sstevel@tonic-gate 			result = (char *)uri;
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 		if ((strcmp(result, "jobs") == 0) ||
1157c478bd9Sstevel@tonic-gate 		    (strcmp(result, "any") == 0) ||
1167c478bd9Sstevel@tonic-gate 		    (strcmp(result, "all") == 0))
1177c478bd9Sstevel@tonic-gate 			result = "";
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 
120*e4fb8a5fSToomas Soome 	if ((result[0] == '\0') && (id != -1)) {
1217c478bd9Sstevel@tonic-gate 		char path[32];
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		snprintf(path, sizeof (path), "%d-0", id);
1247c478bd9Sstevel@tonic-gate 		if ((request = getrequest(path)) != NULL)
1257c478bd9Sstevel@tonic-gate 			result = request->destination;
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	result = strdup(result);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	if (request != NULL)
1317c478bd9Sstevel@tonic-gate 		freerequest(request);
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	return (result);
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * LP content type <-> MIME type conversion table. (order dependent)
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static struct {
1407c478bd9Sstevel@tonic-gate 	char *mime_type;
1417c478bd9Sstevel@tonic-gate 	char *lp_type;
1427c478bd9Sstevel@tonic-gate } type_map[] = {
14339a8b5b3Sjacobs 	{ "text/plain", "simple" },
1447c478bd9Sstevel@tonic-gate 	{ "application/octet-stream", "raw" },
1457c478bd9Sstevel@tonic-gate 	{ "application/octet-stream", "any" },
1467c478bd9Sstevel@tonic-gate 	{ "application/postscript", "postscript" },
1477c478bd9Sstevel@tonic-gate 	{ "application/postscript", "ps" },
1487c478bd9Sstevel@tonic-gate 	{ "application/x-cif", "cif" },
1497c478bd9Sstevel@tonic-gate 	{ "application/x-dvi", "dvi" },
1507c478bd9Sstevel@tonic-gate 	{ "application/x-plot", "plot" },
1517c478bd9Sstevel@tonic-gate 	{ "application/x-ditroff", "troff" },
1527c478bd9Sstevel@tonic-gate 	{ "application/x-troff", "otroff" },
1537c478bd9Sstevel@tonic-gate 	{ "application/x-pr", "pr" },
1547c478bd9Sstevel@tonic-gate 	{ "application/x-fortran", "fortran" },
1557c478bd9Sstevel@tonic-gate 	{ "application/x-raster", "raster" },
1567c478bd9Sstevel@tonic-gate 	{ NULL, NULL}
1577c478bd9Sstevel@tonic-gate };
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate char *
mime_type_to_lp_type(char * mime_type)1607c478bd9Sstevel@tonic-gate mime_type_to_lp_type(char *mime_type)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	int i;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (mime_type == NULL)
1657c478bd9Sstevel@tonic-gate 		return ("simple");
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	for (i = 0; type_map[i].mime_type != NULL; i++)
1687c478bd9Sstevel@tonic-gate 		if (strcasecmp(type_map[i].mime_type, mime_type) == 0)
1697c478bd9Sstevel@tonic-gate 			return (type_map[i].lp_type);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	return (mime_type);
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate char *
lp_type_to_mime_type(char * lp_type)1757c478bd9Sstevel@tonic-gate lp_type_to_mime_type(char *lp_type)
1767c478bd9Sstevel@tonic-gate {
1777c478bd9Sstevel@tonic-gate 	int i;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	if (lp_type == NULL)
18039a8b5b3Sjacobs 		return ("text/plain");
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	for (i = 0; type_map[i].lp_type != NULL; i++)
1837c478bd9Sstevel@tonic-gate 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1847c478bd9Sstevel@tonic-gate 			return (type_map[i].mime_type);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (lp_type);
1877c478bd9Sstevel@tonic-gate }
188