1 | /*
|
---|
2 | * Copyright (c) 2010 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @file Task builtin binding. */
|
---|
30 |
|
---|
31 | #include <stdio.h>
|
---|
32 | #include <stdlib.h>
|
---|
33 | #include <assert.h>
|
---|
34 | #include "../builtin.h"
|
---|
35 | #include "../list.h"
|
---|
36 | #include "../mytypes.h"
|
---|
37 | #include "../os/os.h"
|
---|
38 | #include "../run.h"
|
---|
39 | #include "../strtab.h"
|
---|
40 |
|
---|
41 | #include "bi_task.h"
|
---|
42 |
|
---|
43 | static void bi_task_exec(run_t *run);
|
---|
44 |
|
---|
45 | /** Declare task builtin.
|
---|
46 | *
|
---|
47 | * @param bi Builtin object
|
---|
48 | */
|
---|
49 | void bi_task_declare(builtin_t *bi)
|
---|
50 | {
|
---|
51 | builtin_code_snippet(bi,
|
---|
52 | "class Task is\n"
|
---|
53 | "fun Exec(args : string[], packed), static, builtin;\n"
|
---|
54 | "end\n");
|
---|
55 | }
|
---|
56 |
|
---|
57 | /** Bind builtin functions.
|
---|
58 | *
|
---|
59 | * @param bi Builtin object
|
---|
60 | */
|
---|
61 | void bi_task_bind(builtin_t *bi)
|
---|
62 | {
|
---|
63 | builtin_fun_bind(bi, "Task", "Exec", bi_task_exec);
|
---|
64 | }
|
---|
65 |
|
---|
66 | /** Start an executable and wait for it to finish.
|
---|
67 | *
|
---|
68 | * @param run Runner object
|
---|
69 | */
|
---|
70 | static void bi_task_exec(run_t *run)
|
---|
71 | {
|
---|
72 | rdata_var_t *args;
|
---|
73 | rdata_var_t *var;
|
---|
74 | rdata_array_t *array;
|
---|
75 | rdata_var_t *arg;
|
---|
76 | int idx, dim;
|
---|
77 | const char **cmd;
|
---|
78 |
|
---|
79 | #ifdef DEBUG_RUN_TRACE
|
---|
80 | printf("Called Task.Exec()\n");
|
---|
81 | #endif
|
---|
82 | args = run_local_vars_lookup(run, strtab_get_sid("args"));
|
---|
83 |
|
---|
84 | assert(args);
|
---|
85 | assert(args->vc == vc_ref);
|
---|
86 |
|
---|
87 | var = args->u.ref_v->vref;
|
---|
88 | assert(var->vc == vc_array);
|
---|
89 |
|
---|
90 | array = var->u.array_v;
|
---|
91 | assert(array->rank == 1);
|
---|
92 | dim = array->extent[0];
|
---|
93 |
|
---|
94 | if (dim == 0) {
|
---|
95 | printf("Error: Builtin.Exec() expects at least one argument.\n");
|
---|
96 | exit(1);
|
---|
97 | }
|
---|
98 |
|
---|
99 | cmd = calloc(dim + 1, sizeof(char *));
|
---|
100 | if (cmd == NULL) {
|
---|
101 | printf("Memory allocation failed.\n");
|
---|
102 | exit(1);
|
---|
103 | }
|
---|
104 |
|
---|
105 | for (idx = 0; idx < dim; ++idx) {
|
---|
106 | arg = array->element[idx];
|
---|
107 | if (arg->vc != vc_string) {
|
---|
108 | printf("Error: Argument to Builtin.Exec() must be "
|
---|
109 | "string (found %d).\n", arg->vc);
|
---|
110 | exit(1);
|
---|
111 | }
|
---|
112 |
|
---|
113 | cmd[idx] = arg->u.string_v->value;
|
---|
114 | }
|
---|
115 |
|
---|
116 | cmd[dim] = NULL;
|
---|
117 |
|
---|
118 | if (os_exec((char *const *)cmd) != EOK) {
|
---|
119 | printf("Error: Exec failed.\n");
|
---|
120 | exit(1);
|
---|
121 | }
|
---|
122 | }
|
---|