118746Sedward /* 2*62459Sbostic * Copyright (c) 1983, 1993 3*62459Sbostic * The Regents of the University of California. All rights reserved. 433514Sbostic * 542954Sbostic * This code is derived from software contributed to Berkeley by 642954Sbostic * Edward Wang at The University of California, Berkeley. 742954Sbostic * 842835Sbostic * %sccs.include.redist.c% 918746Sedward */ 1018746Sedward 1133514Sbostic #ifndef lint 12*62459Sbostic static char sccsid[] = "@(#)context.c 8.1 (Berkeley) 06/06/93"; 1333514Sbostic #endif /* not lint */ 1433514Sbostic 1515562Sedward #include "value.h" 1615579Sedward #include "string.h" 1715562Sedward #include "context.h" 1856015Sedward #include <fcntl.h> 1915562Sedward 2015562Sedward /* 2115562Sedward * Context push/pop for nested command files. 2215562Sedward */ 2315562Sedward 2415562Sedward char *malloc(); 2515562Sedward cx_alloc()2615562Sedwardcx_alloc() 2715562Sedward { 2815562Sedward register struct context *xp; 2915562Sedward 3015562Sedward if (cx.x_type != 0) { 3115562Sedward xp = (struct context *) 3215579Sedward malloc((unsigned) sizeof (struct context)); 3315562Sedward if (xp == 0) 3415562Sedward return -1; 3515562Sedward *xp = cx; 3615562Sedward cx.x_link = xp; 3715562Sedward cx.x_type = 0; 3815562Sedward } 3915562Sedward cx.x_erred = 0; 4015562Sedward cx.x_synerred = 0; 4115562Sedward cx.x_abort = 0; 4215562Sedward return 0; 4315562Sedward } 4415562Sedward cx_free()4515562Sedwardcx_free() 4615562Sedward { 4715579Sedward struct context *xp; 4815579Sedward 4915579Sedward if ((xp = cx.x_link) != 0) { 5015579Sedward cx = *xp; 5115579Sedward free((char *)xp); 5215562Sedward } else 5315562Sedward cx.x_type = 0; 5415562Sedward } 5515562Sedward cx_beginfile(filename)5615855Sedwardcx_beginfile(filename) 5715562Sedward char *filename; 5815562Sedward { 5915562Sedward if (cx_alloc() < 0) 6015562Sedward return -1; 6115562Sedward cx.x_type = X_FILE; 6215562Sedward if ((cx.x_filename = str_cpy(filename)) == 0) 6315562Sedward goto bad; 6415562Sedward cx.x_fp = fopen(filename, "r"); 6515562Sedward if (cx.x_fp == 0) 6615562Sedward goto bad; 6756015Sedward (void) fcntl(fileno(cx.x_fp), F_SETFD, 1); 6815562Sedward cx.x_bol = 1; 6915562Sedward cx.x_lineno = 0; 7015562Sedward cx.x_errwin = 0; 7115855Sedward cx.x_noerr = 0; 7215562Sedward return 0; 7315562Sedward bad: 7415562Sedward if (cx.x_filename != 0) 7515562Sedward str_free(cx.x_filename); 7615562Sedward cx_free(); 7715562Sedward return -1; 7815562Sedward } 7915562Sedward cx_beginbuf(buf,arg,narg)8016448Sedwardcx_beginbuf(buf, arg, narg) 8115562Sedward char *buf; 8216448Sedward struct value *arg; 8316448Sedward int narg; 8415562Sedward { 8515562Sedward if (cx_alloc() < 0) 8615562Sedward return -1; 8715562Sedward cx.x_type = X_BUF; 8815562Sedward cx.x_bufp = cx.x_buf = buf; 8916448Sedward cx.x_arg = arg; 9016448Sedward cx.x_narg = narg; 9115562Sedward return 0; 9215562Sedward } 9315562Sedward cx_end()9415562Sedwardcx_end() 9515562Sedward { 9615562Sedward switch (cx.x_type) { 9715562Sedward case X_BUF: 9815562Sedward break; 9915562Sedward case X_FILE: 10015562Sedward (void) fclose(cx.x_fp); 10115562Sedward str_free(cx.x_filename); 10215562Sedward break; 10315562Sedward } 10415562Sedward cx_free(); 10515562Sedward } 106