aboutsummaryrefslogtreecommitdiff
path: root/src/preproc.c
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2011-04-30 16:40:46 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2011-04-30 17:22:32 +0300
commita3599c1135e2eefe9bb0d910694150feddfa5439 (patch)
treecb476381d039ee526fbc779c0678936cc3a1f419 /src/preproc.c
parentbd4a203ab453d78e87e29d11017b35248c9babca (diff)
downloadgrecs-a3599c1135e2eefe9bb0d910694150feddfa5439.tar.gz
grecs-a3599c1135e2eefe9bb0d910694150feddfa5439.tar.bz2
Drop all dependencies from gnulib.
* gnulib.modules: Remove. * src/mem.c: New file. * src/symtab.c: New file. * src/Makefile.am (libgrecs_a_SOURCES): Add mem.c and symtab.c. (INCLUDES): Remove -Ignu * src/grecs-gram.y (grecs_vasprintf, grecs_asprintf): New functions. (grecs_warning, grecs_error): Use grecs_vasprintf. (string_to_signed, string_to_unsigned): Remove. (STRTONUM,STRxTONUM,GETUNUM,GETSNUM): New macros (from Mailutils). (grecs_string_convert): Use macros for numeric conversions. (grecs_prop_tab): Remove entries for uintmax_t and intmax_t. * src/grecs-lex.l: Drop dependency on obstack. (line_acc, string_list): New statics. (line_acc_free_entry,line_acc_add_string): new function (line_acc_add_char,list_acc_unescape_char): new function (line_add_unescape_last): new function (grecs_lex_begin,grecs_line_add, multiline_begin) (grecs_line_finish): Rewrite. * src/grecs.h (grecs_type_uintmax,grecs_type_intmax): Remove. (grecs_malloc_fun,grecs_realloc_fun,grecs_alloc_die_fun): New externs. (grecs_malloc,grecs_realloc,grecs_alloc_die,grecs_strdup) (grecs_list_clear,grecs_list_free) (grecs_vasprintf,grecs_asprintf): New protos. (grecs_symtab,grecs_syment): New structs. (grecs_symtab_enumerator_t): New type. (grecs_symtab_strerror,grecs_symtab_lookup_or_install) (grecs_symtab_clear,grecs_symtab_create) (grecs_symtab_create_default) (grecs_symtab_free,grecs_symtab_remove,grecs_symtab_replace) (grecs_symtab_enumerate,grecs_symtab_count_entries): New protos. * src/list.c (grecs_list_clear): New function. (grecs_list_free): Use grecs_list_clear. * src/preproc.c (linebufbase,linebufsize): New variables. (pp_getline): New function. (pp_line_stmt_size): Remove. (pp_line_stmt): Use grecs_asprintf to format data. (next_line,grecs_preproc_extrn_start): Use pp_getline. (source_lookup): Use grecs_symtab instead of hash. * src/text.c: Rewrite using grecs_symtab.
Diffstat (limited to 'src/preproc.c')
-rw-r--r--src/preproc.c165
1 files changed, 93 insertions, 72 deletions
diff --git a/src/preproc.c b/src/preproc.c
index 9cdea3c..a71c93f 100644
--- a/src/preproc.c
+++ b/src/preproc.c
@@ -29,18 +29,8 @@
29#include <errno.h> 29#include <errno.h>
30#include <signal.h> 30#include <signal.h>
31 31
32#include <xalloc.h>
33#include <hash.h>
34#include <inttostr.h>
35#include <wordsplit.h> 32#include <wordsplit.h>
36 33
37#if ENABLE_NLS
38# include "gettext.h"
39# define _(msgid) gettext (msgid)
40#else
41# define _(msgid) msgid
42#endif
43
44int grecs_log_to_stderr = 1; 34int grecs_log_to_stderr = 1;
45void (*grecs_log_setup_hook) () = NULL; 35void (*grecs_log_setup_hook) () = NULL;
46 36
@@ -62,6 +52,8 @@ struct buffer_ctx
62 52
63extern int yy_grecs_flex_debug; 53extern int yy_grecs_flex_debug;
64static struct buffer_ctx *context_stack; 54static struct buffer_ctx *context_stack;
55static char *linebufbase = NULL;
56static size_t linebufsize = 0;
65 57
66#define INFILE context_stack->infile 58#define INFILE context_stack->infile
67#define LOCUS context_stack->locus 59#define LOCUS context_stack->locus
@@ -76,6 +68,49 @@ static int push_source (const char *name, int once);
76static int pop_source (void); 68static int pop_source (void);
77static int parse_include (const char *text, int once); 69static int parse_include (const char *text, int once);
78 70
71static ssize_t
72pp_getline (char **pbuf, size_t *psize, FILE *fp)
73{
74 char *buf = *pbuf;
75 size_t size = *psize;
76 ssize_t off = 0;
77
78 do
79 {
80 size_t len;
81
82 if (off == size - 1)
83 {
84 if (!buf)
85 {
86 size = 1;
87 buf = grecs_malloc (size);
88 }
89 else
90 {
91 size_t nsize = 2 * size;
92 if (nsize < size)
93 grecs_alloc_die ();
94 buf = grecs_realloc (buf, nsize);
95 size = nsize;
96 }
97 }
98 if (!fgets (buf + off, size - off, fp))
99 {
100 if (off == 0)
101 off = -1;
102 break;
103 }
104 off += strlen (buf + off);
105 }
106 while (buf[off - 1] != '\n');
107
108 *pbuf = buf;
109 *psize = size;
110 return off;
111}
112
113
79static void 114static void
80putback (const char *str) 115putback (const char *str)
81{ 116{
@@ -87,51 +122,34 @@ putback (const char *str)
87 if (len > putback_max) 122 if (len > putback_max)
88 { 123 {
89 putback_max = len; 124 putback_max = len;
90 putback_buffer = xrealloc (putback_buffer, putback_max); 125 putback_buffer = grecs_realloc (putback_buffer, putback_max);
91 } 126 }
92 strcpy (putback_buffer, str); 127 strcpy (putback_buffer, str);
93 putback_size = len - 1; 128 putback_size = len - 1;
94} 129}
95 130
96/* Compute the size of the line
97
98 #line NNN "FILENAME"
99*/
100static size_t
101pp_line_stmt_size ()
102{
103 char lbuf[INT_BUFSIZE_BOUND(uintmax_t)];
104 char xbuf[INT_BUFSIZE_BOUND(uintmax_t)];
105 char *lp, *xp;
106
107 lp = umaxtostr (LOCUS.line, lbuf);
108 xp = umaxtostr (context_stack->xlines + 1, xbuf);
109 if (context_stack->namelen == 0)
110 context_stack->namelen = strlen (LOCUS.file);
111 /* "#line " is 6 chars, two more spaces, two quotes and a linefeed
112 make another 5, summa facit 11 */
113 return 11 + strlen (lp) + strlen (xp) + context_stack->namelen;
114}
115
116static void 131static void
117pp_line_stmt () 132pp_line_stmt ()
118{ 133{
119 char *p; 134 size_t ls_size;
120 size_t ls_size = pp_line_stmt_size (); 135 size_t pb_size;
121 size_t pb_size = putback_size + ls_size + 1; 136
137 if (grecs_asprintf (&linebufbase, &linebufsize, "#line %lu \"%s\" %lu\n",
138 (unsigned long) LOCUS.line,
139 LOCUS.file, (unsigned long) context_stack->xlines))
140 grecs_alloc_die ();
141
142 ls_size = strlen (linebufbase);
143 pb_size = putback_size + ls_size + 1;
122 144
123 if (pb_size > putback_max) 145 if (pb_size > putback_max)
124 { 146 {
125 putback_max = pb_size; 147 putback_max = pb_size;
126 putback_buffer = xrealloc (putback_buffer, putback_max); 148 putback_buffer = grecs_realloc (putback_buffer, putback_max);
127 } 149 }
128 150
129 p = putback_buffer + putback_size;
130 context_stack->xlines++; 151 context_stack->xlines++;
131 snprintf (p, putback_max - putback_size, 152 strcpy (putback_buffer + putback_size, linebufbase);
132 "#line %lu \"%s\" %lu\n",
133 (unsigned long) LOCUS.line,
134 LOCUS.file, (unsigned long) context_stack->xlines);
135 putback_size += ls_size; 153 putback_size += ls_size;
136} 154}
137 155
@@ -151,7 +169,7 @@ next_line ()
151 if (putback_size + 1 > bufsize) 169 if (putback_size + 1 > bufsize)
152 { 170 {
153 bufsize = putback_size + 1; 171 bufsize = putback_size + 1;
154 linebuf = xrealloc (linebuf, bufsize); 172 linebuf = grecs_realloc (linebuf, bufsize);
155 } 173 }
156 strcpy (linebuf, putback_buffer); 174 strcpy (linebuf, putback_buffer);
157 rc = putback_size; 175 rc = putback_size;
@@ -160,7 +178,7 @@ next_line ()
160 else if (!context_stack) 178 else if (!context_stack)
161 return 0; 179 return 0;
162 else 180 else
163 rc = getline (&linebuf, &bufsize, INFILE); 181 rc = pp_getline (&linebuf, &bufsize, INFILE);
164 } 182 }
165 while (rc == -1 && pop_source () == 0); 183 while (rc == -1 && pop_source () == 0);
166 return rc; 184 return rc;
@@ -264,7 +282,7 @@ pp_list_find (struct grecs_list *list, struct file_data *dptr)
264 if (size > dptr->buflen) 282 if (size > dptr->buflen)
265 { 283 {
266 dptr->buflen = size; 284 dptr->buflen = size;
267 dptr->buf = xrealloc (dptr->buf, dptr->buflen); 285 dptr->buf = grecs_realloc (dptr->buf, dptr->buflen);
268 } 286 }
269 strcpy (dptr->buf, dir); 287 strcpy (dptr->buf, dir);
270 strcat (dptr->buf, "/"); 288 strcat (dptr->buf, "/");
@@ -285,7 +303,7 @@ grecs_include_path_setup_v (char **dirs)
285 int i; 303 int i;
286 for (i = 0; dirs[i]; i++) 304 for (i = 0; dirs[i]; i++)
287 /* FIXME: Element never freed */ 305 /* FIXME: Element never freed */
288 grecs_list_append (std_include_path, xstrdup (dirs[i])); 306 grecs_list_append (std_include_path, grecs_strdup (dirs[i]));
289 } 307 }
290} 308}
291 309
@@ -306,7 +324,9 @@ grecs_include_path_setup (const char *dir, ...)
306 { 324 {
307 if (argc == 0) 325 if (argc == 0)
308 argc = 16; 326 argc = 16;
309 argv = x2nrealloc (argv, &argc, sizeof (argv[0])); 327 else
328 argc += 16;
329 argv = grecs_realloc (argv, argc * sizeof (argv[0]));
310 } 330 }
311 argv[argi++] = (char*) p; 331 argv[argi++] = (char*) p;
312 if (!p) 332 if (!p)
@@ -326,23 +346,23 @@ grecs_preproc_add_include_dir (char *dir)
326 grecs_list_append (include_path, dir); 346 grecs_list_append (include_path, dir);
327} 347}
328 348
329static Hash_table *incl_sources; 349static struct grecs_symtab *incl_sources;
330 350
331/* Calculate the hash of a struct input_file_ident. */ 351/* Calculate the hash of a struct input_file_ident. */
332static size_t 352static unsigned
333incl_hasher (void const *data, size_t n_buckets) 353incl_hasher (void *data, unsigned long n_buckets)
334{ 354{
335 const struct input_file_ident *id = data; 355 const struct input_file_ident *id = data;
336 return (id->i_node + id->device) % n_buckets; 356 return (id->i_node + id->device) % n_buckets;
337} 357}
338 358<