blob: a927880c68ec2336a5a6cf612b5ef607b4cf7e31 [file] [log] [blame]
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +04001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvin4def1a82016-05-09 13:59:44 -07003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +04004 * 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
H. Peter Anvinc2f3f262018-12-27 12:37:25 -080042#include "nctype.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040043#include <time.h>
44
45#include "nasm.h"
46#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080047#include "error.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040048#include "preproc.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080049#include "listing.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040050
51#define BUF_DELTA 512
52
53static FILE *nop_fp;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040054static int32_t nop_lineinc;
55
H. Peter Anvin169ac7c2016-09-25 17:08:05 -070056static void nop_init(void)
57{
58 /* Nothing to do */
59}
60
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -080061static void nop_reset(const char *file, enum preproc_mode mode,
62 struct strlist *deplist)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040063{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -080064 (void)mode; /* placate compilers */
65
H. Peter Anvin274cda82016-05-10 02:56:29 -070066 src_set(0, file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040067 nop_lineinc = 1;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -070068 nop_fp = nasm_open_read(file, NF_TEXT);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040069
70 if (!nop_fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030071 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040072
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030073 strlist_add(deplist, file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040074}
75
76static char *nop_getline(void)
77{
78 char *buffer, *p, *q;
79 int bufsize;
80
81 bufsize = BUF_DELTA;
82 buffer = nasm_malloc(BUF_DELTA);
83 src_set_linnum(src_get_linnum() + nop_lineinc);
84
85 while (1) { /* Loop to handle %line */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040086 p = buffer;
87 while (1) { /* Loop to handle long lines */
88 q = fgets(p, bufsize - (p - buffer), nop_fp);
89 if (!q)
90 break;
91 p += strlen(p);
92 if (p > buffer && p[-1] == '\n')
93 break;
94 if (p - buffer > bufsize - 10) {
95 int offset;
96 offset = p - buffer;
97 bufsize += BUF_DELTA;
98 buffer = nasm_realloc(buffer, bufsize);
99 p = buffer + offset;
100 }
101 }
102
103 if (!q && p == buffer) {
104 nasm_free(buffer);
105 return NULL;
106 }
107
108 /*
109 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
110 * them are present at the end of the line.
111 */
112 buffer[strcspn(buffer, "\r\n\032")] = '\0';
113
114 if (!nasm_strnicmp(buffer, "%line", 5)) {
115 int32_t ln;
116 int li;
117 char *nm = nasm_malloc(strlen(buffer));
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800118 int conv = sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm);
119 if (conv >= 2) {
120 if (!pp_noline)
121 src_set(ln, conv >= 3 ? nm : NULL);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400122 nop_lineinc = li;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400123 }
124 nasm_free(nm);
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800125 if (conv >= 2)
126 continue;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400127 }
128 break;
129 }
130
H. Peter Anvinab6f8312019-08-09 22:31:45 -0700131 lfmt->line(LIST_READ, src_get_linnum(), buffer);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400132
133 return buffer;
134}
135
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800136static void nop_cleanup_pass(void)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400137{
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400138 if (nop_fp) {
139 fclose(nop_fp);
140 nop_fp = NULL;
141 }
142}
143
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800144static void nop_cleanup_session(void)
145{
146 /* Nothing we need to do */
147}
148
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400149static void nop_extra_stdmac(macros_t *macros)
150{
151 (void)macros;
152}
153
154static void nop_pre_define(char *definition)
155{
156 (void)definition;
157}
158
159static void nop_pre_undefine(char *definition)
160{
161 (void)definition;
162}
163
164static void nop_pre_include(char *fname)
165{
166 (void)fname;
167}
168
H. Peter Anvin05990342018-06-11 13:32:42 -0700169static void nop_pre_command(const char *what, char *string)
170{
171 (void)what;
172 (void)string;
173}
174
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300175static void nop_include_path(struct strlist *list)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400176{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300177 (void)list;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400178}
179
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800180static void nop_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700181{
182 (void)severity;
183}
184
H. Peter Anvina73ccfe2019-08-28 19:02:47 -0700185static bool nop_suppress_error(errflags severity)
186{
187 (void)severity;
188 return false;
189}
190
H. Peter Anvine7469712016-02-18 02:20:59 -0800191const struct preproc_ops preproc_nop = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700192 nop_init,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400193 nop_reset,
194 nop_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800195 nop_cleanup_pass,
196 nop_cleanup_session,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400197 nop_extra_stdmac,
198 nop_pre_define,
199 nop_pre_undefine,
200 nop_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -0700201 nop_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700202 nop_include_path,
203 nop_error_list_macros,
H. Peter Anvina73ccfe2019-08-28 19:02:47 -0700204 nop_suppress_error
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400205};