X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Ffmt_new.c;h=c1386d4c710bb4de354bbbbc9d219b6753ccc5eb;hp=c1bc7c730e83edfccf4462ba2094ea50d3af89fb;hb=18591f8e001ecedbee48a51c1d1f08ebaa1c15c8;hpb=81a21a9a97d8633f6d6231e31fdb6e328d0d3ff2 diff --git a/sbr/fmt_new.c b/sbr/fmt_new.c index c1bc7c7..c1386d4 100644 --- a/sbr/fmt_new.c +++ b/sbr/fmt_new.c @@ -1,109 +1,118 @@ - /* - * fmt_new.c -- read format file/string and normalize - * - * $Id$ - * - * 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. - */ +** fmt_new.c -- read format file/string and normalize +** +** 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 #include +#include +#include +#include -#define QUOTE '\\' - -static char *formats = 0; +static char *formats = NULL; /* - * static prototypes - */ -static void normalize (char *); +** static prototypes +*/ +static void normalize(char *); /* - * Get new format string - */ - +** Copy first available format string, store in static memory and normalize it. +*/ char * -new_fs (char *form, char *format, char *default_fs) +new_fs(char *form, char *def_form) { - 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"); + struct stat st; + FILE *fp; - 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); - } + if (formats) { + mh_free0(&formats); + } - normalize (formats); /* expand escapes */ + if (form) { + if (*form == '=') { + formats = mh_xstrdup(form+1); + } else { + if ((fp = fopen(etcpath(form), "r")) == NULL) { + adios(EX_IOERR, form, "unable to open format file"); + } + if (fstat(fileno(fp), &st) == -1) { + adios(EX_IOERR, form, "unable to stat format file"); + } + formats = mh_xcalloc(st.st_size + 1, sizeof(char)); + if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { + adios(EX_IOERR, form, "error reading format file"); + } + formats[st.st_size] = '\0'; + fclose(fp); + } + } else if (def_form) { + if (*def_form == '=') { + formats = mh_xstrdup(def_form+1); + } else { + if ((fp = fopen(etcpath(def_form), "r")) == NULL) { + adios(EX_IOERR, def_form, "unable to open format file"); + } + if (fstat(fileno(fp), &st) == -1) { + adios(EX_IOERR, def_form, "unable to stat format file"); + } + formats = mh_xcalloc(st.st_size + 1, sizeof(char)); + if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) { + adios(EX_IOERR, def_form, "error reading format file"); + } + formats[st.st_size] = '\0'; + fclose(fp); + } + } + normalize(formats); /* expand escapes */ - return formats; + return formats; } /* - * Expand escapes in format strings - */ - +** Expand escapes in format strings +*/ static void -normalize (char *cp) +normalize(char *cp) { - char *dp; + char *dp; - for (dp = cp; *cp; cp++) { - if (*cp != QUOTE) { - *dp++ = *cp; - } else { - switch (*++cp) { - case 'b': - *dp++ = '\b'; - break; + for (dp = cp; *cp; cp++) { + if (*cp != '\\') { + *dp++ = *cp; + continue; + } + switch (*++cp) { + case 'b': + *dp++ = '\b'; + break; case 'f': - *dp++ = '\f'; - break; - + *dp++ = '\f'; + break; case 'n': - *dp++ = '\n'; - break; - + *dp++ = '\n'; + break; case 'r': - *dp++ = '\r'; - break; - + *dp++ = '\r'; + break; case 't': - *dp++ = '\t'; - break; - + *dp++ = '\t'; + break; case '\n': - break; - - case 0: - cp--; /* fall */ - default: - *dp++ = *cp; - break; - } + break; + case '\0': + cp--; + /* fall */ + default: + *dp++ = *cp; + break; + } } - } - *dp = '\0'; + *dp = '\0'; }