| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: setupterm.c,v 1.5 2017/03/23 00:23:29 roy Exp $ */ | | 1 | /* $NetBSD: setupterm.c,v 1.6 2017/03/23 00:36:37 roy Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2009, 2011 The NetBSD Foundation, Inc. |
5 | * | | 5 | * |
6 | * This code is derived from software contributed to The NetBSD Foundation | | 6 | * This code is derived from software contributed to The NetBSD Foundation |
7 | * by Roy Marples. | | 7 | * by Roy Marples. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -18,79 +18,62 @@ | | | @@ -18,79 +18,62 @@ |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | | 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 | | 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | | 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | | 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 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 | | 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 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 | | 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. | | 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ | | 28 | */ |
29 | | | 29 | |
30 | #include <sys/cdefs.h> | | 30 | #include <sys/cdefs.h> |
31 | __RCSID("$NetBSD: setupterm.c,v 1.5 2017/03/23 00:23:29 roy Exp $"); | | 31 | __RCSID("$NetBSD: setupterm.c,v 1.6 2017/03/23 00:36:37 roy Exp $"); |
32 | | | 32 | |
33 | #include <sys/ioctl.h> | | | |
34 | #include <assert.h> | | 33 | #include <assert.h> |
35 | #include <err.h> | | 34 | #include <err.h> |
36 | #include <stdbool.h> | | | |
37 | #include <stdio.h> | | 35 | #include <stdio.h> |
38 | #include <stdlib.h> | | 36 | #include <stdlib.h> |
39 | #include <strings.h> | | 37 | #include <strings.h> |
40 | #include <unistd.h> | | 38 | #include <unistd.h> |
41 | #include <term_private.h> | | 39 | #include <term_private.h> |
42 | #include <term.h> | | 40 | #include <term.h> |
43 | | | 41 | |
44 | /* | | | |
45 | * use_env is really a curses function - POSIX mandates it's in curses.h | | | |
46 | * But it has to live in terminfo because it must precede a call to setupterm(). | | | |
47 | */ | | | |
48 | #include <curses.h> | | | |
49 | | | | |
50 | static bool __use_env = true; | | | |
51 | | | | |
52 | void | | | |
53 | use_env(bool value) | | | |
54 | { | | | |
55 | | | | |
56 | __use_env = value; | | | |
57 | } | | | |
58 | #define reterr(code, msg) \ | | 42 | #define reterr(code, msg) \ |
59 | do { \ | | 43 | do { \ |
60 | if (errret == NULL) \ | | 44 | if (errret == NULL) \ |
61 | errx(EXIT_FAILURE, msg); \ | | 45 | errx(EXIT_FAILURE, msg); \ |
62 | else { \ | | 46 | else { \ |
63 | *errret = code; \ | | 47 | *errret = code; \ |
64 | return ERR; \ | | 48 | return ERR; \ |
65 | } \ | | 49 | } \ |
66 | } while (0 /* CONSTCOND */) | | 50 | } while (0 /* CONSTCOND */) |
67 | | | 51 | |
68 | #define reterrarg(code, msg, arg) \ | | 52 | #define reterrarg(code, msg, arg) \ |
69 | do { \ | | 53 | do { \ |
70 | if (errret == NULL) \ | | 54 | if (errret == NULL) \ |
71 | errx(EXIT_FAILURE, msg, arg); \ | | 55 | errx(EXIT_FAILURE, msg, arg); \ |
72 | else { \ | | 56 | else { \ |
73 | *errret = code; \ | | 57 | *errret = code; \ |
74 | return ERR; \ | | 58 | return ERR; \ |
75 | } \ | | 59 | } \ |
76 | } while (0 /* CONSTCOND */) | | 60 | } while (0 /* CONSTCOND */) |
77 | | | 61 | |
78 | | | 62 | |
79 | int | | 63 | int |
80 | ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret) | | 64 | ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret) |
81 | { | | 65 | { |
82 | int error; | | 66 | int error; |
83 | struct winsize win; | | | |
84 | | | 67 | |
85 | _DIAGASSERT(nterm != NULL); | | 68 | _DIAGASSERT(nterm != NULL); |
86 | | | 69 | |
87 | if (term == NULL) | | 70 | if (term == NULL) |
88 | term = getenv("TERM"); | | 71 | term = getenv("TERM"); |
89 | if (term == NULL || *term == '\0') { | | 72 | if (term == NULL || *term == '\0') { |
90 | *nterm = NULL; | | 73 | *nterm = NULL; |
91 | reterr(0, "TERM environment variable not set"); | | 74 | reterr(0, "TERM environment variable not set"); |
92 | } | | 75 | } |
93 | if (fildes == STDOUT_FILENO && !isatty(fildes)) | | 76 | if (fildes == STDOUT_FILENO && !isatty(fildes)) |
94 | fildes = STDERR_FILENO; | | 77 | fildes = STDERR_FILENO; |
95 | | | 78 | |
96 | *nterm = calloc(1, sizeof(**nterm)); | | 79 | *nterm = calloc(1, sizeof(**nterm)); |
| @@ -112,45 +95,26 @@ ti_setupterm(TERMINAL **nterm, const cha | | | @@ -112,45 +95,26 @@ ti_setupterm(TERMINAL **nterm, const cha |
112 | /* NOTREACHED */ | | 95 | /* NOTREACHED */ |
113 | default: | | 96 | default: |
114 | reterr(-1, "unknown error"); | | 97 | reterr(-1, "unknown error"); |
115 | /* NOTREACHED */ | | 98 | /* NOTREACHED */ |
116 | } | | 99 | } |
117 | } | | 100 | } |
118 | | | 101 | |
119 | (*nterm)->fildes = fildes; | | 102 | (*nterm)->fildes = fildes; |
120 | _ti_setospeed(*nterm); | | 103 | _ti_setospeed(*nterm); |
121 | if (t_generic_type(*nterm)) | | 104 | if (t_generic_type(*nterm)) |
122 | reterrarg(0, "%s: generic terminal", term); | | 105 | reterrarg(0, "%s: generic terminal", term); |
123 | if (t_hard_copy(*nterm)) | | 106 | if (t_hard_copy(*nterm)) |
124 | reterrarg(1, "%s: hardcopy terminal", term); | | 107 | reterrarg(1, "%s: hardcopy terminal", term); |
125 | | | | |
126 | /* If TIOCGWINSZ works, then set initial lines and columns. */ | | | |
127 | if (ioctl(fildes, TIOCGWINSZ, &win) != -1 && | | | |
128 | win.ws_row != 0 && win.ws_col != 0) | | | |
129 | { | | | |
130 | t_lines(*nterm) = win.ws_row; | | | |
131 | t_columns(*nterm) = win.ws_col; | | | |
132 | } | | | |
133 | | | | |
134 | /* POSIX 1003.2 requires that the environment override. */ | | | |
135 | if (__use_env) { | | | |
136 | char *p; | | | |
137 | | | | |
138 | if ((p = getenv("LINES")) != NULL) | | | |
139 | t_lines(*nterm) = (int)strtol(p, NULL, 0); | | | |
140 | if ((p = getenv("COLUMNS")) != NULL) | | | |
141 | t_columns(*nterm) = (int)strtol(p, NULL, 0); | | | |
142 | } | | | |
143 | | | | |
144 | /* POSIX requires 1 for success */ | | 108 | /* POSIX requires 1 for success */ |
145 | if (errret) | | 109 | if (errret) |
146 | *errret = 1; | | 110 | *errret = 1; |
147 | return OK; | | 111 | return OK; |
148 | } | | 112 | } |
149 | | | 113 | |
150 | int | | 114 | int |
151 | setupterm(const char *term, int fildes, int *errret) | | 115 | setupterm(const char *term, int fildes, int *errret) |
152 | { | | 116 | { |
153 | TERMINAL *nterm; | | 117 | TERMINAL *nterm; |
154 | int ret; | | 118 | int ret; |
155 | | | 119 | |
156 | if (errret != NULL) | | 120 | if (errret != NULL) |