xref: /onnv-gate/usr/src/cmd/lp/lib/papi/lpsched-misc.c (revision 3135:286b0dd13054)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52264Sjacobs  * Common Development and Distribution License (the "License").
62264Sjacobs  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
222264Sjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*LINTLIBRARY*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <stdlib.h>
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <papi_impl.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate 
370Sstevel@tonic-gate papi_status_t
papiAttributeListAddLPString(papi_attribute_t *** list,int flags,char * name,char * value)382264Sjacobs papiAttributeListAddLPString(papi_attribute_t ***list, int flags, char *name,
392264Sjacobs 		char *value)
400Sstevel@tonic-gate {
410Sstevel@tonic-gate 	papi_status_t result = PAPI_BAD_ARGUMENT;
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 	if ((list != NULL) && (name != NULL) && (value != NULL) &&
440Sstevel@tonic-gate 	    (value[0] != NULL))
450Sstevel@tonic-gate 		result = papiAttributeListAddString(list, flags, name, value);
460Sstevel@tonic-gate 	return (result);
470Sstevel@tonic-gate }
480Sstevel@tonic-gate 
490Sstevel@tonic-gate papi_status_t
papiAttributeListAddLPStrings(papi_attribute_t *** list,int flags,char * name,char ** values)502264Sjacobs papiAttributeListAddLPStrings(papi_attribute_t ***list, int flags, char *name,
510Sstevel@tonic-gate 				char **values)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	papi_status_t result = PAPI_OK;
540Sstevel@tonic-gate 	int i, flgs = flags;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if ((list == NULL) || (name == NULL) || (values == NULL))
570Sstevel@tonic-gate 		result = PAPI_BAD_ARGUMENT;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	for (i = 0; ((result == PAPI_OK) && (values[i] != NULL));
600Sstevel@tonic-gate 		i++, flgs = PAPI_ATTR_APPEND)
610Sstevel@tonic-gate 		result = papiAttributeListAddString(list, flgs, name,
620Sstevel@tonic-gate 							values[i]);
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	return (result);
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
672264Sjacobs void
papiAttributeListGetLPString(papi_attribute_t ** attributes,char * key,char ** string)682264Sjacobs papiAttributeListGetLPString(papi_attribute_t **attributes, char *key,
692264Sjacobs 				char **string)
702264Sjacobs {
712264Sjacobs 	char *value = NULL;
722264Sjacobs 
732264Sjacobs 	papiAttributeListGetString(attributes, NULL, key,  &value);
742264Sjacobs 	if (value != NULL) {
752264Sjacobs 		if (*string != NULL)
762264Sjacobs 			free(*string);
772264Sjacobs 		*string = strdup(value);
782264Sjacobs 	}
792264Sjacobs }
802264Sjacobs 
812264Sjacobs void
papiAttributeListGetLPStrings(papi_attribute_t ** attributes,char * key,char *** strings)822264Sjacobs papiAttributeListGetLPStrings(papi_attribute_t **attributes, char *key,
832264Sjacobs 				char ***strings)
842264Sjacobs {
852264Sjacobs 	papi_status_t status;
862264Sjacobs 	char **values = NULL;
872264Sjacobs 	char *value = NULL;
882264Sjacobs 	void *iter = NULL;
892264Sjacobs 
902264Sjacobs 	for (status = papiAttributeListGetString(attributes, &iter,
912264Sjacobs 				key, &value);
922264Sjacobs 	    status == PAPI_OK;
932264Sjacobs 	    status = papiAttributeListGetString(attributes, &iter,
942264Sjacobs 				NULL, &value))
952264Sjacobs 		addlist(&values, value);
962264Sjacobs 
972264Sjacobs 	if (values != NULL) {
982264Sjacobs 		if (*strings != NULL)
992264Sjacobs 			freelist(*strings);
1002264Sjacobs 		*strings = values;
1012264Sjacobs 	}
1022264Sjacobs }
1032264Sjacobs 
1040Sstevel@tonic-gate char *
printer_name_from_uri_id(char * uri,int32_t id)1052264Sjacobs printer_name_from_uri_id(char *uri, int32_t id)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate 	REQUEST *request = NULL;
1080Sstevel@tonic-gate 	char *result = "";
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	if (uri != NULL) {
1110Sstevel@tonic-gate 		if ((result = strrchr(uri, '/')) != NULL) {
1120Sstevel@tonic-gate 			result += 1;
1130Sstevel@tonic-gate 		} else
1140Sstevel@tonic-gate 			result = (char *)uri;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 		if ((strcmp(result, "jobs") == 0) ||
1170Sstevel@tonic-gate 		    (strcmp(result, "any") == 0) ||
1180Sstevel@tonic-gate 		    (strcmp(result, "all") == 0))
1190Sstevel@tonic-gate 			result = "";
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if ((result[0] == NULL) && (id != -1)) {
1230Sstevel@tonic-gate 		char path[32];
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 		snprintf(path, sizeof (path), "%d-0", id);
1260Sstevel@tonic-gate 		if ((request = getrequest(path)) != NULL)
1270Sstevel@tonic-gate 			result = request->destination;
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	result = strdup(result);
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (request != NULL)
1330Sstevel@tonic-gate 		freerequest(request);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	return (result);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate  * LP content type <-> MIME type conversion table. (order dependent)
1400Sstevel@tonic-gate  */
1410Sstevel@tonic-gate static struct {
1420Sstevel@tonic-gate 	char *mime_type;
1430Sstevel@tonic-gate 	char *lp_type;
1440Sstevel@tonic-gate } type_map[] = {
145*3135Sjacobs 	{ "text/plain", "simple" },
1460Sstevel@tonic-gate 	{ "application/octet-stream", "raw" },
1470Sstevel@tonic-gate 	{ "application/octet-stream", "any" },
1480Sstevel@tonic-gate 	{ "application/postscript", "postscript" },
1490Sstevel@tonic-gate 	{ "application/postscript", "ps" },
1500Sstevel@tonic-gate 	{ "application/x-cif", "cif" },
1510Sstevel@tonic-gate 	{ "application/x-dvi", "dvi" },
1520Sstevel@tonic-gate 	{ "application/x-plot", "plot" },
1530Sstevel@tonic-gate 	{ "application/x-ditroff", "troff" },
1540Sstevel@tonic-gate 	{ "application/x-troff", "otroff" },
1550Sstevel@tonic-gate 	{ "application/x-pr", "pr" },
1560Sstevel@tonic-gate 	{ "application/x-fortran", "fortran" },
1570Sstevel@tonic-gate 	{ "application/x-raster", "raster" },
1580Sstevel@tonic-gate 	{ NULL, NULL}
1590Sstevel@tonic-gate };
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate char *
mime_type_to_lp_type(char * mime_type)1620Sstevel@tonic-gate mime_type_to_lp_type(char *mime_type)
1630Sstevel@tonic-gate {
1640Sstevel@tonic-gate 	int i;
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (mime_type == NULL)
1670Sstevel@tonic-gate 		return ("simple");
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	for (i = 0; type_map[i].mime_type != NULL; i++)
1700Sstevel@tonic-gate 		if (strcasecmp(type_map[i].mime_type, mime_type) == 0)
1710Sstevel@tonic-gate 			return (type_map[i].lp_type);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	return (mime_type);
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate char *
lp_type_to_mime_type(char * lp_type)1770Sstevel@tonic-gate lp_type_to_mime_type(char *lp_type)
1780Sstevel@tonic-gate {
1790Sstevel@tonic-gate 	int i;
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (lp_type == NULL)
182*3135Sjacobs 		return ("text/plain");
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	for (i = 0; type_map[i].lp_type != NULL; i++)
1850Sstevel@tonic-gate 		if (strcasecmp(type_map[i].lp_type, lp_type) == 0)
1860Sstevel@tonic-gate 			return (type_map[i].mime_type);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	return (lp_type);
1890Sstevel@tonic-gate }
190