source: mainline/uspace/app/sbi/src/program.c@ bd01a4e

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

Update SBI to rev. 184.

  • Property mode set to 100644
File size: 3.7 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 Main module.
30 *
31 * Main entry point for SBI, the Sysel Bootstrap Interpreter.
32 * When run without parameters, the interpreter will enter interactive
33 * mode.
34 */
35
36#include <stdio.h>
37#include <stdlib.h>
38#include "os/os.h"
39#include "mytypes.h"
40#include "stype.h"
41#include "input.h"
42#include "lex.h"
43#include "parse.h"
44
45#include "program.h"
46
47#define MAX_NAME_SIZE 64
48static char name_buf[MAX_NAME_SIZE + 1];
49
50/** Process one specified source file.
51 *
52 * @param program The program to which the parsed code is added.
53 * @param fname Name of file to read from.
54 * @return EOK on success, EIO if file is not found,
55 * EINVAL if file has syntax errors.
56 */
57int program_file_process(stree_program_t *program, const char *fname)
58{
59 input_t *input;
60 lex_t lex;
61 parse_t parse;
62 int rc;
63
64 rc = input_new_file(&input, fname);
65 if (rc != EOK) {
66 printf("Failed opening source file '%s'.\n", fname);
67 return EIO;
68 }
69
70 /* Parse input file. */
71 lex_init(&lex, input);
72 parse_init(&parse, program, &lex);
73 parse_module(&parse);
74
75 /* Check for parse errors. */
76 if (parse.error)
77 return EINVAL;
78
79 return EOK;
80}
81
82/** Process sources of the library.
83 *
84 * Processes all source files in the library. The list of library source files
85 * is read from '<libdir>/libflist'. Each line of the file contains one file
86 * name relative to <libdir>.
87 */
88int program_lib_process(stree_program_t *program)
89{
90 int rc;
91 char *path, *fname;
92 char *tmp;
93 char *cp;
94
95 FILE *f;
96
97 path = os_get_lib_path();
98 fname = os_str_acat(path, "/libflist");
99
100 f = fopen(fname, "rt");
101 if (f == NULL) {
102 printf("Failed opening library list file '%s'.\n", fname);
103 free(path);
104 free(fname);
105 return EIO;
106 }
107
108 free(fname);
109
110 while (b_true) {
111 if (fgets(name_buf, MAX_NAME_SIZE + 1, f) == NULL)
112 break;
113
114 /*
115 * Remove trailing newline, if present.
116 */
117 cp = name_buf;
118 while (*cp != '\0' && *cp != '\n')
119 ++cp;
120
121 if (*cp == '\n')
122 *cp = '\0';
123
124 tmp = os_str_acat(path, "/");
125 fname = os_str_acat(tmp, name_buf);
126 free(tmp);
127
128 rc = program_file_process(program, fname);
129 if (rc != EOK) {
130 free(fname);
131 free(path);
132 fclose(f);
133 return rc;
134 }
135
136 free(fname);
137 }
138
139 if (ferror(f)) {
140 printf("Error reading from library list file.\n");
141 free(path);
142 fclose(f);
143 return EIO;
144 }
145
146 free(path);
147 fclose(f);
148
149 return rc;
150}
Note: See TracBrowser for help on using the repository browser.