[BACK]Return to context.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/context.c, Revision 1.1.1.1

1.1       deraadt     1: /*     $NetBSD: context.c,v 1.3 1995/09/28 10:34:15 tls Exp $  */
                      2:
                      3: /*
                      4:  * Copyright (c) 1983, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to Berkeley by
                      8:  * Edward Wang at The University of California, Berkeley.
                      9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "@(#)context.c  8.1 (Berkeley) 6/6/93";
                     42: #else
                     43: static char rcsid[] = "$NetBSD: context.c,v 1.3 1995/09/28 10:34:15 tls Exp $";
                     44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include "value.h"
                     48: #include "string.h"
                     49: #include "context.h"
                     50: #include <fcntl.h>
                     51:
                     52: /*
                     53:  * Context push/pop for nested command files.
                     54:  */
                     55:
                     56: char *malloc();
                     57:
                     58: cx_alloc()
                     59: {
                     60:        register struct context *xp;
                     61:
                     62:        if (cx.x_type != 0) {
                     63:                xp = (struct context *)
                     64:                        malloc((unsigned) sizeof (struct context));
                     65:                if (xp == 0)
                     66:                        return -1;
                     67:                *xp = cx;
                     68:                cx.x_link = xp;
                     69:                cx.x_type = 0;
                     70:        }
                     71:        cx.x_erred = 0;
                     72:        cx.x_synerred = 0;
                     73:        cx.x_abort = 0;
                     74:        return 0;
                     75: }
                     76:
                     77: cx_free()
                     78: {
                     79:        struct context *xp;
                     80:
                     81:        if ((xp = cx.x_link) != 0) {
                     82:                cx = *xp;
                     83:                free((char *)xp);
                     84:        } else
                     85:                cx.x_type = 0;
                     86: }
                     87:
                     88: cx_beginfile(filename)
                     89: char *filename;
                     90: {
                     91:        if (cx_alloc() < 0)
                     92:                return -1;
                     93:        cx.x_type = X_FILE;
                     94:        if ((cx.x_filename = str_cpy(filename)) == 0)
                     95:                goto bad;
                     96:        cx.x_fp = fopen(filename, "r");
                     97:        if (cx.x_fp == 0)
                     98:                goto bad;
                     99:        (void) fcntl(fileno(cx.x_fp), F_SETFD, 1);
                    100:        cx.x_bol = 1;
                    101:        cx.x_lineno = 0;
                    102:        cx.x_errwin = 0;
                    103:        cx.x_noerr = 0;
                    104:        return 0;
                    105: bad:
                    106:        if (cx.x_filename != 0)
                    107:                str_free(cx.x_filename);
                    108:        cx_free();
                    109:        return -1;
                    110: }
                    111:
                    112: cx_beginbuf(buf, arg, narg)
                    113: char *buf;
                    114: struct value *arg;
                    115: int narg;
                    116: {
                    117:        if (cx_alloc() < 0)
                    118:                return -1;
                    119:        cx.x_type = X_BUF;
                    120:        cx.x_bufp = cx.x_buf = buf;
                    121:        cx.x_arg = arg;
                    122:        cx.x_narg = narg;
                    123:        return 0;
                    124: }
                    125:
                    126: cx_end()
                    127: {
                    128:        switch (cx.x_type) {
                    129:        case X_BUF:
                    130:                break;
                    131:        case X_FILE:
                    132:                (void) fclose(cx.x_fp);
                    133:                str_free(cx.x_filename);
                    134:                break;
                    135:        }
                    136:        cx_free();
                    137: }