Skip to content

Commit

Permalink
Changes yes_no_query to signal error on cmus exit (cmus#858)
Browse files Browse the repository at this point in the history
This prevents the cmus process from zombiefying, when the Terminal is
closed while a yes/no query is displayed.

Fixes cmus#765
  • Loading branch information
nefthy authored and flyingmutant committed Feb 15, 2019
1 parent 483d186 commit fead80b
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ void browser_delete(void)
char *name;

name = fullname(browser_dir, e->name);
if (yes_no_query("Delete file '%s'? [y/N]", e->name)) {
if (yes_no_query("Delete file '%s'? [y/N]", e->name) == UI_QUERY_ANSWER_YES) {
if (unlink(name) == -1) {
error_msg("deleting '%s': %s", e->name, strerror(errno));
} else {
Expand Down
9 changes: 6 additions & 3 deletions command_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,14 @@ static void cmd_showbind(char *arg)
static void cmd_quit(char *arg)
{
int flag = parse_flags((const char **)&arg, "i");
enum ui_query_answer answer;
if (!worker_has_job_by_type(JOB_TYPE_ANY)) {
if (flag != 'i' || yes_no_query("Quit cmus? [y/N]"))
answer = yes_no_query("Quit cmus? [y/N]");
if (flag != 'i' || answer != UI_QUERY_ANSWER_NO)
cmus_running = 0;
} else {
if (yes_no_query("Tracks are being added. Quit and truncate playlist(s)? [y/N]"))
answer = yes_no_query("Tracks are being added. Quit and truncate playlist(s)? [y/N]");
if (answer != UI_QUERY_ANSWER_NO)
cmus_running = 0;
}
}
Expand Down Expand Up @@ -1043,7 +1046,7 @@ static void cmd_run(char *arg)

run = 1;
if (confirm_run && (sel.tis_nr > 1 || strcmp(argv[0], "rm") == 0)) {
if (!yes_no_query("Execute %s for the %d selected files? [y/N]", arg, sel.tis_nr)) {
if (yes_no_query("Execute %s for the %d selected files? [y/N]", arg, sel.tis_nr) != UI_QUERY_ANSWER_YES) {
info_msg("Aborted");
run = 0;
}
Expand Down
2 changes: 1 addition & 1 deletion filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ void filters_delete_filter(void)
struct filter_entry *e;

e = iter_to_filter_entry(&iter);
if (yes_no_query("Delete filter '%s'? [y/N]", e->name)) {
if (yes_no_query("Delete filter '%s'? [y/N]", e->name) == UI_QUERY_ANSWER_YES) {
window_row_vanishes(filters_win, &iter);
list_del(&e->node);
free_filter(e);
Expand Down
2 changes: 1 addition & 1 deletion help.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ void help_remove(void)
ent = iter_to_help_entry(&sel);
switch (ent->type) {
case HE_BOUND:
if (yes_no_query("Remove selected binding? [y/N]"))
if (yes_no_query("Remove selected binding? [y/N]") == UI_QUERY_ANSWER_YES)
key_unbind(key_context_names[ent->binding->ctx],
ent->binding->key->name, 0);
break;
Expand Down
2 changes: 1 addition & 1 deletion input.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ static void set_ip_priority(void *data, const char *val)
static const char *msg =
"Metadata might become inconsistent "
"after this change. Continue? [y/N]";
if (!yes_no_query("%s", msg)) {
if (yes_no_query("%s", msg) != UI_QUERY_ANSWER_YES) {
info_msg("Aborted");
return;
}
Expand Down
6 changes: 3 additions & 3 deletions pl.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ static void pl_delete_selected_pl(void)
return;
}

if (!yes_no_query("Delete selected playlist? [y/N]"))
if (yes_no_query("Delete selected playlist? [y/N]") != UI_QUERY_ANSWER_YES)
return;

struct playlist *pl = pl_visible;
Expand Down Expand Up @@ -609,7 +609,7 @@ void pl_import(const char *path)
void pl_export_selected_pl(const char *path)
{
char *tmp = expand_filename(path);
if (access(tmp, F_OK) != 0 || yes_no_query("File exists. Overwrite? [y/N]"))
if (access(tmp, F_OK) != 0 || yes_no_query("File exists. Overwrite? [y/N]") == UI_QUERY_ANSWER_YES)
cmus_save(pl_save_cb, tmp, pl_visible);
free(tmp);
}
Expand Down Expand Up @@ -826,7 +826,7 @@ void pl_win_toggle(void)

void pl_win_update(void)
{
if (!yes_no_query("Reload this playlist? [y/N]"))
if (yes_no_query("Reload this playlist? [y/N]") != UI_QUERY_ANSWER_YES)
return;

pl_clear_visible_pl();
Expand Down
22 changes: 16 additions & 6 deletions ui_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -1599,7 +1599,7 @@ void error_msg(const char *format, ...)
}
}

int yes_no_query(const char *format, ...)
enum ui_query_answer yes_no_query(const char *format, ...)
{
char buffer[512];
va_list ap;
Expand All @@ -1623,12 +1623,21 @@ int yes_no_query(const char *format, ...)

while (1) {
int ch = getch();

if (ch == ERR || ch == 0)
if (ch == ERR || ch == 0) {
if (!cmus_running) {
ret = UI_QUERY_ANSWER_ERROR;
break;
}
continue;
if (ch == 'y')
ret = 1;
break;
}

if (ch == 'y') {
ret = UI_QUERY_ANSWER_YES;
break;
} else {
ret = UI_QUERY_ANSWER_NO;
break;
}
}
update_commandline();
return ret;
Expand Down Expand Up @@ -1885,6 +1894,7 @@ static void sig_int(int sig)

static void sig_shutdown(int sig)
{
d_print("sig_shutdown %d\n", sig);
cmus_running = 0;
}

Expand Down
8 changes: 7 additions & 1 deletion ui_curses.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ enum ui_input_mode {
SEARCH_MODE
};

enum ui_query_answer {
UI_QUERY_ANSWER_ERROR = -1,
UI_QUERY_ANSWER_NO = 0,
UI_QUERY_ANSWER_YES = 1
};

#include <signal.h>

extern volatile sig_atomic_t cmus_running;
Expand All @@ -54,7 +60,7 @@ void update_colors(void);
void update_full(void);
void info_msg(const char *format, ...) CMUS_FORMAT(1, 2);
void error_msg(const char *format, ...) CMUS_FORMAT(1, 2);
int yes_no_query(const char *format, ...) CMUS_FORMAT(1, 2);
enum ui_query_answer yes_no_query(const char *format, ...) CMUS_FORMAT(1, 2);
void search_not_found(void);
void set_view(int view);
void set_client_fd(int fd);
Expand Down

0 comments on commit fead80b

Please sign in to comment.