summaryrefslogtreecommitdiff
path: root/sys/src/cmd/upas/smtp/parsetest.c
blob: 38e77fb9a92806dc1f8f0a49cd5952b5c8fdfd62 (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
#include <u.h>
#include <libc.h>
#include <String.h>
#include <bio.h>
#include "smtp.h"

Biobuf o;

void
freefields(void)
{
	Field *f, *fn;
	Node *n, *nn;

	for(f = firstfield; f != nil; f = fn){
		fn = f->next;
		for(n = f->node; n != nil; n = nn){
			nn = n->next;
			s_free(n->s);
			s_free(n->white);
			free(n);
		}
		free(f);
	}
	firstfield = nil;
}

void
printhdr(void)
{
	Field *f;
	Node *n;

	for(f = firstfield; f != nil; f = f->next){
		for(n = f->node; n != nil; n = n->next){
			if(n->s != nil)
				Bprint(&o, "%s", s_to_c(n->s));
			else
				Bprint(&o, "%c", n->c);
			if(n->white != nil)
				Bprint(&o, "%s", s_to_c(n->white));
		}
		Bprint(&o, "\n");
	}
}

void
usage(void)
{
	fprint(2, "usage: parsetest file ...\n");
	exits("usage");
}

void
main(int argc, char **argv)
{
	int i, fd, nbuf;
	char *buf;

	ARGBEGIN{
	default:
		usage();
	}ARGEND

	if(Binit(&o, 1, OWRITE) == -1)
		sysfatal("Binit: %r");
	for(i = 0; i < argc; i++){
		fd = open(argv[i], OREAD);
		if(fd == -1)
			sysfatal("open: %r");
		buf = malloc(128*1024);
		if(buf == nil)
			sysfatal("malloc: %r");
		nbuf = read(fd, buf, 128*1024);
		if(nbuf == -1)
			sysfatal("read: %r");
		close(fd);
		yyinit(buf, nbuf);
		yyparse();
		printhdr();
		freefields();
		free(buf);
		Bflush(&o);
	}
	exits("");
}