summary refs log tree commit diff
path: root/msed.c
blob: 4fef8f4fbd3069acdc22a64ccd4b1a45e0f8f142 (plain) (blame)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <sys/stat.h>
#include <sys/types.h>

#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "blaze822.h"

static char *expr;

char *
subst(char *str, char *srch, char *repl, char *flags)
{
	static char buf[4096];
	char *bufe = buf + sizeof buf;

	int dflag = !!strchr(flags, 'd');
	int gflag = !!strchr(flags, 'g');
	int iflag = !!strchr(flags, 'i');

#define APP(o, l) do { if (bufe-b < (ssize_t)l) return str; memcpy(b, str+i+o, l); b += l; } while (0)
#define APPC(c) do { if (b >= bufe) return str; *b++ = c; } while (0)

	regex_t srchrx;
	regmatch_t pmatch[10] = {{0}};
	if (regcomp(&srchrx, srch, iflag ? REG_ICASE : 0) != 0)
		return str;

	char *b = buf;

	regoff_t i = 0;
	while (1) {
		if (regexec(&srchrx, str+i, 9, pmatch, 0) != 0)
			break;

		if (dflag)
			return 0;

		APP(0, pmatch[0].rm_so);

		char *t = repl;
		while (*t) {
			// & == \0
			if (*t == '&' || (*t == '\\' && isdigit(*(t+1)))) {
				int n;
				if (*t == '&') {
					t++;
					n = 0;
				} else {
					t++;
					n = *t++ - '0';
				}

				APP(pmatch[n].rm_so,
				    pmatch[n].rm_eo - pmatch[n].rm_so);
			} else if (*t == '\\' && *(t+1)) {
				t++;
				APPC(*t++);
			} else {
				APPC(*t++);
			}
		}

		i += pmatch[0].rm_eo;  // advance to end of match
		if (!gflag)
			break;
	}

	if (i > 0) { // any match?
		APP(0, strlen(str + i));
		*b = 0;
		return buf;
	}

	return str;
}

void
printhdr(char *hdr, int rest)
{
	int uc = 1;

	while (*hdr && *hdr != ':') {
		putc(uc ? toupper(*hdr) : *hdr, stdout);
		uc = (*hdr == '-');
		hdr++;
	}

	if (rest) {
		printf("%s\n", hdr);
	}
}

