]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/lib_setup.c
ncurses 4.2
[ncurses.git] / ncurses / lib_setup.c
1 /****************************************************************************
2  * Copyright (c) 1998 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 /*
36  * Terminal setup routines common to termcap and terminfo:
37  *
38  *              use_env(bool)
39  *              setupterm(char *, int, int *)
40  */
41
42 #include <curses.priv.h>
43
44 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
45 #define _POSIX_SOURCE
46 #endif
47
48 #include <term.h>       /* lines, columns, cur_term */
49
50 MODULE_ID("$Id: lib_setup.c,v 1.37 1998/02/11 12:13:56 tom Exp $")
51
52 /****************************************************************************
53  *
54  * Terminal size computation
55  *
56  ****************************************************************************/
57
58 #if HAVE_SIZECHANGE
59 # if !defined(sun) || !HAVE_TERMIOS_H
60 #  if HAVE_SYS_IOCTL_H
61 #   include <sys/ioctl.h>
62 #  endif
63 # endif
64 #endif
65
66 #if NEED_PTEM_H
67  /* On SCO, they neglected to define struct winsize in termios.h -- it's only
68   * in termio.h and ptem.h (the former conflicts with other definitions).
69   */
70 # include <sys/stream.h>
71 # include <sys/ptem.h>
72 #endif
73
74 /*
75  * SCO defines TIOCGSIZE and the corresponding struct.  Other systems (SunOS,
76  * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
77  */
78 #ifdef TIOCGSIZE
79 # define IOCTL_WINSIZE TIOCGSIZE
80 # define STRUCT_WINSIZE struct ttysize
81 # define WINSIZE_ROWS(n) (int)n.ts_lines
82 # define WINSIZE_COLS(n) (int)n.ts_cols
83 #else
84 # ifdef TIOCGWINSZ
85 #  define IOCTL_WINSIZE TIOCGWINSZ
86 #  define STRUCT_WINSIZE struct winsize
87 #  define WINSIZE_ROWS(n) (int)n.ws_row
88 #  define WINSIZE_COLS(n) (int)n.ws_col
89 # endif
90 #endif
91
92 extern TERMINAL *cur_term;
93
94 static int _use_env = TRUE;
95
96 static void do_prototype(void);
97
98 void use_env(bool f)
99 {
100         _use_env = f;
101 }
102
103 int LINES, COLS, TABSIZE;
104
105 static void _nc_get_screensize(int *linep, int *colp)
106 /* Obtain lines/columns values from the environment and/or terminfo entry */
107 {
108 char    *rows, *cols;
109
110         /* figure out the size of the screen */
111         T(("screen size: terminfo lines = %d columns = %d", lines, columns));
112
113         if (!_use_env)
114         {
115             *linep = (int)lines;
116             *colp  = (int)columns;
117         }
118         else    /* usually want to query LINES and COLUMNS from environment */
119         {
120             *linep = *colp = 0;
121
122             /* first, look for environment variables */
123             rows = getenv("LINES");
124             if (rows != 0)
125                 *linep = atoi(rows);
126             cols = getenv("COLUMNS");
127             if (cols != 0)
128                 *colp = atoi(cols);
129             T(("screen size: environment LINES = %d COLUMNS = %d",*linep,*colp));
130
131 #if HAVE_SIZECHANGE
132             /* if that didn't work, maybe we can try asking the OS */
133             if (*linep <= 0 || *colp <= 0)
134             {
135                 if (isatty(cur_term->Filedes))
136                 {
137                     STRUCT_WINSIZE size;
138
139                     errno = 0;
140                     do {
141                         if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
142                                 && errno != EINTR)
143                             goto failure;
144                     } while
145                         (errno == EINTR);
146
147                     *linep = WINSIZE_ROWS(size);
148                     *colp  = WINSIZE_COLS(size);
149                 }
150                 /* FALLTHRU */
151             failure:;
152             }
153 #endif /* HAVE_SIZECHANGE */
154
155             /* if we can't get dynamic info about the size, use static */
156             if (*linep <= 0 || *colp <= 0)
157                 if (lines > 0 && columns > 0)
158                 {
159                     *linep = (int)lines;
160                     *colp  = (int)columns;
161                 }
162
163             /* the ultimate fallback, assume fixed 24x80 size */
164             if (*linep <= 0 || *colp <= 0)
165             {
166                 *linep = 24;
167                 *colp  = 80;
168             }
169
170             /*
171              * Put the derived values back in the screen-size caps, so
172              * tigetnum() and tgetnum() will do the right thing.
173              */
174             lines   = (short)(*linep);
175             columns = (short)(*colp);
176         }
177
178         T(("screen size is %dx%d", *linep, *colp));
179
180 #ifdef init_tabs
181         if (init_tabs != -1)
182                 TABSIZE = (int)init_tabs;
183         else
184 #endif /* init_tabs */
185                 TABSIZE = 8;
186         T(("TABSIZE = %d", TABSIZE));
187
188 }
189
190 #if USE_SIZECHANGE
191 void _nc_update_screensize(void)
192 {
193         int my_lines, my_cols;
194
195         _nc_get_screensize(&my_lines, &my_cols);
196         if (SP != 0 && SP->_resize != 0)
197                 SP->_resize(my_lines, my_cols);
198 }
199 #endif
200
201 /****************************************************************************
202  *
203  * Terminal setup
204  *
205  ****************************************************************************/
206
207 #define ret_error(code, fmt, arg)       if (errret) {\
208                                             *errret = code;\
209                                             returnCode(ERR);\
210                                         } else {\
211                                             fprintf(stderr, fmt, arg);\
212                                             exit(EXIT_FAILURE);\
213                                         }
214
215 #define ret_error0(code, msg)           if (errret) {\
216                                             *errret = code;\
217                                             returnCode(ERR);\
218                                         } else {\
219                                             fprintf(stderr, msg);\
220                                             exit(EXIT_FAILURE);\
221                                         }
222
223 #if USE_DATABASE
224 static int grab_entry(const char *const tn, TERMTYPE *const tp)
225 /* return 1 if entry found, 0 if not found, -1 if database not accessible */
226 {
227         char    filename[PATH_MAX];
228         int     status;
229
230         if ((status = _nc_read_entry(tn, filename, tp)) == 1)
231             return(1);
232
233 #ifndef PURE_TERMINFO
234         /*
235          * Try falling back on the termcap file.  Note: allowing this call
236          * links the entire terminfo/termcap compiler into the startup code.
237          * It's preferable to build a real terminfo database and use that.
238          */
239         status = _nc_read_termcap_entry(tn, tp);
240 #endif /* PURE_TERMINFO */
241
242         return(status);
243 }
244 #endif
245
246 char ttytype[NAMESIZE];
247
248 /*
249  *      setupterm(termname, Filedes, errret)
250  *
251  *      Find and read the appropriate object file for the terminal
252  *      Make cur_term point to the structure.
253  *
254  */
255
256 int setupterm(const char *tname, int Filedes, int *errret)
257 {
258 struct term     *term_ptr;
259 int status;
260
261         T((T_CALLED("setupterm(\"%s\",%d,%p)"), tname, Filedes, errret));
262
263         if (tname == 0) {
264                 tname = getenv("TERM");
265                 if (tname == 0 || *tname == '\0')
266                         ret_error0(-1, "TERM environment variable not set.\n");
267         }
268
269         T(("your terminal name is %s", tname));
270
271         term_ptr = typeCalloc(TERMINAL, 1);
272
273         if (term_ptr == 0)
274                 ret_error0(-1, "Not enough memory to create terminal structure.\n") ;
275 #if USE_DATABASE
276         status = grab_entry(tname, &term_ptr->type);
277 #else
278         status = 0;
279 #endif
280
281         /* try fallback list if entry on disk */
282         if (status != 1)
283         {
284             const TERMTYPE      *fallback = _nc_fallback(tname);
285
286             if (fallback)
287             {
288                 memcpy(&term_ptr->type, fallback, sizeof(TERMTYPE));
289                 status = 1;
290             }
291         }
292
293         if (status == -1)
294         {
295                 ret_error0(-1, "terminals database is inaccessible\n");
296         }
297         else if (status == 0)
298         {
299                 ret_error(0, "'%s': unknown terminal type.\n", tname);
300         }
301
302         set_curterm(term_ptr);
303
304         if (command_character  &&  getenv("CC"))
305                 do_prototype();
306
307         strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
308         ttytype[NAMESIZE - 1] = '\0';
309
310         /*
311          * Allow output redirection.  This is what SVr3 does.
312          * If stdout is directed to a file, screen updates go
313          * to standard error.
314          */
315         if (Filedes == STDOUT_FILENO && !isatty(Filedes))
316             Filedes = STDERR_FILENO;
317         cur_term->Filedes = Filedes;
318
319         _nc_get_screensize(&LINES, &COLS);
320
321         if (errret)
322                 *errret = 1;
323
324         T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
325
326         if (generic_type)
327                 ret_error(0, "'%s': I need something more specific.\n", tname);
328         if (hard_copy)
329                 ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);
330
331         returnCode(OK);
332 }
333
334 /*
335 **      do_prototype()
336 **
337 **      Take the real command character out of the CC environment variable
338 **      and substitute it in for the prototype given in 'command_character'.
339 **
340 */
341
342 static void
343 do_prototype(void)
344 {
345 int     i, j;
346 char    CC;
347 char    proto;
348 char    *tmp;
349
350         tmp = getenv("CC");
351         CC = *tmp;
352         proto = *command_character;
353
354         for (i=0; i < STRCOUNT; i++) {
355                 j = 0;
356                 while (cur_term->type.Strings[i][j]) {
357                         if (cur_term->type.Strings[i][j] == proto)
358                                 cur_term->type.Strings[i][j] = CC;
359                         j++;
360                 }
361         }
362 }