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.52 2000/07/23 11:14:44 skimo Exp $ (Berkeley) $Date: 2000/07/23 11:14:44 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
29 #include "../common/common.h"
30 #include "../ip/extern.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 *));
47 * This is the main loop for the standalone curses editor.
60 char **p_av
, **t_av
, *ttype
;
62 /* If loaded at 0 and jumping through a NULL pointer, stop. */
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
;;) {
79 if (!strcmp(*t_av
, "--")) {
80 while ((*p_av
++ = *t_av
++) != NULL
);
86 /* Create new window */
89 /* Create and initialize the CL_PRIVATE structure. */
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
)
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
))
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
))
123 rval
= editor(wp
, argc
, argv
);
125 /* Clean out the global structure. */
128 /* Clean up signals. */
131 /* Clean up the terminal. */
136 * Reset the O_MESG option.
138 if (clp
->tgw
!= TGW_UNKNOWN
)
139 (void)cl_omesg(NULL
, clp
, clp
->tgw
== TGW_SET
);
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
);
155 /* Free the global and CL private areas. */
156 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
166 * Create and partially initialize the CL structure.
175 /* Allocate the CL private structure. */
176 CALLOC_NOMSG(NULL
, clp
, CL_PRIVATE
*, 1, sizeof(CL_PRIVATE
));
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
187 if (isatty(STDIN_FILENO
))
188 F_SET(clp
, CL_STDIN_TTY
);
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)
199 } else if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) != -1) {
200 if (tcgetattr(fd
, &clp
->orig
) == -1) {
201 tcfail
: perr(gp
->progname
, "tcgetattr");
207 /* Initialize the list of curses functions. */
215 * Discard the CL structure.
221 if (clp
->oname
!= NULL
)
228 * Initialize terminal information.
231 term_init(name
, ttype
)
236 /* Set up the terminal database information. */
237 setupterm(ttype
, STDOUT_FILENO
, &err
);
240 (void)fprintf(stderr
,
241 "%s: No terminal database found\n", name
);
244 (void)fprintf(stderr
,
245 "%s: %s: unknown terminal type\n", name
, ttype
);
251 CL_PRIVATE *clp = GCLP(__global_list);
258 F_SET(clp
, CL_SIGHUP
);
259 clp
->killersig
= SIGHUP
;
268 F_SET(clp
, CL_SIGINT
);
277 F_SET(clp
, CL_SIGTERM
);
278 clp
->killersig
= SIGTERM
;
287 F_SET(clp
, CL_SIGWINCH
);
293 * Initialize signals.
295 * PUBLIC: int sig_init __P((GS *, SCR *));
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
)
316 sigaddset(&__sigblockset
, SIGWINCH
) ||
317 setsig(SIGWINCH
, &clp
->oact
[INDX_WINCH
], h_winch
)
320 perr(gp
->progname
, NULL
);
324 if (setsig(SIGHUP
, NULL
, h_hup
) ||
325 setsig(SIGINT
, NULL
, h_int
) ||
326 setsig(SIGTERM
, NULL
, h_term
)
329 setsig(SIGWINCH
, NULL
, h_winch
)
332 msgq(sp
, M_SYSERR
, "signal-reset");
339 * Set a signal handler.
342 setsig(signo
, oactp
, handler
)
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
360 act
.sa_handler
= handler
;
361 sigemptyset(&act
.sa_mask
);
364 act
.sa_flags
= SA_INTERRUPT
;
368 return (sigaction(signo
, &act
, oactp
));
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
]);
386 (void)sigaction(SIGWINCH
, NULL
, &clp
->oact
[INDX_WINCH
]);
392 * Initialize the standard curses functions.
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
;
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
;
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
;
428 * Print system error.
434 (void)fprintf(stderr
, "%s:", name
);
436 (void)fprintf(stderr
, "%s:", msg
);
437 (void)fprintf(stderr
, "%s\n", strerror(errno
));