1 | /* ----------------------------------------------------------------------- * |
2 | * |
3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved |
4 | * See the file AUTHORS included with the NASM distribution for |
5 | * the specific copyright holders. |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following |
9 | * conditions are met: |
10 | * |
11 | * * Redistributions of source code must retain the above copyright |
12 | * notice, this list of conditions and the following disclaimer. |
13 | * * Redistributions in binary form must reproduce the above |
14 | * copyright notice, this list of conditions and the following |
15 | * disclaimer in the documentation and/or other materials provided |
16 | * with the distribution. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 | * |
32 | * ----------------------------------------------------------------------- */ |
33 | |
34 | /* |
35 | * This is a null preprocessor which just copies lines from input |
36 | * to output. It's used when someone explicitly requests that NASM |
37 | * not preprocess their source file. |
38 | */ |
39 | |
40 | #include "compiler.h" |
41 | |
42 | #include <stdio.h> |
43 | #include <stdarg.h> |
44 | #include <stdlib.h> |
45 | #include <string.h> |
46 | #include <ctype.h> |
47 | #include <limits.h> |
48 | #include <time.h> |
49 | |
50 | #include "nasm.h" |
51 | #include "nasmlib.h" |
52 | #include "error.h" |
53 | #include "preproc.h" |
54 | #include "listing.h" |
55 | |
56 | #define BUF_DELTA 512 |
57 | |
58 | static FILE *nop_fp; |
59 | static int32_t nop_lineinc; |
60 | |
61 | static void nop_init(void) |
62 | { |
63 | /* Nothing to do */ |
64 | } |
65 | |
66 | static void nop_reset(const char *file, int pass, StrList **deplist) |
67 | { |
68 | src_set(0, file); |
69 | nop_lineinc = 1; |
70 | nop_fp = nasm_open_read(file, NF_TEXT); |
71 | |
72 | if (!nop_fp) |
73 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'" , file); |
74 | (void)pass; /* placate compilers */ |
75 | |
76 | nasm_add_string_to_strlist(deplist, file); |
77 | } |
78 | |
79 | static char *nop_getline(void) |
80 | { |
81 | char *buffer, *p, *q; |
82 | int bufsize; |
83 | |
84 | bufsize = BUF_DELTA; |
85 | buffer = nasm_malloc(BUF_DELTA); |
86 | src_set_linnum(src_get_linnum() + nop_lineinc); |
87 | |
88 | while (1) { /* Loop to handle %line */ |
89 | p = buffer; |
90 | while (1) { /* Loop to handle long lines */ |
91 | q = fgets(p, bufsize - (p - buffer), nop_fp); |
92 | if (!q) |
93 | break; |
94 | p += strlen(p); |
95 | if (p > buffer && p[-1] == '\n') |
96 | break; |
97 | if (p - buffer > bufsize - 10) { |
98 | int offset; |
99 | offset = p - buffer; |
100 | bufsize += BUF_DELTA; |
101 | buffer = nasm_realloc(buffer, bufsize); |
102 | p = buffer + offset; |
103 | } |
104 | } |
105 | |
106 | if (!q && p == buffer) { |
107 | nasm_free(buffer); |
108 | return NULL; |
109 | } |
110 | |
111 | /* |
112 | * Play safe: remove CRs, LFs and any spurious ^Zs, if any of |
113 | * them are present at the end of the line. |
114 | */ |
115 | buffer[strcspn(buffer, "\r\n\032" )] = '\0'; |
116 | |
117 | if (!nasm_strnicmp(buffer, "%line" , 5)) { |
118 | int32_t ln; |
119 | int li; |
120 | char *nm = nasm_malloc(strlen(buffer)); |
121 | int conv = sscanf(buffer + 5, "%" PRId32"+%d %s" , &ln, &li, nm); |
122 | if (conv >= 2) { |
123 | if (!pp_noline) |
124 | src_set(ln, conv >= 3 ? nm : NULL); |
125 | nop_lineinc = li; |
126 | } |
127 | nasm_free(nm); |
128 | if (conv >= 2) |
129 | continue; |
130 | } |
131 | break; |
132 | } |
133 | |
134 | lfmt->line(LIST_READ, buffer); |
135 | |
136 | return buffer; |
137 | } |
138 | |
139 | static void nop_cleanup(int pass) |
140 | { |
141 | (void)pass; /* placate GCC */ |
142 | if (nop_fp) { |
143 | fclose(nop_fp); |
144 | nop_fp = NULL; |
145 | } |
146 | } |
147 | |
148 | static void (macros_t *macros) |
149 | { |
150 | (void)macros; |
151 | } |
152 | |
153 | static void nop_pre_define(char *definition) |
154 | { |
155 | (void)definition; |
156 | } |
157 | |
158 | static void nop_pre_undefine(char *definition) |
159 | { |
160 | (void)definition; |
161 | } |
162 | |
163 | static void nop_pre_include(char *fname) |
164 | { |
165 | (void)fname; |
166 | } |
167 | |
168 | static void nop_pre_command(const char *what, char *string) |
169 | { |
170 | (void)what; |
171 | (void)string; |
172 | } |
173 | |
174 | static void nop_include_path(char *path) |
175 | { |
176 | (void)path; |
177 | } |
178 | |
179 | static void nop_error_list_macros(int severity) |
180 | { |
181 | (void)severity; |
182 | } |
183 | |
184 | const struct preproc_ops preproc_nop = { |
185 | nop_init, |
186 | nop_reset, |
187 | nop_getline, |
188 | nop_cleanup, |
189 | nop_extra_stdmac, |
190 | nop_pre_define, |
191 | nop_pre_undefine, |
192 | nop_pre_include, |
193 | nop_pre_command, |
194 | nop_include_path, |
195 | nop_error_list_macros, |
196 | }; |
197 | |