Augmented new_fs() with an additional argument for a default form.
[mmh] / sbr / fmt_new.c
1 /*
2 ** fmt_new.c -- read format file/string and normalize
3 **
4 ** This code is Copyright (c) 2002, by the authors of nmh.  See the
5 ** COPYRIGHT file in the root directory of the nmh distribution for
6 ** complete copyright information.
7 */
8
9 #include <h/mh.h>
10 #include <h/utils.h>
11
12 static char *formats = NULL;
13
14 /*
15 ** static prototypes
16 */
17 static void normalize(char *);
18
19
20 /*
21 ** Copy first available format string, store in static memory and normalize it.
22 */
23 char *
24 new_fs(char *form, char *format, char *def_form, char *default_fs)
25 {
26         struct stat st;
27         register FILE *fp;
28
29         if (formats) {
30                 free(formats);
31         }
32
33         if (form) {
34                 if ((fp = fopen(etcpath(form), "r")) == NULL) {
35                         adios(form, "unable to open format file");
36                 }
37                 if (fstat(fileno(fp), &st) == -1) {
38                         adios(form, "unable to stat format file");
39                 }
40                 formats = mh_xmalloc((size_t) st.st_size + 1);
41                 if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
42                         adios(form, "error reading format file");
43                 }
44                 formats[st.st_size] = '\0';
45                 fclose(fp);
46
47         } else if (format) {
48                 formats = getcpy(format);
49
50         } else if (def_form) {
51                 if ((fp = fopen(etcpath(def_form), "r")) == NULL) {
52                         adios(def_form, "unable to open format file");
53                 }
54                 if (fstat(fileno(fp), &st) == -1) {
55                         adios(def_form, "unable to stat format file");
56                 }
57                 formats = mh_xmalloc((size_t) st.st_size + 1);
58                 if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
59                         adios(def_form, "error reading format file");
60                 }
61                 formats[st.st_size] = '\0';
62                 fclose(fp);
63
64         } else {
65                 formats = getcpy(default_fs);
66         }
67         normalize(formats);  /* expand escapes */
68
69         return formats;
70 }
71
72
73 /*
74 ** Expand escapes in format strings
75 */
76 static void
77 normalize(char *cp)
78 {
79         char *dp;
80
81         for (dp = cp; *cp; cp++) {
82                 if (*cp != '\\') {
83                         *dp++ = *cp;
84                         continue;
85                 }
86
87                 switch (*++cp) {
88                 case 'b':
89                         *dp++ = '\b';
90                         break;
91                 case 'f':
92                         *dp++ = '\f';
93                         break;
94                 case 'n':
95                         *dp++ = '\n';
96                         break;
97                 case 'r':
98                         *dp++ = '\r';
99                         break;
100                 case 't':
101                         *dp++ = '\t';
102                         break;
103                 case '\n':
104                         break;
105                 case '\0':
106                         cp--;
107                         /* fall */
108                 default:
109                         *dp++ = *cp;
110                         break;
111                 }
112         }
113         *dp = '\0';
114 }