aboutsummaryrefslogtreecommitdiffstats
path: root/lib-src
diff options
context:
space:
mode:
authorRichard M. Stallman1995-01-09 18:31:22 +0000
committerRichard M. Stallman1995-01-09 18:31:22 +0000
commite19bdc1471737b32c839b81daf1916556e13986c (patch)
treea3d21f2a11d70f4a07f69850f0fb421dc6faac2e /lib-src
parent44dc5252922b9a7bacb679f57e95e5a02a87fa13 (diff)
downloademacs-e19bdc1471737b32c839b81daf1916556e13986c.tar.gz
emacs-e19bdc1471737b32c839b81daf1916556e13986c.zip
(concat, xmalloc, xrealloc, readline, xnew): Four new
functions and a macro that allow the program to work on input lines of whatever length. Copied from etags.c. (fatal): Print a fatal error message and exit. (main): Use the new functions. Fixed a bug that made a \037 char appear at the end of the output.
Diffstat (limited to 'lib-src')
-rw-r--r--lib-src/b2m.c253
1 files changed, 194 insertions, 59 deletions
diff --git a/lib-src/b2m.c b/lib-src/b2m.c
index ededf304f00..416db6385ff 100644
--- a/lib-src/b2m.c
+++ b/lib-src/b2m.c
@@ -15,6 +15,9 @@
15 * Mon Nov 7 15:54:06 PDT 1988 15 * Mon Nov 7 15:54:06 PDT 1988
16 */ 16 */
17 17
18/* Made conformant to the GNU coding standards January, 1995
19 by Francesco Potorti` <pot@cnuce.cnr.it>. */
20
18#include <stdio.h> 21#include <stdio.h>
19#include <time.h> 22#include <time.h>
20#include <sys/types.h> 23#include <sys/types.h>
@@ -22,101 +25,233 @@
22#include <fcntl.h> 25#include <fcntl.h>
23#endif 26#endif
24 27
25#include <../src/config.h> 28#ifdef HAVE_CONFIG_H
29#include <config.h>
30/* On some systems, Emacs defines static as nothing for the sake
31 of unexec. We don't want that here since we don't use unexec. */
32#undef static
33#endif
26 34
27/* BSD's strings.h does not declare the type of strtok. */ 35#undef TRUE
28extern char *strtok (); 36#define TRUE 1
37#undef FALSE
38#define FALSE 0
29 39
30#ifndef TRUE 40/* Exit codes for success and failure. */
31#define TRUE (1) 41#ifdef VMS
32#endif 42#define GOOD 1
33#ifndef FALSE 43#define BAD 0
34#define FALSE (0) 44#else
45#define GOOD 0
46#define BAD 1
35#endif 47#endif
36 48
37#define MAX_DATA_LEN 256 /* size for from[], labels[], and data[] arrays */ 49#define streq(s,t) (strcmp (s, t) == 0)
50#define strneq(s,t,n) (strncmp (s, t, n) == 0)
51
52typedef int logical;
53
54/*
55 * A `struct linebuffer' is a structure which holds a line of text.
56 * `readline' reads a line from a stream into a linebuffer and works
57 * regardless of the length of the line.
58 */
59struct linebuffer
60{
61 long size;
62 char *buffer;
63};
64
65extern char *strtok();
38 66
39int header = FALSE, printing; 67char *xmalloc (), *xrealloc ();
40time_t ltoday; 68char *concat ();
41char from[MAX_DATA_LEN], labels[MAX_DATA_LEN], data[MAX_DATA_LEN], *p, *today; 69long readline ();
70void 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
79char *progname;
42 80
43int
44main (argc, argv) 81main (argc, argv)
45 int argc; 82 int argc;
46 char **argv; 83 char **argv;
47{ 84{
85 logical labels_saved, printing, header;
86 time_t ltoday;
87 char *labels, *p, *today;
88 struct linebuffer data;
89
48#ifdef MSDOS 90#ifdef MSDOS
49 _fmode = O_BINARY; /* all of files are treated as binary files */ 91 _fmode = O_BINARY; /* all of files are treated as binary files */
50 (stdout)->_flag &= ~_IOTEXT; 92 (stdout)->_flag &= ~_IOTEXT;
51 (stdin)->_flag &= ~_IOTEXT; 93 (stdin)->_flag &= ~_IOTEXT;
52#endif 94#endif
53 if (argc >= 2 && strcmp (argv[1], "--help") == 0) 95 if (argc != 1)
54 { 96 {
55 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", argv[0]); 97 fprintf (stderr, "Usage: %s <babylmailbox >unixmailbox\n", progname);
56 exit (0); 98 exit (GOOD);
57 } 99 }
100 labels_saved = printing = header = FALSE;
101 progname = argv[0];
58 ltoday = time (0); 102 ltoday = time (0);
59 today = ctime (&ltoday); 103 today = ctime (&ltoday);
104 data.size = 200;
105 data.buffer = xnew (200, char);
60 106
61 if (fgets (data, MAX_DATA_LEN, stdin)) 107 if (readline (&data, stdin) == 0
62 { 108 || !strneq (data.buffer, "BABYL OPTIONS:", 14))
63 if (strncmp (data, "BABYL OPTIONS:", 14)) 109 fatal ("standard input is not a Babyl mailfile.");
64 {
65 fprintf (stderr, "%s: not a Babyl mailfile!\n", argv[0]);
66 exit (-1);
67 }
68 else
69 printing = FALSE;
70 }
71 else
72 exit (-1);
73 if (printing)
74 puts (data);
75 110
76 while (fgets (data, MAX_DATA_LEN, stdin)) 111 while (readline (&data, stdin) > 0)
77 { 112 {
78 113 if (streq (data.buffer, "*** EOOH ***") && !printing)
79#if 0
80 /* What was this for? Does somebody have something against blank
81 lines? */
82 if (!strcmp (data, ""))
83 exit (0);
84#endif
85
86 if (!strcmp (data, "*** EOOH ***") && !printing)
87 { 114 {
88 printing = header = TRUE; 115 printing = header = TRUE;
89 printf ("From %s %s", argv[0], today); 116 printf ("From Babyl to mail by %s %s", progname, today);
90 continue; 117 continue;
91 } 118 }
92 119
93 if (!strcmp (data, "\037\f")) 120 if (data.buffer[0] == '\037')
94 { 121 {
95 /* save labels */ 122 if (data.buffer[1] == '\0')
96 fgets (data, MAX_DATA_LEN, stdin); 123 continue;
97 p = strtok (data, " ,\r\n\t"); 124 else if (data.buffer[1] == '\f')
98 strcpy (labels, "X-Babyl-Labels: ");
99
100 while (p = strtok (NULL, " ,\r\n\t"))
101 { 125 {
102 strcat (labels, p); 126 /* Save labels. */
103 strcat (labels, ", "); 127 readline (&data, stdin);
104 } 128 p = strtok (data.buffer, " ,\r\n\t");
129 labels = "X-Babyl-Labels: ";
105 130
106 labels[strlen (labels) - 2] = '\0'; 131 while (p = strtok (NULL, " ,\r\n\t"))
107 printing = header = FALSE; 132 labels = concat (labels, p, ", ");
108 continue; 133
134 labels[strlen (labels) - 2] = '\0';
135 printing = header = FALSE;
136 labels_saved = TRUE;
137 continue;
138 }
109 } 139 }
110 140
111 if (!strlen (data) && header) 141 if ((data.buffer[0] == '\0') && header)
112 { 142 {
113 header = FALSE; 143 header = FALSE;
114 if (strcmp (labels, "X-Babyl-Labels")) 144 if (labels_saved)
115 puts (labels); 145 puts (labels);
116 } 146 }
117 147
118 if (printing) 148 if (printing)
119 puts (data); 149 puts (data.buffer);
150 }
151}
152
153
154
155/*
156 * Return a newly-allocated string whose contents
157 * concatenate those of s1, s2, s3.
158 */
159char *
160concat (s1, s2, s3)
161 char *s1, *s2, *s3;
162{
163 int len1 = strlen (s1), len2 = strlen (s2), len3 = strlen (s3);
164 char *result = xnew (len1 + len2 + len3 + 1, char);
165
166 strcpy (result, s1);
167 strcpy (result + len1, s2);
168 strcpy (result + len1 + len2, s3);
169 result[len1 + len2 + len3] = '\0';
170
171 return result;
172}
173
174/*
175 * Read a line of text from `stream' into `linebuffer'.
176 * Return the number of characters read from `stream',
177 * which is the length of the line including the newline, if any.
178 */
179long
180readline (linebuffer, stream)
181 struct linebuffer *linebuffer;
182 register FILE *stream;
183{
184 char *buffer = linebuffer->buffer;
185 register char *p = linebuffer->buffer;
186 register char *pend;
187 int chars_deleted;
188
189 pend = p + linebuffer->size; /* Separate to avoid 386/IX compiler bug. */
190
191 while (1)
192 {
193 register int c = getc (stream);
194 if (p == pend)
195 {
196 linebuffer->size *= 2;
197 buffer = (char *) xrealloc (buffer, linebuffer->size);
198 p += buffer - linebuffer->buffer;
199 pend = buffer + linebuffer->size;
200 linebuffer->buffer = buffer;
201 }
202 if (c == EOF)
203 {
204 chars_deleted = 0;
205 break;
206 }
207 if (c == '\n')
208 {
209 if (p[-1] == '\r' && p > buffer)
210 {
211 *--p = '\0';
212 chars_deleted = 2;
213 }
214 else
215 {
216 *p = '\0';
217 chars_deleted = 1;
218 }
219 break;
220 }
221 *p++ = c;
120 } 222 }
121 return 0; 223
224 return (p - buffer + chars_deleted);
122} 225}
226
227/*
228 * Like malloc but get fatal error if memory is exhausted.
229 */
230char *
231xmalloc (size)
232 unsigned int size;
233{
234 char *result = (char *) malloc (size);
235 if (result == NULL)
236 fatal ("virtual memory exhausted");
237 return result;
238}
239
240char *
241xrealloc (ptr, size)
242 char *ptr;
243 unsigned int size;
244{
245 char *result = (char *) realloc (ptr, size);
246 if (result == NULL)
247 fatal ("virtual memory exhausted");
248 return result;
249}
250
251void
252fatal (message)
253{
254 fprintf (stderr, "%s: %s\n", progname, message);
255 exit (BAD);
256}
257