xref: /openbsd-src/usr.bin/vi/cl/cl_main.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: cl_main.c,v 1.11 2001/01/29 01:58:27 niklas 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 <termios.h>
30 #include <unistd.h>
31 
32 #include "../common/common.h"
33 #ifdef RUNNING_IP
34 #include "../ip/ip.h"
35 #endif
36 #include "cl.h"
37 #include "pathnames.h"
38 
39 GS *__global_list;				/* GLOBAL: List of screens. */
40 sigset_t __sigblockset;				/* GLOBAL: Blocked signals. */
41 
42 static void	   cl_func_std __P((GS *));
43 static CL_PRIVATE *cl_init __P((GS *));
44 static GS	  *gs_init __P((char *));
45 static void	   perr __P((char *, char *));
46 static int	   setsig __P((int, struct sigaction *, void (*)(int)));
47 static void	   sig_end __P((GS *));
48 static void	   term_init __P((char *, char *));
49 
50 /*
51  * main --
52  *	This is the main loop for the standalone curses editor.
53  */
54 int
55 main(argc, argv)
56 	int argc;
57 	char *argv[];
58 {
59 	static int reenter;
60 	CL_PRIVATE *clp;
61 	GS *gp;
62 	size_t rows, cols;
63 	int rval;
64 	char *ip_arg, **p_av, **t_av, *ttype;
65 
66 	/* If loaded at 0 and jumping through a NULL pointer, stop. */
67 	if (reenter++)
68 		abort();
69 
70 	/* Create and initialize the global structure. */
71 	__global_list = gp = gs_init(argv[0]);
72 
73 	/*
74 	 * Strip out any arguments that vi isn't going to understand.  There's
75 	 * no way to portably call getopt twice, so arguments parsed here must
76 	 * be removed from the argument list.
77 	 */
78 #ifdef RUNNING_IP
79 	ip_arg = NULL;
80 	for (p_av = t_av = argv;;) {
81 		if (*t_av == NULL) {
82 			*p_av = NULL;
83 			break;
84 		}
85 		if (!strcmp(*t_av, "--")) {
86 			while ((*p_av++ = *t_av++) != NULL);
87 			break;
88 		}
89 		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
90 			if (t_av[0][2] != '\0') {
91 				ip_arg = t_av[0] + 2;
92 				++t_av;
93 				--argc;
94 				continue;
95 			}
96 			if (t_av[1] != NULL) {
97 				ip_arg = t_av[1];
98 				t_av += 2;
99 				argc -= 2;
100 				continue;
101 			}
102 		}
103 		*p_av++ = *t_av++;
104 	}
105 
106 	/*
107 	 * If we're being called as an editor library, we're done here, we
108 	 * get loaded with the curses screen, we don't share much code.
109 	 */
110 	if (ip_arg != NULL)
111 		exit (ip_main(argc, argv, gp, ip_arg));
112 #else
113 	ip_arg = argv[0];
114 #endif
115 
116 	/* Create and initialize the CL_PRIVATE structure. */
117 	clp = cl_init(gp);
118 
119 	/*
120 	 * Initialize the terminal information.
121 	 *
122 	 * We have to know what terminal it is from the start, since we may
123 	 * have to use termcap/terminfo to find out how big the screen is.
124 	 */
125 	if ((ttype = getenv("TERM")) == NULL)
126 		ttype = "unknown";
127 	term_init(gp->progname, ttype);
128 
129 	/* Add the terminal type to the global structure. */
130 	if ((OG_D_STR(gp, GO_TERM) =
131 	    OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
132 		perr(gp->progname, NULL);
133 
134 	/* Figure out how big the screen is. */
135 	if (cl_ssize(NULL, 0, &rows, &cols, NULL))
136 		exit (1);
137 
138 	/* Add the rows and columns to the global structure. */
139 	OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
140 	OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
141 
142 	/* Ex wants stdout to be buffered. */
143 	(void)setvbuf(stdout, NULL, _IOFBF, 0);
144 
145 	/* Start catching signals. */
146 	if (sig_init(gp, NULL))
147 		exit (1);
148 
149 	/* Run ex/vi. */
150 	rval = editor(gp, argc, argv);
151 
152 	/* Clean up signals. */
153 	sig_end(gp);
154 
155 	/* Clean up the terminal. */
156 	(void)cl_quit(gp);
157 
158 	/*
159 	 * XXX
160 	 * Reset the O_MESG option.
161 	 */
162 	if (clp->tgw != TGW_UNKNOWN)
163 		(void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
164 
165 	/*
166 	 * XXX
167 	 * Reset the X11 xterm icon/window name.
168 	 */
169 	if (F_ISSET(clp, CL_RENAME)) {
170 		(void)printf(XTERM_RENAME, ttype);
171 		(void)fflush(stdout);
172 	}
173 
174 	/* If a killer signal arrived, pretend we just got it. */
175 	if (clp->killersig) {
176 		(void)signal(clp->killersig, SIG_DFL);
177 		(void)kill(getpid(), clp->killersig);
178 		/* NOTREACHED */
179 	}
180 
181 	/* Free the global and CL private areas. */
182 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
183 	free(clp);
184 	free(gp);
185 #endif
186 
187 	exit (rval);
188 }
189 
190 /*
191  * gs_init --
192  *	Create and partially initialize the GS structure.
193  */
194 static GS *
195 gs_init(name)
196 	char *name;
197 {
198 	CL_PRIVATE *clp;
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 __P((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) __P((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