rework initial error handling -- since we check for any screens on
[nvi.git] / ex / ex_init.c
blobd272353e2ae64427654edf6bb96e41e95377d947
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: ex_init.c,v 8.11 1994/01/09 16:45:46 bostic Exp $ (Berkeley) $Date: 1994/01/09 16:45:46 $";
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 "excmd.h"
20 #include "tag.h"
23 * ex_screen_copy --
24 * Copy ex screen.
26 int
27 ex_screen_copy(orig, sp)
28 SCR *orig, *sp;
30 EX_PRIVATE *oexp, *nexp;
32 /* Create the private ex structure. */
33 CALLOC_RET(orig, nexp, EX_PRIVATE *, 1, sizeof(EX_PRIVATE));
34 sp->ex_private = nexp;
36 /* Initialize queues. */
37 TAILQ_INIT(&nexp->tagq);
38 TAILQ_INIT(&nexp->tagfq);
39 CIRCLEQ_INIT(&nexp->rangeq);
41 if (orig == NULL) {
42 nexp->at_lbuf_set = 0;
43 } else {
44 oexp = EXP(orig);
46 nexp->at_lbuf = oexp->at_lbuf;
47 nexp->at_lbuf_set = oexp->at_lbuf_set;
49 if (oexp->lastbcomm != NULL &&
50 (nexp->lastbcomm = strdup(oexp->lastbcomm)) == NULL) {
51 msgq(sp, M_SYSERR, NULL);
52 return(1);
55 if (ex_tagcopy(orig, sp))
56 return (1);
59 nexp->lastcmd = &cmds[C_PRINT];
60 return (0);
64 * ex_screen_end --
65 * End a vi screen.
67 int
68 ex_screen_end(sp)
69 SCR *sp;
71 EX_PRIVATE *exp;
72 int rval;
74 rval = 0;
75 exp = EXP(sp);
77 if (argv_free(sp))
78 rval = 1;
80 if (exp->ibp != NULL)
81 FREE(exp->ibp, exp->ibp_len);
83 if (exp->lastbcomm != NULL)
84 FREE(exp->lastbcomm, strlen(exp->lastbcomm) + 1);
86 /* Free private memory. */
87 FREE(exp, sizeof(EX_PRIVATE));
88 sp->ex_private = NULL;
90 return (rval);
94 * ex_init --
95 * Initialize ex.
97 int
98 ex_init(sp, ep)
99 SCR *sp;
100 EXF *ep;
102 size_t len;
105 * The default address is the last line of the file. If the address
106 * set bit is on for this file, load the address, ensuring that it
107 * exists.
109 if (F_ISSET(sp->frp, FR_CURSORSET)) {
110 sp->lno = sp->frp->lno;
111 sp->cno = sp->frp->cno;
113 if (file_gline(sp, ep, sp->lno, &len) == NULL) {
114 if (file_lline(sp, ep, &sp->lno))
115 return (1);
116 if (sp->lno == 0)
117 sp->lno = 1;
118 sp->cno = 0;
119 } else if (sp->cno >= len)
120 sp->cno = 0;
121 } else {
122 if (file_lline(sp, ep, &sp->lno))
123 return (1);
124 if (sp->lno == 0)
125 sp->lno = 1;
126 sp->cno = 0;
129 /* Display the status line. */
130 return (status(sp, ep, sp->lno, 0));
134 * ex_end --
135 * End ex session.
138 ex_end(sp)
139 SCR *sp;
141 /* Save the cursor location. */
142 sp->frp->lno = sp->lno;
143 sp->frp->cno = sp->cno;
144 F_SET(sp->frp, FR_CURSORSET);
146 return (0);
150 * ex_optchange --
151 * Handle change of options for vi.
154 ex_optchange(sp, opt)
155 SCR *sp;
156 int opt;
158 switch (opt) {
159 case O_TAGS:
160 return (ex_tagalloc(sp, O_STR(sp, O_TAGS)));
162 return (0);