Changeset 94d484a in mainline for uspace/app/sbi/src/builtin.c


Ignore:
Timestamp:
2010-03-07T17:45:33Z (14 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d0febca
Parents:
fa36f29
Message:

Update SBI to rev. 90.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/sbi/src/builtin.c

    rfa36f29 r94d484a  
    3434#include "list.h"
    3535#include "mytypes.h"
     36#include "os/os.h"
    3637#include "run.h"
    3738#include "stree.h"
    3839#include "strtab.h"
     40#include "symbol.h"
    3941
    4042#include "builtin.h"
    4143
     44static stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, char *name);
     45static void builtin_fun_add_arg(stree_symbol_t *fun_sym, char *name);
     46static void builtin_fun_add_vararg(stree_symbol_t *fun_sym, char *name);
     47
    4248static void builtin_write_line(run_t *run);
     49static void builtin_exec(run_t *run);
    4350
    4451static stree_symbol_t *bi_write_line;
     52static stree_symbol_t *bi_exec;
    4553
    4654/** Declare builtin symbols in the program.
     
    5361        stree_csi_t *csi;
    5462        stree_ident_t *ident;
    55         stree_csimbr_t *csimbr;
    56         stree_fun_t *fun;
    57         stree_fun_arg_t *fun_arg;
    5863        stree_symbol_t *symbol;
    5964
     
    7681
    7782        /* Declare builtin functions. */
    78         ident = stree_ident_new();
    79         ident->sid = strtab_get_sid("WriteLine");
    80 
    81         fun = stree_fun_new();
    82         fun->name = ident;
    83         fun->body = NULL;
    84         list_init(&fun->args);
    85 
    86         csimbr = stree_csimbr_new(csimbr_fun);
    87         csimbr->u.fun = fun;
    88 
    89         symbol = stree_symbol_new(sc_fun);
    90         symbol->u.fun = fun;
    91         symbol->outer_csi = csi;
    92         fun->symbol = symbol;
    93 
    94         list_append(&csi->members, csimbr);
    95 
    96         fun_arg = stree_fun_arg_new();
    97         fun_arg->name = stree_ident_new();
    98         fun_arg->name->sid = strtab_get_sid("arg");
    99         fun_arg->type = NULL; /* XXX */
    100 
    101         list_append(&fun->args, fun_arg);
    102 
    103         bi_write_line = symbol;
     83
     84        bi_write_line = builtin_declare_fun(csi, "WriteLine");
     85        builtin_fun_add_arg(bi_write_line, "arg");
     86
     87        bi_exec = builtin_declare_fun(csi, "Exec");
     88        builtin_fun_add_vararg(bi_exec, "args");
    10489}
    10590
     
    11196        if (fun_sym == bi_write_line) {
    11297                builtin_write_line(run);
     98        } else if (fun_sym == bi_exec) {
     99                builtin_exec(run);
    113100        } else {
    114101                assert(b_false);
     
    116103}
    117104
     105/** Declare a builtin function in @a csi. */
     106static stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, char *name)
     107{
     108        stree_ident_t *ident;
     109        stree_fun_t *fun;
     110        stree_csimbr_t *csimbr;
     111        stree_symbol_t *symbol;
     112
     113        ident = stree_ident_new();
     114        ident->sid = strtab_get_sid(name);
     115
     116        fun = stree_fun_new();
     117        fun->name = ident;
     118        fun->body = NULL;
     119        list_init(&fun->args);
     120
     121        csimbr = stree_csimbr_new(csimbr_fun);
     122        csimbr->u.fun = fun;
     123
     124        symbol = stree_symbol_new(sc_fun);
     125        symbol->u.fun = fun;
     126        symbol->outer_csi = csi;
     127        fun->symbol = symbol;
     128
     129        list_append(&csi->members, csimbr);
     130
     131        return symbol;
     132}
     133
     134/** Add one formal parameter to function. */
     135static void builtin_fun_add_arg(stree_symbol_t *fun_sym, char *name)
     136{
     137        stree_fun_arg_t *fun_arg;
     138        stree_fun_t *fun;
     139
     140        fun = symbol_to_fun(fun_sym);
     141        assert(fun != NULL);
     142
     143        fun_arg = stree_fun_arg_new();
     144        fun_arg->name = stree_ident_new();
     145        fun_arg->name->sid = strtab_get_sid(name);
     146        fun_arg->type = NULL; /* XXX */
     147
     148        list_append(&fun->args, fun_arg);
     149}
     150
     151/** Add variadic formal parameter to function. */
     152static void builtin_fun_add_vararg(stree_symbol_t *fun_sym, char *name)
     153{
     154        stree_fun_arg_t *fun_arg;
     155        stree_fun_t *fun;
     156
     157        fun = symbol_to_fun(fun_sym);
     158        assert(fun != NULL);
     159
     160        fun_arg = stree_fun_arg_new();
     161        fun_arg->name = stree_ident_new();
     162        fun_arg->name->sid = strtab_get_sid(name);
     163        fun_arg->type = NULL; /* XXX */
     164
     165        fun->varg = fun_arg;
     166}
     167
    118168static void builtin_write_line(run_t *run)
    119169{
     
    121171
    122172#ifdef DEBUG_RUN_TRACE
    123         printf("Called Builtin.writeLine()\n");
     173        printf("Called Builtin.WriteLine()\n");
    124174#endif
    125175        var = run_local_vars_lookup(run, strtab_get_sid("arg"));
     
    138188        }
    139189}
     190
     191/** Start an executable and wait for it to finish. */
     192static void builtin_exec(run_t *run)
     193{
     194        rdata_var_t *args;
     195        rdata_var_t *var;
     196        rdata_array_t *array;
     197        rdata_var_t *arg;
     198        int idx, dim;
     199        char **cmd;
     200
     201#ifdef DEBUG_RUN_TRACE
     202        printf("Called Builtin.Exec()\n");
     203#endif
     204        args = run_local_vars_lookup(run, strtab_get_sid("args"));
     205
     206        assert(args);
     207        assert(args->vc == vc_ref);
     208
     209        var = args->u.ref_v->vref;
     210        assert(var->vc == vc_array);
     211
     212        array = var->u.array_v;
     213        assert(array->rank == 1);
     214        dim = array->extent[0];
     215
     216        if (dim == 0) {
     217                printf("Error: Builtin.Exec() expects at least one argument.\n");
     218                exit(1);
     219        }
     220
     221        cmd = calloc(dim + 1, sizeof(char *));
     222        if (cmd == NULL) {
     223                printf("Memory allocation failed.\n");
     224                exit(1);
     225        }
     226
     227        for (idx = 0; idx < dim; ++idx) {
     228                arg = array->element[idx];
     229                if (arg->vc != vc_string) {
     230                        printf("Error: Argument to Builtin.Exec() must be "
     231                            "string (found %d).\n", arg->vc);
     232                        exit(1);
     233                }
     234
     235                cmd[idx] = arg->u.string_v->value;
     236        }
     237
     238        cmd[dim] = '\0';
     239
     240        if (os_exec(cmd) != EOK) {
     241                printf("Error: Exec failed.\n");
     242                exit(1);
     243        }
     244}
Note: See TracChangeset for help on using the changeset viewer.