never tell the scroll functions that the file has no lines.
[nvi.git] / ip / ip_main.c
blobab5f7b42942a3a6f2ff75d901ad9b9c4a61edeaa
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.7 1996/12/11 13:34:22 bostic Exp $ (Berkeley) $Date: 1996/12/11 13:34:22 $";
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>
24 #include "../common/common.h"
25 #include "ip.h"
26 #include "../include/ipc_extern.h"
28 int vi_ofd; /* GLOBAL: known to __vi_send(). */
30 static void ip_func_std __P((GS *));
31 static IP_PRIVATE *ip_init __P((GS *, char *));
32 static void perr __P((char *, char *));
35 * ip_main --
36 * This is the main loop for the vi-as-library editor.
38 * PUBLIC: int ip_main __P((int, char *[], GS *, char *));
40 int
41 ip_main(argc, argv, gp, ip_arg)
42 int argc;
43 char *argv[], *ip_arg;
44 GS *gp;
46 EVENT ev;
47 IP_PRIVATE *ipp;
48 IP_BUF ipb;
49 int rval;
51 /* Create and partially initialize the IP structure. */
52 if ((ipp = ip_init(gp, ip_arg)) == NULL)
53 return (1);
55 /* Add the terminal type to the global structure. */
56 if ((OG_D_STR(gp, GO_TERM) =
57 OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL)
58 perr(gp->progname, NULL);
61 * Figure out how big the screen is -- read events until we get
62 * the rows and columns.
64 for (;;) {
65 if (ip_event(NULL, &ev, 0, 0))
66 return (1);
67 if (ev.e_event == E_WRESIZE)
68 break;
69 if (ev.e_event == E_EOF || ev.e_event == E_ERR ||
70 ev.e_event == E_SIGHUP || ev.e_event == E_SIGTERM)
71 return (1);
72 if (ev.e_event == E_IPCOMMAND && ev.e_ipcom == VI_QUIT)
73 return (1);
76 /* Run ex/vi. */
77 rval = editor(gp, argc, argv);
79 /* Clean up the screen. */
80 (void)ip_quit(gp);
82 /* Send the quit message. */
83 ipb.code = SI_QUIT;
84 (void)__vi_send(NULL, &ipb);
86 /* Give the screen a couple of seconds to deal with it. */
87 sleep(2);
89 /* Free the global and IP private areas. */
90 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
91 free(ipp);
92 free(gp);
93 #endif
94 return (rval);
98 * ip_init --
99 * Create and partially initialize the GS structure.
101 static IP_PRIVATE *
102 ip_init(gp, ip_arg)
103 GS *gp;
104 char *ip_arg;
106 IP_PRIVATE *ipp;
107 char *ep;
109 /* Allocate the IP private structure. */
110 CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE));
111 if (ipp == NULL)
112 perr(gp->progname, NULL);
113 gp->ip_private = ipp;
116 * Crack ip_arg -- it's of the form #.#, where the first number is the
117 * file descriptor from the screen, the second is the file descriptor
118 * to the screen.
120 if (!isdigit(ip_arg[0]))
121 goto usage;
122 ipp->i_fd = strtol(ip_arg, &ep, 10);
123 if (ep[0] != '.' || !isdigit(ep[1]))
124 goto usage;
125 vi_ofd = strtol(++ep, &ep, 10);
126 if (ep[0] != '\0') {
127 usage: ip_usage();
128 return (NULL);
131 /* Initialize the list of ip functions. */
132 ip_func_std(gp);
134 return (ipp);
138 * ip_func_std --
139 * Initialize the standard ip functions.
141 static void
142 ip_func_std(gp)
143 GS *gp;
145 gp->scr_addstr = ip_addstr;
146 gp->scr_attr = ip_attr;
147 gp->scr_baud = ip_baud;
148 gp->scr_bell = ip_bell;
149 gp->scr_busy = ip_busy;
150 gp->scr_clrtoeol = ip_clrtoeol;
151 gp->scr_cursor = ip_cursor;
152 gp->scr_deleteln = ip_deleteln;
153 gp->scr_discard = ip_discard;
154 gp->scr_event = ip_event;
155 gp->scr_ex_adjust = ip_ex_adjust;
156 gp->scr_fmap = ip_fmap;
157 gp->scr_insertln = ip_insertln;
158 gp->scr_keyval = ip_keyval;
159 gp->scr_move = ip_move;
160 gp->scr_msg = NULL;
161 gp->scr_optchange = ip_optchange;
162 gp->scr_refresh = ip_refresh;
163 gp->scr_rename = ip_rename;
164 gp->scr_screen = ip_screen;
165 gp->scr_split = ip_split;
166 gp->scr_suspend = ip_suspend;
167 gp->scr_usage = ip_usage;
171 * perr --
172 * Print system error.
174 static void
175 perr(name, msg)
176 char *name, *msg;
178 (void)fprintf(stderr, "%s:", name);
179 if (msg != NULL)
180 (void)fprintf(stderr, "%s:", msg);
181 (void)fprintf(stderr, "%s\n", strerror(errno));
182 exit(1);