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> |
| 45 | #include <string.h> |
| 46 | #include <ctype.h> |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 47 | #include <limits.h> |
| 48 | #include <time.h> |
| 49 | |
| 50 | #include "nasm.h" |
| 51 | #include "nasmlib.h" |
| 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 | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 60 | static void nop_reset(char *file, int pass, StrList **deplist) |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 61 | { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 62 | src_set(0, file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 63 | nop_lineinc = 1; |
H. Peter Anvin | 3e83cec | 2016-05-25 04:28:46 -0700 | [diff] [blame] | 64 | nop_fp = nasm_open_read(file, NF_TEXT); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 65 | |
| 66 | if (!nop_fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 67 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 68 | (void)pass; /* placate compilers */ |
| 69 | |
| 70 | if (deplist) { |
| 71 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 72 | sl->next = NULL; |
| 73 | strcpy(sl->str, file); |
| 74 | *deplist = sl; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static char *nop_getline(void) |
| 79 | { |
| 80 | char *buffer, *p, *q; |
| 81 | int bufsize; |
| 82 | |
| 83 | bufsize = BUF_DELTA; |
| 84 | buffer = nasm_malloc(BUF_DELTA); |
| 85 | src_set_linnum(src_get_linnum() + nop_lineinc); |
| 86 | |
| 87 | while (1) { /* Loop to handle %line */ |
| 88 | |
| 89 | p = buffer; |
| 90 | while (1) { /* Loop to handle long lines */ |
| 91 | q = fgets(p, bufsize - (p - buffer), nop_fp); |
| 92 | if (!q) |
| 93 | break; |
| 94 | p += strlen(p); |
| 95 | if (p > buffer && p[-1] == '\n') |
| 96 | break; |
| 97 | if (p - buffer > bufsize - 10) { |
| 98 | int offset; |
| 99 | offset = p - buffer; |
| 100 | bufsize += BUF_DELTA; |
| 101 | buffer = nasm_realloc(buffer, bufsize); |
| 102 | p = buffer + offset; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!q && p == buffer) { |
| 107 | nasm_free(buffer); |
| 108 | return NULL; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * Play safe: remove CRs, LFs and any spurious ^Zs, if any of |
| 113 | * them are present at the end of the line. |
| 114 | */ |
| 115 | buffer[strcspn(buffer, "\r\n\032")] = '\0'; |
| 116 | |
| 117 | if (!nasm_strnicmp(buffer, "%line", 5)) { |
| 118 | int32_t ln; |
| 119 | int li; |
| 120 | char *nm = nasm_malloc(strlen(buffer)); |
| 121 | if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) { |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 122 | src_set(ln, nm); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 123 | nop_lineinc = li; |
H. Peter Anvin | 274cda8 | 2016-05-10 02:56:29 -0700 | [diff] [blame] | 124 | nasm_free(nm); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 125 | continue; |
| 126 | } |
| 127 | nasm_free(nm); |
| 128 | } |
| 129 | break; |
| 130 | } |
| 131 | |
H. Peter Anvin | 8ac25aa | 2016-02-18 01:16:18 -0800 | [diff] [blame] | 132 | lfmt->line(LIST_READ, buffer); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 133 | |
| 134 | return buffer; |
| 135 | } |
| 136 | |
| 137 | static void nop_cleanup(int pass) |
| 138 | { |
| 139 | (void)pass; /* placate GCC */ |
| 140 | if (nop_fp) { |
| 141 | fclose(nop_fp); |
| 142 | nop_fp = NULL; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static void nop_extra_stdmac(macros_t *macros) |
| 147 | { |
| 148 | (void)macros; |
| 149 | } |
| 150 | |
| 151 | static void nop_pre_define(char *definition) |
| 152 | { |
| 153 | (void)definition; |
| 154 | } |
| 155 | |
| 156 | static void nop_pre_undefine(char *definition) |
| 157 | { |
| 158 | (void)definition; |
| 159 | } |
| 160 | |
| 161 | static void nop_pre_include(char *fname) |
| 162 | { |
| 163 | (void)fname; |
| 164 | } |
| 165 | |
| 166 | static void nop_include_path(char *path) |
| 167 | { |
| 168 | (void)path; |
| 169 | } |
| 170 | |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 171 | static void nop_error_list_macros(int severity) |
| 172 | { |
| 173 | (void)severity; |
| 174 | } |
| 175 | |
H. Peter Anvin | e746971 | 2016-02-18 02:20:59 -0800 | [diff] [blame] | 176 | const struct preproc_ops preproc_nop = { |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 177 | nop_reset, |
| 178 | nop_getline, |
| 179 | nop_cleanup, |
| 180 | nop_extra_stdmac, |
| 181 | nop_pre_define, |
| 182 | nop_pre_undefine, |
| 183 | nop_pre_include, |
H. Peter Anvin | 4def1a8 | 2016-05-09 13:59:44 -0700 | [diff] [blame] | 184 | nop_include_path, |
| 185 | nop_error_list_macros, |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 186 | }; |