Remove unused code
[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 #include <unistd.h>
12 #include <sys/stat.h>
13 #include <sysexits.h>
14
15 static char *formats = NULL;
16
17 /*
18 ** static prototypes
19 */
20 static void normalize(char *);
21
22
23 /*
24 ** Copy first available format string, store in static memory and normalize it.
25 */
26 char *
27 new_fs(char *form, char *def_form)
28 {
29         struct stat st;
30         FILE *fp;
31
32         if (formats) {
33                 mh_free0(&formats);
34         }
35
36         if (form) {
37                 if (*form == '=') {
38                         formats = mh_xstrdup(form+1);
39                 } else {
40                         if ((fp = fopen(etcpath(form), "r")) == NULL) {
41                                 adios(EX_IOERR, form, "unable to open format file");
42                         }
43                         if (fstat(fileno(fp), &st) == -1) {
44                                 adios(EX_IOERR, form, "unable to stat format file");
45                         }
46                         formats = mh_xcalloc(st.st_size + 1, sizeof(char));
47                         if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
48                                 adios(EX_IOERR, form, "error reading format file");
49                         }
50                         formats[st.st_size] = '\0';
51                         fclose(fp);
52                 }
53         } else if (def_form) {
54                 if (*def_form == '=') {
55                         formats = mh_xstrdup(def_form+1);
56                 } else {
57                         if ((fp = fopen(etcpath(def_form), "r")) == NULL) {
58                                 adios(EX_IOERR, def_form, "unable to open format file");
59                         }
60                         if (fstat(fileno(fp), &st) == -1) {
61                                 adios(EX_IOERR, def_form, "unable to stat format file");
62                         }
63                         formats = mh_xcalloc(st.st_size + 1, sizeof(char));
64                         if (read(fileno(fp), formats, (int)st.st_size) != st.st_size) {
65                                 adios(EX_IOERR, def_form, "error reading format file");
66                         }
67                         formats[st.st_size] = '\0';
68                         fclose(fp);
69                 }
70         }
71         normalize(formats);  /* expand escapes */
72
73         return formats;
74 }
75
76
77 /*
78 ** Expand escapes in format strings
79 */
80 static void
81 normalize(char *cp)
82 {
83         char *dp;
84
85         for (dp = cp; *cp; cp++) {
86                 if (*cp != '\\') {
87                         *dp++ = *cp;
88                         continue;
89                 }
90
91                 switch (*++cp) {
92                 case 'b':
93                         *dp++ = '\b';
94                         break;
95                 case 'f':
96                         *dp++ = '\f';
97                         break;
98                 case 'n':
99                         *dp++ = '\n';
100                         break;
101                 case 'r':
102                         *dp++ = '\r';
103                         break;
104                 case 't':
105                         *dp++ = '\t';
106                         break;
107                 case '\n':
108                         break;
109                 case '\0':
110                         cp--;
111                         /* fall */
112                 default:
113                         *dp++ = *cp;
114                         break;
115                 }
116         }
117         *dp = '\0';
118 }