date and time created 83/11/22 12:48:04 by edward
[unix-history] / usr / src / usr.bin / window / context.c
CommitLineData
e020e0b3
EW
1#ifndef lint
2static char *sccsid = "@(#)context.c 3.1 83/11/22";
3#endif
4
5#include <stdio.h>
6#include "value.h"
7#include "context.h"
8
9/*
10 * Context push/pop for nested command files.
11 */
12
13char *malloc();
14
15cx_alloc()
16{
17 register struct context *xp;
18
19 if (cx.x_type != 0) {
20 xp = (struct context *)
21 malloc(1, (unsigned) sizeof (struct context));
22 if (xp == 0)
23 return -1;
24 *xp = cx;
25 cx.x_link = xp;
26 cx.x_type = 0;
27 }
28 cx.x_erred = 0;
29 cx.x_synerred = 0;
30 cx.x_abort = 0;
31 return 0;
32}
33
34cx_free()
35{
36 if (cx.x_link != 0) {
37 free(cx.x_link);
38 cx = *cx.x_link;
39 } else
40 cx.x_type = 0;
41}
42
43cx_setfile(filename)
44char *filename;
45{
46 if (cx_alloc() < 0)
47 return -1;
48 cx.x_type = X_FILE;
49 if ((cx.x_filename = str_cpy(filename)) == 0)
50 goto bad;
51 cx.x_fp = fopen(filename, "r");
52 if (cx.x_fp == 0)
53 goto bad;
54 cx.x_bol = 1;
55 cx.x_lineno = 0;
56 cx.x_errwin = 0;
57 cx.x_baderr = 0;
58 return 0;
59bad:
60 if (cx.x_filename != 0)
61 str_free(cx.x_filename);
62 cx_free();
63 return -1;
64}
65
66cx_setbuf(buf)
67char *buf;
68{
69 if (cx_alloc() < 0)
70 return -1;
71 cx.x_type = X_BUF;
72 cx.x_bufp = cx.x_buf = buf;
73 return 0;
74}
75
76cx_end()
77{
78 switch (cx.x_type) {
79 case X_BUF:
80 break;
81 case X_FILE:
82 (void) fclose(cx.x_fp);
83 str_free(cx.x_filename);
84 break;
85 }
86 cx_free();
87}