MPX: Add test cases for MPX
[platform/upstream/nasm.git] / preproc-nop.c
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 1996-2012 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 <inttypes.h>
48 #include <limits.h>
49 #include <time.h>
50
51 #include "nasm.h"
52 #include "nasmlib.h"
53 #include "preproc.h"
54
55 #define BUF_DELTA 512
56
57 static FILE *nop_fp;
58 static ListGen *nop_list;
59 static int32_t nop_lineinc;
60
61 static void nop_reset(char *file, int pass, ListGen *listgen, StrList **deplist)
62 {
63     src_set_fname(nasm_strdup(file));
64     src_set_linnum(0);
65     nop_lineinc = 1;
66     nop_fp = fopen(file, "r");
67
68     if (!nop_fp)
69         nasm_error(ERR_FATAL | ERR_NOFILE,
70                    "unable to open input file `%s'", file);
71     nop_list = listgen;
72     (void)pass;                 /* placate compilers */
73
74     if (deplist) {
75         StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
76         sl->next = NULL;
77         strcpy(sl->str, file);
78         *deplist = sl;
79     }
80 }
81
82 static char *nop_getline(void)
83 {
84     char *buffer, *p, *q;
85     int bufsize;
86
87     bufsize = BUF_DELTA;
88     buffer = nasm_malloc(BUF_DELTA);
89     src_set_linnum(src_get_linnum() + nop_lineinc);
90
91     while (1) {                 /* Loop to handle %line */
92
93         p = buffer;
94         while (1) {             /* Loop to handle long lines */
95             q = fgets(p, bufsize - (p - buffer), nop_fp);
96             if (!q)
97                 break;
98             p += strlen(p);
99             if (p > buffer && p[-1] == '\n')
100                 break;
101             if (p - buffer > bufsize - 10) {
102                 int offset;
103                 offset = p - buffer;
104                 bufsize += BUF_DELTA;
105                 buffer = nasm_realloc(buffer, bufsize);
106                 p = buffer + offset;
107             }
108         }
109
110         if (!q && p == buffer) {
111             nasm_free(buffer);
112             return NULL;
113         }
114
115         /*
116          * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
117          * them are present at the end of the line.
118          */
119         buffer[strcspn(buffer, "\r\n\032")] = '\0';
120
121         if (!nasm_strnicmp(buffer, "%line", 5)) {
122             int32_t ln;
123             int li;
124             char *nm = nasm_malloc(strlen(buffer));
125             if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
126                 nasm_free(src_set_fname(nm));
127                 src_set_linnum(ln);
128                 nop_lineinc = li;
129                 continue;
130             }
131             nasm_free(nm);
132         }
133         break;
134     }
135
136     nop_list->line(LIST_READ, buffer);
137
138     return buffer;
139 }
140
141 static void nop_cleanup(int pass)
142 {
143     (void)pass;                     /* placate GCC */
144     if (nop_fp) {
145         fclose(nop_fp);
146         nop_fp = NULL;
147     }
148 }
149
150 static void nop_extra_stdmac(macros_t *macros)
151 {
152     (void)macros;
153 }
154
155 static void nop_pre_define(char *definition)
156 {
157     (void)definition;
158 }
159
160 static void nop_pre_undefine(char *definition)
161 {
162     (void)definition;
163 }
164
165 static void nop_pre_include(char *fname)
166 {
167     (void)fname;
168 }
169
170 static void nop_include_path(char *path)
171 {
172     (void)path;
173 }
174
175 struct preproc_ops preproc_nop = {
176     nop_reset,
177     nop_getline,
178     nop_cleanup,
179     nop_extra_stdmac,
180     nop_pre_define,
181     nop_pre_undefine,
182     nop_pre_include,
183     nop_include_path
184 };