Typo's.
[minix3.git] / commands / yap / main.c
blob52d4e7cff77de88eecbf7e0367c18879a2bf558a
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;
41 if (! isatty(1)) {
42 no_tty = 1;
44 argv[argc] = 0;
45 progname = argv[0];
46 if ((av = readoptions(argv)) == (char **) 0 ||
47 initialize(*av ? 1 : 0)) {
48 if (no_tty) {
49 close(1);
50 (VOID) dup(2);
52 putline("Usage: ");
53 putline(argv[0]);
54 putline(
55 " [-c] [-u] [-n] [-q] [-number] [+command] [file ... ]\n");
56 flush();
57 exit(1);
59 if (no_tty) {
60 *--av = "cat";
61 execve("/bin/cat", av, (char *) 0);
63 else processfiles(argc-(av-argv), av);
64 (VOID) quit();
65 /* NOTREACHED */
68 char *mktemp();
71 * Open temporary file for reading and writing.
72 * Panic if it fails
75 static char indexfile[30], tempfile[30];
77 int
78 opentemp(i) {
80 register fildes;
81 register char *f;
83 f = i ? mktemp(indexfile) : mktemp(tempfile);
84 # if BSD4_2_OPEN || USG_OPEN || POSIX_OPEN
85 if ((fildes = open(f,O_RDWR|O_TRUNC|O_CREAT,0600)) < 0) {
86 # else
87 if ((fildes = creat(f,0600)) <= 0 || close(fildes) < 0 ||
88 (fildes = open(f,2)) < 0) {
89 # endif
90 panic("Couldn't open temporary file");
92 (VOID) unlink(f);
93 return fildes;
97 * Collect initializing stuff here.
100 STATIC int
101 initialize(x) {
103 if (!(nopipe = x)) {
105 * Reading from pipe
107 if (isatty(0)) {
108 return 1;
110 stdf = dup(0); /* Duplicate file descriptor of input */
111 if (no_tty) return 0;
113 * Make sure standard input is from the terminal.
115 (VOID) close(0);
116 # if BSD4_2_OPEN || USG_OPEN || POSIX_OPEN
117 if (open("/dev/tty",O_RDONLY,0) != 0) {
118 # else
119 if (open("/dev/tty",0) != 0) {
120 # endif
121 putline("Couldn't open terminal\n");
122 flush();
123 exit(1);
126 if (no_tty) return 0;
127 (VOID) strcpy(tempfile,"/usr/tmp/yap_XXXXXX");
128 (VOID) strcpy(indexfile,"/usr/tmp/yap-XXXXXX");
130 * Handle signals.
131 * Catch QUIT, DELETE and ^Z
133 (VOID) signal(SIGQUIT,SIG_IGN);
134 (VOID) signal(SIGINT, catchdel);
135 ini_terminal();
136 # ifdef SIGTSTP
137 if (signal(SIGTSTP,SIG_IGN) == SIG_DFL) {
138 (VOID) signal(SIGTSTP,suspsig);
140 # endif
141 (VOID) signal(SIGQUIT,quit);
142 return 0;
146 catchdel() {
147 (VOID) signal(SIGINT, catchdel);
148 interrupt = 1;
151 # ifdef SIGTSTP
154 * We had a SIGTSTP signal.
155 * Suspend, by a call to this routine.
158 VOID
159 suspend() {
161 nflush();
162 resettty();
163 (VOID) signal(SIGTSTP,SIG_DFL);
164 #if BSD4_2_OPEN
165 sigsetmask(sigblock(0)&~(1 << (SIGTSTP - 1)));
166 #endif
167 (VOID) kill(0, SIGTSTP);
169 * We are not here anymore ...
173 * But we arive here ...
175 inittty();
176 putline(TI);
177 flush();
178 (VOID) signal(SIGTSTP,suspsig);
182 * SIGTSTP signal handler.
183 * Just indicate that we had one, ignore further ones and return.
186 STATIC int
187 suspsig() {
189 suspend();
190 if (DoneSetJmp) longjmp(SetJmpBuf, 1);
192 # endif
195 * quit : called on exit.
196 * I bet you guessed that much.
200 quit() {
202 clrbline();
203 resettty();
204 flush();
205 exit(0);
209 * Exit, but nonvoluntarily.
210 * At least tell the user why.
213 VOID
214 panic(s) char *s; {
216 putline("\007\007\007\r\n");
217 putline(s);
218 putline("\r\n");
219 quit();