summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCyrill Gorcunov <gorcunov@gmail.com>2010-07-13 11:27:41 +0400
committerCyrill Gorcunov <gorcunov@gmail.com>2010-07-13 21:17:10 +0400
commit15bdc51187d32b8dd34e7809bc6944ff4778d1bb (patch)
tree2999c83206b540b3b2ee274a636caf2c4ce63432
parent924df0d498d8ab19958faa27cb023ee8fba52c3d (diff)
downloadnasm-15bdc51187d32b8dd34e7809bc6944ff4778d1bb.tar.gz
preproc: Extract reading line from predefined macros from read_line
It makes read_line less complex Signed-off-by: Cyrill Gorcunov <gorcunov@gmail.com>
-rw-r--r--preproc.c143
1 files changed, 83 insertions, 60 deletions
diff --git a/preproc.c b/preproc.c
index 9d560b9d..be321f33 100644
--- a/preproc.c
+++ b/preproc.c
@@ -680,6 +680,80 @@ hash_findix(struct hash_table *hash, const char *str)
return p ? *p : NULL;
}
+/*
+ * read line from standart macros set,
+ * if there no more left -- return NULL
+ */
+static char *line_from_stdmac(void)
+{
+ unsigned char c;
+ const unsigned char *p = stdmacpos;
+ char *line, *q;
+ size_t len = 0;
+
+ if (!stdmacpos)
+ return NULL;
+
+ while ((c = *p++)) {
+ if (c >= 0x80)
+ len += pp_directives_len[c - 0x80] + 1;
+ else
+ len++;
+ }
+
+ line = nasm_malloc(len + 1);
+ q = line;
+ while ((c = *stdmacpos++)) {
+ if (c >= 0x80) {
+ memcpy(q, pp_directives[c - 0x80], pp_directives_len[c - 0x80]);
+ q += pp_directives_len[c - 0x80];
+ *q++ = ' ';
+ } else {
+ *q++ = c;
+ }
+ }
+ stdmacpos = p;
+ *q = '\0';
+
+ if (!*stdmacpos) {
+ /* This was the last of the standard macro chain... */
+ stdmacpos = NULL;
+ if (any_extrastdmac) {
+ stdmacpos = extrastdmac;
+ any_extrastdmac = false;
+ } else if (do_predef) {
+ Line *pd, *l;
+ Token *head, **tail, *t;
+
+ /*
+ * Nasty hack: here we push the contents of
+ * `predef' on to the top-level expansion stack,
+ * since this is the most convenient way to
+ * implement the pre-include and pre-define
+ * features.
+ */
+ list_for_each(pd, predef) {
+ head = NULL;
+ tail = &head;
+ list_for_each(t, pd->first) {
+ *tail = new_Token(NULL, t->type, t->text, 0);
+ tail = &(*tail)->next;
+ }
+
+ l = nasm_malloc(sizeof(Line));
+ l->next = istk->expansion;
+ l->first = head;
+ l->finishes = NULL;
+
+ istk->expansion = l;
+ }
+ do_predef = false;
+ }
+ }
+
+ return line;
+}
+
#define BUF_DELTA 512
/*
* Read a line from the top file in istk, handling multiple CR/LFs
@@ -692,67 +766,16 @@ static char *read_line(void)
char *buffer, *p, *q;
int bufsize, continued_count;
- if (stdmacpos) {
- unsigned char c;
- const unsigned char *p = stdmacpos;
- char *ret, *q;
- size_t len = 0;
- while ((c = *p++)) {
- if (c >= 0x80)
- len += pp_directives_len[c-0x80]+1;
- else
- len++;
- }
- ret = nasm_malloc(len+1);
- q = ret;
- while ((c = *stdmacpos++)) {
- if (c >= 0x80) {
- memcpy(q, pp_directives[c-0x80], pp_directives_len[c-0x80]);
- q += pp_directives_len[c-0x80];
- *q++ = ' ';
- } else {
- *q++ = c;
- }
- }
- stdmacpos = p;
- *q = '\0';
-
- if (!*stdmacpos) {
- /* This was the last of the standard macro chain... */
- stdmacpos = NULL;
- if (any_extrastdmac) {
- stdmacpos = extrastdmac;
- any_extrastdmac = false;
- } else if (do_predef) {
- Line *pd, *l;
- Token *head, **tail, *t;
-
- /*
- * Nasty hack: here we push the contents of
- * `predef' on to the top-level expansion stack,
- * since this is the most convenient way to
- * implement the pre-include and pre-define
- * features.
- */
- list_for_each(pd, predef) {
- head = NULL;
- tail = &head;
- list_for_each(t, pd->first) {
- *tail = new_Token(NULL, t->type, t->text, 0);
- tail = &(*tail)->next;
- }
- l = nasm_malloc(sizeof(Line));
- l->next = istk->expansion;
- l->first = head;
- l->finishes = NULL;
- istk->expansion = l;
- }
- do_predef = false;
- }
- }
- return ret;
- }
+ /*
+ * standart macros set (predefined) goes first
+ */
+ p = line_from_stdmac();
+ if (p)
+ return p;
+ /*
+ * regular read from a file
+ */
bufsize = BUF_DELTA;
buffer = nasm_malloc(BUF_DELTA);
p = buffer;