DB 3.1 update
[nvi.git] / ip / ip_main.c
blob914b6de8d39343e5178fc0621515b442828f527a
1 /*-
2 * Copyright (c) 1996
3 * Keith Bostic. All rights reserved.
5 * See the LICENSE file for redistribution information.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "$Id: ip_main.c,v 8.10 1997/08/02 16:48:59 bostic Exp $ (Berkeley) $Date: 1997/08/02 16:48:59 $";
12 #endif /* not lint */
14 #include <sys/types.h>
15 #include <sys/queue.h>
17 #include <bitstring.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
25 #include "../common/common.h"
26 #include "../ipc/ip.h"
27 #include "extern.h"
29 int vi_ofd; /* GLOBAL: known to vi_send(). */
31 static void ip_func_std __P((GS *));
32 static IP_PRIVATE *ip_init __P((GS *, char *));
33 static void perr __P((char *, char *));
36 * ip_main --
37 * This is the main loop for the vi-as-library editor.
39 * PUBLIC: int ip_main __P((int, char *[], GS *, char *));
41 int
42 ip_main(argc, argv, gp, ip_arg)
43 int argc;
44 char *argv[], *ip_arg;
45 GS *gp;
47 EVENT ev;
48 IP_PRIVATE *ipp;
49 IP_BUF ipb;
50 int rval;
52 /* Create and partially initialize the IP structure. */
53 if ((ipp = ip_init(gp, ip_arg)) == NULL)
54 return (1);
56 /* Add the terminal type to the global structure. */
57 if ((OG_D_STR(gp, GO_TERM) =
58 OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL)
59 perr(gp->progname, NULL);
62 * Figure out how big the screen is -- read events until we get
63 * the rows and columns.
65 for (;;) {
66 if (ip_event(NULL, &ev, 0, 0))
67 return (1);
68 if (ev.e_event == E_WRESIZE)
69 break;
70 if (ev.e_event == E_EOF || ev.e_event == E_ERR ||
71 ev.e_event == E_SIGHUP || ev.e_event == E_SIGTERM)
72 return (1);
73 if (ev.e_event == E_IPCOMMAND && ev.e_ipcom == VI_QUIT)
74 return (1);
77 /* Run ex/vi. */
78 rval = editor(gp, argc, argv);
80 /* Clean up the screen. */
81 (void)ip_quit(gp);
83 /* Send the quit message. */
84 ipb.code = SI_QUIT;
85 (void)vi_send(NULL, &ipb);
87 /* Give the screen a couple of seconds to deal with it. */
88 sleep(2);
90 /* Free the global and IP private areas. */
91 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
92 free(ipp);
93 free(gp);
94 #endif
95 return (rval);
99 * ip_init --
100 * Create and partially initialize the GS structure.
102 static IP_PRIVATE *
103 ip_init(gp, ip_arg)
104 GS *gp;
105 char *ip_arg;
107 IP_PRIVATE *ipp;
108 char *ep;
110 /* Allocate the IP private structure. */
111 CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE));
112 if (ipp == NULL)
113 perr(gp->progname, NULL);
114 gp->ip_private = ipp;
117 * Crack ip_arg -- it's of the form #.#, where the first number is the
118 * file descriptor from the screen, the second is the file descriptor
119 * to the screen.
121 if (!isdigit(ip_arg[0]))
122 goto usage;
123 ipp->i_fd = strtol(ip_arg, &ep, 10);
124 if (ep[0] != '.' || !isdigit(ep[1]))
125 goto usage;
126 vi_ofd = strtol(++ep, &ep, 10);
127 if (ep[0] != '\0') {
128 usage: ip_usage();
129 return (NULL);
132 /* Initialize the list of ip functions. */
133 ip_func_std(gp);
135 return (ipp);
139 * ip_func_std --
140 * Initialize the standard ip functions.
142 static void
143 ip_func_std(gp)
144 GS *gp;
146 gp->scr_addstr = ip_addstr;
147 gp->scr_attr = ip_attr;
148 gp->scr_baud = ip_baud;
149 gp->scr_bell = ip_bell;
150 gp->scr_busy = ip_busy;
151 gp->scr_clrtoeol = ip_clrtoeol;
152 gp->scr_cursor = ip_cursor;
153 gp->scr_deleteln = ip_deleteln;
154 gp->scr_discard = ip_discard;
155 gp->scr_event = ip_event;
156 gp->scr_ex_adjust = ip_ex_adjust;
157 gp->scr_fmap = ip_fmap;
158 gp->scr_insertln = ip_insertln;
159 gp->scr_keyval = ip_keyval;
160 gp->scr_move = ip_move;
161 gp->scr_msg = NULL;
162 gp->scr_optchange = ip_optchange;
163 gp->scr_refresh = ip_refresh;
164 gp->scr_rename = ip_rename;
165 gp->scr_reply = ip_reply;
166 gp->scr_screen = ip_screen;
167 gp->scr_split = ip_split;
168 gp->scr_suspend = ip_suspend;
169 gp->scr_usage = ip_usage;
173 * perr --
174 * Print system error.
176 static void
177 perr(name, msg)
178 char *name, *msg;
180 (void)fprintf(stderr, "%s:", name);
181 if (msg != NULL)
182 (void)fprintf(stderr, "%s:", msg);
183 (void)fprintf(stderr, "%s\n", strerror(errno));
184 exit(1);