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
|
static char rcsid[]="$Id$";
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
*
* 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 of the License, 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 "mutt.h"
#include "mutt_menu.h"
#include "mutt_curses.h"
#include "pager.h"
#include <termios.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
/* not possible to unget more than one char under some curses libs, and it
* is impossible to unget function keys in SLang, so roll our own input
* buffering routines.
*/
static short UngetCount = 0;
#define UngetBufLen 128
static event_t KeyEvent[UngetBufLen] = { {0,0} };
void mutt_refresh (void)
{
/* don't refresh in the middle of macros unless necessary */
if (!UngetCount || option (OPTFORCEREFRESH))
refresh ();
}
event_t mutt_getch (void)
{
int ch;
event_t err = {-1, OP_NULL }, ret;
if (UngetCount)
return (KeyEvent[--UngetCount]);
Signals &= ~S_INTERRUPT;
#ifdef KEY_RESIZE
/* ncurses 4.2 sends this when the screen is resized */
ch = KEY_RESIZE;
while (ch == KEY_RESIZE)
#endif /* KEY_RESIZE */
ch = getch ();
if (Signals & S_INTERRUPT)
mutt_query_exit ();
if(ch == ERR)
return err;
if ((ch & 0x80) && option (OPTMETAKEY))
{
/* send ALT-x as ESC-x */
ch &= ~0x80;
mutt_ungetch (ch, 0);
ret.ch = '\033';
ret.op = 0;
return ret;
}
ret.ch = ch;
ret.op = 0;
return (ch == ctrl ('G') ? err : ret);
}
int mutt_get_field (/* const */ char *field, char *buf, size_t buflen, int complete)
{
int ret;
int len = strlen (field); /* in case field==buffer */
do
{
CLEARLINE (LINES-1);
addstr (field);
mutt_refresh ();
ret = mutt_enter_string ((unsigned char *) buf, buflen, LINES-1, len, complete);
}
while (ret == 1);
CLEARLINE (LINES-1);
return (ret);
}
int mutt_get_password (char *msg, char *buf, size_t buflen)
{
int rc;
CLEARLINE (LINES-1);
addstr (msg);
rc = mutt_enter_string ((unsigned char *) buf, buflen, LINES - 1, strlen (msg), M_PASS);
CLEARLINE (LINES-1);
return (rc);
}
void mutt_clear_error (void)
{
Errorbuf[0] = 0;
CLEARLINE (LINES-1);
}
void mutt_edit_file (const char *editor, const char *data)
{
char cmd[LONG_STRING];
endwin ();
mutt_expand_file_fmt (cmd, sizeof (cmd), editor, data);
mutt_system (cmd);
keypad (stdscr, TRUE);
clearok (stdscr, TRUE);
}
int mutt_yesorno (const char *msg, int def)
{
event_t ch;
char *yes = _("yes");
char *no = _("no");
CLEARLINE(LINES-1);
printw("%s ([%c]/%c): ", msg, def ? *yes : *no,
def ? *no : *yes);
FOREVER
{
mutt_refresh ();
ch = mutt_getch ();
if (ch.ch == -1) return(-1);
if (CI_is_return (ch.ch))
break;
else if (tolower(ch.ch) == tolower(*yes))
{
def = 1;
break;
}
else if (tolower(ch.ch) == tolower(*no))
{
def = 0;
break;
}
else
{
BEEP();
}
}
addstr (def ? yes : no);
mutt_refresh ();
return (def);
}
/* this function is called when the user presses the abort key */
void mutt_query_exit (void)
{
mutt_flushinp ();
curs_set (1);
if (Timeout)
timeout (-1); /* restore blocking operation */
if (mutt_yesorno (_("Exit Mutt?"), 1) == 1)
{
endwin ();
exit (0);
}
mutt_clear_error();
mutt_curs_set (-1);
Signals &= ~S_INTERRUPT;
}
void mutt_curses_error (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
va_end (ap);
dprint (1, (debugfile, "%s\n", Errorbuf));
Errorbuf[ (COLS < sizeof (Errorbuf) ? COLS : sizeof (Errorbuf)) - 2 ] = 0;
BEEP ();
SETCOLOR (MT_COLOR_ERROR);
mvaddstr (LINES-1, 0, Errorbuf);
clrtoeol ();
SETCOLOR (MT_COLOR_NORMAL);
mutt_refresh ();
set_option (OPTMSGERR);
}
void mutt_message (const char *fmt, ...)
{
va_list ap;
va_start (ap, fmt);
vsnprintf (Errorbuf, sizeof (Errorbuf), fmt, ap);
va_end (ap);
Errorbuf[ (COLS < sizeof (Errorbuf) ? COLS : sizeof (Errorbuf)) - 2 ] = 0;
SETCOLOR (MT_COLOR_MESSAGE);
mvaddstr (LINES - 1, 0, Errorbuf);
clrtoeol ();
SETCOLOR (MT_COLOR_NORMAL);
mutt_refresh ();
unset_option (OPTMSGERR);
}
void mutt_show_error (void)
{
SETCOLOR (option (OPTMSGERR) ? MT_COLOR_ERROR : MT_COLOR_MESSAGE);
CLEARLINE (LINES-1);
addstr (Errorbuf);
SETCOLOR (MT_COLOR_NORMAL);
}
void mutt_endwin (const char *msg)
{
move (LINES - 1, COLS - 1);
attrset (A_NORMAL);
mutt_refresh ();
endwin ();
fputc ('\n', stdout);
if (msg)
puts (msg);
}
void mutt_perror (const char *s)
{
char *p = strerror (errno);
dprint (1, (debugfile, "%s: %s (errno = %d)\n", s,
p ? p : "unknown error", errno));
mutt_error ("%s: %s (errno = %d)", s, p ? p : _("unknown error"), errno);
}
int mutt_any_key_to_continue (const char *s)
{
struct termios t;
struct termios old;
int f, ch;
f = open ("/dev/tty", O_RDONLY);
tcgetattr (f, &t);
memcpy ((void *)&old, (void *)&t, sizeof(struct termios)); /* save original state */
t.c_lflag &= ~(ICANON | ECHO);
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
tcsetattr (f, TCSADRAIN, &t);
fflush (stdout);
if (s)
fputs (s, stdout);
else
fputs (_("Press any key to continue..."), stdout);
ff
|