chiark / gitweb /
Add __oop-read-copy.c
[inn-innduct.git] / lib / cleanfrom.c
1 /*  $Id: cleanfrom.c 6135 2003-01-19 01:15:40Z rra $
2 **
3 */
4
5 #include "config.h"
6 #include "clibrary.h"
7 #include "libinn.h"
8
9
10 #define LPAREN  '('
11 #define RPAREN  ')'
12
13
14 /*
15 **  Clean up a from line, making the following transformations:
16 **      address                 address
17 **      address (stuff)         address
18 **      stuff <address>         address
19 */
20 void HeaderCleanFrom(char *from)
21 {
22     char                *p;
23     char                *end;
24     int                 len;
25
26     if ((len = strlen(from)) == 0)
27         return;
28     /* concatenate folded header */
29     for (p = end = from ; p < from + len ;) {
30         if (*p == '\n') {
31             if ((p + 1 < from + len) && ISWHITE(p[1])) {
32                 if ((p - 1 >= from) && (p[-1] == '\r')) {
33                     end--;
34                     *end = p[1];
35                     p += 2;
36                 } else {
37                     *end = p[1];
38                     p++;
39                 }
40             } else {
41                 *end = '\0';
42                 break;
43             }
44         } else
45             *end++ = *p++;
46     }
47     if (end != from)
48         *end = '\0';
49
50     /* Do pretty much the equivalent of sed's "s/(.*)//g"; */
51     while ((p = strchr(from, LPAREN)) && (end = strchr(p, RPAREN))) {
52         while (*++end)
53             *p++ = *end;
54         *p = '\0';
55     }
56
57     /* Do pretty much the equivalent of sed's "s/\".*\"//g"; */
58     while ((p = strchr(from, '"')) && (end = strchr(p, '"'))) {
59         while (*++end)
60             *p++ = *end;
61         *p = '\0';
62     }
63
64     /* Do the equivalent of sed's "s/.*<\(.*\)>/\1/" */
65     if ((p = strrchr(from, '<')) && (end = strrchr(p, '>'))) {
66         while (++p < end)
67             *from++ = *p;
68         *from = '\0';
69     }
70
71     /* drop white spaces */
72     if ((len = strlen(from)) == 0)
73         return;
74     for (p = end = from ; p < from + len ;) {
75         if (ISWHITE(*p)) {
76             p++;
77             continue;
78         }
79         *end++ = *p++;
80     }
81     if (end != from)
82         *end = '\0';
83 }