Changeset 94d484a in mainline for uspace/app/sbi/src/builtin.c
- Timestamp:
- 2010-03-07T17:45:33Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- d0febca
- Parents:
- fa36f29
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/sbi/src/builtin.c
rfa36f29 r94d484a 34 34 #include "list.h" 35 35 #include "mytypes.h" 36 #include "os/os.h" 36 37 #include "run.h" 37 38 #include "stree.h" 38 39 #include "strtab.h" 40 #include "symbol.h" 39 41 40 42 #include "builtin.h" 41 43 44 static stree_symbol_t *builtin_declare_fun(stree_csi_t *csi, char *name); 45 static void builtin_fun_add_arg(stree_symbol_t *fun_sym, char *name); 46 static void builtin_fun_add_vararg(stree_symbol_t *fun_sym, char *name); 47 42 48 static void builtin_write_line(run_t *run); 49 static void builtin_exec(run_t *run); 43 50 44 51 static stree_symbol_t *bi_write_line; 52 static stree_symbol_t *bi_exec; 45 53 46 54 /** Declare builtin symbols in the program. … … 53 61 stree_csi_t *csi; 54 62 stree_ident_t *ident; 55 stree_csimbr_t *csimbr;56 stree_fun_t *fun;57 stree_fun_arg_t *fun_arg;58 63 stree_symbol_t *symbol; 59 64 … … 76 81 77 82 /* 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"); 104 89 } 105 90 … … 111 96 if (fun_sym == bi_write_line) { 112 97 builtin_write_line(run); 98 } else if (fun_sym == bi_exec) { 99 builtin_exec(run); 113 100 } else { 114 101 assert(b_false); … … 116 103 } 117 104 105 /** Declare a builtin function in @a csi. */ 106 static 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. */ 135 static 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. */ 152 static 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 118 168 static void builtin_write_line(run_t *run) 119 169 { … … 121 171 122 172 #ifdef DEBUG_RUN_TRACE 123 printf("Called Builtin. writeLine()\n");173 printf("Called Builtin.WriteLine()\n"); 124 174 #endif 125 175 var = run_local_vars_lookup(run, strtab_get_sid("arg")); … … 138 188 } 139 189 } 190 191 /** Start an executable and wait for it to finish. */ 192 static 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.