source: mainline/uspace/app/sbi/src/builtin/bi_fun.c@ 051bc69a

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 051bc69a was 38aaacc2, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Update SBI to rev. 207.

  • Property mode set to 100644
File size: 5.2 KB
Line 
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 Builtin functions. */
30
31#include <stdio.h>
32#include <stdlib.h>
33#include <assert.h>
34#include "../bigint.h"
35#include "../builtin.h"
36#include "../list.h"
37#include "../mytypes.h"
38#include "../os/os.h"
39#include "../run.h"
40#include "../stree.h"
41#include "../strtab.h"
42#include "../symbol.h"
43
44#include "bi_fun.h"
45
46static void bi_fun_builtin_write(run_t *run);
47static void bi_fun_builtin_writeline(run_t *run);
48static void bi_fun_task_exec(run_t *run);
49
50/** Declare builtin functions.
51 *
52 * @param bi Builtin object
53 */
54void bi_fun_declare(builtin_t *bi)
55{
56 stree_modm_t *modm;
57 stree_csi_t *csi;
58 stree_ident_t *ident;
59 stree_symbol_t *symbol;
60 stree_symbol_t *fun_sym;
61
62 /* Declare class Builtin */
63
64 ident = stree_ident_new();
65 ident->sid = strtab_get_sid("Builtin");
66
67 csi = stree_csi_new(csi_class);
68 csi->name = ident;
69 list_init(&csi->targ);
70 list_init(&csi->members);
71
72 modm = stree_modm_new(mc_csi);
73 modm->u.csi = csi;
74
75 symbol = stree_symbol_new(sc_csi);
76 symbol->u.csi = csi;
77 symbol->outer_csi = NULL;
78 csi->symbol = symbol;
79
80 list_append(&bi->program->module->members, modm);
81
82 /* Declare Builtin.Write(). */
83
84 fun_sym = builtin_declare_fun(csi, "Write");
85 builtin_fun_add_arg(fun_sym, "arg");
86
87 /* Declare Builtin.WriteLine(). */
88
89 fun_sym = builtin_declare_fun(csi, "WriteLine");
90 builtin_fun_add_arg(fun_sym, "arg");
91
92 /* Declare class Task. */
93
94 builtin_code_snippet(bi,
95 "class Task is\n"
96 "fun Exec(args : string[], packed), builtin;\n"
97 "end\n");
98}
99
100/** Bind builtin functions.
101 *
102 * @param bi Builtin object
103 */
104void bi_fun_bind(builtin_t *bi)
105{
106 builtin_fun_bind(bi, "Builtin", "Write", bi_fun_builtin_write);
107 builtin_fun_bind(bi, "Builtin", "WriteLine", bi_fun_builtin_writeline);
108 builtin_fun_bind(bi, "Task", "Exec", bi_fun_task_exec);
109}
110
111/** Write to the console.
112 *
113 * @param run Runner object
114 */
115static void bi_fun_builtin_write(run_t *run)
116{
117 rdata_var_t *var;
118 int char_val;
119 int rc;
120
121#ifdef DEBUG_RUN_TRACE
122 printf("Called Builtin.Write()\n");
123#endif
124 var = run_local_vars_lookup(run, strtab_get_sid("arg"));
125 assert(var);
126
127 switch (var->vc) {
128 case vc_bool:
129 printf("%s", var->u.bool_v->value ? "true" : "false");
130 break;
131 case vc_char:
132 rc = bigint_get_value_int(&var->u.char_v->value, &char_val);
133 if (rc == EOK)
134 printf("%lc", char_val);
135 else
136 printf("???");
137 break;
138 case vc_int:
139 bigint_print(&var->u.int_v->value);
140 break;
141 case vc_string:
142 printf("%s", var->u.string_v->value);
143 break;
144 default:
145 printf("Unimplemented: Write() with unsupported type.\n");
146 exit(1);
147 }
148}
149
150/** Write a line of output.
151 *
152 * @param run Runner object
153 */
154static void bi_fun_builtin_writeline(run_t *run)
155{
156#ifdef DEBUG_RUN_TRACE
157 printf("Called Builtin.WriteLine()\n");
158#endif
159 bi_fun_builtin_write(run);
160 putchar('\n');
161}
162
163/** Start an executable and wait for it to finish.
164 *
165 * @param run Runner object
166 */
167static void bi_fun_task_exec(run_t *run)
168{
169 rdata_var_t *args;
170 rdata_var_t *var;
171 rdata_array_t *array;
172 rdata_var_t *arg;
173 int idx, dim;
174 const char **cmd;
175
176#ifdef DEBUG_RUN_TRACE
177 printf("Called Task.Exec()\n");
178#endif
179 args = run_local_vars_lookup(run, strtab_get_sid("args"));
180
181 assert(args);
182 assert(args->vc == vc_ref);
183
184 var = args->u.ref_v->vref;
185 assert(var->vc == vc_array);
186
187 array = var->u.array_v;
188 assert(array->rank == 1);
189 dim = array->extent[0];
190
191 if (dim == 0) {
192 printf("Error: Builtin.Exec() expects at least one argument.\n");
193 exit(1);
194 }
195
196 cmd = calloc(dim + 1, sizeof(char *));
197 if (cmd == NULL) {
198 printf("Memory allocation failed.\n");
199 exit(1);
200 }
201
202 for (idx = 0; idx < dim; ++idx) {
203 arg = array->element[idx];
204 if (arg->vc != vc_string) {
205 printf("Error: Argument to Builtin.Exec() must be "
206 "string (found %d).\n", arg->vc);
207 exit(1);
208 }
209
210 cmd[idx] = arg->u.string_v->value;
211 }
212
213 cmd[dim] = '\0';
214
215 if (os_exec((char * const *)cmd) != EOK) {
216 printf("Error: Exec failed.\n");
217 exit(1);
218 }
219}
Note: See TracBrowser for help on using the repository browser.