5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
13 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";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
29 #include "../common/common.h"
30 #include "../ipc/ip.h"
33 GS
*__global_list
; /* GLOBAL: List of screens. */
35 static void ip_func_std
__P((WIN
*));
36 static IP_PRIVATE
*ip_init
__P((WIN
*wp
, int i_fd
, int o_fd
, int, int argc
, char *argv
[]));
37 static void perr
__P((char *, char *));
38 static int get_fds
__P((char *ip_arg
, int *i_fd
, int *o_fd
));
39 static int get_connection
__P((WIN
*wp
, int main_ifd
, int main_ofd
,
40 int *i_fd
, int *o_fd
, int *, int can_pass
));
41 static void *run_editor
__P((void * vp
));
45 * This is the main loop for the vi-as-library editor.
48 main(int argc
, char **argv
)
56 int i_fd
, o_fd
, t_fd
, main_ifd
, main_ofd
;
58 /* Create and initialize the global structure. */
59 __global_list
= gp
= gs_init(argv
[0]);
62 * Strip out any arguments that vi isn't going to understand. There's
63 * no way to portably call getopt twice, so arguments parsed here must
64 * be removed from the argument list.
67 for (p_av
= t_av
= argv
;;) {
72 if (!strcmp(*t_av
, "--")) {
73 while ((*p_av
++ = *t_av
++) != NULL
);
76 if (!memcmp(*t_av
, "-I", sizeof("-I") - 1)) {
77 if (t_av
[0][2] != '\0') {
83 else if (t_av
[1] != NULL
) {
93 if (get_fds(ip_arg
, &main_ifd
, &main_ofd
))
98 while (get_connection(wp
, main_ifd
, main_ofd
, &i_fd
, &o_fd
, &t_fd
, 1) == 0) {
99 /* Create new window */
102 /* Create and partially initialize the IP structure. */
103 if ((ipp
= ip_init(wp
, i_fd
, o_fd
, t_fd
, argc
, argv
)) == NULL
)
106 gp
->run(wp
, run_editor
, (void *)wp
);
109 /* Clean out the global structure. */
112 /* Free the global and IP private areas. */
113 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
120 run_editor(void * vp
)
131 ipp
= wp
->ip_private
;
133 /* Add the terminal type to the global structure. */
134 if ((OG_D_STR(gp
, GO_TERM
) =
135 OG_STR(gp
, GO_TERM
) = strdup("ip_curses")) == NULL
)
136 perr(gp
->progname
, NULL
);
139 * Figure out how big the screen is -- read events until we get
140 * the rows and columns.
143 if (ip_wevent(wp
, NULL
, &ev
, 0, 0))
145 if (ev
.e_event
== E_WRESIZE
)
147 if (ev
.e_event
== E_EOF
|| ev
.e_event
== E_ERR
||
148 ev
.e_event
== E_SIGHUP
|| ev
.e_event
== E_SIGTERM
)
150 if (ev
.e_event
== E_IPCOMMAND
&& ev
.e_ipcom
== VI_QUIT
)
155 rval
= editor(wp
, ipp
->argc
, ipp
->argv
);
157 /* Clean up the screen. */
160 /* Send the quit message. */
162 (void)vi_send(ipp
->o_fd
, NULL
, &ipb
);
164 /* Give the screen a couple of seconds to deal with it. */
167 /* Remove window; correct place ? */
170 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
178 * Create and partially initialize the GS structure.
181 ip_init(WIN
*wp
, int i_fd
, int o_fd
, int t_fd
, int argc
, char *argv
[])
185 /* Allocate the IP private structure. */
186 CALLOC_NOMSG(NULL
, ipp
, IP_PRIVATE
*, 1, sizeof(IP_PRIVATE
));
188 perr(wp
->gp
->progname
, NULL
);
189 wp
->ip_private
= ipp
;
198 /* Initialize the list of ip functions. */
205 get_fds(char *ip_arg
, int *i_fd
, int *o_fd
)
210 * Crack ip_arg -- it's of the form #.#, where the first number is the
211 * file descriptor from the screen, the second is the file descriptor
214 if (!ip_arg
|| !isdigit(ip_arg
[0]))
216 *i_fd
= strtol(ip_arg
, &ep
, 10);
217 if (ep
[0] != '.' || !isdigit(ep
[1]))
219 *o_fd
= strtol(++ep
, &ep
, 10);
229 get_connection(WIN
*wp
, int main_ifd
, int main_ofd
,
230 int *i_fd
, int *o_fd
, int *t_fd
, int can_pass
)
235 if (wp
== NULL
) { /* First call */
250 mh
.msg_controllen
= sizeof(ch
);
251 mh
.msg_control
= (void *)&ch
;
254 iov
.iov_base
= &dummy
;
256 if (recvmsg(main_ifd
, &mh
, 0) != 1)
258 *i_fd
= *(int *)CMSG_DATA(&ch
.header
);
259 if (recvmsg(*i_fd
, &mh
, 0) != 1)
261 *o_fd
= *(int *)CMSG_DATA(&ch
.header
);
263 if (recvmsg(*i_fd
, &mh
, 0) != 1)
265 *t_fd
= *(int *)CMSG_DATA(&ch
.header
);
274 * Initialize the standard ip functions.
283 gp
->scr_addstr
= ip_addstr
;
284 gp
->scr_waddstr
= ip_waddstr
;
285 gp
->scr_attr
= ip_attr
;
286 gp
->scr_baud
= ip_baud
;
287 gp
->scr_bell
= ip_bell
;
288 gp
->scr_busy
= ip_busy
;
289 gp
->scr_child
= ip_child
;
290 gp
->scr_clrtoeol
= ip_clrtoeol
;
291 gp
->scr_cursor
= ip_cursor
;
292 gp
->scr_deleteln
= ip_deleteln
;
293 gp
->scr_discard
= ip_discard
;
294 gp
->scr_event
= ip_event
;
295 gp
->scr_ex_adjust
= ip_ex_adjust
;
296 gp
->scr_fmap
= ip_fmap
;
297 gp
->scr_insertln
= ip_insertln
;
298 gp
->scr_keyval
= ip_keyval
;
299 gp
->scr_move
= ip_move
;
300 wp
->scr_msg
= ip_msg
;
301 gp
->scr_optchange
= ip_optchange
;
302 gp
->scr_refresh
= ip_refresh
;
303 gp
->scr_rename
= ip_rename
;
304 gp
->scr_reply
= ip_reply
;
305 gp
->scr_screen
= ip_screen
;
306 gp
->scr_split
= ip_split
;
307 gp
->scr_suspend
= ip_suspend
;
308 gp
->scr_usage
= ip_usage
;
313 * Print system error.
316 perr(char *name
, char *msg
)
318 (void)fprintf(stderr
, "%s:", name
);
320 (void)fprintf(stderr
, "%s:", msg
);
321 (void)fprintf(stderr
, "%s\n", strerror(errno
));