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