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.8 */
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate #include "sys/types.h"
35*0Sstevel@tonic-gate #include "sys/stat.h"
36*0Sstevel@tonic-gate #include "stdio.h"
37*0Sstevel@tonic-gate #include "string.h"
38*0Sstevel@tonic-gate #include "errno.h"
39*0Sstevel@tonic-gate #include "stdlib.h"
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate #include "lp.h"
42*0Sstevel@tonic-gate #include "form.h"
43*0Sstevel@tonic-gate
44*0Sstevel@tonic-gate extern struct {
45*0Sstevel@tonic-gate char *v;
46*0Sstevel@tonic-gate short len;
47*0Sstevel@tonic-gate short infile;
48*0Sstevel@tonic-gate } formheadings[];
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate int _search_fheading ( char * );
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate static void print_sdn(int, char *, SCALED);
53*0Sstevel@tonic-gate static void print_str(int, char *, char *);
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /**
56*0Sstevel@tonic-gate ** wrform()
57*0Sstevel@tonic-gate **/
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate int
wrform(char * name,FORM * formp,int fd,int (* error_handler)(int,int,int),int * which_set)60*0Sstevel@tonic-gate wrform(char *name, FORM *formp, int fd, int (*error_handler)( int , int , int ),
61*0Sstevel@tonic-gate int *which_set)
62*0Sstevel@tonic-gate {
63*0Sstevel@tonic-gate int fld;
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate char * cp;
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate errno = 0;
69*0Sstevel@tonic-gate for (fld = 0; fld < FO_MAX; fld++)
70*0Sstevel@tonic-gate if ((!which_set || which_set[fld]) &&
71*0Sstevel@tonic-gate (formheadings[fld].infile || error_handler))
72*0Sstevel@tonic-gate switch (fld) {
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate #define HEAD formheadings[fld].v
75*0Sstevel@tonic-gate
76*0Sstevel@tonic-gate case FO_PLEN:
77*0Sstevel@tonic-gate print_sdn(fd, HEAD, formp->plen);
78*0Sstevel@tonic-gate break;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate case FO_PWID:
81*0Sstevel@tonic-gate print_sdn(fd, HEAD, formp->pwid);
82*0Sstevel@tonic-gate break;
83*0Sstevel@tonic-gate
84*0Sstevel@tonic-gate case FO_LPI:
85*0Sstevel@tonic-gate print_sdn(fd, HEAD, formp->lpi);
86*0Sstevel@tonic-gate break;
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate case FO_CPI:
89*0Sstevel@tonic-gate if (formp->cpi.val == N_COMPRESSED)
90*0Sstevel@tonic-gate print_str(fd, HEAD, NAME_COMPRESSED);
91*0Sstevel@tonic-gate else
92*0Sstevel@tonic-gate print_sdn(fd, HEAD, formp->cpi);
93*0Sstevel@tonic-gate break;
94*0Sstevel@tonic-gate
95*0Sstevel@tonic-gate case FO_NP:
96*0Sstevel@tonic-gate fdprintf(fd, "%s %d\n", HEAD, formp->np);
97*0Sstevel@tonic-gate break;
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate case FO_CHSET:
100*0Sstevel@tonic-gate fdprintf(fd, "%s %s", HEAD, formp->chset);
101*0Sstevel@tonic-gate if (formp->mandatory == 1)
102*0Sstevel@tonic-gate fdprintf(fd, ",%s", MANSTR);
103*0Sstevel@tonic-gate fdprintf(fd, "\n");
104*0Sstevel@tonic-gate break;
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate case FO_RCOLOR:
107*0Sstevel@tonic-gate print_str(fd, HEAD, formp->rcolor);
108*0Sstevel@tonic-gate break;
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate case FO_CMT:
111*0Sstevel@tonic-gate if ((cp = formp->comment) && *cp) {
112*0Sstevel@tonic-gate fdprintf(fd, "%s\n", HEAD);
113*0Sstevel@tonic-gate do {
114*0Sstevel@tonic-gate char * nl = strchr(cp, '\n');
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate if (nl)
117*0Sstevel@tonic-gate *nl = 0;
118*0Sstevel@tonic-gate if (_search_fheading(cp) < FO_MAX)
119*0Sstevel@tonic-gate fdputc ('>', fd);
120*0Sstevel@tonic-gate fdprintf(fd, "%s\n", cp);
121*0Sstevel@tonic-gate if (nl)
122*0Sstevel@tonic-gate *nl = '\n';
123*0Sstevel@tonic-gate cp = nl;
124*0Sstevel@tonic-gate } while (cp++); /* NOT *cp++ */
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate break;
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate case FO_ALIGN:
129*0Sstevel@tonic-gate /* this must always be the last field in the file
130*0Sstevel@tonic-gate it is done outside of this loop */
131*0Sstevel@tonic-gate break;
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate case FO_PAPER:
134*0Sstevel@tonic-gate if (formp->paper) {
135*0Sstevel@tonic-gate fdprintf(fd, "%s %s", HEAD, formp->paper);
136*0Sstevel@tonic-gate if (formp->isDefault == 1)
137*0Sstevel@tonic-gate fdprintf(fd, ",%s", DFTSTR);
138*0Sstevel@tonic-gate fdprintf(fd, "\n");
139*0Sstevel@tonic-gate }
140*0Sstevel@tonic-gate break;
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate if ((!which_set || which_set[FO_ALIGN]) &&
145*0Sstevel@tonic-gate (formheadings[FO_ALIGN].infile || error_handler)) {
146*0Sstevel@tonic-gate print_str(fd, formheadings[FO_ALIGN].v, formp->conttype);
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate * Actual alignment pattern has to be written
149*0Sstevel@tonic-gate * out by caller; we leave the file pointer ready.
150*0Sstevel@tonic-gate */
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate
153*0Sstevel@tonic-gate if (errno != 0)
154*0Sstevel@tonic-gate return (-1);
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate /*
157*0Sstevel@tonic-gate * Write out comment to a separate file (?)
158*0Sstevel@tonic-gate */
159*0Sstevel@tonic-gate if (!error_handler) {
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate char * path;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate if (!(path = getformfile(name, COMMENT)))
165*0Sstevel@tonic-gate return (-1);
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate if (formp->comment) {
168*0Sstevel@tonic-gate if (dumpstring(path, formp->comment) == -1) {
169*0Sstevel@tonic-gate Free (path);
170*0Sstevel@tonic-gate return (-1);
171*0Sstevel@tonic-gate }
172*0Sstevel@tonic-gate
173*0Sstevel@tonic-gate } else
174*0Sstevel@tonic-gate Unlink (path);
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate Free (path);
177*0Sstevel@tonic-gate
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate return (0);
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gate /**
184*0Sstevel@tonic-gate ** print_sdn() - PRINT SCALED DECIMAL NUMBER WITH HEADER
185*0Sstevel@tonic-gate ** print_str() - PRINT STRING WITH HEADER
186*0Sstevel@tonic-gate **/
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate static void
print_sdn(int fd,char * head,SCALED sdn)189*0Sstevel@tonic-gate print_sdn(int fd, char *head, SCALED sdn)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate if (sdn.val <= 0)
192*0Sstevel@tonic-gate return;
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate (void)fdprintf(fd, "%s ", head);
195*0Sstevel@tonic-gate fdprintsdn(fd, sdn);
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate return;
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate
200*0Sstevel@tonic-gate static void
print_str(int fd,char * head,char * str)201*0Sstevel@tonic-gate print_str(int fd, char *head, char *str)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate if (!str || !*str)
204*0Sstevel@tonic-gate return;
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate (void)fdprintf(fd, "%s %s\n", head, str);
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate return;
209*0Sstevel@tonic-gate }
210