top.c revision 66641
1char *copyright =
2    "Copyright (c) 1984 through 1996, William LeFebvre";
3
4/*
5 *  Top users/processes display for Unix
6 *  Version 3
7 *
8 *  This program may be freely redistributed,
9 *  but this entire comment MUST remain intact.
10 *
11 *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
12 *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
13 *
14 * $FreeBSD: head/contrib/top/top.c 66641 2000-10-04 23:34:16Z imp $
15 */
16
17/*
18 *  See the file "Changes" for information on version-to-version changes.
19 */
20
21/*
22 *  This file contains "main" and other high-level routines.
23 */
24
25/*
26 * The following preprocessor variables, when defined, are used to
27 * distinguish between different Unix implementations:
28 *
29 *	SIGHOLD  - use SVR4 sighold function when defined
30 *	SIGRELSE - use SVR4 sigrelse function when defined
31 *	FD_SET   - macros FD_SET and FD_ZERO are used when defined
32 */
33
34#include "os.h"
35#include <signal.h>
36#include <setjmp.h>
37#include <ctype.h>
38#include <sys/time.h>
39
40/* includes specific to top */
41#include "display.h"		/* interface to display package */
42#include "screen.h"		/* interface to screen package */
43#include "top.h"
44#include "top.local.h"
45#include "boolean.h"
46#include "machine.h"
47#include "utils.h"
48
49/* Size of the stdio buffer given to stdout */
50#define Buffersize	2048
51
52/* The buffer that stdio will use */
53char stdoutbuf[Buffersize];
54
55/* build Signal masks */
56#define Smask(s)	(1 << ((s) - 1))
57
58/* for system errors */
59extern int errno;
60
61/* for getopt: */
62extern int  optind;
63extern char *optarg;
64
65/* imported from screen.c */
66extern int overstrike;
67
68/* signal handling routines */
69sigret_t leave();
70sigret_t onalrm();
71sigret_t tstop();
72#ifdef SIGWINCH
73sigret_t winch();
74#endif
75
76/* internal routines */
77void quit();
78
79/* values which need to be accessed by signal handlers */
80static int max_topn;		/* maximum displayable processes */
81
82/* miscellaneous things */
83char *myname = "top";
84jmp_buf jmp_int;
85
86/* routines that don't return int */
87
88char *username();
89char *ctime();
90char *kill_procs();
91char *renice_procs();
92
93#ifdef ORDER
94extern int (*proc_compares[])();
95#else
96extern int proc_compare();
97#endif
98time_t time();
99
100caddr_t get_process_info();
101
102/* different routines for displaying the user's identification */
103/* (values assigned to get_userid) */
104char *username();
105char *itoa7();
106
107/* display routines that need to be predeclared */
108int i_loadave();
109int u_loadave();
110int i_procstates();
111int u_procstates();
112int i_cpustates();
113int u_cpustates();
114int i_memory();
115int u_memory();
116int i_swap();
117int u_swap();
118int i_message();
119int u_message();
120int i_header();
121int u_header();
122int i_process();
123int u_process();
124
125/* pointers to display routines */
126int (*d_loadave)() = i_loadave;
127int (*d_procstates)() = i_procstates;
128int (*d_cpustates)() = i_cpustates;
129int (*d_memory)() = i_memory;
130int (*d_swap)() = i_swap;
131int (*d_message)() = i_message;
132int (*d_header)() = i_header;
133int (*d_process)() = i_process;
134
135
136main(argc, argv)
137
138int  argc;
139char *argv[];
140
141{
142    register int i;
143    register int active_procs;
144    register int change;
145
146    struct system_info system_info;
147    struct statics statics;
148    caddr_t processes;
149
150    static char tempbuf1[50];
151    static char tempbuf2[50];
152    int old_sigmask;		/* only used for BSD-style signals */
153    int topn = Default_TOPN;
154    int delay = Default_DELAY;
155    int displays = 0;		/* indicates unspecified */
156    time_t curr_time;
157    char *(*get_userid)() = username;
158    char *uname_field = "USERNAME";
159    char *header_text;
160    char *env_top;
161    char **preset_argv;
162    int  preset_argc = 0;
163    char **av;
164    int  ac;
165    char dostates = No;
166    char do_unames = Yes;
167    char interactive = Maybe;
168    char warnings = 0;
169#if Default_TOPN == Infinity
170    char topn_specified = No;
171#endif
172    char ch;
173    char *iptr;
174    char no_command = 1;
175    struct timeval timeout;
176    struct process_select ps;
177#ifdef ORDER
178    char *order_name = NULL;
179    int order_index = 0;
180#endif
181#ifndef FD_SET
182    /* FD_SET and friends are not present:  fake it */
183    typedef int fd_set;
184#define FD_ZERO(x)     (*(x) = 0)
185#define FD_SET(f, x)   (*(x) = f)
186#endif
187    fd_set readfds;
188
189#ifdef ORDER
190    static char command_chars[] = "\f qh?en#sdkriIuto";
191#else
192    static char command_chars[] = "\f qh?en#sdkriIut";
193#endif
194/* these defines enumerate the "strchr"s of the commands in command_chars */
195#define CMD_redraw	0
196#define CMD_update	1
197#define CMD_quit	2
198#define CMD_help1	3
199#define CMD_help2	4
200#define CMD_OSLIMIT	4    /* terminals with OS can only handle commands */
201#define CMD_errors	5    /* less than or equal to CMD_OSLIMIT	   */
202#define CMD_number1	6
203#define CMD_number2	7
204#define CMD_delay	8
205#define CMD_displays	9
206#define CMD_kill	10
207#define CMD_renice	11
208#define CMD_idletog     12
209#define CMD_idletog2    13
210#define CMD_user	14
211#define CMD_selftog	15
212#ifdef ORDER
213#define CMD_order       16
214#endif
215
216    /* set the buffer for stdout */
217#ifdef DEBUG
218    setbuffer(stdout, NULL, 0);
219#else
220    setbuffer(stdout, stdoutbuf, Buffersize);
221#endif
222
223    /* get our name */
224    if (argc > 0)
225    {
226	if ((myname = strrchr(argv[0], '/')) == 0)
227	{
228	    myname = argv[0];
229	}
230	else
231	{
232	    myname++;
233	}
234    }
235
236    /* initialize some selection options */
237    ps.idle    = Yes;
238    ps.self    = -1;
239    ps.system  = No;
240    ps.uid     = -1;
241    ps.command = NULL;
242
243    /* get preset options from the environment */
244    if ((env_top = getenv("TOP")) != NULL)
245    {
246	av = preset_argv = argparse(env_top, &preset_argc);
247	ac = preset_argc;
248
249	/* set the dummy argument to an explanatory message, in case
250	   getopt encounters a bad argument */
251	preset_argv[0] = "while processing environment";
252    }
253
254    /* process options */
255    do {
256	/* if we're done doing the presets, then process the real arguments */
257	if (preset_argc == 0)
258	{
259	    ac = argc;
260	    av = argv;
261
262	    /* this should keep getopt happy... */
263	    optind = 1;
264	}
265
266	while ((i = getopt(ac, av, "SIbinqus:d:U:o:t")) != EOF)
267	{
268	    switch(i)
269	    {
270	      case 'u':			/* toggle uid/username display */
271		do_unames = !do_unames;
272		break;
273
274	      case 'U':			/* display only username's processes */
275		if ((ps.uid = userid(optarg)) == -1)
276		{
277		    fprintf(stderr, "%s: unknown user\n", optarg);
278		    exit(1);
279		}
280		break;
281
282	      case 'S':			/* show system processes */
283		ps.system = !ps.system;
284		break;
285
286	      case 'I':                   /* show idle processes */
287		ps.idle = !ps.idle;
288		break;
289
290	      case 'i':			/* go interactive regardless */
291		interactive = Yes;
292		break;
293
294	      case 'n':			/* batch, or non-interactive */
295	      case 'b':
296		interactive = No;
297		break;
298
299	      case 'd':			/* number of displays to show */
300		if ((i = atoiwi(optarg)) == Invalid || i == 0)
301		{
302		    fprintf(stderr,
303			"%s: warning: display count should be positive -- option ignored\n",
304			myname);
305		    warnings++;
306		}
307		else
308		{
309		    displays = i;
310		}
311		break;
312
313	      case 's':
314		if ((delay = atoi(optarg)) < 0)
315		{
316		    fprintf(stderr,
317			"%s: warning: seconds delay should be non-negative -- using default\n",
318			myname);
319		    delay = Default_DELAY;
320		    warnings++;
321		}
322		break;
323
324	      case 'q':		/* be quick about it */
325		/* only allow this if user is really root */
326		if (getuid() == 0)
327		{
328		    /* be very un-nice! */
329		    (void) nice(-20);
330		}
331		else
332		{
333		    fprintf(stderr,
334			"%s: warning: `-q' option can only be used by root\n",
335			myname);
336		    warnings++;
337		}
338		break;
339
340	      case 'o':		/* select sort order */
341#ifdef ORDER
342		order_name = optarg;
343#else
344		fprintf(stderr,
345			"%s: this platform does not support arbitrary ordering.  Sorry.\n",
346			myname);
347		warnings++;
348#endif
349		break;
350
351	      case 't':
352		ps.self = (ps.self == -1) ? getpid() : -1;
353		break;
354
355	      default:
356		fprintf(stderr, "\
357Top version %s\n\
358Usage: %s [-ISbinqut] [-d x] [-s x] [-o field] [-U username] [number]\n",
359			version_string(), myname);
360		exit(1);
361	    }
362	}
363
364	/* get count of top processes to display (if any) */
365	if (optind < ac)
366	{
367	    if ((topn = atoiwi(av[optind])) == Invalid)
368	    {
369		fprintf(stderr,
370			"%s: warning: process display count should be non-negative -- using default\n",
371			myname);
372		warnings++;
373	    }
374#if Default_TOPN == Infinity
375            else
376	    {
377		topn_specified = Yes;
378	    }
379#endif
380	}
381
382	/* tricky:  remember old value of preset_argc & set preset_argc = 0 */
383	i = preset_argc;
384	preset_argc = 0;
385
386    /* repeat only if we really did the preset arguments */
387    } while (i != 0);
388
389    /* set constants for username/uid display correctly */
390    if (!do_unames)
391    {
392	uname_field = "   UID  ";
393	get_userid = itoa7;
394    }
395
396    /* initialize the kernel memory interface */
397    if (machine_init(&statics) == -1)
398    {
399	exit(1);
400    }
401
402#ifdef ORDER
403    /* determine sorting order index, if necessary */
404    if (order_name != NULL)
405    {
406	if ((order_index = string_index(order_name, statics.order_names)) == -1)
407	{
408	    char **pp;
409
410	    fprintf(stderr, "%s: '%s' is not a recognized sorting order.\n",
411		    myname, order_name);
412	    fprintf(stderr, "\tTry one of these:");
413	    pp = statics.order_names;
414	    while (*pp != NULL)
415	    {
416		fprintf(stderr, " %s", *pp++);
417	    }
418	    fputc('\n', stderr);
419	    exit(1);
420	}
421    }
422#endif
423
424#ifdef no_initialization_needed
425    /* initialize the hashing stuff */
426    if (do_unames)
427    {
428	init_hash();
429    }
430#endif
431
432    /* initialize termcap */
433    init_termcap(interactive);
434
435    /* get the string to use for the process area header */
436    header_text = format_header(uname_field);
437
438    /* initialize display interface */
439    if ((max_topn = display_init(&statics)) == -1)
440    {
441	fprintf(stderr, "%s: can't allocate sufficient memory\n", myname);
442	exit(4);
443    }
444
445    /* print warning if user requested more processes than we can display */
446    if (topn > max_topn)
447    {
448	fprintf(stderr,
449		"%s: warning: this terminal can only display %d processes.\n",
450		myname, max_topn);
451	warnings++;
452    }
453
454    /* adjust for topn == Infinity */
455    if (topn == Infinity)
456    {
457	/*
458	 *  For smart terminals, infinity really means everything that can
459	 *  be displayed, or Largest.
460	 *  On dumb terminals, infinity means every process in the system!
461	 *  We only really want to do that if it was explicitly specified.
462	 *  This is always the case when "Default_TOPN != Infinity".  But if
463	 *  topn wasn't explicitly specified and we are on a dumb terminal
464	 *  and the default is Infinity, then (and only then) we use
465	 *  "Nominal_TOPN" instead.
466	 */
467#if Default_TOPN == Infinity
468	topn = smart_terminal ? Largest :
469		    (topn_specified ? Largest : Nominal_TOPN);
470#else
471	topn = Largest;
472#endif
473    }
474
475    /* set header display accordingly */
476    display_header(topn > 0);
477
478    /* determine interactive state */
479    if (interactive == Maybe)
480    {
481	interactive = smart_terminal;
482    }
483
484    /* if # of displays not specified, fill it in */
485    if (displays == 0)
486    {
487	displays = smart_terminal ? Infinity : 1;
488    }
489
490    /* hold interrupt signals while setting up the screen and the handlers */
491#ifdef SIGHOLD
492    sighold(SIGINT);
493    sighold(SIGQUIT);
494    sighold(SIGTSTP);
495#else
496    old_sigmask = sigblock(Smask(SIGINT) | Smask(SIGQUIT) | Smask(SIGTSTP));
497#endif
498    init_screen();
499    (void) signal(SIGINT, leave);
500    (void) signal(SIGQUIT, leave);
501    (void) signal(SIGTSTP, tstop);
502#ifdef SIGWINCH
503    (void) signal(SIGWINCH, winch);
504#endif
505#ifdef SIGRELSE
506    sigrelse(SIGINT);
507    sigrelse(SIGQUIT);
508    sigrelse(SIGTSTP);
509#else
510    (void) sigsetmask(old_sigmask);
511#endif
512    if (warnings)
513    {
514	fputs("....", stderr);
515	fflush(stderr);			/* why must I do this? */
516	sleep((unsigned)(3 * warnings));
517	fputc('\n', stderr);
518    }
519
520    /* setup the jump buffer for stops */
521    if (setjmp(jmp_int) != 0)
522    {
523	/* control ends up here after an interrupt */
524	reset_display();
525    }
526
527    /*
528     *  main loop -- repeat while display count is positive or while it
529     *		indicates infinity (by being -1)
530     */
531
532    while ((displays == -1) || (displays-- > 0))
533    {
534	/* get the current stats */
535	get_system_info(&system_info);
536
537	/* get the current set of processes */
538	processes =
539		get_process_info(&system_info,
540				 &ps,
541#ifdef ORDER
542				 proc_compares[order_index]);
543#else
544				 proc_compare);
545#endif
546
547	/* display the load averages */
548	(*d_loadave)(system_info.last_pid,
549		     system_info.load_avg);
550
551	/* display the current time */
552	/* this method of getting the time SHOULD be fairly portable */
553	time(&curr_time);
554	i_uptime(&system_info.boottime, &curr_time);
555	i_timeofday(&curr_time);
556
557	/* display process state breakdown */
558	(*d_procstates)(system_info.p_total,
559			system_info.procstates);
560
561	/* display the cpu state percentage breakdown */
562	if (dostates)	/* but not the first time */
563	{
564	    (*d_cpustates)(system_info.cpustates);
565	}
566	else
567	{
568	    /* we'll do it next time */
569	    if (smart_terminal)
570	    {
571		z_cpustates();
572	    }
573	    else
574	    {
575		putchar('\n');
576	    }
577	    dostates = Yes;
578	}
579
580	/* display memory stats */
581	(*d_memory)(system_info.memory);
582
583	/* display swap stats */
584	(*d_swap)(system_info.swap);
585
586	/* handle message area */
587	(*d_message)();
588
589	/* update the header area */
590	(*d_header)(header_text);
591
592	if (topn > 0)
593	{
594	    /* determine number of processes to actually display */
595	    /* this number will be the smallest of:  active processes,
596	       number user requested, number current screen accomodates */
597	    active_procs = system_info.p_active;
598	    if (active_procs > topn)
599	    {
600		active_procs = topn;
601	    }
602	    if (active_procs > max_topn)
603	    {
604		active_procs = max_topn;
605	    }
606
607	    /* now show the top "n" processes. */
608	    for (i = 0; i < active_procs; i++)
609	    {
610		(*d_process)(i, format_next_process(processes, get_userid));
611	    }
612	}
613	else
614	{
615	    i = 0;
616	}
617
618	/* do end-screen processing */
619	u_endscreen(i);
620
621	/* now, flush the output buffer */
622	fflush(stdout);
623
624	/* only do the rest if we have more displays to show */
625	if (displays)
626	{
627	    /* switch out for new display on smart terminals */
628	    if (smart_terminal)
629	    {
630		if (overstrike)
631		{
632		    reset_display();
633		}
634		else
635		{
636		    d_loadave = u_loadave;
637		    d_procstates = u_procstates;
638		    d_cpustates = u_cpustates;
639		    d_memory = u_memory;
640		    d_swap = u_swap;
641		    d_message = u_message;
642		    d_header = u_header;
643		    d_process = u_process;
644		}
645	    }
646
647	    no_command = Yes;
648	    if (!interactive)
649	    {
650		/* set up alarm */
651		(void) signal(SIGALRM, onalrm);
652		(void) alarm((unsigned)delay);
653
654		/* wait for the rest of it .... */
655		pause();
656	    }
657	    else while (no_command)
658	    {
659		/* assume valid command unless told otherwise */
660		no_command = No;
661
662		/* set up arguments for select with timeout */
663		FD_ZERO(&readfds);
664		FD_SET(1, &readfds);		/* for standard input */
665		timeout.tv_sec  = delay;
666		timeout.tv_usec = 0;
667
668		/* wait for either input or the end of the delay period */
669		if (select(32, &readfds, (fd_set *)NULL, (fd_set *)NULL, &timeout) > 0)
670		{
671		    int newval;
672		    char *errmsg;
673
674		    /* something to read -- clear the message area first */
675		    clear_message();
676
677		    /* now read it and convert to command strchr */
678		    /* (use "change" as a temporary to hold strchr) */
679		    (void) read(0, &ch, 1);
680		    if ((iptr = strchr(command_chars, ch)) == NULL)
681		    {
682			if (ch != '\r' && ch != '\n')
683			{
684			    /* illegal command */
685			    new_message(MT_standout, " Command not understood");
686			}
687			putchar('\r');
688			no_command = Yes;
689		    }
690		    else
691		    {
692			change = iptr - command_chars;
693			if (overstrike && change > CMD_OSLIMIT)
694			{
695			    /* error */
696			    new_message(MT_standout,
697			    " Command cannot be handled by this terminal");
698			    putchar('\r');
699			    no_command = Yes;
700			}
701			else switch(change)
702			{
703			    case CMD_redraw:	/* redraw screen */
704				reset_display();
705				break;
706
707			    case CMD_update:	/* merely update display */
708				/* is the load average high? */
709				if (system_info.load_avg[0] > LoadMax)
710				{
711				    /* yes, go home for visual feedback */
712				    go_home();
713				    fflush(stdout);
714				}
715				break;
716
717			    case CMD_quit:	/* quit */
718				quit(0);
719				/*NOTREACHED*/
720				break;
721
722			    case CMD_help1:	/* help */
723			    case CMD_help2:
724				reset_display();
725				clear();
726				show_help();
727				standout("Hit any key to continue: ");
728				fflush(stdout);
729				(void) read(0, &ch, 1);
730				break;
731
732			    case CMD_errors:	/* show errors */
733				if (error_count() == 0)
734				{
735				    new_message(MT_standout,
736					" Currently no errors to report.");
737				    putchar('\r');
738				    no_command = Yes;
739				}
740				else
741				{
742				    reset_display();
743				    clear();
744				    show_errors();
745				    standout("Hit any key to continue: ");
746				    fflush(stdout);
747				    (void) read(0, &ch, 1);
748				}
749				break;
750
751			    case CMD_number1:	/* new number */
752			    case CMD_number2:
753				new_message(MT_standout,
754				    "Number of processes to show: ");
755				newval = readline(tempbuf1, 8, Yes);
756				if (newval > -1)
757				{
758				    if (newval > max_topn)
759				    {
760					new_message(MT_standout | MT_delayed,
761					  " This terminal can only display %d processes.",
762					  max_topn);
763					putchar('\r');
764				    }
765
766				    if (newval == 0)
767				    {
768					/* inhibit the header */
769					display_header(No);
770				    }
771				    else if (newval > topn && topn == 0)
772				    {
773					/* redraw the header */
774					display_header(Yes);
775					d_header = i_header;
776				    }
777				    topn = newval;
778				}
779				break;
780
781			    case CMD_delay:	/* new seconds delay */
782				new_message(MT_standout, "Seconds to delay: ");
783				if ((i = readline(tempbuf1, 8, Yes)) > -1)
784				{
785				    delay = i;
786				}
787				clear_message();
788				break;
789
790			    case CMD_displays:	/* change display count */
791				new_message(MT_standout,
792					"Displays to show (currently %s): ",
793					displays == -1 ? "infinite" :
794							 itoa(displays));
795				if ((i = readline(tempbuf1, 10, Yes)) > 0)
796				{
797				    displays = i;
798				}
799				else if (i == 0)
800				{
801				    quit(0);
802				}
803				clear_message();
804				break;
805
806			    case CMD_kill:	/* kill program */
807				new_message(0, "kill ");
808				if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
809				{
810				    if ((errmsg = kill_procs(tempbuf2)) != NULL)
811				    {
812					new_message(MT_standout, "%s", errmsg);
813					putchar('\r');
814					no_command = Yes;
815				    }
816				}
817				else
818				{
819				    clear_message();
820				}
821				break;
822
823			    case CMD_renice:	/* renice program */
824				new_message(0, "renice ");
825				if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
826				{
827				    if ((errmsg = renice_procs(tempbuf2)) != NULL)
828				    {
829					new_message(MT_standout, errmsg);
830					putchar('\r');
831					no_command = Yes;
832				    }
833				}
834				else
835				{
836				    clear_message();
837				}
838				break;
839
840			    case CMD_idletog:
841			    case CMD_idletog2:
842				ps.idle = !ps.idle;
843				new_message(MT_standout | MT_delayed,
844				    " %sisplaying idle processes.",
845				    ps.idle ? "D" : "Not d");
846				putchar('\r');
847				break;
848
849			    case CMD_selftog:
850				ps.self = (ps.self == -1) ? getpid() : -1;
851				new_message(MT_standout | MT_delayed,
852				    " %sisplaying self.",
853				    (ps.self == -1) ? "D" : "Not d");
854				putchar('\r');
855				break;
856
857			    case CMD_user:
858				new_message(MT_standout,
859				    "Username to show: ");
860				if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
861				{
862				    if (tempbuf2[0] == '+' &&
863					tempbuf2[1] == '\0')
864				    {
865					ps.uid = -1;
866				    }
867				    else if ((i = userid(tempbuf2)) == -1)
868				    {
869					new_message(MT_standout,
870					    " %s: unknown user", tempbuf2);
871					no_command = Yes;
872				    }
873				    else
874				    {
875					ps.uid = i;
876				    }
877				    putchar('\r');
878				}
879				else
880				{
881				    clear_message();
882				}
883				break;
884
885#ifdef ORDER
886			    case CMD_order:
887				new_message(MT_standout,
888				    "Order to sort: ");
889				if (readline(tempbuf2, sizeof(tempbuf2), No) > 0)
890				{
891				  if ((i = string_index(tempbuf2, statics.order_names)) == -1)
892					{
893					  new_message(MT_standout,
894					      " %s: unrecognized sorting order", tempbuf2);
895					  no_command = Yes;
896				    }
897				    else
898				    {
899					order_index = i;
900				    }
901				    putchar('\r');
902				}
903				else
904				{
905				    clear_message();
906				}
907				break;
908#endif
909
910			    default:
911				new_message(MT_standout, " BAD CASE IN SWITCH!");
912				putchar('\r');
913			}
914		    }
915
916		    /* flush out stuff that may have been written */
917		    fflush(stdout);
918		}
919	    }
920	}
921    }
922
923    quit(0);
924    /*NOTREACHED*/
925}
926
927/*
928 *  reset_display() - reset all the display routine pointers so that entire
929 *	screen will get redrawn.
930 */
931
932reset_display()
933
934{
935    d_loadave    = i_loadave;
936    d_procstates = i_procstates;
937    d_cpustates  = i_cpustates;
938    d_memory     = i_memory;
939    d_swap       = i_swap;
940    d_message	 = i_message;
941    d_header	 = i_header;
942    d_process	 = i_process;
943}
944
945/*
946 *  signal handlers
947 */
948
949sigret_t leave()	/* exit under normal conditions -- INT handler */
950
951{
952    end_screen();
953    exit(0);
954}
955
956sigret_t tstop(i)	/* SIGTSTP handler */
957
958int i;
959
960{
961    /* move to the lower left */
962    end_screen();
963    fflush(stdout);
964
965    /* default the signal handler action */
966    (void) signal(SIGTSTP, SIG_DFL);
967
968    /* unblock the signal and send ourselves one */
969#ifdef SIGRELSE
970    sigrelse(SIGTSTP);
971#else
972    (void) sigsetmask(sigblock(0) & ~(1 << (SIGTSTP - 1)));
973#endif
974    (void) kill(0, SIGTSTP);
975
976    /* reset the signal handler */
977    (void) signal(SIGTSTP, tstop);
978
979    /* reinit screen */
980    reinit_screen();
981
982    /* jump to appropriate place */
983    longjmp(jmp_int, 1);
984
985    /*NOTREACHED*/
986}
987
988#ifdef SIGWINCH
989sigret_t winch(i)		/* SIGWINCH handler */
990
991int i;
992
993{
994    /* reascertain the screen dimensions */
995    get_screensize();
996
997    /* tell display to resize */
998    max_topn = display_resize();
999
1000    /* reset the signal handler */
1001    (void) signal(SIGWINCH, winch);
1002
1003    /* jump to appropriate place */
1004    longjmp(jmp_int, 1);
1005}
1006#endif
1007
1008void quit(status)		/* exit under duress */
1009
1010int status;
1011
1012{
1013    end_screen();
1014    exit(status);
1015    /*NOTREACHED*/
1016}
1017
1018sigret_t onalrm()	/* SIGALRM handler */
1019
1020{
1021    /* this is only used in batch mode to break out of the pause() */
1022    /* return; */
1023}
1024
1025