Annotation of Gnu-Mach/ddb/db_expr.c, revision 1.1.1.4

1.1.1.2   root        1: /*
1.1       root        2:  * Mach Operating System
                      3:  * Copyright (c) 1991,1990 Carnegie Mellon University
                      4:  * All Rights Reserved.
1.1.1.2   root        5:  *
1.1       root        6:  * Permission to use, copy, modify and distribute this software and its
                      7:  * documentation is hereby granted, provided that both the copyright
                      8:  * notice and this permission notice appear in all copies of the
                      9:  * software, derivative works or modified versions, and any portions
                     10:  * thereof, and that both notices appear in supporting documentation.
1.1.1.2   root       11:  *
1.1       root       12:  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
                     13:  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
                     14:  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
1.1.1.2   root       15:  *
1.1       root       16:  * Carnegie Mellon requests users of this software to return to
1.1.1.2   root       17:  *
1.1       root       18:  *  Software Distribution Coordinator  or  [email protected]
                     19:  *  School of Computer Science
                     20:  *  Carnegie Mellon University
                     21:  *  Pittsburgh PA 15213-3890
1.1.1.2   root       22:  *
1.1       root       23:  * any improvements or extensions that they make and grant Carnegie Mellon
                     24:  * the rights to redistribute these changes.
                     25:  */
                     26: /*
                     27:  *     Author: David B. Golub, Carnegie Mellon University
                     28:  *     Date:   7/90
                     29:  */
1.1.1.3   root       30: 
1.1       root       31: #if MACH_KDB
                     32: 
                     33: #include <mach/boolean.h>
                     34: #include <machine/db_machdep.h>
                     35: #include <ddb/db_lex.h>
                     36: #include <ddb/db_access.h>
                     37: #include <ddb/db_command.h>
1.1.1.3   root       38: #include <ddb/db_expr.h>
                     39: #include <ddb/db_output.h>
                     40: #include <ddb/db_sym.h>
                     41: #include <ddb/db_variables.h>
1.1       root       42: #include <kern/task.h>
                     43: 
                     44: boolean_t
1.1.1.4 ! root       45: db_term(db_expr_t *valuep)
1.1       root       46: {
                     47:        int     t;
                     48: 
                     49:        switch(t = db_read_token()) {
                     50:        case tIDENT:
                     51:            if (!db_value_of_name(db_tok_string, valuep)) {
                     52:                db_printf("Symbol \"%s\" not found\n", db_tok_string);
                     53:                db_error(0);
                     54:                /*NOTREACHED*/
                     55:            }
                     56:            return (TRUE);
                     57:        case tNUMBER:
                     58:            *valuep = db_tok_number;
                     59:            return (TRUE);
                     60:        case tDOT:
                     61:            *valuep = (db_expr_t)db_dot;
                     62:            return (TRUE);
                     63:        case tDOTDOT:
                     64:            *valuep = (db_expr_t)db_prev;
                     65:            return (TRUE);
                     66:        case tPLUS:
                     67:            *valuep = (db_expr_t) db_next;
                     68:            return (TRUE);
                     69:        case tQUOTE:
                     70:            *valuep = (db_expr_t)db_last_addr;
                     71:            return (TRUE);
                     72:        case tDOLLAR:
                     73:            if (!db_get_variable(valuep))
                     74:                return (FALSE);
                     75:            return (TRUE);
                     76:        case tLPAREN:
                     77:            if (!db_expression(valuep)) {
                     78:                db_error("Unmached ()s\n");
                     79:                /*NOTREACHED*/
                     80:            }
                     81:            t = db_read_token();
                     82:            if (t != tRPAREN) {
                     83:                db_printf("')' expected at \"%s...\"\n", db_tok_string);
                     84:                db_error(0);
                     85:                /*NOTREACHED*/
                     86:            }
                     87:            return (TRUE);
                     88:        default:
                     89:            db_unread_token(t);
                     90:            return (FALSE);
                     91:        }
                     92: }
                     93: 
                     94: int
                     95: db_size_option(modif, u_option, t_option)
1.1.1.4 ! root       96:        const char *modif;
1.1       root       97:        boolean_t *u_option;
                     98:        boolean_t *t_option;
                     99: {
1.1.1.4 ! root      100:        const char      *p;
        !           101:        int             size = sizeof(int);
1.1       root      102: 
                    103:        *u_option = FALSE;
                    104:        *t_option = FALSE;
                    105:        for (p = modif; *p; p++) {
                    106:            switch(*p) {
                    107:            case 'b':
                    108:                size = sizeof(char);
                    109:                break;
                    110:            case 'h':
                    111:                size = sizeof(short);
                    112:                break;
                    113:            case 'l':
                    114:                size = sizeof(long);
                    115:                break;
                    116:            case 'u':
                    117:                *u_option = TRUE;
                    118:                break;
                    119:            case 't':
                    120:                *t_option = TRUE;
                    121:                break;
                    122:            }
                    123:        }
                    124:        return(size);
                    125: }
                    126: 
                    127: boolean_t
