1 /****************************************************************************
2 * Copyright (c) 1998 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
36 * Terminal setup routines common to termcap and terminfo:
39 * setupterm(char *, int, int *)
42 #include <curses.priv.h>
44 #if defined(SVR4_TERMIO) && !defined(_POSIX_SOURCE)
48 #include <term.h> /* lines, columns, cur_term */
50 MODULE_ID("$Id: lib_setup.c,v 1.37 1998/02/11 12:13:56 tom Exp $")
52 /****************************************************************************
54 * Terminal size computation
56 ****************************************************************************/
59 # if !defined(sun) || !HAVE_TERMIOS_H
61 # include <sys/ioctl.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).
70 # include <sys/stream.h>
71 # include <sys/ptem.h>
75 * SCO defines TIOCGSIZE and the corresponding struct. Other systems (SunOS,
76 * Solaris, IRIX) define TIOCGWINSZ and struct winsize.
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
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
92 extern TERMINAL *cur_term;
94 static int _use_env = TRUE;
96 static void do_prototype(void);
103 int LINES, COLS, TABSIZE;
105 static void _nc_get_screensize(int *linep, int *colp)
106 /* Obtain lines/columns values from the environment and/or terminfo entry */
110 /* figure out the size of the screen */
111 T(("screen size: terminfo lines = %d columns = %d", lines, columns));
116 *colp = (int)columns;
118 else /* usually want to query LINES and COLUMNS from environment */
122 /* first, look for environment variables */
123 rows = getenv("LINES");
126 cols = getenv("COLUMNS");
129 T(("screen size: environment LINES = %d COLUMNS = %d",*linep,*colp));
132 /* if that didn't work, maybe we can try asking the OS */
133 if (*linep <= 0 || *colp <= 0)
135 if (isatty(cur_term->Filedes))
141 if (ioctl(cur_term->Filedes, IOCTL_WINSIZE, &size) < 0
147 *linep = WINSIZE_ROWS(size);
148 *colp = WINSIZE_COLS(size);
153 #endif /* HAVE_SIZECHANGE */
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)
160 *colp = (int)columns;
163 /* the ultimate fallback, assume fixed 24x80 size */
164 if (*linep <= 0 || *colp <= 0)
171 * Put the derived values back in the screen-size caps, so
172 * tigetnum() and tgetnum() will do the right thing.
174 lines = (short)(*linep);
175 columns = (short)(*colp);
178 T(("screen size is %dx%d", *linep, *colp));
182 TABSIZE = (int)init_tabs;
184 #endif /* init_tabs */
186 T(("TABSIZE = %d", TABSIZE));
191 void _nc_update_screensize(void)
193 int my_lines, my_cols;
195 _nc_get_screensize(&my_lines, &my_cols);
196 if (SP != 0 && SP->_resize != 0)
197 SP->_resize(my_lines, my_cols);
201 /****************************************************************************
205 ****************************************************************************/
207 #define ret_error(code, fmt, arg) if (errret) {\
211 fprintf(stderr, fmt, arg);\
215 #define ret_error0(code, msg) if (errret) {\
219 fprintf(stderr, msg);\
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 */
227 char filename[PATH_MAX];
230 if ((status = _nc_read_entry(tn, filename, tp)) == 1)
233 #ifndef PURE_TERMINFO
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.
239 status = _nc_read_termcap_entry(tn, tp);
240 #endif /* PURE_TERMINFO */
246 char ttytype[NAMESIZE];
249 * setupterm(termname, Filedes, errret)
251 * Find and read the appropriate object file for the terminal
252 * Make cur_term point to the structure.
256 int setupterm(const char *tname, int Filedes, int *errret)
258 struct term *term_ptr;
261 T((T_CALLED("setupterm(\"%s\",%d,%p)"), tname, Filedes, errret));
264 tname = getenv("TERM");
265 if (tname == 0 || *tname == '\0')
266 ret_error0(-1, "TERM environment variable not set.\n");
269 T(("your terminal name is %s", tname));
271 term_ptr = typeCalloc(TERMINAL, 1);
274 ret_error0(-1, "Not enough memory to create terminal structure.\n") ;
276 status = grab_entry(tname, &term_ptr->type);
281 /* try fallback list if entry on disk */
284 const TERMTYPE *fallback = _nc_fallback(tname);
288 memcpy(&term_ptr->type, fallback, sizeof(TERMTYPE));
295 ret_error0(-1, "terminals database is inaccessible\n");
297 else if (status == 0)
299 ret_error(0, "'%s': unknown terminal type.\n", tname);
302 set_curterm(term_ptr);
304 if (command_character && getenv("CC"))
307 strncpy(ttytype, cur_term->type.term_names, NAMESIZE - 1);
308 ttytype[NAMESIZE - 1] = '\0';
311 * Allow output redirection. This is what SVr3 does.
312 * If stdout is directed to a file, screen updates go
315 if (Filedes == STDOUT_FILENO && !isatty(Filedes))
316 Filedes = STDERR_FILENO;
317 cur_term->Filedes = Filedes;
319 _nc_get_screensize(&LINES, &COLS);
324 T((T_CREATE("screen %s %dx%d"), tname, LINES, COLS));
327 ret_error(0, "'%s': I need something more specific.\n", tname);
329 ret_error(1, "'%s': I can't handle hardcopy terminals.\n", tname);
337 ** Take the real command character out of the CC environment variable
338 ** and substitute it in for the prototype given in 'command_character'.
352 proto = *command_character;
354 for (i=0; i < STRCOUNT; i++) {
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;