Merge from vendor branch NTPD:
[dragonfly.git] / contrib / nvi / ip / ip_main.c
1 /*-
2  * Copyright (c) 1996
3  *      Keith Bostic.  All rights reserved.
4  *
5  * See the LICENSE file for redistribution information.
6  */
7
8 #include "config.h"
9
10 #ifndef lint
11 static const char sccsid[] = "@(#)ip_main.c     8.3 (Berkeley) 10/13/96";
12 #endif /* not lint */
13
14 #include <sys/types.h>
15 #include <sys/queue.h>
16
17 #include <bitstring.h>
18 #include <ctype.h>
19 #include <errno.h>
20 #include <stdio.h>
21
22 #include "../common/common.h"
23 #include "ip.h"
24
25 static void        ip_func_std __P((GS *));
26 static IP_PRIVATE *ip_init __P((GS *, char *));
27 static void        perr __P((char *, char *));
28
29 /*
30  * main --
31  *      This is the main loop for the vi-as-library editor.
32  */
33 int
34 ip_main(argc, argv, gp, ip_arg)
35         int argc;
36         char *argv[], *ip_arg;
37         GS *gp;
38 {
39         EVENT ev;
40         IP_PRIVATE *ipp;
41         IP_BUF ipb;
42         int rval;
43
44         /* Create and partially initialize the IP structure. */
45         if ((ipp = ip_init(gp, ip_arg)) == NULL)
46                 return (1);
47
48         /* Add the terminal type to the global structure. */
49         if ((OG_D_STR(gp, GO_TERM) =
50             OG_STR(gp, GO_TERM) = strdup("ip_curses")) == NULL)
51                 perr(gp->progname, NULL);
52
53         /*
54          * Figure out how big the screen is -- read events until we get
55          * the rows and columns.
56          */
57         do {
58                 if (ip_event(NULL, &ev, 0, 0))
59                         return (1);
60         } while (ev.e_event != E_EOF && ev.e_event != E_ERR &&
61             ev.e_event != E_QUIT && ev.e_event != E_WRESIZE &&
62             ev.e_event != E_SIGHUP && ev.e_event != E_SIGTERM);
63         if (ev.e_event != E_WRESIZE)
64                 return (1);
65
66         /* Run ex/vi. */
67         rval = editor(gp, argc, argv);
68
69         /* Clean up the screen. */
70         (void)ip_quit(gp);
71
72         /* Free the global and IP private areas. */
73 #if defined(DEBUG) || defined(PURIFY) || defined(LIBRARY)
74         free(ipp);
75         free(gp);
76 #endif
77
78         return (rval);
79 }
80
81 /*
82  * ip_init --
83  *      Create and partially initialize the GS structure.
84  */
85 static IP_PRIVATE *
86 ip_init(gp, ip_arg)
87         GS *gp;
88         char *ip_arg;
89 {
90         IP_PRIVATE *ipp;
91         char *ep;
92
93         /* Allocate the IP private structure. */
94         CALLOC_NOMSG(NULL, ipp, IP_PRIVATE *, 1, sizeof(IP_PRIVATE));
95         if (ipp == NULL)
96                 perr(gp->progname,  NULL);
97         gp->ip_private = ipp;
98
99         /*
100          * Crack ip_arg -- it's of the form #.#, where the first number is the
101          * file descriptor from the screen, the second is the file descriptor
102          * to the screen.
103          */
104         if (!isdigit(ip_arg[0]))
105                 goto usage;
106         ipp->i_fd = strtol(ip_arg, &ep, 10);
107         if (ep[0] != '.' || !isdigit(ep[1]))
108                 goto usage;
109         ipp->o_fd = strtol(++ep, &ep, 10);
110         if (ep[0] != '\0') {
111 usage:          ip_usage();
112                 return (NULL);
113         }
114
115         /* Initialize the list of ip functions. */
116         ip_func_std(gp);
117
118         return (ipp);
119 }
120
121 /*
122  * ip_func_std --
123  *      Initialize the standard ip functions.
124  */
125 static void
126 ip_func_std(gp)
127         GS *gp;
128 {
129         gp->scr_addstr = ip_addstr;
130         gp->scr_attr = ip_attr;
131         gp->scr_baud = ip_baud;
132         gp->scr_bell = ip_bell;
133         gp->scr_busy = ip_busy;
134         gp->scr_clrtoeol = ip_clrtoeol;
135         gp->scr_cursor = ip_cursor;
136         gp->scr_deleteln = ip_deleteln;
137         gp->scr_event = ip_event;
138         gp->scr_ex_adjust = ip_ex_adjust;
139         gp->scr_fmap = ip_fmap;
140         gp->scr_insertln = ip_insertln;
141         gp->scr_keyval = ip_keyval;
142         gp->scr_move = ip_move;
143         gp->scr_msg = NULL;
144         gp->scr_optchange = ip_optchange;
145         gp->scr_refresh = ip_refresh;
146         gp->scr_rename = ip_rename;
147         gp->scr_screen = ip_screen;
148         gp->scr_suspend = ip_suspend;
149         gp->scr_usage = ip_usage;
150 }
151
152 /*
153  * perr --
154  *      Print system error.
155  */
156 static void
157 perr(name, msg)
158         char *name, *msg;
159 {
160         (void)fprintf(stderr, "%s:", name);
161         if (msg != NULL)
162                 (void)fprintf(stderr, "%s:", msg);
163         (void)fprintf(stderr, "%s\n", strerror(errno));
164         exit(1);
165 }