home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
b
/
bmh02src.zip
/
HEADER.C
< prev
next >
Wrap
C/C++ Source or Header
|
1992-08-16
|
5KB
|
220 lines
/*
header.c: Copyright Paul Healy, EI9GL, 1992.
Derived from bm.
Copyright 1986 Bdale Garbee, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
Copyright 1987 1988 Dave Trulli NN2Z, All Rights Reserved.
Permission granted for non-commercial copying and use, provided
this notice is retained.
920712: Added this header.
920717: Todo - add line continuation support
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "header.h"
#include "buffer.h"
#include "misc.h"
#define MaxHdrLength 10
static char hd[][MaxHdrLength] = {
/*
* Currently only interested in parsing these headers.
*/
"cc",
"date",
"from",
"newsgroup",
"reply-to",
"subject",
"to"
};
static const int NumHeaders = sizeof(hd) / MaxHdrLength;
/*
* return the header token type
* On a undamaged mailfile, this routine should always be called with some
* kind of header. Line continuations shouldn't reach this far as well.
*
*/
Header
htype(char *s)
{
char *p, *k, key[100], *result;
p = s;
k = key;
/*
* check to see if there is a ':' before and white space after
*/
while (*p && (*p != ':') && (*p != ' ') )
*k++ = isupper(*p) ? tolower(*p++) : *p++;
if ( (*p != ':') || ( *(p+1) != ' ') )
return NOHEADER;
*k = '\0';
result = bsearch(key, hd, NumHeaders, MaxHdrLength, strcmp);
return result == NULL ? UNKNOWN : (result - (char *) hd) / MaxHdrLength;
}
void
freeheader(char *argv[])
{
int i;
for (i=0; i<MAXHDR; i++)
if ( argv[i] != NULL )
free(argv[i]);
}
/*
* Read from the file parsing headers. Fill in details in the argv array
* for any non-null entry.
*/
int
parseheader(char **start, char **end, char *argv[])
{
char line[1024];
int i;
for (i=0; i<MAXHDR; i++)
argv[i] = NULL;
while (getstring(start, end, line, sizeof(line)) != NULL) {
Header h;
if (line[0] == '\0') /* end of headers */
break;
if (line[0] == ' ' || line[0] == '\t') /* line continuation */
continue; /* fix this later */
if (line[0] == '>') /* Escaped '>From' */
continue;
h = htype(line);
if ( h == UNKNOWN )
continue;
if ( h == NOHEADER ) {
fprintf(stderr, "parseheader: bad header %s\n", line);
return -1;
}
argv[h] = strdup(line);
}
return 0;
}
/*
* Read from the file parsing headers. Fill in details in the argv array
* for any non-null entry. The file is left at the first line of the
* message body.
*/
int
parseheaderfile(FILE *fp, char *argv[])
{
char line[1024];
int i;
for (i=0; i<MAXHDR; i++)
argv[i] = NULL;
while (fgets(line, sizeof(line), fp) != NULL) {
Header h;
if ((*line == '\n') || (*line == '-') ) /* end of headers */
break;
if (*line == ' ' || *line == '\t') /* line continuation */
continue; /* fix this later */
if (line[0] == '>') /* Escaped '>From' */
continue;
h = htype(line);
if ( h == UNKNOWN )
continue;
if ( h == NOHEADER ) {
fprintf(stderr, "parseheader: bad header %s", line);
return -1;
}
rip(line);
argv[h] = strdup(line);
}
return 0;
}
/*
* return the header token type
* On a undamaged mailfile, this routine should always be called with some
* kind of header. Line continuations shouldn't reach this far as well.
*
*/
Header
loosehtype(char *s, char **data)
{
char *p = s,
key[100], *k = key, *result;
/*
* check to see if there is a ':'
*/
while (*p && (*p != ':') )
*k++ = isupper(*p) ? tolower(*p++) : *p++;
if ( *p != ':' )
return NOHEADER;
*k = '\0';
result = bsearch(key, hd, NumHeaders, MaxHdrLength, strcmp);
k = p + 1;
while (isspace(*k))
k++;
*data = k;
return result == NULL ? UNKNOWN : (result - (char *) hd) / MaxHdrLength;
}
/*
* Read from the file parsing headers. Fill in details in the argv array
* for any non-null entry. The file is left at the first line of the
* message body.
*/
int
looseparseheaderfile(FILE *fp, char *argv[])
{
char line[1024];
int i;
for (i=0; i<MAXHDR; i++)
argv[i] = NULL;
while (fgets(line, sizeof(line), fp) != NULL) {
Header h;
char *data;
if ((*line == '\n') || (*line == '-') ) /* end of headers */
break;
if (*line == ' ' || *line == '\t') /* line continuation */
continue; /* fix this later */
if (line[0] == '>') /* Escaped '>From' */
continue;
h = loosehtype(line, &data);
if ( h == UNKNOWN )
continue;
if ( h == NOHEADER ) {
fprintf(stderr, "looseparseheader: bad header %s", line);
return -1;
}
rip(data);
argv[h] = strdup(data);
}
return 0;
}