xref: /illumos-gate/usr/src/cmd/lp/lib/lp/set_charset.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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.11	*/
32 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33 
34 #include "stdio.h"
35 #include "string.h"
36 #include "errno.h"
37 #include "stdlib.h"
38 
39 #include "lp.h"
40 #include "lp.set.h"
41 
42 #if	defined(__STDC__)
43 char *			tparm ( char * , ... );
44 #else
45 extern char		*tparm();
46 #endif
47 
48 #if	defined(__STDC__)
49 static int		cat_charset ( char *, int , char * , int );
50 #else
51 static int		cat_charset();
52 #endif
53 
54 /**
55  ** set_charset()
56  **/
57 
58 int
59 #if	defined(__STDC__)
60 set_charset (
61 	char *			char_set,
62 	int			putout,
63 	char *			type
64 )
65 #else
66 set_charset (char_set, putout, type)
67 	char			*char_set;
68 	int			putout;
69 	char			*type;
70 #endif
71 {
72 	int			cs,
73 				ret;
74 
75 	char			*rest,
76 				*char_set_nm,
77 				*char_set_names,
78 				*select_char_set,
79 				*start_char_set_def,
80 				*p,
81 				*q;
82 
83 	unsigned short		has_print_wheel;
84 
85 
86 	tidbit ((char *)0, "daisy", &has_print_wheel);
87 	if (has_print_wheel)
88 		return (E_SUCCESS);
89 
90 	tidbit ((char *)0, "csnm", &char_set_names);
91 	if (
92 		strlen(char_set) > (size_t) 2
93 	     && char_set[0] == 'c'
94 	     && char_set[1] == 's'
95 	     && char_set[2]
96 	     && 0 <= (cs = strtol(char_set + 2, &rest, 10)) && cs <= 63
97 	     && !*rest
98 	)
99 		char_set_nm = tparm(char_set_names, cs);
100 
101 	else {
102 		for (cs = 0; cs <= 63; cs++)
103 			if (
104 				(char_set_nm = tparm(char_set_names, cs))
105 			     && *char_set_nm
106 			     && STREQU(char_set_nm, char_set)
107 			)
108 				break;
109 		if (cs > 63)
110 			return (E_FAILURE);
111 	}
112 
113 	if (cs == 0)
114 		return (E_SUCCESS);
115 
116 	if (char_set_nm)
117 		if (!(char_set_nm = Strdup(char_set_nm))) {
118 			errno = ENOMEM;
119 			ret = E_FAILURE;
120 			goto Return;
121 		}
122 
123 	tidbit ((char *)0, "scs", &select_char_set);
124 	p = q = 0;
125 	if ((p = tparm(select_char_set, cs)) && *p && (p = Strdup(p))) {
126 
127 		tidbit ((char *)0, "scsd", &start_char_set_def);
128 		if ((q = tparm(start_char_set_def, cs)) && *q) {
129 			/*
130 			 * The ``start char. set def'n.'' capability
131 			 * is defined and set, so assume we MUST
132 			 * download the character set before using it.
133 			 */
134 			if (
135 				OKAY(char_set_nm)
136 			     && cat_charset(char_set_nm, 0, type, putout) != -1
137 			     || cat_charset((char *)0, cs, type, putout) != -1
138 			     || cat_charset(char_set, 0, type, putout) != -1
139 			)
140 				;
141 			else {
142 				ret = E_FAILURE;
143 				goto Return;
144 			}
145 
146 		} else {
147 			/*
148 			 * The ``start char. set def'n.'' capability
149 			 * is not defined and or set, so assume we MAY
150 			 * download the character set before using it.
151 			 */
152 			if (
153 				OKAY(char_set_nm)
154 			     && cat_charset(char_set_nm, 0, type, putout) != -1
155 			     || cat_charset((char *)0, cs, type, putout) != -1
156 			     || cat_charset(char_set, 0, type, putout) != -1
157 			)
158 				;
159 		}
160 
161 		if (putout)
162 			putp (p);
163 		ret = E_SUCCESS;
164 
165 	} else
166 		ret = E_FAILURE;
167 
168 Return:	if (p)
169 		Free (p);
170 	if (q)
171 		Free (q);
172 	if (char_set_nm)
173 		Free (char_set_nm);
174 	return (ret);
175 }
176 
177 /**
178  ** cat_charset() - DUMP CONTENT OF CHARACTER SET DEF'N FILE
179  **/
180 
181 static int
182 #if	defined(__STDC__)
183 cat_charset (
184 	char *			name,
185 	int			number,
186 	char *			type,
187 	int			putout
188 )
189 #else
190 cat_charset (name, number, type, putout)
191 	char			*name;
192 	int			number,
193 				putout;
194 	char			*type;
195 #endif
196 {
197 	int fd;
198 
199 	char			*p,
200 				*parent,
201 				*T,
202 				buf[BUFSIZ];
203 
204 	int			n,
205 				ret;
206 
207 	if (!name)
208 		sprintf ((name = "63"), "%d", number);
209 
210 	if (!(parent = getenv("CHARSETDIR")))
211 		parent = CHARSETDIR;
212 
213 	(T = "x")[0] = type[0];
214 	p = makepath(parent, T, type, name, (char *)0);
215 	if (!p)
216 		return (-1);
217 	if ((fd = open_locked(p, "r", 0)) < 0) {
218 		Free (p);
219 		return (-1);
220 	}
221 	Free (p);
222 
223 	if (putout) {
224 
225 		errno = 0;
226 		while ((n = read(fd, buf, BUFSIZ)))
227 			fwrite (buf, 1, n, stdout);
228 
229 		if (errno != 0)
230 			ret = -1;
231 		else
232 			ret = 0;
233 
234 	}
235 	close(fd);
236 	return (ret);
237 }
238