xref: /titanic_51/usr/src/cmd/lp/lib/requests/getrequest.c (revision 0a44ef6d9afbfe052a7e975f55ea0d2954b62a82)
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
5*0a44ef6dSjacobs  * Common Development and Distribution License (the "License").
6*0a44ef6dSjacobs  * 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  */
21*0a44ef6dSjacobs 
227c478bd9Sstevel@tonic-gate /*
23*0a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
31*0a44ef6dSjacobs #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include "stdio.h"
357c478bd9Sstevel@tonic-gate #include "string.h"
367c478bd9Sstevel@tonic-gate #include "errno.h"
377c478bd9Sstevel@tonic-gate #include "sys/types.h"
387c478bd9Sstevel@tonic-gate #include "stdlib.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include "lp.h"
417c478bd9Sstevel@tonic-gate #include "requests.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate extern struct {
447c478bd9Sstevel@tonic-gate 	char			*v;
457c478bd9Sstevel@tonic-gate 	short			len;
467c478bd9Sstevel@tonic-gate }			reqheadings[];
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /**
497c478bd9Sstevel@tonic-gate  ** getrequest() - EXTRACT REQUEST STRUCTURE FROM DISK FILE
507c478bd9Sstevel@tonic-gate  **/
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate REQUEST *
537c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
547c478bd9Sstevel@tonic-gate getrequest (
557c478bd9Sstevel@tonic-gate 	char *			file
567c478bd9Sstevel@tonic-gate )
577c478bd9Sstevel@tonic-gate #else
587c478bd9Sstevel@tonic-gate getrequest (file)
597c478bd9Sstevel@tonic-gate 	char			*file;
607c478bd9Sstevel@tonic-gate #endif
617c478bd9Sstevel@tonic-gate {
62*0a44ef6dSjacobs 	REQUEST		*reqp;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ],
657c478bd9Sstevel@tonic-gate 				*path,
667c478bd9Sstevel@tonic-gate 				*p;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	int fd;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	int			fld;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	/*
747c478bd9Sstevel@tonic-gate 	 * Full pathname? If so the file must lie in LP's
757c478bd9Sstevel@tonic-gate 	 * regular temporary directory.
767c478bd9Sstevel@tonic-gate 	 */
777c478bd9Sstevel@tonic-gate 	if (*file == '/') {
787c478bd9Sstevel@tonic-gate 		if (!STRNEQU(file, Lp_Tmp, strlen(Lp_Tmp))) {
797c478bd9Sstevel@tonic-gate 			errno = EINVAL;
807c478bd9Sstevel@tonic-gate 			return (0);
817c478bd9Sstevel@tonic-gate 		}
827c478bd9Sstevel@tonic-gate 		path = Strdup(file);
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/*
857c478bd9Sstevel@tonic-gate 	 * A relative pathname (such as system/name)?
867c478bd9Sstevel@tonic-gate 	 * If so we'll locate it under LP's regular temporary
877c478bd9Sstevel@tonic-gate 	 * directory.
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	} else if (strchr(file, '/')) {
907c478bd9Sstevel@tonic-gate 		if (!(path = makepath(Lp_Tmp, file, (char *)0)))
917c478bd9Sstevel@tonic-gate 			return (0);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * It must be a simple name. Locate this under the
957c478bd9Sstevel@tonic-gate 	 * special temporary directory that is linked to the
967c478bd9Sstevel@tonic-gate 	 * regular place for the local system.
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	} else if (!(path = makepath(Lp_Temp, file, (char *)0)))
997c478bd9Sstevel@tonic-gate 		return (0);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", 0)) < 0) {
1037c478bd9Sstevel@tonic-gate 		Free (path);
1047c478bd9Sstevel@tonic-gate 		return (0);
1057c478bd9Sstevel@tonic-gate 	}
1067c478bd9Sstevel@tonic-gate 	Free (path);
1077c478bd9Sstevel@tonic-gate 
108*0a44ef6dSjacobs 	reqp = calloc(sizeof (*reqp), 1);
109*0a44ef6dSjacobs 	reqp->copies		= 1;
110*0a44ef6dSjacobs 	reqp->priority		= -1;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	errno = 0;
1137c478bd9Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd)) {
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 		for (fld = 0; fld < RQ_MAX; fld++)
1187c478bd9Sstevel@tonic-gate 			if (
1197c478bd9Sstevel@tonic-gate 				reqheadings[fld].v
1207c478bd9Sstevel@tonic-gate 			     && reqheadings[fld].len
1217c478bd9Sstevel@tonic-gate 			     && STRNEQU(
1227c478bd9Sstevel@tonic-gate 					buf,
1237c478bd9Sstevel@tonic-gate 					reqheadings[fld].v,
1247c478bd9Sstevel@tonic-gate 					reqheadings[fld].len
1257c478bd9Sstevel@tonic-gate 				)
1267c478bd9Sstevel@tonic-gate 			) {
1277c478bd9Sstevel@tonic-gate 				p = buf + reqheadings[fld].len;
1287c478bd9Sstevel@tonic-gate 				break;
1297c478bd9Sstevel@tonic-gate 			}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 		/*
1327c478bd9Sstevel@tonic-gate 		 * To allow future extensions to not impact applications
1337c478bd9Sstevel@tonic-gate 		 * using old versions of this routine, ignore strange
1347c478bd9Sstevel@tonic-gate 		 * fields.
1357c478bd9Sstevel@tonic-gate 		 */
1367c478bd9Sstevel@tonic-gate 		if (fld >= RQ_MAX)
1377c478bd9Sstevel@tonic-gate 			continue;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 		switch (fld) {
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		case RQ_COPIES:
142*0a44ef6dSjacobs 			reqp->copies = atoi(p);
1437c478bd9Sstevel@tonic-gate 			break;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		case RQ_DEST:
146*0a44ef6dSjacobs 			reqp->destination = Strdup(p);
1477c478bd9Sstevel@tonic-gate 			break;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		case RQ_FILE:
150*0a44ef6dSjacobs 			appendlist (&reqp->file_list, p);
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		case RQ_FORM:
1547c478bd9Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
155*0a44ef6dSjacobs 				reqp->form = Strdup(p);
1567c478bd9Sstevel@tonic-gate 			break;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		case RQ_HANDL:
1597c478bd9Sstevel@tonic-gate 			if (STREQU(p, NAME_RESUME))
160*0a44ef6dSjacobs 				reqp->actions |= ACT_RESUME;
1617c478bd9Sstevel@tonic-gate 			else if (STREQU(p, NAME_HOLD))
162*0a44ef6dSjacobs 				reqp->actions |= ACT_HOLD;
1637c478bd9Sstevel@tonic-gate 			else if (STREQU(p, NAME_IMMEDIATE))
164*0a44ef6dSjacobs 				reqp->actions |= ACT_IMMEDIATE;
1657c478bd9Sstevel@tonic-gate 			break;
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		case RQ_NOTIFY:
1687c478bd9Sstevel@tonic-gate 			if (STREQU(p, "M"))
169*0a44ef6dSjacobs 				reqp->actions |= ACT_MAIL;
1707c478bd9Sstevel@tonic-gate 			else if (STREQU(p, "W"))
171*0a44ef6dSjacobs 				reqp->actions |= ACT_WRITE;
1727c478bd9Sstevel@tonic-gate 			else if (STREQU(p, "N"))
173*0a44ef6dSjacobs 				reqp->actions |= ACT_NOTIFY;
1747c478bd9Sstevel@tonic-gate 			else
175*0a44ef6dSjacobs 				reqp->alert = Strdup(p);
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 		case RQ_OPTS:
179*0a44ef6dSjacobs 			reqp->options = Strdup(p);
1807c478bd9Sstevel@tonic-gate 			break;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		case RQ_PRIOR:
183*0a44ef6dSjacobs 			reqp->priority = atoi(p);
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		case RQ_PAGES:
187*0a44ef6dSjacobs 			reqp->pages = Strdup(p);
1887c478bd9Sstevel@tonic-gate 			break;
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		case RQ_CHARS:
1917c478bd9Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
192*0a44ef6dSjacobs 				reqp->charset = Strdup(p);
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 		case RQ_TITLE:
196*0a44ef6dSjacobs 			reqp->title = Strdup(p);
1977c478bd9Sstevel@tonic-gate 			break;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 		case RQ_MODES:
200*0a44ef6dSjacobs 			reqp->modes = Strdup(p);
2017c478bd9Sstevel@tonic-gate 			break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		case RQ_TYPE:
204*0a44ef6dSjacobs 			reqp->input_type = Strdup(p);
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		case RQ_USER:
208*0a44ef6dSjacobs 			reqp->user = Strdup(p);
2097c478bd9Sstevel@tonic-gate 			break;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 		case RQ_RAW:
212*0a44ef6dSjacobs 			reqp->actions |= ACT_RAW;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		case RQ_FAST:
216*0a44ef6dSjacobs 			reqp->actions |= ACT_FAST;
2177c478bd9Sstevel@tonic-gate 			break;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 		case RQ_STAT:
220*0a44ef6dSjacobs 			reqp->outcome = (ushort)strtol(p, (char **)0, 16);
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	if (errno != 0) {
2277c478bd9Sstevel@tonic-gate 		int			save_errno = errno;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		close(fd);
2307c478bd9Sstevel@tonic-gate 		errno = save_errno;
2317c478bd9Sstevel@tonic-gate 		return (0);
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate 	close(fd);
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	 * Now go through the structure and see if we have
2377c478bd9Sstevel@tonic-gate 	 * anything strange.
2387c478bd9Sstevel@tonic-gate 	 */
2397c478bd9Sstevel@tonic-gate 	if (
240*0a44ef6dSjacobs 		reqp->copies <= 0
241*0a44ef6dSjacobs 	     || !reqp->file_list || !*(reqp->file_list)
242*0a44ef6dSjacobs 	     || reqp->priority < -1 || 39 < reqp->priority
243*0a44ef6dSjacobs 	     || STREQU(reqp->input_type, NAME_ANY)
244*0a44ef6dSjacobs 	     || STREQU(reqp->input_type, NAME_TERMINFO)
2457c478bd9Sstevel@tonic-gate 	) {
246*0a44ef6dSjacobs 		freerequest (reqp);
2477c478bd9Sstevel@tonic-gate 		errno = EBADF;
2487c478bd9Sstevel@tonic-gate 		return (0);
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	/*
2527c478bd9Sstevel@tonic-gate 	 * Guarantee some return values won't be null or empty.
2537c478bd9Sstevel@tonic-gate 	 */
254*0a44ef6dSjacobs 	if (!reqp->destination || !*reqp->destination) {
255*0a44ef6dSjacobs 		if (reqp->destination)
256*0a44ef6dSjacobs 			Free (reqp->destination);
257*0a44ef6dSjacobs 		reqp->destination = Strdup(NAME_ANY);
2587c478bd9Sstevel@tonic-gate 	}
259*0a44ef6dSjacobs 	if (!reqp->input_type || !*reqp->input_type) {
260*0a44ef6dSjacobs 		if (reqp->input_type)
261*0a44ef6dSjacobs 			Free (reqp->input_type);
262*0a44ef6dSjacobs 		reqp->input_type = Strdup(NAME_SIMPLE);
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
265*0a44ef6dSjacobs 	return (reqp);
2667c478bd9Sstevel@tonic-gate }
267