forget difference between big and small commands - obsolete with vm.
[minix.git] / commands / yap / main.c
bloba095092b4eaf7fe4a3c8fb76cb0abf12330c457a
1 /* Copyright (c) 1985 Ceriel J.H. Jacobs */
3 # ifndef lint
4 static char rcsid[] = "$Header$";
5 # endif
7 # define _MAIN_
9 # include "in_all.h"
10 # if USG_OPEN
11 # include <fcntl.h>
12 # endif
13 # if BSD4_2_OPEN
14 # include <sys/file.h>
15 # endif
16 # if POSIX_OPEN
17 # include <sys/types.h>
18 # include <fcntl.h>
19 # endif
20 # include "main.h"
21 # include "term.h"
22 # include "options.h"
23 # include "output.h"
24 # include "process.h"
25 # include "commands.h"
26 # include "display.h"
27 # include "prompt.h"
29 char *strcpy();
31 STATIC int initialize();
32 # ifdef SIGTSTP
33 STATIC int suspsig();
34 # endif
36 int
37 main(argc,argv) register char ** argv; {
39 register char ** av;
40 char *empty_envp[] = { (char *) 0 };
42 if (! isatty(1)) {
43 no_tty = 1;
45 argv[argc] = 0;
46 progname = argv[0];
47 if ((av = readoptions(argv)) == (char **) 0 ||
48 initialize(*av ? 1 : 0)) {
49 if (no_tty) {
50 close(1);
51 (VOID) dup(2);
53 putline("Usage: ");
54 putline(argv[0]);
55 putline(
56 " [-c] [-u] [-n] [-q] [-number] [+command] [file ... ]\n");
57 flush();
58 exit(1);
60 if (no_tty) {
61 *--av = "cat";
62 execve("/bin/cat", av, &empty_envp);
64 else processfiles(argc-(av-argv), av);
65 (VOID) quit();
66 /* NOTREACHED */
69 char *mktemp();
72 * Open temporary file for reading and writing.
73 * Panic if it fails
76 static char indexfile[30], tempfile[30];
78 int
79 opentemp(i) {
81 register fildes;
82 register char *f;
84 f = i ? mktemp(indexfile) : mktemp(tempfile);
85 # if BSD4_2_OPEN || USG_OPEN || POSIX_OPEN
86 if ((fildes = open(f,O_RDWR|O_TRUNC|O_CREAT,0600)) < 0) {
87 # else
88 if ((fildes = creat(f,0600)) <= 0 || close(fildes) < 0 ||
89 (fildes = open(f,2)) < 0) {
90 # endif
91 panic("Couldn't open temporary file");
93 (VOID) unlink(f);
94 return fildes;
98 * Collect initializing stuff here.
101 STATIC int
102 initialize(x) {
104 if (!(nopipe = x)) {
106 * Reading from pipe
108 if (isatty(0)) {
109 return 1;
111 stdf = dup(0); /* Duplicate file descriptor of input */
112 if (no_tty) return 0;
114 * Make sure standard input is from the terminal.
116 (VOID) close(0);
117 # if BSD4_2_OPEN || USG_OPEN || POSIX_OPEN
118 if (open("/dev/tty",O_RDONLY,0) != 0) {
119 # else
120 if (open("/dev/tty",0) != 0) {
121 # endif
122 putline("Couldn't open terminal\n");
123 flush();
124 exit(1);
127 if (no_tty) return 0;
128 (VOID) strcpy(tempfile,"/usr/tmp/yap_XXXXXX");
129 (VOID) strcpy(indexfile,"/usr/tmp/yap-XXXXXX");
131 * Handle signals.
132 * Catch QUIT, DELETE and ^Z
134 (VOID) signal(SIGQUIT,SIG_IGN);
135 (VOID) signal(SIGINT, catchdel);
136 ini_terminal();
137 # ifdef SIGTSTP
138 if (signal(SIGTSTP,SIG_IGN) == SIG_DFL) {
139 (VOID) signal(SIGTSTP,suspsig);
141 # endif
142 (VOID) signal(SIGQUIT,quit);
143 return 0;
147 catchdel() {
148 (VOID) signal(SIGINT, catchdel);
149 interrupt = 1;
152 # ifdef SIGTSTP
155 * We had a SIGTSTP signal.
156 * Suspend, by a call to this routine.
159 VOID
160 suspend() {
162 nflush();
163 resettty();
164 (VOID) signal(SIGTSTP,SIG_DFL);
165 #if BSD4_2_OPEN
166 sigsetmask(sigblock(0)&~(1 << (SIGTSTP - 1)));
167 #endif
168 (VOID) kill(0, SIGTSTP);
170 * We are not here anymore ...
174 * But we arive here ...
176 inittty();
177 putline(TI);
178 flush();
179 (VOID) signal(SIGTSTP,suspsig);
183 * SIGTSTP signal handler.
184 * Just indicate that we had one, ignore further ones and return.
187 STATIC int
188 suspsig() {
190 suspend();
191 if (DoneSetJmp) longjmp(SetJmpBuf, 1);
193 # endif
196 * quit : called on exit.
197 * I bet you guessed that much.
201 quit() {
203 clrbline();
204 resettty();
205 flush();
206 exit(0);
210 * Exit, but nonvoluntarily.
211 * At least tell the user why.
214 VOID
215 panic(s) char *s; {
217 putline("\007\007\007\r\n");
218 putline(s);
219 putline("\r\n");
220 quit();