2 * Top users/processes display for Unix
5 * This program may be freely redistributed,
6 * but this entire comment MUST remain intact.
8 * Copyright (c) 1984, 1989, William LeFebvre, Rice University
9 * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
14 /* This file contains the routines that interface to termcap and stty/gtty.
16 * Paul Vixie, February 1987: converted to use ioctl() instead of stty/gtty.
18 * I put in code to turn on the TOSTOP bit while top was running, but I
19 * didn't really like the results. If you desire it, turn on the
20 * preprocessor variable "TOStop". --wnl
25 #include <sys/ioctl.h>
42 static char termcap_buf[1024];
43 static char string_buffer[1024];
45 static char lower_left[15];
47 static char *clear_screen;
50 static char *start_standout;
51 static char *end_standout;
52 static char *terminal_init;
53 static char *terminal_end;
55 static struct termios old_settings;
56 static struct termios new_settings;
57 static char is_a_terminal = No;
64 init_termcap(int interactive)
71 /* set defaults in case we aren't smart */
72 screen_width = MAX_COLS;
77 /* pretend we have a dumb terminal */
82 /* assume we have a smart terminal until proven otherwise */
85 /* get the terminal name */
86 term_name = getenv("TERM");
88 /* if there is no TERM, assume it's a dumb terminal */
89 /* patch courtesy of Sam Horrocks at telegraph.ics.uci.edu */
90 if (term_name == NULL)
96 /* now get the termcap entry */
97 if ((status = tgetent(termcap_buf, term_name)) != 1)
101 fprintf(stderr, "%s: can't open termcap file\n", myname);
105 fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n",
109 /* pretend it's dumb and proceed */
114 /* "hardcopy" immediately indicates a very stupid terminal */
121 /* set up common terminal capabilities */
122 if ((screen_length = tgetnum("li")) <= 0)
124 screen_length = smart_terminal = 0;
128 /* screen_width is a little different */
129 if ((screen_width = tgetnum("co")) == -1)
138 /* terminals that overstrike need special attention */
139 overstrike = tgetflag("os");
141 /* initialize the pointer into the termcap string buffer */
142 bufptr = string_buffer;
144 /* get "ce", clear to end */
147 clear_line = tgetstr("ce", &bufptr);
150 /* get necessary capabilities */
151 if ((clear_screen = tgetstr("cl", &bufptr)) == NULL ||
152 (cursor_motion = tgetstr("cm", &bufptr)) == NULL)
158 /* get some more sophisticated stuff -- these are optional */
159 clear_to_end = tgetstr("cd", &bufptr);
160 terminal_init = tgetstr("ti", &bufptr);
161 terminal_end = tgetstr("te", &bufptr);
162 start_standout = tgetstr("so", &bufptr);
163 end_standout = tgetstr("se", &bufptr);
166 PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0;
168 /* set convenience strings */
169 (void) strncpy(home, tgoto(cursor_motion, 0, 0), sizeof(home) - 1);
170 home[sizeof(home) - 1] = '\0';
171 /* (lower_left is set in get_screensize) */
173 /* get the actual screen size with an ioctl, if needed */
174 /* This may change screen_width and screen_length, and it always
178 /* if stdout is not a terminal, pretend we are a dumb terminal */
179 if (tcgetattr(STDOUT, &old_settings) == -1)
188 /* get the old settings for safe keeping */
189 if (tcgetattr(STDOUT, &old_settings) != -1)
191 /* copy the settings so we can modify them */
192 new_settings = old_settings;
194 /* turn off ICANON, character echo and tab expansion */
195 new_settings.c_lflag &= ~(ICANON|ECHO);
196 new_settings.c_oflag &= ~(TAB3);
197 new_settings.c_cc[VMIN] = 1;
198 new_settings.c_cc[VTIME] = 0;
199 (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings);
201 /* remember the erase and kill characters */
202 ch_erase = old_settings.c_cc[VERASE];
203 ch_kill = old_settings.c_cc[VKILL];
205 /* remember that it really is a terminal */
208 /* send the termcap initialization string */
209 putcap(terminal_init);
214 /* not a terminal at all---consider it dumb */
222 /* move to the lower left, clear the line and send "te" */
228 putcap(terminal_end);
231 /* if we have settings to reset, then do so */
234 (void) tcsetattr(STDOUT, TCSADRAIN, &old_settings);
241 /* install our settings if it is a terminal */
244 (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings);
247 /* send init string */
250 putcap(terminal_init);
259 if (ioctl (1, TIOCGWINSZ, &ws) != -1)
263 screen_length = ws.ws_row;
267 screen_width = ws.ws_col - 1;
272 (void) strncpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1),
273 sizeof(lower_left) - 1);
274 lower_left[sizeof(lower_left) - 1] = '\0';
278 top_standout(char *msg)
282 putcap(start_standout);
284 putcap(end_standout);
297 putcap(clear_screen);
304 if (smart_terminal && !overstrike && len > 0)
332 /* This has to be defined as a subroutine for tputs (instead of a macro) */