Commit | Line | Data |
---|---|---|
e020e0b3 | 1 | #ifndef lint |
7edc52ec | 2 | static char sccsid[] = "@(#)context.c 3.6 %G%"; |
e020e0b3 EW |
3 | #endif |
4 | ||
5 | #include <stdio.h> | |
6 | #include "value.h" | |
98c26215 | 7 | #include "string.h" |
e020e0b3 EW |
8 | #include "context.h" |
9 | ||
10 | /* | |
11 | * Context push/pop for nested command files. | |
12 | */ | |
13 | ||
14 | char *malloc(); | |
15 | ||
16 | cx_alloc() | |
17 | { | |
18 | register struct context *xp; | |
19 | ||
20 | if (cx.x_type != 0) { | |
21 | xp = (struct context *) | |
98c26215 | 22 | malloc((unsigned) sizeof (struct context)); |
e020e0b3 EW |
23 | if (xp == 0) |
24 | return -1; | |
25 | *xp = cx; | |
26 | cx.x_link = xp; | |
27 | cx.x_type = 0; | |
28 | } | |
29 | cx.x_erred = 0; | |
30 | cx.x_synerred = 0; | |
31 | cx.x_abort = 0; | |
32 | return 0; | |
33 | } | |
34 | ||
35 | cx_free() | |
36 | { | |
98c26215 EW |
37 | struct context *xp; |
38 | ||
39 | if ((xp = cx.x_link) != 0) { | |
40 | cx = *xp; | |
41 | free((char *)xp); | |
e020e0b3 EW |
42 | } else |
43 | cx.x_type = 0; | |
44 | } | |
45 | ||
a40a2f9a | 46 | cx_beginfile(filename) |
e020e0b3 EW |
47 | char *filename; |
48 | { | |
49 | if (cx_alloc() < 0) | |
50 | return -1; | |
51 | cx.x_type = X_FILE; | |
52 | if ((cx.x_filename = str_cpy(filename)) == 0) | |
53 | goto bad; | |
54 | cx.x_fp = fopen(filename, "r"); | |
55 | if (cx.x_fp == 0) | |
56 | goto bad; | |
57 | cx.x_bol = 1; | |
58 | cx.x_lineno = 0; | |
59 | cx.x_errwin = 0; | |
a40a2f9a | 60 | cx.x_noerr = 0; |
e020e0b3 EW |
61 | return 0; |
62 | bad: | |
63 | if (cx.x_filename != 0) | |
64 | str_free(cx.x_filename); | |
65 | cx_free(); | |
66 | return -1; | |
67 | } | |
68 | ||
bb4a0c0b | 69 | cx_beginbuf(buf, arg, narg) |
e020e0b3 | 70 | char *buf; |
bb4a0c0b EW |
71 | struct value *arg; |
72 | int narg; | |
e020e0b3 EW |
73 | { |
74 | if (cx_alloc() < 0) | |
75 | return -1; | |
76 | cx.x_type = X_BUF; | |
77 | cx.x_bufp = cx.x_buf = buf; | |
bb4a0c0b EW |
78 | cx.x_arg = arg; |
79 | cx.x_narg = narg; | |
e020e0b3 EW |
80 | return 0; |
81 | } | |
82 | ||
83 | cx_end() | |
84 | { | |
85 | switch (cx.x_type) { | |
86 | case X_BUF: | |
87 | break; | |
88 | case X_FILE: | |
89 | (void) fclose(cx.x_fp); | |
90 | str_free(cx.x_filename); | |
91 | break; | |
92 | } | |
93 | cx_free(); | |
94 | } |