Commit | Line | Data |
---|---|---|
60de5df9 | 1 | /* |
46e9ea25 KB |
2 | * Copyright (c) 1983 Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
5e8b0e60 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
60de5df9 EW |
16 | */ |
17 | ||
46e9ea25 | 18 | #ifndef lint |
f8bd9384 | 19 | static char sccsid[] = "@(#)context.c 3.10 (Berkeley) %G%"; |
46e9ea25 KB |
20 | #endif /* not lint */ |
21 | ||
e020e0b3 | 22 | #include "value.h" |
98c26215 | 23 | #include "string.h" |
e020e0b3 EW |
24 | #include "context.h" |
25 | ||
26 | /* | |
27 | * Context push/pop for nested command files. | |
28 | */ | |
29 | ||
30 | char *malloc(); | |
31 | ||
32 | cx_alloc() | |
33 | { | |
34 | register struct context *xp; | |
35 | ||
36 | if (cx.x_type != 0) { | |
37 | xp = (struct context *) | |
98c26215 | 38 | malloc((unsigned) sizeof (struct context)); |
e020e0b3 EW |
39 | if (xp == 0) |
40 | return -1; | |
41 | *xp = cx; | |
42 | cx.x_link = xp; | |
43 | cx.x_type = 0; | |
44 | } | |
45 | cx.x_erred = 0; | |
46 | cx.x_synerred = 0; | |
47 | cx.x_abort = 0; | |
48 | return 0; | |
49 | } | |
50 | ||
51 | cx_free() | |
52 | { | |
98c26215 EW |
53 | struct context *xp; |
54 | ||
55 | if ((xp = cx.x_link) != 0) { | |
56 | cx = *xp; | |
57 | free((char *)xp); | |
e020e0b3 EW |
58 | } else |
59 | cx.x_type = 0; | |
60 | } | |
61 | ||
a40a2f9a | 62 | cx_beginfile(filename) |
e020e0b3 EW |
63 | char *filename; |
64 | { | |
65 | if (cx_alloc() < 0) | |
66 | return -1; | |
67 | cx.x_type = X_FILE; | |
68 | if ((cx.x_filename = str_cpy(filename)) == 0) | |
69 | goto bad; | |
70 | cx.x_fp = fopen(filename, "r"); | |
71 | if (cx.x_fp == 0) | |
72 | goto bad; | |
73 | cx.x_bol = 1; | |
74 | cx.x_lineno = 0; | |
75 | cx.x_errwin = 0; | |
a40a2f9a | 76 | cx.x_noerr = 0; |
e020e0b3 EW |
77 | return 0; |
78 | bad: | |
79 | if (cx.x_filename != 0) | |
80 | str_free(cx.x_filename); | |
81 | cx_free(); | |
82 | return -1; | |
83 | } | |
84 | ||
bb4a0c0b | 85 | cx_beginbuf(buf, arg, narg) |
e020e0b3 | 86 | char *buf; |
bb4a0c0b EW |
87 | struct value *arg; |
88 | int narg; | |
e020e0b3 EW |
89 | { |
90 | if (cx_alloc() < 0) | |
91 | return -1; | |
92 | cx.x_type = X_BUF; | |
93 | cx.x_bufp = cx.x_buf = buf; | |
bb4a0c0b EW |
94 | cx.x_arg = arg; |
95 | cx.x_narg = narg; | |
e020e0b3 EW |
96 | return 0; |
97 | } | |
98 | ||
99 | cx_end() | |
100 | { | |
101 | switch (cx.x_type) { | |
102 | case X_BUF: | |
103 | break; | |
104 | case X_FILE: | |
105 | (void) fclose(cx.x_fp); | |
106 | str_free(cx.x_filename); | |
107 | break; | |
108 | } | |
109 | cx_free(); | |
110 | } |