align CHAR_T string in log
[nvi.git] / cl / cl_main.c
blob1995e416b5a2bf67bf45d782947436c84ee1277c
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.52 2000/07/23 11:14:44 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:44 $";
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((GS *));
38 static void cl_end __P((CL_PRIVATE *));
39 static CL_PRIVATE *cl_init __P((GS *));
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(argc, argv)
51 int argc;
52 char *argv[];
54 static int reenter;
55 CL_PRIVATE *clp;
56 GS *gp;
57 WIN *wp;
58 size_t rows, cols;
59 int rval;
60 char **p_av, **t_av, *ttype;
62 /* If loaded at 0 and jumping through a NULL pointer, stop. */
63 if (reenter++)
64 abort();
66 /* Create and initialize the global structure. */
67 __global_list = gp = gs_init(argv[0]);
70 * Strip out any arguments that vi isn't going to understand. There's
71 * no way to portably call getopt twice, so arguments parsed here must
72 * be removed from the argument list.
74 for (p_av = t_av = argv;;) {
75 if (*t_av == NULL) {
76 *p_av = NULL;
77 break;
79 if (!strcmp(*t_av, "--")) {
80 while ((*p_av++ = *t_av++) != NULL);
81 break;
83 *p_av++ = *t_av++;
86 /* Create new window */
87 wp = gs_new_win(gp);
89 /* Create and initialize the CL_PRIVATE structure. */
90 clp = cl_init(gp);
93 * Initialize the terminal information.
95 * We have to know what terminal it is from the start, since we may
96 * have to use termcap/terminfo to find out how big the screen is.
98 if ((ttype = getenv("TERM")) == NULL)
99 ttype = "unknown";
100 term_init(gp->progname, ttype);
102 /* Add the terminal type to the global structure. */
103 if ((OG_D_STR(gp, GO_TERM) =
104 OG_STR(gp, GO_TERM) = strdup(ttype)) == NULL)
105 perr(gp->progname, NULL);
107 /* Figure out how big the screen is. */
108 if (cl_ssize(NULL, 0, &rows, &cols, NULL))
109 exit (1);
111 /* Add the rows and columns to the global structure. */
112 OG_VAL(gp, GO_LINES) = OG_D_VAL(gp, GO_LINES) = rows;
113 OG_VAL(gp, GO_COLUMNS) = OG_D_VAL(gp, GO_COLUMNS) = cols;
115 /* Ex wants stdout to be buffered. */
116 (void)setvbuf(stdout, NULL, _IOFBF, 0);
118 /* Start catching signals. */
119 if (sig_init(gp, NULL))
120 exit (1);
122 /* Run ex/vi. */
123 rval = editor(wp, argc, argv);
125 /* Clean out the global structure. */
126 gs_end(gp);
128 /* Clean up signals. */
129 sig_end(gp);
131 /* Clean up the terminal. */
132 (void)cl_quit(gp);
135 * XXX
136 * Reset the O_MESG option.
138 if (clp->tgw != TGW_UNKNOWN)
139 (void)cl_omesg(NULL, clp, clp->tgw == TGW_SET);
142 * XXX
143 * Reset the X11 xterm icon/window name.
145 if (F_ISSET(clp, CL_RENAME))
146 cl_setname(gp, clp->oname);
148 /* If a killer signal arrived, pretend we just got it. */
149 if (clp->killersig) {
150 (void)signal(clp->killersig, SIG_DFL);
151 (void)kill(getpid(), clp->killersig);
152 /* NOTREACHED */
155 /* Free the global and CL private areas. */
156 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
157 cl_end(clp);
158 free(gp);
159 #endif
161 exit (rval);
165 * cl_init --
166 * Create and partially initialize the CL structure.
168 static CL_PRIVATE *
169 cl_init(gp)
170 GS *gp;
172 CL_PRIVATE *clp;
173 int fd;
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(gp);
210 return (clp);
214 * cl_end --
215 * Discard the CL structure.
217 static void
218 cl_end(clp)
219 CL_PRIVATE *clp;
221 if (clp->oname != NULL)
222 free(clp->oname);
223 free(clp);
227 * term_init --
228 * Initialize terminal information.
230 static void
231 term_init(name, ttype)
232 char *name, *ttype;
234 int err;
236 /* Set up the terminal database information. */
237 setupterm(ttype, STDOUT_FILENO, &err);
238 switch (err) {
239 case -1:
240 (void)fprintf(stderr,
241 "%s: No terminal database found\n", name);
242 exit (1);
243 case 0:
244 (void)fprintf(stderr,
245 "%s: %s: unknown terminal type\n", name, ttype);
246 exit (1);
250 #define GLOBAL_CLP \
251 CL_PRIVATE *clp = GCLP(__global_list);
252 static void
253 h_hup(signo)
254 int signo;
256 GLOBAL_CLP;
258 F_SET(clp, CL_SIGHUP);
259 clp->killersig = SIGHUP;
262 static void
263 h_int(signo)
264 int signo;
266 GLOBAL_CLP;
268 F_SET(clp, CL_SIGINT);
271 static void
272 h_term(signo)
273 int signo;
275 GLOBAL_CLP;
277 F_SET(clp, CL_SIGTERM);
278 clp->killersig = SIGTERM;
281 static void
282 h_winch(signo)
283 int signo;
285 GLOBAL_CLP;
287 F_SET(clp, CL_SIGWINCH);
289 #undef GLOBAL_CLP
292 * sig_init --
293 * Initialize signals.
295 * PUBLIC: int sig_init __P((GS *, SCR *));
298 sig_init(gp, sp)
299 GS *gp;
300 SCR *sp;
302 CL_PRIVATE *clp;
304 clp = GCLP(gp);
306 if (sp == NULL) {
307 (void)sigemptyset(&__sigblockset);
308 if (sigaddset(&__sigblockset, SIGHUP) ||
309 setsig(SIGHUP, &clp->oact[INDX_HUP], h_hup) ||
310 sigaddset(&__sigblockset, SIGINT) ||
311 setsig(SIGINT, &clp->oact[INDX_INT], h_int) ||
312 sigaddset(&__sigblockset, SIGTERM) ||
313 setsig(SIGTERM, &clp->oact[INDX_TERM], h_term)
314 #ifdef SIGWINCH
316 sigaddset(&__sigblockset, SIGWINCH) ||
317 setsig(SIGWINCH, &clp->oact[INDX_WINCH], h_winch)
318 #endif
320 perr(gp->progname, NULL);
321 return (1);
323 } else
324 if (setsig(SIGHUP, NULL, h_hup) ||
325 setsig(SIGINT, NULL, h_int) ||
326 setsig(SIGTERM, NULL, h_term)
327 #ifdef SIGWINCH
329 setsig(SIGWINCH, NULL, h_winch)
330 #endif
332 msgq(sp, M_SYSERR, "signal-reset");
334 return (0);
338 * setsig --
339 * Set a signal handler.
341 static int
342 setsig(signo, oactp, handler)
343 int signo;
344 struct sigaction *oactp;
345 void (*handler) __P((int));
347 struct sigaction act;
350 * Use sigaction(2), not signal(3), since we don't always want to
351 * restart system calls. The example is when waiting for a command
352 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
353 * restart system calls (thanks, POSIX!). On the other hand, you
354 * can't portably NOT restart system calls (thanks, Sun!). SunOS
355 * used SA_INTERRUPT as their extension to NOT restart read calls.
356 * We sure hope nobody else used it for anything else. Mom told me
357 * there'd be days like this. She just never told me that there'd
358 * be so many.
360 act.sa_handler = handler;
361 sigemptyset(&act.sa_mask);
363 #ifdef SA_INTERRUPT
364 act.sa_flags = SA_INTERRUPT;
365 #else
366 act.sa_flags = 0;
367 #endif
368 return (sigaction(signo, &act, oactp));
372 * sig_end --
373 * End signal setup.
375 static void
376 sig_end(gp)
377 GS *gp;
379 CL_PRIVATE *clp;
381 clp = GCLP(gp);
382 (void)sigaction(SIGHUP, NULL, &clp->oact[INDX_HUP]);
383 (void)sigaction(SIGINT, NULL, &clp->oact[INDX_INT]);
384 (void)sigaction(SIGTERM, NULL, &clp->oact[INDX_TERM]);
385 #ifdef SIGWINCH
386 (void)sigaction(SIGWINCH, NULL, &clp->oact[INDX_WINCH]);
387 #endif
391 * cl_func_std --
392 * Initialize the standard curses functions.
394 static void
395 cl_func_std(gp)
396 GS *gp;
398 gp->scr_addstr = cl_addstr;
399 gp->scr_waddstr = cl_waddstr;
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_child = NULL;
405 gp->scr_clrtoeol = cl_clrtoeol;
406 gp->scr_cursor = cl_cursor;
407 gp->scr_deleteln = cl_deleteln;
408 gp->scr_reply = NULL;
409 gp->scr_discard = cl_discard;
410 gp->scr_event = cl_event;
411 gp->scr_ex_adjust = cl_ex_adjust;
412 gp->scr_fmap = cl_fmap;
413 gp->scr_insertln = cl_insertln;
414 gp->scr_keyval = cl_keyval;
415 gp->scr_move = cl_move;
416 gp->scr_msg = NULL;
417 gp->scr_optchange = cl_optchange;
418 gp->scr_refresh = cl_refresh;
419 gp->scr_rename = cl_rename;
420 gp->scr_screen = cl_screen;
421 gp->scr_split = cl_split;
422 gp->scr_suspend = cl_suspend;
423 gp->scr_usage = cl_usage;
427 * perr --
428 * Print system error.
430 static void
431 perr(name, msg)
432 char *name, *msg;
434 (void)fprintf(stderr, "%s:", name);
435 if (msg != NULL)
436 (void)fprintf(stderr, "%s:", msg);
437 (void)fprintf(stderr, "%s\n", strerror(errno));
438 exit(1);