Annotation of src/usr.bin/window/parser4.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: parser4.c,v 1.5 1995/09/29 00:44:05 cgd 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[] = "@(#)parser4.c 8.1 (Berkeley) 6/6/93";
! 42: #else
! 43: static char rcsid[] = "$NetBSD: parser4.c,v 1.5 1995/09/29 00:44:05 cgd Exp $";
! 44: #endif
! 45: #endif /* not lint */
! 46:
! 47: #include "parser.h"
! 48: #include <string.h>
! 49:
! 50: /*
! 51: * | 3
! 52: * ^ 4
! 53: * & 5
! 54: * == != 6
! 55: * < <= > >= 7
! 56: * << >> 8
! 57: * + - 9
! 58: * * / % 10
! 59: */
! 60: p_expr3_10(level, v, flag)
! 61: register struct value *v;
! 62: char flag;
! 63: {
! 64: struct value l, r;
! 65: int op;
! 66: char *opname;
! 67:
! 68: if ((level == 10 ? p_expr11(v, flag)
! 69: : p_expr3_10(level + 1, v, flag)) < 0)
! 70: return -1;
! 71: for (;;) {
! 72: switch (level) {
! 73: case 3:
! 74: if (token != T_OR)
! 75: return 0;
! 76: opname = "|";
! 77: break;
! 78: case 4:
! 79: if (token != T_XOR)
! 80: return 0;
! 81: opname = "^";
! 82: break;
! 83: case 5:
! 84: if (token != T_AND)
! 85: return 0;
! 86: opname = "&";
! 87: break;
! 88: case 6:
! 89: if (token == T_EQ)
! 90: opname = "==";
! 91: else if (token == T_NE)
! 92: opname = "!=";
! 93: else
! 94: return 0;
! 95: break;
! 96: case 7:
! 97: switch (token) {
! 98: case T_LT:
! 99: opname = "<";
! 100: break;
! 101: case T_LE:
! 102: opname = "<=";
! 103: break;
! 104: case T_GT:
! 105: opname = ">";
! 106: break;
! 107: case T_GE:
! 108: opname = ">=";
! 109: break;
! 110: default:
! 111: return 0;
! 112: }
! 113: break;
! 114: case 8:
! 115: if (token == T_LS)
! 116: opname = "<<";
! 117: else if (token == T_RS)
! 118: opname = ">>";
! 119: else
! 120: return 0;
! 121: break;
! 122: case 9:
! 123: if (token == T_PLUS)
! 124: opname = "+";
! 125: else if (token == T_MINUS)
! 126: opname = "-";
! 127: else
! 128: return 0;
! 129: break;
! 130: case 10:
! 131: switch (token) {
! 132: case T_MUL:
! 133: opname = "*";
! 134: break;
! 135: case T_DIV:
! 136: opname = "/";
! 137: break;
! 138: case T_MOD:
! 139: opname = "%";
! 140: break;
! 141: default:
! 142: return 0;
! 143: }
! 144: break;
! 145: }
! 146: l = *v;
! 147: if (l.v_type == V_ERR)
! 148: flag = 0;
! 149:
! 150: op = token;
! 151: (void) s_gettok();
! 152: if ((level == 10 ? p_expr11(&r, flag)
! 153: : p_expr3_10(level + 1, &r, flag)) < 0) {
! 154: p_synerror();
! 155: val_free(l);
! 156: return -1;
! 157: }
! 158:
! 159: if (r.v_type == V_ERR)
! 160: flag = 0;
! 161: else switch (op) {
! 162: case T_EQ:
! 163: case T_NE:
! 164: case T_LT:
! 165: case T_LE:
! 166: case T_GT:
! 167: case T_GE:
! 168: case T_PLUS:
! 169: if (l.v_type == V_STR) {
! 170: if (r.v_type == V_NUM)
! 171: if (p_convstr(&r) < 0)
! 172: flag = 0;
! 173: } else
! 174: if (r.v_type == V_STR)
! 175: if (p_convstr(&l) < 0)
! 176: flag = 0;
! 177: break;
! 178: case T_LS:
! 179: case T_RS:
! 180: if (r.v_type == V_STR) {
! 181: char *p = r.v_str;
! 182: r.v_type = V_NUM;
! 183: r.v_num = strlen(p);
! 184: str_free(p);
! 185: }
! 186: break;
! 187: case T_OR:
! 188: case T_XOR:
! 189: case T_AND:
! 190: case T_MINUS:
! 191: case T_MUL:
! 192: case T_DIV:
! 193: case T_MOD:
! 194: default:
! 195: if (l.v_type == V_STR || r.v_type == V_STR) {
! 196: p_error("%s: Numeric operands required.",
! 197: opname);
! 198: flag = 0;
! 199: }
! 200: }
! 201: if (!flag) {
! 202: val_free(l);
! 203: val_free(r);
! 204: v->v_type = V_ERR;
! 205: if (p_abort())
! 206: return -1;
! 207: continue;
! 208: }
! 209:
! 210: v->v_type = V_NUM;
! 211: switch (op) {
! 212: case T_EQ:
! 213: case T_NE:
! 214: case T_LT:
! 215: case T_LE:
! 216: case T_GT:
! 217: case T_GE:
! 218: if (l.v_type == V_STR) {
! 219: int tmp = strcmp(l.v_str, r.v_str);
! 220: str_free(l.v_str);
! 221: str_free(r.v_str);
! 222: l.v_type = V_NUM;
! 223: l.v_num = tmp;
! 224: r.v_type = V_NUM;
! 225: r.v_num = 0;
! 226: }
! 227: break;
! 228: }
! 229: switch (op) {
! 230: case T_OR:
! 231: v->v_num = l.v_num | r.v_num;
! 232: break;
! 233: case T_XOR:
! 234: v->v_num = l.v_num ^ r.v_num;
! 235: break;
! 236: case T_AND:
! 237: v->v_num = l.v_num & r.v_num;
! 238: break;
! 239: case T_EQ:
! 240: v->v_num = l.v_num == r.v_num;
! 241: break;
! 242: case T_NE:
! 243: v->v_num = l.v_num != r.v_num;
! 244: break;
! 245: case T_LT:
! 246: v->v_num = l.v_num < r.v_num;
! 247: break;
! 248: case T_LE:
! 249: v->v_num = l.v_num <= r.v_num;
! 250: break;
! 251: case T_GT:
! 252: v->v_num = l.v_num > r.v_num;
! 253: break;
! 254: case T_GE:
! 255: v->v_num = l.v_num >= r.v_num;
! 256: break;
! 257: case T_LS:
! 258: if (l.v_type == V_STR) {
! 259: int i;
! 260: if ((i = strlen(l.v_str)) > r.v_num)
! 261: i = r.v_num;
! 262: v->v_str = str_ncpy(l.v_str, i);
! 263: v->v_type = V_STR;
! 264: } else
! 265: v->v_num = l.v_num << r.v_num;
! 266: break;
! 267: case T_RS:
! 268: if (l.v_type == V_STR) {
! 269: int i;
! 270: if ((i = strlen(l.v_str)) > r.v_num)
! 271: i -= r.v_num;
! 272: else
! 273: i = 0;
! 274: v->v_str = str_cpy(l.v_str + i);
! 275: v->v_type = V_STR;
! 276: } else
! 277: v->v_num = l.v_num >> r.v_num;
! 278: break;
! 279: case T_PLUS:
! 280: if (l.v_type == V_STR) {
! 281: v->v_str = str_cat(l.v_str, r.v_str);
! 282: v->v_type = V_STR;
! 283: } else
! 284: v->v_num = l.v_num + r.v_num;
! 285: break;
! 286: case T_MINUS:
! 287: v->v_num = l.v_num - r.v_num;
! 288: break;
! 289: case T_MUL:
! 290: v->v_num = l.v_num * r.v_num;
! 291: break;
! 292: case T_DIV:
! 293: v->v_num = l.v_num / r.v_num;
! 294: break;
! 295: case T_MOD:
! 296: v->v_num = l.v_num % r.v_num;
! 297: break;
! 298: }
! 299: val_free(l);
! 300: val_free(r);
! 301: }
! 302: /*NOTREACHED*/
! 303: }