bi_task.c

00001 /*
00002  * Copyright (c) 2010 Jiri Svoboda
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7