1.1.1.4 ! root      128: db_unary(db_expr_t *valuep)
1.1       root      129: {
                    130:        int       t;
                    131:        int       size;
                    132:        boolean_t u_opt, t_opt;
                    133:        task_t    task;
                    134:        extern    task_t db_default_task;
                    135: 
                    136:        t = db_read_token();
                    137:        if (t == tMINUS) {
                    138:            if (!db_unary(valuep)) {
                    139:                db_error("Expression syntax error after '-'\n");
                    140:                /*NOTREACHED*/
                    141:            }
                    142:            *valuep = -*valuep;
                    143:            return (TRUE);
                    144:        }
                    145:        if (t == tSTAR) {
                    146:            /* indirection */
                    147:            if (!db_unary(valuep)) {
                    148:                db_error("Expression syntax error after '*'\n");
                    149:                /*NOTREACHED*/
                    150:            }
                    151:            task = TASK_NULL;
                    152:            size = sizeof(db_addr_t);
                    153:            u_opt = FALSE;
                    154:            t = db_read_token();
                    155:            if (t == tIDENT && db_tok_string[0] == ':') {
                    156:                size = db_size_option(&db_tok_string[1], &u_opt, &t_opt);
                    157:                if (t_opt)
                    158:                    task = db_default_task;
                    159:            } else
                    160:                db_unread_token(t);
                    161:            *valuep = db_get_task_value((db_addr_t)*valuep, size, !u_opt, task);
                    162:            return (TRUE);
                    163:        }
                    164:        if (t == tEXCL) {
                    165:            if (!db_unary(valuep)) {
                    166:                db_error("Expression syntax error after '!'\n");
                    167:                /*NOTREACHED*/
                    168:            }
                    169:            *valuep = (!(*valuep));
                    170:            return (TRUE);
                    171:        }
                    172:        db_unread_token(t);
                    173:        return (db_term(valuep));
                    174: }
                    175: 
                    176: boolean_t
1.1.1.4 ! root      177: db_mult_expr(db_expr_t *valuep)
1.1       root      178: {
1.1.1.4 ! root      179:        db_expr_t       lhs = 0, rhs;
1.1       root      180:        int             t;
                    181:        char            c;
                    182: 
                    183:        if (!db_unary(&lhs))
                    184:            return (FALSE);
                    185: 
                    186:        t = db_read_token();
                    187:        while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH
                    188:                || t == tBIT_AND) {
                    189:            c = db_tok_string[0];
                    190:            if (!db_term(&rhs)) {
                    191:                db_printf("Expression syntax error after '%c'\n", c);
                    192:                db_error(0);
                    193:                /*NOTREACHED*/
                    194:            }
                    195:            switch(t) {
                    196:            case tSTAR:
                    197:                lhs *= rhs;
                    198:                break;
                    199:            case tBIT_AND:
                    200:                lhs &= rhs;
                    201:                break;
                    202:            default:
                    203:                if (rhs == 0) {
                    204:                    db_error("Divide by 0\n");
                    205:                    /*NOTREACHED*/
                    206:                }
                    207:                if (t == tSLASH)
                    208:                    lhs /= rhs;
                    209:                else if (t == tPCT)
                    210:                    lhs %= rhs;
                    211:                else
                    212:                    lhs = ((lhs+rhs-1)/rhs)*rhs;
                    213:            }
                    214:            t = db_read_token();
                    215:        }
                    216:        db_unread_token(t);
                    217:        *valuep = lhs;
                    218:        return (TRUE);
                    219: }
                    220: 
                    221: boolean_t
1.1.1.4 ! root      222: db_add_expr(db_expr_t *valuep)
1.1       root      223: {
                    224:        db_expr_t       lhs, rhs;
                    225:        int             t;
                    226:        char            c;
                    227: 
                    228:        if (!db_mult_expr(&lhs))
                    229:            return (FALSE);
                    230: 
                    231:        t = db_read_token();
                    232:        while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
                    233:            c = db_tok_string[0];
                    234:            if (!db_mult_expr(&rhs)) {
                    235:                db_printf("Expression syntax error after '%c'\n", c);
                    236:                db_error(0);
                    237:                /*NOTREACHED*/
                    238:            }
                    239:            if (t == tPLUS)
                    240:                lhs += rhs;
                    241:            else if (t == tMINUS)
                    242:                lhs -= rhs;
                    243:            else
                    244:                lhs |= rhs;
                    245:            t = db_read_token();
                    246:        }
                    247:        db_unread_token(t);
                    248:        *valuep = lhs;
                    249:        return (TRUE);
                    250: }
                    251: 
                    252: boolean_t
