aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/mk/word.c
blob: ac34c47be4aa35a5dc3f85131448439b52a341e3 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include	"mk.h"

static	Word	*nextword(char**);

Word*
newword(char *s)
{
	Word *w;

	w = (Word *)Malloc(sizeof(Word));
	w->s = strdup(s);
	w->next = 0;
	return(w);
}

Word *
stow(char *s)
{
	Word *head, *w, *new;

	w = head = 0;
	while(*s){
		new = nextword(&s);
		if(new == 0)
			break;
		if (w)
			w->next = new;
		else
			head = w = new;
		while(w->next)
			w = w->next;
		
	}
	if (!head)
		head = newword("");
	return(head);
}

char *
wtos(Word *w, int sep)
{
	Bufblock *buf;
	char *cp;

	buf = newbuf();
	for(; w; w = w->next){
		for(cp = w->s; *cp; cp++)
			insert(buf, *cp);
		if(w->next)
			insert(buf, sep);
	}
	insert(buf, 0);
	cp = strdup(buf->start);
	freebuf(buf);
	return(cp);
}

Word*
wdup(Word *w)
{
	Word *v, *new, *base;

	v = base = 0;
	while(w){
		new = newword(w->s);
		if(v)
			v->next = new;
		else
			base = new;
		v = new;
		w = w->next;
	}
	return base;
}

void
delword(Word *w)
{
	Word *v;

	while(v = w){
		w = w->next;
		if(v->s)
			free(v->s);
		free(v);
	}
}

/*
 *	break out a word from a string handling quotes, executions,
 *	and variable expansions.
 */
static Word*
nextword(char **s)
{
	Bufblock *b;
	Word *head, *tail, *w;
	Rune r;
	char *cp;

	cp = *s;
	b = newbuf();
	head = tail = 0;
	while(*cp == ' ' || *cp == '\t')		/* leading white space */
		cp++;
	while(*cp){
		cp += chartorune(&r, cp);
		switch(r)
		{
		case ' ':
		case '\t':
		case '\n':
			goto out;
		case '\\':
		case '\'':
		case '"':
			cp = expandquote(cp, r, b);
			if(cp == 0){
				fprint(2, "missing closing quote: %s\n", *s);
				Exit();
			}
			break;
		case '$':
			w = varsub(&cp);
			if(w == 0)
				break;
			if(b->current != b->start){
				bufcpy(b, w->s, strlen(w->s));
				insert(b, 0);
				free(w->s);
				w->s = strdup(b->start);
				b->current = b->start;
			}
			if(head){
				bufcpy(b, tail->s, strlen(tail->s));
				bufcpy(b, w->s, strlen(w->s));
				insert(b, 0);
				free(tail->s);
				tail->s = strdup(b->start);
				tail->next = w->next;
				free(w->s);
				free(w);
				b->current = b->start;
			} else
				tail = head = w;
			while(tail->next)
				tail = tail->next;
			break;
		default:
			rinsert(b, r);
			break;
		}
	}
out:
	*s = cp;
	if(b->current != b->start){
		if(head){
			cp = b->current;
			bufcpy(b, tail->s, strlen(tail->s));
			bufcpy(b, b->start, cp-b->start);
			insert(b, 0);
			free(tail->s);
			tail->s = strdup(cp);
		} else {
			insert(b, 0);
			head = newword(b->start);
		}
	}
	freebuf(b);
	return head;
}

void
dumpw(char *s, Word *w)
{
	Bprint(&bout, "%s", s);
	for(; w; w = w->next)
		Bprint(&bout, " '%s'", w->s);
	Bputc(&bout, '\n');
}