]> diplodocus.org Git - nmh/blob - sbr/fmt_new.c
mhbuildsbr.c: Flip logic, moving goto to then-block; no need for else.
[nmh] / sbr / fmt_new.c
1 /* fmt_new.c -- read format file/string and normalize
2 *
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
6 */
7
8 #include <h/mh.h>
9 #include <h/utils.h>
10
11 #define QUOTE '\\'
12
13 static char *formats = 0;
14
15 /*
16 * static prototypes
17 */
18 static void normalize (char *);
19
20
21 /*
22 * Get new format string
23 */
24
25 char *
26 new_fs (char *form, char *format, char *default_fs)
27 {
28 struct stat st;
29 FILE *fp;
30
31 mh_xfree(formats);
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
42 if (read (fileno(fp), formats, (int) st.st_size) != st.st_size)
43 adios (form, "error reading format file");
44
45 formats[st.st_size] = '\0';
46
47 fclose (fp);
48 } else {
49 formats = getcpy (format ? format : default_fs);
50 }
51
52 normalize (formats); /* expand escapes */
53
54 return formats;
55 }
56
57
58 void
59 free_fs(void) {
60 free (formats);
61 formats = 0;
62 }
63
64
65 /*
66 * Expand escapes in format strings
67 */
68
69 static void
70 normalize (char *cp)
71 {
72 char *dp;
73
74 for (dp = cp; *cp; cp++) {
75 if (*cp != QUOTE) {
76 *dp++ = *cp;
77 } else {
78 switch (*++cp) {
79 case 'b':
80 *dp++ = '\b';
81 break;
82
83 case 'f':
84 *dp++ = '\f';
85 break;
86
87 case 'n':
88 *dp++ = '\n';
89 break;
90
91 case 'r':
92 *dp++ = '\r';
93 break;
94
95 case 't':
96 *dp++ = '\t';
97 break;
98
99 case '\n':
100 break;
101
102 case 0:
103 cp--;
104 /* FALLTHRU */
105 default:
106 *dp++ = *cp;
107 break;
108 }
109 }
110 }
111 *dp = '\0';
112 }