Annotation of src/usr.bin/window/parser1.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: parser1.c,v 1.3 1995/09/28 10:34:31 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[] = "@(#)parser1.c 8.1 (Berkeley) 6/6/93";
! 42: #else
! 43: static char rcsid[] = "$NetBSD: parser1.c,v 1.3 1995/09/28 10:34:31 tls Exp $";
! 44: #endif
! 45: #endif /* not lint */
! 46:
! 47: #include "parser.h"
! 48:
! 49: p_start()
! 50: {
! 51: char flag = 1;
! 52:
! 53: (void) s_gettok();
! 54: for (;;) {
! 55: p_statementlist(flag);
! 56: if (token == T_EOF || p_abort())
! 57: break;
! 58: flag = 0;
! 59: p_synerror();
! 60: while (token != T_EOL && token != T_EOF) {
! 61: if (token == T_STR)
! 62: str_free(token_str);
! 63: (void) s_gettok();
! 64: }
! 65: if (token == T_EOL)
! 66: (void) s_gettok();
! 67: p_clearerr();
! 68: }
! 69: }
! 70:
! 71: p_statementlist(flag)
! 72: char flag;
! 73: {
! 74: for (; p_statement(flag) >= 0; p_clearerr())
! 75: ;
! 76: }
! 77:
! 78: p_statement(flag)
! 79: char flag;
! 80: {
! 81: switch (token) {
! 82: case T_EOL:
! 83: (void) s_gettok();
! 84: return 0;
! 85: case T_IF:
! 86: return p_if(flag);
! 87: default:
! 88: return p_expression(flag);
! 89: }
! 90: }
! 91:
! 92: p_if(flag)
! 93: char flag;
! 94: {
! 95: struct value t;
! 96: char true = 0;
! 97:
! 98: top:
! 99: (void) s_gettok();
! 100:
! 101: if (p_expr(&t, flag) < 0) {
! 102: p_synerror();
! 103: return -1;
! 104: }
! 105: switch (t.v_type) {
! 106: case V_NUM:
! 107: true = !true && t.v_num != 0;
! 108: break;
! 109: case V_STR:
! 110: p_error("if: Numeric value required.");
! 111: str_free(t.v_str);
! 112: case V_ERR:
! 113: flag = 0;
! 114: break;
! 115: }
! 116:
! 117: if (token != T_THEN) {
! 118: p_synerror();
! 119: return -1;
! 120: }
! 121:
! 122: (void) s_gettok();
! 123: p_statementlist(flag && true);
! 124: if (p_erred())
! 125: return -1;
! 126:
! 127: if (token == T_ELSIF)
! 128: goto top;
! 129:
! 130: if (token == T_ELSE) {
! 131: (void) s_gettok();
! 132: p_statementlist(flag && !true);
! 133: if (p_erred())
! 134: return -1;
! 135: }
! 136:
! 137: if (token == T_ENDIF) {
! 138: (void) s_gettok();
! 139: return 0;
! 140: }
! 141:
! 142: p_synerror();
! 143: return -1;
! 144: }
! 145:
! 146: p_expression(flag)
! 147: char flag;
! 148: {
! 149: struct value t;
! 150: char *cmd;
! 151: int p_function(), p_assign();
! 152:
! 153: switch (token) {
! 154: case T_NUM:
! 155: t.v_type = V_NUM;
! 156: t.v_num = token_num;
! 157: (void) s_gettok();
! 158: break;
! 159: case T_STR:
! 160: t.v_type = V_STR;
! 161: t.v_str = token_str;
! 162: (void) s_gettok();
! 163: break;
! 164: default:
! 165: if (p_expr(&t, flag) < 0)
! 166: return -1;
! 167: if (token == T_EOF) {
! 168: val_free(t);
! 169: return 0;
! 170: }
! 171: }
! 172: if (token != T_ASSIGN && p_convstr(&t) < 0)
! 173: return -1;
! 174: cmd = t.v_type == V_STR ? t.v_str : 0;
! 175: if ((*(token == T_ASSIGN ? p_assign : p_function))(cmd, &t, flag) < 0) {
! 176: if (cmd)
! 177: str_free(cmd);
! 178: return -1;
! 179: }
! 180: if (cmd)
! 181: str_free(cmd);
! 182: val_free(t);
! 183: if (token == T_EOL)
! 184: (void) s_gettok();
! 185: else if (token != T_EOF) {
! 186: p_synerror();
! 187: return -1;
! 188: }
! 189: return 0;
! 190: }
! 191:
! 192: p_convstr(v)
! 193: register struct value *v;
! 194: {
! 195: if (v->v_type != V_NUM)
! 196: return 0;
! 197: if ((v->v_str = str_itoa(v->v_num)) == 0) {
! 198: p_memerror();
! 199: v->v_type = V_ERR;
! 200: return -1;
! 201: }
! 202: v->v_type = V_STR;
! 203: return 0;
! 204: }
! 205:
! 206: p_synerror()
! 207: {
! 208: if (!cx.x_synerred) {
! 209: cx.x_synerred = cx.x_erred = 1;
! 210: error("Syntax error.");
! 211: }
! 212: }
! 213:
! 214: /*VARARGS1*/
! 215: p_error(msg, a, b, c)
! 216: char *msg;
! 217: {
! 218: if (!cx.x_erred) {
! 219: cx.x_erred = 1;
! 220: error(msg, a, b, c);
! 221: }
! 222: }
! 223:
! 224: p_memerror()
! 225: {
! 226: cx.x_erred = cx.x_abort = 1;
! 227: error("Out of memory.");
! 228: }