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
|
/*
* fmt_new.c -- read format file/string and normalize
*
* $Id: fmt_new.c,v 1.3 2006/01/02 03:17:42 bress Exp $
*
* This code is Copyright (c) 2002, by the authors of nmh. See the
* COPYRIGHT file in the root directory of the nmh distribution for
* complete copyright information.
*/
#include <h/mh.h>
#include <h/utils.h>
#define QUOTE '\\'
static char *formats = 0;
/*
* static prototypes
*/
static void normalize (char *);
/*
* Get new format string
*/
char *
new_fs (char *form, char *format, char *default_fs)
{
struct stat st;
register FILE *fp;
if (formats)
free (formats);
if (form) {
if ((fp = fopen (etcpath (form), "r")) == NULL)
adios (form, "unable to open format file");
if (fstat (fileno (fp), &st) == -1)
adios (form, "unable to stat format file");
formats = mh_xmalloc ((size_t) st.st_size + 1);
if (read (fileno(fp), formats, (int) st.st_size) != st.st_size)
adios (form, "error reading format file");
formats[st.st_size] = '\0';
fclose (fp);
} else {
formats = getcpy (format ? format : default_fs);
}
normalize (formats); /* expand escapes */
return formats;
}
/*
* Expand escapes in format strings
*/
static void
normalize (char *cp)
{
char *dp;
for (dp = cp; *cp; cp++) {
if (*cp != QUOTE) {
*dp++ = *cp;
} else {
switch (*++cp) {
case 'b':
*dp++ = '\b';
break;
case 'f':
*dp++ = '\f';
break;
case 'n':
*dp++ = '\n';
break;
case 'r':
*dp++ = '\r';
break;
case 't':
*dp++ = '\t';
break;
case '\n':
break;
case 0:
cp--; /* fall */
default:
*dp++ = *cp;
break;
}
}
}
*dp = '\0';
}
|