Fix how keywords are picked up.
[nvi.git] / cl / cl_main.c
blob0ecbe443b4e658a2f2df33eb08715ad224e76d2e
1 /*-
2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: cl_main.c,v 10.54 2001/07/29 19:07:27 skimo Exp $ (Berkeley) $Date: 2001/07/29 19:07:27 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <termios.h>
27 #include <unistd.h>
29 #include "../common/common.h"
30 #include "../ip/extern.h"
31 #include "cl.h"
32 #include "pathnames.h"
34 GS *__global_list; /* GLOBAL: List of screens. */
35 sigset_t __sigblockset; /* GLOBAL: Blocked signals. */
37 static void cl_func_std __P((WIN *));
38 static void cl_end __P((CL_PRIVATE *));
39 static CL_PRIVATE *cl_init __P((WIN *));
40 static void perr __P((char *, char *));
41 static int setsig __P((int, struct sigaction *, void (*)(int)));
42 static void sig_end __P((GS *));
43 static void term_init __P((char *, char *));
46 * main --
47 * This is the main loop for the standalone curses editor.
49 int
50 main(int argc, char **argv)
52 static int reenter;
53 CL_PRIVATE *clp;
54 GS *gp;
55 WIN *wp;
56 size_t rows, cols;
57 int rval;
58 char **p_av, **t_av, *ttype;
60 /* If loaded at 0 and jumping through a NULL pointer, stop. */
61 if (reenter++)
62 abort();
64 /* Create and initialize the global structure. */
65 __global_list = gp = gs_init(argv[0]);
68 * Strip out any arguments that vi isn't going to understand. There's
69 * no way to portably call getopt twice, so arguments parsed here must
70 * be removed from the argument list.
72 for (p_av = t_av = argv;;) {
73 if (*t_av == NULL) {
74 *p_av = NULL;
75 break;
77 if (!strcmp(*t_av, "--")) {
78 while ((*p_av++ = *t_av++) != NULL);
79 break;
81 *p_av++ = *t_av++;
84 /* Create new window */
85 wp = gs_new_win(gp);
87 /* Create and initialize the CL_PRIVATE structure. */
88 clp = cl_init(wp);
91 * Initialize the terminal information.
93 * We have to know what terminal it is from the start, since we may
94 * have to use termcap/terminfo to find out how big the screen is.
96 if ((ttype = getenv("TERM")) == NULL)
97 ttype = "unknown";
98 term_init(gp->progname, ttype);
100 /* Add the terminal type to the global structure. */
101 if ((OG_D_STR(gp, GO_TERM) =
102 OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
103 perr(gp->progname, NULL);
105 /* Figure out how big the screen is. */
106 if (cl_ssize(NULL, 0, &rows, &cols, NULL))
107 exit (1);
109 /* Add the rows and columns to the global structure. */
110 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
111 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
113 /* Ex wants stdout to be buffered. */
114 (void)setvbuf(stdout, NULL, _IOFBF, 0);
116 /* Start catching signals. */
117 if (sig_init(gp, NULL))
118 exit (1);
120 /* Run ex/vi. */
121 rval = editor(wp, argc, argv);
123 /* Clean out the global structure. */
124 gs_end(gp);
126 /* Clean up signals. */
127 sig_end(gp);
129 /* Clean up the terminal. */
130 (void)cl_quit(gp);
133 * XXX
134 * Reset the O_MESG option.
136 if (clp->tgw != TGW_UNKNOWN)
137 (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
140 * XXX
141 * Reset the X11 xterm icon/window name.
143 if (F_ISSET(clp, CL_RENAME))
144 cl_setname(gp, clp->oname);
146 /* If a killer signal arrived, pretend we just got it. */
147 if (clp->killersig) {
148 (void)signal(clp->killersig, SIG_DFL);
149 (void)kill(getpid(), clp->killersig);
150 /* NOTREACHED */
153 /* Free the global and CL private areas. */
154 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
155 cl_end(clp);
156 free(gp);
157 #endif
159 exit (rval);
163 * cl_init --
164 * Create and partially initialize the CL structure.
166 static CL_PRIVATE *
167 cl_init(WIN *wp)
169 CL_PRIVATE *clp;
170 int fd;
171 GS *gp;
173 gp = wp->gp;
175 /* Allocate the CL private structure. */
176 CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
177 if (clp == NULL)
178 perr(gp->progname, NULL);
179 gp->cl_private = clp;
182 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
183 * and resetting the tty if the input isn't from there. We also
184 * use the same test to determine if we're running a script or
185 * not.
187 if (isatty(STDIN_FILENO))
188 F_SET(clp, CL_STDIN_TTY);
189 else
190 F_SET(gp, G_SCRIPTED);
193 * We expect that if we've lost our controlling terminal that the
194 * open() (but not the tcgetattr()) will fail.
196 if (F_ISSET(clp, CL_STDIN_TTY)) {
197 if (tcgetattr(STDIN_FILENO, &clp->orig) == -1)
198 goto tcfail;
199 } else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
200 if (tcgetattr(fd, &clp->orig) == -1) {
201 tcfail: perr(gp->progname, "tcgetattr");
202 exit (1);
204 (void)close(fd);
207 /* Initialize the list of curses functions. */
208 cl_func_std(wp);
210 return (clp);
214 * cl_end --
215 * Discard the CL structure.
217 static void
218 cl_end(CL_PRIVATE *clp)
220 if (clp->oname != NULL)
221 free(clp->oname);
222 free(clp);
226 * term_init --
227 * Initialize terminal information.
229 static void
230 term_init(char *name, char *ttype)
232 int err;
234 /* Set up the terminal database information. */
235 setupterm(ttype, STDOUT_FILENO, &err);
236 switch (err) {
237 case -1:
238 (void)fprintf(stderr,
239 "%s: No terminal database found\n", name);
240 exit (1);
241 case 0:
242 (void)fprintf(stderr,
243 "%s: %s: unknown terminal type\n", name, ttype);
244 exit (1);
248 #define GLOBAL_CLP \
249 CL_PRIVATE *clp = GCLP(__global_list);
250 static void
251 h_hup(int signo)
253 GLOBAL_CLP;
255 F_SET(clp, CL_SIGHUP);
256 clp->killersig = SIGHUP;
259 static void
260 h_int(int signo)
262 GLOBAL_CLP;
264 F_SET(clp, CL_SIGINT);
267 static void
268 h_term(int signo)
270 GLOBAL_CLP;
272 F_SET(clp, CL_SIGTERM);
273 clp->killersig = SIGTERM;
276 static void
277 h_winch(int signo)
279 GLOBAL_CLP;
281 F_SET(clp, CL_SIGWINCH);
283 #undef GLOBAL_CLP
286 * sig_init --
287 * Initialize signals.
289 * PUBLIC: int sig_init __P((GS *, SCR *));
292 sig_init(GS *gp, SCR *sp)
294 CL_PRIVATE *clp;
296 clp = GCLP(gp);
298 if (sp == NULL) {
299 (void)sigemptyset(&__sigblockset);
300 if (sigaddset(&__sigblockset, SIGHUP) ||
301 setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
302 sigaddset(&__sigblockset, SIGINT) ||
303 setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
304 sigaddset(&__sigblockset, SIGTERM) ||
305 setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
306 #ifdef SIGWINCH
308 sigaddset(&__sigblockset, SIGWINCH) ||
309 setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
310 #endif
312 perr(gp->progname, NULL);
313 return (1);
315 } else
316 if (setsig(SIGHUP, NULL, h_hup) ||
317 setsig(SIGINT, NULL, h_int) ||
318 setsig(SIGTERM, NULL, h_term)
319 #ifdef SIGWINCH
321 setsig(SIGWINCH, NULL, h_winch)
322 #endif
324 msgq(sp, M_SYSERR, "signal-reset");
326 return (0);
330 * setsig --
331 * Set a signal handler.
333 static int
334 setsig(int signo, struct sigaction *oactp, void (*handler) (int))
336 struct sigaction act;
339 * Use sigaction(2), not signal(3), since we don't always want to
340 * restart system calls. The example is when waiting for a command
341 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
342 * restart system calls (thanks, POSIX!). On the other hand, you
343 * can't portably NOT restart system calls (thanks, Sun!). SunOS
344 * used SA_INTERRUPT as their extension to NOT restart read calls.
345 * We sure hope nobody else used it for anything else. Mom told me
346 * there'd be days like this. She just never told me that there'd
347 * be so many.
349 act.sa_handler = handler;
350 sigemptyset(&act.sa_mask);
352 #ifdef SA_INTERRUPT
353 act.sa_flags = SA_INTERRUPT;
354 #else
355 act.sa_flags = 0;
356 #endif
357 return (sigaction(signo, &act, oactp));
361 * sig_end --
362 * End signal setup.
364 static void
365 sig_end(GS *gp)
367 CL_PRIVATE *clp;
369 clp = GCLP(gp);
370 (void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
371 (void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
372 (void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
373 #ifdef SIGWINCH
374 (void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
375 #endif
379 * cl_func_std --
380 * Initialize the standard curses functions.
382 static void
383 cl_func_std(WIN *wp)
385 GS *gp;
387 gp = wp->gp;
389 gp->scr_addstr = cl_addstr;
390 gp->scr_waddstr = cl_waddstr;
391 gp->scr_attr = cl_attr;
392 gp->scr_baud = cl_baud;
393 gp->scr_bell = cl_bell;
394 gp->scr_busy = NULL;
395 gp->scr_child = NULL;
396 gp->scr_clrtoeol = cl_clrtoeol;
397 gp->scr_cursor = cl_cursor;
398 gp->scr_deleteln = cl_deleteln;
399 gp->scr_reply = NULL;
400 gp->scr_discard = cl_discard;
401 gp->scr_event = cl_event;
402 gp->scr_ex_adjust = cl_ex_adjust;
403 gp->scr_fmap = cl_fmap;
404 gp->scr_insertln = cl_insertln;
405 gp->scr_keyval = cl_keyval;
406 gp->scr_move = cl_move;
407 wp->scr_msg = NULL;
408 gp->scr_optchange = cl_optchange;
409 gp->scr_refresh = cl_refresh;
410 gp->scr_rename = cl_rename;
411 gp->scr_screen = cl_screen;
412 gp->scr_split = cl_split;
413 gp->scr_suspend = cl_suspend;
414 gp->scr_usage = cl_usage;
418 * perr --
419 * Print system error.
421 static void
422 perr(char *name, char *msg)
424 (void)fprintf(stderr, "%s:", name);
425 if (msg != NULL)
426 (void)fprintf(stderr, "%s:", msg);
427 (void)fprintf(stderr, "%s\n", strerror(errno));
428 exit(1);