forked from rakitzis/rc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheredoc.c
158 lines (146 loc) · 3.35 KB
/
heredoc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* heredoc.c: heredoc slurping is done here */
#include "rc.h"
#include "input.h"
struct Hq {
Node *doc;
char *name;
Hq *n;
bool quoted;
} *hq;
static bool dead = FALSE;
/*
* read in a heredocument. A clever trick: skip over any partially matched end-of-file
* marker storing only the number of characters matched. If the whole marker is matched,
* return from readheredoc(). If only part of the marker is matched, copy that part into
* the heredocument.
*
* BUG: if the eof string contains a newline, the state can get confused, and the
* heredoc may continue past where it should. on the other hand, /bin/sh seems to
* never get out of its readheredoc() when the heredoc string contains a newline
*/
static char *readheredoc(char *eof) {
int c;
char *t, *buf, *bufend;
unsigned char *s;
size_t bufsize;
t = buf = nalloc(bufsize = 512);
bufend = &buf[bufsize];
dead = FALSE;
#define RESIZE(extra) { \
char *nbuf; \
bufsize = bufsize * 2 + extra; \
nbuf = nalloc(bufsize); \
memcpy(nbuf, buf, (size_t) (t - buf)); \
t = nbuf + (t - buf); \
buf = nbuf; \
bufend = &buf[bufsize]; \
}
for (;;) {
nextline();
for (s = (unsigned char *) eof; (c = gchar()) == *s; s++)
;
if (*s == '\0' && (c == '\n' || c == EOF)) {
*t++ = '\0';
return buf;
}
if (s != (unsigned char *) eof) {
size_t len = s - (unsigned char *) eof;
if (t + len >= bufend)
RESIZE(len);
memcpy(t, eof, len);
t += len;
}
for (;; c = gchar()) {
if (c == EOF) {
yyerror("heredoc incomplete");
dead = TRUE;
return NULL;
}
if (t + 1 >= bufend)
RESIZE(0);
*t++ = c;
if (c == '\n')
break;
}
}
}
/* parseheredoc -- turn a heredoc with variable references into a node chain */
static Node *parseheredoc(char *s) {
int c = *s;
Node *result = NULL;
while (TRUE) {
Node *node;
switch (c) {
default: {
char *begin = s;
while ((c = *s++) != '\0' && c != '$')
;
*--s = '\0';
node = mk(nWord, begin, NULL);
break;
}
case '$': {
char *begin = ++s, *var;
c = *s++;
if (c == '$') {
node = mk(nWord, "$", NULL);
c = *s;
} else {
size_t len = 0;
do
len++;
while (!dnw[c = *(unsigned char *) s++]);
if (c == '^')
c = *s;
else
s--;
var = nalloc(len + 1);
var[len] = '\0';
memcpy(var, begin, len);
node = mk(nFlat, mk(nWord, var, NULL));
}
break;
}
case '\0':
return result;
}
result = (result == NULL) ? node : mk(nConcat, result, node);
}
}
/* read in heredocs when yyparse hits a newline. called from yyparse */
extern int heredoc(int end) {
Hq *here;
if ((here = hq) != NULL) {
hq = NULL;
if (end) {
yyerror("heredoc incomplete");
return FALSE;
}
do {
Node *n = here->doc;
char *s = readheredoc(here->name);
if (dead)
return FALSE;
n->u[2].p = here->quoted ? mk(nWord, s, NULL, FALSE) : parseheredoc(s);
n->u[0].i = rHerestring;
} while ((here = here->n) != NULL);
}
return TRUE;
}
/* queue pending heredocs into a queue. called from yyparse */
extern int qdoc(Node *name, Node *n) {
Hq *new, **prev;
if (name->type != nWord) {
yyerror("eof-marker not a single literal word");
skiptonl();
return FALSE;
}
for (prev = &hq; (new = *prev) != NULL; prev = &new->n)
;
*prev = new = nnew(Hq);
new->name = name->u[0].s;
new->quoted = name->u[2].i;
new->doc = n;
new->n = NULL;
return TRUE;
}