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.45 1996/12/18 17:06:41 bostic Exp $ (Berkeley) $Date: 1996/12/18 17:06:41 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
30 #include "../common/common.h"
31 #include "../ip/extern.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 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.
61 char *ip_arg
, **p_av
, **t_av
, *ttype
;
63 /* If loaded at 0 and jumping through a NULL pointer, stop. */
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.
76 for (p_av
= t_av
= argv
;;) {
81 if (!strcmp(*t_av
, "--")) {
82 while ((*p_av
++ = *t_av
++) != NULL
);
85 if (!memcmp(*t_av
, "-I", sizeof("-I") - 1)) {
86 if (t_av
[0][2] != '\0') {
92 if (t_av
[1] != NULL
) {
103 * If we're being called as an editor library, we're done here, we
104 * get loaded with the curses screen, we don't share much code.
107 exit (ip_main(argc
, argv
, gp
, ip_arg
));
109 /* Create and initialize the CL_PRIVATE structure. */
113 * Initialize the terminal information.
115 * We have to know what terminal it is from the start, since we may
116 * have to use termcap/terminfo to find out how big the screen is.
118 if ((ttype
= getenv("TERM")) == NULL
)
120 term_init(gp
->progname
, ttype
);
122 /* Add the terminal type to the global structure. */
123 if ((OG_D_STR(gp
, GO_TERM
) =
124 OG_STR(gp
, GO_TERM
) = strdup(ttype
)) == NULL
)
125 perr(gp
->progname
, NULL
);
127 /* Figure out how big the screen is. */
128 if (cl_ssize(NULL
, 0, &rows
, &cols
, NULL
))
131 /* Add the rows and columns to the global structure. */
132 OG_VAL(gp
, GO_LINES
) = OG_D_VAL(gp
, GO_LINES
) = rows
;
133 OG_VAL(gp
, GO_COLUMNS
) = OG_D_VAL(gp
, GO_COLUMNS
) = cols
;
135 /* Ex wants stdout to be buffered. */
136 (void)setvbuf(stdout
, NULL
, _IOFBF
, 0);
138 /* Start catching signals. */
139 if (sig_init(gp
, NULL
))
143 rval
= editor(gp
, argc
, argv
);
145 /* Clean up signals. */
148 /* Clean up the terminal. */
153 * Reset the O_MESG option.
155 if (clp
->tgw
!= TGW_UNKNOWN
)
156 (void)cl_omesg(NULL
, clp
, clp
->tgw
== TGW_SET
);
160 * Reset the X11 xterm icon/window name.
162 if (F_ISSET(clp
, CL_RENAME
))
163 cl_setname(gp
, clp
->oname
);
165 /* If a killer signal arrived, pretend we just got it. */
166 if (clp
->killersig
) {
167 (void)signal(clp
->killersig
, SIG_DFL
);
168 (void)kill(getpid(), clp
->killersig
);
172 /* Free the global and CL private areas. */
173 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
183 * Create and partially initialize the GS structure.
192 /* Figure out what our name is. */
193 if ((p
= strrchr(name
, '/')) != NULL
)
196 /* Allocate the global structure. */
197 CALLOC_NOMSG(NULL
, gp
, GS
*, 1, sizeof(GS
));
208 * Create and partially initialize the CL structure.
217 /* Allocate the CL private structure. */
218 CALLOC_NOMSG(NULL
, clp
, CL_PRIVATE
*, 1, sizeof(CL_PRIVATE
));
220 perr(gp
->progname
, NULL
);
221 gp
->cl_private
= clp
;
224 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
225 * and resetting the tty if the input isn't from there. We also
226 * use the same test to determine if we're running a script or
229 if (isatty(STDIN_FILENO
))
230 F_SET(clp
, CL_STDIN_TTY
);
232 F_SET(gp
, G_SCRIPTED
);
235 * We expect that if we've lost our controlling terminal that the
236 * open() (but not the tcgetattr()) will fail.
238 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
239 if (tcgetattr(STDIN_FILENO
, &clp
->orig
) == -1)
241 } else if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) != -1) {
242 if (tcgetattr(fd
, &clp
->orig
) == -1) {
243 tcfail
: perr(gp
->progname
, "tcgetattr");
249 /* Initialize the list of curses functions. */
257 * Discard the CL structure.
263 if (clp
->oname
!= NULL
)
270 * Initialize terminal information.
273 term_init(name
, ttype
)
278 /* Set up the terminal database information. */
279 setupterm(ttype
, STDOUT_FILENO
, &err
);
282 (void)fprintf(stderr
,
283 "%s: No terminal database found\n", name
);
286 (void)fprintf(stderr
,
287 "%s: %s: unknown terminal type\n", name
, ttype
);
293 CL_PRIVATE *clp = GCLP(__global_list);
300 F_SET(clp
, CL_SIGHUP
);
301 clp
->killersig
= SIGHUP
;
310 F_SET(clp
, CL_SIGINT
);
319 F_SET(clp
, CL_SIGTERM
);
320 clp
->killersig
= SIGTERM
;
329 F_SET(clp
, CL_SIGWINCH
);
335 * Initialize signals.
337 * PUBLIC: int sig_init __P((GS *, SCR *));
349 (void)sigemptyset(&__sigblockset
);
350 if (sigaddset(&__sigblockset
, SIGHUP
) ||
351 setsig(SIGHUP
, &clp
->oact
[INDX_HUP
], h_hup
) ||
352 sigaddset(&__sigblockset
, SIGINT
) ||
353 setsig(SIGINT
, &clp
->oact
[INDX_INT
], h_int
) ||
354 sigaddset(&__sigblockset
, SIGTERM
) ||
355 setsig(SIGTERM
, &clp
->oact
[INDX_TERM
], h_term
)
358 sigaddset(&__sigblockset
, SIGWINCH
) ||
359 setsig(SIGWINCH
, &clp
->oact
[INDX_WINCH
], h_winch
)
362 perr(gp
->progname
, NULL
);
366 if (setsig(SIGHUP
, NULL
, h_hup
) ||
367 setsig(SIGINT
, NULL
, h_int
) ||
368 setsig(SIGTERM
, NULL
, h_term
)
371 setsig(SIGWINCH
, NULL
, h_winch
)
374 msgq(sp
, M_SYSERR
, "signal-reset");
381 * Set a signal handler.
384 setsig(signo
, oactp
, handler
)
386 struct sigaction
*oactp
;
387 void (*handler
) __P((int));
389 struct sigaction act
;
392 * Use sigaction(2), not signal(3), since we don't always want to
393 * restart system calls. The example is when waiting for a command
394 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
395 * restart system calls (thanks, POSIX!). On the other hand, you
396 * can't portably NOT restart system calls (thanks, Sun!). SunOS
397 * used SA_INTERRUPT as their extension to NOT restart read calls.
398 * We sure hope nobody else used it for anything else. Mom told me
399 * there'd be days like this. She just never told me that there'd
402 act
.sa_handler
= handler
;
403 sigemptyset(&act
.sa_mask
);
406 act
.sa_flags
= SA_INTERRUPT
;
410 return (sigaction(signo
, &act
, oactp
));
424 (void)sigaction(SIGHUP
, NULL
, &clp
->oact
[INDX_HUP
]);
425 (void)sigaction(SIGINT
, NULL
, &clp
->oact
[INDX_INT
]);
426 (void)sigaction(SIGTERM
, NULL
, &clp
->oact
[INDX_TERM
]);
428 (void)sigaction(SIGWINCH
, NULL
, &clp
->oact
[INDX_WINCH
]);
434 * Initialize the standard curses functions.
440 gp
->scr_addstr
= cl_addstr
;
441 gp
->scr_attr
= cl_attr
;
442 gp
->scr_baud
= cl_baud
;
443 gp
->scr_bell
= cl_bell
;
445 gp
->scr_clrtoeol
= cl_clrtoeol
;
446 gp
->scr_cursor
= cl_cursor
;
447 gp
->scr_deleteln
= cl_deleteln
;
448 gp
->scr_discard
= cl_discard
;
449 gp
->scr_event
= cl_event
;
450 gp
->scr_ex_adjust
= cl_ex_adjust
;
451 gp
->scr_fmap
= cl_fmap
;
452 gp
->scr_insertln
= cl_insertln
;
453 gp
->scr_keyval
= cl_keyval
;
454 gp
->scr_move
= cl_move
;
456 gp
->scr_optchange
= cl_optchange
;
457 gp
->scr_refresh
= cl_refresh
;
458 gp
->scr_rename
= cl_rename
;
459 gp
->scr_screen
= cl_screen
;
460 gp
->scr_split
= cl_split
;
461 gp
->scr_suspend
= cl_suspend
;
462 gp
->scr_usage
= cl_usage
;
467 * Print system error.
473 (void)fprintf(stderr
, "%s:", name
);
475 (void)fprintf(stderr
, "%s:", msg
);
476 (void)fprintf(stderr
, "%s\n", strerror(errno
));