1 /* $NetBSD: setupterm.c,v 1.4 2013/06/07 13:16:18 roy Exp $ */
4 * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: setupterm.c,v 1.4 2013/06/07 13:16:18 roy Exp $");
39 #include <term_private.h>
42 #define reterr(code, msg) \
45 errx(EXIT_FAILURE, msg); \
50 } while (0 /* CONSTCOND */)
52 #define reterrarg(code, msg, arg) \
55 errx(EXIT_FAILURE, msg, arg); \
60 } while (0 /* CONSTCOND */)
64 ti_setupterm(TERMINAL
**nterm
, const char *term
, int fildes
, int *errret
)
68 _DIAGASSERT(nterm
!= NULL
);
71 term
= getenv("TERM");
72 if (term
== NULL
|| *term
== '\0') {
74 reterr(0, "TERM environment variable not set");
76 if (fildes
== STDOUT_FILENO
&& !isatty(fildes
))
77 fildes
= STDERR_FILENO
;
79 *nterm
= calloc(1, sizeof(**nterm
));
81 reterr(-1, "not enough memory to create terminal structure");
83 error
= _ti_getterm(*nterm
, term
, 0);
89 reterr(error
, "cannot access the terminfo database");
93 "%s: terminal not listed in terminfo datase",
97 reterr(-1, "unknown error");
102 (*nterm
)->fildes
= fildes
;
103 _ti_setospeed(*nterm
);
104 if (t_generic_type(*nterm
))
105 reterrarg(0, "%s: generic terminal", term
);
106 if (t_hard_copy(*nterm
))
107 reterrarg(1, "%s: hardcopy terminal", term
);
108 /* POSIX requires 1 for success */
115 setupterm(const char *term
, int fildes
, int *errret
)
122 ret
= ti_setupterm(&nterm
, term
, fildes
, errret
);