Mercurial > emacs
annotate lib-src/fakemail.c @ 19173:04ed7c3f5cee
(detect_eol_type): If EOL representation does not
seem consistent, use no conversion.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 05 Aug 1997 18:19:33 +0000 |
parents | 36704f455f32 |
children | 48690fd8d40a |
rev | line source |
---|---|
18 | 1 /* sendmail-like interface to /bin/mail for system V, |
7307 | 2 Copyright (C) 1985, 1994 Free Software Foundation, Inc. |
18 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
37 | 6 GNU Emacs is free software; you can redistribute it and/or modify |
7 it under the terms of the GNU General Public License as published by | |
6109 | 8 the Free Software Foundation; either version 2, or (at your option) |
37 | 9 any later version. |
18 | 10 |
37 | 11 GNU Emacs is distributed in the hope that it will be useful, |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
14186
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
12840
diff
changeset
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
ee40177f6c68
Update FSF's address in the preamble.
Erik Naggum <erik@naggum.no>
parents:
12840
diff
changeset
|
19 Boston, MA 02111-1307, USA. */ |
18 | 20 |
21 | |
22 #define NO_SHORTNAMES | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
3219
diff
changeset
|
23 #include <../src/config.h> |
18 | 24 |
16218
32f82ca8b41f
Replaced all BSD with BSD_SYSTEM.
Karl Heuer <kwzh@gnu.org>
parents:
16121
diff
changeset
|
25 #if defined (BSD_SYSTEM) && !defined (BSD4_1) && !defined (USE_FAKEMAIL) |
18 | 26 /* This program isnot used in BSD, so just avoid loader complaints. */ |
15683 | 27 int |
18 | 28 main () |
29 { | |
16121 | 30 return 0; |
18 | 31 } |
32 #else /* not BSD 4.2 (or newer) */ | |
5447
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
33 #ifdef MSDOS |
15683 | 34 int |
5447
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
35 main () |
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
36 { |
16121 | 37 return 0; |
5447
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
38 } |
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
39 #else /* not MSDOS */ |
18 | 40 /* This conditional contains all the rest of the file. */ |
41 | |
42 /* These are defined in config in some versions. */ | |
43 | |
44 #ifdef static | |
45 #undef static | |
46 #endif | |
47 | |
48 #ifdef read | |
49 #undef read | |
50 #undef write | |
51 #undef open | |
52 #undef close | |
53 #endif | |
54 | |
15099
857388330750
[WINDOWSNT]: Include ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
55 #ifdef WINDOWSNT |
857388330750
[WINDOWSNT]: Include ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
56 #include "ntlib.h" |
857388330750
[WINDOWSNT]: Include ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
57 #endif |
857388330750
[WINDOWSNT]: Include ntlib.h.
Richard M. Stallman <rms@gnu.org>
parents:
14186
diff
changeset
|
58 |
18 | 59 #include <stdio.h> |
60 #include <string.h> | |
61 #include <ctype.h> | |
62 #include <time.h> | |
63 #include <pwd.h> | |
18841
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
64 |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
65 /* This is to declare cuserid. */ |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
66 #ifdef HAVE_UNISTD_H |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
67 #include <unistd.h> |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
68 #endif |
18 | 69 |
70 /* Type definitions */ | |
71 | |
72 #define boolean int | |
73 #define true 1 | |
74 #define false 0 | |
75 | |
76 /* Various lists */ | |
77 | |
78 struct line_record | |
79 { | |
80 char *string; | |
81 struct line_record *continuation; | |
82 }; | |
83 typedef struct line_record *line_list; | |
84 | |
85 struct header_record | |
86 { | |
87 line_list text; | |
88 struct header_record *next; | |
89 struct header_record *previous; | |
90 }; | |
91 typedef struct header_record *header; | |
92 | |
93 struct stream_record | |
94 { | |
95 FILE *handle; | |
96 int (*action)(); | |
97 struct stream_record *rest_streams; | |
98 }; | |
99 typedef struct stream_record *stream_list; | |
100 | |
101 /* A `struct linebuffer' is a structure which holds a line of text. | |
102 * `readline' reads a line from a stream into a linebuffer | |
103 * and works regardless of the length of the line. | |
104 */ | |
105 | |
106 struct linebuffer | |
107 { | |
108 long size; | |
109 char *buffer; | |
110 }; | |
111 | |
112 struct linebuffer lb; | |
113 | |
114 #define new_list() \ | |
115 ((line_list) xmalloc (sizeof (struct line_record))) | |
116 #define new_header() \ | |
117 ((header) xmalloc (sizeof (struct header_record))) | |
118 #define new_stream() \ | |
119 ((stream_list) xmalloc (sizeof (struct stream_record))) | |
120 #define alloc_string(nchars) \ | |
121 ((char *) xmalloc ((nchars) + 1)) | |
122 | |
123 /* Global declarations */ | |
124 | |
125 #define BUFLEN 1024 | |
126 #define KEYWORD_SIZE 256 | |
127 #define FROM_PREFIX "From" | |
128 #define MY_NAME "fakemail" | |
129 #define NIL ((line_list) NULL) | |
130 #define INITIAL_LINE_SIZE 200 | |
131 | |
132 #ifndef MAIL_PROGRAM_NAME | |
133 #define MAIL_PROGRAM_NAME "/bin/mail" | |
134 #endif | |
135 | |
136 static char *my_name; | |
137 static char *the_date; | |
138 static char *the_user; | |
139 static line_list file_preface; | |
140 static stream_list the_streams; | |
141 static boolean no_problems = true; | |
142 | |
143 extern FILE *popen (); | |
144 extern int fclose (), pclose (); | |
145 | |
146 #ifdef CURRENT_USER | |
147 extern struct passwd *getpwuid (); | |
148 extern unsigned short geteuid (); | |
149 static struct passwd *my_entry; | |
150 #define cuserid(s) \ | |
151 (my_entry = getpwuid (((int) geteuid ())), \ | |
152 my_entry->pw_name) | |
153 #endif | |
154 | |
155 /* Utilities */ | |
156 | |
157 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
158 | |
159 static void | |
160 error (s1, s2) | |
161 char *s1, *s2; | |
162 { | |
163 printf ("%s: ", my_name); | |
164 printf (s1, s2); | |
165 printf ("\n"); | |
166 no_problems = false; | |
167 } | |
168 | |
169 /* Print error message and exit. */ | |
170 | |
171 static void | |
172 fatal (s1, s2) | |
173 char *s1, *s2; | |
174 { | |
175 error (s1, s2); | |
176 exit (1); | |
177 } | |
178 | |
179 /* Like malloc but get fatal error if memory is exhausted. */ | |
180 | |
12833
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
181 static long * |
18 | 182 xmalloc (size) |
183 int size; | |
184 { | |
12833
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
185 long *result = (long *) malloc (((unsigned) size)); |
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
186 if (result == ((long *) NULL)) |
18 | 187 fatal ("virtual memory exhausted", 0); |
188 return result; | |
189 } | |
190 | |
12833
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
191 static long * |
18 | 192 xrealloc (ptr, size) |
12833
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
193 long *ptr; |
18 | 194 int size; |
195 { | |
12833
25464bf61eb1
(xmalloc, xrealloc): Use return-type long *.
Richard M. Stallman <rms@gnu.org>
parents:
10265
diff
changeset
|
196 long *result = (long *) realloc (ptr, ((unsigned) size)); |
12840
4e9a14304b8b
(xrealloc): Change cast to match return type.
Karl Heuer <kwzh@gnu.org>
parents:
12833
diff
changeset
|
197 if (result == ((long *) NULL)) |
18 | 198 fatal ("virtual memory exhausted"); |
199 return result; | |
200 } | |
201 | |
202 /* Initialize a linebuffer for use */ | |
203 | |
204 void | |
205 init_linebuffer (linebuffer) | |
206 struct linebuffer *linebuffer; | |
207 { | |
208 linebuffer->size = INITIAL_LINE_SIZE; | |
209 linebuffer->buffer = ((char *) xmalloc (INITIAL_LINE_SIZE)); | |
210 } | |
211 | |
212 /* Read a line of text from `stream' into `linebuffer'. | |
213 * Return the length of the line. | |
214 */ | |
215 | |
216 long | |
217 readline (linebuffer, stream) | |
218 struct linebuffer *linebuffer; | |
219 FILE *stream; | |
220 { | |
221 char *buffer = linebuffer->buffer; | |
222 char *p = linebuffer->buffer; | |
223 char *end = p + linebuffer->size; | |
224 | |
225 while (true) | |
226 { | |
227 int c = getc (stream); | |
228 if (p == end) | |
229 { | |
230 linebuffer->size *= 2; | |
231 buffer = ((char *) xrealloc (buffer, linebuffer->size)); | |
6992
ed57331fb222
(readline): Fix updating of p when buffer grows.
Richard M. Stallman <rms@gnu.org>
parents:
6954
diff
changeset
|
232 p = buffer + (p - linebuffer->buffer); |
6954
774fdc20d115
(readline): When extending the buffer,
Richard M. Stallman <rms@gnu.org>
parents:
6109
diff
changeset
|
233 end = buffer + linebuffer->size; |
18 | 234 linebuffer->buffer = buffer; |
235 } | |
236 if (c < 0 || c == '\n') | |
237 { | |
238 *p = 0; | |
239 break; | |
240 } | |
241 *p++ = c; | |
242 } | |
243 | |
244 return p - buffer; | |
245 } | |
246 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
247 /* Extract a colon-terminated keyword from the string FIELD. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
248 Return that keyword as a string stored in a static buffer. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
249 Store the address of the rest of the string into *REST. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
250 |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
251 If there is no keyword, return NULL and don't alter *REST. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
252 |
18 | 253 char * |
254 get_keyword (field, rest) | |
255 register char *field; | |
256 char **rest; | |
257 { | |
258 static char keyword[KEYWORD_SIZE]; | |
259 register char *ptr; | |
260 register char c; | |
261 | |
262 ptr = &keyword[0]; | |
263 c = *field++; | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
264 if (isspace (c) || c == ':') |
18 | 265 return ((char *) NULL); |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
266 *ptr++ = (islower (c) ? toupper (c) : c); |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
267 while (((c = *field++) != ':') && ! isspace (c)) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
268 *ptr++ = (islower (c) ? toupper (c) : c); |
18 | 269 *ptr++ = '\0'; |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
270 while (isspace (c)) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
271 c = *field++; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
272 if (c != ':') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
273 return ((char *) NULL); |
18 | 274 *rest = field; |
275 return &keyword[0]; | |
276 } | |
277 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
278 /* Nonzero if the string FIELD starts with a colon-terminated keyword. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
279 |
18 | 280 boolean |
281 has_keyword (field) | |
282 char *field; | |
283 { | |
284 char *ignored; | |
285 return (get_keyword (field, &ignored) != ((char *) NULL)); | |
286 } | |
287 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
288 /* Store the string FIELD, followed by any lines in THE_LIST, |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
289 into the buffer WHERE. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
290 Concatenate lines, putting just a space between them. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
291 Delete everything contained in parentheses. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
292 When a recipient name contains <...>, we discard |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
293 everything except what is inside the <...>. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
294 |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
295 We don't pay attention to overflowing WHERE; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
296 the caller has to make it big enough. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
297 |
18 | 298 char * |
299 add_field (the_list, field, where) | |
300 line_list the_list; | |
301 register char *field, *where; | |
302 { | |
303 register char c; | |
304 while (true) | |
305 { | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
306 char *this_recipient_where; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
307 int in_quotes = 0; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
308 |
18 | 309 *where++ = ' '; |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
310 this_recipient_where = where; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
311 |
18 | 312 while ((c = *field++) != '\0') |
313 { | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
314 if (c == '\\') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
315 *where++ = c; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
316 else if (c == '"') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
317 { |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
318 in_quotes = ! in_quotes; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
319 *where++ = c; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
320 } |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
321 else if (in_quotes) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
322 *where++ = c; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
323 else if (c == '(') |
18 | 324 { |
325 while (*field && *field != ')') ++field; | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
326 if (! (*field++)) break; /* no close */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
327 continue; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
328 } |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
329 else if (c == ',') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
330 { |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
331 *where++ = ' '; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
332 /* When we get to the end of one recipient, |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
333 don't discard it if the next one has <...>. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
334 this_recipient_where = where; |
18 | 335 } |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
336 else if (c == '<') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
337 /* Discard everything we got before the `<'. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
338 where = this_recipient_where; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
339 else if (c == '>') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
340 /* Discard the rest of this name that follows the `>'. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
341 { |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
342 while (*field && *field != ',') ++field; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
343 if (! (*field++)) break; /* no comma */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
344 continue; |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
345 } |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
346 else |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
347 *where++ = c; |
18 | 348 } |
349 if (the_list == NIL) break; | |
350 field = the_list->string; | |
351 the_list = the_list->continuation; | |
352 } | |
353 return where; | |
354 } | |
355 | |
356 line_list | |
357 make_file_preface () | |
358 { | |
359 char *the_string, *temp; | |
360 long idiotic_interface; | |
361 long prefix_length; | |
362 long user_length; | |
363 long date_length; | |
364 line_list result; | |
365 | |
366 prefix_length = strlen (FROM_PREFIX); | |
367 time (&idiotic_interface); | |
368 the_date = ctime (&idiotic_interface); | |
369 /* the_date has an unwanted newline at the end */ | |
370 date_length = strlen (the_date) - 1; | |
371 the_date[date_length] = '\0'; | |
372 temp = cuserid ((char *) NULL); | |
373 user_length = strlen (temp); | |
374 the_user = alloc_string (user_length + 1); | |
375 strcpy (the_user, temp); | |
18841
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
376 the_string = alloc_string (3 + prefix_length |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
377 + user_length |
36704f455f32
[HAVE_UNISTD_H]: Include unistd.h.
Richard M. Stallman <rms@gnu.org>
parents:
16218
diff
changeset
|
378 + date_length); |
18 | 379 temp = the_string; |
380 strcpy (temp, FROM_PREFIX); | |
381 temp = &temp[prefix_length]; | |
382 *temp++ = ' '; | |
383 strcpy (temp, the_user); | |
384 temp = &temp[user_length]; | |
385 *temp++ = ' '; | |
386 strcpy (temp, the_date); | |
387 result = new_list (); | |
388 result->string = the_string; | |
389 result->continuation = ((line_list) NULL); | |
390 return result; | |
391 } | |
392 | |
393 void | |
394 write_line_list (the_list, the_stream) | |
395 register line_list the_list; | |
396 FILE *the_stream; | |
397 { | |
398 for ( ; | |
399 the_list != ((line_list) NULL) ; | |
400 the_list = the_list->continuation) | |
401 { | |
402 fputs (the_list->string, the_stream); | |
403 putc ('\n', the_stream); | |
404 } | |
405 return; | |
406 } | |
407 | |
408 int | |
409 close_the_streams () | |
410 { | |
411 register stream_list rem; | |
412 for (rem = the_streams; | |
413 rem != ((stream_list) NULL); | |
414 rem = rem->rest_streams) | |
415 no_problems = (no_problems && | |
416 ((*rem->action) (rem->handle) == 0)); | |
417 the_streams = ((stream_list) NULL); | |
418 return (no_problems ? 0 : 1); | |
419 } | |
420 | |
421 void | |
422 add_a_stream (the_stream, closing_action) | |
423 FILE *the_stream; | |
424 int (*closing_action)(); | |
425 { | |
426 stream_list old = the_streams; | |
427 the_streams = new_stream (); | |
428 the_streams->handle = the_stream; | |
429 the_streams->action = closing_action; | |
430 the_streams->rest_streams = old; | |
431 return; | |
432 } | |
433 | |
434 int | |
435 my_fclose (the_file) | |
436 FILE *the_file; | |
437 { | |
438 putc ('\n', the_file); | |
439 fflush (the_file); | |
440 return fclose (the_file); | |
441 } | |
442 | |
443 boolean | |
444 open_a_file (name) | |
445 char *name; | |
446 { | |
447 FILE *the_stream = fopen (name, "a"); | |
448 if (the_stream != ((FILE *) NULL)) | |
449 { | |
450 add_a_stream (the_stream, my_fclose); | |
451 if (the_user == ((char *) NULL)) | |
452 file_preface = make_file_preface (); | |
453 write_line_list (file_preface, the_stream); | |
454 return true; | |
455 } | |
456 return false; | |
457 } | |
458 | |
459 void | |
460 put_string (s) | |
461 char *s; | |
462 { | |
463 register stream_list rem; | |
464 for (rem = the_streams; | |
465 rem != ((stream_list) NULL); | |
466 rem = rem->rest_streams) | |
467 fputs (s, rem->handle); | |
468 return; | |
469 } | |
470 | |
471 void | |
20 | 472 put_line (string) |
473 char *string; | |
18 | 474 { |
475 register stream_list rem; | |
476 for (rem = the_streams; | |
477 rem != ((stream_list) NULL); | |
478 rem = rem->rest_streams) | |
479 { | |
20 | 480 char *s = string; |
481 int column = 0; | |
482 | |
483 /* Divide STRING into lines. */ | |
484 while (*s != 0) | |
485 { | |
486 char *breakpos; | |
487 | |
5959
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
488 /* Find the last char that fits. */ |
20 | 489 for (breakpos = s; *breakpos && column < 78; ++breakpos) |
490 { | |
491 if (*breakpos == '\t') | |
492 column += 8; | |
493 else | |
494 column++; | |
495 } | |
5959
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
496 /* If we didn't reach end of line, break the line. */ |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
497 if (*breakpos) |
20 | 498 { |
5959
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
499 /* Back up to just after the last comma that fits. */ |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
500 while (breakpos != s && breakpos[-1] != ',') --breakpos; |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
501 |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
502 if (breakpos == s) |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
503 { |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
504 /* If no comma fits, move past the first address anyway. */ |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
505 while (*breakpos != 0 && *breakpos != ',') ++breakpos; |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
506 if (*breakpos != 0) |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
507 /* Include the comma after it. */ |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
508 ++breakpos; |
e4337a7bbe32
(put_line): Don't break the line if it all fits.
Richard M. Stallman <rms@gnu.org>
parents:
5447
diff
changeset
|
509 } |
20 | 510 } |
511 /* Output that much, then break the line. */ | |
512 fwrite (s, 1, breakpos - s, rem->handle); | |
513 column = 8; | |
514 | |
515 /* Skip whitespace and prepare to print more addresses. */ | |
516 s = breakpos; | |
517 while (*s == ' ' || *s == '\t') ++s; | |
3219
1aa8fa0a569e
(put_line): Don't output \n\t unless more text follows.
Richard M. Stallman <rms@gnu.org>
parents:
37
diff
changeset
|
518 if (*s != 0) |
1aa8fa0a569e
(put_line): Don't output \n\t unless more text follows.
Richard M. Stallman <rms@gnu.org>
parents:
37
diff
changeset
|
519 fputs ("\n\t", rem->handle); |
20 | 520 } |
18 | 521 putc ('\n', rem->handle); |
522 } | |
523 return; | |
524 } | |
525 | |
526 #define mail_error error | |
527 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
528 /* Handle an FCC field. FIELD is the text of the first line (after |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
529 the header name), and THE_LIST holds the continuation lines if any. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
530 Call open_a_file for each file. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
531 |
18 | 532 void |
533 setup_files (the_list, field) | |
534 register line_list the_list; | |
535 register char *field; | |
536 { | |
537 register char *start; | |
538 register char c; | |
539 while (true) | |
540 { | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
541 while (((c = *field) != '\0') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
542 && (c == ' ' |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
543 || c == '\t' |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
544 || c == ',')) |
18 | 545 field += 1; |
546 if (c != '\0') | |
547 { | |
548 start = field; | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
549 while (((c = *field) != '\0') |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
550 && c != ' ' |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
551 && c != '\t' |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
552 && c != ',') |
18 | 553 field += 1; |
554 *field = '\0'; | |
555 if (!open_a_file (start)) | |
556 mail_error ("Could not open file %s", start); | |
557 *field = c; | |
558 if (c != '\0') continue; | |
559 } | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
560 if (the_list == ((line_list) NULL)) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
561 return; |
18 | 562 field = the_list->string; |
563 the_list = the_list->continuation; | |
564 } | |
565 } | |
566 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
567 /* Compute the total size of all recipient names stored in THE_HEADER. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
568 The result says how big to make the buffer to pass to parse_header. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
569 |
18 | 570 int |
571 args_size (the_header) | |
572 header the_header; | |
573 { | |
574 register header old = the_header; | |
575 register line_list rem; | |
576 register int size = 0; | |
577 do | |
578 { | |
579 char *field; | |
580 register char *keyword = get_keyword (the_header->text->string, &field); | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
581 if ((strcmp (keyword, "TO") == 0) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
582 || (strcmp (keyword, "CC") == 0) |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
583 || (strcmp (keyword, "BCC") == 0)) |
18 | 584 { |
585 size += 1 + strlen (field); | |
586 for (rem = the_header->text->continuation; | |
587 rem != NIL; | |
588 rem = rem->continuation) | |
589 size += 1 + strlen (rem->string); | |
590 } | |
591 the_header = the_header->next; | |
592 } while (the_header != old); | |
593 return size; | |
594 } | |
595 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
596 /* Scan the header described by the lists THE_HEADER, |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
597 and put all recipient names into the buffer WHERE. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
598 Precede each recipient name with a space. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
599 |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
600 Also, if the header has any FCC fields, call setup_files for each one. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
601 |
18 | 602 parse_header (the_header, where) |
603 header the_header; | |
604 register char *where; | |
605 { | |
606 register header old = the_header; | |
607 do | |
608 { | |
609 char *field; | |
610 register char *keyword = get_keyword (the_header->text->string, &field); | |
611 if (strcmp (keyword, "TO") == 0) | |
612 where = add_field (the_header->text->continuation, field, where); | |
613 else if (strcmp (keyword, "CC") == 0) | |
614 where = add_field (the_header->text->continuation, field, where); | |
615 else if (strcmp (keyword, "BCC") == 0) | |
616 { | |
617 where = add_field (the_header->text->continuation, field, where); | |
618 the_header->previous->next = the_header->next; | |
619 the_header->next->previous = the_header->previous; | |
620 } | |
621 else if (strcmp (keyword, "FCC") == 0) | |
622 setup_files (the_header->text->continuation, field); | |
623 the_header = the_header->next; | |
624 } while (the_header != old); | |
625 *where = '\0'; | |
626 return; | |
627 } | |
628 | |
10265
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
629 /* Read lines from the input until we get a blank line. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
630 Create a list of `header' objects, one for each header field, |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
631 each of which points to a list of `line_list' objects, |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
632 one for each line in that field. |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
633 Continuation lines are grouped in the headers they continue. */ |
c53a70ec8d85
(xmalloc, xrealloc): Add casts.
Richard M. Stallman <rms@gnu.org>
parents:
9621
diff
changeset
|
634 |
18 | 635 header |
636 read_header () | |
637 { | |
638 register header the_header = ((header) NULL); | |
639 register line_list *next_line = ((line_list *) NULL); | |
640 | |
641 init_linebuffer (&lb); | |
642 | |
643 do | |
644 { | |
645 long length; | |
646 register char *line; | |
647 | |
648 readline (&lb, stdin); | |
649 line = lb.buffer; | |
650 length = strlen (line); | |
651 if (length == 0) break; | |
652 | |
653 if (has_keyword (line)) | |
654 { | |
655 register header old = the_header; | |
656 the_header = new_header (); | |
657 if (old == ((header) NULL)) | |
658 { | |
659 the_header->next = the_header; | |
660 the_header->previous = the_header; | |
661 } | |
662 else | |
663 { | |
664 the_header->previous = old; | |
665 the_header->next = old->next; | |
666 old->next = the_header; | |
667 } | |
668 next_line = &(the_header->text); | |
669 } | |
670 | |
671 if (next_line == ((line_list *) NULL)) | |
672 { | |
673 /* Not a valid header */ | |
674 exit (1); | |
675 } | |
676 *next_line = new_list (); | |
677 (*next_line)->string = alloc_string (length); | |
678 strcpy (((*next_line)->string), line); | |
679 next_line = &((*next_line)->continuation); | |
680 *next_line = NIL; | |
681 | |
682 } while (true); | |
683 | |
684 return the_header->next; | |
685 } | |
686 | |
687 void | |
688 write_header (the_header) | |
689 header the_header; | |
690 { | |
691 register header old = the_header; | |
692 do | |
693 { | |
694 register line_list the_list; | |
695 for (the_list = the_header->text; | |
696 the_list != NIL; | |
697 the_list = the_list->continuation) | |
698 put_line (the_list->string); | |
699 the_header = the_header->next; | |
700 } while (the_header != old); | |
701 put_line (""); | |
702 return; | |
703 } | |
704 | |
15683 | 705 int |
18 | 706 main (argc, argv) |
707 int argc; | |
708 char **argv; | |
709 { | |
710 char *command_line; | |
711 header the_header; | |
712 long name_length; | |
713 char *mail_program_name; | |
714 char buf[BUFLEN + 1]; | |
715 register int size; | |
716 FILE *the_pipe; | |
717 | |
718 extern char *getenv (); | |
719 | |
720 mail_program_name = getenv ("FAKEMAILER"); | |
721 if (!(mail_program_name && *mail_program_name)) | |
722 mail_program_name = MAIL_PROGRAM_NAME; | |
723 name_length = strlen (mail_program_name); | |
724 | |
725 my_name = MY_NAME; | |
726 the_streams = ((stream_list) NULL); | |
727 the_date = ((char *) NULL); | |
728 the_user = ((char *) NULL); | |
729 | |
730 the_header = read_header (); | |
731 command_line = alloc_string (name_length + args_size (the_header)); | |
732 strcpy (command_line, mail_program_name); | |
733 parse_header (the_header, &command_line[name_length]); | |
734 | |
735 the_pipe = popen (command_line, "w"); | |
736 if (the_pipe == ((FILE *) NULL)) | |
737 fatal ("cannot open pipe to real mailer"); | |
738 | |
739 add_a_stream (the_pipe, pclose); | |
740 | |
741 write_header (the_header); | |
742 | |
743 /* Dump the message itself */ | |
744 | |
745 while (!feof (stdin)) | |
746 { | |
747 size = fread (buf, 1, BUFLEN, stdin); | |
748 buf[size] = '\0'; | |
749 put_string (buf); | |
750 } | |
751 | |
752 exit (close_the_streams ()); | |
753 } | |
754 | |
5447
6f0905b05218
(main) [MSDOS]: Dummy stub just to make the file compile.
Richard M. Stallman <rms@gnu.org>
parents:
4696
diff
changeset
|
755 #endif /* not MSDOS */ |
18 | 756 #endif /* not BSD 4.2 (or newer) */ |