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.54 2001/07/29 19:07:27 skimo Exp $ (Berkeley) $Date: 2001/07/29 19:07:27 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
32 #include "../common/common.h"
33 #include "../ip/extern.h"
35 #include "pathnames.h"
37 GS
*__global_list
; /* GLOBAL: List of screens. */
38 sigset_t __sigblockset
; /* GLOBAL: Blocked signals. */
40 static void cl_func_std
__P((WIN
*));
41 static void cl_end
__P((CL_PRIVATE
*));
42 static CL_PRIVATE
*cl_init
__P((WIN
*));
43 static void perr
__P((char *, char *));
44 static int setsig
__P((int, struct sigaction
*, void (*)(int)));
45 static void sig_end
__P((GS
*));
46 static void term_init
__P((char *, char *));
50 * This is the main loop for the standalone curses editor.
53 main(int argc
, char **argv
)
61 char **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.
75 for (p_av
= t_av
= argv
;;) {
80 if (!strcmp(*t_av
, "--")) {
81 while ((*p_av
++ = *t_av
++) != NULL
);
87 /* Create new window */
90 /* Create and initialize the CL_PRIVATE structure. */
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
)
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
))
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
))
124 rval
= editor(wp
, argc
, argv
);
126 /* Clean out the global structure. */
129 /* Clean up signals. */
132 /* Clean up the terminal. */
137 * Reset the O_MESG option.
139 if (clp
->tgw
!= TGW_UNKNOWN
)
140 (void)cl_omesg(NULL
, clp
, clp
->tgw
== TGW_SET
);
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
);
156 /* Free the global and CL private areas. */
157 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
167 * Create and partially initialize the CL structure.
178 /* Allocate the CL private structure. */
179 CALLOC_NOMSG(NULL
, clp
, CL_PRIVATE
*, 1, sizeof(CL_PRIVATE
));
181 perr(gp
->progname
, NULL
);
182 gp
->cl_private
= clp
;
185 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
186 * and resetting the tty if the input isn't from there. We also
187 * use the same test to determine if we're running a script or
190 if (isatty(STDIN_FILENO
))
191 F_SET(clp
, CL_STDIN_TTY
);
193 F_SET(gp
, G_SCRIPTED
);
196 * We expect that if we've lost our controlling terminal that the
197 * open() (but not the tcgetattr()) will fail.
199 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
200 if (tcgetattr(STDIN_FILENO
, &clp
->orig
) == -1)
202 } else if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) != -1) {
203 if (tcgetattr(fd
, &clp
->orig
) == -1) {
204 tcfail
: perr(gp
->progname
, "tcgetattr");
210 /* Initialize the list of curses functions. */
218 * Discard the CL structure.
221 cl_end(CL_PRIVATE
*clp
)
223 if (clp
->oname
!= NULL
)
230 * Initialize terminal information.
233 term_init(char *name
, char *ttype
)
237 /* Set up the terminal database information. */
238 setupterm(ttype
, STDOUT_FILENO
, &err
);
241 (void)fprintf(stderr
,
242 "%s: No terminal database found\n", name
);
245 (void)fprintf(stderr
,
246 "%s: %s: unknown terminal type\n", name
, ttype
);
252 CL_PRIVATE *clp = GCLP(__global_list);
258 F_SET(clp
, CL_SIGHUP
);
259 clp
->killersig
= SIGHUP
;
267 F_SET(clp
, CL_SIGINT
);
275 F_SET(clp
, CL_SIGTERM
);
276 clp
->killersig
= SIGTERM
;
284 F_SET(clp
, CL_SIGWINCH
);
290 * Initialize signals.
292 * PUBLIC: int sig_init __P((GS *, SCR *));
295 sig_init(GS
*gp
, SCR
*sp
)
302 (void)sigemptyset(&__sigblockset
);
303 if (sigaddset(&__sigblockset
, SIGHUP
) ||
304 setsig(SIGHUP
, &clp
->oact
[INDX_HUP
], h_hup
) ||
305 sigaddset(&__sigblockset
, SIGINT
) ||
306 setsig(SIGINT
, &clp
->oact
[INDX_INT
], h_int
) ||
307 sigaddset(&__sigblockset
, SIGTERM
) ||
308 setsig(SIGTERM
, &clp
->oact
[INDX_TERM
], h_term
)
311 sigaddset(&__sigblockset
, SIGWINCH
) ||
312 setsig(SIGWINCH
, &clp
->oact
[INDX_WINCH
], h_winch
)
315 perr(gp
->progname
, NULL
);
319 if (setsig(SIGHUP
, NULL
, h_hup
) ||
320 setsig(SIGINT
, NULL
, h_int
) ||
321 setsig(SIGTERM
, NULL
, h_term
)
324 setsig(SIGWINCH
, NULL
, h_winch
)
327 msgq(sp
, M_SYSERR
, "signal-reset");
334 * Set a signal handler.
337 setsig(int signo
, struct sigaction
*oactp
, void (*handler
) (int))
339 struct sigaction act
;
342 * Use sigaction(2), not signal(3), since we don't always want to
343 * restart system calls. The example is when waiting for a command
344 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
345 * restart system calls (thanks, POSIX!). On the other hand, you
346 * can't portably NOT restart system calls (thanks, Sun!). SunOS
347 * used SA_INTERRUPT as their extension to NOT restart read calls.
348 * We sure hope nobody else used it for anything else. Mom told me
349 * there'd be days like this. She just never told me that there'd
352 act
.sa_handler
= handler
;
353 sigemptyset(&act
.sa_mask
);
356 act
.sa_flags
= SA_INTERRUPT
;
360 return (sigaction(signo
, &act
, oactp
));
373 (void)sigaction(SIGHUP
, NULL
, &clp
->oact
[INDX_HUP
]);
374 (void)sigaction(SIGINT
, NULL
, &clp
->oact
[INDX_INT
]);
375 (void)sigaction(SIGTERM
, NULL
, &clp
->oact
[INDX_TERM
]);
377 (void)sigaction(SIGWINCH
, NULL
, &clp
->oact
[INDX_WINCH
]);
383 * Initialize the standard curses functions.
392 gp
->scr_addstr
= cl_addstr
;
393 gp
->scr_waddstr
= cl_waddstr
;
394 gp
->scr_attr
= cl_attr
;
395 gp
->scr_baud
= cl_baud
;
396 gp
->scr_bell
= cl_bell
;
398 gp
->scr_child
= NULL
;
399 gp
->scr_clrtoeol
= cl_clrtoeol
;
400 gp
->scr_cursor
= cl_cursor
;
401 gp
->scr_deleteln
= cl_deleteln
;
402 gp
->scr_reply
= NULL
;
403 gp
->scr_discard
= cl_discard
;
404 gp
->scr_event
= cl_event
;
405 gp
->scr_ex_adjust
= cl_ex_adjust
;
406 gp
->scr_fmap
= cl_fmap
;
407 gp
->scr_insertln
= cl_insertln
;
408 gp
->scr_keyval
= cl_keyval
;
409 gp
->scr_move
= cl_move
;
411 gp
->scr_optchange
= cl_optchange
;
412 gp
->scr_refresh
= cl_refresh
;
413 gp
->scr_rename
= cl_rename
;
414 gp
->scr_screen
= cl_screen
;
415 gp
->scr_split
= cl_split
;
416 gp
->scr_suspend
= cl_suspend
;
417 gp
->scr_usage
= cl_usage
;
422 * Print system error.
425 perr(char *name
, char *msg
)
427 (void)fprintf(stderr
, "%s:", name
);
429 (void)fprintf(stderr
, "%s:", msg
);
430 (void)fprintf(stderr
, "%s\n", strerror(errno
));