xref: /minix/external/bsd/nvi/dist/ip/ip_main.c (revision 0a6a1f1d)
1 /*	$NetBSD: ip_main.c,v 1.3 2014/01/26 21:43:45 christos Exp $ */
2 /*-
3  * Copyright (c) 1996
4  *	Keith Bostic.  All rights reserved.
5  *
6  * See the LICENSE file for redistribution information.
7  */
8 
9 #include "config.h"
10 
11 #include <sys/cdefs.h>
12 #if 0
13 #ifndef lint
14 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 ";
15 #endif /* not lint */
16 #else
17 __RCSID("$NetBSD: ip_main.c,v 1.3 2014/01/26 21:43:45 christos Exp $");
18 #endif
19 
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 
23 #include <bitstring.h>
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 #include <sys/uio.h>
32 
33 #include "../common/common.h"
34 #include "../ipc/ip.h"
35 
36 GS *__global_list;				/* GLOBAL: List of screens. */
37 
38 static void	   ip_func_std __P((WIN *));
39 static IP_PRIVATE *ip_init __P((WIN *wp, int i_fd, int o_fd, int, int argc, char *argv[]));
40 static void	   perr __P((char *, char *));
41 static int	   get_fds __P((char *ip_arg, int *i_fd, int *o_fd));
42 static int  get_connection __P((WIN *wp, int main_ifd, int main_ofd,
43 				int *i_fd, int *o_fd, int *, int can_pass));
44 static void *run_editor __P((void * vp));
45 
46 /*
47  * ip_main --
48  *      This is the main loop for the vi-as-library editor.
49  */
50 int
main(int argc,char ** argv)51 main(int argc, char **argv)
52 {
53 	IP_PRIVATE *ipp;
54 	char *ip_arg;
55 	char **p_av, **t_av;
56 	GS *gp;
57 	WIN *wp;
58 	int i_fd, o_fd, t_fd, main_ifd, main_ofd;
59 
60 	/* Create and initialize the global structure. */
61 	__global_list = gp = gs_init(argv[0]);
62 
63 	/*
64 	 * Strip out any arguments that vi isn't going to understand.  There's
65 	 * no way to portably call getopt twice, so arguments parsed here must
66 	 * be removed from the argument list.
67 	 */
68 	ip_arg = NULL;
69 	for (p_av = t_av = argv;;) {
70 		if (*t_av == NULL) {
71 			*p_av = NULL;
72 			break;
73 		}
74 		if (!strcmp(*t_av, "--")) {
75 			while ((*p_av++ = *t_av++) != NULL);
76 			break;
77 		}
78 		if (!memcmp(*t_av, "-I", sizeof("-I") - 1)) {
79 			if (t_av[0][2] != '\0') {
80 				ip_arg = t_av[0] + 2;
81 				++t_av;
82 				--argc;
83 				continue;
84 			}
85 			else if (t_av[1] != NULL) {
86 				ip_arg = t_av[1];
87 				t_av += 2;
88 				argc -= 2;
89 				continue;
90 			}
91 		}
92 		*p_av++ = *t_av++;
93 	}
94 
95 	if (get_fds(ip_arg, &main_ifd, &main_ofd))
96 		return 1;
97 
98 	wp = NULL;
99 
100 	while (get_connection(wp, main_ifd, main_ofd, &i_fd, &o_fd, &t_fd, 1) == 0) {
101 		/* Create new window */
102 		wp = gs_new_win(gp);
103 
104 		/* Create and partially initialize the IP structure. */
105 		if ((ipp = ip_init(wp, i_fd, o_fd, t_fd, argc, argv)) == NULL)
106 			return (1);
107 
108 		gp->run(wp, run_editor, (void *)wp);
109 	}
110 
111 	/* Clean out the global structure. */
112 	gs_end(gp);
113 
114 	/* Free the global and IP private areas. */
115 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
116 	free(gp);
117 #endif
118 	exit (0);
119 }
120 
121 static void *
run_editor(void * vp)122 run_editor(void * vp)
123 {
124 	GS *gp;
125 	IP_PRIVATE *ipp;
126 	WIN *wp;
127 	EVENT ev;
128 	int rval;
129 	IP_BUF ipb;
130 
131 	wp = (WIN *) vp;
132 	gp = wp->gp;
133 	ipp = wp->ip_private;
134 
135 	/* Add the terminal type to the global structure. */
136 	if ((OG_D_STR(gp, GO_TERM) =
137 	    OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL)
138 		perr(gp->progname, NULL);
139 
140 	/*
141 	 * Figure out how big the screen is -- read events until we get
142 	 * the rows and columns.
143 	 */
144 	for (;;) {
145 		if (ip_wevent(wp, NULL, &ev, 0, 0))
146 			return NULL;
147 		if (ev.e_event == E_WRESIZE)
148 			break;
149 		if (ev.e_event == E_EOF || ev.e_event == E_ERR ||
150 		    ev.e_event == E_SIGHUP || ev.e_event == E_SIGTERM)
151 			return NULL;
152 		if (ev.e_event == E_IPCOMMAND && ev.e_ipcom == VI_QUIT)
153 			return NULL;
154 	}
155 
156 	/* Run ex/vi. */
157 	rval = editor(wp, ipp->argc, ipp->argv);
158 
159 	/* Clean up the screen. */
160 	(void)ip_quit(wp);
161 
162 	/* Send the quit message. */
163 	ipb.code = SI_QUIT;
164 	(void)vi_send(ipp->o_fd, NULL, &ipb);
165 
166 	/* Give the screen a couple of seconds to deal with it. */
167 	sleep(2);
168 
169 	/* Remove window; correct place ? */
170 	win_end(wp);
171 
172 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
173 	free(ipp);
174 #endif
175 	return NULL;
176 }
177 
178 /*
179  * ip_init --
180  *	Create and partially initialize the GS structure.
181  */
182 static IP_PRIVATE *
ip_init(WIN * wp,int i_fd,int o_fd,int t_fd,int argc,char * argv[])183 ip_init(WIN *wp, int i_fd, int o_fd, int t_fd, int argc, char *argv[])
184 {
185 	IP_PRIVATE *ipp;
186 
187 	/* Allocate the IP private structure. */
188 	CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE));
189 	if (ipp == NULL)
190 		perr(wp->gp->progname,  NULL);
191 	wp->ip_private = ipp;
192 
193 	ipp->i_fd = i_fd;
194 	ipp->o_fd = o_fd;
195 	ipp->t_fd = t_fd;
196 
197  	ipp->argc = argc;
198  	ipp->argv = argv;
199 
200 	/* Initialize the list of ip functions. */
201 	ip_func_std(wp);
202 
203 	return (ipp);
204 }
205 
206 static int
get_fds(char * ip_arg,int * i_fd,int * o_fd)207 get_fds(char *ip_arg, int *i_fd, int *o_fd)
208 {
209 	char *ep;
210 
211 	/*
212 	 * Crack ip_arg -- it's of the form #.#, where the first number is the
213 	 * file descriptor from the screen, the second is the file descriptor
214 	 * to the screen.
215 	 */
216 	if (!ip_arg || !isdigit((unsigned char)ip_arg[0]))
217 		goto usage;
218 	*i_fd = strtol(ip_arg, &ep, 10);
219 	if (ep[0] != '.' || !isdigit((unsigned char)ep[1]))
220 		goto usage;
221 	*o_fd = strtol(++ep, &ep, 10);
222 	if (ep[0] != '\0') {
223 usage:		ip_usage();
224 		return 1;
225 	}
226 
227 	return 0;
228 }
229 
230 static int
get_connection(WIN * wp,int main_ifd,int main_ofd,int * i_fd,int * o_fd,int * t_fd,int can_pass)231 get_connection(WIN *wp, int main_ifd, int main_ofd,
232 	int *i_fd, int *o_fd, int *t_fd, int can_pass)
233 {
234     *t_fd = -1;
235 
236     if (!can_pass) {
237 	if (wp == NULL) {		    /* First call */
238 	    *i_fd = main_ifd;
239 	    *o_fd = main_ofd;
240 	} else {
241 	    return 1;
242 	}
243     } else {
244 	struct msghdr   mh;
245 	IPCMSGHDR	    ch;
246 	char	    dummy;
247 	struct iovec    iov;
248 
249 	mh.msg_namelen = 0;
250 	mh.msg_iovlen = 1;
251 	mh.msg_iov = &iov;
252 	mh.msg_controllen = sizeof(ch);
253 	mh.msg_control = (void *)&ch;
254 
255 	iov.iov_len = 1;
256 	iov.iov_base = &dummy;
257 
258 	if (recvmsg(main_ifd, &mh, 0) != 1)
259 	    return 1;
260 	*i_fd = *(int *)CMSG_DATA(&ch.header);
261 	if (recvmsg(*i_fd, &mh, 0) != 1)
262 	    return 1;
263 	*o_fd = *(int *)CMSG_DATA(&ch.header);
264 	if (dummy == 'F') {
265 	    if (recvmsg(*i_fd, &mh, 0) != 1)
266 		return 1;
267 	    *t_fd = *(int *)CMSG_DATA(&ch.header);
268 	}
269     }
270 
271     return 0;
272 }
273 
274 /*
275  * ip_func_std --
276  *	Initialize the standard ip functions.
277  */
278 static void
ip_func_std(WIN * wp)279 ip_func_std(WIN *wp)
280 {
281 	GS *gp;
282 
283 	gp = wp->gp;
284 
285 	gp->scr_addstr = ip_addstr;
286 	gp->scr_waddstr = ip_waddstr;
287 	gp->scr_attr = ip_attr;
288 	gp->scr_baud = ip_baud;
289 	gp->scr_bell = ip_bell;
290 	gp->scr_busy = ip_busy;
291 	gp->scr_child = ip_child;
292 	gp->scr_clrtoeol = ip_clrtoeol;
293 	gp->scr_cursor = ip_cursor;
294 	gp->scr_deleteln = ip_deleteln;
295 	gp->scr_discard = ip_discard;
296 	gp->scr_event = ip_event;
297 	gp->scr_ex_adjust = ip_ex_adjust;
298 	gp->scr_fmap = ip_fmap;
299 	gp->scr_insertln = ip_insertln;
300 	gp->scr_keyval = ip_keyval;
301 	gp->scr_move = ip_move;
302 	wp->scr_msg = ip_msg;
303 	gp->scr_optchange = ip_optchange;
304 	gp->scr_refresh = ip_refresh;
305 	gp->scr_rename = ip_rename;
306 	gp->scr_reply = ip_reply;
307 	gp->scr_screen = ip_screen;
308 	gp->scr_split = ip_split;
309 	gp->scr_suspend = ip_suspend;
310 	gp->scr_usage = ip_usage;
311 }
312 
313 /*
314  * perr --
315  *	Print system error.
316  */
317 static void
perr(char * name,char * msg)318 perr(char *name, char *msg)
319 {
320 	(void)fprintf(stderr, "%s:", name);
321 	if (msg != NULL)
322 		(void)fprintf(stderr, "%s:", msg);
323 	(void)fprintf(stderr, "%s\n", strerror(errno));
324 	exit(1);
325 }
326