source: trunk/minix/commands/yap/main.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 3.5 KB
Line 
1/* Copyright (c) 1985 Ceriel J.H. Jacobs */
2
3# ifndef lint
4static char rcsid[] = "$Header: /cvsup/minix/src/commands/yap/main.c,v 1.1.1.1 2005/04/21 14:55:40 beng Exp $";
5# endif
6
7# define _MAIN_
8
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"
28
29char *strcpy();
30
31STATIC int initialize();
32# ifdef SIGTSTP
33STATIC int suspsig();
34# endif
35
36int
37main(argc,argv) register char ** argv; {
38
39 register char ** av;
40
41 if (! isatty(1)) {
42 no_tty = 1;
43 }
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);
51 }
52 putline("Usage: ");
53 putline(argv[0]);
54 putline(
55" [-c] [-u] [-n] [-q] [-number] [+command] [file ... ]\n");
56 flush();
57 exit(1);
58 }
59 if (no_tty) {
60 *--av = "cat";
61 execve("/bin/cat", av, (char *) 0);
62 }
63 else processfiles(argc-(av-argv), av);
64 (VOID) quit();
65 /* NOTREACHED */
66}
67
68char *mktemp();
69
70/*
71 * Open temporary file for reading and writing.
72 * Panic if it fails
73 */
74
75static char indexfile[30], tempfile[30];
76
77int
78opentemp(i) {
79
80 register fildes;
81 register char *f;
82
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");
91 }
92 (VOID) unlink(f);
93 return fildes;
94}
95
96/*
97 * Collect initializing stuff here.
98 */
99
100STATIC int
101initialize(x) {
102
103 if (!(nopipe = x)) {
104 /*
105 * Reading from pipe
106 */
107 if (isatty(0)) {
108 return 1;
109 }
110 stdf = dup(0); /* Duplicate file descriptor of input */
111 if (no_tty) return 0;
112 /*
113 * Make sure standard input is from the terminal.
114 */
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);
124 }
125 }
126 if (no_tty) return 0;
127 (VOID) strcpy(tempfile,"/usr/tmp/yap_XXXXXX");
128 (VOID) strcpy(indexfile,"/usr/tmp/yap-XXXXXX");
129 /*
130 * Handle signals.
131 * Catch QUIT, DELETE and ^Z
132 */
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);
139 }
140# endif
141 (VOID) signal(SIGQUIT,quit);
142 return 0;
143}
144
145int
146catchdel() {
147 (VOID) signal(SIGINT, catchdel);
148 interrupt = 1;
149}
150
151# ifdef SIGTSTP
152
153/*
154 * We had a SIGTSTP signal.
155 * Suspend, by a call to this routine.
156 */
157
158VOID
159suspend() {
160
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);
168 /*
169 * We are not here anymore ...
170 *
171
172 *
173 * But we arive here ...
174 */
175 inittty();
176 putline(TI);
177 flush();
178 (VOID) signal(SIGTSTP,suspsig);
179}
180
181/*
182 * SIGTSTP signal handler.
183 * Just indicate that we had one, ignore further ones and return.
184 */
185
186STATIC int
187suspsig() {
188
189 suspend();
190 if (DoneSetJmp) longjmp(SetJmpBuf, 1);
191}
192# endif
193
194/*
195 * quit : called on exit.
196 * I bet you guessed that much.
197 */
198
199int
200quit() {
201
202 clrbline();
203 resettty();
204 flush();
205 exit(0);
206}
207
208/*
209 * Exit, but nonvoluntarily.
210 * At least tell the user why.
211 */
212
213VOID
214panic(s) char *s; {
215
216 putline("\007\007\007\r\n");
217 putline(s);
218 putline("\r\n");
219 quit();
220}
Note: See TracBrowser for help on using the repository browser.