*** empty log message ***
[nvi.git] / cl / cl_main.c
blobd8c76ac2f3a343cf081b8d99dde30b664eea8654
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.49 2000/07/07 22:29:12 skimo Exp $ (Berkeley) $Date: 2000/07/07 22:29:12 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <curses.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <termios.h>
28 #include <unistd.h>
30 #include "../common/common.h"
31 #include "../ip/extern.h"
32 #include "cl.h"
33 #include "pathnames.h"
35 GS *__global_list; /* GLOBAL: List of screens. */
36 sigset_t __sigblockset; /* GLOBAL: Blocked signals. */
38 static void cl_func_std __P((GS *));
39 static void cl_end __P((CL_PRIVATE *));
40 static CL_PRIVATE *cl_init __P((GS *));
41 static void perr __P((char *, char *));
42 static int setsig __P((int, struct sigaction *, void (*)(int)));
43 static void sig_end __P((GS *));
44 static void term_init __P((char *, char *));
47 * main --
48 * This is the main loop for the standalone curses editor.
50 int
51 main(argc, argv)
52 int argc;
53 char *argv[];
55 static int reenter;
56 CL_PRIVATE *clp;
57 GS *gp;
58 WIN *wp;
59 size_t rows, cols;
60 int rval;
61 char **p_av, **t_av, *ttype;
63 /* If loaded at 0 and jumping through a NULL pointer, stop. */
64 if (reenter++)
65 abort();
67 /* Create and initialize the global structure. */
68 __global_list = gp = gs_init(argv[0]);
71 * Strip out any arguments that vi isn't going to understand. There's
72 * no way to portably call getopt twice, so arguments parsed here must
73 * be removed from the argument list.
75 for (p_av = t_av = argv;;) {
76 if (*t_av == NULL) {
77 *p_av = NULL;
78 break;
80 if (!strcmp(*t_av, "--")) {
81 while ((*p_av++ = *t_av++) != NULL);
82 break;
84 *p_av++ = *t_av++;
87 /* Create new window */
88 wp = gs_new_win(gp);
90 /* Create and initialize the CL_PRIVATE structure. */
91 clp = cl_init(gp);
94 * Initialize the terminal information.
96 * We have to know what terminal it is from the start, since we may
97 * have to use termcap/terminfo to find out how big the screen is.
99 if ((ttype = getenv("TERM")) == NULL)
100 ttype = "unknown";
101 term_init(gp->progname, ttype);
103 /* Add the terminal type to the global structure. */
104 if ((OG_D_STR(gp, GO_TERM) =
105 OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
106 perr(gp->progname, NULL);
108 /* Figure out how big the screen is. */
109 if (cl_ssize(NULL, 0, &rows, &cols, NULL))
110 exit (1);
112 /* Add the rows and columns to the global structure. */
113 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
114 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
116 /* Ex wants stdout to be buffered. */
117 (void)setvbuf(stdout, NULL, _IOFBF, 0);
119 /* Start catching signals. */
120 if (sig_init(gp, NULL))
121 exit (1);
123 /* Run ex/vi. */
124 rval = editor(wp, argc, argv);
126 /* Clean out the global structure. */
127 gs_end(gp);
129 /* Clean up signals. */
130 sig_end(gp);
132 /* Clean up the terminal. */
133 (void)cl_quit(gp);
136 * XXX
137 * Reset the O_MESG option.
139 if (clp->tgw != TGW_UNKNOWN)
140 (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
143 * XXX
144 * Reset the X11 xterm icon/window name.
146 if (F_ISSET(clp, CL_RENAME))
147 cl_setname(gp, clp->oname);
149 /* If a killer signal arrived, pretend we just got it. */
150 if (clp->killersig) {
151 (void)signal(clp->killersig, SIG_DFL);
152 (void)kill(getpid(), clp->killersig);
153 /* NOTREACHED */
156 /* Free the global and CL private areas. */
157 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
158 cl_end(clp);
159 free(gp);
160 #endif
162 exit (rval);
166 * cl_init --
167 * Create and partially initialize the CL structure.
169 static CL_PRIVATE *
170 cl_init(gp)
171 GS *gp;
173 CL_PRIVATE *clp;
174 int fd;
176 /* Allocate the CL private structure. */
177 CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
178 if (clp == NULL)
179 perr(gp->progname, NULL);
180 gp->cl_private = clp;
183 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
184 * and resetting the tty if the input isn't from there. We also
185 * use the same test to determine if we're running a script or
186 * not.
188 if (isatty(STDIN_FILENO))
189 F_SET(clp, CL_STDIN_TTY);
190 else
191 F_SET(gp, G_SCRIPTED);
194 * We expect that if we've lost our controlling terminal that the
195 * open() (but not the tcgetattr()) will fail.
197 if (F_ISSET(clp, CL_STDIN_TTY)) {
198 if (tcgetattr(STDIN_FILENO, &clp->orig) == -1)
199 goto tcfail;
200 } else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
201 if (tcgetattr(fd, &clp->orig) == -1) {
202 tcfail: perr(gp->progname, "tcgetattr");
203 exit (1);
205 (void)close(fd);
208 /* Initialize the list of curses functions. */
209 cl_func_std(gp);
211 return (clp);
215 * cl_end --
216 * Discard the CL structure.
218 static void
219 cl_end(clp)
220 CL_PRIVATE *clp;
222 if (clp->oname != NULL)
223 free(clp->oname);
224 free(clp);
228 * term_init --
229 * Initialize terminal information.
231 static void
232 term_init(name, ttype)
233 char *name, *ttype;
235 int err;
237 /* Set up the terminal database information. */
238 setupterm(ttype, STDOUT_FILENO, &err);
239 switch (err) {
240 case -1:
241 (void)fprintf(stderr,
242 "%s: No terminal database found\n", name);
243 exit (1);
244 case 0:
245 (void)fprintf(stderr,
246 "%s: %s: unknown terminal type\n", name, ttype);
247 exit (1);
251 #define GLOBAL_CLP \
252 CL_PRIVATE *clp = GCLP(__global_list);
253 static void
254 h_hup(signo)
255 int signo;
257 GLOBAL_CLP;
259 F_SET(clp, CL_SIGHUP);
260 clp->killersig = SIGHUP;
263 static void
264 h_int(signo)
265 int signo;
267 GLOBAL_CLP;
269 F_SET(clp, CL_SIGINT);
272 static void
273 h_term(signo)
274 int signo;
276 GLOBAL_CLP;
278 F_SET(clp, CL_SIGTERM);
279 clp->killersig = SIGTERM;
282 static void
283 h_winch(signo)
284 int signo;
286 GLOBAL_CLP;
288 F_SET(clp, CL_SIGWINCH);
290 #undef GLOBAL_CLP
293 * sig_init --
294 * Initialize signals.
296 * PUBLIC: int sig_init __P((GS *, SCR *));
299 sig_init(gp, sp)
300 GS *gp;
301 SCR *sp;
303 CL_PRIVATE *clp;
305 clp = GCLP(gp);
307 if (sp == NULL) {
308 (void)sigemptyset(&__sigblockset);
309 if (sigaddset(&__sigblockset, SIGHUP) ||
310 setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
311 sigaddset(&__sigblockset, SIGINT) ||
312 setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
313 sigaddset(&__sigblockset, SIGTERM) ||
314 setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
315 #ifdef SIGWINCH
317 sigaddset(&__sigblockset, SIGWINCH) ||
318 setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
319 #endif
321 perr(gp->progname, NULL);
322 return (1);
324 } else
325 if (setsig(SIGHUP, NULL, h_hup) ||
326 setsig(SIGINT, NULL, h_int) ||
327 setsig(SIGTERM, NULL, h_term)
328 #ifdef SIGWINCH
330 setsig(SIGWINCH, NULL, h_winch)
331 #endif
333 msgq(sp, M_SYSERR, "signal-reset");
335 return (0);
339 * setsig --
340 * Set a signal handler.
342 static int
343 setsig(signo, oactp, handler)
344 int signo;
345 struct sigaction *oactp;
346 void (*handler) __P((int));
348 struct sigaction act;
351 * Use sigaction(2), not signal(3), since we don't always want to
352 * restart system calls. The example is when waiting for a command
353 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
354 * restart system calls (thanks, POSIX!). On the other hand, you
355 * can't portably NOT restart system calls (thanks, Sun!). SunOS
356 * used SA_INTERRUPT as their extension to NOT restart read calls.
357 * We sure hope nobody else used it for anything else. Mom told me
358 * there'd be days like this. She just never told me that there'd
359 * be so many.
361 act.sa_handler = handler;
362 sigemptyset(&act.sa_mask);
364 #ifdef SA_INTERRUPT
365 act.sa_flags = SA_INTERRUPT;
366 #else
367 act.sa_flags = 0;
368 #endif
369 return (sigaction(signo, &act, oactp));
373 * sig_end --
374 * End signal setup.
376 static void
377 sig_end(gp)
378 GS *gp;
380 CL_PRIVATE *clp;
382 clp = GCLP(gp);
383 (void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
384 (void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
385 (void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
386 #ifdef SIGWINCH
387 (void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
388 #endif
392 * cl_func_std --
393 * Initialize the standard curses functions.
395 static void
396 cl_func_std(gp)
397 GS *gp;
399 gp->scr_addstr = cl_addstr;
400 gp->scr_attr = cl_attr;
401 gp->scr_baud = cl_baud;
402 gp->scr_bell = cl_bell;
403 gp->scr_busy = NULL;
404 gp->scr_clrtoeol = cl_clrtoeol;
405 gp->scr_cursor = cl_cursor;
406 gp->scr_deleteln = cl_deleteln;
407 gp->scr_reply = NULL;
408 gp->scr_discard = cl_discard;
409 gp->scr_event = cl_event;
410 gp->scr_ex_adjust = cl_ex_adjust;
411 gp->scr_fmap = cl_fmap;
412 gp->scr_insertln = cl_insertln;
413 gp->scr_keyval = cl_keyval;
414 gp->scr_move = cl_move;
415 gp->scr_msg = NULL;
416 gp->scr_optchange = cl_optchange;
417 gp->scr_refresh = cl_refresh;
418 gp->scr_rename = cl_rename;
419 gp->scr_screen = cl_screen;
420 gp->scr_split = cl_split;
421 gp->scr_suspend = cl_suspend;
422 gp->scr_usage = cl_usage;
426 * perr --
427 * Print system error.
429 static void
430 perr(name, msg)
431 char *name, *msg;
433 (void)fprintf(stderr, "%s:", name);
434 if (msg != NULL)
435 (void)fprintf(stderr, "%s:", msg);
436 (void)fprintf(stderr, "%s\n", strerror(errno));
437 exit(1);