mailline.c
3.99 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
/* GNU mailutils - a suite of utilities for electronic mail
Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
This program 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.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#include "mail.h"
static char **ml_command_completion __P((char *cmd, int start, int end));
static char *ml_command_generator __P((char *text, int state));
static volatile int _interrupted;
static RETSIGTYPE
sig_handler (int signo)
{
switch (signo)
{
case SIGINT:
if (util_find_env ("quit")->set)
exit (0);
_interrupted++;
break;
#if defined (SIGWINCH)
case SIGWINCH:
break;
#endif
}
signal (signo, sig_handler);
}
void
ml_clear_interrupt ()
{
_interrupted = 0;
}
int
ml_got_interrupt ()
{
int rc = _interrupted;
_interrupted = 0;
return rc;
}
#ifdef WITH_READLINE
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;
}
#endif
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
signal (SIGINT, sig_handler);
#if defined(SIGWINCH)
signal (SIGWINCH, sig_handler);
#endif
}
#ifdef WITH_READLINE
static char *insert_text;
static int
ml_insert_hook ()
{
if (insert_text)
rl_insert_text (insert_text);
return 0;
}
int
ml_reread (char *prompt, char **text)
{
char *s;
insert_text = *text;
rl_startup_hook = ml_insert_hook;
s = readline (prompt);
if (*text)
free (*text);
*text = s;
rl_startup_hook = NULL;
return 0;
}
/*
* readline tab completion
*/
char **
ml_command_completion (char *cmd, int start, int end)
{
if (start == 0)
return completion_matches (cmd, ml_command_generator);
return NULL;
}
/*
* more readline
*/
char *
ml_command_generator (char *text, int state)
{
static int i, len;
char *name;
if (!state)
{
i = 0;
len = strlen (text);
}
while ((name = mail_command_table[i].longname))
{
if (strlen (mail_command_table[i].shortname) > strlen(name))
name = mail_command_table[i].shortname;
i++;
if (strncmp (name, text, len) == 0)
return (strdup(name));
}
return NULL;
}
#else
int
ml_reread (char *prompt, char **text)
{
char *s;
/*FIXME*/
s = readline (prompt);
if (*text)
free (*text);
*text = s;
return 0;
}
char *
readline (const char *prompt)
{
char *line;
char *p;
size_t alloclen, linelen;
if (prompt)
{
fprintf (ofile, "%s", prompt);
fflush (ofile);
}
p = line = calloc (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;
}
}
}
#endif