3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
11 static const char sccsid
[] = "$Id: ip_main.c,v 8.14 2000/06/30 20:11:28 skimo Exp $ (Berkeley) $Date: 2000/06/30 20:11:28 $";
14 #include <sys/types.h>
15 #include <sys/queue.h>
17 #include <bitstring.h>
28 #include "../common/common.h"
29 #include "../ipc/ip.h"
32 GS
*__global_list
; /* GLOBAL: List of screens. */
34 static void ip_func_std
__P((GS
*));
35 static IP_PRIVATE
*ip_init
__P((WIN
*wp
, int i_fd
, int o_fd
, int argc
, char *argv
[]));
36 static void perr
__P((char *, char *));
37 static int get_fds
__P((char *ip_arg
, int *i_fd
, int *o_fd
));
38 static void run_editor
__P((void * vp
));
42 * This is the main loop for the vi-as-library editor.
44 * PUBLIC: int ip_main __P((int, char *[], GS *, char *));
57 int i_fd
, o_fd
, main_ifd
, main_ofd
;
59 /* Create and initialize the global structure. */
60 __global_list
= gp
= gs_init(argv
[0]);
63 * Strip out any arguments that vi isn't going to understand. There's
64 * no way to portably call getopt twice, so arguments parsed here must
65 * be removed from the argument list.
68 for (p_av
= t_av
= argv
;;) {
73 if (!strcmp(*t_av
, "--")) {
74 while ((*p_av
++ = *t_av
++) != NULL
);
77 if (!memcmp(*t_av
, "-I", sizeof("-I") - 1)) {
78 if (t_av
[0][2] != '\0') {
84 else if (t_av
[1] != NULL
) {
94 if (get_fds(ip_arg
, &i_fd
, &o_fd
))
97 /* Create new window */
100 /* Create and partially initialize the IP structure. */
101 if ((ipp
= ip_init(wp
, i_fd
, o_fd
, argc
, argv
)) == NULL
)
104 run_editor((void *)wp
);
106 /* Free the global and IP private areas. */
107 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
114 run_editor(void * vp
)
125 ipp
= wp
->ip_private
;
127 /* Add the terminal type to the global structure. */
128 if ((OG_D_STR(gp
, GO_TERM
) =
129 OG_STR(gp
, GO_TERM
) = strdup("ip_curses")) == NULL
)
130 perr(gp
->progname
, NULL
);
133 * Figure out how big the screen is -- read events until we get
134 * the rows and columns.
137 if (ip_wevent(wp
, NULL
, &ev
, 0, 0))
139 if (ev
.e_event
== E_WRESIZE
)
141 if (ev
.e_event
== E_EOF
|| ev
.e_event
== E_ERR
||
142 ev
.e_event
== E_SIGHUP
|| ev
.e_event
== E_SIGTERM
)
144 if (ev
.e_event
== E_IPCOMMAND
&& ev
.e_ipcom
== VI_QUIT
)
149 rval
= editor(wp
, ipp
->argc
, ipp
->argv
);
151 /* Clean up the screen. */
154 /* Send the quit message. */
156 (void)vi_send(ipp
->o_fd
, NULL
, &ipb
);
158 /* Give the screen a couple of seconds to deal with it. */
161 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
168 * Create and partially initialize the GS structure.
171 ip_init(WIN
*wp
, int i_fd
, int o_fd
, int argc
, char *argv
[])
175 /* Allocate the IP private structure. */
176 CALLOC_NOMSG(NULL
, ipp
, IP_PRIVATE
*, 1, sizeof(IP_PRIVATE
));
178 perr(wp
->gp
->progname
, NULL
);
179 wp
->ip_private
= ipp
;
187 /* Initialize the list of ip functions. */
194 get_fds(char *ip_arg
, int *i_fd
, int *o_fd
)
199 * Crack ip_arg -- it's of the form #.#, where the first number is the
200 * file descriptor from the screen, the second is the file descriptor
203 if (!ip_arg
|| !isdigit(ip_arg
[0]))
205 *i_fd
= strtol(ip_arg
, &ep
, 10);
206 if (ep
[0] != '.' || !isdigit(ep
[1]))
208 *o_fd
= strtol(++ep
, &ep
, 10);
219 * Initialize the standard ip functions.
225 gp
->scr_addstr
= ip_addstr
;
226 gp
->scr_attr
= ip_attr
;
227 gp
->scr_baud
= ip_baud
;
228 gp
->scr_bell
= ip_bell
;
229 gp
->scr_busy
= ip_busy
;
230 gp
->scr_clrtoeol
= ip_clrtoeol
;
231 gp
->scr_cursor
= ip_cursor
;
232 gp
->scr_deleteln
= ip_deleteln
;
233 gp
->scr_discard
= ip_discard
;
234 gp
->scr_event
= ip_event
;
235 gp
->scr_ex_adjust
= ip_ex_adjust
;
236 gp
->scr_fmap
= ip_fmap
;
237 gp
->scr_insertln
= ip_insertln
;
238 gp
->scr_keyval
= ip_keyval
;
239 gp
->scr_move
= ip_move
;
241 gp
->scr_optchange
= ip_optchange
;
242 gp
->scr_refresh
= ip_refresh
;
243 gp
->scr_rename
= ip_rename
;
244 gp
->scr_reply
= ip_reply
;
245 gp
->scr_screen
= ip_screen
;
246 gp
->scr_split
= ip_split
;
247 gp
->scr_suspend
= ip_suspend
;
248 gp
->scr_usage
= ip_usage
;
253 * Print system error.
259 (void)fprintf(stderr
, "%s:", name
);
261 (void)fprintf(stderr
, "%s:", msg
);
262 (void)fprintf(stderr
, "%s\n", strerror(errno
));