raw-preproc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Raw preprocessor (preforms NO preprocessing)
  3. *
  4. * Copyright (C) 2001-2007 Peter Johnson
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  19. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. * POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include <util.h>
  28. #include <libyasm.h>
  29. #define BSIZE 512
  30. typedef struct yasm_preproc_raw {
  31. yasm_preproc_base preproc; /* base structure */
  32. FILE *in;
  33. yasm_linemap *cur_lm;
  34. yasm_errwarns *errwarns;
  35. } yasm_preproc_raw;
  36. yasm_preproc_module yasm_raw_LTX_preproc;
  37. static yasm_preproc *
  38. raw_preproc_create(const char *in_filename, yasm_symtab *symtab,
  39. yasm_linemap *lm, yasm_errwarns *errwarns)
  40. {
  41. FILE *f;
  42. yasm_preproc_raw *preproc_raw = yasm_xmalloc(sizeof(yasm_preproc_raw));
  43. if (strcmp(in_filename, "-") != 0) {
  44. f = fopen(in_filename, "r");
  45. if (!f)
  46. yasm__fatal_missing_input_file( N_("Could not open input file"), in_filename );
  47. }
  48. else
  49. f = stdin;
  50. preproc_raw->preproc.module = &yasm_raw_LTX_preproc;
  51. preproc_raw->in = f;
  52. preproc_raw->cur_lm = lm;
  53. preproc_raw->errwarns = errwarns;
  54. return (yasm_preproc *)preproc_raw;
  55. }
  56. static void
  57. raw_preproc_destroy(yasm_preproc *preproc)
  58. {
  59. yasm_xfree(preproc);
  60. }
  61. static char *
  62. raw_preproc_get_line(yasm_preproc *preproc)
  63. {
  64. yasm_preproc_raw *preproc_raw = (yasm_preproc_raw *)preproc;
  65. int bufsize = BSIZE;
  66. char *buf = yasm_xmalloc((size_t)bufsize);
  67. char *p;
  68. /* Loop to ensure entire line is read (don't want to limit line length). */
  69. p = buf;
  70. for (;;) {
  71. if (!fgets(p, bufsize-(p-buf), preproc_raw->in)) {
  72. if (ferror(preproc_raw->in)) {
  73. yasm_error_set(YASM_ERROR_IO,
  74. N_("error when reading from file"));
  75. yasm_errwarn_propagate(preproc_raw->errwarns,
  76. yasm_linemap_get_current(preproc_raw->cur_lm));
  77. }
  78. break;
  79. }
  80. p += strlen(p);
  81. if (p > buf && p[-1] == '\n')
  82. break;
  83. if ((p-buf)+1 >= bufsize) {
  84. /* Increase size of buffer */
  85. char *oldbuf = buf;
  86. bufsize *= 2;
  87. buf = yasm_xrealloc(buf, (size_t)bufsize);
  88. p = buf + (p-oldbuf);
  89. }
  90. }
  91. if (p == buf) {
  92. /* No data; must be at EOF */
  93. yasm_xfree(buf);
  94. return NULL;
  95. }
  96. /* Strip the line ending */
  97. buf[strcspn(buf, "\r\n")] = '\0';
  98. return buf;
  99. }
  100. static size_t
  101. raw_preproc_get_included_file(yasm_preproc *preproc, char *buf,
  102. size_t max_size)
  103. {
  104. /* no included files */
  105. return 0;
  106. }
  107. static void
  108. raw_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
  109. {
  110. /* no pre-include files */
  111. }
  112. static void
  113. raw_preproc_predefine_macro(yasm_preproc *preproc, const char *macronameval)
  114. {
  115. /* no pre-defining macros */
  116. }
  117. static void
  118. raw_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname)
  119. {
  120. /* no undefining macros */
  121. }
  122. static void
  123. raw_preproc_define_builtin(yasm_preproc *preproc, const char *macronameval)
  124. {
  125. /* no builtin defines */
  126. }
  127. static void
  128. raw_preproc_add_standard(yasm_preproc *preproc, const char **macros)
  129. {
  130. /* no standard macros */
  131. }
  132. /* Define preproc structure -- see preproc.h for details */
  133. yasm_preproc_module yasm_raw_LTX_preproc = {
  134. "Disable preprocessing",
  135. "raw",
  136. raw_preproc_create,
  137. raw_preproc_destroy,
  138. raw_preproc_get_line,
  139. raw_preproc_get_included_file,
  140. raw_preproc_add_include_file,
  141. raw_preproc_predefine_macro,
  142. raw_preproc_undefine_macro,
  143. raw_preproc_define_builtin,
  144. raw_preproc_add_standard
  145. };