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