1 /* $NetBSD: cl_main.c,v 1.4 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 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 ";
19 __RCSID("$NetBSD: cl_main.c,v 1.4 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
35 #include "../common/common.h"
36 #include "ip_extern.h"
38 #include "pathnames.h"
40 GS
*__global_list
; /* GLOBAL: List of screens. */
41 sigset_t __sigblockset
; /* GLOBAL: Blocked signals. */
43 static void cl_func_std
__P((WIN
*));
45 static void cl_end
__P((CL_PRIVATE
*));
47 static CL_PRIVATE
*cl_init
__P((WIN
*));
48 __dead
static void perr
__P((const char *, const char *));
49 static int setsig
__P((int, struct sigaction
*, void (*)(int)));
50 static void sig_end
__P((GS
*));
51 static void term_init
__P((const char *, const char *));
55 * This is the main loop for the standalone curses editor.
58 main(int argc
, char **argv
)
69 /* If loaded at 0 and jumping through a NULL pointer, stop. */
73 /* Create and initialize the global structure. */
74 __global_list
= gp
= gs_init(argv
[0]);
77 * Strip out any arguments that vi isn't going to understand. There's
78 * no way to portably call getopt twice, so arguments parsed here must
79 * be removed from the argument list.
81 for (p_av
= t_av
= argv
;;) {
86 if (!strcmp(*t_av
, "--")) {
87 while ((*p_av
++ = *t_av
++) != NULL
);
93 /* Create new window */
96 /* Create and initialize the CL_PRIVATE structure. */
100 * Initialize the terminal information.
102 * We have to know what terminal it is from the start, since we may
103 * have to use termcap/terminfo to find out how big the screen is.
105 if ((ttype
= getenv("TERM")) == NULL
) {
106 if (isatty(STDIN_FILENO
))
107 fprintf(stderr
, "%s: warning: TERM is not set\n",
111 term_init(gp
->progname
, ttype
);
113 /* Add the terminal type to the global structure. */
114 if ((OG_D_STR(gp
, GO_TERM
) =
115 OG_STR(gp
, GO_TERM
) = strdup(ttype
)) == NULL
)
116 perr(gp
->progname
, NULL
);
118 /* Figure out how big the screen is. */
119 if (cl_ssize(NULL
, 0, &rows
, &cols
, NULL
))
122 /* Add the rows and columns to the global structure. */
123 OG_VAL(gp
, GO_LINES
) = OG_D_VAL(gp
, GO_LINES
) = rows
;
124 OG_VAL(gp
, GO_COLUMNS
) = OG_D_VAL(gp
, GO_COLUMNS
) = cols
;
126 /* Ex wants stdout to be buffered. */
127 (void)setvbuf(stdout
, NULL
, _IOFBF
, 0);
129 /* Start catching signals. */
130 if (sig_init(gp
, NULL
))
134 rval
= editor(wp
, argc
, argv
);
136 /* Clean out the global structure. */
139 /* Clean up signals. */
142 /* Clean up the terminal. */
147 * Reset the O_MESG option.
149 if (clp
->tgw
!= TGW_UNKNOWN
)
150 (void)cl_omesg(NULL
, clp
, clp
->tgw
== TGW_SET
);
154 * Reset the X11 xterm icon/window name.
156 if (F_ISSET(clp
, CL_RENAME
))
157 cl_setname(gp
, clp
->oname
);
159 /* If a killer signal arrived, pretend we just got it. */
160 if (clp
->killersig
) {
161 (void)signal(clp
->killersig
, SIG_DFL
);
162 (void)kill(getpid(), clp
->killersig
);
166 /* Free the global and CL private areas. */
167 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
177 * Create and partially initialize the CL structure.
188 /* Allocate the CL private structure. */
189 CALLOC_NOMSG(NULL
, clp
, CL_PRIVATE
*, 1, sizeof(CL_PRIVATE
));
191 perr(gp
->progname
, NULL
);
192 gp
->cl_private
= clp
;
195 * Set the CL_STDIN_TTY flag. It's purpose is to avoid setting
196 * and resetting the tty if the input isn't from there. We also
197 * use the same test to determine if we're running a script or
200 if (isatty(STDIN_FILENO
))
201 F_SET(clp
, CL_STDIN_TTY
);
203 F_SET(gp
, G_SCRIPTED
);
206 * We expect that if we've lost our controlling terminal that the
207 * open() (but not the tcgetattr()) will fail.
209 if (F_ISSET(clp
, CL_STDIN_TTY
)) {
210 if (tcgetattr(STDIN_FILENO
, &clp
->orig
) == -1)
212 } else if ((fd
= open(_PATH_TTY
, O_RDONLY
, 0)) != -1) {
213 if (tcgetattr(fd
, &clp
->orig
) == -1) {
214 tcfail
: perr(gp
->progname
, "tcgetattr");
220 /* Initialize the list of curses functions. */
229 * Discard the CL structure.
232 cl_end(CL_PRIVATE
*clp
)
234 if (clp
->oname
!= NULL
)
242 * Initialize terminal information.
245 term_init(const char *name
, const char *ttype
)
249 /* Set up the terminal database information. */
250 setupterm(__UNCONST(ttype
), STDOUT_FILENO
, &error
);
253 (void)fprintf(stderr
,
254 "%s: No terminal database found\n", name
);
257 (void)fprintf(stderr
,
258 "%s: %s: unknown terminal type\n", name
, ttype
);
264 CL_PRIVATE *clp = GCLP(__global_list);
270 F_SET(clp
, CL_SIGHUP
);
271 clp
->killersig
= SIGHUP
;
279 F_SET(clp
, CL_SIGINT
);
287 F_SET(clp
, CL_SIGTERM
);
288 clp
->killersig
= SIGTERM
;
296 F_SET(clp
, CL_SIGWINCH
);
302 * Initialize signals.
304 * PUBLIC: int sig_init __P((GS *, SCR *));
307 sig_init(GS
*gp
, SCR
*sp
)
314 (void)sigemptyset(&__sigblockset
);
315 if (sigaddset(&__sigblockset
, SIGHUP
) ||
316 setsig(SIGHUP
, &clp
->oact
[INDX_HUP
], h_hup
) ||
317 sigaddset(&__sigblockset
, SIGINT
) ||
318 setsig(SIGINT
, &clp
->oact
[INDX_INT
], h_int
) ||
319 sigaddset(&__sigblockset
, SIGTERM
) ||
320 setsig(SIGTERM
, &clp
->oact
[INDX_TERM
], h_term
)
323 sigaddset(&__sigblockset
, SIGWINCH
) ||
324 setsig(SIGWINCH
, &clp
->oact
[INDX_WINCH
], h_winch
)
327 perr(gp
->progname
, NULL
);
331 if (setsig(SIGHUP
, NULL
, h_hup
) ||
332 setsig(SIGINT
, NULL
, h_int
) ||
333 setsig(SIGTERM
, NULL
, h_term
)
336 setsig(SIGWINCH
, NULL
, h_winch
)
339 msgq(sp
, M_SYSERR
, "signal-reset");
346 * Set a signal handler.
349 setsig(int signo
, struct sigaction
*oactp
, void (*handler
) (int))
351 struct sigaction act
;
354 * Use sigaction(2), not signal(3), since we don't always want to
355 * restart system calls. The example is when waiting for a command
356 * mode keystroke and SIGWINCH arrives. Besides, you can't portably
357 * restart system calls (thanks, POSIX!). On the other hand, you
358 * can't portably NOT restart system calls (thanks, Sun!). SunOS
359 * used SA_INTERRUPT as their extension to NOT restart read calls.
360 * We sure hope nobody else used it for anything else. Mom told me
361 * there'd be days like this. She just never told me that there'd
364 act
.sa_handler
= handler
;
365 sigemptyset(&act
.sa_mask
);
368 act
.sa_flags
= SA_INTERRUPT
;
372 return (sigaction(signo
, &act
, oactp
));
385 (void)sigaction(SIGHUP
, NULL
, &clp
->oact
[INDX_HUP
]);
386 (void)sigaction(SIGINT
, NULL
, &clp
->oact
[INDX_INT
]);
387 (void)sigaction(SIGTERM
, NULL
, &clp
->oact
[INDX_TERM
]);
389 (void)sigaction(SIGWINCH
, NULL
, &clp
->oact
[INDX_WINCH
]);
395 * Initialize the standard curses functions.
404 gp
->scr_addstr
= cl_addstr
;
405 gp
->scr_waddstr
= cl_waddstr
;
406 gp
->scr_attr
= cl_attr
;
407 gp
->scr_baud
= cl_baud
;
408 gp
->scr_bell
= cl_bell
;
410 gp
->scr_child
= NULL
;
411 gp
->scr_clrtoeol
= cl_clrtoeol
;
412 gp
->scr_cursor
= cl_cursor
;
413 gp
->scr_deleteln
= cl_deleteln
;
414 gp
->scr_reply
= NULL
;
415 gp
->scr_discard
= cl_discard
;
416 gp
->scr_event
= cl_event
;
417 gp
->scr_ex_adjust
= cl_ex_adjust
;
418 gp
->scr_fmap
= cl_fmap
;
419 gp
->scr_insertln
= cl_insertln
;
420 gp
->scr_keyval
= cl_keyval
;
421 gp
->scr_move
= cl_move
;
423 gp
->scr_optchange
= cl_optchange
;
424 gp
->scr_refresh
= cl_refresh
;
425 gp
->scr_rename
= cl_rename
;
426 gp
->scr_screen
= cl_screen
;
427 gp
->scr_split
= cl_split
;
428 gp
->scr_suspend
= cl_suspend
;
429 gp
->scr_usage
= cl_usage
;
434 * Print system error.
437 perr(const char *name
, const char *msg
)
439 (void)fprintf(stderr
, "%s:", name
);
441 (void)fprintf(stderr
, "%s:", msg
);
442 (void)fprintf(stderr
, "%s\n", strerror(errno
));