blob: 5c93a849c806daf13ae15671d1d497dc31a3ce15 [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"
52#include "preproc.h"
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -080053#include "listing.h"
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040054
55#define BUF_DELTA 512
56
57static FILE *nop_fp;
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040058static int32_t nop_lineinc;
59
H. Peter Anvin169ac7c2016-09-25 17:08:05 -070060static void nop_init(void)
61{
62 /* Nothing to do */
63}
64
H. Peter Anvin130736c2016-02-17 20:27:41 -080065static void nop_reset(char *file, int pass, StrList **deplist)
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040066{
H. Peter Anvin274cda82016-05-10 02:56:29 -070067 src_set(0, file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040068 nop_lineinc = 1;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -070069 nop_fp = nasm_open_read(file, NF_TEXT);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040070
71 if (!nop_fp)
H. Peter Anvin130736c2016-02-17 20:27:41 -080072 nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +040073 (void)pass; /* placate compilers */
74
75 if (deplist) {
76 StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
77 sl->next = NULL;
78 strcpy(sl->str, file);
79 *deplist = sl;
80 }
81}
82
83static char *nop_getline(void)
84{
85 char *buffer, *p, *q;
86 int bufsize;
87
88 bufsize = BUF_DELTA;
89 buffer = nasm_malloc(BUF_DELTA);
90 src_set_linnum(src_get_linnum() + nop_lineinc);
91
92 while (1) { /* Loop to handle %line */
93
94 p = buffer;
95 while (1) { /* Loop to handle long lines */
96 q = fgets(p, bufsize - (p - buffer), nop_fp);
97 if (!q)
98 break;
99 p += strlen(p);
100 if (p > buffer && p[-1] == '\n')
101 break;
102 if (p - buffer > bufsize - 10) {
103 int offset;
104 offset = p - buffer;
105 bufsize += BUF_DELTA;
106 buffer = nasm_realloc(buffer, bufsize);
107 p = buffer + offset;
108 }
109 }
110
111 if (!q && p == buffer) {
112 nasm_free(buffer);
113 return NULL;
114 }
115
116 /*
117 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
118 * them are present at the end of the line.
119 */
120 buffer[strcspn(buffer, "\r\n\032")] = '\0';
121
122 if (!nasm_strnicmp(buffer, "%line", 5)) {
123 int32_t ln;
124 int li;
125 char *nm = nasm_malloc(strlen(buffer));
126 if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
H. Peter Anvin274cda82016-05-10 02:56:29 -0700127 src_set(ln, nm);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400128 nop_lineinc = li;
H. Peter Anvin274cda82016-05-10 02:56:29 -0700129 nasm_free(nm);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400130 continue;
131 }
132 nasm_free(nm);
133 }
134 break;
135 }
136
H. Peter Anvin8ac25aa2016-02-18 01:16:18 -0800137 lfmt->line(LIST_READ, buffer);
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400138
139 return buffer;
140}
141
142static void nop_cleanup(int pass)
143{
144 (void)pass; /* placate GCC */
145 if (nop_fp) {
146 fclose(nop_fp);
147 nop_fp = NULL;
148 }
149}
150
151static void nop_extra_stdmac(macros_t *macros)
152{
153 (void)macros;
154}
155
156static void nop_pre_define(char *definition)
157{
158 (void)definition;
159}
160
161static void nop_pre_undefine(char *definition)
162{
163 (void)definition;
164}
165
166static void nop_pre_include(char *fname)
167{
168 (void)fname;
169}
170
171static void nop_include_path(char *path)
172{
173 (void)path;
174}
175
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700176static void nop_error_list_macros(int severity)
177{
178 (void)severity;
179}
180
H. Peter Anvine7469712016-02-18 02:20:59 -0800181const struct preproc_ops preproc_nop = {
H. Peter Anvin169ac7c2016-09-25 17:08:05 -0700182 nop_init,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400183 nop_reset,
184 nop_getline,
185 nop_cleanup,
186 nop_extra_stdmac,
187 nop_pre_define,
188 nop_pre_undefine,
189 nop_pre_include,
H. Peter Anvin4def1a82016-05-09 13:59:44 -0700190 nop_include_path,
191 nop_error_list_macros,
Cyrill Gorcunovb5e8fec2012-05-07 11:34:27 +0400192};