1.1.1.4 ! root      253: db_shift_expr(db_expr_t *valuep)
1.1       root      254: {
                    255:        db_expr_t       lhs, rhs;
                    256:        int             t;
                    257: 
                    258:        if (!db_add_expr(&lhs))
                    259:            return (FALSE);
                    260: 
                    261:        t = db_read_token();
                    262:        while (t == tSHIFT_L || t == tSHIFT_R) {
                    263:            if (!db_add_expr(&rhs)) {
                    264:                db_printf("Expression syntax error after \"%s\"\n",
                    265:                        (t == tSHIFT_L)? "<<": ">>");
                    266:                db_error(0);
                    267:                /*NOTREACHED*/
                    268:            }
                    269:            if (rhs < 0) {
                    270:                db_error("Negative shift amount\n");
                    271:                /*NOTREACHED*/
                    272:            }
                    273:            if (t == tSHIFT_L)
                    274:                lhs <<= rhs;
                    275:            else {
                    276:                /* Shift right is unsigned */
                    277:                lhs = (natural_t) lhs >> rhs;
                    278:            }
                    279:            t = db_read_token();
                    280:        }
                    281:        db_unread_token(t);
                    282:        *valuep = lhs;
                    283:        return (TRUE);
                    284: }
                    285: 
                    286: boolean_t
1.1.1.4 ! root      287: db_logical_relation_expr(db_expr_t *valuep)
1.1       root      288: {
                    289:        db_expr_t       lhs, rhs;
                    290:        int             t;
                    291:        char            op[3];
                    292: 
                    293:        if (!db_shift_expr(&lhs))
                    294:            return(FALSE);
                    295: 
                    296:        t = db_read_token();
                    297:        while (t == tLOG_EQ || t == tLOG_NOT_EQ
                    298:                || t == tGREATER || t == tGREATER_EQ
                    299:                || t == tLESS || t == tLESS_EQ) {
                    300:            op[0] = db_tok_string[0];
                    301:            op[1] = db_tok_string[1];
                    302:            op[2] = 0;
                    303:            if (!db_shift_expr(&rhs)) {
                    304:                db_printf("Expression syntax error after \"%s\"\n", op);
                    305:                db_error(0);
                    306:                /*NOTREACHED*/
                    307:            }
                    308:            switch(t) {
                    309:            case tLOG_EQ:
                    310:                lhs = (lhs == rhs);
                    311:                break;
                    312:            case tLOG_NOT_EQ:
                    313:                lhs = (lhs != rhs);
                    314:                break;
                    315:            case tGREATER:
                    316:                lhs = (lhs > rhs);
                    317:                break;
                    318:            case tGREATER_EQ:
                    319:                lhs = (lhs >= rhs);
                    320:                break;
                    321:            case tLESS:
                    322:                lhs = (lhs < rhs);
                    323:                break;
                    324:            case tLESS_EQ:
                    325:                lhs = (lhs <= rhs);
                    326:                break;
                    327:            }
                    328:            t = db_read_token();
                    329:        }
                    330:        db_unread_token(t);
                    331:        *valuep = lhs;
                    332:        return (TRUE);
                    333: }
                    334: 
                    335: boolean_t
1.1.1.4 ! root      336: db_logical_and_expr(db_expr_t *valuep)
1.1       root      337: {
                    338:        db_expr_t       lhs, rhs;
                    339:        int             t;
                    340: 
                    341:        if (!db_logical_relation_expr(&lhs))
                    342:            return(FALSE);
                    343: 
                    344:        t = db_read_token();
                    345:        while (t == tLOG_AND) {
                    346:            if (!db_logical_relation_expr(&rhs)) {
                    347:                db_error("Expression syntax error after \"&&\"\n");
                    348:                /*NOTREACHED*/
                    349:            }
                    350:            lhs = (lhs && rhs);
                    351:        }
                    352:        db_unread_token(t);
                    353:        *valuep = lhs;
                    354:        return (TRUE);
                    355: }
                    356: 
                    357: boolean_t
1.1.1.4 ! root      358: db_logical_or_expr(db_expr_t *valuep)
1.1       root      359: {
                    360:        db_expr_t       lhs, rhs;
                    361:        int             t;
                    362: 
                    363:        if (!db_logical_and_expr(&lhs))
                    364:            return(FALSE);
                    365: 
                    366:        t = db_read_token();
                    367:        while (t == tLOG_OR) {
                    368:            if (!db_logical_and_expr(&rhs)) {
                    369:                db_error("Expression syntax error after \"||\"\n");
                    370:                /*NOTREACHED*/
                    371:            }
                    372:            lhs = (lhs || rhs);
                    373:        }
                    374:        db_unread_token(t);
                    375:        *valuep = lhs;
                    376:        return (TRUE);
                    377: }
                    378: 
                    379: int
1.1.1.4 ! root      380: db_expression(db_expr_t *valuep)
1.1       root      381: {
                    382:        return (db_logical_or_expr(valuep));
                    383: }
                    384: 
1.1.1.2   root      385: #endif /* MACH_KDB */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.