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