~kevin8t8/mutt

mutt/background.c -rw-r--r-- 14.8 KiB View raw
14709d7dKevin McCarthy Change postpone mode to write Date header too. 2 days ago
                                                                                
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
/*
 * Copyright (C) 1996-2000,2013 Michael R. Elkins <me@mutt.org>
 * Copyright (C) 2020 Kevin J. McCarthy <kevin@8t8.us>
 *
 *     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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include "mutt.h"
#include "mutt_menu.h"
#include "send.h"
#include "background.h"

#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <errno.h>

typedef struct background_process
{
  pid_t pid;
  unsigned int finished;
  SEND_CONTEXT *sctx;

  struct background_process *next;
} BACKGROUND_PROCESS;

static BACKGROUND_PROCESS *ProcessList = NULL;

static BACKGROUND_PROCESS *bg_process_new (pid_t pid, SEND_CONTEXT *sctx)
{
  BACKGROUND_PROCESS *process;

  process = safe_calloc (1, sizeof(BACKGROUND_PROCESS));
  process->pid = pid;
  process->sctx = sctx;

  return process;
}

static void bg_process_free (BACKGROUND_PROCESS **process)
{
  if (!process || !*process)
    return;

  /* The SEND_CONTEXT is managed independently of the process.
   * Don't free it here */
  FREE (process);   /* __FREE_CHECKED__ */
}

static void process_list_add (BACKGROUND_PROCESS *process)
{
  process->next = ProcessList;
  ProcessList = process;
  BackgroundProcessCount++;
}

static void process_list_remove (BACKGROUND_PROCESS *process)
{
  BACKGROUND_PROCESS *cur = ProcessList;
  BACKGROUND_PROCESS **plast = &ProcessList;

  while (cur)
  {
    if (cur == process)
    {
      *plast = cur->next;
      cur->next = NULL;
      BackgroundProcessCount--;
      break;
    }
    plast = &cur->next;
    cur = cur->next;
  }
}

int mutt_background_has_backgrounded (void)
{
  return ProcessList ? 1 : 0;
}

/* Returns 0 if no processes were updated to finished.
 *         1 if one or more processes finished
 */
int mutt_background_process_waitpid (void)
{
  BACKGROUND_PROCESS *process;
  pid_t pid;
  int has_finished = 0;

  if (!ProcessList)
    return 0;

  while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
  {
    process = ProcessList;
    while (process)
    {
      if (process->pid == pid)
      {
        process->finished = 1;
        has_finished = 1;
        break;
      }
      process = process->next;
    }
  }

  return has_finished;
}

static pid_t mutt_background_run (const char *cmd)
{
  struct sigaction act;
  pid_t thepid;
  int fd;

  if (!cmd || !*cmd)
    return (0);

  /* must ignore SIGINT and SIGQUIT */
  mutt_block_signals_system ();

  if ((thepid = fork ()) == 0)
  {
    /* give up controlling terminal */
    setsid ();

    /* this ensures the child can't use stdin to take control of the
     * terminal */
#if defined(OPEN_MAX)
    for (fd = 0; fd < OPEN_MAX; fd++)
      close (fd);
#elif defined(_POSIX_OPEN_MAX)
    for (fd = 0; fd < _POSIX_OPEN_MAX; fd++)
      close (fd);
#else
    close (0);
    close (1);
    close (2);
#endif

    /* reset signals for the child; not really needed, but... */
    mutt_unblock_signals_system (0);
    act.sa_handler = SIG_DFL;
    act.sa_flags = 0;
    sigemptyset (&act.sa_mask);
    sigaction (SIGTERM, &act, NULL);
    sigaction (SIGTSTP, &act, NULL);
    sigaction (SIGCONT, &act, NULL);

    execle (EXECSHELL, "sh", "-c", cmd, NULL, mutt_envlist ());
    _exit (127); /* execl error */
  }

  /* reset SIGINT, SIGQUIT and SIGCHLD */
  mutt_unblock_signals_system (1);

  return (thepid);
}

static const struct mapping_t LandingHelp[] = {
  { N_("Exit"),  OP_EXIT },
  { N_("Redraw"), OP_REDRAW },
  { N_("Help"),  OP_HELP },
  { NULL,	 0 }
};


