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

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: 
                     45: boolean_t
                     46: db_term(valuep)
                     47:        db_expr_t *valuep;
                     48: {
                     49:        int     t;
                     50: 
                     51:        switch(t = db_read_token()) {
                     52:        case tIDENT:
                     53:            if (!db_value_of_name(db_tok_string, valuep)) {
                     54:                db_printf("Symbol \"%s\" not found\n", db_tok_string);
                     55:                db_error(0);
                     56:                /*NOTREACHED*/
                     57:            }
                     58:            return (TRUE);
                     59:        case tNUMBER:
                     60:            *valuep = db_tok_number;
                     61:            return (TRUE);
                     62:        case tDOT:
                     63:            *valuep = (db_expr_t)db_dot;
                     64:            return (TRUE);
                     65:        case tDOTDOT:
                     66:            *valuep = (db_expr_t)db_prev;
                     67:            return (TRUE);
                     68:        case tPLUS:
                     69:            *valuep = (db_expr_t) db_next;
                     70:            return (TRUE);
                     71:        case tQUOTE:
                     72:            *valuep = (db_expr_t)db_last_addr;
                     73:            return (TRUE);
                     74:        case tDOLLAR:
                     75:            if (!db_get_variable(valuep))
                     76:                return (FALSE);
                     77:            return (TRUE);
                     78:        case tLPAREN:
                     79:            if (!db_expression(valuep)) {
                     80:                db_error("Unmached ()s\n");
                     81:                /*NOTREACHED*/
                     82:            }
                     83:            t = db_read_token();
                     84:            if (t != tRPAREN) {
                     85:                db_printf("')' expected at \"%s...\"\n", db_tok_string);
                     86:                db_error(0);
                     87:                /*NOTREACHED*/
                     88:            }
                     89:            return (TRUE);
                     90:        default:
                     91:            db_unread_token(t);
                     92:            return (FALSE);
                     93:        }
                     94: }
                     95: 
                     96: int
                     97: db_size_option(modif, u_option, t_option)
                     98:        char *modif;
                     99:        boolean_t *u_option;
                    100:        boolean_t *t_option;
                    101: {
                    102:        register  char *p;
                    103:        int       size = sizeof(int);
                    104: 
                    105:        *u_option = FALSE;
                    106:        *t_option = FALSE;
                    107:        for (p = modif; *p; p++) {
                    108:            switch(*p) {
                    109:            case 'b':
                    110:                size = sizeof(char);
                    111:                break;
                    112:            case 'h':
                    113:                size = sizeof(short);
                    114:                break;
                    115:            case 'l':
                    116:                size = sizeof(long);
                    117:                break;
                    118:            case 'u':
                    119:                *u_option = TRUE;
                    120:                break;
                    121:            case 't':
                    122:                *t_option = TRUE;
                    123:                break;
                    124:            }
                    125:        }
                    126:        return(size);
                    127: }
                    128: 
                    129: boolean_t
                    130: db_unary(valuep)
                    131:        db_expr_t *valuep;
                    132: {
                    133:        int       t;
                    134:        int       size;
                    135:        boolean_t u_opt, t_opt;
                    136:        task_t    task;
                    137:        extern    task_t db_default_task;
                    138: 
                    139:        t = db_read_token();
                    140:        if (t == tMINUS) {
                    141:            if (!db_unary(valuep)) {
                    142:                db_error("Expression syntax error after '-'\n");
                    143:                /*NOTREACHED*/
                    144:            }
                    145:            *valuep = -*valuep;
                    146:            return (TRUE);
                    147:        }
                    148:        if (t == tSTAR) {
                    149:            /* indirection */
                    150:            if (!db_unary(valuep)) {
                    151:                db_error("Expression syntax error after '*'\n");
                    152:                /*NOTREACHED*/
                    153:            }
                    154:            task = TASK_NULL;
                    155:            size = sizeof(db_addr_t);
                    156:            u_opt = FALSE;
                    157:            t = db_read_token();
                    158:            if (t == tIDENT && db_tok_string[0] == ':') {
                    159:                size = db_size_option(&db_tok_string[1], &u_opt, &t_opt);
                    160:                if (t_opt)
                    161:                    task = db_default_task;
                    162:            } else
                    163:                db_unread_token(t);
                    164:            *valuep = db_get_task_value((db_addr_t)*valuep, size, !u_opt, task);
                    165:            return (TRUE);
                    166:        }
                    167:        if (t == tEXCL) {
                    168:            if (!db_unary(valuep)) {
                    169:                db_error("Expression syntax error after '!'\n");
                    170:                /*NOTREACHED*/
                    171:            }
                    172:            *valuep = (!(*valuep));
                    173:            return (TRUE);
                    174:        }
                    175:        db_unread_token(t);
                    176:        return (db_term(valuep));
                    177: }
                    178: 
                    179: boolean_t
                    180: db_mult_expr(valuep)
                    181:        db_expr_t *valuep;
                    182: {
                    183:        db_expr_t       lhs, rhs;
                    184:        int             t;
                    185:        char            c;
                    186: 
                    187:        if (!db_unary(&lhs))
                    188:            return (FALSE);
                    189: 
                    190:        t = db_read_token();
                    191:        while (t == tSTAR || t == tSLASH || t == tPCT || t == tHASH
                    192:                || t == tBIT_AND) {
                    193:            c = db_tok_string[0];
                    194:            if (!db_term(&rhs)) {
                    195:                db_printf("Expression syntax error after '%c'\n", c);
                    196:                db_error(0);
                    197:                /*NOTREACHED*/
                    198:            }
                    199:            switch(t) {
                    200:            case tSTAR:
                    201:                lhs *= rhs;
                    202:                break;
                    203:            case tBIT_AND:
                    204:                lhs &= rhs;
                    205:                break;
                    206:            default:
                    207:                if (rhs == 0) {
                    208:                    db_error("Divide by 0\n");
                    209:                    /*NOTREACHED*/
                    210:                }
                    211:                if (t == tSLASH)
                    212:                    lhs /= rhs;
                    213:                else if (t == tPCT)
                    214:                    lhs %= rhs;
                    215:                else
                    216:                    lhs = ((lhs+rhs-1)/rhs)*rhs;
                    217:            }
                    218:            t = db_read_token();
                    219:        }
                    220:        db_unread_token(t);
                    221:        *valuep = lhs;
                    222:        return (TRUE);
                    223: }
                    224: 
                    225: boolean_t
                    226: db_add_expr(valuep)
                    227:        db_expr_t *valuep;
                    228: {
                    229:        db_expr_t       lhs, rhs;
                    230:        int             t;
                    231:        char            c;
                    232: 
                    233:        if (!db_mult_expr(&lhs))
                    234:            return (FALSE);
                    235: 
                    236:        t = db_read_token();
                    237:        while (t == tPLUS || t == tMINUS || t == tBIT_OR) {
                    238:            c = db_tok_string[0];
                    239:            if (!db_mult_expr(&rhs)) {
                    240:                db_printf("Expression syntax error after '%c'\n", c);
                    241:                db_error(0);
                    242:                /*NOTREACHED*/
                    243:            }
                    244:            if (t == tPLUS)
                    245:                lhs += rhs;
                    246:            else if (t == tMINUS)
                    247:                lhs -= rhs;
                    248:            else
                    249:                lhs |= rhs;
                    250:            t = db_read_token();
                    251:        }
                    252:        db_unread_token(t);
                    253:        *valuep = lhs;
                    254:        return (TRUE);
                    255: }
                    256: 
                    257: boolean_t
                    258: db_shift_expr(valuep)
                    259:        db_expr_t *valuep;
                    260: {
                    261:        db_expr_t       lhs, rhs;
                    262:        int             t;
                    263: 
                    264:        if (!db_add_expr(&lhs))
                    265:            return (FALSE);
                    266: 
                    267:        t = db_read_token();
                    268:        while (t == tSHIFT_L || t == tSHIFT_R) {
                    269:            if (!db_add_expr(&rhs)) {
                    270:                db_printf("Expression syntax error after \"%s\"\n",
                    271:                        (t == tSHIFT_L)? "<<": ">>");
                    272:                db_error(0);
                    273:                /*NOTREACHED*/
                    274:            }
                    275:            if (rhs < 0) {
                    276:                db_error("Negative shift amount\n");
                    277:                /*NOTREACHED*/
                    278:            }
                    279:            if (t == tSHIFT_L)
                    280:                lhs <<= rhs;
                    281:            else {
                    282:                /* Shift right is unsigned */
                    283:                lhs = (natural_t) lhs >> rhs;
                    284:            }
                    285:            t = db_read_token();
                    286:        }
                    287:        db_unread_token(t);
                    288:        *valuep = lhs;
                    289:        return (TRUE);
                    290: }
                    291: 
                    292: boolean_t
                    293: db_logical_relation_expr(valuep)
                    294:        db_expr_t *valuep;
                    295: {
                    296:        db_expr_t       lhs, rhs;
                    297:        int             t;
                    298:        char            op[3];
                    299: 
                    300:        if (!db_shift_expr(&lhs))
                    301:            return(FALSE);
                    302: 
                    303:        t = db_read_token();
                    304:        while (t == tLOG_EQ || t == tLOG_NOT_EQ
                    305:                || t == tGREATER || t == tGREATER_EQ
                    306:                || t == tLESS || t == tLESS_EQ) {
                    307:            op[0] = db_tok_string[0];
                    308:            op[1] = db_tok_string[1];
                    309:            op[2] = 0;
                    310:            if (!db_shift_expr(&rhs)) {
                    311:                db_printf("Expression syntax error after \"%s\"\n", op);
                    312:                db_error(0);
                    313:                /*NOTREACHED*/
                    314:            }
                    315:            switch(t) {
                    316:            case tLOG_EQ:
                    317:                lhs = (lhs == rhs);
                    318:                break;
                    319:            case tLOG_NOT_EQ:
                    320:                lhs = (lhs != rhs);
                    321:                break;
                    322:            case tGREATER:
                    323:                lhs = (lhs > rhs);
                    324:                break;
                    325:            case tGREATER_EQ:
                    326:                lhs = (lhs >= rhs);
                    327:                break;
                    328:            case tLESS:
                    329:                lhs = (lhs < rhs);
                    330:                break;
                    331:            case tLESS_EQ:
                    332:                lhs = (lhs <= rhs);
                    333:                break;
                    334:            }
                    335:            t = db_read_token();
                    336:        }
                    337:        db_unread_token(t);
                    338:        *valuep = lhs;
                    339:        return (TRUE);
                    340: }
                    341: 
                    342: boolean_t
                    343: db_logical_and_expr(valuep)
                    344:        db_expr_t *valuep;
                    345: {
                    346:        db_expr_t       lhs, rhs;
                    347:        int             t;
                    348: 
                    349:        if (!db_logical_relation_expr(&lhs))
                    350:            return(FALSE);
                    351: 
                    352:        t = db_read_token();
                    353:        while (t == tLOG_AND) {
                    354:            if (!db_logical_relation_expr(&rhs)) {
                    355:                db_error("Expression syntax error after \"&&\"\n");
                    356:                /*NOTREACHED*/
                    357:            }
                    358:            lhs = (lhs && rhs);
                    359:        }
                    360:        db_unread_token(t);
                    361:        *valuep = lhs;
                    362:        return (TRUE);
                    363: }
                    364: 
                    365: boolean_t
                    366: db_logical_or_expr(valuep)
                    367:        db_expr_t *valuep;
                    368: {
                    369:        db_expr_t       lhs, rhs;
                    370:        int             t;
                    371: 
                    372:        if (!db_logical_and_expr(&lhs))
                    373:            return(FALSE);
                    374: 
                    375:        t = db_read_token();
                    376:        while (t == tLOG_OR) {
                    377:            if (!db_logical_and_expr(&rhs)) {
                    378:                db_error("Expression syntax error after \"||\"\n");
                    379:                /*NOTREACHED*/
                    380:            }
                    381:            lhs = (lhs || rhs);
                    382:        }
                    383:        db_unread_token(t);
                    384:        *valuep = lhs;
                    385:        return (TRUE);
                    386: }
                    387: 
                    388: int
                    389: db_expression(valuep)
                    390:        db_expr_t *valuep;
                    391: {
                    392:        return (db_logical_or_expr(valuep));
                    393: }
                    394: 
1.1.1.2   root      395: #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.