blob: 3d77dd527fb90fb74ebf0a7c1ab5af36c74fd4bf [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
42#include <stdio.h>
43#include <stdarg.h>
44#include <stdlib.h>
45#include <string.h>
46#include <ctype.h>
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040047#include <limits.h>
48#include <time.h>
49
50#include "nasm.h"
51#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -080052#include "error.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040053#include "preproc.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080054#include "listing.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040055
56#define BUF_DELTA 512
57
58static FILE *nop_fp;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040059static int32_t nop_lineinc;
60
H. Peter Anvin169ac7c2016-09-25 17:08:05 -070061static void nop_init(void)
62{
63 /* Nothing to do */
64}
65
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -080066static void nop_reset(const char *file, enum preproc_mode mode,
67 struct strlist *deplist)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040068{
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -080069 (void)mode; /* placate compilers */
70
H. Peter Anvin274cda82016-05-10 02:56:29 -070071 src_set(0, file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040072 nop_lineinc = 1;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -070073 nop_fp = nasm_open_read(file, NF_TEXT);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040074
75 if (!nop_fp)
Cyrill Gorcunovc3527dd2018-11-24 22:17:47 +030076 nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040077
Cyrill Gorcunovb7bb5ac2018-11-11 21:33:52 +030078 strlist_add(deplist, file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040079}
80
81static char *nop_getline(void)
82{
83 char *buffer, *p, *q;
84 int bufsize;
85
86 bufsize = BUF_DELTA;
87 buffer = nasm_malloc(BUF_DELTA);
88 src_set_linnum(src_get_linnum() + nop_lineinc);
89
90 while (1) { /* Loop to handle %line */
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040091 p = buffer;
92 while (1) { /* Loop to handle long lines */
93 q = fgets(p, bufsize - (p - buffer), nop_fp);
94 if (!q)
95 break;
96 p += strlen(p);
97 if (p > buffer && p[-1] == '\n')
98 break;
99 if (p - buffer > bufsize - 10) {
100 int offset;
101 offset = p - buffer;
102 bufsize += BUF_DELTA;
103 buffer = nasm_realloc(buffer, bufsize);
104 p = buffer + offset;
105 }
106 }
107
108 if (!q && p == buffer) {
109 nasm_free(buffer);
110 return NULL;
111 }
112
113 /*
114 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
115 * them are present at the end of the line.
116 */
117 buffer[strcspn(buffer, "\r\n\032")] = '\0';
118
119 if (!nasm_strnicmp(buffer, "%line", 5)) {
120 int32_t ln;
121 int li;
122 char *nm = nasm_malloc(strlen(buffer));
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800123 int conv = sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm);
124 if (conv >= 2) {
125 if (!pp_noline)
126 src_set(ln, conv >= 3 ? nm : NULL);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400127 nop_lineinc = li;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400128 }
129 nasm_free(nm);
H. Peter Anvin (Intel)800c1682018-12-14 12:22:11 -0800130 if (conv >= 2)
131 continue;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400132 }
133 break;
134 }
135
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800136 lfmt->line(LIST_READ, buffer);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400137
138 return buffer;
139}
140
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800141static void nop_cleanup_pass(void)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400142{
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400143 if (nop_fp) {
144 fclose(nop_fp);
145 nop_fp = NULL;
146 }
147}
148
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800149static void nop_cleanup_session(void)
150{
151 /* Nothing we need to do */
152}
153
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400154static void nop_extra_stdmac(macros_t *macros)
155{
156 (void)macros;
157}
158
159static void nop_pre_define(char *definition)
160{
161 (void)definition;
162}
163
164static void nop_pre_undefine(char *definition)
165{
166 (void)definition;
167}
168
169static void nop_pre_include(char *fname)
170{
171 (void)fname;
172}
173
H. Peter Anvin05990342018-06-11 13:32:42 -0700174static void nop_pre_command(const char *what, char *string)
175{
176 (void)what;
177 (void)string;
178}
179
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300180static void nop_include_path(struct strlist *list)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400181{
Cyrill Gorcunov8c0666b2018-11-24 14:33:48 +0300182 (void)list;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400183}
184
H. Peter Anvin (Intel)6bde2ed2018-12-13 19:39:41 -0800185static void nop_error_list_macros(errflags severity)
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700186{
187 (void)severity;
188}
189
H. Peter Anvine7469712016-02-18 02:20:59 -0800190const struct preproc_ops preproc_nop = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700191 nop_init,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400192 nop_reset,
193 nop_getline,
H. Peter Anvin (Intel)e55d03d2018-12-18 11:12:46 -0800194 nop_cleanup_pass,
195 nop_cleanup_session,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400196 nop_extra_stdmac,
197 nop_pre_define,
198 nop_pre_undefine,
199 nop_pre_include,
H. Peter Anvin05990342018-06-11 13:32:42 -0700200 nop_pre_command,
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700201 nop_include_path,
202 nop_error_list_macros,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400203};