Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 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> |
H. Peter Anvin | c2f3f26 | 2018-12-27 12:37:25 -0800 | [diff] [blame^] | 45 | #include "nctype.h" |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 46 | #include <limits.h> |
| 47 | #include <time.h> |
| 48 | |
| 49 | #include "nasm.h" |
| 50 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 51 | #include "error.h" |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 52 | #include "preproc.h" |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 53 | #include "listing.h" |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 54 | |
| 55 | #define BUF_DELTA 512 |
| 56 | |
| 57 | static FILE *nop_fp; |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 58 | static int32_t nop_lineinc; |
| 59 | |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 60 | static void nop_init(void) |
| 61 | { |
| 62 | /* Nothing to do */ |
| 63 | } |
| 64 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 65 | static void nop_reset(const char *file, enum preproc_mode mode, |
| 66 | struct strlist *deplist) |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 67 | { |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 68 | (void)mode; /* placate compilers */ |
| 69 | |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 70 | src_set(0, file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 71 | nop_lineinc = 1; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 72 | nop_fp = nasm_open_read(file, NF_TEXT); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 73 | |
| 74 | if (!nop_fp) |
Cyrill Gorcunov | c3527dd | 2018-11-24 22:17:47 +0300 | [diff] [blame] | 75 | nasm_fatalf(ERR_NOFILE, "unable to open input file `%s'", file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 76 | |
Cyrill Gorcunov | b7bb5ac | 2018-11-11 21:33:52 +0300 | [diff] [blame] | 77 | strlist_add(deplist, file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static char *nop_getline(void) |
| 81 | { |
| 82 | char *buffer, *p, *q; |
| 83 | int bufsize; |
| 84 | |
| 85 | bufsize = BUF_DELTA; |
| 86 | buffer = nasm_malloc(BUF_DELTA); |
| 87 | src_set_linnum(src_get_linnum() + nop_lineinc); |
| 88 | |
| 89 | while (1) { /* Loop to handle %line */ |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 90 | p = buffer; |
| 91 | while (1) { /* Loop to handle long lines */ |
| 92 | q = fgets(p, bufsize - (p - buffer), nop_fp); |
| 93 | if (!q) |
| 94 | break; |
| 95 | p += strlen(p); |
| 96 | if (p > buffer && p[-1] == '\n') |
| 97 | break; |
| 98 | if (p - buffer > bufsize - 10) { |
| 99 | int offset; |
| 100 | offset = p - buffer; |
| 101 | bufsize += BUF_DELTA; |
| 102 | buffer = nasm_realloc(buffer, bufsize); |
| 103 | p = buffer + offset; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | if (!q && p == buffer) { |
| 108 | nasm_free(buffer); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Play safe: remove CRs, LFs and any spurious ^Zs, if any of |
| 114 | * them are present at the end of the line. |
| 115 | */ |
| 116 | buffer[strcspn(buffer, "\r\n\032")] = '\0'; |
| 117 | |
| 118 | if (!nasm_strnicmp(buffer, "%line", 5)) { |
| 119 | int32_t ln; |
| 120 | int li; |
| 121 | char *nm = nasm_malloc(strlen(buffer)); |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 122 | int conv = sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm); |
| 123 | if (conv >= 2) { |
| 124 | if (!pp_noline) |
| 125 | src_set(ln, conv >= 3 ? nm : NULL); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 126 | nop_lineinc = li; |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 127 | } |
| 128 | nasm_free(nm); |
H. Peter Anvin (Intel) | 800c168 | 2018-12-14 12:22:11 -0800 | [diff] [blame] | 129 | if (conv >= 2) |
| 130 | continue; |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 131 | } |
| 132 | break; |
| 133 | } |
| 134 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 135 | lfmt->line(LIST_READ, buffer); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 136 | |
| 137 | return buffer; |
| 138 | } |
| 139 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 140 | static void nop_cleanup_pass(void) |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 141 | { |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 142 | if (nop_fp) { |
| 143 | fclose(nop_fp); |
| 144 | nop_fp = NULL; |
| 145 | } |
| 146 | } |
| 147 | |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 148 | static void nop_cleanup_session(void) |
| 149 | { |
| 150 | /* Nothing we need to do */ |
| 151 | } |
| 152 | |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 153 | static void nop_extra_stdmac(macros_t *macros) |
| 154 | { |
| 155 | (void)macros; |
| 156 | } |
| 157 | |
| 158 | static void nop_pre_define(char *definition) |
| 159 | { |
| 160 | (void)definition; |
| 161 | } |
| 162 | |
| 163 | static void nop_pre_undefine(char *definition) |
| 164 | { |
| 165 | (void)definition; |
| 166 | } |
| 167 | |
| 168 | static void nop_pre_include(char *fname) |
| 169 | { |
| 170 | (void)fname; |
| 171 | } |
| 172 | |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 173 | static void nop_pre_command(const char *what, char *string) |
| 174 | { |
| 175 | (void)what; |
| 176 | (void)string; |
| 177 | } |
| 178 | |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 179 | static void nop_include_path(struct strlist *list) |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 180 | { |
Cyrill Gorcunov | 8c0666b | 2018-11-24 14:33:48 +0300 | [diff] [blame] | 181 | (void)list; |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 182 | } |
| 183 | |
H. Peter Anvin (Intel) | 6bde2ed | 2018-12-13 19:39:41 -0800 | [diff] [blame] | 184 | static void nop_error_list_macros(errflags severity) |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 185 | { |
| 186 | (void)severity; |
| 187 | } |
| 188 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 189 | const struct preproc_ops preproc_nop = { |
H. Peter Anvin | 169ac7c | 2016-09-25 17:08:05 -0700 | [diff] [blame] | 190 | nop_init, |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 191 | nop_reset, |
| 192 | nop_getline, |
H. Peter Anvin (Intel) | e55d03d | 2018-12-18 11:12:46 -0800 | [diff] [blame] | 193 | nop_cleanup_pass, |
| 194 | nop_cleanup_session, |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 195 | nop_extra_stdmac, |
| 196 | nop_pre_define, |
| 197 | nop_pre_undefine, |
| 198 | nop_pre_include, |
H. Peter Anvin | 0599034 | 2018-06-11 13:32:42 -0700 | [diff] [blame] | 199 | nop_pre_command, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 200 | nop_include_path, |
| 201 | nop_error_list_macros, |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 202 | }; |