preproc-nop.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2016 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. * This is a null preprocessor which just copies lines from input
  35. * to output. It's used when someone explicitly requests that NASM
  36. * not preprocess their source file.
  37. */
  38. #include "compiler.h"
  39. #include <stdio.h>
  40. #include <stdarg.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ctype.h>
  44. #include <limits.h>
  45. #include <time.h>
  46. #include "nasm.h"
  47. #include "nasmlib.h"
  48. #include "error.h"
  49. #include "preproc.h"
  50. #include "listing.h"
  51. #define BUF_DELTA 512
  52. static FILE *nop_fp;
  53. static int32_t nop_lineinc;
  54. static void nop_init(void)
  55. {
  56. /* Nothing to do */
  57. }
  58. static void nop_reset(const char *file, int pass, StrList **deplist)
  59. {
  60. src_set(0, file);
  61. nop_lineinc = 1;
  62. nop_fp = nasm_open_read(file, NF_TEXT);
  63. if (!nop_fp)
  64. nasm_fatal(ERR_NOFILE, "unable to open input file `%s'", file);
  65. (void)pass; /* placate compilers */
  66. nasm_add_string_to_strlist(deplist, file);
  67. }
  68. static char *nop_getline(void)
  69. {
  70. char *buffer, *p, *q;
  71. int bufsize;
  72. bufsize = BUF_DELTA;
  73. buffer = nasm_malloc(BUF_DELTA);
  74. src_set_linnum(src_get_linnum() + nop_lineinc);
  75. while (1) { /* Loop to handle %line */
  76. p = buffer;
  77. while (1) { /* Loop to handle long lines */
  78. q = fgets(p, bufsize - (p - buffer), nop_fp);
  79. if (!q)
  80. break;
  81. p += strlen(p);
  82. if (p > buffer && p[-1] == '\n')
  83. break;
  84. if (p - buffer > bufsize - 10) {
  85. int offset;
  86. offset = p - buffer;
  87. bufsize += BUF_DELTA;
  88. buffer = nasm_realloc(buffer, bufsize);
  89. p = buffer + offset;
  90. }
  91. }
  92. if (!q && p == buffer) {
  93. nasm_free(buffer);
  94. return NULL;
  95. }
  96. /*
  97. * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
  98. * them are present at the end of the line.
  99. */
  100. buffer[strcspn(buffer, "\r\n\032")] = '\0';
  101. if (!nasm_strnicmp(buffer, "%line", 5)) {
  102. int32_t ln;
  103. int li;
  104. char *nm = nasm_malloc(strlen(buffer));
  105. int conv = sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm);
  106. if (conv >= 2) {
  107. if (!pp_noline)
  108. src_set(ln, conv >= 3 ? nm : NULL);
  109. nop_lineinc = li;
  110. }
  111. nasm_free(nm);
  112. if (conv >= 2)
  113. continue;
  114. }
  115. break;
  116. }
  117. lfmt->line(LIST_READ, buffer);
  118. return buffer;
  119. }
  120. static void nop_cleanup(int pass)
  121. {
  122. (void)pass; /* placate GCC */
  123. if (nop_fp) {
  124. fclose(nop_fp);
  125. nop_fp = NULL;
  126. }
  127. }
  128. static void nop_extra_stdmac(macros_t *macros)
  129. {
  130. (void)macros;
  131. }
  132. static void nop_pre_define(char *definition)
  133. {
  134. (void)definition;
  135. }
  136. static void nop_pre_undefine(char *definition)
  137. {
  138. (void)definition;
  139. }
  140. static void nop_pre_include(char *fname)
  141. {
  142. (void)fname;
  143. }
  144. static void nop_pre_command(const char *what, char *string)
  145. {
  146. (void)what;
  147. (void)string;
  148. }
  149. static void nop_include_path(char *path)
  150. {
  151. (void)path;
  152. }
  153. static void nop_error_list_macros(int severity)
  154. {
  155. (void)severity;
  156. }
  157. const struct preproc_ops preproc_nop = {
  158. nop_init,
  159. nop_reset,
  160. nop_getline,
  161. nop_cleanup,
  162. nop_extra_stdmac,
  163. nop_pre_define,
  164. nop_pre_undefine,
  165. nop_pre_include,
  166. nop_pre_command,
  167. nop_include_path,
  168. nop_error_list_macros,
  169. };