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