xref: /csrg-svn/usr.bin/window/context.c (revision 42954)
118746Sedward /*
233514Sbostic  * Copyright (c) 1983 Regents of the University of California.
333514Sbostic  * All rights reserved.
433514Sbostic  *
5*42954Sbostic  * This code is derived from software contributed to Berkeley by
6*42954Sbostic  * Edward Wang at The University of California, Berkeley.
7*42954Sbostic  *
842835Sbostic  * %sccs.include.redist.c%
918746Sedward  */
1018746Sedward 
1133514Sbostic #ifndef lint
12*42954Sbostic static char sccsid[] = "@(#)context.c	3.12 (Berkeley) 06/06/90";
1333514Sbostic #endif /* not lint */
1433514Sbostic 
1515562Sedward #include "value.h"
1615579Sedward #include "string.h"
1715562Sedward #include "context.h"
1815562Sedward 
1915562Sedward /*
2015562Sedward  * Context push/pop for nested command files.
2115562Sedward  */
2215562Sedward 
2315562Sedward char *malloc();
2415562Sedward 
2515562Sedward cx_alloc()
2615562Sedward {
2715562Sedward 	register struct context *xp;
2815562Sedward 
2915562Sedward 	if (cx.x_type != 0) {
3015562Sedward 		xp = (struct context *)
3115579Sedward 			malloc((unsigned) sizeof (struct context));
3215562Sedward 		if (xp == 0)
3315562Sedward 			return -1;
3415562Sedward 		*xp = cx;
3515562Sedward 		cx.x_link = xp;
3615562Sedward 		cx.x_type = 0;
3715562Sedward 	}
3815562Sedward 	cx.x_erred = 0;
3915562Sedward 	cx.x_synerred = 0;
4015562Sedward 	cx.x_abort = 0;
4115562Sedward 	return 0;
4215562Sedward }
4315562Sedward 
4415562Sedward cx_free()
4515562Sedward {
4615579Sedward 	struct context *xp;
4715579Sedward 
4815579Sedward 	if ((xp = cx.x_link) != 0) {
4915579Sedward 		cx = *xp;
5015579Sedward 		free((char *)xp);
5115562Sedward 	} else
5215562Sedward 		cx.x_type = 0;
5315562Sedward }
5415562Sedward 
5515855Sedward cx_beginfile(filename)
5615562Sedward char *filename;
5715562Sedward {
5815562Sedward 	if (cx_alloc() < 0)
5915562Sedward 		return -1;
6015562Sedward 	cx.x_type = X_FILE;
6115562Sedward 	if ((cx.x_filename = str_cpy(filename)) == 0)
6215562Sedward 		goto bad;
6315562Sedward 	cx.x_fp = fopen(filename, "r");
6415562Sedward 	if (cx.x_fp == 0)
6515562Sedward 		goto bad;
6615562Sedward 	cx.x_bol = 1;
6715562Sedward 	cx.x_lineno = 0;
6815562Sedward 	cx.x_errwin = 0;
6915855Sedward 	cx.x_noerr = 0;
7015562Sedward 	return 0;
7115562Sedward bad:
7215562Sedward 	if (cx.x_filename != 0)
7315562Sedward 		str_free(cx.x_filename);
7415562Sedward 	cx_free();
7515562Sedward 	return -1;
7615562Sedward }
7715562Sedward 
7816448Sedward cx_beginbuf(buf, arg, narg)
7915562Sedward char *buf;
8016448Sedward struct value *arg;
8116448Sedward int narg;
8215562Sedward {
8315562Sedward 	if (cx_alloc() < 0)
8415562Sedward 		return -1;
8515562Sedward 	cx.x_type = X_BUF;
8615562Sedward 	cx.x_bufp = cx.x_buf = buf;
8716448Sedward 	cx.x_arg = arg;
8816448Sedward 	cx.x_narg = narg;
8915562Sedward 	return 0;
9015562Sedward }
9115562Sedward 
9215562Sedward cx_end()
9315562Sedward {
9415562Sedward 	switch (cx.x_type) {
9515562Sedward 	case X_BUF:
9615562Sedward 		break;
9715562Sedward 	case X_FILE:
9815562Sedward 		(void) fclose(cx.x_fp);
9915562Sedward 		str_free(cx.x_filename);
10015562Sedward 		break;
10115562Sedward 	}
10215562Sedward 	cx_free();
10315562Sedward }
104