Mercurial > hg > xemacs-beta
comparison lib-src/b2m.c @ 146:2af401a6ecca r20-2p1
Import from CVS: tag r20-2p1
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:34:46 +0200 |
parents | 376386a54a3c |
children | 59463afc5666 |
comparison
equal
deleted
inserted
replaced
145:e13feca31ba6 | 146:2af401a6ecca |
---|---|
11 * it's not much but it's free! | 11 * it's not much but it's free! |
12 * | 12 * |
13 * Ed Wilkinson | 13 * Ed Wilkinson |
14 * E.Wilkinson@massey.ac.nz | 14 * E.Wilkinson@massey.ac.nz |
15 * Mon Nov 7 15:54:06 PDT 1988 | 15 * Mon Nov 7 15:54:06 PDT 1988 |
16 * | 16 */ |
17 */ | 17 |
18 | 18 /* Made conformant to the GNU coding standards January, 1995 |
19 /* Synched up with: FSF 19.28. */ | 19 by Francesco Potorti` <pot@cnuce.cnr.it>. */ |
20 | 20 |
21 /* Serious bug: This program uses `gets', which is intrinsically | |
22 unreliable--long lines will cause crashes. | |
23 Someone should fix this program not to use `gets'. */ | |
24 #include <stdio.h> | 21 #include <stdio.h> |
25 #include <time.h> | 22 #include <time.h> |
26 #include <sys/types.h> | 23 #include <sys/types.h> |
27 #ifdef MSDOS | 24 #ifdef MSDOS |
28 #include <fcntl.h> | 25 #include <fcntl.h> |
29 #endif | 26 #endif |
30 | 27 |
31 #include <../src/config.h> | 28 #ifdef HAVE_CONFIG_H |
32 | 29 #include <config.h> |
33 #if __STDC__ || defined(STDC_HEADERS) | 30 /* On some systems, Emacs defines static as nothing for the sake |
34 # include <stdlib.h> | 31 of unexec. We don't want that here since we don't use unexec. */ |
35 # include <string.h> | 32 #undef static |
36 #else /* ! (__STDC__ || defined(STDC_HEADERS)) */ | 33 #endif |
37 # ifdef USG | 34 |
38 # include <string.h> | 35 #undef TRUE |
39 # else | 36 #define TRUE 1 |
40 # include <strings.h> | 37 #undef FALSE |
41 # endif | 38 #define FALSE 0 |
42 | 39 |
43 /* BSD's strings.h does not declare the type of strtok. */ | 40 /* Exit codes for success and failure. */ |
44 extern char *strtok (); | 41 #ifdef VMS |
45 #endif /* ! (__STDC__ || defined(STDC_HEADERS)) */ | 42 #define GOOD 1 |
46 | 43 #define BAD 0 |
47 | 44 #else |
48 #ifndef TRUE | 45 #define GOOD 0 |
49 #define TRUE (1) | 46 #define BAD 1 |
50 #endif | 47 #endif |
51 #ifndef FALSE | 48 |
52 #define FALSE (0) | 49 #define streq(s,t) (strcmp (s, t) == 0) |
53 #endif | 50 #define strneq(s,t,n) (strncmp (s, t, n) == 0) |
54 | 51 |
55 int header = FALSE, printing; | 52 typedef int logical; |
56 time_t ltoday; | 53 |
57 char from[256], labels[256], data[256], *p, *today; | 54 /* |
58 | 55 * A `struct linebuffer' is a structure which holds a line of text. |
59 void | 56 * `readline' reads a line from a stream into a linebuffer and works |
60 main (int argc, char **argv) | 57 * regardless of the length of the line. |
61 { | 58 */ |
59 struct linebuffer | |
60 { | |
61 long size; | |
62 char *buffer; | |
63 }; | |
64 | |
65 extern char *strtok(); | |
66 | |
67 long *xmalloc (), *xrealloc (); | |
68 char *concat (); | |
69 long readline (); | |
70 void fatal (); | |
71 | |
72 /* | |
73 * xnew -- allocate storage. SYNOPSIS: Type *xnew (int n, Type); | |
74 */ | |
75 #define xnew(n, Type) ((Type *) xmalloc ((n) * sizeof (Type))) | |
76 | |
77 | |
78 | |
79 char *progname; | |
80 | |
81 main (argc, argv) | |
82 int argc; | |
83 char **argv; | |
84 { | |
85 logical labels_saved, printing, header; | |
86 time_t ltoday; | |
87 char *labels, *p, *today; | |
88 struct linebuffer data; | |
89 | |
62 #ifdef MSDOS | 90 #ifdef MSDOS |
63 _fmode = O_BINARY; /* all of files are treated as binary files */ | 91 _fmode = O_BINARY; /* all of files are treated as binary files */ |
92 #if __DJGPP__ > 1 | |
93 if (!isatty (fileno (stdout))) | |
94 setmode (fileno (stdout), O_BINARY); | |
95 if (!isatty (fileno (stdin))) | |
96 setmode (fileno (stdin), O_BINARY); | |
97 #else /* not __DJGPP__ > 1 */ | |
64 (stdout)->_flag &= ~_IOTEXT; | 98 (stdout)->_flag &= ~_IOTEXT; |
65 (stdin)->_flag &= ~_IOTEXT; | 99 (stdin)->_flag &= ~_IOTEXT; |
66 #endif | 100 #endif /* not __DJGPP__ > 1 */ |
67 if ((argc >= 2) && strcmp (argv[1], "--help") == 0) | 101 #endif |
102 progname = argv[0]; | |
103 | |
104 if (argc != 1) | |
68 { | 105 { |
69 fprintf (stderr, "%s - a filter for Babyl -> Unix mail files\n", argv[0]); | 106 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname); |
70 fprintf (stderr, "Usage: %s < babylmailbox > unixmailbox\n", argv[0]); | 107 exit (GOOD); |
71 exit (0); | |
72 } | 108 } |
73 | 109 labels_saved = printing = header = FALSE; |
74 ltoday = time (0); | 110 ltoday = time (0); |
75 today = ctime (<oday); | 111 today = ctime (<oday); |
76 | 112 data.size = 200; |
77 /* BUG! Must not use gets in a reliable program! */ | 113 data.buffer = xnew (200, char); |
78 if (gets (data)) | 114 |
115 if (readline (&data, stdin) == 0 | |
116 || !strneq (data.buffer, "BABYL OPTIONS:", 14)) | |
117 fatal ("standard input is not a Babyl mailfile."); | |
118 | |
119 while (readline (&data, stdin) > 0) | |
79 { | 120 { |
80 if (strncmp (data, "BABYL OPTIONS:", 14)) | 121 if (streq (data.buffer, "*** EOOH ***") && !printing) |
81 { | 122 { |
82 fprintf (stderr, "%s: not a Babyl mailfile!\n", argv[0]); | 123 printing = header = TRUE; |
83 exit (-1); | 124 printf ("From \"Babyl to mail by %s\" %s", progname, today); |
84 } | 125 continue; |
85 else | 126 } |
86 printing = FALSE; | 127 |
128 if (data.buffer[0] == '\037') | |
129 { | |
130 if (data.buffer[1] == '\0') | |
131 continue; | |
132 else if (data.buffer[1] == '\f') | |
133 { | |
134 /* Save labels. */ | |
135 readline (&data, stdin); | |
136 p = strtok (data.buffer, " ,\r\n\t"); | |
137 labels = "X-Babyl-Labels: "; | |
138 | |
139 while (p = strtok (NULL, " ,\r\n\t")) | |
140 labels = concat (labels, p, ", "); | |
141 | |
142 p = &labels[strlen (labels) - 2]; | |
143 if (*p == ',') | |
144 *p = '\0'; | |
145 printing = header = FALSE; | |
146 labels_saved = TRUE; | |
147 continue; | |
148 } | |
149 } | |
150 | |
151 if ((data.buffer[0] == '\0') && header) | |
152 { | |
153 header = FALSE; | |
154 if (labels_saved) | |
155 puts (labels); | |
156 } | |
157 | |
158 if (printing) | |
159 puts (data.buffer); | |
87 } | 160 } |
88 else | 161 } |
89 exit (-1); | 162 |
90 if (printing) | 163 |
91 puts (data); | 164 |
92 | 165 /* |
93 while (gets (data)) | 166 * Return a newly-allocated string whose contents |
167 * concatenate those of s1, s2, s3. | |
168 */ | |
169 char * | |
170 concat (s1, s2, s3) | |
171 char *s1, *s2, *s3; | |
172 { | |
173 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3); | |
174 char *result = xnew (len1 + len2 + len3 + 1, char); | |
175 | |
176 strcpy (result, s1); | |
177 strcpy (result + len1, s2); | |
178 strcpy (result + len1 + len2, s3); | |
179 result[len1 + len2 + len3] = '\0'; | |
180 | |
181 return result; | |
182 } | |
183 | |
184 /* | |
185 * Read a line of text from `stream' into `linebuffer'. | |
186 * Return the number of characters read from `stream', | |
187 * which is the length of the line including the newline, if any. | |
188 */ | |
189 long | |
190 readline (linebuffer, stream) | |
191 struct linebuffer *linebuffer; | |
192 register FILE *stream; | |
193 { | |
194 char *buffer = linebuffer->buffer; | |
195 register char *p = linebuffer->buffer; | |
196 register char *pend; | |
197 int chars_deleted; | |
198 | |
199 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */ | |
200 | |
201 while (1) | |
94 { | 202 { |
95 | 203 register int c = getc (stream); |
96 #if 0 | 204 if (p == pend) |
97 /* What was this for? Does somebody have something against blank | 205 { |
98 lines? */ | 206 linebuffer->size *= 2; |
99 if (!strcmp (data, "")) | 207 buffer = (char *) xrealloc (buffer, linebuffer->size); |
100 exit (0); | 208 p += buffer - linebuffer->buffer; |
101 #endif | 209 pend = buffer + linebuffer->size; |
102 | 210 linebuffer->buffer = buffer; |
103 if (!strcmp (data, "*** EOOH ***") && !printing) | 211 } |
104 { | 212 if (c == EOF) |
105 printing = header = TRUE; | 213 { |
106 printf ("From %s %s", argv[0], today); | 214 chars_deleted = 0; |
107 continue; | 215 break; |
108 } | 216 } |
109 | 217 if (c == '\n') |
110 if (!strcmp (data, "\037\f")) | 218 { |
111 { | 219 if (p[-1] == '\r' && p > buffer) |
112 /* save labels */ | |
113 gets (data); | |
114 p = strtok (data, " ,\r\n\t"); | |
115 strcpy (labels, "X-Babyl-Labels: "); | |
116 | |
117 while ((p = strtok (NULL, " ,\r\n\t"))) | |
118 { | 220 { |
119 strcat (labels, p); | 221 *--p = '\0'; |
120 strcat (labels, ", "); | 222 chars_deleted = 2; |
121 } | 223 } |
122 | 224 else |
123 labels[strlen (labels) - 2] = '\0'; | 225 { |
124 printing = header = FALSE; | 226 *p = '\0'; |
125 continue; | 227 chars_deleted = 1; |
126 } | 228 } |
127 | 229 break; |
128 if (!strlen (data) && header) | 230 } |
129 { | 231 *p++ = c; |
130 header = FALSE; | |
131 if (strcmp (labels, "X-Babyl-Labels")) | |
132 puts (labels); | |
133 } | |
134 | |
135 if (printing) | |
136 puts (data); | |
137 } | 232 } |
138 } | 233 |
234 return (p - buffer + chars_deleted); | |
235 } | |
236 | |
237 /* | |
238 * Like malloc but get fatal error if memory is exhausted. | |
239 */ | |
240 long * | |
241 xmalloc (size) | |
242 unsigned int size; | |
243 { | |
244 long *result = (long *) malloc (size); | |
245 if (result == NULL) | |
246 fatal ("virtual memory exhausted"); | |
247 return result; | |
248 } | |
249 | |
250 long * | |
251 xrealloc (ptr, size) | |
252 char *ptr; | |
253 unsigned int size; | |
254 { | |
255 long *result = (long *) realloc (ptr, size); | |
256 if (result == NULL) | |
257 fatal ("virtual memory exhausted"); | |
258 return result; | |
259 } | |
260 | |
261 void | |
262 fatal (message) | |
263 { | |
264 fprintf (stderr, "%s: %s\n", progname, message); | |
265 exit (BAD); | |
266 } | |
267 |