]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_setup.c
ncurses 5.1
[ncurses.git] / ncurses / tinfo / lib_setup.c
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33
34 /*
35  * Terminal setup routines common to termcap and terminfo:
36  *
37  *              use_env(bool)
38  *              setupterm(char *, int, int *)
39  */
40
41 #include <curses.priv.h>
42 #include <tic.h>                /* for MAX_NAME_SIZE */
43 #include <term_entry.h>
44
45 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
46 #define _POSIX_SOURCE
47 #endif
48
49 #include <term.h>               /* lines, columns, cur_term */
50
51 MODULE_ID("$Id: lib_setup.c,v 1.59 2000/02/13 01:01:26 tom Exp $")
52
53 /****************************************************************************
54  *
55  * Terminal size computation
56  *
57  ****************************************************************************/
58
59 #if HAVE_SIZECHANGE
60 # if !defined(sun) || !TERMIOS
61 #  if HAVE_SYS_IOCTL_H
62 #   include <sys/ioctl.h>
63 #  endif
64 # endif
65 #endif
66
67 #if NEED_PTEM_H
68  /* On SCO, they neglected to define struct winsize in termios.h -- it's only
69   * in termio.h and ptem.h (the former conflicts with other definitions).
70   */
71 # include <sys/stream.h>
72 # include <sys/ptem.h>
73 #endif
74
75 /*
76  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
77  * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
78  */
79 #ifdef TIOCGSIZE
80 # define IOCTL_WINSIZE TIOCGSIZE
81 # define STRUCT_WINSIZE struct ttysize
82 # define WINSIZE_ROWS(n) (int)n.ts_lines
83 # define WINSIZE_COLS(n) (int)n.ts_cols
84 #else
85 # ifdef TIOCGWINSZ
86 #  define IOCTL_WINSIZE TIOCGWINSZ
87 #  define STRUCT_WINSIZE struct winsize
88 #  define WINSIZE_ROWS(n) (int)n.ws_row
89 #  define WINSIZE_COLS(n) (int)n.ws_col
90 # endif
91 #endif
92
93 static int _use_env = TRUE;
94
95 static void do_prototype(void);
96
97 void
98 use_env(bool f)
99 {
100     _use_env = f;
101 }
102
103 int LINES = 0, COLS = 0, TABSIZE = 0;
104
105 static void
106 _nc_get_screensize(int *linep, int *colp)
107 /* Obtain lines/columns values from the environment and/or terminfo entry */
108 {
109     /* figure out the size of the screen */
110     T(("screen size: terminfo lines = %d columns = %d", lines, columns));
111
112     if (!_use_env) {
113         *linep = (int) lines;
114         *colp = (int) columns;
115     } else {                    /* usually want to query LINES and COLUMNS from environment */
116         int value;
117
118         *linep = *colp = 0;
119
120         /* first, look for environment variables */
121         if ((value = _nc_getenv_num("LINES")) > 0) {
122             *linep = value;
123         }
124         if ((value = _nc_getenv_num("COLUMNS")) > 0) {
125             *colp = value;
126         }
127         T(("screen size: environment LINES = %d COLUMNS = %d", *linep, *colp));
128
129 #ifdef __EMX__
130         if (*linep <= 0 || *colp <= 0) {
131             int screendata[2];
132             _scrsize(screendata);
133             *colp = screendata[0];
134             *linep = screendata[1];
135             T(("EMX screen size: environment LINES = %d COLUMNS = %d",
136                     *linep, *colp));
137         }
138 #endif
139 #if HAVE_SIZECHANGE
140         /* if that didn't work, maybe we can try asking the OS */
141         if (*linep <= 0 || *colp <= 0) {
142             if (isatty(cur_term->Filedes)) {
143                 STRUCT_WINSIZE size;
144
145                 errno = 0;
146                 do {
147                     if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
148                         && errno != EINTR)
149                         goto failure;
150                 } while
151                     (errno == EINTR);
152
153                 /*
154                  * Solaris lets users override either dimension with an
155                  * environment variable.
156                  */
157                 if (*linep <= 0)
158                     *linep = WINSIZE_ROWS(size);
159                 if (*colp <= 0)
160                     *colp = WINSIZE_COLS(size);
161             }
162             /* FALLTHRU */
163           failure:;
164         }
165 #endif /* HAVE_SIZECHANGE */
166
167         /* if we can't get dynamic info about the size, use static */
168         if (*linep <= 0 || *colp <= 0)
169             if (lines > 0 && columns > 0) {
170                 *linep = (int) lines;
171                 *colp = (int) columns;
172             }
173
174         /* the ultimate fallback, assume fixed 24x80 size */
175         if (*linep <= 0 || *colp <= 0) {
176             *linep = 24;
177             *colp = 80;
178         }
179
180         /*
181          * Put the derived values back in the screen-size caps, so
182          * tigetnum() and tgetnum() will do the right thing.
183          */
184         lines = (short) (*linep);
185         columns = (short) (*colp);
186     }
187
188     T(("screen size is %dx%d", *linep, *colp));
189
190     if (VALID_NUMERIC(init_tabs))
191         TABSIZE = (int) init_tabs;
192     else
193         TABSIZE = 8;
194     T(("TABSIZE = %d", TABSIZE));
195
196 }
197
198 #if USE_SIZECHANGE
199 void
200 _nc_update_screensize(void)
201 {
202     int my_lines, my_cols;
203
204     _nc_get_screensize(&my_lines, &my_cols);
205     if (SP != 0 && SP->_resize != 0)
206         SP->_resize(my_lines, my_cols);
207 }
208 #endif
209
210 /****************************************************************************
211  *
212  * Terminal setup
213  *
214  ****************************************************************************/
215
216 #define ret_error(code, fmt, arg)       if (errret) {\
217                                             *errret = code;\
218                                             returnCode(ERR);\
219                                         } else {\
220                                             fprintf(stderr, fmt, arg);\
221                                             exit(EXIT_FAILURE);\
222                                         }
223
224 #define ret_error0(code, msg)           if (errret) {\
225                                             *errret = code;\
226                                             returnCode(ERR);\
227                                         } else {\
228                                             fprintf(stderr, msg);\
229                                             exit(EXIT_FAILURE);\
230                                         }
231
232 #if USE_DATABASE
233 static int
234 grab_entry(const char *const tn, TERMTYPE * const tp)
235 /* return 1 if entry found, 0 if not found, -1 if database not accessible */
236 {
237     char filename[PATH_MAX];
238     int status;
239
240     /*
241      * $TERM shouldn't contain pathname delimiters.
242      */
243     if (strchr(tn, '/'))
244         return 0;
245
246     if ((status = _nc_read_entry(tn, filename, tp)) != 1) {
247
248 #ifndef PURE_TERMINFO
249         /*
250          * Try falling back on the termcap file.
251          * Note:  allowing this call links the entire terminfo/termcap
252          * compiler into the startup code.  It's preferable to build a
253          * real terminfo database and use that.
254          */
255         status = _nc_read_termcap_entry(tn, tp);
256 #endif /* PURE_TERMINFO */
257
258     }
259
260     /*
261      * If we have an entry, force all of the cancelled strings to null
262      * pointers so we don't have to test them in the rest of the library.
263      * (The terminfo compiler bypasses this logic, since it must know if
264      * a string is cancelled, for merging entries).
265      */
266     if (status == 1) {
267         int n;
268         for_each_boolean(n, tp)
269             if (!VALID_BOOLEAN(tp->Booleans[n]))
270             tp->Booleans[n] = FALSE;
271         for_each_string(n, tp)
272             if (tp->Strings[n] == CANCELLED_STRING)
273             tp->Strings[n] = ABSENT_STRING;
274     }
275     return (status);
276 }
277 #endif
278
279 char ttytype[NAMESIZE] = "";
280
281 /*
282  *      setupterm(termname, Filedes, errret)
283  *
284  *      Find and read the appropriate object file for the terminal
285  *      Make cur_term point to the structure.
286  *
287  */
288
289 int
290 setupterm(NCURSES_CONST char *tname, int Filedes, int *errret)
291 {
292     struct term *term_ptr;
293     int status;
294
295     T((T_CALLED("setupterm(%s,%d,%p)"), _nc_visbuf(tname), Filedes, errret));
296
297     if (tname == 0) {
298         tname = getenv("TERM");
299         if (tname == 0 || *tname == '\0') {
300             ret_error0(-1, "TERM environment variable not set.\n");
301         }
302     }
303     if (strlen(tname) > MAX_NAME_SIZE) {
304         ret_error(-1, "TERM environment must be <= %d characters.\n",
305             MAX_NAME_SIZE);
306     }
307
308     T(("your terminal name is %s", tname));
309
310     term_ptr = typeCalloc(TERMINAL, 1);
311
312     if (term_ptr == 0) {
313         ret_error0(-1, "Not enough memory to create terminal structure.\n");
314     }
315 #if USE_DATABASE
316     status = grab_entry(tname, &term_ptr->type);
317 #else
318     status = 0;
319 #endif
320
321     /* try fallback list if entry on disk */
322     if (status != 1) {
323         const TERMTYPE *fallback = _nc_fallback(tname);
324
325         if (fallback) {
326             term_ptr->type = *fallback;
327             status = 1;
328         }
329     }
330
331     if (status == -1) {
332         ret_error0(-1, "terminals database is inaccessible\n");
333     } else if (status == 0) {
334         ret_error(0, "'%s': unknown terminal type.\n", tname);
335     }
336
337     /*
338      * Improve on SVr4 curses.  If an application mixes curses and termcap
339      * calls, it may call both initscr and tgetent.  This is not really a
340      * good thing to do, but can happen if someone tries using ncurses with
341      * the readline library.  The problem we are fixing is that when
342      * tgetent calls setupterm, the resulting Ottyb struct in cur_term is
343      * zeroed.  A subsequent call to endwin uses the zeroed terminal
344      * settings rather than the ones saved in initscr.  So we check if
345      * cur_term appears to contain terminal settings for the same output
346      * file as our current call - and copy those terminal settings.  (SVr4
347      * curses does not do this, however applications that are working
348      * around the problem will still work properly with this feature).
349      */
350     if (cur_term != 0) {
351         if (cur_term->Filedes == Filedes)
352             term_ptr->Ottyb = cur_term->Ottyb;
353     }
354
355     set_curterm(term_ptr);
356
357     if (command_character && getenv("CC"))
358         do_prototype();
359
360     strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
361     ttytype[NAMESIZE - 1] = '\0';
362
363     /*
364      * Allow output redirection.  This is what SVr3 does.
365      * If stdout is directed to a file, screen updates go
366      * to standard error.
367      */
368     if (Filedes == STDOUT_FILENO && !isatty(Filedes))
369         Filedes = STDERR_FILENO;
370     cur_term->Filedes = Filedes;
371
372     _nc_get_screensize(&LINES, &COLS);
373
374     if (errret)
375         *errret = 1;
376
377     T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
378
379     if (generic_type) {
380         ret_error(0, "'%s': I need something more specific.\n", tname);
381     }
382     if (hard_copy) {
383         ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);
384     }
385     returnCode(OK);
386 }
387
388 /*
389 **      do_prototype()
390 **
391 **      Take the real command character out of the CC environment variable
392 **      and substitute it in for the prototype given in 'command_character'.
393 **
394 */
395
396 static void
397 do_prototype(void)
398 {
399     int i;
400     char CC;
401     char proto;
402     char *tmp;
403
404     tmp = getenv("CC");
405     CC = *tmp;
406     proto = *command_character;
407
408     for_each_string(i, &(cur_term->type)) {
409         for (tmp = cur_term->type.Strings[i]; *tmp; tmp++) {
410             if (*tmp == proto)
411                 *tmp = CC;
412         }
413     }
414 }