1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #ifndef lint 19 static char sccsid[] = "@(#)context.c 3.10 (Berkeley) 10/13/89"; 20 #endif /* not lint */ 21 22 #include "value.h" 23 #include "string.h" 24 #include "context.h" 25 26 /* 27 * Context push/pop for nested command files. 28 */ 29 30 char *malloc(); 31 32 cx_alloc() 33 { 34 register struct context *xp; 35 36 if (cx.x_type != 0) { 37 xp = (struct context *) 38 malloc((unsigned) sizeof (struct context)); 39 if (xp == 0) 40 return -1; 41 *xp = cx; 42 cx.x_link = xp; 43 cx.x_type = 0; 44 } 45 cx.x_erred = 0; 46 cx.x_synerred = 0; 47 cx.x_abort = 0; 48 return 0; 49 } 50 51 cx_free() 52 { 53 struct context *xp; 54 55 if ((xp = cx.x_link) != 0) { 56 cx = *xp; 57 free((char *)xp); 58 } else 59 cx.x_type = 0; 60 } 61 62 cx_beginfile(filename) 63 char *filename; 64 { 65 if (cx_alloc() < 0) 66 return -1; 67 cx.x_type = X_FILE; 68 if ((cx.x_filename = str_cpy(filename)) == 0) 69 goto bad; 70 cx.x_fp = fopen(filename, "r"); 71 if (cx.x_fp == 0) 72 goto bad; 73 cx.x_bol = 1; 74 cx.x_lineno = 0; 75 cx.x_errwin = 0; 76 cx.x_noerr = 0; 77 return 0; 78 bad: 79 if (cx.x_filename != 0) 80 str_free(cx.x_filename); 81 cx_free(); 82 return -1; 83 } 84 85 cx_beginbuf(buf, arg, narg) 86 char *buf; 87 struct value *arg; 88 int narg; 89 { 90 if (cx_alloc() < 0) 91 return -1; 92 cx.x_type = X_BUF; 93 cx.x_bufp = cx.x_buf = buf; 94 cx.x_arg = arg; 95 cx.x_narg = narg; 96 return 0; 97 } 98 99 cx_end() 100 { 101 switch (cx.x_type) { 102 case X_BUF: 103 break; 104 case X_FILE: 105 (void) fclose(cx.x_fp); 106 str_free(cx.x_filename); 107 break; 108 } 109 cx_free(); 110 } 111