mailline.c
14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001, 2002, 2005 Free Software Foundation, Inc.
GNU Mailutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Mailutils is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Mailutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA */
#include "mail.h"
#include <mailutils/folder.h>
#ifdef WITH_READLINE
static char **ml_command_completion (char *cmd, int start, int end);
static char *ml_command_generator (const char *text, int state);
#endif
static volatile int _interrupted;
static RETSIGTYPE
sig_handler (int signo)
{
switch (signo)
{
case SIGINT:
if (util_getenv (NULL, "quit", Mail_env_boolean, 0) == 0)
exit (0);
_interrupted++;
break;
#if defined (SIGWINCH)
case SIGWINCH:
util_do_command ("set screen=%d", util_getlines());
util_do_command ("set columns=%d", util_getcols());
page_invalidate (1);
break;
#endif
}
#ifndef HAVE_SIGACTION
signal (signo, sig_handler);
#endif
}
void
ml_clear_interrupt ()
{
_interrupted = 0;
}
int
ml_got_interrupt ()
{
int rc = _interrupted;
_interrupted = 0;
return rc;
}
static int
ml_getc (FILE *stream)
{
unsigned char c;
while (1)
{
if (read (fileno (stream), &c, 1) == 1)
return c;
if (errno == EINTR)
{
if (_interrupted)
break;
/* keep going if we handled the signal */
}
else
break;
}
return EOF;
}
void
ml_readline_init ()
{
if (!interactive)
return;
#ifdef WITH_READLINE
rl_readline_name = "mail";
rl_attempted_completion_function = (CPPFunction*)ml_command_completion;
rl_getc_function = ml_getc;
#endif
#ifdef HAVE_SIGACTION
{
struct sigaction act;
act.sa_handler = sig_handler;
sigemptyset (&act.sa_mask);
act.sa_flags = 0;
sigaction (SIGINT, &act, NULL);
#if defined(SIGWINCH)
sigaction (SIGWINCH, &act, NULL);
#endif
}
#else
signal (SIGINT, sig_handler);
#if defined(SIGWINCH)
signal (SIGWINCH, sig_handler);
#endif
#endif
}
char *
ml_readline_internal ()
{
char *line;
char *p;
size_t alloclen, linelen;
p = line = xcalloc (1, 255);
alloclen = 255;
linelen = 0;
for (;;)
{
size_t n;
p = fgets (p, alloclen - linelen, stdin);
if (p)
n = strlen(p);
else if (_interrupted)
{
free (line);
return NULL;
}
else
n = 0;
linelen += n;
/* Error. */
if (linelen == 0)
{
free (line);
return NULL;
}
/* Ok. */
if (line[linelen - 1] == '\n')
{
line[linelen - 1] = '\0';
return line;
}
else
{
char *tmp;
alloclen *= 2;
tmp = realloc (line, alloclen);
if (tmp == NULL)
{
free (line);
return NULL;
}
line = tmp;
p = line + linelen;
}
}
}
char *
ml_readline (char *prompt)
{
if (interactive)
return readline (prompt);
return ml_readline_internal ();
}
char *
ml_readline_with_intr (char *prompt)
{
char *str = ml_readline (prompt);
if (_interrupted)
printf ("\n");
return str;
}
#ifdef WITH_READLINE
static char *insert_text;
static int
ml_insert_hook (void)
{
if (insert_text)
rl_insert_text (insert_text);
return 0;
}
int
ml_reread (const char *prompt, char **text)
{
char *s;
ml_clear_interrupt ();
insert_text = *text;
rl_startup_hook = ml_insert_hook;
s = readline ((char *)prompt);
if (!ml_got_interrupt ())
{
if (*text)
free (*text);
*text = s;
}
else
{
putc('\n', stdout);
}
rl_startup_hook = NULL;
return 0;
}
/*
* readline tab completion
*/
char **
ml_command_completion (char *cmd, int start, int end)
{
int argc;
char **argv;
char **ret;
char *p;
for (p = rl_line_buffer; p < rl_line_buffer + start && isspace (*p); p++)
;
if (mu_argcv_get_n (p, end, NULL, NULL, &argc, &argv))
return NULL;
rl_completion_append_character = ' ';
if (argc <= 1 && strlen (argv[0]) <= end - start)
{
ret = rl_completion_matches (cmd, ml_command_generator);
rl_attempted_completion_over = 1;
}
else
{
const struct mail_command_entry *entry = mail_find_command (argv[0]);
if (entry && entry->command_completion)
ret = entry->command_completion (argc, argv, start == end);
else
ret = NULL;
}
mu_argcv_free (argc, argv);
return ret;
}
/*
* more readline
*/
char *
ml_command_generator (const char *text, int state)
{
static int i, len;
const char *name;
const struct mail_command *cp;
if (!state)
{
i = 0;
len = strlen (text);
}
while ((cp = mail_command_name (i)))
{
name = cp->longname;
if (strlen (cp->shortname) > strlen (name))
name = cp->shortname;
i++;
if (strncmp (name, text, len) == 0)
return strdup (name);
}
return NULL;
}
void
ml_set_completion_append_character (int c)
{
rl_completion_append_character = c;
}
void
ml_attempted_completion_over ()
{
rl_attempted_completion_over = 1;
}
/* Completion functions */
char **
no_compl (int argc ARG_UNUSED, char **argv ARG_UNUSED, int ws ARG_UNUSED)
{
ml_attempted_completion_over ();
return NULL;
}
char **
msglist_compl (int argc, char **argv, int ws)
{
/* FIXME */
ml_attempted_completion_over ();
return NULL;
}
char **
msglist_file_compl (int argc, char **argv, int ws ARG_UNUSED)
{
if (argc == 1)
ml_attempted_completion_over ();
return NULL;
}
char **
command_compl (int argc, char **argv, int ws)
{
ml_set_completion_append_character (0);
if (ws)
return NULL;
return rl_completion_matches (argv[argc-1], ml_command_generator);
}
static char *
file_generator (const char *text, int state, char *path, size_t pathlen,
char repl,
int flags)
{
static struct mu_folder_list list;
static int i;
if (!state)
{
char *wcard;
mu_folder_t folder;
wcard = xmalloc (strlen (text) + 2);
strcat (strcpy (wcard, text), "*");
mu_folder_create (&folder, path);
mu_folder_list (folder, path, wcard, &list);
free (wcard);
mu_folder_destroy (&folder);
if (list.num == 0)
return NULL;
else if (list.num == 1)
ml_set_completion_append_character (0);
i = 0;
}
while (i < list.num)
{
if (list.element[i]->type & flags)
{
char *ret;
if (repl)
{
int len = strlen (list.element[i]->name + pathlen);
ret = xmalloc (len + 2);
ret[0] = repl;
memcpy (ret + 1, list.element[i]->name + pathlen, len);
ret[len+1] = 0;
}
else
ret = xstrdup (list.element[i]->name);
i++;
return ret;
}
i++;
}
mu_folder_list_destroy (&list);
return NULL;
}
static char *
folder_generator (const char *text, int state)
{
char *ret;
static size_t pathlen;
if (!state)
{
char *path = util_folder_path ("");
if (!path)
return NULL;
pathlen = strlen (path);
ret = file_generator (text, state, path, pathlen, '+',
MU_FOLDER_ATTRIBUTE_ALL);
free (path);
}
else
ret = file_generator (text, state, NULL, pathlen, '+',
MU_FOLDER_ATTRIBUTE_ALL);
return ret;
}
char **
file_compl (int argc, char **argv, int ws)
{
char *text;
if (ws)
{
ml_set_completion_append_character (0);
ml_attempted_completion_over ();
return NULL;
}
text = argv[argc-1];
switch (text[0])
{
case '+':
text++;
break;
case '#':
case '&':
ml_attempted_completion_over ();
return NULL;
default:
return NULL; /* Will be expanded by readline itself */
}
return rl_completion_matches (text, folder_generator);
}
static char *
dir_generator (const char *text, int state)
{
char *ret;
static size_t pathlen;
static int repl;
if (!state)
{
char *path;
switch (text[0])
{
case '+':
text++;
repl = '+';
path = util_folder_path (text);
pathlen = strlen (path) - strlen (text);
break;
case '~':
repl = '~';
if (text[1] == '/')
{
path = mu_get_homedir ();
text += 2;
pathlen = strlen (path);
break;
}
/* else FIXME! */
default:
path = strdup (text);
pathlen = 0;
repl = 0;
}
ret = file_generator (text, state, path, pathlen, repl,
MU_FOLDER_ATTRIBUTE_DIRECTORY);
free (path);
}
else
ret = file_generator (text, state, NULL, pathlen, repl,
MU_FOLDER_ATTRIBUTE_DIRECTORY);
return ret;
}
char **
dir_compl (int argc, char **argv, int ws)
{
ml_attempted_completion_over ();
if (ws)
{
ml_set_completion_append_character (0);
return NULL;
}
return rl_completion_matches (argv[argc-1], dir_generator);
}
static char *
alias_generator (const char *text, int state)
{
static alias_iterator_t itr;
const char *p;
if (!state)
p = alias_iterate_first (text, &itr);
else
p = alias_iterate_next (itr);
if (!p)
{
alias_iterate_end (&itr);
return NULL;
}
return strdup (p);
}
char **
alias_compl (int argc, char **argv, int ws)
{
ml_attempted_completion_over ();
return rl_completion_matches (ws ? "" : argv[argc-1], alias_generator);
}
static char *
var_generator (const char *text, int state)
{
static var_iterator_t itr;
const char *p;
if (!state)
p = var_iterate_first (text, &itr);
else
p = var_iterate_next (itr);
if (!p)
{
var_iterate_end (&itr);
return NULL;
}
return strdup (p);
}
char **
var_compl (int argc, char **argv, int ws)
{
ml_attempted_completion_over ();
return rl_completion_matches (ws ? "" : argv[argc-1], var_generator);
}
#else
#include <sys/ioctl.h>
#define STDOUT 1
#ifndef TIOCSTI
static int ch_erase;
static int ch_kill;
#endif
#if defined(TIOCSTI)
#elif defined(HAVE_TERMIOS_H)
# include <termios.h>
static struct termios term_settings;
int
set_tty ()
{
struct termios new_settings;
if (tcgetattr(STDOUT, &term_settings) == -1)
return 1;
ch_erase = term_settings.c_cc[VERASE];
ch_kill = term_settings.c_cc[VKILL];
new_settings = term_settings;
new_settings.c_lflag &= ~(ICANON|ECHO);
#if defined(TAB3)
new_settings.c_oflag &= ~(TAB3);
#elif defined(OXTABS)
new_settings.c_oflag &= ~(OXTABS);
#endif
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(STDOUT, TCSADRAIN, &new_settings);
return 0;
}
void
restore_tty ()
{
tcsetattr (STDOUT, TCSADRAIN, &term_settings);
}
#elif defined(HAVE_TERMIO_H)
# include <termio.h>
static struct termio term_settings;
int
set_tty ()
{
struct termio new_settings;
if (ioctl(STDOUT, TCGETA, &term_settings) < 0)
return -1;
ch_erase = term_settings.c_cc[VERASE];
ch_kill = term_settings.c_cc[VKILL];
new_settings = term_settings;
new_settings.c_lflag &= ~(ICANON | ECHO);
new_settings.c_oflag &= ~(TAB3);
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
ioctl(STDOUT, TCSETA, &new_settings);
return 0;
}
void
restore_tty ()
{
ioctl(STDOUT, TCSETA, &term_settings);
}
#elif defined(HAVE_SGTTY_H)
# include <sgtty.h>
static struct sgttyb term_settings;
int
set_tty ()
{
struct sgttyb new_settings;
if (ioctl(STDOUT, TIOCGETP, &term_settings) < 0)
return 1;
ch_erase = term_settings.sg_erase;
ch_kill = term_settings.sg_kill;
new_settings = term_settings;
new_settings.sg_flags |= CBREAK;
new_settings.sg_flags &= ~(ECHO | XTABS);
ioctl(STDOUT, TIOCSETP, &new_settings);
return 0;
}
void
restore_tty ()
{
ioctl(STDOUT, TIOCSETP, &term_settings);
}
#else
# define DUMB_MODE
#endif
#define LINE_INC 80
int
ml_reread (const char *prompt, char **text)
{
int ch;
char *line;
int line_size;
int pos;
char *p;
if (*text)
{
line = strdup (*text);
if (line)
{
pos = strlen(line);
line_size = pos + 1;
}
}
else
{
line_size = LINE_INC;
line = malloc (line_size);
pos = 0;
}
if (!line)
{
util_error (_("Not enough memory to edit the line"));
return -1;
}
line[pos] = 0;
if (prompt)
{
fputs (prompt, stdout);
fflush (stdout);
}
#ifdef TIOCSTI
for (p = line; *p; p++)
{
ioctl(0, TIOCSTI, p);
}
pos = 0;
while ((ch = ml_getc (stdin)) != EOF && ch != '\n')
{
if (pos >= line_size)
{
if ((p = realloc (line, line_size + LINE_INC)) == NULL)
{
fputs ("\n", stdout);
util_error (_("Not enough memory to edit the line"));
break;
}
else
{
line_size += LINE_INC;
line = p;
}
}
line[pos++] = ch;
}
#else
fputs (line, stdout);
fflush (stdout);
# ifndef DUMB_MODE
set_tty ();
while ((ch = ml_getc (stdin)) != EOF)
{
if (ch == ch_erase)
{
/* kill last char */
if (pos > 0)
line[--pos] = 0;
putc('\b', stdout);
}
else if (ch == ch_kill)
{
/* kill the entire line */
pos = 0;
line[pos] = 0;
putc ('\r', stdout);
if (prompt)
fputs (prompt, stdout);
}
else if (ch == '\n' || ch == EOF)
break;
else
{
if (pos >= line_size)
{
if ((p = realloc (line, line_size + LINE_INC)) == NULL)
{
fputs ("\n", stdout);
util_error (_("Not enough memory to edit the line"));
break;
}
else
{
line_size += LINE_INC;
line = p;
}
}
line[pos++] = ch;
putc(ch, stdout);
}
fflush (stdout);
}
putc ('\n', stdout);
restore_tty ();
# else
/* Dumb mode: the last resort */
putc ('\n', stdout);
if (prompt)
{
fputs (prompt, stdout);
fflush (stdout);
}
pos = 0;
while ((ch = ml_getc (stdin)) != EOF && ch != '\n')
{
if (pos >= line_size)
{
if ((p = realloc (line, line_size + LINE_INC)) == NULL)
{
fputs ("\n", stdout);
util_error (_("Not enough memory to edit the line"));
break;
}
else
{
line_size += LINE_INC;
line = p;
}
}
line[pos++] = ch;
}
# endif
#endif
line[pos] = 0;
if (ml_got_interrupt ())
free (line);
else
{
if (*text)
free (*text);
*text = line;
}
return 0;
}
char *
readline (char *prompt)
{
if (prompt)
{
fprintf (ofile, "%s", prompt);
fflush (ofile);
}
return ml_readline_internal ();
}
void
ml_set_completion_append_character (int c ARG_UNUSED)
{
}
void
ml_attempted_completion_over ()
{
}
#endif