Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 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; |
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 | { |
| 62 | src_set_fname(nasm_strdup(file)); |
| 63 | src_set_linnum(0); |
| 64 | nop_lineinc = 1; |
| 65 | nop_fp = fopen(file, "r"); |
| 66 | |
| 67 | if (!nop_fp) |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 68 | nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 69 | (void)pass; /* placate compilers */ |
| 70 | |
| 71 | if (deplist) { |
| 72 | StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next); |
| 73 | sl->next = NULL; |
| 74 | strcpy(sl->str, file); |
| 75 | *deplist = sl; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static char *nop_getline(void) |
| 80 | { |
| 81 | char *buffer, *p, *q; |
| 82 | int bufsize; |
| 83 | |
| 84 | bufsize = BUF_DELTA; |
| 85 | buffer = nasm_malloc(BUF_DELTA); |
| 86 | src_set_linnum(src_get_linnum() + nop_lineinc); |
| 87 | |
| 88 | while (1) { /* Loop to handle %line */ |
| 89 | |
| 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)); |
| 122 | if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) { |
| 123 | nasm_free(src_set_fname(nm)); |
| 124 | src_set_linnum(ln); |
| 125 | nop_lineinc = li; |
| 126 | continue; |
| 127 | } |
| 128 | nasm_free(nm); |
| 129 | } |
| 130 | break; |
| 131 | } |
| 132 | |
H. Peter Anvin | 130736c | 2016-02-17 20:27:41 -0800 | [diff] [blame] | 133 | nasmlist->line(LIST_READ, buffer); |
Cyrill Gorcunov | b5e8fec | 2012-05-07 11:34:27 +0400 | [diff] [blame] | 134 | |
| 135 | return buffer; |
| 136 | } |
| 137 | |
| 138 | static void nop_cleanup(int pass) |
| 139 | { |
| 140 | (void)pass; /* placate GCC */ |
| 141 | if (nop_fp) { |
| 142 | fclose(nop_fp); |
| 143 | nop_fp = NULL; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | static void nop_extra_stdmac(macros_t *macros) |
| 148 | { |
| 149 | (void)macros; |
| 150 | } |
| 151 | |
| 152 | static void nop_pre_define(char *definition) |
| 153 | { |
| 154 | (void)definition; |
| 155 | } |
| 156 | |
| 157 | static void nop_pre_undefine(char *definition) |
| 158 | { |
| 159 | (void)definition; |
| 160 | } |
| 161 | |
| 162 | static void nop_pre_include(char *fname) |
| 163 | { |
| 164 | (void)fname; |
| 165 | } |
| 166 | |
| 167 | static void nop_include_path(char *path) |
| 168 | { |
| 169 | (void)path; |
| 170 | } |
| 171 | |
| 172 | struct preproc_ops preproc_nop = { |
| 173 | nop_reset, |
| 174 | nop_getline, |
| 175 | nop_cleanup, |
| 176 | nop_extra_stdmac, |
| 177 | nop_pre_define, |
| 178 | nop_pre_undefine, |
| 179 | nop_pre_include, |
| 180 | nop_include_path |
| 181 | }; |