Refactor wins_launch_external()

* Do window preparation and restoring in separate functions
  wins_prepare_external() and wins_unprepare_external().

* Use fork_exec() and child_wait() instead of system().

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2011-07-01 15:45:11 +02:00
parent 7982c98be4
commit 233980622f
2 changed files with 27 additions and 20 deletions

View File

@ -935,7 +935,9 @@ void wins_update_border (void);
void wins_update_panels (void); void wins_update_panels (void);
void wins_update (void); void wins_update (void);
void wins_reset (void); void wins_reset (void);
void wins_launch_external (const char *, const char *); void wins_prepare_external (void);
void wins_unprepare_external (void);
void wins_launch_external (char *, char *);
void wins_status_bar (void); void wins_status_bar (void);
void wins_erase_status_bar (void); void wins_erase_status_bar (void);
void wins_other_status_page (int); void wins_other_status_page (int);

View File

@ -599,25 +599,10 @@ wins_reset (void)
wins_update (); wins_update ();
} }
/* /* Prepare windows for the execution of an external command. */
* While inside interactive mode, launch the external command cmd on the given
* file.
*/
void void
wins_launch_external (const char *file, const char *cmd) wins_prepare_external (void)
{ {
char *p;
int len;
/* Beware of space between cmd and file. */
len = strlen (file) + strlen (cmd) + 2;
p = (char *) mem_calloc (len, sizeof (char));
if (snprintf (p, len, "%s %s", cmd, file) == -1)
{
mem_free (p);
return;
}
if (notify_bar ()) if (notify_bar ())
notify_stop_main_thread (); notify_stop_main_thread ();
def_prog_mode (); def_prog_mode ();
@ -625,7 +610,12 @@ wins_launch_external (const char *file, const char *cmd)
ui_mode = UI_CMDLINE; ui_mode = UI_CMDLINE;
clear (); clear ();
wins_refresh (); wins_refresh ();
(void)system (p); }
/* Restore windows when returning from an external command. */
void
wins_unprepare_external (void)
{
reset_prog_mode (); reset_prog_mode ();
clearok (curscr, TRUE); clearok (curscr, TRUE);
curs_set (0); curs_set (0);
@ -633,7 +623,22 @@ wins_launch_external (const char *file, const char *cmd)
wins_refresh (); wins_refresh ();
if (notify_bar ()) if (notify_bar ())
notify_start_main_thread (); notify_start_main_thread ();
mem_free (p); }
/*
* While inside interactive mode, launch the external command cmd on the given
* file.
*/
void
wins_launch_external (char *file, char *cmd)
{
char *arg[] = { cmd, file, NULL };
int pid;
wins_prepare_external ();
if ((pid = fork_exec (NULL, NULL, cmd, arg)))
child_wait (NULL, NULL, pid);
wins_unprepare_external ();
} }
#define NB_CAL_CMDS 27 /* number of commands while in cal view */ #define NB_CAL_CMDS 27 /* number of commands while in cal view */