xref: /csrg-svn/usr.bin/window/context.c (revision 33514)
118746Sedward /*
2*33514Sbostic  * Copyright (c) 1983 Regents of the University of California.
3*33514Sbostic  * All rights reserved.
4*33514Sbostic  *
5*33514Sbostic  * Redistribution and use in source and binary forms are permitted
6*33514Sbostic  * provided that this notice is preserved and that due credit is given
7*33514Sbostic  * to the University of California at Berkeley. The name of the University
8*33514Sbostic  * may not be used to endorse or promote products derived from this
9*33514Sbostic  * software without specific prior written permission. This software
10*33514Sbostic  * is provided ``as is'' without express or implied warranty.
1118746Sedward  */
1218746Sedward 
13*33514Sbostic #ifndef lint
14*33514Sbostic static char sccsid[] = "@(#)context.c	3.8 (Berkeley) 02/21/88";
15*33514Sbostic #endif /* not lint */
16*33514Sbostic 
1715562Sedward #include <stdio.h>
1815562Sedward #include "value.h"
1915579Sedward #include "string.h"
2015562Sedward #include "context.h"
2115562Sedward 
2215562Sedward /*
2315562Sedward  * Context push/pop for nested command files.
2415562Sedward  */
2515562Sedward 
2615562Sedward char *malloc();
2715562Sedward 
2815562Sedward cx_alloc()
2915562Sedward {
3015562Sedward 	register struct context *xp;
3115562Sedward 
3215562Sedward 	if (cx.x_type != 0) {
3315562Sedward 		xp = (struct context *)
3415579Sedward 			malloc((unsigned) sizeof (struct context));
3515562Sedward 		if (xp == 0)
3615562Sedward 			return -1;
3715562Sedward 		*xp = cx;
3815562Sedward 		cx.x_link = xp;
3915562Sedward 		cx.x_type = 0;
4015562Sedward 	}
4115562Sedward 	cx.x_erred = 0;
4215562Sedward 	cx.x_synerred = 0;
4315562Sedward 	cx.x_abort = 0;
4415562Sedward 	return 0;
4515562Sedward }
4615562Sedward 
4715562Sedward cx_free()
4815562Sedward {
4915579Sedward 	struct context *xp;
5015579Sedward 
5115579Sedward 	if ((xp = cx.x_link) != 0) {
5215579Sedward 		cx = *xp;
5315579Sedward 		free((char *)xp);
5415562Sedward 	} else
5515562Sedward 		cx.x_type = 0;
5615562Sedward }
5715562Sedward 
5815855Sedward cx_beginfile(filename)
5915562Sedward char *filename;
6015562Sedward {
6115562Sedward 	if (cx_alloc() < 0)
6215562Sedward 		return -1;
6315562Sedward 	cx.x_type = X_FILE;
6415562Sedward 	if ((cx.x_filename = str_cpy(filename)) == 0)
6515562Sedward 		goto bad;
6615562Sedward 	cx.x_fp = fopen(filename, "r");
6715562Sedward 	if (cx.x_fp == 0)
6815562Sedward 		goto bad;
6915562Sedward 	cx.x_bol = 1;
7015562Sedward 	cx.x_lineno = 0;
7115562Sedward 	cx.x_errwin = 0;
7215855Sedward 	cx.x_noerr = 0;
7315562Sedward 	return 0;
7415562Sedward bad:
7515562Sedward 	if (cx.x_filename != 0)
7615562Sedward 		str_free(cx.x_filename);
7715562Sedward 	cx_free();
7815562Sedward 	return -1;
7915562Sedward }
8015562Sedward 
8116448Sedward cx_beginbuf(buf, arg, narg)
8215562Sedward char *buf;
8316448Sedward struct value *arg;
8416448Sedward int narg;
8515562Sedward {
8615562Sedward 	if (cx_alloc() < 0)
8715562Sedward 		return -1;
8815562Sedward 	cx.x_type = X_BUF;
8915562Sedward 	cx.x_bufp = cx.x_buf = buf;
9016448Sedward 	cx.x_arg = arg;
9116448Sedward 	cx.x_narg = narg;
9215562Sedward 	return 0;
9315562Sedward }
9415562Sedward 
9515562Sedward cx_end()
9615562Sedward {
9715562Sedward 	switch (cx.x_type) {
9815562Sedward 	case X_BUF:
9915562Sedward 		break;
10015562Sedward 	case X_FILE:
10115562Sedward 		(void) fclose(cx.x_fp);
10215562Sedward 		str_free(cx.x_filename);
10315562Sedward 		break;
10415562Sedward 	}
10515562Sedward 	cx_free();
10615562Sedward }
107