Annotation of src/usr.bin/window/context.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: context.c,v 1.4 2001/11/19 19:02:18 mpech Exp $ */
1.1 deraadt 2: /* $NetBSD: context.c,v 1.3 1995/09/28 10:34:15 tls Exp $ */
3:
4: /*
5: * Copyright (c) 1983, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * This code is derived from software contributed to Berkeley by
9: * Edward Wang at The University of California, Berkeley.
10: *
11: * Redistribution and use in source and binary forms, with or without
12: * modification, are permitted provided that the following conditions
13: * are met:
14: * 1. Redistributions of source code must retain the above copyright
15: * notice, this list of conditions and the following disclaimer.
16: * 2. Redistributions in binary form must reproduce the above copyright
17: * notice, this list of conditions and the following disclaimer in the
18: * documentation and/or other materials provided with the distribution.
1.5 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)context.c 8.1 (Berkeley) 6/6/93";
39: #else
1.5 ! millert 40: static char rcsid[] = "$OpenBSD: context.c,v 1.4 2001/11/19 19:02:18 mpech Exp $";
1.1 deraadt 41: #endif
42: #endif /* not lint */
43:
44: #include "value.h"
45: #include "string.h"
46: #include "context.h"
47: #include <fcntl.h>
48:
49: /*
50: * Context push/pop for nested command files.
51: */
52:
53: char *malloc();
54:
55: cx_alloc()
56: {
1.4 mpech 57: struct context *xp;
1.1 deraadt 58:
59: if (cx.x_type != 0) {
60: xp = (struct context *)
61: malloc((unsigned) sizeof (struct context));
62: if (xp == 0)
63: return -1;
64: *xp = cx;
65: cx.x_link = xp;
66: cx.x_type = 0;
67: }
68: cx.x_erred = 0;
69: cx.x_synerred = 0;
70: cx.x_abort = 0;
71: return 0;
72: }
73:
74: cx_free()
75: {
76: struct context *xp;
77:
78: if ((xp = cx.x_link) != 0) {
79: cx = *xp;
80: free((char *)xp);
81: } else
82: cx.x_type = 0;
83: }
84:
85: cx_beginfile(filename)
86: char *filename;
87: {
88: if (cx_alloc() < 0)
89: return -1;
90: cx.x_type = X_FILE;
91: if ((cx.x_filename = str_cpy(filename)) == 0)
92: goto bad;
93: cx.x_fp = fopen(filename, "r");
94: if (cx.x_fp == 0)
95: goto bad;
96: (void) fcntl(fileno(cx.x_fp), F_SETFD, 1);
97: cx.x_bol = 1;
98: cx.x_lineno = 0;
99: cx.x_errwin = 0;
100: cx.x_noerr = 0;
101: return 0;
102: bad:
103: if (cx.x_filename != 0)
104: str_free(cx.x_filename);
105: cx_free();
106: return -1;
107: }
108:
109: cx_beginbuf(buf, arg, narg)
110: char *buf;
111: struct value *arg;
112: int narg;
113: {
114: if (cx_alloc() < 0)
115: return -1;
116: cx.x_type = X_BUF;
117: cx.x_bufp = cx.x_buf = buf;
118: cx.x_arg = arg;
119: cx.x_narg = narg;
120: return 0;
121: }
122:
123: cx_end()
124: {
125: switch (cx.x_type) {
126: case X_BUF:
127: break;
128: case X_FILE:
129: (void) fclose(cx.x_fp);
130: str_free(cx.x_filename);
131: break;
132: }
133: cx_free();
134: }