xref: /minix3/external/bsd/nvi/dist/ip/ip_main.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: ip_main.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
284d9c625SLionel Sambuc /*-
384d9c625SLionel Sambuc  * Copyright (c) 1996
484d9c625SLionel Sambuc  *	Keith Bostic.  All rights reserved.
584d9c625SLionel Sambuc  *
684d9c625SLionel Sambuc  * See the LICENSE file for redistribution information.
784d9c625SLionel Sambuc  */
884d9c625SLionel Sambuc 
984d9c625SLionel Sambuc #include "config.h"
1084d9c625SLionel Sambuc 
11*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
12*0a6a1f1dSLionel Sambuc #if 0
1384d9c625SLionel Sambuc #ifndef lint
1484d9c625SLionel Sambuc static const char sccsid[] = "Id: ip_main.c,v 8.24 2001/07/29 19:07:30 skimo Exp  (Berkeley) Date: 2001/07/29 19:07:30 ";
1584d9c625SLionel Sambuc #endif /* not lint */
16*0a6a1f1dSLionel Sambuc #else
17*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: ip_main.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
18*0a6a1f1dSLionel Sambuc #endif
1984d9c625SLionel Sambuc 
2084d9c625SLionel Sambuc #include <sys/types.h>
2184d9c625SLionel Sambuc #include <sys/queue.h>
2284d9c625SLionel Sambuc 
2384d9c625SLionel Sambuc #include <bitstring.h>
2484d9c625SLionel Sambuc #include <ctype.h>
2584d9c625SLionel Sambuc #include <errno.h>
2684d9c625SLionel Sambuc #include <stdio.h>
2784d9c625SLionel Sambuc #include <stdlib.h>
2884d9c625SLionel Sambuc #include <string.h>
2984d9c625SLionel Sambuc #include <unistd.h>
3084d9c625SLionel Sambuc 
3184d9c625SLionel Sambuc #include <sys/uio.h>
3284d9c625SLionel Sambuc 
3384d9c625SLionel Sambuc #include "../common/common.h"
3484d9c625SLionel Sambuc #include "../ipc/ip.h"
3584d9c625SLionel Sambuc 
3684d9c625SLionel Sambuc GS *__global_list;				/* GLOBAL: List of screens. */
3784d9c625SLionel Sambuc 
3884d9c625SLionel Sambuc static void	   ip_func_std __P((WIN *));
3984d9c625SLionel Sambuc static IP_PRIVATE *ip_init __P((WIN *wp, int i_fd, int o_fd, int, int argc, char *argv[]));
4084d9c625SLionel Sambuc static void	   perr __P((char *, char *));
4184d9c625SLionel Sambuc static int	   get_fds __P((char *ip_arg, int *i_fd, int *o_fd));
4284d9c625SLionel Sambuc static int  get_connection __P((WIN *wp, int main_ifd, int main_ofd,
4384d9c625SLionel Sambuc 				int *i_fd, int *o_fd, int *, int can_pass));
4484d9c625SLionel Sambuc static void *run_editor __P((void * vp));
4584d9c625SLionel Sambuc 
4684d9c625SLionel Sambuc /*
4784d9c625SLionel Sambuc  * ip_main --
4884d9c625SLionel Sambuc  *      This is the main loop for the vi-as-library editor.
4984d9c625SLionel Sambuc  */
5084d9c625SLionel Sambuc int
main(int argc,char ** argv)5184d9c625SLionel Sambuc main(int argc, char **argv)
5284d9c625SLionel Sambuc {
5384d9c625SLionel Sambuc 	IP_PRIVATE *ipp;
5484d9c625SLionel Sambuc 	char *ip_arg;
5584d9c625SLionel Sambuc 	char **p_av, **t_av;
5684d9c625SLionel Sambuc 	GS *gp;
5784d9c625SLionel Sambuc 	WIN *wp;
5884d9c625SLionel Sambuc 	int i_fd, o_fd, t_fd, main_ifd, main_ofd;
5984d9c625SLionel Sambuc 
6084d9c625SLionel Sambuc 	/* Create and initialize the global structure. */
6184d9c625SLionel Sambuc 	__global_list = gp = gs_init(argv[0]);
6284d9c625SLionel Sambuc 
6384d9c625SLionel Sambuc 	/*
6484d9c625SLionel Sambuc 	 * Strip out any arguments that vi isn't going to understand.  There's
6584d9c625SLionel Sambuc 	 * no way to portably call getopt twice, so arguments parsed here must
6684d9c625SLionel Sambuc 	 * be removed from the argument list.
6784d9c625SLionel Sambuc 	 */
6884d9c625SLionel Sambuc 	ip_arg = NULL;
6984d9c625SLionel Sambuc 	for (p_av = t_av = argv;;) {
7084d9c625SLionel Sambuc 		if (*t_av == NULL) {
7184d9c625SLionel Sambuc 			*p_av = NULL;
7284d9c625SLionel Sambuc 			break;
7384d9c625SLionel Sambuc 		}
7484d9c625SLionel Sambuc 		if (!strcmp(*t_av, "--")) {
7584d9c625SLionel Sambuc 			while ((*p_av++ = *t_av++) != NULL);
7684d9c625SLionel Sambuc 			break;
7784d9c625SLionel Sambuc 		}
7884d9c625SLionel Sambuc 		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
7984d9c625SLionel Sambuc 			if (t_av[0][2] != '\0') {
8084d9c625SLionel Sambuc 				ip_arg = t_av[0] + 2;
8184d9c625SLionel Sambuc 				++t_av;
8284d9c625SLionel Sambuc 				--argc;
8384d9c625SLionel Sambuc 				continue;
8484d9c625SLionel Sambuc 			}
8584d9c625SLionel Sambuc 			else if (t_av[1] != NULL) {
8684d9c625SLionel Sambuc 				ip_arg = t_av[1];
8784d9c625SLionel Sambuc 				t_av += 2;
8884d9c625SLionel Sambuc 				argc -= 2;
8984d9c625SLionel Sambuc 				continue;
9084d9c625SLionel Sambuc 			}
9184d9c625SLionel Sambuc 		}
9284d9c625SLionel Sambuc 		*p_av++ = *t_av++;
9384d9c625SLionel Sambuc 	}
9484d9c625SLionel Sambuc 
9584d9c625SLionel Sambuc 	if (get_fds(ip_arg, &main_ifd, &main_ofd))
9684d9c625SLionel Sambuc 		return 1;
9784d9c625SLionel Sambuc 
9884d9c625SLionel Sambuc 	wp = NULL;
9984d9c625SLionel Sambuc 
10084d9c625SLionel Sambuc 	while (get_connection(wp, main_ifd, main_ofd, &i_fd, &o_fd, &t_fd, 1) == 0) {
10184d9c625SLionel Sambuc 		/* Create new window */
10284d9c625SLionel Sambuc 		wp = gs_new_win(gp);
10384d9c625SLionel Sambuc 
10484d9c625SLionel Sambuc 		/* Create and partially initialize the IP structure. */
10584d9c625SLionel Sambuc 		if ((ipp = ip_init(wp, i_fd, o_fd, t_fd, argc, argv)) == NULL)
10684d9c625SLionel Sambuc 			return (1);
10784d9c625SLionel Sambuc 
10884d9c625SLionel Sambuc 		gp->run(wp, run_editor, (void *)wp);
10984d9c625SLionel Sambuc 	}
11084d9c625SLionel Sambuc 
11184d9c625SLionel Sambuc 	/* Clean out the global structure. */
11284d9c625SLionel Sambuc 	gs_end(gp);
11384d9c625SLionel Sambuc 
11484d9c625SLionel Sambuc 	/* Free the global and IP private areas. */
11584d9c625SLionel Sambuc #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
11684d9c625SLionel Sambuc 	free(gp);
11784d9c625SLionel Sambuc #endif
11884d9c625SLionel Sambuc 	exit (0);
11984d9c625SLionel Sambuc }
12084d9c625SLionel Sambuc 
12184d9c625SLionel Sambuc static void *
run_editor(void * vp)12284d9c625SLionel Sambuc run_editor(void * vp)
12384d9c625SLionel Sambuc {
12484d9c625SLionel Sambuc 	GS *gp;
12584d9c625SLionel Sambuc 	IP_PRIVATE *ipp;
12684d9c625SLionel Sambuc 	WIN *wp;
12784d9c625SLionel Sambuc 	EVENT ev;
12884d9c625SLionel Sambuc 	int rval;
12984d9c625SLionel Sambuc 	IP_BUF ipb;
13084d9c625SLionel Sambuc 
13184d9c625SLionel Sambuc 	wp = (WIN *) vp;
13284d9c625SLionel Sambuc 	gp = wp->gp;
13384d9c625SLionel Sambuc 	ipp = wp->ip_private;
13484d9c625SLionel Sambuc 
13584d9c625SLionel Sambuc 	/* Add the terminal type to the global structure. */
13684d9c625SLionel Sambuc 	if ((OG_D_STR(gp, GO_TERM) =
13784d9c625SLionel Sambuc 	    OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL)
13884d9c625SLionel Sambuc 		perr(gp->progname, NULL);
13984d9c625SLionel Sambuc 
14084d9c625SLionel Sambuc 	/*
14184d9c625SLionel Sambuc 	 * Figure out how big the screen is -- read events until we get
14284d9c625SLionel Sambuc 	 * the rows and columns.
14384d9c625SLionel Sambuc 	 */
14484d9c625SLionel Sambuc 	for (;;) {
14584d9c625SLionel Sambuc 		if (ip_wevent(wp, NULL, &ev, 0, 0))
14684d9c625SLionel Sambuc 			return NULL;
14784d9c625SLionel Sambuc 		if (ev.e_event == E_WRESIZE)
14884d9c625SLionel Sambuc 			break;
14984d9c625SLionel Sambuc 		if (ev.e_event == E_EOF || ev.e_event == E_ERR ||
15084d9c625SLionel Sambuc 		    ev.e_event == E_SIGHUP || ev.e_event == E_SIGTERM)
15184d9c625SLionel Sambuc 			return NULL;
15284d9c625SLionel Sambuc 		if (ev.e_event == E_IPCOMMAND && ev.e_ipcom == VI_QUIT)
15384d9c625SLionel Sambuc 			return NULL;
15484d9c625SLionel Sambuc 	}
15584d9c625SLionel Sambuc 
15684d9c625SLionel Sambuc 	/* Run ex/vi. */
15784d9c625SLionel Sambuc 	rval = editor(wp, ipp->argc, ipp->argv);
15884d9c625SLionel Sambuc 
15984d9c625SLionel Sambuc 	/* Clean up the screen. */
16084d9c625SLionel Sambuc 	(void)ip_quit(wp);
16184d9c625SLionel Sambuc 
16284d9c625SLionel Sambuc 	/* Send the quit message. */
16384d9c625SLionel Sambuc 	ipb.code = SI_QUIT;
16484d9c625SLionel Sambuc 	(void)vi_send(ipp->o_fd, NULL, &ipb);
16584d9c625SLionel Sambuc 
16684d9c625SLionel Sambuc 	/* Give the screen a couple of seconds to deal with it. */
16784d9c625SLionel Sambuc 	sleep(2);
16884d9c625SLionel Sambuc 
16984d9c625SLionel Sambuc 	/* Remove window; correct place ? */
17084d9c625SLionel Sambuc 	win_end(wp);
17184d9c625SLionel Sambuc 
17284d9c625SLionel Sambuc #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
17384d9c625SLionel Sambuc 	free(ipp);
17484d9c625SLionel Sambuc #endif
17584d9c625SLionel Sambuc 	return NULL;
17684d9c625SLionel Sambuc }
17784d9c625SLionel Sambuc 
17884d9c625SLionel Sambuc /*
17984d9c625SLionel Sambuc  * ip_init --
18084d9c625SLionel Sambuc  *	Create and partially initialize the GS structure.
18184d9c625SLionel Sambuc  */
18284d9c625SLionel Sambuc static IP_PRIVATE *
ip_init(WIN * wp,int i_fd,int o_fd,int t_fd,int argc,char * argv[])18384d9c625SLionel Sambuc ip_init(WIN *wp, int i_fd, int o_fd, int t_fd, int argc, char *argv[])
18484d9c625SLionel Sambuc {
18584d9c625SLionel Sambuc 	IP_PRIVATE *ipp;
18684d9c625SLionel Sambuc 
18784d9c625SLionel Sambuc 	/* Allocate the IP private structure. */
18884d9c625SLionel Sambuc 	CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE));
18984d9c625SLionel Sambuc 	if (ipp == NULL)
19084d9c625SLionel Sambuc 		perr(wp->gp->progname,  NULL);
19184d9c625SLionel Sambuc 	wp->ip_private = ipp;
19284d9c625SLionel Sambuc 
19384d9c625SLionel Sambuc 	ipp->i_fd = i_fd;
19484d9c625SLionel Sambuc 	ipp->o_fd = o_fd;
19584d9c625SLionel Sambuc 	ipp->t_fd = t_fd;
19684d9c625SLionel Sambuc 
19784d9c625SLionel Sambuc  	ipp->argc = argc;
19884d9c625SLionel Sambuc  	ipp->argv = argv;
19984d9c625SLionel Sambuc 
20084d9c625SLionel Sambuc 	/* Initialize the list of ip functions. */
20184d9c625SLionel Sambuc 	ip_func_std(wp);
20284d9c625SLionel Sambuc 
20384d9c625SLionel Sambuc 	return (ipp);
20484d9c625SLionel Sambuc }
20584d9c625SLionel Sambuc 
20684d9c625SLionel Sambuc static int
get_fds(char * ip_arg,int * i_fd,int * o_fd)20784d9c625SLionel Sambuc get_fds(char *ip_arg, int *i_fd, int *o_fd)
20884d9c625SLionel Sambuc {
20984d9c625SLionel Sambuc 	char *ep;
21084d9c625SLionel Sambuc 
21184d9c625SLionel Sambuc 	/*
21284d9c625SLionel Sambuc 	 * Crack ip_arg -- it's of the form #.#, where the first number is the
21384d9c625SLionel Sambuc 	 * file descriptor from the screen, the second is the file descriptor
21484d9c625SLionel Sambuc 	 * to the screen.
21584d9c625SLionel Sambuc 	 */
21684d9c625SLionel Sambuc 	if (!ip_arg || !isdigit((unsigned char)ip_arg[0]))
21784d9c625SLionel Sambuc 		goto usage;
21884d9c625SLionel Sambuc 	*i_fd = strtol(ip_arg, &ep, 10);
21984d9c625SLionel Sambuc 	if (ep[0] != '.' || !isdigit((unsigned char)ep[1]))
22084d9c625SLionel Sambuc 		goto usage;
22184d9c625SLionel Sambuc 	*o_fd = strtol(++ep, &ep, 10);
22284d9c625SLionel Sambuc 	if (ep[0] != '\0') {
22384d9c625SLionel Sambuc usage:		ip_usage();
22484d9c625SLionel Sambuc 		return 1;
22584d9c625SLionel Sambuc 	}
22684d9c625SLionel Sambuc 
22784d9c625SLionel Sambuc 	return 0;
22884d9c625SLionel Sambuc }
22984d9c625SLionel Sambuc 
23084d9c625SLionel Sambuc static int
get_connection(WIN * wp,int main_ifd,int main_ofd,int * i_fd,int * o_fd,int * t_fd,int can_pass)23184d9c625SLionel Sambuc get_connection(WIN *wp, int main_ifd, int main_ofd,
23284d9c625SLionel Sambuc 	int *i_fd, int *o_fd, int *t_fd, int can_pass)
23384d9c625SLionel Sambuc {
23484d9c625SLionel Sambuc     *t_fd = -1;
23584d9c625SLionel Sambuc 
23684d9c625SLionel Sambuc     if (!can_pass) {
23784d9c625SLionel Sambuc 	if (wp == NULL) {		    /* First call */
23884d9c625SLionel Sambuc 	    *i_fd = main_ifd;
23984d9c625SLionel Sambuc 	    *o_fd = main_ofd;
24084d9c625SLionel Sambuc 	} else {
24184d9c625SLionel Sambuc 	    return 1;
24284d9c625SLionel Sambuc 	}
24384d9c625SLionel Sambuc     } else {
24484d9c625SLionel Sambuc 	struct msghdr   mh;
24584d9c625SLionel Sambuc 	IPCMSGHDR	    ch;
24684d9c625SLionel Sambuc 	char	    dummy;
24784d9c625SLionel Sambuc 	struct iovec    iov;
24884d9c625SLionel Sambuc 
24984d9c625SLionel Sambuc 	mh.msg_namelen = 0;
25084d9c625SLionel Sambuc 	mh.msg_iovlen = 1;
25184d9c625SLionel Sambuc 	mh.msg_iov = &iov;
25284d9c625SLionel Sambuc 	mh.msg_controllen = sizeof(ch);
25384d9c625SLionel Sambuc 	mh.msg_control = (void *)&ch;
25484d9c625SLionel Sambuc 
25584d9c625SLionel Sambuc 	iov.iov_len = 1;
25684d9c625SLionel Sambuc 	iov.iov_base = &dummy;
25784d9c625SLionel Sambuc 
25884d9c625SLionel Sambuc 	if (recvmsg(main_ifd, &mh, 0) != 1)
25984d9c625SLionel Sambuc 	    return 1;
26084d9c625SLionel Sambuc 	*i_fd = *(int *)CMSG_DATA(&ch.header);
26184d9c625SLionel Sambuc 	if (recvmsg(*i_fd, &mh, 0) != 1)
26284d9c625SLionel Sambuc 	    return 1;
26384d9c625SLionel Sambuc 	*o_fd = *(int *)CMSG_DATA(&ch.header);
26484d9c625SLionel Sambuc 	if (dummy == 'F') {
26584d9c625SLionel Sambuc 	    if (recvmsg(*i_fd, &mh, 0) != 1)
26684d9c625SLionel Sambuc 		return 1;
26784d9c625SLionel Sambuc 	    *t_fd = *(int *)CMSG_DATA(&ch.header);
26884d9c625SLionel Sambuc 	}
26984d9c625SLionel Sambuc     }
27084d9c625SLionel Sambuc 
27184d9c625SLionel Sambuc     return 0;
27284d9c625SLionel Sambuc }
27384d9c625SLionel Sambuc 
27484d9c625SLionel Sambuc /*
27584d9c625SLionel Sambuc  * ip_func_std --
27684d9c625SLionel Sambuc  *	Initialize the standard ip functions.
27784d9c625SLionel Sambuc  */
27884d9c625SLionel Sambuc static void
ip_func_std(WIN * wp)27984d9c625SLionel Sambuc ip_func_std(WIN *wp)
28084d9c625SLionel Sambuc {
28184d9c625SLionel Sambuc 	GS *gp;
28284d9c625SLionel Sambuc 
28384d9c625SLionel Sambuc 	gp = wp->gp;
28484d9c625SLionel Sambuc 
28584d9c625SLionel Sambuc 	gp->scr_addstr = ip_addstr;
28684d9c625SLionel Sambuc 	gp->scr_waddstr = ip_waddstr;
28784d9c625SLionel Sambuc 	gp->scr_attr = ip_attr;
28884d9c625SLionel Sambuc 	gp->scr_baud = ip_baud;
28984d9c625SLionel Sambuc 	gp->scr_bell = ip_bell;
29084d9c625SLionel Sambuc 	gp->scr_busy = ip_busy;
29184d9c625SLionel Sambuc 	gp->scr_child = ip_child;
29284d9c625SLionel Sambuc 	gp->scr_clrtoeol = ip_clrtoeol;
29384d9c625SLionel Sambuc 	gp->scr_cursor = ip_cursor;
29484d9c625SLionel Sambuc 	gp->scr_deleteln = ip_deleteln;
29584d9c625SLionel Sambuc 	gp->scr_discard = ip_discard;
29684d9c625SLionel Sambuc 	gp->scr_event = ip_event;
29784d9c625SLionel Sambuc 	gp->scr_ex_adjust = ip_ex_adjust;
29884d9c625SLionel Sambuc 	gp->scr_fmap = ip_fmap;
29984d9c625SLionel Sambuc 	gp->scr_insertln = ip_insertln;
30084d9c625SLionel Sambuc 	gp->scr_keyval = ip_keyval;
30184d9c625SLionel Sambuc 	gp->scr_move = ip_move;
30284d9c625SLionel Sambuc 	wp->scr_msg = ip_msg;
30384d9c625SLionel Sambuc 	gp->scr_optchange = ip_optchange;
30484d9c625SLionel Sambuc 	gp->scr_refresh = ip_refresh;
30584d9c625SLionel Sambuc 	gp->scr_rename = ip_rename;
30684d9c625SLionel Sambuc 	gp->scr_reply = ip_reply;
30784d9c625SLionel Sambuc 	gp->scr_screen = ip_screen;
30884d9c625SLionel Sambuc 	gp->scr_split = ip_split;
30984d9c625SLionel Sambuc 	gp->scr_suspend = ip_suspend;
31084d9c625SLionel Sambuc 	gp->scr_usage = ip_usage;
31184d9c625SLionel Sambuc }
31284d9c625SLionel Sambuc 
31384d9c625SLionel Sambuc /*
31484d9c625SLionel Sambuc  * perr --
31584d9c625SLionel Sambuc  *	Print system error.
31684d9c625SLionel Sambuc  */
31784d9c625SLionel Sambuc static void
perr(char * name,char * msg)31884d9c625SLionel Sambuc perr(char *name, char *msg)
31984d9c625SLionel Sambuc {
32084d9c625SLionel Sambuc 	(void)fprintf(stderr, "%s:", name);
32184d9c625SLionel Sambuc 	if (msg != NULL)
32284d9c625SLionel Sambuc 		(void)fprintf(stderr, "%s:", msg);
32384d9c625SLionel Sambuc 	(void)fprintf(stderr, "%s\n", strerror(errno));
32484d9c625SLionel Sambuc 	exit(1);
32584d9c625SLionel Sambuc }
326