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.17 */
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 "form.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 short infile;
47*0Sstevel@tonic-gate } formheadings[];
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate #if defined(__STDC__)
50*0Sstevel@tonic-gate int _search_fheading ( char * );
51*0Sstevel@tonic-gate #else
52*0Sstevel@tonic-gate int _search_fheading();
53*0Sstevel@tonic-gate #endif
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate int
rdform(char * name,FORM * formp,int fd,int (* error_handler)(int,int,int),int * which_set)56*0Sstevel@tonic-gate rdform(char *name, FORM *formp, int fd, int (*error_handler)( int , int , int ),
57*0Sstevel@tonic-gate int *which_set)
58*0Sstevel@tonic-gate {
59*0Sstevel@tonic-gate char buf[BUFSIZ];
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate char * rest;
62*0Sstevel@tonic-gate char * mandp;
63*0Sstevel@tonic-gate char * dftp;
64*0Sstevel@tonic-gate char * here;
65*0Sstevel@tonic-gate
66*0Sstevel@tonic-gate int fld;
67*0Sstevel@tonic-gate int have_line_already;
68*0Sstevel@tonic-gate int found_alignment_pattern;
69*0Sstevel@tonic-gate int size;
70*0Sstevel@tonic-gate int add_size;
71*0Sstevel@tonic-gate int linenum;
72*0Sstevel@tonic-gate int i;
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate SCALED sdn;
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate register char * p;
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate /*
80*0Sstevel@tonic-gate * Initialize the entire structure, to ensure no random
81*0Sstevel@tonic-gate * values get in it. However, make sure some values won't
82*0Sstevel@tonic-gate * be null or empty. Do the latter here as opposed to
83*0Sstevel@tonic-gate * after reading the file, because sometimes the file
84*0Sstevel@tonic-gate * contains an empty header to FORCE a null/empty value.
85*0Sstevel@tonic-gate */
86*0Sstevel@tonic-gate (void)memset ((char *)formp, 0, sizeof(*formp));
87*0Sstevel@tonic-gate formp->name = Strdup(name);
88*0Sstevel@tonic-gate formp->plen.val = DPLEN;
89*0Sstevel@tonic-gate formp->plen.sc = 0;
90*0Sstevel@tonic-gate formp->pwid.val = DPWIDTH;
91*0Sstevel@tonic-gate formp->pwid.sc = 0;
92*0Sstevel@tonic-gate formp->lpi.val = DLPITCH;
93*0Sstevel@tonic-gate formp->lpi.sc = 0;
94*0Sstevel@tonic-gate formp->cpi.val = DCPITCH;
95*0Sstevel@tonic-gate formp->cpi.sc = 0;
96*0Sstevel@tonic-gate formp->np = DNP;
97*0Sstevel@tonic-gate formp->chset = Strdup(DCHSET);
98*0Sstevel@tonic-gate formp->mandatory = 0;
99*0Sstevel@tonic-gate formp->rcolor = Strdup(DRCOLOR);
100*0Sstevel@tonic-gate formp->conttype = Strdup(DCONTYP);
101*0Sstevel@tonic-gate formp->paper = NULL;
102*0Sstevel@tonic-gate formp->isDefault = 0;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate /*
105*0Sstevel@tonic-gate * Read the file.
106*0Sstevel@tonic-gate */
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate #define FGETS(B,S,F) (linenum++, fdgets(B,S,F))
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate have_line_already = 0;
111*0Sstevel@tonic-gate found_alignment_pattern = 0;
112*0Sstevel@tonic-gate linenum = 0;
113*0Sstevel@tonic-gate errno = 0;
114*0Sstevel@tonic-gate while (!found_alignment_pattern
115*0Sstevel@tonic-gate && (have_line_already || FGETS(buf, BUFSIZ, fd))
116*0Sstevel@tonic-gate ) {
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gate int pos = strlen(buf) - 1;
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate
121*0Sstevel@tonic-gate have_line_already = 0;
122*0Sstevel@tonic-gate
123*0Sstevel@tonic-gate while (isspace(buf[pos]))
124*0Sstevel@tonic-gate buf[pos--] = 0;
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gate fld = _search_fheading(buf);
127*0Sstevel@tonic-gate if (fld >= FO_MAX) {
128*0Sstevel@tonic-gate lp_errno = LP_EBADHDR;
129*0Sstevel@tonic-gate BadFile: errno = EBADF;
130*0Sstevel@tonic-gate if (error_handler) {
131*0Sstevel@tonic-gate if ((*error_handler)(errno, lp_errno, linenum) == -1)
132*0Sstevel@tonic-gate return (-1);
133*0Sstevel@tonic-gate continue;
134*0Sstevel@tonic-gate } else {
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate * To allow future extensions to not
137*0Sstevel@tonic-gate * impact applications using old versions
138*0Sstevel@tonic-gate * of this routine, ignore strange fields.
139*0Sstevel@tonic-gate */
140*0Sstevel@tonic-gate continue;
141*0Sstevel@tonic-gate }
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate p = buf + formheadings[fld].len;
145*0Sstevel@tonic-gate while (isspace(*p))
146*0Sstevel@tonic-gate p++;
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate if (which_set)
149*0Sstevel@tonic-gate which_set[fld] = 1;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate if (
152*0Sstevel@tonic-gate formheadings[fld].infile
153*0Sstevel@tonic-gate || error_handler
154*0Sstevel@tonic-gate ) switch (fld) {
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate case FO_PLEN:
157*0Sstevel@tonic-gate sdn = getsdn(p);
158*0Sstevel@tonic-gate if (errno == EINVAL) {
159*0Sstevel@tonic-gate lp_errno = LP_EBADSDN;
160*0Sstevel@tonic-gate goto BadFile;
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate formp->plen = sdn;
163*0Sstevel@tonic-gate break;
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate case FO_PWID:
166*0Sstevel@tonic-gate sdn = getsdn(p);
167*0Sstevel@tonic-gate if (errno == EINVAL) {
168*0Sstevel@tonic-gate lp_errno = LP_EBADSDN;
169*0Sstevel@tonic-gate goto BadFile;
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate formp->pwid = sdn;
172*0Sstevel@tonic-gate break;
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate case FO_CPI:
175*0Sstevel@tonic-gate sdn = getcpi(p);
176*0Sstevel@tonic-gate if (errno == EINVAL) {
177*0Sstevel@tonic-gate lp_errno = LP_EBADSDN;
178*0Sstevel@tonic-gate goto BadFile;
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate formp->cpi = sdn;
181*0Sstevel@tonic-gate break;
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate case FO_LPI:
184*0Sstevel@tonic-gate sdn = getsdn(p);
185*0Sstevel@tonic-gate if (errno == EINVAL) {
186*0Sstevel@tonic-gate lp_errno = LP_EBADSDN;
187*0Sstevel@tonic-gate goto BadFile;
188*0Sstevel@tonic-gate }
189*0Sstevel@tonic-gate formp->lpi = sdn;
190*0Sstevel@tonic-gate break;
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate case FO_NP:
193*0Sstevel@tonic-gate if (
194*0Sstevel@tonic-gate (i = strtol(p, &rest, 10)) <= 0
195*0Sstevel@tonic-gate || *rest
196*0Sstevel@tonic-gate ) {
197*0Sstevel@tonic-gate lp_errno = LP_EBADINT;
198*0Sstevel@tonic-gate goto BadFile;
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate formp->np = i;
201*0Sstevel@tonic-gate break;
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate case FO_CHSET:
204*0Sstevel@tonic-gate if (!(mandp = strchr(p, ',')))
205*0Sstevel@tonic-gate formp->mandatory = 0;
206*0Sstevel@tonic-gate else {
207*0Sstevel@tonic-gate do
208*0Sstevel@tonic-gate *mandp++ = 0;
209*0Sstevel@tonic-gate while (*mandp && isspace(*mandp));
210*0Sstevel@tonic-gate if (CS_STREQU(MANSTR, mandp))
211*0Sstevel@tonic-gate formp->mandatory = 1;
212*0Sstevel@tonic-gate else {
213*0Sstevel@tonic-gate lp_errno = LP_EBADARG;
214*0Sstevel@tonic-gate goto BadFile;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate }
217*0Sstevel@tonic-gate if (!syn_name(p)) {
218*0Sstevel@tonic-gate lp_errno = LP_EBADNAME;
219*0Sstevel@tonic-gate goto BadFile;
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate if (formp->chset)
222*0Sstevel@tonic-gate Free (formp->chset);
223*0Sstevel@tonic-gate formp->chset = Strdup(p);
224*0Sstevel@tonic-gate break;
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate case FO_RCOLOR:
227*0Sstevel@tonic-gate if (formp->rcolor)
228*0Sstevel@tonic-gate Free (formp->rcolor);
229*0Sstevel@tonic-gate formp->rcolor = Strdup(p);
230*0Sstevel@tonic-gate break;
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate case FO_CMT:
233*0Sstevel@tonic-gate if (*p) {
234*0Sstevel@tonic-gate lp_errno = LP_ETRAILIN;
235*0Sstevel@tonic-gate goto BadFile;
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate if (formp->comment)
238*0Sstevel@tonic-gate Free (formp->comment);
239*0Sstevel@tonic-gate formp->comment = 0;
240*0Sstevel@tonic-gate size = 0;
241*0Sstevel@tonic-gate while (FGETS(buf, BUFSIZ, fd)) {
242*0Sstevel@tonic-gate p = buf;
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate * A recognized header ends the comment.
246*0Sstevel@tonic-gate */
247*0Sstevel@tonic-gate if (_search_fheading(p) < FO_MAX) {
248*0Sstevel@tonic-gate have_line_already = 1;
249*0Sstevel@tonic-gate break;
250*0Sstevel@tonic-gate }
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate * On the other hand, a '>' may hide what
254*0Sstevel@tonic-gate * would otherwise look like a header.
255*0Sstevel@tonic-gate */
256*0Sstevel@tonic-gate if (
257*0Sstevel@tonic-gate p[0] == '>'
258*0Sstevel@tonic-gate && _search_fheading(p+1) < FO_MAX
259*0Sstevel@tonic-gate )
260*0Sstevel@tonic-gate p++;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate * (Re)allocate space to hold this
264*0Sstevel@tonic-gate * (additional) line of the comment.
265*0Sstevel@tonic-gate */
266*0Sstevel@tonic-gate add_size = strlen(p);
267*0Sstevel@tonic-gate if (formp->comment)
268*0Sstevel@tonic-gate formp->comment = Realloc(
269*0Sstevel@tonic-gate formp->comment,
270*0Sstevel@tonic-gate size + add_size + 1
271*0Sstevel@tonic-gate );
272*0Sstevel@tonic-gate else
273*0Sstevel@tonic-gate formp->comment = Malloc(
274*0Sstevel@tonic-gate size + add_size + 1
275*0Sstevel@tonic-gate );
276*0Sstevel@tonic-gate if (!formp->comment) {
277*0Sstevel@tonic-gate freeform (formp);
278*0Sstevel@tonic-gate close(fd);
279*0Sstevel@tonic-gate errno = ENOMEM;
280*0Sstevel@tonic-gate return (-1);
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate /*
284*0Sstevel@tonic-gate * Copy this (additional) line of the
285*0Sstevel@tonic-gate * comment to the allocated space. "here"
286*0Sstevel@tonic-gate * points to where to copy the line.
287*0Sstevel@tonic-gate */
288*0Sstevel@tonic-gate strcpy (formp->comment + size, p);
289*0Sstevel@tonic-gate size += add_size;
290*0Sstevel@tonic-gate }
291*0Sstevel@tonic-gate if (errno != 0)
292*0Sstevel@tonic-gate goto BadFile;
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate /*
295*0Sstevel@tonic-gate * The comment is held internally without a
296*0Sstevel@tonic-gate * trailing newline.
297*0Sstevel@tonic-gate */
298*0Sstevel@tonic-gate if (size && formp->comment[size - 1] == '\n')
299*0Sstevel@tonic-gate formp->comment[size - 1] = 0;
300*0Sstevel@tonic-gate
301*0Sstevel@tonic-gate break;
302*0Sstevel@tonic-gate
303*0Sstevel@tonic-gate case FO_ALIGN:
304*0Sstevel@tonic-gate if (*p) {
305*0Sstevel@tonic-gate if (!syn_type(p)) {
306*0Sstevel@tonic-gate lp_errno = LP_EBADCTYPE;
307*0Sstevel@tonic-gate goto BadFile;
308*0Sstevel@tonic-gate }
309*0Sstevel@tonic-gate if (formp->conttype)
310*0Sstevel@tonic-gate Free (formp->conttype);
311*0Sstevel@tonic-gate formp->conttype = Strdup(p);
312*0Sstevel@tonic-gate }
313*0Sstevel@tonic-gate
314*0Sstevel@tonic-gate /*
315*0Sstevel@tonic-gate * Actual alignment pattern has to be read in
316*0Sstevel@tonic-gate * by caller; we leave the file pointer ready.
317*0Sstevel@tonic-gate */
318*0Sstevel@tonic-gate found_alignment_pattern = 1;
319*0Sstevel@tonic-gate break;
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate case FO_PAPER:
322*0Sstevel@tonic-gate if (!(dftp = strchr(p, ',')))
323*0Sstevel@tonic-gate formp->isDefault = 0;
324*0Sstevel@tonic-gate else {
325*0Sstevel@tonic-gate do
326*0Sstevel@tonic-gate *dftp++ = 0;
327*0Sstevel@tonic-gate while (*dftp && isspace(*dftp));
328*0Sstevel@tonic-gate if (CS_STREQU(DFTSTR, dftp))
329*0Sstevel@tonic-gate formp->isDefault = 1;
330*0Sstevel@tonic-gate else {
331*0Sstevel@tonic-gate lp_errno = LP_EBADARG;
332*0Sstevel@tonic-gate goto BadFile;
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate if (!syn_name(p)) {
336*0Sstevel@tonic-gate lp_errno = LP_EBADNAME;
337*0Sstevel@tonic-gate goto BadFile;
338*0Sstevel@tonic-gate }
339*0Sstevel@tonic-gate if (formp->paper)
340*0Sstevel@tonic-gate Free (formp->paper);
341*0Sstevel@tonic-gate formp->paper = Strdup(p);
342*0Sstevel@tonic-gate break;
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate }
346*0Sstevel@tonic-gate if (errno != 0) {
347*0Sstevel@tonic-gate int save_errno = errno;
348*0Sstevel@tonic-gate
349*0Sstevel@tonic-gate freeform (formp);
350*0Sstevel@tonic-gate errno = save_errno;
351*0Sstevel@tonic-gate return (-1);
352*0Sstevel@tonic-gate }
353*0Sstevel@tonic-gate
354*0Sstevel@tonic-gate /*
355*0Sstevel@tonic-gate * Get the form description (if it exists) (?)
356*0Sstevel@tonic-gate */
357*0Sstevel@tonic-gate if (!error_handler) {
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gate char * path;
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate
362*0Sstevel@tonic-gate if (!(path = getformfile(name, COMMENTFILE))) {
363*0Sstevel@tonic-gate freeform (formp);
364*0Sstevel@tonic-gate errno = ENOMEM;
365*0Sstevel@tonic-gate return (-1);
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate if (
368*0Sstevel@tonic-gate !(formp->comment = loadstring(path))
369*0Sstevel@tonic-gate && errno != ENOENT
370*0Sstevel@tonic-gate ) {
371*0Sstevel@tonic-gate Free (path);
372*0Sstevel@tonic-gate freeform (formp);
373*0Sstevel@tonic-gate return (-1);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate Free (path);
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate return (0);
379*0Sstevel@tonic-gate }
380