tidbit.c (7c478bd9) tidbit.c (24da5b34)
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
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.
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance 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/*
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21/*
23 * Copyright 1997 Sun Microsystems, Inc. All rights reserved.
22 * Copyright 2007 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.12 */

--- 8 unchanged lines hidden (view full) ---

40#else
41#include "varargs.h"
42#endif
43
44#include "lp.h"
45
46extern char *boolnames[],
47 *numnames[],
23 * Use is subject to license terms.
24 */
25
26/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27/* All Rights Reserved */
28
29
30#pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.12 */

--- 8 unchanged lines hidden (view full) ---

39#else
40#include "varargs.h"
41#endif
42
43#include "lp.h"
44
45extern char *boolnames[],
46 *numnames[],
48 *stringnames[];
47 *strnames[];
49
50extern char *getenv();
51
48
49extern char *getenv();
50
52ushort tidbit_boolean = 0;
51ushort_t tidbit_boolean = 0;
53
54short tidbit_number = 0;
55
56char *tidbit_string = 0;
57
58#if defined(__STDC__)
52
53short tidbit_number = 0;
54
55char *tidbit_string = 0;
56
57#if defined(__STDC__)
59static int open_terminfo_file ( char * , char * );
58static int open_terminfo_file(char *, char *);
60#else
61static int open_terminfo_file();
62#endif
63
59#else
60static int open_terminfo_file();
61#endif
62
64/**
65 ** _Getsh() - GET TWO-BYTE SHORT FROM (char *) POINTER PORTABLY
66 **/
63/*
64 * _Getsh() - GET TWO-BYTE SHORT FROM "char *" POINTER PORTABLY
65 */
67
68/*
69 * "function" to get a short from a pointer. The short is in a standard
70 * format: two bytes, the first is the low order byte, the second is
71 * the high order byte (base 256). The only negative number allowed is
72 * -1, which is represented as 255, 255. This format happens to be the
73 * same as the hardware on the pdp-11 and vax, making it fast and
74 * convenient and small to do this on a pdp-11.
75 */
76
77#if vax || pdp11 || i386
78#define _Getsh(ip) (*((short *)((char *)(ip))))
79#endif /* vax || pdp11 || i386 */
80
81/*
82 * The following macro is partly due to Mike Laman, laman@sdcsvax
83 * NCR @ Torrey Pines. - Tony Hansen
84 */
85#if u3b || u3b15 || u3b2 || m68000 || sparc
66
67/*
68 * "function" to get a short from a pointer. The short is in a standard
69 * format: two bytes, the first is the low order byte, the second is
70 * the high order byte (base 256). The only negative number allowed is
71 * -1, which is represented as 255, 255. This format happens to be the
72 * same as the hardware on the pdp-11 and vax, making it fast and
73 * convenient and small to do this on a pdp-11.
74 */
75
76#if vax || pdp11 || i386
77#define _Getsh(ip) (*((short *)((char *)(ip))))
78#endif /* vax || pdp11 || i386 */
79
80/*
81 * The following macro is partly due to Mike Laman, laman@sdcsvax
82 * NCR @ Torrey Pines. - Tony Hansen
83 */
84#if u3b || u3b15 || u3b2 || m68000 || sparc
86#define _Getsh(ip) ((short) (*((unsigned char *) ip) | (*(ip+1) << 8)))
85#define _Getsh(ip) ((short)(*((unsigned char *) ip) | (*(ip+1) << 8)))
87#endif /* u3b || u3b15 || u3b2 || m68000 || sparc */
88
89#ifndef _Getsh
90/*
91 * Here is a more portable version, which does not assume byte ordering
92 * in shorts, sign extension, etc. It does assume that the C preprocessor
93 * does sign-extension the same as on the machine being compiled for.
94 * When ANSI C comes along, this should be changed to check <limits.h>
95 * to see if the low character value is negative.
96 */
97
98static int
99#if defined(__STDC__)
86#endif /* u3b || u3b15 || u3b2 || m68000 || sparc */
87
88#ifndef _Getsh
89/*
90 * Here is a more portable version, which does not assume byte ordering
91 * in shorts, sign extension, etc. It does assume that the C preprocessor
92 * does sign-extension the same as on the machine being compiled for.
93 * When ANSI C comes along, this should be changed to check <limits.h>
94 * to see if the low character value is negative.
95 */
96
97static int
98#if defined(__STDC__)
100_Getsh (
101 register char * p
99_Getsh(
100 register char *p
102)
103#else
101)
102#else
104_Getsh (p)
103_Getsh(p)
105 register char *p;
106#endif
107{
108 register int rv,
109 rv2;
110
111#if -1 == '\377' /* sign extension occurs */
112 rv = (*p++) & 0377;
113 rv2 = (*p) & 0377;
114#else /* -1 == '\377' */ /* no sign extension */
115 rv = *p++;
116 rv2 = *p;
117#endif /* -1 == '\377' */
118 if ((rv2 == 0377) && ((rv == 0377) || (rv == 0376)))
119 return (-1);
120 return (rv + (rv2 * 256));
121}
122#endif /* _Getsh */
123
104 register char *p;
105#endif
106{
107 register int rv,
108 rv2;
109
110#if -1 == '\377' /* sign extension occurs */
111 rv = (*p++) & 0377;
112 rv2 = (*p) & 0377;
113#else /* -1 == '\377' */ /* no sign extension */
114 rv = *p++;
115 rv2 = *p;
116#endif /* -1 == '\377' */
117 if ((rv2 == 0377) && ((rv == 0377) || (rv == 0376)))
118 return (-1);
119 return (rv + (rv2 * 256));
120}
121#endif /* _Getsh */
122
124#define MAX_TIDBS 32
123#define MAX_TIDBS 32
125
126static struct tidb {
127
128 int snames,
129 nbools,
130 nints,
131 nstrs;
132
133 char *term,
134 *tiebuf,
135 *boolean_offset,
136 *number_offset,
137 *string_offset,
138 *string_table;
139
140} tidbs[MAX_TIDBS + 1]; /* one for last ditch */
141
124
125static struct tidb {
126
127 int snames,
128 nbools,
129 nints,
130 nstrs;
131
132 char *term,
133 *tiebuf,
134 *boolean_offset,
135 *number_offset,
136 *string_offset,
137 *string_table;
138
139} tidbs[MAX_TIDBS + 1]; /* one for last ditch */
140
142/**
143 ** tidbit() - TERMINFO DATABASE LOOKUP
144 **/
141/*
142 * tidbit() - TERMINFO DATABASE LOOKUP
143 */
145
146/*
147 * Four forms of calling:
148 *
149 * tidbit ("term-type", "boolean-cap-name", &ushort)
150 * tidbit ("term-type", "numeric-cap-name", &short)
151 * tidbit ("term-type", "string-cap-name", &charstar)
152 * tidbit ("term-type", "any-cap-name", (char *)0)

--- 33 unchanged lines hidden (view full) ---

186 * ENOENT can't open Terminfo file for terminal type
187 * EBADF Terminfo file is corrupted
188 * ENOMEM malloc failed
189 */
190
191/*VARARGS2*/
192int
193#if defined(__STDC__)
144
145/*
146 * Four forms of calling:
147 *
148 * tidbit ("term-type", "boolean-cap-name", &ushort)
149 * tidbit ("term-type", "numeric-cap-name", &short)
150 * tidbit ("term-type", "string-cap-name", &charstar)
151 * tidbit ("term-type", "any-cap-name", (char *)0)

--- 33 unchanged lines hidden (view full) ---

185 * ENOENT can't open Terminfo file for terminal type
186 * EBADF Terminfo file is corrupted
187 * ENOMEM malloc failed
188 */
189
190/*VARARGS2*/
191int
192#if defined(__STDC__)
194tidbit (
195 char * term,
196 char * cap,
193tidbit(
194 char *term,
195 char *cap,
197 ...
198)
199#else
196 ...
197)
198#else
200tidbit (term, cap, va_alist)
199tidbit(term, cap, va_alist)
201 char *term,
202 *cap;
203 va_dcl
204#endif
205{
206 va_list ap;
207
208 int rc;

--- 12 unchanged lines hidden (view full) ---

221 term = last_term;
222 else {
223 term = getenv("TERM");
224 if (!term || !*term)
225 term = NAME_UNKNOWN;
226 }
227 if (term != last_term) {
228 if (last_term)
200 char *term,
201 *cap;
202 va_dcl
203#endif
204{
205 va_list ap;
206
207 int rc;

--- 12 unchanged lines hidden (view full) ---

220 term = last_term;
221 else {
222 term = getenv("TERM");
223 if (!term || !*term)
224 term = NAME_UNKNOWN;
225 }
226 if (term != last_term) {
227 if (last_term)
229 Free (last_term);
228 Free(last_term);
230 last_term = Strdup(term);
231 }
232
233 for (i = 0; i < MAX_TIDBS; i++)
234 if (tidbs[i].term && STREQU(tidbs[i].term, term)) {
235 pt = &tidbs[i];
236 break;
237 }

--- 24 unchanged lines hidden (view full) ---

262 tfd = -1;
263 if ((terminfo = getenv("TERMINFO")) && *terminfo)
264 tfd = open_terminfo_file(terminfo, term);
265#if defined(TERMINFO)
266 if (tfd < 0)
267 tfd = open_terminfo_file(TERMINFO, term);
268#endif
269 if (tfd >= 0)
229 last_term = Strdup(term);
230 }
231
232 for (i = 0; i < MAX_TIDBS; i++)
233 if (tidbs[i].term && STREQU(tidbs[i].term, term)) {
234 pt = &tidbs[i];
235 break;
236 }

--- 24 unchanged lines hidden (view full) ---

261 tfd = -1;
262 if ((terminfo = getenv("TERMINFO")) && *terminfo)
263 tfd = open_terminfo_file(terminfo, term);
264#if defined(TERMINFO)
265 if (tfd < 0)
266 tfd = open_terminfo_file(TERMINFO, term);
267#endif
268 if (tfd >= 0)
270 (void)Fstat (tfd, &statbuf);
269 (void) Fstat(tfd, &statbuf);
271
272 if (tfd < 0 || !statbuf.st_size) {
273 errno = ENOENT;
274 return (-1);
275 }
276
277 if (pt->tiebuf)
270
271 if (tfd < 0 || !statbuf.st_size) {
272 errno = ENOENT;
273 return (-1);
274 }
275
276 if (pt->tiebuf)
278 Free (pt->tiebuf);
277 Free(pt->tiebuf);
279 if (!(pt->tiebuf = Malloc(statbuf.st_size))) {
280 errno = ENOMEM;
281 return (-1);
282 }
283
284 n = Read(tfd, pt->tiebuf, statbuf.st_size);
278 if (!(pt->tiebuf = Malloc(statbuf.st_size))) {
279 errno = ENOMEM;
280 return (-1);
281 }
282
283 n = Read(tfd, pt->tiebuf, statbuf.st_size);
285 (void)Close (tfd);
284 (void) Close(tfd);
286 if (n <= 0 || n >= 4096 || _Getsh(pt->tiebuf) != 0432) {
285 if (n <= 0 || n >= 4096 || _Getsh(pt->tiebuf) != 0432) {
287 Free (pt->tiebuf);
286 Free(pt->tiebuf);
288 pt->tiebuf = 0;
289 errno = EBADF;
290 return (-1);
291 }
292
293 if (pt->term)
287 pt->tiebuf = 0;
288 errno = EBADF;
289 return (-1);
290 }
291
292 if (pt->term)
294 Free (pt->term);
293 Free(pt->term);
295 if (!(pt->term = Strdup(term))) {
294 if (!(pt->term = Strdup(term))) {
296 Free (pt->tiebuf);
295 Free(pt->tiebuf);
297 pt->tiebuf = 0;
298 errno = ENOMEM;
299 return (-1);
300 }
301
302 pt->snames = _Getsh(pt->tiebuf + 2);
303 pt->nbools = _Getsh(pt->tiebuf + 4);
304 pt->nints = _Getsh(pt->tiebuf + 6);

--- 9 unchanged lines hidden (view full) ---

314
315 pt->string_table = pt->string_offset + pt->nstrs * 2;
316
317 }
318
319 rc = 0;
320
321#if defined(__STDC__)
296 pt->tiebuf = 0;
297 errno = ENOMEM;
298 return (-1);
299 }
300
301 pt->snames = _Getsh(pt->tiebuf + 2);
302 pt->nbools = _Getsh(pt->tiebuf + 4);
303 pt->nints = _Getsh(pt->tiebuf + 6);

--- 9 unchanged lines hidden (view full) ---

313
314 pt->string_table = pt->string_offset + pt->nstrs * 2;
315
316 }
317
318 rc = 0;
319
320#if defined(__STDC__)
322 va_start (ap, cap);
321 va_start(ap, cap);
323#else
322#else
324 va_start (ap);
323 va_start(ap);
325#endif
326
327 if (!cap || !*cap)
328 ;
329
330 else if ((pp = wherelist(cap, boolnames))) {
324#endif
325
326 if (!cap || !*cap)
327 ;
328
329 else if ((pp = wherelist(cap, boolnames))) {
331 register ushort *ushort_p;
330 register ushort_t *ushort_p;
332
333 register char *ip;
334
335 register int index = pp - boolnames;
336
331
332 register char *ip;
333
334 register int index = pp - boolnames;
335
337 if (!(ushort_p = va_arg(ap, ushort *))) {
336 if (!(ushort_p = va_arg(ap, ushort_t *))) {
338 ushort_p = &tidbit_boolean;
339 rc = 1;
340 }
341
342 if (index >= pt->nbools)
343 *ushort_p = 0;
344 else {
345 ip = pt->boolean_offset + index;

--- 16 unchanged lines hidden (view full) ---

362 *short_p = -1;
363 else {
364 ip = pt->number_offset + index * 2;
365 *short_p = _Getsh(ip);
366 if (*short_p == -2)
367 *short_p = -1;
368 }
369
337 ushort_p = &tidbit_boolean;
338 rc = 1;
339 }
340
341 if (index >= pt->nbools)
342 *ushort_p = 0;
343 else {
344 ip = pt->boolean_offset + index;

--- 16 unchanged lines hidden (view full) ---

361 *short_p = -1;
362 else {
363 ip = pt->number_offset + index * 2;
364 *short_p = _Getsh(ip);
365 if (*short_p == -2)
366 *short_p = -1;
367 }
368
370 } else if ((pp = wherelist(cap, stringnames))) {
369 } else if ((pp = wherelist(cap, strnames))) {
371 register char **charstar_p;
372
373 register char *ip;
374
370 register char **charstar_p;
371
372 register char *ip;
373
375 register int index = pp - stringnames;
374 register int index = pp - strnames;
376
377 register short sindex;
378
379
380 if (!(charstar_p = va_arg(ap, char **))) {
381 charstar_p = &tidbit_string;
382 rc = 3;
383 }

--- 4 unchanged lines hidden (view full) ---

388 ip = pt->string_offset + index * 2;
389 if ((sindex = _Getsh(ip)) >= 0)
390 *charstar_p = pt->string_table + sindex;
391 else
392 *charstar_p = 0;
393 }
394 }
395
375
376 register short sindex;
377
378
379 if (!(charstar_p = va_arg(ap, char **))) {
380 charstar_p = &tidbit_string;
381 rc = 3;
382 }

--- 4 unchanged lines hidden (view full) ---

387 ip = pt->string_offset + index * 2;
388 if ((sindex = _Getsh(ip)) >= 0)
389 *charstar_p = pt->string_table + sindex;
390 else
391 *charstar_p = 0;
392 }
393 }
394
396 va_end (ap);
395 va_end(ap);
397 return (rc);
398}
399
396 return (rc);
397}
398
400/**
401 ** untidbit() - FREE SPACE ASSOCIATED WITH A TERMINFO ENTRY
402 **/
399/*
400 * untidbit() - FREE SPACE ASSOCIATED WITH A TERMINFO ENTRY
401 */
403
404void
405#if defined(__STDC__)
402
403void
404#if defined(__STDC__)
406untidbit (
407 char * term
405untidbit(
406 char *term
408)
409#else
407)
408#else
410untidbit (term)
409untidbit(term)
411 char *term;
412#endif
413{
414 register int i;
415
416
417 for (i = 0; i < MAX_TIDBS; i++)
418 if (tidbs[i].term && STREQU(tidbs[i].term, term)) {
419 if (tidbs[i].tiebuf) {
410 char *term;
411#endif
412{
413 register int i;
414
415
416 for (i = 0; i < MAX_TIDBS; i++)
417 if (tidbs[i].term && STREQU(tidbs[i].term, term)) {
418 if (tidbs[i].tiebuf) {
420 Free (tidbs[i].tiebuf);
419 Free(tidbs[i].tiebuf);
421 tidbs[i].tiebuf = 0;
422 }
420 tidbs[i].tiebuf = 0;
421 }
423 Free (tidbs[i].term);
422 Free(tidbs[i].term);
424 tidbs[i].term = 0;
425 break;
426 }
423 tidbs[i].term = 0;
424 break;
425 }
427 return;
428}
429
426}
427
430/**
431 ** open_terminfo_file() - OPEN FILE FOR TERM ENTRY
432 **/
428/*
429 * open_terminfo_file() - OPEN FILE FOR TERM ENTRY
430 */
433
434static int
435#if defined(__STDC__)
431
432static int
433#if defined(__STDC__)
436open_terminfo_file (
437 char * terminfo,
438 char * term
434open_terminfo_file(
435 char *terminfo,
436 char *term
439)
440#else
437)
438#else
441open_terminfo_file (terminfo, term)
439open_terminfo_file(terminfo, term)
442 char *terminfo,
443 *term;
444#endif
445{
446 char *first_letter = "X",
447 *path;
448
449 int fd;
450
451 first_letter[0] = term[0];
452 path = makepath(terminfo, first_letter, term, (char *)0);
453
454 /* start fix for bugid 1109709 */
455 if (path == NULL) {
440 char *terminfo,
441 *term;
442#endif
443{
444 char *first_letter = "X",
445 *path;
446
447 int fd;
448
449 first_letter[0] = term[0];
450 path = makepath(terminfo, first_letter, term, (char *)0);
451
452 /* start fix for bugid 1109709 */
453 if (path == NULL) {
456 return -1;
454 return (-1);
457 }
458 /* end fix for bugid 1109709 */
459
460 fd = Open(path, 0);
455 }
456 /* end fix for bugid 1109709 */
457
458 fd = Open(path, 0);
461 Free (path);
459 Free(path);
462 return (fd);
463}
460 return (fd);
461}