update to 1.01
[nvi.git] / vi / v_init.c
blob36354235b3733f13731c49ccb76993e391230a47
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: v_init.c,v 8.18 1994/01/09 16:47:16 bostic Exp $ (Berkeley) $Date: 1994/01/09 16:47:16 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
18 #include "vi.h"
19 #include "vcmd.h"
20 #include "excmd.h"
22 static int v_comment __P((SCR *, EXF *));
25 * v_screen_copy --
26 * Copy vi screen.
28 int
29 v_screen_copy(orig, sp)
30 SCR *orig, *sp;
32 VI_PRIVATE *ovip, *nvip;
34 /* Create the private vi structure. */
35 CALLOC_RET(orig, nvip, VI_PRIVATE *, 1, sizeof(VI_PRIVATE));
36 sp->vi_private = nvip;
38 if (orig == NULL) {
39 nvip->inc_lastch = '+';
40 nvip->inc_lastval = 1;
41 } else {
42 ovip = VIP(orig);
44 /* User can replay the last input, but nothing else. */
45 if (ovip->rep_len != 0) {
46 MALLOC(orig, nvip->rep, char *, ovip->rep_len);
47 if (nvip->rep != NULL) {
48 memmove(nvip->rep, ovip->rep, ovip->rep_len);
49 nvip->rep_len = ovip->rep_len;
53 nvip->inc_lastch = ovip->inc_lastch;
54 nvip->inc_lastval = ovip->inc_lastval;
56 if (ovip->paragraph != NULL &&
57 (nvip->paragraph = strdup(ovip->paragraph)) == NULL) {
58 msgq(sp, M_SYSERR, NULL);
59 return (1);
62 return (0);
66 * v_screen_end --
67 * End a vi screen.
69 int
70 v_screen_end(sp)
71 SCR *sp;
73 VI_PRIVATE *vip;
75 vip = VIP(sp);
77 if (vip->rep != NULL)
78 FREE(vip->rep, vip->rep_len);
80 if (vip->paragraph != NULL)
81 FREE(vip->paragraph, vip->paragraph_len);
83 /* Free private memory. */
84 FREE(vip, sizeof(VI_PRIVATE));
85 sp->vi_private = NULL;
87 return (0);
91 * v_init --
92 * Initialize vi.
94 int
95 v_init(sp, ep)
96 SCR *sp;
97 EXF *ep;
99 size_t len;
102 * The default address is line 1, column 0. If the address set
103 * bit is on for this file, load the address, ensuring that it
104 * exists.
106 if (F_ISSET(sp->frp, FR_CURSORSET)) {
107 sp->lno = sp->frp->lno;
108 sp->cno = sp->frp->cno;
110 if (file_gline(sp, ep, sp->lno, &len) == NULL) {
111 if (sp->lno != 1 || sp->cno != 0) {
112 if (file_lline(sp, ep, &sp->lno))
113 return (1);
114 if (sp->lno == 0)
115 sp->lno = 1;
116 sp->cno = 0;
118 } else if (sp->cno >= len)
119 sp->cno = 0;
121 } else {
122 sp->lno = 1;
123 sp->cno = 0;
125 if (O_ISSET(sp, O_COMMENT) && v_comment(sp, ep))
126 return (1);
129 /* Reset strange attraction. */
130 sp->rcm = 0;
131 sp->rcmflags = 0;
133 /* Make ex display to a special function. */
134 if ((sp->stdfp = fwopen(sp, sp->s_ex_write)) == NULL) {
135 msgq(sp, M_SYSERR, "ex output");
136 return (1);
138 #ifdef MAKE_EX_OUTPUT_LINE_BUFFERED
139 (void)setvbuf(sp->stdfp, NULL, _IOLBF, 0);
140 #endif
142 /* Display the status line. */
143 return (status(sp, ep, sp->lno, 0));
147 * v_end --
148 * End vi session.
151 v_end(sp)
152 SCR *sp;
154 /* Close down ex output file descriptor. */
155 (void)fclose(sp->stdfp);
157 return (0);
161 * v_optchange --
162 * Handle change of options for vi.
165 v_optchange(sp, opt)
166 SCR *sp;
167 int opt;
169 switch (opt) {
170 case O_PARAGRAPHS:
171 case O_SECTIONS:
172 return (v_buildparagraph(sp));
174 return (0);
178 * v_comment --
179 * Skip the first comment.
181 static int
182 v_comment(sp, ep)
183 SCR *sp;
184 EXF *ep;
186 recno_t lno;
187 size_t len;
188 char *p;
190 for (lno = 1;
191 (p = file_gline(sp, ep, lno, &len)) != NULL && len == 0; ++lno);
192 if (p == NULL || len <= 1 || memcmp(p, "/*", 2))
193 return (0);
194 do {
195 for (; len; --len, ++p)
196 if (p[0] == '*' && len > 1 && p[1] == '/') {
197 sp->lno = lno;
198 return (0);
200 } while ((p = file_gline(sp, ep, ++lno, &len)) != NULL);
201 return (0);