/* Landing Page */

static void landing_redraw (MUTTMENU *menu)
{
  int row, col;
  char key[SHORT_STRING];
  BUFFER *messagebuf;
  size_t messagelen;

  menu_redraw (menu);

  if (MuttIndexWindow->rows < 2)
    return;

  messagebuf = mutt_buffer_pool_get ();

  /* L10N:
     Background Edit Landing Page message, first line.
     Displays while the editor is running.
  */
  mutt_buffer_strcpy (messagebuf, _("Waiting for editor to exit"));
  messagelen = mutt_buffer_len (messagebuf);
  row = MuttIndexWindow->rows >= 10 ? 5 : 0;
  col = (MuttIndexWindow->cols > messagelen) ?
    ((MuttIndexWindow->cols - messagelen) / 2) : 0;
  mutt_window_mvaddstr (MuttIndexWindow, row, col, mutt_b2s (messagebuf));

  *key = '\0';
  if (!km_expand_key (key, sizeof(key), km_find_func (MENU_GENERIC, OP_EXIT)))
    strfcpy (key, "<exit>", sizeof(key));

  /* L10N:
     Background Edit Landing Page message, second line.
     Displays while the editor is running.
     %s is the key binding for "<exit>", usually "q".
  */
  mutt_buffer_printf (messagebuf, _("Type '%s' to background compose session."),
                                    key);
  messagelen = mutt_buffer_len (messagebuf);
  row = MuttIndexWindow->rows >= 10 ? 6 : 1;
  col = (MuttIndexWindow->cols > messagelen) ?
    ((MuttIndexWindow->cols - messagelen) / 2) : 0;
  mutt_window_mvaddstr (MuttIndexWindow, row, col, mutt_b2s (messagebuf));

  mutt_buffer_pool_release (&messagebuf);
  mutt_curs_set (0);
}

/* Displays the "waiting for editor" page.
 * Returns:
 *   2 if the the menu is exited, leaving the process backgrounded
 *   0 when the waitpid() indicates the process has exited
 */
static int background_edit_landing_page (pid_t bg_pid)
{
  int done = 0, rc = 0, op;
  short orig_timeout;
  pid_t wait_rc;
  MUTTMENU *menu;
  char helpstr[STRING];

  menu = mutt_new_menu (MENU_GENERIC);
  menu->help = mutt_compile_help (helpstr, sizeof(helpstr),
                                  MENU_GENERIC, LandingHelp);
  menu->pagelen = 0;
  menu->title = _("Waiting for editor to exit");

  mutt_push_current_menu (menu);

  /* Reduce timeout so we poll with bg_pid every second */
  orig_timeout = Timeout;
  Timeout = 1;

  while (!done)
  {
    wait_rc = waitpid (bg_pid, NULL, WNOHANG);
    if ((wait_rc > 0) ||
        ((wait_rc < 0) && (errno == ECHILD)))
    {
      rc = 0;
      break;
    }

#if defined (USE_SLANG_CURSES) || defined (HAVE_RESIZETERM)
    if (SigWinch)
    {
      SigWinch = 0;
      mutt_resize_screen ();
      clearok (stdscr, TRUE);
    }
#endif

    if (menu->redraw)
      landing_redraw (menu);

    op = km_dokey (MENU_GENERIC);

    switch (op)
    {
      case OP_HELP:
        mutt_help (MENU_GENERIC);
        menu->redraw = REDRAW_FULL;
        break;

      case OP_EXIT:
        rc = 2;
        done = 1;
        break;

      case OP_REDRAW:
	clearok (stdscr, TRUE);
        menu->redraw = REDRAW_FULL;
        break;
    }
  }

  Timeout = orig_timeout;

  mutt_pop_current_menu (menu);
  mutt_menuDestroy (&menu);

  return rc;
}


