setupterm.c revision 1.1 1 /* $NetBSD: setupterm.c,v 1.1 2010/02/03 15:16:32 roy Exp $ */
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: setupterm.c,v 1.1 2010/02/03 15:16:32 roy Exp $");
32
33 #include <assert.h>
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <strings.h>
38 #include <unistd.h>
39 #include <term_private.h>
40 #include <term.h>
41
42 #define reterr(code, msg) \
43 do { \
44 if (errret == NULL) \
45 errx(EXIT_FAILURE, msg); \
46 else { \
47 *errret = code; \
48 return ERR; \
49 } \
50 } while (0 /* CONSTCOND */)
51
52 #define reterrarg(code, msg, arg) \
53 do { \
54 if (errret == NULL) \
55 errx(EXIT_FAILURE, msg, arg); \
56 else { \
57 *errret = code; \
58 return ERR; \
59 } \
60 } while (0 /* CONSTCOND */)
61
62
63 int
64 ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret)
65 {
66 int error;
67
68 _DIAGASSERT(nterm != NULL);
69
70 if (term == NULL)
71 term = getenv("TERM");
72 if (term == NULL || *term == '\0')
73 reterr(-1, "TERM environment variable not set");
74 if (fildes == STDOUT_FILENO && !isatty(fildes))
75 fildes = STDERR_FILENO;
76
77 *nterm = calloc(1, sizeof(**nterm));
78 if (*nterm == NULL)
79 reterr(-1, "not enough memory to create terminal structure");
80
81 error = _ti_getterm(*nterm, term, 0);
82 if (error != 1) {
83 free(*nterm);
84 *nterm = NULL;
85 switch (error) {
86 case -1:
87 reterr(error, "cannot access the terminfo database");
88 /* NOTREACHED */
89 case 0:
90 reterrarg(error,
91 "%s: terminal not listed in terminfo datase",
92 term);
93 /* NOTREACHED */
94 default:
95 reterr(-1, "unknown error");
96 /* NOTREACHED */
97 }
98 }
99
100 (*nterm)->fildes = fildes;
101 _ti_setospeed(*nterm);
102 if (t_generic_type(*nterm))
103 reterrarg(0, "%s: generic terminal", term);
104 if (t_hard_copy(*nterm))
105 reterrarg(1, "%s: hardcopy terminal", term);
106 /* POSIX requires 1 for success */
107 if (errret)
108 *errret = 1;
109 return OK;
110 }
111
112 int
113 setupterm(const char *term, int fildes, int *errret)
114 {
115 TERMINAL *nterm;
116 int ret;
117
118 if (errret != NULL)
119 *errret = ERR;
120 ret = ti_setupterm(&nterm, term, fildes, errret);
121 if (nterm != NULL)
122 set_curterm(nterm);
123 return ret;
124 }
125