1 #ifndef lint 2 static char sccsid[] = "@(#)context.c 3.7 04/24/85"; 3 #endif 4 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 11 #include <stdio.h> 12 #include "value.h" 13 #include "string.h" 14 #include "context.h" 15 16 /* 17 * Context push/pop for nested command files. 18 */ 19 20 char *malloc(); 21 22 cx_alloc() 23 { 24 register struct context *xp; 25 26 if (cx.x_type != 0) { 27 xp = (struct context *) 28 malloc((unsigned) sizeof (struct context)); 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 41 cx_free() 42 { 43 struct context *xp; 44 45 if ((xp = cx.x_link) != 0) { 46 cx = *xp; 47 free((char *)xp); 48 } else 49 cx.x_type = 0; 50 } 51 52 cx_beginfile(filename) 53 char *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; 66 cx.x_noerr = 0; 67 return 0; 68 bad: 69 if (cx.x_filename != 0) 70 str_free(cx.x_filename); 71 cx_free(); 72 return -1; 73 } 74 75 cx_beginbuf(buf, arg, narg) 76 char *buf; 77 struct value *arg; 78 int narg; 79 { 80 if (cx_alloc() < 0) 81 return -1; 82 cx.x_type = X_BUF; 83 cx.x_bufp = cx.x_buf = buf; 84 cx.x_arg = arg; 85 cx.x_narg = narg; 86 return 0; 87 } 88 89 cx_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 } 101