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