xref: /openbsd-src/usr.bin/vi/cl/cl_main.c (revision 2b0358df1d88d06ef4139321dd05bd5e05d91eaf)
1 /*	$OpenBSD: cl_main.c,v 1.19 2008/03/28 17:58:20 jasper Exp $	*/
2 
3 /*-
4  * Copyright (c) 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1993, 1994, 1995, 1996
7  *	Keith Bostic.  All rights reserved.
8  *
9  * See the LICENSE file for redistribution information.
10  */
11 
12 #include "config.h"
13 
14 #ifndef lint
15 static const char sccsid[] = "@(#)cl_main.c	10.36 (Berkeley) 10/14/96";
16 #endif /* not lint */
17 
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 
21 #include <bitstring.h>
22 #include <curses.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <term.h>
30 #include <termios.h>
31 #include <unistd.h>
32 
33 #include "../common/common.h"
34 #ifdef RUNNING_IP
35 #include "../ip/ip.h"
36 #endif
37 #include "cl.h"
38 #include "pathnames.h"
39 
40 GS *__global_list;				/* GLOBAL: List of screens. */
41 sigset_t __sigblockset;				/* GLOBAL: Blocked signals. */
42 
43 static void	   cl_func_std(GS *);
44 static CL_PRIVATE *cl_init(GS *);
45 static GS	  *gs_init(char *);
46 static void	   perr(char *, char *);
47 static int	   setsig(int, struct sigaction *, void (*)(int));
48 static void	   sig_end(GS *);
49 static void	   term_init(char *, char *);
50 
51 /*
52  * main --
53  *	This is the main loop for the standalone curses editor.
54  */
55 int
56 main(int argc, char *argv[])
57 {
58 	static int reenter;
59 	CL_PRIVATE *clp;
60 	GS *gp;
61 	size_t rows, cols;
62 	int rval;
63 	char *ttype;
64 #ifdef RUNNING_IP
65 	char *ip_arg;
66 	char **p_av, **t_av;
67 #endif
68 
69 	/* If loaded at 0 and jumping through a NULL pointer, stop. */
70 	if (reenter++)
71 		abort();
72 
73 	/* Create and initialize the global structure. */
74 	__global_list = gp = gs_init(argv[0]);
75 
76 	/*
77 	 * Strip out any arguments that vi isn't going to understand.  There's
78 	 * no way to portably call getopt twice, so arguments parsed here must
79 	 * be removed from the argument list.
80 	 */
81 #ifdef RUNNING_IP
82 	ip_arg = NULL;
83 	for (p_av = t_av = argv;;) {
84 		if (*t_av == NULL) {
85 			*p_av = NULL;
86 			break;
87 		}
88 		if (!strcmp(*t_av, "--")) {
89 			while ((*p_av++ = *t_av++) != NULL);
90 			break;
91 		}
92 		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
93 			if (t_av[0][2] != '\0') {
94 				ip_arg = t_av[0] + 2;
95 				++t_av;
96 				--argc;
97 				continue;
98 			}
99 			if (t_av[1] != NULL) {
100 				ip_arg = t_av[1];
101 				t_av += 2;
102 				argc -= 2;
103 				continue;
104 			}
105 		}
106 		*p_av++ = *t_av++;
107 	}
108 
109 	/*
110 	 * If we're being called as an editor library, we're done here, we
111 	 * get loaded with the curses screen, we don't share much code.
112 	 */
113 	if (ip_arg != NULL)
114 		exit (ip_main(argc, argv, gp, ip_arg));
115 #endif
116 
117 	/* Create and initialize the CL_PRIVATE structure. */
118 	clp = cl_init(gp);
119 
120 	/*
121 	 * Initialize the terminal information.
122 	 *
123 	 * We have to know what terminal it is from the start, since we may
124 	 * have to use termcap/terminfo to find out how big the screen is.
125 	 */
126 	if ((ttype = getenv("TERM")) == NULL)
127 		ttype = "unknown";
128 	term_init(gp->progname, ttype);
129 
130 	/* Add the terminal type to the global structure. */
131 	if ((OG_D_STR(gp, GO_TERM) =
132 	    OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
133 		perr(gp->progname, NULL);
134 
135 	/* Figure out how big the screen is. */
136 	if (cl_ssize(NULL, 0, &rows, &cols, NULL))
137 		exit (1);
138 
139 	/* Add the rows and columns to the global structure. */
140 	OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
141 	OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
142 
143 	/* Ex wants stdout to be buffered. */
144 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
145 
146 	/* Start catching signals. */
147 	if (sig_init(gp, NULL))
148 		exit (1);
149 
150 	/* Run ex/vi. */
151 	rval = editor(gp, argc, argv);
152 
153 	/* Clean up signals. */
154 	sig_end(gp);
155 
156 	/* Clean up the terminal. */
157 	(void)cl_quit(gp);
158 
159 	/*
160 	 * XXX
161 	 * Reset the O_MESG option.
162 	 */
163 	if (clp->tgw != TGW_UNKNOWN)
164 		(void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
165 
166 	/*
167 	 * XXX
168 	 * Reset the X11 xterm icon/window name.
169 	 */
170 	if (F_ISSET(clp, CL_RENAME)) {
171 		(void)printf(XTERM_RENAME, ttype);
172 		(void)fflush(stdout);
173 	}
174 
175 	/* If a killer signal arrived, pretend we just got it. */
176 	if (clp->killersig) {
177 		(void)signal(clp->killersig, SIG_DFL);
178 		(void)kill(getpid(), clp->killersig);
179 		/* NOTREACHED */
180 	}
181 
182 	/* Free the global and CL private areas. */
183 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
184 	free(clp);
185 	free(gp);
186 #endif
187 
188 	exit (rval);
189 }
190 
191 /*
192  * gs_init --
193  *	Create and partially initialize the GS structure.
194  */
195 static GS *
196 gs_init(name)
197 	char *name;
198 {
199 	GS *gp;
200 	char *p;
201 
202 	/* Figure out what our name is. */
203 	if ((p = strrchr(name, '/')) != NULL)
204 		name = p + 1;
205 
206 	/* Allocate the global structure. */
207 	CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
208 	if (gp == NULL)
209 		perr(name, NULL);
210 
211 
212 	gp->progname = name;
213 	return (gp);
214 }
215 
216 /*
217  * cl_init --
218  *	Create and partially initialize the CL structure.
219  */
220 static CL_PRIVATE *
221 cl_init(gp)
222 	GS *gp;
223 {
224 	CL_PRIVATE *clp;
225 	int fd;
226 
227 	/* Allocate the CL private structure. */
228 	CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
229 	if (clp == NULL)
230 		perr(gp->progname, NULL);
231 	gp->cl_private = clp;
232 
233 	/*
234 	 * Set the CL_STDIN_TTY flag.  It's purpose is to avoid setting
235 	 * and resetting the tty if the input isn't from there.  We also
236 	 * use the same test to determine if we're running a script or
237 	 * not.
238 	 */
239 	if (isatty(STDIN_FILENO))
240 		F_SET(clp, CL_STDIN_TTY);
241 	else
242 		F_SET(gp, G_SCRIPTED);
243 
244 	/*
245 	 * We expect that if we've lost our controlling terminal that the
246 	 * open() (but not the tcgetattr()) will fail.
247 	 */
248 	if (F_ISSET(clp, CL_STDIN_TTY)) {
249 		if (tcgetattr(STDIN_FILENO, &clp->orig) == -1)
250 			goto tcfail;
251 	} else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
252 		if (tcgetattr(fd, &clp->orig) == -1) {
253 tcfail:			perr(gp->progname, "tcgetattr");
254 			exit (1);
255 		}
256 		(void)close(fd);
257 	}
258 
259 	/* Initialize the list of curses functions. */
260 	cl_func_std(gp);
261 
262 	return (clp);
263 }
264 
265 /*
266  * term_init --
267  *	Initialize terminal information.
268  */
269 static void
270 term_init(name, ttype)
271 	char *name, *ttype;
272 {
273 	int err;
274 
275 	/* Set up the terminal database information. */
276 	setupterm(ttype, STDOUT_FILENO, &err);
277 	switch (err) {
278 	case -1:
279 		(void)fprintf(stderr,
280 		    "%s: No terminal database found\n", name);
281 		exit (1);
282 	case 0:
283 		(void)fprintf(stderr,
284 		    "%s: %s: unknown terminal type\n", name, ttype);
285 		exit (1);
286 	}
287 }
288 
289 #define	GLOBAL_CLP \
290 	CL_PRIVATE *clp = GCLP(__global_list);
291 static void
292 h_hup(signo)
293 	int signo;
294 {
295 	GLOBAL_CLP;
296 
297 	F_SET(clp, CL_SIGHUP);
298 	clp->killersig = SIGHUP;
299 }
300 
301 static void
302 h_int(signo)
303 	int signo;
304 {
305 	GLOBAL_CLP;
306 
307 	F_SET(clp, CL_SIGINT);
308 }
309 
310 static void
311 h_term(signo)
312 	int signo;
313 {
314 	GLOBAL_CLP;
315 
316 	F_SET(clp, CL_SIGTERM);
317 	clp->killersig = SIGTERM;
318 }
319 
320 static void
321 h_winch(signo)
322 	int signo;
323 {
324 	GLOBAL_CLP;
325 
326 	F_SET(clp, CL_SIGWINCH);
327 }
328 #undef	GLOBAL_CLP
329 
330 /*
331  * sig_init --
332  *	Initialize signals.
333  *
334  * PUBLIC: int sig_init(GS *, SCR *);
335  */
336 int
337 sig_init(gp, sp)
338 	GS *gp;
339 	SCR *sp;
340 {
341 	CL_PRIVATE *clp;
342 
343 	clp = GCLP(gp);
344 
345 	if (sp == NULL) {
346 		(void)sigemptyset(&__sigblockset);
347 		if (sigaddset(&__sigblockset, SIGHUP) ||
348 		    setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
349 		    sigaddset(&__sigblockset, SIGINT) ||
350 		    setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
351 		    sigaddset(&__sigblockset, SIGTERM) ||
352 		    setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
353 #ifdef SIGWINCH
354 		    ||
355 		    sigaddset(&__sigblockset, SIGWINCH) ||
356 		    setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
357 #endif
358 		    ) {
359 			perr(gp->progname, NULL);
360 			return (1);
361 		}
362 	} else
363 		if (setsig(SIGHUP, NULL, h_hup) ||
364 		    setsig(SIGINT, NULL, h_int) ||
365 		    setsig(SIGTERM, NULL, h_term)
366 #ifdef SIGWINCH
367 		    ||
368 		    setsig(SIGWINCH, NULL, h_winch)
369 #endif
370 		    ) {
371 			msgq(sp, M_SYSERR, "signal-reset");
372 		}
373 	return (0);
374 }
375 
376 /*
377  * setsig --
378  *	Set a signal handler.
379  */
380 static int
381 setsig(signo, oactp, handler)
382 	int signo;
383 	struct sigaction *oactp;
384 	void (*handler)(int);
385 {
386 	struct sigaction act;
387 
388 	/*
389 	 * Use sigaction(2), not signal(3), since we don't always want to
390 	 * restart system calls.  The example is when waiting for a command
391 	 * mode keystroke and SIGWINCH arrives.  Besides, you can't portably
392 	 * restart system calls (thanks, POSIX!).  On the other hand, you
393 	 * can't portably NOT restart system calls (thanks, Sun!).  SunOS
394 	 * used SA_INTERRUPT as their extension to NOT restart read calls.
395 	 * We sure hope nobody else used it for anything else.  Mom told me
396 	 * there'd be days like this.  She just never told me that there'd
397 	 * be so many.
398 	 */
399 	act.sa_handler = handler;
400 	sigemptyset(&act.sa_mask);
401 
402 #ifdef SA_INTERRUPT
403 	act.sa_flags = SA_INTERRUPT;
404 #else
405 	act.sa_flags = 0;
406 #endif
407 	return (sigaction(signo, &act, oactp));
408 }
409 
410 /*
411  * sig_end --
412  *	End signal setup.
413  */
414 static void
415 sig_end(gp)
416 	GS *gp;
417 {
418 	CL_PRIVATE *clp;
419 
420 	clp = GCLP(gp);
421 	(void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
422 	(void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
423 	(void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
424 #ifdef SIGWINCH
425 	(void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
426 #endif
427 }
428 
429 /*
430  * cl_func_std --
431  *	Initialize the standard curses functions.
432  */
433 static void
434 cl_func_std(gp)
435 	GS *gp;
436 {
437 	gp->scr_addstr = cl_addstr;
438 	gp->scr_attr = cl_attr;
439 	gp->scr_baud = cl_baud;
440 	gp->scr_bell = cl_bell;
441 	gp->scr_busy = NULL;
442 	gp->scr_clrtoeol = cl_clrtoeol;
443 	gp->scr_cursor = cl_cursor;
444 	gp->scr_deleteln = cl_deleteln;
445 	gp->scr_event = cl_event;
446 	gp->scr_ex_adjust = cl_ex_adjust;
447 	gp->scr_fmap = cl_fmap;
448 	gp->scr_insertln = cl_insertln;
449 	gp->scr_keyval = cl_keyval;
450 	gp->scr_move = cl_move;
451 	gp->scr_msg = NULL;
452 	gp->scr_optchange = cl_optchange;
453 	gp->scr_refresh = cl_refresh;
454 	gp->scr_rename = cl_rename;
455 	gp->scr_screen = cl_screen;
456 	gp->scr_suspend = cl_suspend;
457 	gp->scr_usage = cl_usage;
458 }
459 
460 /*
461  * perr --
462  *	Print system error.
463  */
464 static void
465 perr(name, msg)
466 	char *name, *msg;
467 {
468 	(void)fprintf(stderr, "%s:", name);
469 	if (msg != NULL)
470 		(void)fprintf(stderr, "%s:", msg);
471 	(void)fprintf(stderr, "%s\n", strerror(errno));
472 	exit(1);
473 }
474