Mercurial > hg > xemacs-beta
annotate lib-src/fakemail.c @ 5346:b4ef3128160c
Fix some testsuite failures, #'delete, #'delq, #'remove, #'remq.
lisp/ChangeLog addition:
2011-01-23 Aidan Kehoe <kehoea@parhasard.net>
* cl-macs.el (delete):
* cl-macs.el (delq):
* cl-macs.el (remove):
* cl-macs.el (remq):
Don't use the compiler macro if these functions were given the
wrong number of arguments, as happens in lisp-tests.el.
* cl-seq.el (remove, remq): Removed.
I added these to subr.el, and forgot to remove them from here.
tests/ChangeLog addition:
2011-01-23 Aidan Kehoe <kehoea@parhasard.net>
* automated/lisp-tests.el (test-fun):
#'delete* and friends can now throw a wrong-type-argument if
handed a non-sequence; accept this too when checking for an error
when passing a fixnum as the SEQUENCE argument.
Check #'remove*, #'remove and #'remq too.
author | Aidan Kehoe <kehoea@parhasard.net> |
---|---|
date | Sun, 23 Jan 2011 13:13:54 +0000 |
parents | fde0802ee3e0 |
children | a9094f28f9a9 |
rev | line source |
---|---|
428 | 1 /* sendmail-like interface to /bin/mail for system V, |
2 Copyright (C) 1985, 1994 Free Software Foundation, Inc. | |
3 | |
613 | 4 This file is part of XEmacs. |
428 | 5 |
613 | 6 XEmacs is free software; you can redistribute it and/or modify |
428 | 7 it under the terms of the GNU General Public License as published by |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
613 | 11 XEmacs is distributed in the hope that it will be useful, |
428 | 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 | |
613 | 17 along with XEmacs; see the file COPYING. If not, write to |
5287
cd167465bf69
More permission consistency.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
4932
diff
changeset
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
428 | 19 Boston, MA 02111-1307, USA. */ |
20 | |
21 /* Synched up with: FSF 19.28. */ | |
22 | |
23 #define NO_SHORTNAMES | |
438 | 24 #include <config.h> |
428 | 25 |
26 #if defined (BSD) && !defined (BSD4_1) && !defined (USE_FAKEMAIL) | |
27 /* This program is not used in BSD, so just avoid loader complaints. */ | |
28 int | |
29 main (int argc, char *argv[]) | |
30 { | |
31 return 0; | |
32 } | |
33 #elif defined (LINUX) | |
34 #include <stdio.h> | |
35 #include <stdlib.h> | |
36 int | |
2509 | 37 main (int argc, char *argv[]) |
428 | 38 { |
39 /* Linux /bin/mail, if it exists, is NOT the Unix v7 mail that | |
40 fakemail depends on! This causes garbled mail. Better to | |
41 output an error message. */ | |
42 fprintf (stderr, "Sorry, fakemail does not work on Linux.\n"); | |
43 fprintf (stderr, "Make sure you have the sendmail program, and\n"); | |
44 fprintf (stderr, "set the Lisp variable `sendmail-program' to point\n"); | |
45 fprintf (stderr, "to the path of the sendmail binary.\n"); | |
46 return 1; | |
47 } | |
48 #else /* not BSD 4.2 (or newer) */ | |
49 | |
50 /* These are defined in config in some versions. */ | |
51 | |
52 #ifdef static | |
53 #undef static | |
54 #endif | |
55 | |
56 #ifdef read | |
57 #undef read | |
58 #undef write | |
59 #undef open | |
60 #undef close | |
61 #endif | |
62 | |
63 #include <stdio.h> | |
64 #if __STDC__ || defined(STDC_HEADERS) | |
65 #include <stdlib.h> | |
66 #include <unistd.h> | |
67 #endif | |
68 #include <string.h> | |
69 #include <ctype.h> | |
70 #include <time.h> | |
71 #include <pwd.h> | |
72 | |
73 /* Type definitions */ | |
74 | |
75 #define boolean int | |
76 #define true 1 | |
77 #define false 0 | |
78 | |
79 /* Various lists */ | |
80 | |
81 struct line_record | |
82 { | |
83 char *string; | |
84 struct line_record *continuation; | |
85 }; | |
86 typedef struct line_record *line_list; | |
87 | |
88 struct header_record | |
89 { | |
90 line_list text; | |
91 struct header_record *next; | |
92 struct header_record *previous; | |
93 }; | |
94 typedef struct header_record *header; | |
95 | |
96 struct stream_record | |
97 { | |
98 FILE *handle; | |
99 int (*action)(FILE *); | |
100 struct stream_record *rest_streams; | |
101 }; | |
102 typedef struct stream_record *stream_list; | |
103 | |
104 /* A `struct linebuffer' is a structure which holds a line of text. | |
105 * `readline' reads a line from a stream into a linebuffer | |
106 * and works regardless of the length of the line. | |
107 */ | |
108 | |
109 struct linebuffer | |
110 { | |
440 | 111 size_t size; |
428 | 112 char *buffer; |
113 }; | |
114 | |
115 struct linebuffer lb; | |
116 | |
117 #define new_list() \ | |
118 ((line_list) xmalloc (sizeof (struct line_record))) | |
119 #define new_header() \ | |
120 ((header) xmalloc (sizeof (struct header_record))) | |
121 #define new_stream() \ | |
122 ((stream_list) xmalloc (sizeof (struct stream_record))) | |
123 #define alloc_string(nchars) \ | |
124 ((char *) xmalloc ((nchars) + 1)) | |
125 | |
126 /* Global declarations */ | |
127 | |
128 #define BUFLEN 1024 | |
129 #define KEYWORD_SIZE 256 | |
130 #define FROM_PREFIX "From" | |
131 #define MY_NAME "fakemail" | |
132 #define NIL ((line_list) NULL) | |
133 #define INITIAL_LINE_SIZE 200 | |
134 #define MAIL_PROGRAM_NAME "/bin/mail" | |
135 | |
442 | 136 static const char *my_name; |
428 | 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 #if !__STDC__ && !defined(STDC_HEADERS) | |
144 extern FILE *popen (); | |
145 extern int fclose (), pclose (); | |
146 extern char *malloc (), *realloc (); | |
147 #endif | |
148 | |
5341 | 149 #if defined(__FreeBSD__) |
150 #include <osreldate.h> | |
151 #endif | |
152 | |
2687 | 153 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000 |
154 #define CURRENT_USER | |
155 #endif | |
156 | |
428 | 157 #ifdef CURRENT_USER |
158 extern struct passwd *getpwuid (); | |
2687 | 159 #if defined(__FreeBSD_version) && __FreeBSD_version >= 400000 |
160 extern uid_t geteuid (); | |
161 #else | |
162 extern unsigned short geteuid (); | |
163 #endif | |
428 | 164 static struct passwd *my_entry; |
165 #define cuserid(s) \ | |
434 | 166 (my_entry = getpwuid ((int) geteuid ()), \ |
428 | 167 my_entry->pw_name) |
168 #endif | |
169 | |
170 /* Utilities */ | |
171 | |
172 /* Print error message. `s1' is printf control string, `s2' is arg for it. */ | |
173 | |
174 static void | |
442 | 175 error (const char *s1, const char *s2) |
428 | 176 { |
177 printf ("%s: ", my_name); | |
178 printf (s1, s2); | |
179 printf ("\n"); | |
180 no_problems = false; | |
181 } | |
182 | |
183 /* Print error message and exit. */ | |
184 | |
185 static void | |
442 | 186 fatal (const char *s1, const char *s2) |
428 | 187 { |
188 error (s1, s2); | |
189 exit (1); | |
190 } | |
191 | |
192 /* Like malloc but get fatal error if memory is exhausted. */ | |
193 | |
440 | 194 static void * |
428 | 195 xmalloc (size_t size) |
196 { | |
440 | 197 void *result = malloc (size); |
198 if (result == NULL) | |
428 | 199 fatal ("virtual memory exhausted", (char *) 0); |
200 return result; | |
201 } | |
202 | |
440 | 203 static void * |
204 xrealloc (void *ptr, size_t size) | |
428 | 205 { |
440 | 206 void *result = realloc (ptr, size); |
207 if (result == NULL) | |
428 | 208 fatal ("virtual memory exhausted", (char *) 0); |
209 return result; | |
210 } | |
211 | |
212 /* Initialize a linebuffer for use */ | |
213 | |
214 static void | |
215 init_linebuffer (struct linebuffer *linebuffer) | |
216 { | |
217 linebuffer->size = INITIAL_LINE_SIZE; | |
440 | 218 linebuffer->buffer = (char *) xmalloc (INITIAL_LINE_SIZE); |
428 | 219 } |
220 | |
221 /* Read a line of text from `stream' into `linebuffer'. | |
222 * Return the length of the line. | |
223 */ | |
224 | |
225 static long | |
226 readline (struct linebuffer *linebuffer, FILE *stream) | |
227 { | |
228 char *buffer = linebuffer->buffer; | |
229 char *p = linebuffer->buffer; | |
230 char *end = p + linebuffer->size; | |
231 | |
232 while (true) | |
233 { | |
234 int c = getc (stream); | |
235 if (p == end) | |
236 { | |
237 linebuffer->size *= 2; | |
440 | 238 buffer = (char *) xrealloc (buffer, linebuffer->size); |
428 | 239 p = buffer + (p - linebuffer->buffer); |
240 end = buffer + linebuffer->size; | |
241 linebuffer->buffer = buffer; | |
242 } | |
243 if (c < 0 || c == '\n') | |
244 { | |
245 *p = 0; | |
246 break; | |
247 } | |
248 *p++ = c; | |
249 } | |
250 | |
251 return p - buffer; | |
252 } | |
253 | |
254 static char * | |
255 get_keyword (register char *field, char **rest) | |
256 { | |
257 static char keyword[KEYWORD_SIZE]; | |
258 register char *ptr; | |
259 register char c; | |
260 | |
261 ptr = &keyword[0]; | |
262 c = *field++; | |
263 if ((isspace ((int) (unsigned char) c)) || (c == ':')) | |
264 return (char *) NULL; | |
265 *ptr++ = ((islower ((int) (unsigned char) c)) ? | |
266 (toupper ((int) (unsigned char) c)) : c); | |
267 while (((c = *field++) != ':') && | |
268 (!(isspace ((int) (unsigned char) c)))) | |
269 *ptr++ = ((islower ((int) (unsigned char) c)) ? | |
270 (toupper ((int) (unsigned char) c)) : c); | |
271 *ptr++ = '\0'; | |
272 while (isspace ((int) (unsigned char) c)) c = *field++; | |
273 if (c != ':') return (char *) NULL; | |
274 *rest = field; | |
275 return &keyword[0]; | |
276 } | |
277 | |
278 static boolean | |
279 has_keyword (char *field) | |
280 { | |
281 char *ignored; | |
440 | 282 return (get_keyword (field, &ignored) != (char *) NULL); |
428 | 283 } |
284 | |
285 static char * | |
286 add_field (line_list the_list, register char *field, register char *where) | |
287 { | |
288 register char c; | |
289 while (true) | |
290 { | |
291 *where++ = ' '; | |
292 while ((c = *field++) != '\0') | |
293 { | |
294 if (c == '(') | |
295 { | |
296 while (*field && *field != ')') ++field; | |
297 if (! (*field++)) break; /* no closer */ | |
298 if (! (*field)) break; /* closerNULL */ | |
299 c = *field; | |
300 } | |
301 *where++ = ((c == ','||c=='>'||c=='<') ? ' ' : c); | |
302 } | |
303 if (the_list == NIL) break; | |
304 field = the_list->string; | |
305 the_list = the_list->continuation; | |
306 } | |
307 return where; | |
308 } | |
309 | |
310 static line_list | |
311 make_file_preface (void) | |
312 { | |
313 char *the_string, *temp; | |
442 | 314 time_t idiotic_interface; |
428 | 315 long prefix_length; |
316 long user_length; | |
317 long date_length; | |
318 line_list result; | |
319 | |
320 prefix_length = strlen (FROM_PREFIX); | |
321 time (&idiotic_interface); | |
322 the_date = ctime (&idiotic_interface); | |
323 /* the_date has an unwanted newline at the end */ | |
324 date_length = strlen (the_date) - 1; | |
442 | 325 if (the_date[date_length] == '\n') |
326 the_date[date_length] = '\0'; | |
327 #ifdef WIN32_NATIVE | |
428 | 328 temp = "(null)"; |
329 #else | |
330 temp = cuserid ((char *) NULL); | |
331 #endif | |
332 user_length = strlen (temp); | |
333 the_user = alloc_string ((size_t) (user_length + 1)); | |
334 strcpy (the_user, temp); | |
335 the_string = alloc_string ((size_t) (3 + prefix_length + | |
336 user_length + | |
337 date_length)); | |
338 temp = the_string; | |
339 strcpy (temp, FROM_PREFIX); | |
340 temp = &temp[prefix_length]; | |
341 *temp++ = ' '; | |
342 strcpy (temp, the_user); | |
343 temp = &temp[user_length]; | |
344 *temp++ = ' '; | |
345 strcpy (temp, the_date); | |
346 result = new_list (); | |
347 result->string = the_string; | |
348 result->continuation = ((line_list) NULL); | |
349 return result; | |
350 } | |
351 | |
352 static void | |
353 write_line_list (register line_list the_list, FILE *the_stream) | |
354 { | |
355 for ( ; | |
356 the_list != ((line_list) NULL) ; | |
357 the_list = the_list->continuation) | |
358 { | |
359 fputs (the_list->string, the_stream); | |
360 putc ('\n', the_stream); | |
361 } | |
362 return; | |
363 } | |
364 | |
365 static int | |
366 close_the_streams (void) | |
367 { | |
368 register stream_list rem; | |
369 for (rem = the_streams; | |
370 rem != ((stream_list) NULL); | |
371 rem = rem->rest_streams) | |
372 no_problems = (no_problems && | |
373 ((*rem->action) (rem->handle) == 0)); | |
374 the_streams = ((stream_list) NULL); | |
375 return (no_problems ? 0 : 1); | |
376 } | |
377 | |
378 static void | |
379 add_a_stream (FILE *the_stream, int (*closing_action)(FILE *)) | |
380 { | |
381 stream_list old = the_streams; | |
382 the_streams = new_stream (); | |
383 the_streams->handle = the_stream; | |
384 the_streams->action = closing_action; | |
385 the_streams->rest_streams = old; | |
386 return; | |
387 } | |
388 | |
389 static int | |
390 my_fclose (FILE *the_file) | |
391 { | |
392 putc ('\n', the_file); | |
393 fflush (the_file); | |
394 return fclose (the_file); | |
395 } | |
396 | |
397 static boolean | |
398 open_a_file (char *name) | |
399 { | |
400 FILE *the_stream = fopen (name, "a"); | |
401 if (the_stream != ((FILE *) NULL)) | |
402 { | |
403 add_a_stream (the_stream, my_fclose); | |
440 | 404 if (the_user == (char *) NULL) |
428 | 405 file_preface = make_file_preface (); |
406 write_line_list (file_preface, the_stream); | |
407 return true; | |
408 } | |
409 return false; | |
410 } | |
411 | |
412 static void | |
413 put_string (char *s) | |
414 { | |
415 register stream_list rem; | |
416 for (rem = the_streams; | |
417 rem != ((stream_list) NULL); | |
418 rem = rem->rest_streams) | |
419 fputs (s, rem->handle); | |
420 return; | |
421 } | |
422 | |
423 static void | |
442 | 424 put_line (const char *string) |
428 | 425 { |
426 register stream_list rem; | |
427 for (rem = the_streams; | |
428 rem != ((stream_list) NULL); | |
429 rem = rem->rest_streams) | |
430 { | |
442 | 431 const char *s = string; |
428 | 432 int column = 0; |
433 | |
434 /* Divide STRING into lines. */ | |
435 while (*s != 0) | |
436 { | |
442 | 437 const char *breakpos; |
428 | 438 |
439 /* Find the last char that fits. */ | |
440 for (breakpos = s; *breakpos && column < 78; ++breakpos) | |
441 { | |
442 if (*breakpos == '\t') | |
443 column += 8; | |
444 else | |
445 column++; | |
446 } | |
447 /* If we didn't reach end of line, break the line. */ | |
448 if (*breakpos) | |
449 { | |
450 /* Back up to just after the last comma that fits. */ | |
451 while (breakpos != s && breakpos[-1] != ',') --breakpos; | |
452 | |
453 if (breakpos == s) | |
454 { | |
455 /* If no comma fits, move past the first address anyway. */ | |
456 while (*breakpos != 0 && *breakpos != ',') ++breakpos; | |
457 if (*breakpos != 0) | |
458 /* Include the comma after it. */ | |
459 ++breakpos; | |
460 } | |
461 } | |
462 /* Output that much, then break the line. */ | |
463 fwrite (s, 1, breakpos - s, rem->handle); | |
464 column = 8; | |
465 | |
466 /* Skip whitespace and prepare to print more addresses. */ | |
467 s = breakpos; | |
468 while (*s == ' ' || *s == '\t') ++s; | |
469 if (*s != 0) | |
470 fputs ("\n\t", rem->handle); | |
471 } | |
472 putc ('\n', rem->handle); | |
473 } | |
474 return; | |
475 } | |
476 | |
477 #define mail_error error | |
478 | |
479 static void | |
480 setup_files (register line_list the_list, register char *field) | |
481 { | |
482 register char *start; | |
483 register char c; | |
484 while (true) | |
485 { | |
486 while (((c = *field) != '\0') && | |
487 ((c == ' ') || | |
488 (c == '\t') || | |
489 (c == ','))) | |
490 field += 1; | |
491 if (c != '\0') | |
492 { | |
493 start = field; | |
494 while (((c = *field) != '\0') && | |
495 (c != ' ') && | |
496 (c != '\t') && | |
497 (c != ',')) | |
498 field += 1; | |
499 *field = '\0'; | |
500 if (!open_a_file (start)) | |
501 mail_error ("Could not open file %s", start); | |
502 *field = c; | |
503 if (c != '\0') continue; | |
504 } | |
505 if (the_list == ((line_list) NULL)) return; | |
506 field = the_list->string; | |
507 the_list = the_list->continuation; | |
508 } | |
509 } | |
510 | |
511 static int | |
512 args_size (header the_header) | |
513 { | |
514 register header old = the_header; | |
515 register line_list rem; | |
516 register int size = 0; | |
517 do | |
518 { | |
4932 | 519 char *field = NULL; |
428 | 520 register char *keyword = get_keyword (the_header->text->string, &field); |
521 if ((strcmp (keyword, "TO") == 0) || | |
522 (strcmp (keyword, "CC") == 0) || | |
523 (strcmp (keyword, "BCC") == 0)) | |
524 { | |
525 size += 1 + strlen (field); | |
526 for (rem = the_header->text->continuation; | |
527 rem != NIL; | |
528 rem = rem->continuation) | |
529 size += 1 + strlen (rem->string); | |
530 } | |
531 the_header = the_header->next; | |
532 } while (the_header != old); | |
533 return size; | |
534 } | |
535 | |
536 static void | |
537 parse_header (header the_header, register char *where) | |
538 { | |
539 register header old = the_header; | |
540 do | |
541 { | |
4932 | 542 char *field = NULL; |
428 | 543 register char *keyword = get_keyword (the_header->text->string, &field); |
544 if (strcmp (keyword, "TO") == 0) | |
545 where = add_field (the_header->text->continuation, field, where); | |
546 else if (strcmp (keyword, "CC") == 0) | |
547 where = add_field (the_header->text->continuation, field, where); | |
548 else if (strcmp (keyword, "BCC") == 0) | |
549 { | |
550 where = add_field (the_header->text->continuation, field, where); | |
551 the_header->previous->next = the_header->next; | |
552 the_header->next->previous = the_header->previous; | |
553 } | |
554 else if (strcmp (keyword, "FCC") == 0) | |
555 setup_files (the_header->text->continuation, field); | |
556 the_header = the_header->next; | |
557 } while (the_header != old); | |
558 *where = '\0'; | |
559 return; | |
560 } | |
561 | |
562 static header | |
563 read_header (void) | |
564 { | |
565 register header the_header = ((header) NULL); | |
566 register line_list *next_line = ((line_list *) NULL); | |
567 | |
568 init_linebuffer (&lb); | |
569 | |
570 do | |
571 { | |
572 long length; | |
573 register char *line; | |
574 | |
575 readline (&lb, stdin); | |
576 line = lb.buffer; | |
577 length = strlen (line); | |
578 if (length == 0) break; | |
579 | |
580 if (has_keyword (line)) | |
581 { | |
582 register header old = the_header; | |
583 the_header = new_header (); | |
584 if (old == ((header) NULL)) | |
585 { | |
586 the_header->next = the_header; | |
587 the_header->previous = the_header; | |
588 } | |
589 else | |
590 { | |
591 the_header->previous = old; | |
592 the_header->next = old->next; | |
593 old->next = the_header; | |
594 } | |
595 next_line = &(the_header->text); | |
596 } | |
597 | |
598 if (next_line == ((line_list *) NULL)) | |
599 { | |
600 /* Not a valid header */ | |
601 exit (1); | |
602 } | |
603 *next_line = new_list (); | |
604 (*next_line)->string = alloc_string ((size_t) length); | |
605 strcpy (((*next_line)->string), line); | |
606 next_line = &((*next_line)->continuation); | |
607 *next_line = NIL; | |
608 | |
609 } while (true); | |
610 | |
611 return the_header->next; | |
612 } | |
613 | |
614 static void | |
615 write_header (header the_header) | |
616 { | |
617 register header old = the_header; | |
618 do | |
619 { | |
620 register line_list the_list; | |
621 for (the_list = the_header->text; | |
622 the_list != NIL; | |
623 the_list = the_list->continuation) | |
624 put_line (the_list->string); | |
625 the_header = the_header->next; | |
626 } while (the_header != old); | |
627 put_line (""); | |
628 return; | |
629 } | |
630 | |
631 int | |
632 main (int argc, char *argv[]) | |
633 { | |
634 char *command_line; | |
635 header the_header; | |
636 long name_length; | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
2687
diff
changeset
|
637 const char *mail_program_name; |
428 | 638 char buf[BUFLEN + 1]; |
639 register int size; | |
640 FILE *the_pipe; | |
641 | |
642 mail_program_name = getenv ("FAKEMAILER"); | |
643 if (!(mail_program_name && *mail_program_name)) | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
2687
diff
changeset
|
644 mail_program_name = MAIL_PROGRAM_NAME; |
428 | 645 name_length = strlen (mail_program_name); |
646 | |
647 my_name = MY_NAME; | |
648 the_streams = ((stream_list) NULL); | |
440 | 649 the_date = (char *) NULL; |
650 the_user = (char *) NULL; | |
428 | 651 |
652 the_header = read_header (); | |
653 command_line = alloc_string ((size_t) (name_length + | |
654 args_size (the_header))); | |
655 strcpy (command_line, mail_program_name); | |
656 parse_header (the_header, &command_line[name_length]); | |
657 | |
658 the_pipe = popen (command_line, "w"); | |
659 if (the_pipe == ((FILE *) NULL)) | |
440 | 660 fatal ("cannot open pipe to real mailer", (char *) NULL); |
428 | 661 |
662 add_a_stream (the_pipe, pclose); | |
663 | |
664 write_header (the_header); | |
665 | |
666 /* Dump the message itself */ | |
667 | |
668 while (!feof (stdin)) | |
669 { | |
670 size = fread (buf, 1, BUFLEN, stdin); | |
671 buf[size] = '\0'; | |
672 put_string (buf); | |
673 } | |
674 | |
675 return close_the_streams (); | |
676 } | |
677 | |
678 #endif /* not BSD 4.2 (or newer) */ |