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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/* $Id: header.c,v 1.5 1993/11/02 00:03:04 chip Exp $
*
* A program to parse RFC 822 mail/news headers.
*
* usage: header [-c] [-n] [-v] [-f field] ... files
*
* Default action is to print entire header. If one or more -f options
* are given, only the specified fields are printed. The field names are
* not printed unless -n is specified. Field name comparisons are case
* insensitive unless -c is specified. If -v is specified, all headers
* except those specified with -f are printed. NOTE: -v implies -n.
*
* Output lines are preceeded by the filename if more than one file is
* specified.
*
* This program is intended for use in delivery files, to extract multi-
* line header fields.
*
* $Log: header.c,v $
* Revision 1.5 1993/11/02 00:03:04 chip
* Use __GNUC_MINOR__ to trigger attribute keyword.
*
* Revision 1.4 1993/10/28 16:49:51 chip
* Declare function return types, including void.
*
* Revision 1.3 1992/11/13 19:11:07 chip
* Make "-v" really imply "-n".
*
* Revision 1.2 1991/05/23 17:23:19 chip
* Follow RFC822 definition of header syntax.
* Guard isxxx() macros against negative values.
*
* Revision 1.1 1991/05/13 18:36:55 chip
* Initial revision
*
*/
#include <stdio.h>
#include <ctype.h>
/*
* Manifest constants
*/
#define TRUE 1
#define FALSE 0
/*
* Other useful macros.
*/
#define GETSIZE(buf) (sizeof(buf) - 1)
#define ISFROM(p) ((p)[0] == 'F' && (p)[1] == 'r' && (p)[2] == 'o' \
&& (p)[3] == 'm' && (p)[4] == ' ')
/* How to declare functions that don't return
and functions that are like printf. */
#ifdef __GNUC__
# if __GNUC__ < 2 || __GNUC_MINOR__ < 5
# define NORETURN volatile
# define ATTRIBUTE(x) /**/
# else
# define NORETURN /**/
# define ATTRIBUTE(x) __attribute__(x)
# endif
#else /* not GCC */
# define NORETURN /**/
# define ATTRIBUTE(x) /**/
#endif /* GCC */
/*
* External data.
*/
/* Variables set by getopt() [blech] */
extern int optind, opterr;
extern char *optarg;
/*
* Library functions.
*/
extern char *malloc();
extern char *realloc();
extern void free();
/*
* Global data
*/
int field_count = 0;
int field_alloc = 0;
char **field_names = NULL;
int nocasematch = TRUE; /* ignore case in header matches */
int printnames = FALSE; /* print field names with data */
int except = FALSE; /* reverse sense of -f */
/*
* Global functions.
*/
void usage();
void header();
NORETURN void nomem() ATTRIBUTE((noreturn));
/*----------------------------------------------------------------------
* The Program.
*/
int
main(argc, argv)
int argc;
char **argv;
{
int c, errors;
field_alloc = 8;
field_names = (char **) malloc(field_alloc * sizeof(char **));
if (field_names == NULL)
nomem();
errors = FALSE;
while ((c = getopt(argc, argv, "cnvf:")) != EOF)
{
switch (c)
{
case 'c':
nocasematch = FALSE;
break;
case 'n':
printnames = TRUE;
break;
case 'v':
except = TRUE;
printnames = TRUE;
break;
case 'f':
if (field_count >= field_alloc)
{
field_alloc *= 2;
field_names =
(char **) realloc((char *) field_names,
field_alloc * sizeof(char **));
if (field_names == NULL)
nomem();
}
field_names[field_count++] = optarg;
break;
default:
errors = TRUE;
break;
}
}
if (errors)
usage();
if (optind == argc)
header(stdin, (char *) NULL);
else
{
FILE *fp;
int a, filenames;
filenames = ((argc - optind) > 1);
for (a = optind; a < argc; ++a)
{
if ((fp = fopen(argv[a], "r")) == NULL)
{
errors = TRUE;
perror(argv[a]);
continue;
}
header(fp, (filenames ? argv[a] : (char *) NULL));
(void) fclose(fp);
}
}
exit(errors ? 1 : 0);
/* NOTREACHED */
}
void
usage()
{
(void) fprintf(stderr,
"usage: header [-c] [-n] [-v] [-f fieldname] ... files\n");
exit(1);
}
NORETURN void
nomem()
{
(void) fprintf(stderr, "header: out of memory\n");
exit(1);
}
void
header(fp, filename)
FILE *fp;
char *filename;
{
char buf[1024];
if (fgets(buf, GETSIZE(buf), fp) == NULL)
return;
/* Ignore From_ line(s). */
while (ISFROM(buf) || buf[0] == '>')
{
if (fgets(buf, GETSIZE(buf), fp) == NULL)
return;
}
while (buf[0] != '\n')
{
char *p;
int print_this;
p = buf;
while (*p
&& !isspace(*p & 0xFF)
&& !iscntrl(*p & 0xFF)
&& *p != ':')
++p;
if (p == buf || *p != ':')
break;
print_this = field(buf, p - buf);
if (except)
print_this = !print_this;
if (print_this)
{
if (filename)
{
(void) fputs(filename, stdout);
(void) fputc(':', stdout);
}
++p;
if (*p == ' ' || *p == '\t')
++p;
if (field_count == 0 || printnames)
(void) fputs(buf, stdout);
else
(void) fputs(p, stdout);
}
/* get the next input line */
if (fgets(buf, GETSIZE(buf), fp) == NULL)
break;
/* deal with continuation lines */
while (buf[0] == ' ' || buf[0] == '\t')
{
if (print_this)
{
if (filename)
{
(void) fputs(filename, stdout);
(void) fputc(':', stdout);
}
(void) fputs(buf, stdout);
}
if (fgets(buf, GETSIZE(buf), fp) == NULL)
{
buf[0] = '\n';
break;
}
}
}
}
int
field(s, n)
char *s;
int n;
{
int i;
if (field_count == 0)
return TRUE;
for (i = 0; i < field_count; ++i)
{
char *f = field_names[i];
if (strlen(f) == n)
{
if (nocasematch)
{
if (ci_strncmp(f, s, n) == 0)
return TRUE;
}
else
{
if (strncmp(f, s, n) == 0)
return TRUE;
}
}
}
return FALSE;
}
int
ci_strncmp(s, t, n)
char *s, *t;
int n;
{
char c, d;
while (n-- > 0)
{
c = *s++ & 0xFF;
d = *t++ & 0xFF;
if ((c == 0) && (d == 0))
break;
if (isupper(c))
c = tolower(c);
if (isupper(d))
d = tolower(d);
if (c > d)
return 1;
if (c < d)
return -1;
}
return 0;
}
|