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
|
---|
48 | static 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 | */
|
---|
57 | errno_t program_file_process(stree_program_t *program, const char *fname)
|
---|
58 | {
|
---|
59 | input_t *input;
|
---|
60 | lex_t lex;
|
---|
61 | parse_t parse;
|
---|
62 | errno_t 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 | *
|
---|
88 | * @param program The program to which the parsed code is added.
|
---|
89 | * @return EOK on success, EIO if some file comprising the
|
---|
90 | * library is not found, EINVAL if the library
|
---|
91 | * has syntax errors.
|
---|
92 | */
|
---|
93 | errno_t program_lib_process(stree_program_t *program)
|
---|
94 | {
|
---|
95 | errno_t rc;
|
---|
96 | char *path, *fname;
|
---|
97 | char *tmp;
|
---|
98 | char *cp;
|
---|
99 |
|
---|
100 | FILE *f;
|
---|
101 |
|
---|
102 | path = os_get_lib_path();
|
---|
103 | fname = os_str_acat(path, "/libflist");
|
---|
104 |
|
---|
105 | f = fopen(fname, "rt");
|
---|
106 | if (f == NULL) {
|
---|
107 | printf("Failed opening library list file '%s'.\n", fname);
|
---|
108 | free(path);
|
---|
109 | free(fname);
|
---|
110 | return EIO;
|
---|
111 | }
|
---|
112 |
|
---|
113 | free(fname);
|
---|
114 |
|
---|
115 | while (b_true) {
|
---|
116 | if (fgets(name_buf, MAX_NAME_SIZE + 1, f) == NULL)
|
---|
117 | break;
|
---|
118 |
|
---|
119 | /*
|
---|
120 | * Remove trailing newline, if present.
|
---|
121 | */
|
---|
122 | cp = name_buf;
|
---|
123 | while (*cp != '\0' && *cp != '\n')
|
---|
124 | ++cp;
|
---|
125 |
|
---|
126 | if (*cp == '\n')
|
---|
127 | *cp = '\0';
|
---|
128 |
|
---|
129 | tmp = os_str_acat(path, "/");
|
---|
130 | fname = os_str_acat(tmp, name_buf);
|
---|
131 | free(tmp);
|
---|
132 |
|
---|
133 | rc = program_file_process(program, fname);
|
---|
134 | if (rc != EOK) {
|
---|
135 | free(fname);
|
---|
136 | free(path);
|
---|
137 | fclose(f);
|
---|
138 | return rc;
|
---|
139 | }
|
---|
140 |
|
---|
141 | free(fname);
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (ferror(f)) {
|
---|
145 | printf("Error reading from library list file.\n");
|
---|
146 | free(path);
|
---|
147 | fclose(f);
|
---|
148 | return EIO;
|
---|
149 | }
|
---|
150 |
|
---|
151 | free(path);
|
---|
152 | fclose(f);
|
---|
153 |
|
---|
154 | return EOK;
|
---|
155 | }
|
---|