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
23 #include <sys/ioctl.h>
40 static char termcap_buf[1024];
41 static char string_buffer[1024];
43 static char lower_left[15];
45 static char *clear_screen;
48 static char *start_standout;
49 static char *end_standout;
50 static char *terminal_init;
51 static char *terminal_end;
53 static struct termios old_settings;
54 static struct termios new_settings;
55 static char is_a_terminal = false;
58 init_termcap(bool interactive)
70 /* pretend we have a dumb terminal */
71 smart_terminal = false;
75 /* assume we have a smart terminal until proven otherwise */
76 smart_terminal = true;
78 /* get the terminal name */
79 term_name = getenv("TERM");
81 /* if there is no TERM, assume it's a dumb terminal */
82 /* patch courtesy of Sam Horrocks at telegraph.ics.uci.edu */
83 if (term_name == NULL)
85 smart_terminal = false;
89 /* now get the termcap entry */
90 if ((status = tgetent(termcap_buf, term_name)) != 1)
94 fprintf(stderr, "%s: can't open termcap file\n", myname);
98 fprintf(stderr, "%s: no termcap entry for a `%s' terminal\n",
102 /* pretend it's dumb and proceed */
103 smart_terminal = false;
107 /* "hardcopy" immediately indicates a very stupid terminal */
110 smart_terminal = false;
114 /* set up common terminal capabilities */
115 if ((screen_length = tgetnum("li")) <= 0)
117 screen_length = smart_terminal = 0;
121 /* screen_width is a little different */
122 if ((screen_width = tgetnum("co")) == -1)
131 /* terminals that overstrike need special attention */
132 overstrike = tgetflag("os");
134 /* initialize the pointer into the termcap string buffer */
135 bufptr = string_buffer;
137 /* get "ce", clear to end */
140 clear_line = tgetstr("ce", &bufptr);
143 /* get necessary capabilities */
144 if ((clear_screen = tgetstr("cl", &bufptr)) == NULL ||
145 (cursor_motion = tgetstr("cm", &bufptr)) == NULL)
147 smart_terminal = false;
151 /* get some more sophisticated stuff -- these are optional */
152 clear_to_end = tgetstr("cd", &bufptr);
153 terminal_init = tgetstr("ti", &bufptr);
154 terminal_end = tgetstr("te", &bufptr);
155 start_standout = tgetstr("so", &bufptr);
156 end_standout = tgetstr("se", &bufptr);
159 PC = (PCptr = tgetstr("pc", &bufptr)) ? *PCptr : 0;
161 /* set convenience strings */
162 strncpy(home, tgoto(cursor_motion, 0, 0), sizeof(home) - 1);
163 home[sizeof(home) - 1] = '\0';
164 /* (lower_left is set in get_screensize) */
166 /* get the actual screen size with an ioctl, if needed */
167 /* This may change screen_width and screen_length, and it always
171 /* if stdout is not a terminal, pretend we are a dumb terminal */
172 if (tcgetattr(STDOUT_FILENO, &old_settings) == -1)
174 smart_terminal = false;
181 /* get the old settings for safe keeping */
182 if (tcgetattr(STDOUT_FILENO, &old_settings) != -1)
184 /* copy the settings so we can modify them */
185 new_settings = old_settings;
187 /* turn off ICANON, character echo and tab expansion */
188 new_settings.c_lflag &= ~(ICANON|ECHO);
189 new_settings.c_oflag &= ~(TAB3);
190 new_settings.c_cc[VMIN] = 1;
191 new_settings.c_cc[VTIME] = 0;
192 tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings);
194 /* remember the erase and kill characters */
195 ch_erase = old_settings.c_cc[VERASE];
196 ch_kill = old_settings.c_cc[VKILL];
198 /* remember that it really is a terminal */
199 is_a_terminal = true;
201 /* send the termcap initialization string */
202 putcap(terminal_init);
207 /* not a terminal at all---consider it dumb */
208 smart_terminal = false;
215 /* move to the lower left, clear the line and send "te" */
221 putcap(terminal_end);
224 /* if we have settings to reset, then do so */
227 tcsetattr(STDOUT_FILENO, TCSADRAIN, &old_settings);
234 /* install our settings if it is a terminal */
237 tcsetattr(STDOUT_FILENO, TCSADRAIN, &new_settings);
240 /* send init string */
243 putcap(terminal_init);
252 if (ioctl (1, TIOCGWINSZ, &ws) != -1)
256 screen_length = ws.ws_row;
260 screen_width = ws.ws_col - 1;
265 (void) strncpy(lower_left, tgoto(cursor_motion, 0, screen_length - 1),
266 sizeof(lower_left) - 1);
267 lower_left[sizeof(lower_left) - 1] = '\0';
271 top_standout(const char *msg)
275 putcap(start_standout);
277 putcap(end_standout);
290 putcap(clear_screen);
297 if (smart_terminal && !overstrike && len > 0)