/* Runs editor in the background.
 *
 * After backgrounding the process, the background landing page will
 * be displayed.  The user will have the opportunity to "quit" the
 * landing page, exiting back to the index.  That will return 2
 * (chosen for consistency with other backgrounding functions).
 *
 * If they leave the landing page up, it will detect when the editor finishes
 * and return 0, indicating the callers should continue processing
 * as if it were a foreground edit.
 *
 * Returns:
 *      2  - the edit was backgrounded
 *      0  - background edit completed.
 *     -1  - an error occurred
 */
int mutt_background_edit_file (SEND_CONTEXT *sctx, const char *editor,
                               const char *filename)
{
  BUFFER *cmd;
  pid_t pid;
  int rc = -1;
  BACKGROUND_PROCESS *process;

  cmd = mutt_buffer_pool_get ();

  mutt_expand_file_fmt (cmd, editor, filename);
  pid = mutt_background_run (mutt_b2s (cmd));
  if (pid <= 0)
  {
    mutt_error (_("Error running \"%s\"!"), mutt_b2s (cmd));
    mutt_sleep (2);
    goto cleanup;
  }

  rc = background_edit_landing_page (pid);
  if (rc == 2)
  {
    process = bg_process_new (pid, sctx);
    process_list_add (process);
  }

cleanup:
  mutt_buffer_pool_release (&cmd);
  return rc;
}


/* Background Compose Menu */

typedef struct entry
{
  int num;
  BACKGROUND_PROCESS *process;
} BG_ENTRY;

static const struct mapping_t BgComposeHelp[] = {
  { N_("Exit"),   OP_EXIT },
  /* L10N: Background Compose Menu Help line:
     resume composing the mail
  */
  { N_("Resume"),   OP_GENERIC_SELECT_ENTRY },
  { N_("Help"),   OP_HELP },
  { NULL,	  0 }
};

static const char *bg_format_str (char *dest, size_t destlen, size_t col,
                                  int cols, char op, const char *src,
                                  const char *fmt, const char *ifstring,
                                  const char *elsestring,
                                  void *data, format_flag flags)
{
  BG_ENTRY *entry = (BG_ENTRY *)data;
  SEND_CONTEXT *sctx = entry->process->sctx;
  HEADER *hdr = sctx->msg;
  char tmp[SHORT_STRING];
  char buf[LONG_STRING];
  const char *msgid;
  int optional = (flags & MUTT_FORMAT_OPTIONAL);

  switch (op)
  {
    case 'i':
      msgid = sctx->cur_message_id;
      if (!msgid && sctx->tagged_message_ids)
        msgid = sctx->tagged_message_ids->data;
      if (!optional)
        mutt_format_s (dest, destlen, fmt, NONULL (msgid));
      else if (!msgid)
        optional = 0;
      break;
    case 'n':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, entry->num);
      break;
    case 'p':
      snprintf (tmp, sizeof (tmp), "%%%sd", fmt);
      snprintf (dest, destlen, tmp, entry->process->pid);
      break;
    case 'r':
      buf[0] = 0;
      rfc822_write_address(buf, sizeof(buf), hdr->env->to, 1);
      if (optional && buf[0] == '\0')
        optional = 0;
      mutt_format_s (dest, destlen, fmt, buf);
      break;
    case 'R':
      buf[0] = 0;
      rfc822_write_address(buf, sizeof(buf), hdr->env->cc, 1);
      if (optional && buf[0] == '\0')
        optional = 0;
      mutt_format_s (dest, destlen, fmt, buf);
      break;
    case 's':
      mutt_format_s (dest, destlen, fmt, NONULL (hdr->env->subject));
      break;
    case 'S':
      if (!optional)
      {
        if (entry->process->finished)
          /* L10N:
             Background Compose menu
             flag that indicates the editor process has finished.
          */
          mutt_format_s (dest, destlen, fmt, _("finished"));
        else
          /* L10N:
             Background Compose menu
             flag that indicates the editor process is still running.
          */
          mutt_format_s (dest, destlen, fmt, _("running"));
      }
      else if (!entry->process->finished)
        optional = 0;
      break;
  }

  if (optional)
    mutt_FormatString (dest, destlen, col, cols, ifstring, bg_format_str, entry, flags);
  else if (flags & MUTT_FORMAT_OPTIONAL)
    mutt_FormatString (dest, destlen, col, cols, elsestring, bg_format_str, entry, flags);

  return (src);
}

