Home | History | Annotate | Download | only in papi
      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 2008 Sun Microsystems, Inc.  All rights reserved.
     23  * Use is subject to license terms.
     24  */
     25 
     26 /*LINTLIBRARY*/
     27 
     28 #include <stdlib.h>
     29 #include <libintl.h>
     30 #include <unistd.h>
     31 #include <string.h>
     32 #include <sys/utsname.h>
     33 #include <papi_impl.h>
     34 
     35 #include "class.h"
     36 
     37 void
     38 lpsched_printer_status_to_attributes(papi_attribute_t ***attrs,
     39 	unsigned short status)
     40 {
     41 	if (attrs == NULL)
     42 		return;
     43 
     44 	if (status & (PS_DISABLED|PS_LATER|PS_FAULTED|PS_FORM_FAULT)) {
     45 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
     46 				"printer-state", 0x05); /* stopped */
     47 		if (status & PS_LATER)
     48 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
     49 				"printer-state-reasons", "moving-to-paused");
     50 		else if (status & PS_FAULTED)
     51 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
     52 				"printer-state-reasons", "none");
     53 		else if (status & PS_FORM_FAULT)
     54 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
     55 				"printer-state-reasons",
     56 				"interpreter-resource-unavailable");
     57 		else
     58 			papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
     59 				"printer-state-reasons", "paused");
     60 	} else if (status & PS_BUSY) {
     61 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
     62 				"printer-state", 0x04); /* processing */
     63 		papiAttributeListAddString(attrs, PAPI_ATTR_REPLACE,
     64 				"printer-state-reasons", "moving-to-paused");
     65 	} else {
     66 		papiAttributeListAddInteger(attrs, PAPI_ATTR_REPLACE,
     67 				"printer-state", 0x03); /* idle */
     68 	}
     69 
     70 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
     71 			"printer-is-accepting-jobs",
     72 			((status & PS_REJECTED) != PS_REJECTED));
     73 	papiAttributeListAddBoolean(attrs, PAPI_ATTR_REPLACE,
     74 			"printer-is-processing-jobs",
     75 			((status & PS_DISABLED) != PS_DISABLED));
     76 }
     77 
     78 void
     79 lpsched_printer_defaults(papi_attribute_t ***attributes)
     80 {
     81 	if (attributes == NULL)
     82 		return;
     83 
     84 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
     85 			"multiple-document-jobs-supported", PAPI_TRUE);
     86 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
     87 			"multiple-document-handling-supported",
     88 			"seperate-documents-colated-copies");
     89 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
     90 			"pdl-override-supported", "not-attempted");
     91 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
     92 			"job-priority-supported", 40);
     93 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
     94 			"job-priority-default", 20);
     95 	papiAttributeListAddRange(attributes, PAPI_ATTR_REPLACE,
     96 			"copies-supported", 1, 65535);
     97 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
     98 			"copies-default", 1);
     99 	papiAttributeListAddBoolean(attributes, PAPI_ATTR_REPLACE,
    100 			"page-ranges-supported", PAPI_TRUE);
    101 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
    102 			"number-up-supported", 1);
    103 	papiAttributeListAddInteger(attributes, PAPI_ATTR_REPLACE,
    104 			"number-up-default", 1);
    105 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
    106 			"job-hold-until-supported", "no-hold");
    107 	papiAttributeListAddString(attributes, PAPI_ATTR_APPEND,
    108 			"job-hold-until-supported", "indefinite");
    109 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
    110 			"job-hold-until-default", "no-hold");
    111 	papiAttributeListAddString(attributes, PAPI_ATTR_REPLACE,
    112 			"document-format-default", "application/octet-stream");
    113 
    114 }
    115 
    116 papi_status_t
    117 lpsched_printer_configuration_to_attributes(service_t *svc, printer_t *p,
    118 	char *dest)
    119 {
    120 	PRINTER *tmp;
    121 	char buf[BUFSIZ+1];
    122 	struct utsname sysname;
    123 	char **allowed = NULL, **denied = NULL;
    124 	char **f_allowed = NULL, **f_denied = NULL;
    125 
    126 	if ((svc == NULL) || (p == NULL) || (dest == NULL))
    127 		return (PAPI_BAD_ARGUMENT);
    128 
    129 	/* get the configuration DB data */
    130 	if ((tmp = getprinter(dest)) == NULL) {
    131 		detailed_error(svc,
    132 			gettext("unable to read configuration data"));
    133 		return (PAPI_DEVICE_ERROR);
    134 	}
    135 
    136 	/* name */
    137 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    138 				"printer-name", tmp->name);
    139 	if (tmp->name != NULL) {
    140 		char uri[BUFSIZ];
    141 
    142 		snprintf(uri, sizeof (uri), "lpsched://localhost/printers/%s",
    143 				tmp->name);
    144 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
    145 				"printer-uri-supported", uri);
    146 	}
    147 
    148 	/* banner */
    149 	if ((tmp->banner & BAN_OPTIONAL) == BAN_OPTIONAL)
    150 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
    151 				"job-sheets-supported", "optional");
    152 	else if (tmp->banner & BAN_NEVER)
    153 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
    154 				"job-sheets-supported", "none");
    155 	else if (tmp->banner & BAN_ALWAYS)
    156 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_APPEND,
    157 				"job-sheets-supported", "standard");
    158 
    159 	/* input_types */
    160 	if (tmp->input_types != NULL) {
    161 		int i;
    162 
    163 		for (i = 0; tmp->input_types[i] != NULL; i++)
    164 			papiAttributeListAddLPString(&p->attributes,
    165 				PAPI_ATTR_APPEND, "document-format-supported",
    166 				lp_type_to_mime_type(tmp->input_types[i]));
    167 	}
    168 
    169 	/* description */
    170 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    171 			"printer-info", tmp->description);
    172 
    173 	/* add lpsched specific attributes */
    174 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    175 				"device-uri", tmp->device);
    176 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    177 				"lpsched-dial-info", tmp->dial_info);
    178 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    179 				"lpsched-fault-recovery", tmp->fault_rec);
    180 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    181 				"lpsched-interface-script", tmp->interface);
    182 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    183 				"lpsched-data-rate", tmp->speed);
    184 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    185 				"lpsched-stty", tmp->stty);
    186 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
    187 				"lpsched-login-term", tmp->login);
    188 	papiAttributeListAddBoolean(&p->attributes, PAPI_ATTR_REPLACE,
    189 				"lpsched-daisy", tmp->daisy);
    190 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    191 				"lpsched-charsets", tmp->char_sets);
    192 #ifdef CAN_DO_MODULES
    193 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    194 				"lpsched-modules", tmp->modules);
    195 #endif /* CAN_DO_MODULES */
    196 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    197 				"lpsched-options", tmp->options);
    198 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    199 				"lpsched-printer-type", tmp->printer_types);
    200 	if (tmp->fault_alert.shcmd != NULL) {
    201 		papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    202 				"lpsched-fault-alert-command",
    203 				tmp->fault_alert.shcmd);
    204 		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    205 				"lpsched-fault-alert-threshold",
    206 				tmp->fault_alert.Q);
    207 		papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    208 				"lpsched-fault-alert-interval",
    209 				tmp->fault_alert.W);
    210 	}
    211 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    212 				"lpsched-cpi-value", tmp->cpi.val);
    213 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    214 				"lpsched-cpi-unit", tmp->cpi.sc);
    215 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    216 				"lpsched-lpi-value", tmp->lpi.val);
    217 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    218 				"lpsched-lpi-unit", tmp->lpi.sc);
    219 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    220 				"lpsched-plen-value", tmp->plen.val);
    221 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    222 				"lpsched-plen-unit", tmp->plen.sc);
    223 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    224 				"lpsched-pwid-value", tmp->pwid.val);
    225 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    226 				"lpsched-pwid-unit", tmp->pwid.sc);
    227 
    228 	/* allow/deny list */
    229 	load_userprinter_access(dest, &allowed, &denied);
    230 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    231 				"requesting-user-name-allowed", allowed);
    232 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    233 				"requesting-user-name-denied", denied);
    234 
    235 	freelist(allowed);
    236 	freelist(denied);
    237 
    238 	/* forms allow/deny list */
    239 	load_formprinter_access(dest, &f_allowed, &f_denied);
    240 	papiAttributeListAddLPStrings(&p->attributes, PAPI_ATTR_REPLACE,
    241 	    "form-supported", f_allowed);
    242 
    243 	/*
    244 	 * All forms allowed case
    245 	 * When all forms are allowed forms.allow does not get created but
    246 	 * forms.deny file gets created with no entries
    247 	 */
    248 	if ((f_allowed == NULL) && (f_denied != NULL) && (f_denied[0] == NULL))
    249 		papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    250 		    "form-supported", "all");
    251 
    252 	freelist(f_allowed);
    253 	freelist(f_denied);
    254 
    255 #ifdef LP_USE_PAPI_ATTR
    256 	if (tmp->ppd != NULL) {
    257 		int fd;
    258 		struct stat sbuf;
    259 
    260 		/* construct the two URIs for the printer's PPD file */
    261 		if (uname(&sysname) < 0) {
    262 			/* failed to get systen name */
    263 			sysname.nodename[0] = 0;
    264 		}
    265 		snprintf(buf, sizeof (buf), "file://%s%s/ppd/%s.ppd",
    266 			sysname.nodename, ETCDIR, tmp->name);
    267 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
    268 			"ppd-file-uri", buf);
    269 
    270 		snprintf(buf, sizeof (buf), "file://%s%s",
    271 			sysname.nodename, tmp->ppd);
    272 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
    273 			"lpsched-printer-configure-ppd-uri", buf);
    274 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
    275 			"lpsched-ppd-source-path", tmp->ppd);
    276 
    277 		snprintf(buf, sizeof (buf), "%s/ppd/%s.ppd", ETCDIR, tmp->name);
    278 
    279 		/*
    280 		 * We don't return error on any of the error conditions, we just
    281 		 * silently return without adding the attribute.
    282 		 */
    283 		PPDFileToAttributesList(&p->attributes, buf);
    284 	}
    285 #endif
    286 
    287 	freeprinter(tmp);
    288 
    289 	return (PAPI_OK);
    290 }
    291 
    292 papi_status_t
    293 printer_status_to_attributes(printer_t *p, char *printer, char *form,
    294 		char *character_set, char *disable_reason, char *reject_reason,
    295 		short status, char *request_id,
    296 		long disable_date, long reject_date)
    297 {
    298 	if (p == NULL)
    299 		return (PAPI_BAD_ARGUMENT);
    300 
    301 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    302 			"form-ready", form);
    303 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    304 			"lpsched-active-job", request_id);
    305 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    306 			"lpsched-mounted-char-set", character_set);
    307 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    308 			"lpsched-disable-reason", disable_reason);
    309 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
    310 			"lpsched-disable-date", disable_date);
    311 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    312 			"lpsched-reject-reason", reject_reason);
    313 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
    314 			"lpsched-reject-date", reject_date);
    315 
    316 	/* add the current system time */
    317 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
    318 			"printer-current-time", time(NULL));
    319 
    320 	/* add the time since last enabled */
    321 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    322 			"printer-up-time", time(NULL));
    323 
    324 	/* add the status information */
    325 	lpsched_printer_status_to_attributes(&p->attributes, status);
    326 
    327 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
    328 			"printer-state-reasons", "none");
    329 
    330 	lpsched_printer_defaults(&p->attributes);
    331 
    332 	return (PAPI_OK);
    333 }
    334 
    335 
    336 /*
    337  * This puts the class information in only.  It could create a hybrid
    338  * printer object to return, but that is problematic at best.
    339  */
    340 papi_status_t
    341 lpsched_class_configuration_to_attributes(service_t *svc, printer_t *p,
    342 	char *dest)
    343 {
    344 	CLASS *tmp;
    345 
    346 	if ((svc == NULL) || (p == NULL))
    347 		return (PAPI_BAD_ARGUMENT);
    348 
    349 	/* get the configuration DB data */
    350 	if ((tmp = getclass(dest)) == NULL) {
    351 		detailed_error(svc,
    352 			gettext("unable to read configuration data"));
    353 		return (PAPI_DEVICE_ERROR);
    354 	}
    355 
    356 	/* name */
    357 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    358 				"printer-name", tmp->name);
    359 	if (tmp->name != NULL) {
    360 		char uri[BUFSIZ];
    361 
    362 		snprintf(uri, sizeof (uri), "lpsched://localhost/printers/%s",
    363 				tmp->name);
    364 		papiAttributeListAddString(&p->attributes, PAPI_ATTR_REPLACE,
    365 				"printer-uri-supported", uri);
    366 	}
    367 
    368 	if (tmp->members != NULL) {
    369 		char **members = tmp->members;
    370 		int i;
    371 
    372 		for (i = 0; members[i] != NULL; i++)
    373 			papiAttributeListAddString(&p->attributes,
    374 					PAPI_ATTR_APPEND,
    375 					"member-names", members[i]);
    376 	}
    377 
    378 	freeclass(tmp);
    379 
    380 	return (PAPI_OK);
    381 }
    382 
    383 papi_status_t
    384 class_status_to_attributes(printer_t *p, char *printer, short status,
    385 		char *reject_reason, long reject_date)
    386 {
    387 	if (p == NULL)
    388 		return (PAPI_BAD_ARGUMENT);
    389 
    390 	papiAttributeListAddLPString(&p->attributes, PAPI_ATTR_REPLACE,
    391 			"lpsched-reject-reason", reject_reason);
    392 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
    393 			"lpsched-reject-date", reject_date);
    394 
    395 	/* add the current system time */
    396 	papiAttributeListAddDatetime(&p->attributes, PAPI_ATTR_REPLACE,
    397 			"printer-current-time", time(NULL));
    398 
    399 	papiAttributeListAddInteger(&p->attributes, PAPI_ATTR_REPLACE,
    400 			"printer-up-time", time(NULL));
    401 
    402 	/* add the status information */
    403 	lpsched_printer_status_to_attributes(&p->attributes, status);
    404 
    405 	papiAttributeListAddString(&p->attributes, PAPI_ATTR_EXCL,
    406 			"printer-state-reasons", "none");
    407 
    408 	lpsched_printer_defaults(&p->attributes);
    409 
    410 	return (PAPI_OK);
    411 }
    412 
    413 papi_status_t
    414 attributes_to_printer(papi_attribute_t **attributes, PRINTER *tmp)
    415 {
    416 	papi_status_t status;
    417 	void *iter = NULL;
    418 	char *string = NULL;
    419 	int flags;
    420 	char **list = NULL;
    421 
    422 	/* banner needs some conversion to the bitfield */
    423 	iter = NULL, string = NULL; flags = 0;
    424 	for (status = papiAttributeListGetString(attributes, &iter,
    425 				"job-sheets-supported", &string);
    426 	    status == PAPI_OK;
    427 	    status = papiAttributeListGetString(attributes, &iter,
    428 				NULL, &string))
    429 		if (strcasecmp(string, "none") == 0)
    430 			flags |= BAN_NEVER;
    431 		else if (strcasecmp(string, "standard") == 0)
    432 			flags |= BAN_ALWAYS;
    433 	if (flags != 0)
    434 		tmp->banner = flags;
    435 
    436 	/* input_types needs mime-type conversion */
    437 	iter = NULL, string = NULL; list = NULL;
    438 	for (status = papiAttributeListGetString(attributes, &iter,
    439 				"document-format-supported", &string);
    440 	    status == PAPI_OK;
    441 	    status = papiAttributeListGetString(attributes, &iter,
    442 				NULL, &string))
    443 		addlist(&list, mime_type_to_lp_type(string));
    444 	if (list != NULL) {
    445 		if (tmp->input_types != NULL)
    446 			freelist(tmp->input_types);
    447 		tmp->input_types = list;
    448 	}
    449 
    450 	papiAttributeListGetLPString(attributes,
    451 				"device-uri", &tmp->device);
    452 	papiAttributeListGetLPString(attributes,
    453 				"printer-info", &tmp->description);
    454 	papiAttributeListGetLPString(attributes,
    455 				"lpsched-dial-info", &tmp->dial_info);
    456 	papiAttributeListGetLPString(attributes,
    457 				"lpsched-fault-recovery", &tmp->fault_rec);
    458 	papiAttributeListGetLPString(attributes,
    459 				"lpsched-interface-script", &tmp->interface);
    460 	papiAttributeListGetLPString(attributes,
    461 				"lpsched-data-rate", &tmp->speed);
    462 	papiAttributeListGetLPString(attributes,
    463 				"lpsched-stty", &tmp->stty);
    464 	papiAttributeListGetLPStrings(attributes,
    465 				"lpsched-charsets", &tmp->char_sets);
    466 	papiAttributeListGetLPStrings(attributes,
    467 				"lpsched-printer-types", &tmp->printer_types);
    468 	papiAttributeListGetLPStrings(attributes,
    469 				"lpsched-options", &tmp->options);
    470 	papiAttributeListGetLPStrings(attributes,
    471 				"lpsched-modules", &tmp->modules);
    472 #ifdef LP_USE_PAPI_ATTR
    473 	papiAttributeListGetLPString(attributes,
    474 				"lpsched-printer-ppd-uri", &tmp->ppd);
    475 #endif
    476 
    477 	return (PAPI_OK);
    478 }
    479