xref: /illumos-gate/usr/src/cmd/lp/lib/forms/wrform.c (revision 4eaa471005973e11a6110b69fe990530b3b95a38)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.8	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "sys/types.h"
35 #include "sys/stat.h"
36 #include "stdio.h"
37 #include "string.h"
38 #include "errno.h"
39 #include "stdlib.h"
40 
41 #include "lp.h"
42 #include "form.h"
43 
44 extern struct {
45 	char			*v;
46 	short			len;
47 	short			infile;
48 }			formheadings[];
49 
50 int		_search_fheading ( char * );
51 
52 static void	print_sdn(int, char *, SCALED);
53 static void	print_str(int, char *, char *);
54 
55 /**
56  ** wrform()
57  **/
58 
59 int
60 wrform(char *name, FORM *formp, int fd, int (*error_handler)( int , int , int ),
61 	int *which_set)
62 {
63 	int			fld;
64 
65 	char *			cp;
66 
67 
68 	errno = 0;
69 	for (fld = 0; fld < FO_MAX; fld++)
70 	  if ((!which_set || which_set[fld]) &&
71 	      (formheadings[fld].infile || error_handler))
72 		switch (fld) {
73 
74 #define HEAD	formheadings[fld].v
75 
76 		case FO_PLEN:
77 			print_sdn(fd, HEAD, formp->plen);
78 			break;
79 
80 		case FO_PWID:
81 			print_sdn(fd, HEAD, formp->pwid);
82 			break;
83 
84 		case FO_LPI:
85 			print_sdn(fd, HEAD, formp->lpi);
86 			break;
87 
88 		case FO_CPI:
89 			if (formp->cpi.val == N_COMPRESSED)
90 				print_str(fd, HEAD, NAME_COMPRESSED);
91 			else
92 				print_sdn(fd, HEAD, formp->cpi);
93 			break;
94 
95 		case FO_NP:
96 			fdprintf(fd, "%s %d\n", HEAD, formp->np);
97 			break;
98 
99 		case FO_CHSET:
100 			fdprintf(fd, "%s %s", HEAD, formp->chset);
101 			if (formp->mandatory == 1)
102 				fdprintf(fd, ",%s", MANSTR);
103 			fdprintf(fd, "\n");
104 			break;
105 
106 		case FO_RCOLOR:
107 			print_str(fd, HEAD, formp->rcolor);
108 			break;
109 
110 		case FO_CMT:
111 			if ((cp = formp->comment) && *cp) {
112 				fdprintf(fd, "%s\n", HEAD);
113 				do {
114 					char *	nl = strchr(cp, '\n');
115 
116 					if (nl)
117 						*nl = 0;
118 					if (_search_fheading(cp) < FO_MAX)
119 						fdputc ('>', fd);
120 					fdprintf(fd, "%s\n", cp);
121 					if (nl)
122 						*nl = '\n';
123 					cp = nl;
124 				} while (cp++);	/* NOT *cp++ */
125 			}
126 			break;
127 
128 		case FO_ALIGN:
129 			  /* this must always be the last field in the file
130 				  it is done outside of this loop */
131 			break;
132 
133 		case FO_PAPER:
134 			if (formp->paper) {
135 				fdprintf(fd, "%s %s", HEAD, formp->paper);
136 				if (formp->isDefault == 1)
137 					fdprintf(fd, ",%s", DFTSTR);
138 				fdprintf(fd, "\n");
139 			}
140 			break;
141 
142 		}
143 
144 	if ((!which_set || which_set[FO_ALIGN]) &&
145 	    (formheadings[FO_ALIGN].infile || error_handler)) {
146 		  print_str(fd, formheadings[FO_ALIGN].v, formp->conttype);
147 			/*
148 			 * Actual alignment pattern has to be written
149 			 * out by caller; we leave the file pointer ready.
150 			 */
151 	}
152 
153 	if (errno != 0)
154 		return (-1);
155 
156 	/*
157 	 * Write out comment to a separate file (?)
158 	 */
159 	if (!error_handler) {
160 
161 		char *			path;
162 
163 
164 		if (!(path = getformfile(name, COMMENT)))
165 			return (-1);
166 
167 		if (formp->comment) {
168 			if (dumpstring(path, formp->comment) == -1) {
169 				Free (path);
170 				return (-1);
171 			}
172 
173 		} else
174 			Unlink (path);
175 
176 		Free (path);
177 
178 	}
179 
180 	return (0);
181 }
182 
183 /**
184  ** print_sdn() - PRINT SCALED DECIMAL NUMBER WITH HEADER
185  ** print_str() - PRINT STRING WITH HEADER
186  **/
187 
188 static void
189 print_sdn(int fd, char *head, SCALED sdn)
190 {
191 	if (sdn.val <= 0)
192 		return;
193 
194 	(void)fdprintf(fd, "%s ", head);
195 	fdprintsdn(fd, sdn);
196 
197 	return;
198 }
199 
200 static void
201 print_str(int fd, char *head, char *str)
202 {
203 	if (!str || !*str)
204 		return;
205 
206 	(void)fdprintf(fd, "%s %s\n", head, str);
207 
208 	return;
209 }
210