| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: setupterm.c,v 1.4 2013/06/07 13:16:18 roy Exp $ */ | | 1 | /* $NetBSD: setupterm.c,v 1.5 2017/03/23 00:23:29 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,62 +18,79 @@ | | | @@ -18,62 +18,79 @@ |
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.4 2013/06/07 13:16:18 roy Exp $"); | | 31 | __RCSID("$NetBSD: setupterm.c,v 1.5 2017/03/23 00:23:29 roy Exp $"); |
32 | | | 32 | |
| | | 33 | #include <sys/ioctl.h> |
33 | #include <assert.h> | | 34 | #include <assert.h> |
34 | #include <err.h> | | 35 | #include <err.h> |
| | | 36 | #include <stdbool.h> |
35 | #include <stdio.h> | | 37 | #include <stdio.h> |
36 | #include <stdlib.h> | | 38 | #include <stdlib.h> |
37 | #include <strings.h> | | 39 | #include <strings.h> |
38 | #include <unistd.h> | | 40 | #include <unistd.h> |
39 | #include <term_private.h> | | 41 | #include <term_private.h> |
40 | #include <term.h> | | 42 | #include <term.h> |
41 | | | 43 | |
| | | 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 | } |
42 | #define reterr(code, msg) \ | | 58 | #define reterr(code, msg) \ |
43 | do { \ | | 59 | do { \ |
44 | if (errret == NULL) \ | | 60 | if (errret == NULL) \ |
45 | errx(EXIT_FAILURE, msg); \ | | 61 | errx(EXIT_FAILURE, msg); \ |
46 | else { \ | | 62 | else { \ |
47 | *errret = code; \ | | 63 | *errret = code; \ |
48 | return ERR; \ | | 64 | return ERR; \ |
49 | } \ | | 65 | } \ |
50 | } while (0 /* CONSTCOND */) | | 66 | } while (0 /* CONSTCOND */) |
51 | | | 67 | |
52 | #define reterrarg(code, msg, arg) \ | | 68 | #define reterrarg(code, msg, arg) \ |
53 | do { \ | | 69 | do { \ |
54 | if (errret == NULL) \ | | 70 | if (errret == NULL) \ |
55 | errx(EXIT_FAILURE, msg, arg); \ | | 71 | errx(EXIT_FAILURE, msg, arg); \ |
56 | else { \ | | 72 | else { \ |
57 | *errret = code; \ | | 73 | *errret = code; \ |
58 | return ERR; \ | | 74 | return ERR; \ |
59 | } \ | | 75 | } \ |
60 | } while (0 /* CONSTCOND */) | | 76 | } while (0 /* CONSTCOND */) |
61 | | | 77 | |
62 | | | 78 | |
63 | int | | 79 | int |
64 | ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret) | | 80 | ti_setupterm(TERMINAL **nterm, const char *term, int fildes, int *errret) |
65 | { | | 81 | { |
66 | int error; | | 82 | int error; |
| | | 83 | struct winsize win; |
67 | | | 84 | |
68 | _DIAGASSERT(nterm != NULL); | | 85 | _DIAGASSERT(nterm != NULL); |
69 | | | 86 | |
70 | if (term == NULL) | | 87 | if (term == NULL) |
71 | term = getenv("TERM"); | | 88 | term = getenv("TERM"); |
72 | if (term == NULL || *term == '\0') { | | 89 | if (term == NULL || *term == '\0') { |
73 | *nterm = NULL; | | 90 | *nterm = NULL; |
74 | reterr(0, "TERM environment variable not set"); | | 91 | reterr(0, "TERM environment variable not set"); |
75 | } | | 92 | } |
76 | if (fildes == STDOUT_FILENO && !isatty(fildes)) | | 93 | if (fildes == STDOUT_FILENO && !isatty(fildes)) |
77 | fildes = STDERR_FILENO; | | 94 | fildes = STDERR_FILENO; |
78 | | | 95 | |
79 | *nterm = calloc(1, sizeof(**nterm)); | | 96 | *nterm = calloc(1, sizeof(**nterm)); |
| @@ -95,26 +112,45 @@ ti_setupterm(TERMINAL **nterm, const cha | | | @@ -95,26 +112,45 @@ ti_setupterm(TERMINAL **nterm, const cha |
95 | /* NOTREACHED */ | | 112 | /* NOTREACHED */ |
96 | default: | | 113 | default: |
97 | reterr(-1, "unknown error"); | | 114 | reterr(-1, "unknown error"); |
98 | /* NOTREACHED */ | | 115 | /* NOTREACHED */ |
99 | } | | 116 | } |
100 | } | | 117 | } |
101 | | | 118 | |
102 | (*nterm)->fildes = fildes; | | 119 | (*nterm)->fildes = fildes; |
103 | _ti_setospeed(*nterm); | | 120 | _ti_setospeed(*nterm); |
104 | if (t_generic_type(*nterm)) | | 121 | if (t_generic_type(*nterm)) |
105 | reterrarg(0, "%s: generic terminal", term); | | 122 | reterrarg(0, "%s: generic terminal", term); |
106 | if (t_hard_copy(*nterm)) | | 123 | if (t_hard_copy(*nterm)) |
107 | reterrarg(1, "%s: hardcopy terminal", term); | | 124 | 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 | |
108 | /* POSIX requires 1 for success */ | | 144 | /* POSIX requires 1 for success */ |
109 | if (errret) | | 145 | if (errret) |
110 | *errret = 1; | | 146 | *errret = 1; |
111 | return OK; | | 147 | return OK; |
112 | } | | 148 | } |
113 | | | 149 | |
114 | int | | 150 | int |
115 | setupterm(const char *term, int fildes, int *errret) | | 151 | setupterm(const char *term, int fildes, int *errret) |
116 | { | | 152 | { |
117 | TERMINAL *nterm; | | 153 | TERMINAL *nterm; |
118 | int ret; | | 154 | int ret; |
119 | | | 155 | |
120 | if (errret != NULL) | | 156 | if (errret != NULL) |