1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.14	*/
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "stdio.h"
35*0Sstevel@tonic-gate #include "string.h"
36*0Sstevel@tonic-gate #include "errno.h"
37*0Sstevel@tonic-gate #include "sys/types.h"
38*0Sstevel@tonic-gate #include "stdlib.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include "lp.h"
41*0Sstevel@tonic-gate #include "requests.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate extern struct {
44*0Sstevel@tonic-gate 	char			*v;
45*0Sstevel@tonic-gate 	short			len;
46*0Sstevel@tonic-gate }			reqheadings[];
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate /**
49*0Sstevel@tonic-gate  ** getrequest() - EXTRACT REQUEST STRUCTURE FROM DISK FILE
50*0Sstevel@tonic-gate  **/
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate REQUEST *
53*0Sstevel@tonic-gate #if	defined(__STDC__)
54*0Sstevel@tonic-gate getrequest (
55*0Sstevel@tonic-gate 	char *			file
56*0Sstevel@tonic-gate )
57*0Sstevel@tonic-gate #else
58*0Sstevel@tonic-gate getrequest (file)
59*0Sstevel@tonic-gate 	char			*file;
60*0Sstevel@tonic-gate #endif
61*0Sstevel@tonic-gate {
62*0Sstevel@tonic-gate 	static REQUEST		reqbuf;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	char			buf[BUFSIZ],
65*0Sstevel@tonic-gate 				*path,
66*0Sstevel@tonic-gate 				*p;
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	int fd;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	int			fld;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate 	/*
74*0Sstevel@tonic-gate 	 * Full pathname? If so the file must lie in LP's
75*0Sstevel@tonic-gate 	 * regular temporary directory.
76*0Sstevel@tonic-gate 	 */
77*0Sstevel@tonic-gate 	if (*file == '/') {
78*0Sstevel@tonic-gate 		if (!STRNEQU(file, Lp_Tmp, strlen(Lp_Tmp))) {
79*0Sstevel@tonic-gate 			errno = EINVAL;
80*0Sstevel@tonic-gate 			return (0);
81*0Sstevel@tonic-gate 		}
82*0Sstevel@tonic-gate 		path = Strdup(file);
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	/*
85*0Sstevel@tonic-gate 	 * A relative pathname (such as system/name)?
86*0Sstevel@tonic-gate 	 * If so we'll locate it under LP's regular temporary
87*0Sstevel@tonic-gate 	 * directory.
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	} else if (strchr(file, '/')) {
90*0Sstevel@tonic-gate 		if (!(path = makepath(Lp_Tmp, file, (char *)0)))
91*0Sstevel@tonic-gate 			return (0);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * It must be a simple name. Locate this under the
95*0Sstevel@tonic-gate 	 * special temporary directory that is linked to the
96*0Sstevel@tonic-gate 	 * regular place for the local system.
97*0Sstevel@tonic-gate 	 */
98*0Sstevel@tonic-gate 	} else if (!(path = makepath(Lp_Temp, file, (char *)0)))
99*0Sstevel@tonic-gate 		return (0);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", 0)) < 0) {
103*0Sstevel@tonic-gate 		Free (path);
104*0Sstevel@tonic-gate 		return (0);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 	Free (path);
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	reqbuf.copies		= 1;
109*0Sstevel@tonic-gate 	reqbuf.destination	= 0;
110*0Sstevel@tonic-gate 	reqbuf.file_list	= 0;
111*0Sstevel@tonic-gate 	reqbuf.form		= 0;
112*0Sstevel@tonic-gate 	reqbuf.actions		= 0;
113*0Sstevel@tonic-gate 	reqbuf.alert		= 0;
114*0Sstevel@tonic-gate 	reqbuf.options		= 0;
115*0Sstevel@tonic-gate 	reqbuf.priority		= -1;
116*0Sstevel@tonic-gate 	reqbuf.pages		= 0;
117*0Sstevel@tonic-gate 	reqbuf.charset		= 0;
118*0Sstevel@tonic-gate 	reqbuf.modes		= 0;
119*0Sstevel@tonic-gate 	reqbuf.title		= 0;
120*0Sstevel@tonic-gate 	reqbuf.input_type	= 0;
121*0Sstevel@tonic-gate 	reqbuf.user		= 0;
122*0Sstevel@tonic-gate 	reqbuf.outcome		= 0;
123*0Sstevel@tonic-gate 	reqbuf.version		= VERSION_OLD_LP;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	errno = 0;
126*0Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fd)) {
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 		buf[strlen(buf) - 1] = 0;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 		for (fld = 0; fld < RQ_MAX; fld++)
131*0Sstevel@tonic-gate 			if (
132*0Sstevel@tonic-gate 				reqheadings[fld].v
133*0Sstevel@tonic-gate 			     && reqheadings[fld].len
134*0Sstevel@tonic-gate 			     && STRNEQU(
135*0Sstevel@tonic-gate 					buf,
136*0Sstevel@tonic-gate 					reqheadings[fld].v,
137*0Sstevel@tonic-gate 					reqheadings[fld].len
138*0Sstevel@tonic-gate 				)
139*0Sstevel@tonic-gate 			) {
140*0Sstevel@tonic-gate 				p = buf + reqheadings[fld].len;
141*0Sstevel@tonic-gate 				break;
142*0Sstevel@tonic-gate 			}
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 		/*
145*0Sstevel@tonic-gate 		 * To allow future extensions to not impact applications
146*0Sstevel@tonic-gate 		 * using old versions of this routine, ignore strange
147*0Sstevel@tonic-gate 		 * fields.
148*0Sstevel@tonic-gate 		 */
149*0Sstevel@tonic-gate 		if (fld >= RQ_MAX)
150*0Sstevel@tonic-gate 			continue;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 		switch (fld) {
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 		case RQ_COPIES:
155*0Sstevel@tonic-gate 			reqbuf.copies = atoi(p);
156*0Sstevel@tonic-gate 			break;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		case RQ_DEST:
159*0Sstevel@tonic-gate 			reqbuf.destination = Strdup(p);
160*0Sstevel@tonic-gate 			break;
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		case RQ_FILE:
163*0Sstevel@tonic-gate 			appendlist (&reqbuf.file_list, p);
164*0Sstevel@tonic-gate 			break;
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 		case RQ_FORM:
167*0Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
168*0Sstevel@tonic-gate 				reqbuf.form = Strdup(p);
169*0Sstevel@tonic-gate 			break;
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate 		case RQ_HANDL:
172*0Sstevel@tonic-gate 			if (STREQU(p, NAME_RESUME))
173*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_RESUME;
174*0Sstevel@tonic-gate 			else if (STREQU(p, NAME_HOLD))
175*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_HOLD;
176*0Sstevel@tonic-gate 			else if (STREQU(p, NAME_IMMEDIATE))
177*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_IMMEDIATE;
178*0Sstevel@tonic-gate 			break;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 		case RQ_NOTIFY:
181*0Sstevel@tonic-gate 			if (STREQU(p, "M"))
182*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_MAIL;
183*0Sstevel@tonic-gate 			else if (STREQU(p, "W"))
184*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_WRITE;
185*0Sstevel@tonic-gate 			else if (STREQU(p, "N"))
186*0Sstevel@tonic-gate 				reqbuf.actions |= ACT_NOTIFY;
187*0Sstevel@tonic-gate 			else
188*0Sstevel@tonic-gate 				reqbuf.alert = Strdup(p);
189*0Sstevel@tonic-gate 			break;
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		case RQ_OPTS:
192*0Sstevel@tonic-gate 			reqbuf.options = Strdup(p);
193*0Sstevel@tonic-gate 			break;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 		case RQ_PRIOR:
196*0Sstevel@tonic-gate 			reqbuf.priority = atoi(p);
197*0Sstevel@tonic-gate 			break;
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		case RQ_PAGES:
200*0Sstevel@tonic-gate 			reqbuf.pages = Strdup(p);
201*0Sstevel@tonic-gate 			break;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate 		case RQ_CHARS:
204*0Sstevel@tonic-gate 			if (!STREQU(p, NAME_ANY))
205*0Sstevel@tonic-gate 				reqbuf.charset = Strdup(p);
206*0Sstevel@tonic-gate 			break;
207*0Sstevel@tonic-gate 
208*0Sstevel@tonic-gate 		case RQ_TITLE:
209*0Sstevel@tonic-gate 			reqbuf.title = Strdup(p);
210*0Sstevel@tonic-gate 			break;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 		case RQ_MODES:
213*0Sstevel@tonic-gate 			reqbuf.modes = Strdup(p);
214*0Sstevel@tonic-gate 			break;
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 		case RQ_TYPE:
217*0Sstevel@tonic-gate 			reqbuf.input_type = Strdup(p);
218*0Sstevel@tonic-gate 			break;
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate 		case RQ_USER:
221*0Sstevel@tonic-gate 			reqbuf.user = Strdup(p);
222*0Sstevel@tonic-gate 			break;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 		case RQ_RAW:
225*0Sstevel@tonic-gate 			reqbuf.actions |= ACT_RAW;
226*0Sstevel@tonic-gate 			break;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		case RQ_FAST:
229*0Sstevel@tonic-gate 			reqbuf.actions |= ACT_FAST;
230*0Sstevel@tonic-gate 			break;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		case RQ_STAT:
233*0Sstevel@tonic-gate 			reqbuf.outcome = (ushort)strtol(p, (char **)0, 16);
234*0Sstevel@tonic-gate 			break;
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate 		case RQ_VERSION:
237*0Sstevel@tonic-gate 			reqbuf.version = atoi(p);
238*0Sstevel@tonic-gate 			break;
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate 		}
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	}
243*0Sstevel@tonic-gate 	if (errno != 0) {
244*0Sstevel@tonic-gate 		int			save_errno = errno;
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 		close(fd);
247*0Sstevel@tonic-gate 		errno = save_errno;
248*0Sstevel@tonic-gate 		return (0);
249*0Sstevel@tonic-gate 	}
250*0Sstevel@tonic-gate 	close(fd);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	/*
253*0Sstevel@tonic-gate 	 * Now go through the structure and see if we have
254*0Sstevel@tonic-gate 	 * anything strange.
255*0Sstevel@tonic-gate 	 */
256*0Sstevel@tonic-gate 	if (
257*0Sstevel@tonic-gate 		reqbuf.copies <= 0
258*0Sstevel@tonic-gate 	     || !reqbuf.file_list || !*(reqbuf.file_list)
259*0Sstevel@tonic-gate 	     || reqbuf.priority < -1 || 39 < reqbuf.priority
260*0Sstevel@tonic-gate 	     || STREQU(reqbuf.input_type, NAME_ANY)
261*0Sstevel@tonic-gate 	     || STREQU(reqbuf.input_type, NAME_TERMINFO)
262*0Sstevel@tonic-gate 	) {
263*0Sstevel@tonic-gate 		freerequest (&reqbuf);
264*0Sstevel@tonic-gate 		errno = EBADF;
265*0Sstevel@tonic-gate 		return (0);
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 	/*
269*0Sstevel@tonic-gate 	 * Guarantee some return values won't be null or empty.
270*0Sstevel@tonic-gate 	 */
271*0Sstevel@tonic-gate 	if (!reqbuf.destination || !*reqbuf.destination) {
272*0Sstevel@tonic-gate 		if (reqbuf.destination)
273*0Sstevel@tonic-gate 			Free (reqbuf.destination);
274*0Sstevel@tonic-gate 		reqbuf.destination = Strdup(NAME_ANY);
275*0Sstevel@tonic-gate 	}
276*0Sstevel@tonic-gate 	if (!reqbuf.input_type || !*reqbuf.input_type) {
277*0Sstevel@tonic-gate 		if (reqbuf.input_type)
278*0Sstevel@tonic-gate 			Free (reqbuf.input_type);
279*0Sstevel@tonic-gate 		reqbuf.input_type = Strdup(NAME_SIMPLE);
280*0Sstevel@tonic-gate 	}
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	return (&reqbuf);
283*0Sstevel@tonic-gate }
284