File: rfcaddr.c

package info (click to toggle)
ifmail 2.14tx8.10-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,056 kB
  • sloc: ansic: 30,328; perl: 4,955; yacc: 839; makefile: 716; sh: 424; cpp: 235; lex: 206; awk: 24
file content (315 lines) | stat: -rw-r--r-- 6,040 bytes parent folder | download | duplicates (15)
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
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#ifdef TESTING
#include <stdio.h>
#endif
#include "xutil.h"
#include "rfcaddr.h"

int addrerror;

static char *errname[] = {
	"nested <>",
	"multiple <>",
	"unmatched <>""()",
	"badtoken",
	"badstructure",
};

char *addrerrstr(err)
int err;
{
	int i;
	static char buf[128];

	buf[0]='\0';
	for (i=0;i<ADDR_ERRMAX;i++)
	if (err & (1 << i))
	{
		if (buf[0]) strcat(buf,",");
		strcat(buf,errname[i]);
	}
	if (buf[0] == '\0') strcpy(buf,"none");
	return buf;
}

void tidyrfcaddr(addr)
parsedaddr addr;
{
	if (addr.target) free(addr.target);
	if (addr.remainder) free(addr.remainder);
	if (addr.comment) free(addr.comment);
}

parsedaddr parserfcaddr(s)
char *s;
{
	parsedaddr result;
	char *inbrackets,*outbrackets,*cleanbuf=NULL,*combuf=NULL;
	char *t,*r,*c,*p,*q,**x;
	int quotes,brackets,escaped,anglecomplete;
	char *firstat,*lastat,*percent,*colon,*comma,*exclam;

	result.target=NULL;
	result.remainder=NULL;
	result.comment=NULL;
	addrerror=0;

	if ((s == NULL) || (*s == '\0')) return result;

	/* First check if there is an "angled" portion */

	inbrackets=xmalloc(strlen(s)+1);
	outbrackets=xmalloc(strlen(s)+1);
	*inbrackets=*outbrackets='\0';
	brackets=quotes=escaped=anglecomplete=0;
	for (p=s,q=inbrackets,r=outbrackets,x=&r;
		*p;
		p++)
	{
		if (escaped) escaped=0;
		else /* process all special chars */
		switch (*p)
		{
		case '\\':	escaped=1; break;
		case '\"':	quotes=!quotes; break;
		case '<':	if (quotes) break;
				if (brackets) addrerror |= ADDR_NESTED;
				if (anglecomplete) addrerror |= ADDR_MULTIPLE;
				brackets++;
				x=&q;
				break;
		case '>':	if (quotes) break;
				if (brackets) brackets--;
				else addrerror |= ADDR_UNMATCHED;
				if (!brackets) anglecomplete=1;
				break;
		}
		*((*x)++)=*p;
		if (!brackets) x=&r;
	}
	*q='\0';
	*r='\0';
	if (brackets || quotes) addrerror |= ADDR_UNMATCHED;

#ifdef TESTING
	printf(" inbrackets: \"%s\"\n",inbrackets);
	printf("outbrackets: \"%s\"\n",outbrackets);
	printf("addrerror: 0x%04x\n",addrerror);
#endif

	if (addrerror) goto leave1;

	cleanbuf=xmalloc(strlen(s)+1);
	*cleanbuf='\0';
	combuf=xmalloc(strlen(s)+1);
	*combuf='\0';

	if (*inbrackets) /* there actually is an angled portion */
	{
		strcpy(combuf,outbrackets);
		c=combuf+strlen(combuf);
		p=inbrackets+1;
		*(p+strlen(p)-1)='\0';
	}
	else
	{
		c=combuf;
		p=outbrackets;
	}

#ifdef TESTING
	printf("    now parsing: \"%s\"\n",p);
	printf("current comment: \"%s\"\n",result.comment);
#endif

	/* OK, now we have result.comment filled with wat was outside
	   angle brackets, c pointing past the end of it,
	   p pointing to what is supposed to be address, with angle
	   brackets already removed */

	quotes=brackets=escaped=0;
	for (r=cleanbuf,x=&r;
		*p;
		p++)
	{
		if (escaped)
		{
			escaped=0;
			*((*x)++)=*p;
		}
		else /* process all special chars */
		if (isspace(*p))
		{
			if ((quotes) || (brackets)) *((*x)++)=*p;
		}
		else
		switch (*p)
		{
		case '\\':	escaped=1;
				/* pass backslash itself only inside quotes
				   and comments, or for the special cases
				   \" and \\  otherwise eat it away */
		if ((quotes) || (brackets)) *((*x)++)=*p;
		else if ((*(p+1)=='"') || (*(p+1)=='\\')) *((*x)++)=*p;
				break;
		case '\"':	quotes=!quotes;
				*((*x)++)=*p;
				break;
		case '(':
				brackets++;
				x=&c;
				break;
		case ')':
				if (brackets) brackets--;
				else addrerror |= ADDR_UNMATCHED;
				if (!brackets) x=&r;
				break;
		default:
				*((*x)++)=*p;
				break;
		}
	}
	*r='\0';
	*c='\0';
	if (brackets || quotes) addrerror |= ADDR_UNMATCHED;

#ifdef TESTING
	printf("     now parsing: \"%s\"\n",inbrackets);
	printf("complete comment: \"%s\"\n",result.comment);
	printf("       addrerror: 0x%04x\n",addrerror);
#endif

	if (addrerror) goto leave2;

	/* OK, now we have inangles buffer filled with the 'clean' address,
	   all comments removed, and result.comment is ready filled */

	/* seach for special chars that are outside quotes */

	firstat=lastat=percent=colon=comma=exclam=NULL;
	quotes=0; escaped=0;
	for (p=cleanbuf;
		*p;
		p++)
	if (*p == '\\') p++; 
	else if (*p == '\"') quotes = !quotes;
	else if (!quotes)
	switch (*p)
	{
	case '@':
			if (!firstat) firstat=p;
			lastat=p;
		break;
	case '%':
			percent=p;
		break;
	case ':':
			colon=p;
		break;
	case ',':
			comma=p;
		break;
	case '!':
			if (!exclam) exclam=p;
		break;
	}

	if ((firstat == cleanbuf) && colon)
	{
#ifdef TESTING
		printf("@aaa,@bbb:xxx@yyy construct\n");
#endif
		if (comma && (comma < colon))
		{
			*comma='\0';
			r=comma+1;
		}
		else
		{
			*colon='\0';
			r=colon+1;
		}
		t=firstat+1;
	}
	else if (lastat)
	{
#ifdef TESTING
		printf("anything@somewhere construct\n");
#endif
		*lastat='\0';
		r=cleanbuf;
		t=lastat+1;
	}
	else if (exclam)
	{
#ifdef TESTING
		printf("domain!something construct (without @'s)\n");
#endif
		*exclam='\0';
		r=exclam+1;
		t=cleanbuf;
	}
	else if (percent)
	{
#ifdef TESTING
		printf("anything%%somewhere construct (without !'s and @'s)\n");
#endif
		*percent='\0';
		r=cleanbuf;
		t=percent+1;
	}
	else
	{
#ifdef TESTING
		printf("remainder only present\n");
#endif
		/* unquote it if necessary */
		if ((*cleanbuf == '\"') &&
		    (*(p=(cleanbuf+strlen(cleanbuf)-1)) == '\"'))
		{
			*p='\0';
			r=cleanbuf+1;
		}
		else r=cleanbuf;
		t=NULL;
	}

	if (t && (*t != '\0')) result.target=xstrcpy(t);
	if (r && (*r != '\0')) result.remainder=xstrcpy(r);
	if (*combuf != '\0') result.comment=xstrcpy(combuf);

leave1: /* this is also normal exit */
	free(cleanbuf);
	free(combuf);
	free(inbrackets);
	free(outbrackets);
	return result;

leave2: /* if error found on second stage, free */
	free(cleanbuf);
	free(combuf);
	return result;
}

#ifdef TESTING

int main()
{
	parsedaddr tmp;
	char buffer[128];

	while(gets(buffer))
	{
		printf("  parsing: \"%s\"\n",buffer);
		tmp=parserfcaddr(buffer);
		printf("   target: \"%s\"\n",tmp.target);
		printf("remainder: \"%s\"\n",tmp.remainder);
		printf("  comment: \"%s\"\n",tmp.comment);
	}
	tidyrfcaddr(tmp);
	return 0;
}

#endif