00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <assert.h>
00034 #include "../builtin.h"
00035 #include "../list.h"
00036 #include "../mytypes.h"
00037 #include "../os/os.h"
00038 #include "../run.h"
00039 #include "../strtab.h"
00040
00041 #include "bi_task.h"
00042
00043 static void bi_task_exec(run_t *run);
00044
00049 void bi_task_declare(builtin_t *bi)
00050 {
00051 builtin_code_snippet(bi,
00052 "class Task is\n"
00053 "fun Exec(args : string[], packed), static, builtin;\n"
00054 "end\n");
00055 }
00056
00061 void bi_task_bind(builtin_t *bi)
00062 {
00063 builtin_fun_bind(bi, "Task", "Exec", bi_task_exec);
00064 }
00065
00070 static void bi_task_exec(run_t *run)
00071 {
00072 rdata_var_t *args;
00073 rdata_var_t *var;
00074 rdata_array_t *array;
00075 rdata_var_t *arg;
00076 int idx, dim;
00077 const char **cmd;
00078
00079 #ifdef DEBUG_RUN_TRACE
00080 printf("Called Task.Exec()\n");
00081 #endif
00082 args = run_local_vars_lookup(run, strtab_get_sid("args"));
00083
00084 assert(args);
00085 assert(args->vc == vc_ref);
00086
00087 var = args->u.ref_v->vref;
00088 assert(var->vc == vc_array);
00089
00090 array = var->u.array_v;
00091 assert(array->rank == 1);
00092 dim = array->extent[0];
00093
00094 if (dim == 0) {
00095 printf("Error: Builtin.Exec() expects at least one argument.\n");
00096 exit(1);
00097 }
00098
00099 cmd = calloc(dim + 1, sizeof(char *));
00100 if (cmd == NULL) {
00101 printf("Memory allocation failed.\n");
00102 exit(1);
00103 }
00104
00105 for (idx = 0; idx < dim; ++idx) {
00106 arg = array->element[idx];
00107 if (arg->vc != vc_string) {
00108 printf("Error: Argument to Builtin.Exec() must be "
00109 "string (found %d).\n", arg->vc);
00110 exit(1);
00111 }
00112
00113 cmd[idx] = arg->u.string_v->value;
00114 }
00115
00116 cmd[dim] = '\0';
00117
00118 if (os_exec((char * const *)cmd) != EOK) {
00119 printf("Error: Exec failed.\n");
00120 exit(1);
00121 }
00122 }