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.
13 static const char sccsid[] = "$Id: cl_main.c,v 10.55 2011/08/15 19:52:28 zy Exp $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
32 #include "../common/common.h"
34 #include "pathnames.h"
36 GS *__global_list; /* GLOBAL: List of screens. */
37 sigset_t __sigblockset; /* GLOBAL: Blocked signals. */
39 static void cl_func_std __P((GS *));
40 static CL_PRIVATE *cl_init __P((GS *));
41 static GS *gs_init __P((char *));
42 static void perr __P((char *, char *));
43 static int setsig __P((int, struct sigaction *, void (*)(int)));
44 static void sig_end __P((GS *));
45 static void term_init __P((char *, char *));
49 * This is the main loop for the standalone curses editor.
52 main(int argc, char **argv)
59 char **p_av, **t_av, *ttype;
61 /* If loaded at 0 and jumping through a NULL pointer, stop. */
65 /* Create and initialize the global structure. */
66 __global_list = gp = gs_init(argv[0]);
69 * Strip out any arguments that vi isn't going to understand. There's
70 * no way to portably call getopt twice, so arguments parsed here must
71 * be removed from the argument list.
73 for (p_av = t_av = argv;;) {
78 if (!strcmp(*t_av, "--")) {
79 while ((*p_av++ = *t_av++) != NULL);
85 /* Create and initialize the CL_PRIVATE structure. */
89 * Initialize the terminal information.
91 * We have to know what terminal it is from the start, since we may
92 * have to use termcap/terminfo to find out how big the screen is.
94 if ((ttype = getenv("TERM")) == NULL)
96 term_init(gp->progname, ttype);
98 /* Add the terminal type to the global structure. */
99 if ((OG_D_STR(gp, GO_TERM) =
100 OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
101 perr(gp->progname, NULL);
103 /* Figure out how big the screen is. */
104 if (cl_ssize(NULL, 0, &rows, &cols, NULL))
107 /* Add the rows and columns to the global structure. */
108 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
109 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
111 /* Ex wants stdout to be buffered. */
112 (void)setvbuf(stdout, NULL, _IOFBF, 0);
114 /* Start catching signals. */
115 if (sig_init(gp, NULL))
119 rval = editor(gp, argc, argv);
121 /* Clean up signals. */
124 /* Clean up the terminal. */
129 * Reset the O_MESG option.
131 if (clp->tgw != TGW_UNKNOWN)
132 (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
136 * Reset the X11 xterm icon/window name.
138 if (F_ISSET(clp, CL_RENAME))
139 cl_setname(gp, clp->oname);
141 /* If a killer signal arrived, pretend we just got it. */
142 if (clp->killersig) {
143 (void)signal(clp->killersig, SIG_DFL);
144 (void)kill(getpid(), clp->killersig);
148 /* Free the global and CL private areas. */
149 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
150 if (clp->oname != NULL)
153 free(OG_STR(gp, GO_TERM));
162 * Create and partially initialize the GS structure.
170 /* Figure out what our name is. */
171 if ((p = strrchr(name, '/')) != NULL)
174 /* Allocate the global structure. */
175 CALLOC_NOMSG(NULL, gp, GS *, 1, sizeof(GS));
185 * Create and partially initialize the CL structure.
193 /* Allocate the CL private structure. */
194 CALLOC_NOMSG(NULL, clp, CL_PRIVATE *, 1, sizeof(CL_PRIVATE));
196 perr(gp->progname, NULL);
197 gp->cl_private = clp;
200 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
201 * and resetting the tty if the input isn't from there. We also
202 * use the same test to determine if we're running a script or
205 if (isatty(STDIN_FILENO))
206 F_SET(clp, CL_STDIN_TTY);
208 F_SET(gp, G_SCRIPTED);
211 * We expect that if we've lost our controlling terminal that the
212 * open() (but not the tcgetattr()) will fail.
214 if (F_ISSET(clp, CL_STDIN_TTY)) {
215 if (tcgetattr(STDIN_FILENO, &clp->orig) == -1)
217 } else if ((fd = open(_PATH_TTY, O_RDONLY, 0)) != -1) {
218 if (tcgetattr(fd, &clp->orig) == -1) {
219 tcfail: perr(gp->progname, "tcgetattr");
225 /* Initialize the list of curses functions. */
233 * Initialize terminal information.
236 term_init(char *name, char *ttype)
240 /* Set up the terminal database information. */
241 setupterm(ttype, STDOUT_FILENO, &err);
244 (void)fprintf(stderr,
245 "%s: No terminal database found\n", name);
248 (void)fprintf(stderr,
249 "%s: %s: unknown terminal type\n", name, ttype);
255 CL_PRIVATE *clp = GCLP(__global_list);
261 F_SET(clp, CL_SIGHUP);
262 clp->killersig = SIGHUP;
270 F_SET(clp, CL_SIGINT);
278 F_SET(clp, CL_SIGTERM);
279 clp->killersig = SIGTERM;
287 F_SET(clp, CL_SIGWINCH);
293 * Initialize signals.
295 * PUBLIC: int sig_init __P((GS *, SCR *));
298 sig_init(GS *gp, SCR *sp)
305 (void)sigemptyset(&__sigblockset);
306 if (sigaddset(&__sigblockset, SIGHUP) ||
307 setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
308 sigaddset(&__sigblockset, SIGINT) ||
309 setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
310 sigaddset(&__sigblockset, SIGTERM) ||
311 setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
314 sigaddset(&__sigblockset, SIGWINCH) ||
315 setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
318 perr(gp->progname, NULL);
322 if (setsig(SIGHUP, NULL, h_hup) ||
323 setsig(SIGINT, NULL, h_int) ||
324 setsig(SIGTERM, NULL, h_term)
327 setsig(SIGWINCH, NULL, h_winch)
330 msgq(sp, M_SYSERR, "signal-reset");
337 * Set a signal handler.
340 setsig(int signo, struct sigaction *oactp, void (*handler) (int))
342 struct sigaction act;
345 * Use sigaction(2), not signal(3), since we don't always want to
346 * restart system calls. The example is when waiting for a command
347 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
348 * restart system calls (thanks, POSIX!). On the other hand, you
349 * can't portably NOT restart system calls (thanks, Sun!). SunOS
350 * used SA_INTERRUPT as their extension to NOT restart read calls.
351 * We sure hope nobody else used it for anything else. Mom told me
352 * there'd be days like this. She just never told me that there'd
355 act.sa_handler = handler;
356 sigemptyset(&act.sa_mask);
359 act.sa_flags = SA_INTERRUPT;
363 return (sigaction(signo, &act, oactp));
376 (void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
377 (void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
378 (void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
380 (void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
386 * Initialize the standard curses functions.
391 gp->scr_addstr = cl_addstr;
392 gp->scr_waddstr = cl_waddstr;
393 gp->scr_attr = cl_attr;
394 gp->scr_baud = cl_baud;
395 gp->scr_bell = cl_bell;
397 gp->scr_child = NULL;
398 gp->scr_clrtoeol = cl_clrtoeol;
399 gp->scr_cursor = cl_cursor;
400 gp->scr_deleteln = cl_deleteln;
401 gp->scr_reply = NULL;
402 gp->scr_discard = cl_discard;
403 gp->scr_event = cl_event;
404 gp->scr_ex_adjust = cl_ex_adjust;
405 gp->scr_fmap = cl_fmap;
406 gp->scr_insertln = cl_insertln;
407 gp->scr_keyval = cl_keyval;
408 gp->scr_move = cl_move;
410 gp->scr_optchange = cl_optchange;
411 gp->scr_refresh = cl_refresh;
412 gp->scr_rename = cl_rename;
413 gp->scr_screen = cl_screen;
414 gp->scr_split = cl_split;
415 gp->scr_suspend = cl_suspend;
416 gp->scr_usage = cl_usage;
421 * Print system error.
424 perr(char *name, char *msg)
426 (void)fprintf(stderr, "%s:", name);
428 (void)fprintf(stderr, "%s:", msg);
429 (void)fprintf(stderr, "%s\n", strerror(errno));