File: token.c

package info (click to toggle)
zmailer 2.99.51.52pre3-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 16,596 kB
  • ctags: 7,422
  • sloc: ansic: 90,470; sh: 3,608; makefile: 2,784; perl: 1,585; python: 115; awk: 22
file content (342 lines) | stat: -rw-r--r-- 7,289 bytes parent folder | download
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
336
337
338
339
340
341
342
/*
 *	Copyright 1988 by Rayan S. Zachariassen, all rights reserved.
 *	This will be free software, but only when it is finished.
 */

/*
 * Token manipulation functions.
 */

#include "hostenv.h"
#include "mailer.h"
#include "../libsh/io.h"
#include <ctype.h>
#include "libz.h"

token822 *
makeToken(s, n)
	const char	*s;
	register u_int	n;
{
	register token822	*t;

	t = (token822 *)tmalloc(sizeof (token822));
	t->t_pname = strnsave(s, n);
	t->t_len = 0;
	t->t_type = Empty;
	t->t_next = 0;
	return t;
}

token822 *
copyToken(t)
	register token822 *t;
{
	register token822 *ct;
	int tlen;
	
	if (stickymem != MEM_TEMP) {
		tlen = TOKENLEN(t);
	} else {
		tlen = t->t_len;
	}

	if (tlen > 50000) {
		/* Eh ?!  Got 50k chars of (header) token ?  Umm...
		   (indeed we have seen such monster; a 205 kB of SINGLE
		   "To:" header with 8700+ addresses on it.) */
		tlen = 50000; /* We use AT MOST 50k from start! */
	}

	ct = (token822 *)tmalloc(sizeof (token822));
	if (stickymem != MEM_TEMP)
	  ct->t_pname = strnsave((const char *)t->t_pname, tlen);
	else
	  ct->t_pname = t->t_pname;
	ct->t_len = tlen;

	ct->t_type = t->t_type;
	ct->t_next = t->t_next;
	return ct;
}

const char *
formatToken(t)
	register token822 *t;
{
	const char *name;
	int len;
	static char buf[256];

	switch (t->t_type) {
	case String:	name = "string"; break;
	case Atom:	name = "atom"; break;
	case Special:	name = "special"; break;
	case DomainLiteral:	name = "domainLiteral"; break;
	case Line:	name = "line"; break;
	case Space:	name = "space"; break;
	case Word:	name = "word"; break;
	case Comment:	name = "comment"; break;
	case Empty:	name = "empty"; break;
	case Error:	name = "error"; break;
	default:	name = "unknown"; break;
	}
	buf[0] = '\'';
	len = TOKENLEN(t);
	if ((len + 4 + strlen(name)) >= sizeof(buf))
	  len = sizeof(buf) - strlen(name) - 4;
	memcpy(buf+1, (char *)(t->t_pname), len);
	sprintf(buf + len + 1, "'(%s)", name);
	return buf;
}

const char *
formatAddr(d)
	AddrComponent d;
{
	switch (d) {
	case aPhrase:	return "aPhrase";
	case aComment:	return "aComment";
	case aSpecial:	return "aSpecial";
	case aGroup:	return "aGroup";
	case anAddress:	return "anAddress";
	case aDomain:	return "aDomain";
	case aWord:	return "aWord";
	case anError:	return "anError";
	case reSync:	return "reSync";
	/*case aSpace:	return "aSpace";*/
	}
	return "unknown";
}

int
printToken(buf, eob, t, tend, quotespecials)
	char *buf, *eob;
	register token822 *t, *tend;
	int quotespecials;
{
	register char *cp;
	register const char *s;
	register int len;

	--eob;		/* make space for terminating NUL */
	for (cp = buf; t != NULL && t != tend && cp < eob; t = t->t_next) {
		if (t->t_type == DomainLiteral)
			*cp++ = '[';
		else if (t->t_type == String)
			*cp++ = '"';
		else if (quotespecials && t->t_type == Special)
			*cp++ = '\\';
		for (s = t->t_pname, len = TOKENLEN(t); len > 0; --len)
			*cp++ = *s++;
		if (t->t_type == DomainLiteral)
			*cp++ = ']';
		else if (t->t_type == String)
			*cp++ = '"';
	}
	*cp = '\0';
	return cp - buf;
}

int
printdToken(bufp, buflenp, t, tend, quotespecials)
	char **bufp;
	register token822 *t, *tend;
	int quotespecials, *buflenp;
{
	register char *cp, *buf = *bufp;
	register const char *s;
	register int len, buflen = *buflenp;

	for (cp = buf; t != NULL && t != tend; t = t->t_next) {
		/* If it won't fit in, extent the space */
		while (((cp - buf) + 3 + TOKENLEN(t)) > buflen) {
		  /* Must extend */
		  buflen <<= 1; /* Multiply by two ! */
		  len = (cp - buf);
		  buf = erealloc(buf,buflen);
		  cp = buf+len;
		}
		if (t->t_type == DomainLiteral)
			*cp++ = '[';
		else if (t->t_type == String)
			*cp++ = '"';
		else if (quotespecials && t->t_type == Special)
			*cp++ = '\\';
		for (s = t->t_pname, len = TOKENLEN(t); len > 0; --len)
			*cp++ = *s++;
		if (t->t_type == DomainLiteral)
			*cp++ = ']';
		else if (t->t_type == String)
			*cp++ = '"';
	}
	*cp = '\0';
	*bufp    = buf;
	*buflenp = buflen;
	return cp - buf;
}

