Commit 2a4860b9 2a4860b9fd39c563de2eac81bc91fe518b6f3fcc by Sergey Poznyakoff

Added extra argument to util_find_entry(): a pointer to the table where

to look up. Added util_error(), util_help(), util_tempfile().
1 parent 727d451f
Showing 1 changed file with 137 additions and 153 deletions
...@@ -242,7 +242,7 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -242,7 +242,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
242 ret = calloc (1, sizeof (int)); 242 ret = calloc (1, sizeof (int));
243 if (!ret) 243 if (!ret)
244 { 244 {
245 fprintf (ofile, "not enough memory\n"); 245 util_error("not enough memory");
246 exit (1); 246 exit (1);
247 } 247 }
248 ret [0] = cursor; 248 ret [0] = cursor;
...@@ -253,7 +253,7 @@ util_expand_msglist (const int argc, char **argv, int **list) ...@@ -253,7 +253,7 @@ util_expand_msglist (const int argc, char **argv, int **list)
253 ret = malloc (lc * sizeof (int)); 253 ret = malloc (lc * sizeof (int));
254 if (!ret) 254 if (!ret)
255 { 255 {
256 fprintf (ofile, "not enough memory\n"); 256 util_error("not enough memory");
257 exit (1); 257 exit (1);
258 } 258 }
259 lc = 0; 259 lc = 0;
...@@ -279,7 +279,7 @@ util_do_command (const char *c, ...) ...@@ -279,7 +279,7 @@ util_do_command (const char *c, ...)
279 function_t *command; 279 function_t *command;
280 char *cmd = NULL; 280 char *cmd = NULL;
281 va_list ap; 281 va_list ap;
282 int i, zcnt = 0; 282 static const char *delim = "=";
283 283
284 va_start (ap, c); 284 va_start (ap, c);
285 status = vasprintf (&cmd, c, ap); 285 status = vasprintf (&cmd, c, ap);
...@@ -294,34 +294,12 @@ util_do_command (const char *c, ...) ...@@ -294,34 +294,12 @@ util_do_command (const char *c, ...)
294 if (cmd[0] == '#') 294 if (cmd[0] == '#')
295 return 0; 295 return 0;
296 296
297 if (argcv_get (cmd, &argc, &argv) != 0) 297 if (argcv_get (cmd, delim, &argc, &argv) != 0)
298 return argcv_free (argc, argv); 298 return argcv_free (argc, argv);
299 299
300 /* Eliminate empty strings */ 300 entry = util_find_entry (mail_command_table, argv[0]);
301 for (i = 0; i < argc; i++)
302 {
303 if (argv[i][0] == 0)
304 {
305 int d;
306 for (d = i; d < argc && argv[d][0] == 0; d++)
307 ;
308 if (d == argc)
309 {
310 break;
311 }
312 else
313 {
314 char *s = argv[d];
315 argv[d] = argv[i];
316 argv[i] = s;
317 }
318 zcnt++;
319 }
320 }
321
322 entry = util_find_entry (argv[0]);
323 301
324 if (if_cond() == 0 && entry.isflow == 0) 302 if (if_cond() == 0 && (entry.flags & EF_FLOW) == 0)
325 { 303 {
326 argcv_free (argc, argv); 304 argcv_free (argc, argv);
327 return 0; 305 return 0;
...@@ -332,10 +310,10 @@ util_do_command (const char *c, ...) ...@@ -332,10 +310,10 @@ util_do_command (const char *c, ...)
332 command = util_command_get ("quit"); 310 command = util_command_get ("quit");
333 311
334 if (command != NULL) 312 if (command != NULL)
335 status = command (argc - zcnt, argv); 313 status = command (argc, argv);
336 else 314 else
337 { 315 {
338 fprintf (ofile, "Unknown command: %s\n", argv[0]); 316 util_error("Unknown command: %s", argv[0]);
339 status = 1; 317 status = 1;
340 } 318 }
341 319
...@@ -383,7 +361,7 @@ util_msglist_command (function_t *func, int argc, char **argv, int set_cursor) ...@@ -383,7 +361,7 @@ util_msglist_command (function_t *func, int argc, char **argv, int set_cursor)
383 function_t * 361 function_t *
384 util_command_get (char *cmd) 362 util_command_get (char *cmd)
385 { 363 {
386 struct mail_command_entry entry = util_find_entry (cmd); 364 struct mail_command_entry entry = util_find_entry (mail_command_table, cmd);
387 return entry.func; 365 return entry.func;
388 } 366 }
389 367
...@@ -391,24 +369,24 @@ util_command_get (char *cmd) ...@@ -391,24 +369,24 @@ util_command_get (char *cmd)
391 * returns the mail_command_entry structure for the command matching cmd 369 * returns the mail_command_entry structure for the command matching cmd
392 */ 370 */
393 struct mail_command_entry 371 struct mail_command_entry
394 util_find_entry (char *cmd) 372 util_find_entry (const struct mail_command_entry *table, char *cmd)
395 { 373 {
396 int i = 0, ll = 0, sl = 0; 374 int i = 0, ll = 0, sl = 0;
397 int len = strlen (cmd); 375 int len = strlen (cmd);
398 376
399 while (mail_command_table[i].shortname != 0) 377 while (table[i].shortname != 0)
400 { 378 {
401 sl = strlen (mail_command_table[i].shortname); 379 sl = strlen (table[i].shortname);
402 ll = strlen (mail_command_table[i].longname); 380 ll = strlen (table[i].longname);
403 if (sl > ll && !strncmp (mail_command_table[i].shortname, cmd, sl)) 381 if (sl > ll && !strncmp (table[i].shortname, cmd, sl))
404 return mail_command_table[i]; 382 return table[i];
405 else if (sl == len && !strcmp (mail_command_table[i].shortname, cmd)) 383 else if (sl == len && !strcmp (table[i].shortname, cmd))
406 return mail_command_table[i]; 384 return table[i];
407 else if (sl < len && !strncmp (mail_command_table[i].longname, cmd, len)) 385 else if (sl < len && !strncmp (table[i].longname, cmd, len))
408 return mail_command_table[i]; 386 return table[i];
409 i++; 387 i++;
410 } 388 }
411 return mail_command_table[i]; 389 return table[i];
412 } 390 }
413 391
414 /* 392 /*
...@@ -523,7 +501,7 @@ util_printenv (int set) ...@@ -523,7 +501,7 @@ util_printenv (int set)
523 { 501 {
524 fprintf (ofile, "%s", env_cursor->env_entry.var); 502 fprintf (ofile, "%s", env_cursor->env_entry.var);
525 if (env_cursor->env_entry.value != NULL) 503 if (env_cursor->env_entry.value != NULL)
526 fprintf (ofile, "=%s", env_cursor->env_entry.value); 504 fprintf (ofile, "=\"%s\"", env_cursor->env_entry.value);
527 fprintf (ofile, "\n"); 505 fprintf (ofile, "\n");
528 } 506 }
529 } 507 }
...@@ -546,101 +524,6 @@ util_isdeleted (int n) ...@@ -546,101 +524,6 @@ util_isdeleted (int n)
546 return 0; 524 return 0;
547 } 525 }
548 526
549 /*
550 * readline tab completion
551 */
552 #ifdef WITH_READLINE
553 char **
554 util_command_completion (char *cmd, int start, int end)
555 {
556 if (start == 0)
557 return completion_matches (cmd, util_command_generator);
558 return NULL;
559 }
560
561 /*
562 * more readline
563 */
564 char *
565 util_command_generator (char *text, int state)
566 {
567 static int i, len;
568 char *name;
569
570 if (!state)
571 {
572 i = 0;
573 len = strlen (text);
574 }
575
576 while ((name = mail_command_table[i].longname))
577 {
578 if (strlen (mail_command_table[i].shortname) > strlen(name))
579 name = mail_command_table[i].shortname;
580 i++;
581 if (strncmp (name, text, len) == 0)
582 return (strdup(name));
583 }
584
585 return NULL;
586 }
587
588 #else
589
590 char *
591 readline (const char *prompt)
592 {
593 char *line;
594 char *p;
595 size_t alloclen, linelen;
596
597 if (prompt)
598 {
599 fprintf (ofile, "%s",prompt);
600 fflush (ofile);
601 }
602
603 p = line = calloc (1, 255);
604 alloclen = 255;
605 linelen = 0;
606 for (;;)
607 {
608 size_t n;
609 p = fgets (p, alloclen - linelen, stdin);
610 n = (p) ? strlen (p) : 0;
611
612 linelen += n;
613
614 /* Error. */
615 if (linelen == 0)
616 {
617 free (line);
618 return NULL;
619 }
620
621 /* Ok. */
622 if (line[linelen - 1] == '\n')
623 {
624 line[linelen - 1] = '\0';
625 return line;
626 }
627 else
628 {
629 char *tmp;
630 alloclen *= 2;
631 tmp = realloc (line, alloclen);
632 if (tmp == NULL)
633 {
634 free (line);
635 return NULL;
636 }
637 line = tmp;
638 p = line + linelen;
639 }
640 }
641 }
642 #endif
643
644 char * 527 char *
645 util_get_homedir() 528 util_get_homedir()
646 { 529 {
...@@ -648,7 +531,7 @@ util_get_homedir() ...@@ -648,7 +531,7 @@ util_get_homedir()
648 if (!homedir) 531 if (!homedir)
649 { 532 {
650 /* Shouldn't happen, but one never knows */ 533 /* Shouldn't happen, but one never knows */
651 fprintf(ofile, "can't get homedir\n"); 534 util_error("can't get homedir");
652 exit (EXIT_FAILURE); 535 exit (EXIT_FAILURE);
653 } 536 }
654 return strdup(homedir); 537 return strdup(homedir);
...@@ -678,14 +561,14 @@ util_get_sender(int msgno, int strip) ...@@ -678,14 +561,14 @@ util_get_sender(int msgno, int strip)
678 if (envelope_sender (env, buffer, sizeof (buffer), NULL) 561 if (envelope_sender (env, buffer, sizeof (buffer), NULL)
679 || address_create (&addr, buffer)) 562 || address_create (&addr, buffer))
680 { 563 {
681 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno); 564 util_error("can't determine sender name (msg %d)", msgno);
682 return NULL; 565 return NULL;
683 } 566 }
684 } 567 }
685 568
686 if (address_get_email (addr, 1, buffer, sizeof(buffer), NULL)) 569 if (address_get_email (addr, 1, buffer, sizeof(buffer), NULL))
687 { 570 {
688 fprintf (ofile, "can't determine sender name (msg %d)\n", msgno); 571 util_error("can't determine sender name (msg %d)", msgno);
689 address_destroy (&addr); 572 address_destroy (&addr);
690 return NULL; 573 return NULL;
691 } 574 }
...@@ -753,7 +636,7 @@ util_slist_add (list_t *list, char *value) ...@@ -753,7 +636,7 @@ util_slist_add (list_t *list, char *value)
753 636
754 if ((p = strdup(value)) == NULL) 637 if ((p = strdup(value)) == NULL)
755 { 638 {
756 fprintf (ofile, "not enough memory\n"); 639 util_error("not enough memory\n");
757 return; 640 return;
758 } 641 }
759 list_append (*list, p); 642 list_append (*list, p);
...@@ -837,7 +720,7 @@ util_escape_percent (char **str) ...@@ -837,7 +720,7 @@ util_escape_percent (char **str)
837 newstr = malloc (strlen (*str) + 1 + count); 720 newstr = malloc (strlen (*str) + 1 + count);
838 if (!newstr) 721 if (!newstr)
839 { 722 {
840 fprintf (ofile, "not enough memory\n"); 723 util_error("not enough memory");
841 exit (1); /* be on the safe side */ 724 exit (1); /* be on the safe side */
842 } 725 }
843 726
...@@ -881,13 +764,6 @@ util_outfolder_name (char *str) ...@@ -881,13 +764,6 @@ util_outfolder_name (char *str)
881 return str; 764 return str;
882 } 765 }
883 766
884 char *
885 util_whoami()
886 {
887 struct passwd *pw = getpwuid(getuid());
888 return pw ? pw->pw_name : "unknown";
889 }
890
891 /* Save an outgoing message. "savefile" allows to override the setting 767 /* Save an outgoing message. "savefile" allows to override the setting
892 of the "record" variable. */ 768 of the "record" variable. */
893 void 769 void
...@@ -902,7 +778,7 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -902,7 +778,7 @@ util_save_outgoing (message_t msg, char *savefile)
902 outfile = fopen (filename, "a"); 778 outfile = fopen (filename, "a");
903 if (!outfile) 779 if (!outfile)
904 { 780 {
905 fprintf (outfile, "can't open save file %s: %s", 781 util_error("can't open save file %s: %s",
906 filename, strerror (errno)); 782 filename, strerror (errno));
907 } 783 }
908 else 784 else
...@@ -919,7 +795,7 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -919,7 +795,7 @@ util_save_outgoing (message_t msg, char *savefile)
919 795
920 if (!bsize) 796 if (!bsize)
921 { 797 {
922 fprintf (ofile, "not enough memory for creating save file\n"); 798 util_error("not enough memory for creating save file");
923 } 799 }
924 else 800 else
925 { 801 {
...@@ -932,7 +808,7 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -932,7 +808,7 @@ util_save_outgoing (message_t msg, char *savefile)
932 time(&t); 808 time(&t);
933 tm = gmtime(&t); 809 tm = gmtime(&t);
934 strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y%n", tm); 810 strftime (date, sizeof (date), "%a %b %e %H:%M:%S %Y%n", tm);
935 fprintf (outfile, "From %s %s\n", util_whoami(), date); 811 fprintf (outfile, "From %s %s\n", mail_whoami(), date);
936 812
937 message_get_stream (msg, &stream); 813 message_get_stream (msg, &stream);
938 while (stream_read (stream, buf, bsize, off, &n) == 0 814 while (stream_read (stream, buf, bsize, off, &n) == 0
...@@ -948,3 +824,111 @@ util_save_outgoing (message_t msg, char *savefile) ...@@ -948,3 +824,111 @@ util_save_outgoing (message_t msg, char *savefile)
948 free (filename); 824 free (filename);
949 } 825 }
950 } 826 }
827
828 #ifdef HAVE_STDARG_H
829 void
830 util_error (const char *format, ...)
831 #else
832 void
833 util_error (va_alist)
834 va_dcl
835 #endif
836 {
837 va_list ap;
838
839 #ifdef HAVE_STDARG_H
840 va_start(ap, format);
841 #else
842 char *format;
843
844 va_start(ap);
845 format = va_arg(ap, char *);
846 #endif
847
848 vfprintf(stderr, format, ap);
849 fprintf(stderr, "\n");
850
851 va_end(ap);
852 }
853
854 int
855 util_help (const struct mail_command_entry *table, char *word)
856 {
857 if (!word)
858 {
859 int i = 0;
860 FILE *out = stdout;
861
862 if ((util_find_env("crt"))->set)
863 out = popen (getenv("PAGER"), "w");
864
865 while (table[i].synopsis != 0)
866 fprintf (out, "%s\n", table[i++].synopsis);
867
868 if (out != stdout)
869 pclose (out);
870
871 return 0;
872 }
873 else
874 {
875 int status = 0;
876 struct mail_command_entry entry = util_find_entry(table, word);
877 if (entry.synopsis != NULL)
878 fprintf (stdout, "%s\n", entry.synopsis);
879 else
880 {
881 status = 1;
882 fprintf (stdout, "Unknown command: %s\n", word);
883 }
884 return status;
885 }
886 return 1;
887 }
888
889 int
890 util_tempfile(char **namep)
891 {
892 char *filename;
893 char *tmpdir;
894 int fd;
895
896 /* We have to be extra careful about opening temporary files, since we
897 may be running with extra privilege i.e setgid(). */
898 tmpdir = (getenv ("TMPDIR")) ? getenv ("TMPDIR") : "/tmp";
899
900 filename = malloc (strlen (tmpdir) + /*'/'*/1 + /* "muXXXXXX" */8 + 1);
901 if (!filename)
902 return -1;
903 sprintf (filename, "%s/muXXXXXX", tmpdir);
904
905 #ifdef HAVE_MKSTEMP
906 {
907 int save_mask = umask(077);
908 fd = mkstemp (filename);
909 umask(save_mask);
910 }
911 #else
912 if (mktemp (filename))
913 fd = open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
914 else
915 fd = -1;
916 #endif
917
918 if (fd == -1)
919 {
920 util_error("Can not open temporary file: %s", strerror(errno));
921 free(filename);
922 return -1;
923 }
924
925 if (namep)
926 *namep = filename;
927 else
928 {
929 unlink(filename);
930 free(filename);
931 }
932
933 return fd;
934 }
......