1 # include <stdio.h> 2 # include <ctype.h> 3 # include <errno.h> 4 # include "sendmail.h" 5 6 static char SccsId[] = "@(#)headers.c 3.1 08/09/81"; 7 8 /* 9 ** CHOMPHEADER -- process and save a header line. 10 ** 11 ** Called by collect and by readcf to deal with header lines. 12 ** 13 ** Parameters: 14 ** line -- header as a text line. 15 ** def -- if set, this is a default value. 16 ** 17 ** Returns: 18 ** flags for this header. 19 ** 20 ** Side Effects: 21 ** The header is saved on the header list. 22 */ 23 24 chompheader(line, def) 25 char *line; 26 bool def; 27 { 28 register char *p; 29 register HDR *h; 30 HDR **hp; 31 extern bool isheader(); 32 char *fname; 33 char *fvalue; 34 struct hdrinfo *hi; 35 36 /* strip off trailing newline */ 37 p = rindex(line, '\n'); 38 if (p != NULL) 39 *p = '\0'; 40 41 /* find canonical name */ 42 fname = line; 43 p = index(line, ':'); 44 fvalue = &p[1]; 45 while (isspace(*--p)) 46 continue; 47 *++p = '\0'; 48 makelower(fname); 49 50 /* strip field value on front */ 51 if (*fvalue == ' ') 52 fvalue++; 53 54 /* search header list for this header */ 55 for (hp = &Header, h = Header; h != NULL; hp = &h->h_link, h = h->h_link) 56 { 57 if (strcmp(fname, h->h_field) == 0 && bitset(H_DEFAULT, h->h_flags)) 58 break; 59 } 60 61 /* see if it is a known type */ 62 for (hi = HdrInfo; hi->hi_field != NULL; hi++) 63 { 64 if (strcmp(hi->hi_field, fname) == 0) 65 break; 66 } 67 68 /* if this means "end of header" quit now */ 69 if (bitset(H_EOH, hi->hi_flags)) 70 return (hi->hi_flags); 71 72 /* create/fill in a new node */ 73 if (h == NULL) 74 { 75 /* create a new node */ 76 *hp = h = (HDR *) xalloc(sizeof *h); 77 h->h_field = newstr(fname); 78 h->h_value = NULL; 79 h->h_link = NULL; 80 h->h_flags = hi->hi_flags; 81 h->h_mflags = hi->hi_mflags; 82 } 83 if (def) 84 h->h_flags |= H_DEFAULT; 85 else 86 h->h_flags &= ~H_CHECK; 87 if (h->h_value != NULL) 88 free(h->h_value); 89 h->h_value = newstr(fvalue); 90 91 return (h->h_flags); 92 } 93 /* 94 ** HVALUE -- return value of a header. 95 ** 96 ** Only "real" fields (i.e., ones that have not been supplied 97 ** as a default) are used. 98 ** 99 ** Parameters: 100 ** field -- the field name. 101 ** 102 ** Returns: 103 ** pointer to the value part. 104 ** NULL if not found. 105 ** 106 ** Side Effects: 107 ** sets the H_USED bit in the header if found. 108 */ 109 110 char * 111 hvalue(field) 112 char *field; 113 { 114 register HDR *h; 115 116 for (h = Header; h != NULL; h = h->h_link) 117 { 118 if (!bitset(H_DEFAULT, h->h_flags) && strcmp(h->h_field, field) == 0) 119 { 120 h->h_flags |= H_USED; 121 return (h->h_value); 122 } 123 } 124 return (NULL); 125 } 126 /* 127 ** ISHEADER -- predicate telling if argument is a header. 128 ** 129 ** Parameters: 130 ** s -- string to check for possible headerness. 131 ** 132 ** Returns: 133 ** TRUE if s is a header. 134 ** FALSE otherwise. 135 ** 136 ** Side Effects: 137 ** none. 138 */ 139 140 bool 141 isheader(s) 142 register char *s; 143 { 144 if (!isalnum(*s)) 145 return (FALSE); 146 while (!isspace(*s) && *s != ':') 147 s++; 148 while (isspace(*s)) 149 s++; 150 return (*s == ':'); 151 } 152