/* return output cursor column if 'fp' is non-null,
 * else return the number of output chars. */

int
fprintToken(fp, t, onlylength)
	FILE *fp;
	token822 *t;
	int onlylength;
{
	int len, col = onlylength;

	len = TOKENLEN(t);
	if (t->t_type == DomainLiteral) {
		if (fp) putc('[', fp);
		else ++onlylength;
		++col;
	} else if (t->t_type == String) {
		if (fp) putc('"', fp);
		else ++onlylength;
		++col;
	} else if (t->t_type == Error) {
		if (fp) putc('?', fp);
		else ++onlylength;
		++col;
	}
	if (fp) {
		const char *s = (const char *)t->t_pname;
		int i;
		fwrite(s, sizeof (char), len, fp);
		for (i = 0; i < len; ++i) {
		  if (s[i] == '\t')
		    col += 8 - (col % 8);
		  else if (s[i] == '\n')
		    col = 0;
		  else
		    ++col;
		}
	} else {
		onlylength += len;
	}
	if (t->t_type == DomainLiteral) {
		if (fp) putc(']', fp);
		else ++onlylength;
		++col;
	} else if (t->t_type == String) {
		if (fp) putc('"', fp);
		else ++onlylength;
		++col;
	} else if (t->t_type == Error) {
		if (fp) putc('?', fp);
		else ++onlylength;
		++col;
	}
	return (fp ? col : onlylength);
}

#define LINELEN	80

/* return output cursor column if 'fp' is non-null,
 * else return the number of output chars. */

int
fprintFold(fp, t, col, foldcol)
	FILE *fp;
	token822 *t;
	int col, foldcol;
{
	int len;
	const char *cp, *ncp;

	len = TOKENLEN(t);
	if (fp) {
	  if (t->t_type == DomainLiteral)
	    putc('[', fp);
	  else if (t->t_type == String)
	    putc('"', fp);
	  else if (t->t_type == Error)
	    putc('?', fp);
	}
	++col;

	cp = t->t_pname;
	do {
		/* find a breakpoint */
		if (col < LINELEN && len > (LINELEN-col)) {
		  /* there is a breakpoint */
		  for (ncp = cp + (LINELEN-col); ncp > cp; --ncp) {
		    if (isascii((*ncp)&0xFF) && (isspace((*ncp)&0xFF)
						 || *ncp == ','))
		      break;
		  }
		  if (ncp == cp) {
		    for (ncp = cp + (LINELEN-col); ncp < cp + len; ++ncp)
		      if (isascii((*ncp)&0xFF) && (isspace((*ncp)&0xFF)
						   || *ncp == ','))
			break;
		  }
		  /* found breakpoint */
		} else
		  ncp = cp + len;
		while (cp < ncp && len > 0) {
		  int c = (*cp) & 0xFF;
		  if (isascii(c) && isspace(c)) {
		    ++col, putc(' ', fp);
		    while (cp < ncp && len > 0) {
		      c = (*cp) & 0xFF;
		      if (!(isascii(c) && isspace(c)))
			break;
		      --len;
		      ++cp;
		    }
		  } else {
		    ++col;
		    putc(*cp, fp);
		    --len;
		    ++cp;
		  }
		}
		if (len > 0) {
		  /* gobble LWSP at beginning of line */
		  while (len > 0 && isascii((*cp)&0xFF) && isspace((*cp)&0xFF))
		    --len, ++cp;
		  putc('\n', fp);
		  for (col = 0; col + 8  < foldcol; col += 8)
		    putc('\t', fp);
		  for (;col < foldcol; ++col)
		    putc(' ', fp);
		  if (col < 1)
		    putc(' ', fp), ++col;
		}
	} while (len > 0);

	if (fp) {
	  if (t->t_type == DomainLiteral)
	    putc(']', fp);
	  else if (t->t_type == String)
	    putc('"', fp);
	  else if (t->t_type == Error)
	    putc('?', fp);
	}
	++col;
	return col;
}

void
freeTokens(t, memtype)
	token822 *t;
	int memtype;
{
	token822 *nt;

	if (memtype != MEM_MALLOC)
		return;
	while (t != NULL) {
		if (t->t_len > 600000)
			abort(); /* The copyToken() builds at most 50k
				    sized tokens, this is 600k, and thus
				    it MUST be failure! */
		nt = t->t_next;
		free((char *)t->t_pname);
		free((char *)t);
		t = nt;
	}
}