| 1 | /* |
| 2 | ** Copyright 1998 - 2006 Double Precision, Inc. |
| 3 | ** See COPYING for distribution information. |
| 4 | */ |
| 5 | |
| 6 | #include "rfc822.h" |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | |
| 10 | static const char rcsid[]="$Id: testsuite.c,v 1.7 2007/02/26 04:13:41 mrsam Exp $"; |
| 11 | |
| 12 | static void print_func(char c, void *p) |
| 13 | { |
| 14 | p=p; |
| 15 | putchar(c); |
| 16 | } |
| 17 | |
| 18 | static void print_separator(const char *s, void *p) |
| 19 | { |
| 20 | p=p; |
| 21 | printf("%s", s); |
| 22 | } |
| 23 | |
| 24 | static struct rfc822t *tokenize(const char *p) |
| 25 | { |
| 26 | struct rfc822t *tp; |
| 27 | int i; |
| 28 | char buf[2]; |
| 29 | |
| 30 | printf("Tokenize: %s\n", p); |
| 31 | tp=rfc822t_alloc_new(p, NULL, NULL); |
| 32 | if (!tp) exit(0); |
| 33 | buf[1]=0; |
| 34 | for (i=0; i<tp->ntokens; i++) |
| 35 | { |
| 36 | buf[0]=tp->tokens[i].token; |
| 37 | if (buf[0] == '\0' || buf[0] == '"' || buf[0] == '(') |
| 38 | { |
| 39 | printf("%s: ", buf[0] == '"' ? "Quote": |
| 40 | buf[0] == '(' ? "Comment":"Atom"); |
| 41 | if (fwrite(tp->tokens[i].ptr, tp->tokens[i].len, 1, |
| 42 | stdout) != 1) |
| 43 | exit(1); |
| 44 | |
| 45 | printf("\n"); |
| 46 | } |
| 47 | else printf("Token: %s\n", buf[0] ? buf:"atom"); |
| 48 | } |
| 49 | return (tp); |
| 50 | } |
| 51 | |
| 52 | static struct rfc822a *doaddr(struct rfc822t *t) |
| 53 | { |
| 54 | struct rfc822a *a=rfc822a_alloc(t); |
| 55 | |
| 56 | if (!a) exit(0); |
| 57 | printf("----\n"); |
| 58 | rfc822_print(a, print_func, print_separator, NULL); |
| 59 | printf("\n"); |
| 60 | rfc822_addrlist(a, print_func, NULL); |
| 61 | rfc822_namelist(a, print_func, NULL); |
| 62 | return (a); |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | struct rfc822t *t1, *t2, *t3, *t4, *t5; |
| 68 | struct rfc822a *a1, *a2, *a3, *a4, *a5; |
| 69 | |
| 70 | t1=tokenize("nobody@example.com (Nobody (is) here\\) right)"); |
| 71 | t2=tokenize("Distribution list: nobody@example.com daemon@example.com"); |
| 72 | t3=tokenize("Mr Nobody <nobody@example.com>, Mr. Nobody <nobody@example.com>"); |
| 73 | t4=tokenize("nobody@example.com, <nobody@example.com>, Mr. Nobody <nobody@example.com>"); |
| 74 | |
| 75 | t5=tokenize("=?UTF-8?Q?Test?= <nobody@example.com>, foo=bar <nobody@example.com>"); |
| 76 | |
| 77 | a1=doaddr(t1); |
| 78 | a2=doaddr(t2); |
| 79 | a3=doaddr(t3); |
| 80 | a4=doaddr(t4); |
| 81 | a5=doaddr(t5); |
| 82 | |
| 83 | rfc822a_free(a5); |
| 84 | rfc822a_free(a4); |
| 85 | rfc822a_free(a3); |
| 86 | rfc822a_free(a2); |
| 87 | rfc822a_free(a1); |
| 88 | rfc822t_free(t5); |
| 89 | rfc822t_free(t4); |
| 90 | rfc822t_free(t3); |
| 91 | rfc822t_free(t2); |
| 92 | rfc822t_free(t1); |
| 93 | return (0); |
| 94 | } |