void
sed(char *file)
{
	struct message *msg = blaze822_file(file);
	if (!msg)
		return;

	char *h = 0;
	while ((h = blaze822_next_header(msg, h))) {
		regex_t headerrx;
		char headersel[1024];

		char *v = strchr(h, ':');
		if (!v)
			continue;
		v++;
		while (*v && (*v == ' ' || *v == '\t'))
			v++;

		v = strdup(v);

		char *e = expr;
		while (*e) {
			while (*e &&
			    (*e == ' ' || *e == '\t' || *e == '\n' || *e == ';'))
				e++;

			*headersel = 0;
			if (*e == '/') {
				e++;
				char *s = e;
				// parse_headers, sets headersel
				while (*e && *e != '/')
					e++;
				snprintf(headersel, sizeof headersel,
				    "^(%.*s)*:", (int)(e-s), s);
				for (s = headersel; *s && *(s+1); s++)
					if (*s == ':')
						*s = '|';
				int rv;
				if ((rv = regcomp(&headerrx, headersel, REG_EXTENDED)) != 0) {
					char buf[100];
					regerror(rv, &headerrx, buf, sizeof buf);
					fprintf(stderr, "msed: %s\n", buf);
					exit(1);
				}
				if (*e)
					e++;
			}

			char sep;
			char *s;
			if (!*headersel || regexec(&headerrx, h, 0, 0, 0) == 0) {
				switch (*e) {
				case 'd':
					free(v);
					v = 0;
					break;
				case 'a':
					// skipped here;
					e++;
					if ((*e == ' ' || *e == ';' || *e == '\n' || !*e)) {
						break;
					}
					sep = *e++;
					if (!sep) {
						fprintf(stderr, "msed: unterminated a command\n");
						exit(1);
					}
					while (*e && *e != sep)
						e++;
					if (*e == sep)
						e++;
					if (!(*e == ' ' || *e == ';' || *e == '\n' || !*e)) {
						fprintf(stderr, "msed: unterminated a command\n");
						exit(1);
					}
					break;
				case 'c':
					sep = *++e;
					s = ++e;
					while (*e && *e != sep)
						e++;
					free(v);
					v = strndup(s, e-s);
					break;
				case 's':
					sep = *++e;
					s = ++e;
					while (*e && *e != sep)
						e++;
					char *t = ++e;
					while (*e && *e != sep)
						e++;
					char *u = ++e;
					while (*e == 'd' || *e == 'g' || *e == 'i')
						e++;

					if (!(*e == ' ' || *e == ';' || *e == '\n' || !*e)) {
						fprintf(stderr, "msed: unterminated s command\n");
						exit(1);
					}

					// XXX stack allocate
					char *from = strndup(s, t-s-1);
					char *to = strndup(t, u-t-1);
					char *flags = strndup(u, e-u);

					char *ov = v;
					char *r = subst(ov, from, to, flags);
					if (r)
						v = strdup(r);
					else
						v = 0;

					free(ov);
					free(from);
					free(to);
					free(flags);

					break;
				case '\0':
					break;
				default:
					fprintf(stderr, "msed: unknown command: '%c'\n", *e);
					exit(1);
				}
			}
			while (*e && *e != ';' && *e != '\n')
				e++;
		}
		if (v) {
			printhdr(h, 0);
			printf(": %s\n", v);
			free(v);
		}
	}

	// loop, do all a//

	char *hs, *he;
	char *e = expr;
	while (*e) {
		while (*e &&
		    (*e == ' ' || *e == '\t' || *e == '\n' || *e == ';'))
			e++;

		hs = he = 0;
		if (*e == '/') {
			e++;
			hs = e;
			// parse_headers, sets headersel
			while (*e && *e != '/')
				e++;
			he = e;
			if (*e)
				e++;
		}

		char sep;
		char *s;
		char *h = 0;
		char *v = 0;
		switch (*e) {
		case 'a':
			if (he != hs) {
				h = strndup(hs, he-hs);
			} else {
				fprintf(stderr, "msed: used command a without header name\n");
				exit(1);
			}

			e++;
			if (*e == ' ' || *e == '\t' || *e == '\n' || *e == ';' || !*e) {
				fprintf(stderr, "msed: no header value for %s\n", h);
				exit(1);
			} else {
				sep = *e;
				if (!sep) {
					fprintf(stderr, "msed: unterminated a command\n");
					exit(1);
				}
				s = ++e;
				while (*e && *e != sep)
					e++;
				v = strndup(s, e-s);
			}

			if (blaze822_chdr(msg, h)) {
				free(h);
				free(v);
				break;
			}

			printhdr(h, 0);
			printf(": %s\n", v);
			free(h);
			free(v);

			break;

		case 'c':
		case 'd':
		case 's':
			// ignore here;
			break;
		}
		while (*e && *e != ';' && *e != '\n')
			e++;
	}

	printf("\n");
	fwrite(blaze822_body(msg), 1, blaze822_bodylen(msg), stdout);
}

int
main(int argc, char *argv[])
{
	int c;
	while ((c = getopt(argc, argv, "")) != -1)
		switch (c) {
		default:
			fprintf(stderr, "Usage: msed [expr] [msgs...]\n");
			exit(1);
		}

	expr = argv[optind];
	optind++;

	if (argc == optind && isatty(0))
		blaze822_loop1(".", sed);
	else
		blaze822_loop(argc-optind, argv+optind, sed);

	return 0;
}