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>
46 char termcap_buf[1024];
47 char string_buffer[1024];
59 static struct termios old_settings;
60 static struct termios new_settings;
61 static char is_a_terminal = No;
70 init_termcap(interactive)
81 /* set defaults in case we aren't smart */
82 screen_width = MAX_COLS;
87 /* pretend we have a dumb terminal */
92 /* assume we have a smart terminal until proven otherwise */
95 /* get the terminal name */
96 term_name = getenv("TERM");
98 /* if there is no TERM, assume it's a dumb terminal */
99 /* patch courtesy of Sam Horrocks at telegraph.ics.uci.edu */
100 if (term_name == NULL)
106 /* now get the termcap entry */
107 if ((status = tgetent(termcap_buf, term_name)) != 1)
111 fprintf(stderr, "%s: can't open termcap file\n", myname);
115 fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n",
119 /* pretend it's dumb and proceed */
124 /* "hardcopy" immediately indicates a very stupid terminal */
131 /* set up common terminal capabilities */
132 if ((screen_length = tgetnum("li")) <= 0)
134 screen_length = smart_terminal = 0;
138 /* screen_width is a little different */
139 if ((screen_width = tgetnum("co")) == -1)
148 /* terminals that overstrike need special attention */
149 overstrike = tgetflag("os");
151 /* initialize the pointer into the termcap string buffer */
152 bufptr = string_buffer;
154 /* get "ce", clear to end */
157 clear_line = tgetstr("ce", &bufptr);
160 /* get necessary capabilities */
161 if ((clear_screen = tgetstr("cl", &bufptr)) == NULL ||
162 (cursor_motion = tgetstr("cm", &bufptr)) == NULL)
168 /* get some more sophisticated stuff -- these are optional */
169 clear_to_end = tgetstr("cd", &bufptr);
170 terminal_init = tgetstr("ti", &bufptr);
171 terminal_end = tgetstr("te", &bufptr);
172 start_standout = tgetstr("so", &bufptr);
173 end_standout = tgetstr("se", &bufptr);
176 PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0;
178 /* set convenience strings */
179 (void) strncpy(home, tgoto(cursor_motion, 0, 0), sizeof(home) - 1);
180 home[sizeof(home) - 1] = '\0';
181 /* (lower_left is set in get_screensize) */
183 /* get the actual screen size with an ioctl, if needed */
184 /* This may change screen_width and screen_length, and it always
188 /* if stdout is not a terminal, pretend we are a dumb terminal */
189 if (tcgetattr(STDOUT, &old_settings) == -1)
199 /* get the old settings for safe keeping */
200 if (tcgetattr(STDOUT, &old_settings) != -1)
202 /* copy the settings so we can modify them */
203 new_settings = old_settings;
205 /* turn off ICANON, character echo and tab expansion */
206 new_settings.c_lflag &= ~(ICANON|ECHO);
207 new_settings.c_oflag &= ~(TAB3);
208 new_settings.c_cc[VMIN] = 1;
209 new_settings.c_cc[VTIME] = 0;
210 (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings);
212 /* remember the erase and kill characters */
213 ch_erase = old_settings.c_cc[VERASE];
214 ch_kill = old_settings.c_cc[VKILL];
216 /* remember that it really is a terminal */
219 /* send the termcap initialization string */
220 putcap(terminal_init);
225 /* not a terminal at all---consider it dumb */
234 /* move to the lower left, clear the line and send "te" */
240 putcap(terminal_end);
243 /* if we have settings to reset, then do so */
246 (void) tcsetattr(STDOUT, TCSADRAIN, &old_settings);
254 /* install our settings if it is a terminal */
257 (void) tcsetattr(STDOUT, TCSADRAIN, &new_settings);
260 /* send init string */
263 putcap(terminal_init);
275 if (ioctl (1, TIOCGWINSZ, &ws) != -1)
279 screen_length = ws.ws_row;
283 screen_width = ws.ws_col - 1;
288 (void) strncpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1),
289 sizeof(lower_left) - 1);
290 lower_left[sizeof(lower_left) - 1] = '\0';
294 top_standout(char *msg)
298 putcap(start_standout);
300 putcap(end_standout);
313 putcap(clear_screen);
320 if (smart_terminal && !overstrike && len > 0)
349 /* This has to be defined as a subroutine for tputs (instead of a macro) */