• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 #include <libyasm.h>
30 
31 
32 #define BSIZE 512
33 
34 typedef struct yasm_preproc_raw {
35     yasm_preproc_base preproc;   /* base structure */
36 
37     FILE *in;
38     yasm_linemap *cur_lm;
39     yasm_errwarns *errwarns;
40 } yasm_preproc_raw;
41 
42 yasm_preproc_module yasm_raw_LTX_preproc;
43 
44 static yasm_preproc *
raw_preproc_create(const char * in_filename,yasm_symtab * symtab,yasm_linemap * lm,yasm_errwarns * errwarns)45 raw_preproc_create(const char *in_filename, yasm_symtab *symtab,
46                    yasm_linemap *lm, yasm_errwarns *errwarns)
47 {
48     FILE *f;
49     yasm_preproc_raw *preproc_raw = yasm_xmalloc(sizeof(yasm_preproc_raw));
50 
51     if (strcmp(in_filename, "-") != 0) {
52         f = fopen(in_filename, "r");
53         if (!f)
54             yasm__fatal( N_("Could not open input file") );
55     }
56     else
57         f = stdin;
58 
59     preproc_raw->preproc.module = &yasm_raw_LTX_preproc;
60     preproc_raw->in = f;
61     preproc_raw->cur_lm = lm;
62     preproc_raw->errwarns = errwarns;
63 
64     return (yasm_preproc *)preproc_raw;
65 }
66 
67 static void
raw_preproc_destroy(yasm_preproc * preproc)68 raw_preproc_destroy(yasm_preproc *preproc)
69 {
70     yasm_xfree(preproc);
71 }
72 
73 static char *
raw_preproc_get_line(yasm_preproc * preproc)74 raw_preproc_get_line(yasm_preproc *preproc)
75 {
76     yasm_preproc_raw *preproc_raw = (yasm_preproc_raw *)preproc;
77     int bufsize = BSIZE;
78     char *buf = yasm_xmalloc((size_t)bufsize);
79     char *p;
80 
81     /* Loop to ensure entire line is read (don't want to limit line length). */
82     p = buf;
83     for (;;) {
84         if (!fgets(p, bufsize-(p-buf), preproc_raw->in)) {
85             if (ferror(preproc_raw->in)) {
86                 yasm_error_set(YASM_ERROR_IO,
87                                N_("error when reading from file"));
88                 yasm_errwarn_propagate(preproc_raw->errwarns,
89                     yasm_linemap_get_current(preproc_raw->cur_lm));
90             }
91             break;
92         }
93         p += strlen(p);
94         if (p > buf && p[-1] == '\n')
95             break;
96         if ((p-buf)+1 >= bufsize) {
97             /* Increase size of buffer */
98             char *oldbuf = buf;
99             bufsize *= 2;
100             buf = yasm_xrealloc(buf, (size_t)bufsize);
101             p = buf + (p-oldbuf);
102         }
103     }
104 
105     if (p == buf) {
106         /* No data; must be at EOF */
107         yasm_xfree(buf);
108         return NULL;
109     }
110 
111     /* Strip the line ending */
112     buf[strcspn(buf, "\r\n")] = '\0';
113 
114     return buf;
115 }
116 
117 static size_t
raw_preproc_get_included_file(yasm_preproc * preproc,char * buf,size_t max_size)118 raw_preproc_get_included_file(yasm_preproc *preproc, char *buf,
119                               size_t max_size)
120 {
121     /* no included files */
122     return 0;
123 }
124 
125 static void
raw_preproc_add_include_file(yasm_preproc * preproc,const char * filename)126 raw_preproc_add_include_file(yasm_preproc *preproc, const char *filename)
127 {
128     /* no pre-include files */
129 }
130 
131 static void
raw_preproc_predefine_macro(yasm_preproc * preproc,const char * macronameval)132 raw_preproc_predefine_macro(yasm_preproc *preproc, const char *macronameval)
133 {
134     /* no pre-defining macros */
135 }
136 
137 static void
raw_preproc_undefine_macro(yasm_preproc * preproc,const char * macroname)138 raw_preproc_undefine_macro(yasm_preproc *preproc, const char *macroname)
139 {
140     /* no undefining macros */
141 }
142 
143 static void
raw_preproc_define_builtin(yasm_preproc * preproc,const char * macronameval)144 raw_preproc_define_builtin(yasm_preproc *preproc, const char *macronameval)
145 {
146     /* no builtin defines */
147 }
148 
149 static void
raw_preproc_add_standard(yasm_preproc * preproc,const char ** macros)150 raw_preproc_add_standard(yasm_preproc *preproc, const char **macros)
151 {
152     /* no standard macros */
153 }
154 
155 
156 /* Define preproc structure -- see preproc.h for details */
157 yasm_preproc_module yasm_raw_LTX_preproc = {
158     "Disable preprocessing",
159     "raw",
160     raw_preproc_create,
161     raw_preproc_destroy,
162     raw_preproc_get_line,
163     raw_preproc_get_included_file,
164     raw_preproc_add_include_file,
165     raw_preproc_predefine_macro,
166     raw_preproc_undefine_macro,
167     raw_preproc_define_builtin,
168     raw_preproc_add_standard
169 };
170