static void make_bg_entry (char *s, size_t slen, MUTTMENU *m, int num)
{
  BG_ENTRY *entry = &((BG_ENTRY *) m->data)[num];

  mutt_FormatString (s, slen, 0, MuttIndexWindow->cols,
                     NONULL (BackgroundFormat),
                     bg_format_str,
		     entry, MUTT_FORMAT_ARROWCURSOR);
}

static void update_bg_menu (MUTTMENU *menu)
{
  if (SigChld)
  {
    SigChld = 0;
    if (mutt_background_process_waitpid ())
      menu->redraw |= REDRAW_INDEX;
  }
}

static MUTTMENU *create_bg_menu ()
{
  MUTTMENU *menu = NULL;
  BACKGROUND_PROCESS *process;
  BG_ENTRY *entries = NULL;
  int num_entries = 0, i;
  char *helpstr;

  process = ProcessList;
  while (process)
  {
    num_entries++;
    process = process->next;
  }

  menu = mutt_new_menu (MENU_GENERIC);
  menu->make_entry = make_bg_entry;
  menu->custom_menu_update = update_bg_menu;

  /* L10N:
     Background Compose Menu title
  */
  menu->title = _("Background Compose Menu");
  helpstr = safe_malloc (STRING);
  menu->help = mutt_compile_help (helpstr, STRING, MENU_GENERIC,
                                  BgComposeHelp);

  menu->data = entries = safe_calloc (num_entries, sizeof(BG_ENTRY));
  menu->max = num_entries;

  process = ProcessList;
  i = 0;
  while (process)
  {
    entries[i].num = i + 1;
    entries[i].process = process;

    process = process->next;
    i++;
  }

  mutt_push_current_menu (menu);

  return menu;
}

static void free_bg_menu (MUTTMENU **menu)
{
  mutt_pop_current_menu (*menu);
  FREE (&(*menu)->data);
  FREE (&(*menu)->help);
  mutt_menuDestroy (menu);
}

void mutt_background_compose_menu (void)
{
  MUTTMENU *menu;
  int done = 0, op;
  BG_ENTRY *entry;
  BACKGROUND_PROCESS *process;
  SEND_CONTEXT *sctx;
  char msg[SHORT_STRING];

  if (!ProcessList)
  {
    /* L10N:
       Background Compose Menu:
       displayed if there are no background processes and the
       user tries to bring up the background compose menu
    */
    mutt_message _("No backgrounded editing sessions.");
    return;
  }

  /* Force a rescan, just in case somehow the signal was missed. */
  SigChld = 1;
  mutt_background_process_waitpid ();

  /* If there is only one process and it's finished, skip the menu */
  if (!ProcessList->next && ProcessList->finished)
  {
    process = ProcessList;
    sctx = process->sctx;
    process_list_remove (process);
    bg_process_free (&process);
    mutt_send_message_resume (&sctx);
    return;
  }

  menu = create_bg_menu ();
  while (!done)
  {
    switch ((op = mutt_menuLoop (menu)))
    {
      case OP_EXIT:
        done = 1;
        break;

      case OP_GENERIC_SELECT_ENTRY:
        if (menu->data)
        {
          entry = (BG_ENTRY *)(menu->data) + menu->current;
          process = entry->process;
          sctx = process->sctx;

          if (!process->finished)
          {
            snprintf (msg, sizeof(msg),
                      /* L10N:
                         Background Compose menu:
                         Confirms if an unfinished process is selected
                         to continue.
                      */
                      _("Process is still running. Really select?"));
            if (mutt_yesorno (msg, MUTT_NO) != MUTT_YES)
              break;
            mutt_message _("Waiting for editor to exit");
            waitpid (process->pid, NULL, 0);
            mutt_clear_error ();
          }

          process_list_remove (process);
          bg_process_free (&process);

          mutt_send_message_resume (&sctx);

          if (!ProcessList)
          {
            done = 1;
            break;
          }

          free_bg_menu (&menu);
          menu = create_bg_menu ();
        }
        break;
    }
  }

  free_bg_menu (&menu);
}