commit 1dfee0a16c83d73b5d6844574552aed4ab455b08
parent 109c1e21635dd1c77cab80b1ae6f8739dadd8da9
Author: Wim Dupont <wim@wimdupont.com>
Date: Mon, 22 Jul 2024 23:05:31 +0200
command fixes and added prompt functions
Diffstat:
M | cex.c | | | 168 | +++++++++++++++++++++++++++++++++---------------------------------------------- |
M | cex.h | | | 2 | ++ |
2 files changed, 73 insertions(+), 97 deletions(-)
diff --git a/cex.c b/cex.c
@@ -290,7 +290,7 @@ wpath(const char *filename)
FILE *fptr;
if ((fptr = fopen(filename, "w")) == NULL)
- fatal("Error opening file '%s'\n", filename);
+ fatal("Error opening file \"%s\"\n", filename);
fprintf(fptr, "%s\n", curwin.path);
fclose(fptr);
}
@@ -648,7 +648,7 @@ print_content()
set_win_message(&childwin, "No permission.");
return;
default:
- fatal("Error opening file '%s'\n", buf);
+ fatal("Error opening file \"%s\"\n", buf);
}
}
@@ -853,7 +853,7 @@ open_child()
if (get_mime(mime, MIME_MAX, curwin.winfiles[curwin.highlight].d_name) != 0) {
move(1, 1);
clrtoeol();
- mvprintw(1, 1, "Can't open %s", curwin.winfiles[curwin.highlight].d_name);
+ mvprintw(1, 1, "Can\'t open %s", curwin.winfiles[curwin.highlight].d_name);
return;
}
@@ -862,7 +862,7 @@ open_child()
if (!istext && get_mime_default(mimedefault, MIME_APP_MAX, mime) != 0) {
move(1, 1);
clrtoeol();
- mvprintw(1, 1, "Can't open for mime %s", mime);
+ mvprintw(1, 1, "Can\'t open for mime %s", mime);
return;
}
@@ -875,7 +875,7 @@ open_child()
if (sigprocmask(SIG_BLOCK, &set, NULL) != 0)
fatal("Blocking sigprocmask failed.");
- run_command(PATH_MAX + 12, "%s '%s'", istext ? editor : "xdg-open",
+ run_command(PATH_MAX + 12, "%s \"%s\"", istext ? editor : "xdg-open",
get_fullpath(pathbuf, &curwin, curwin.highlight));
if (sigprocmask(SIG_UNBLOCK, &set, NULL) != 0)
@@ -895,7 +895,7 @@ open_child()
static void
open_nohup_xdg()
{
- char *cmdfmt = "nohup xdg-open '%s' > /dev/null 2>&1 &";
+ char *cmdfmt = "nohup xdg-open \"%s\" > /dev/null 2>&1 &";
size_t size = PATH_MAX + strlen(cmdfmt);
run_command(size, cmdfmt, curwin.winfiles[curwin.highlight].d_name);
@@ -1078,6 +1078,53 @@ is_dir(DirWin *dirwin, size_t index)
return FALSE;
}
+static char *
+prompt_answer(char *buf, size_t size, const char *fmt, ...)
+{
+ va_list ap;
+
+ move(maxy-1, 0);
+ clrtoeol();
+
+ attron(COLOR_PAIR(PROMPT_COLOR));
+ va_start(ap, fmt);
+ vw_printw(stdscr, fmt, ap);
+ va_end(ap);
+ attroff(COLOR_PAIR(PROMPT_COLOR));
+
+ echo();
+ curs_set(1);
+ getnstr(buf, size);
+ noecho();
+ curs_set(0);
+
+ return buf;
+}
+
+static bool
+prompt_confirm(const char *fmt, ...)
+{
+ char response[1];
+ va_list ap;
+
+ move(maxy-1, 0);
+ clrtoeol();
+
+ attron(COLOR_PAIR(PROMPT_COLOR));
+ va_start(ap, fmt);
+ vw_printw(stdscr, fmt, ap);
+ va_end(ap);
+ attroff(COLOR_PAIR(PROMPT_COLOR));
+
+ echo();
+ curs_set(1);
+ getnstr(response, 1);
+ noecho();
+ curs_set(0);
+
+ return strcasecmp(response, "y") == 0;
+}
+
static int
compare_file(const void *a, const void *b)
{
@@ -1094,7 +1141,7 @@ compare_file(const void *a, const void *b)
static int
get_mime(char *buf, size_t bufsize, const char *path)
{
- char *cmdfmt = "xdg-mime query filetype '%s'";
+ char *cmdfmt = "xdg-mime query filetype \"%s\"";
size_t size = PATH_MAX + strlen(cmdfmt);
char cmd[size];
@@ -1106,7 +1153,7 @@ get_mime(char *buf, size_t bufsize, const char *path)
static int
get_mime_default(char *buf, size_t bufsize, const char *mime)
{
- char *cmdfmt = "xdg-mime query default '%s'";
+ char *cmdfmt = "xdg-mime query default \"%s\"";
size_t cmdsize = MIME_MAX + strlen(cmdfmt);
char cmd[cmdsize];
@@ -1156,19 +1203,7 @@ make_dir(const char *path)
{
char name[PATH_MAX];
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- addstr("Name of directory? ");
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(name, PATH_MAX);
- noecho();
- curs_set(0);
-
+ prompt_answer(name, PATH_MAX, "Name of directory? ");
return mkdir(name, 0755);
}
@@ -1178,22 +1213,11 @@ make_file(const char *path)
FILE *fptr;
char name[PATH_MAX];
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- addstr("Name of file? ");
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(name, PATH_MAX);
- noecho();
- curs_set(0);
+ prompt_answer(name, PATH_MAX, "Name of file? ");
if (strlen(name) > 0) {
if ((fptr = fopen(name, "w")) == NULL)
- fatal("Error opening file '%s'\n", name);
+ fatal("Error opening file \"%s\"\n", name);
fclose(fptr);
}
}
@@ -1201,24 +1225,11 @@ make_file(const char *path)
static int
rm_file(const char *fname)
{
- char *msg, response[1], *rmdirfmt = "rm -rf %s";
+ char *msg, *rmdirfmt = "rm -rf \"%s\"";
int res = 0;
size_t size = PATH_MAX + strlen(rmdirfmt);
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- printw("Remove %s? (y/N) ", fname);
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(response, 1);
- noecho();
- curs_set(0);
-
- if (strcasecmp(response, "y") == 0) {
+ if (prompt_confirm("Remove %s? (y/N) ", fname)) {
if (is_dir(&curwin, curwin.highlight))
run_command(size, rmdirfmt, fname);
else
@@ -1253,18 +1264,7 @@ rename_file(const char *fname)
{
char name[PATH_MAX];
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- addstr("New name? ");
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(name, PATH_MAX);
- noecho();
- curs_set(0);
+ prompt_answer(name, PATH_MAX, "New name? ");
if (strlen(name) > 0)
return rename(fname, name);
@@ -1278,18 +1278,7 @@ make_mod()
char name[3];
bool isvalid;
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- addstr("File mods (numeric)? ");
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(name, 3);
- noecho();
- curs_set(0);
+ prompt_answer(name, 3, "File mods (numeric)? ");
if (strlen(name) == 3) {
isvalid = TRUE;
@@ -1299,7 +1288,7 @@ make_mod()
}
if (isvalid)
chmod(curwin.winfiles[curwin.highlight].d_name, strtol(name, NULL, 8));
- else
+ else
return -1;
}
@@ -1309,51 +1298,36 @@ make_mod()
static int
make_access()
{
- char pathbuf[PATH_MAX], *cmdfmt = "ln -s %s %s";
+ char pathbuf[PATH_MAX], *cmdfmt = "ln -s \"%s\" \"%s\"";
size_t size = PATH_MAX + strlen(cmdfmt);
- run_command(size, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
+ if (prompt_confirm("Make access \"%s\"? (y/N) ", curwin.winfiles[curwin.highlight].d_name))
+ run_command(size, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR);
}
static void
exe_selection(SelAction action, const char *askn)
{
- char response[1];
size_t size = PATH_MAX*2 + 20;
if (selected == NULL || selc == 0)
return;
- if (askn != NULL) {
- move(maxy-1, 0);
- clrtoeol();
-
- attron(COLOR_PAIR(PROMPT_COLOR));
- printw("%s selection (%d files) ? (y/N) ", askn, selc);
- attroff(COLOR_PAIR(PROMPT_COLOR));
-
- echo();
- curs_set(1);
- getnstr(response, 1);
- noecho();
- curs_set(0);
-
- if (!strcasecmp(response, "y") == 0)
- return;
- }
+ if (askn != NULL && !prompt_confirm("%s selection (%d files) ? (y/N) ", askn, selc))
+ return;
switch (action) {
case COPY:
for (size_t i = 0; i <selc; i++)
- run_command(size, "cp -rf %s %s", selected[i], curwin.path);
+ run_command(size, "cp -rf \"%s\" \"%s\"", selected[i], curwin.path);
break;
case REMOVE:
for (size_t i = 0; i <selc; i++)
- run_command(size, "rm -rf %s", selected[i]);
+ run_command(size, "rm -rf \"%s\"", selected[i]);
break;
case MOVE:
for (size_t i = 0; i <selc; i++)
- run_command(size, "mv %s %s", selected[i], curwin.path);
+ run_command(size, "mv \"%s\" \"%s\"", selected[i], curwin.path);
break;
default:
break;
diff --git a/cex.h b/cex.h
@@ -130,6 +130,7 @@ static void fatal(const char *fmt, ...);
static bool is_dir(DirWin *dirwin, size_t index);
static bool is_selected(DirWin *dirwin, size_t count);
static bool remove_selected(const char *sel);
+static bool prompt_confirm(const char *fmt, ...);
static int compare_file(const void *a, const void *b);
static int get_mime(char *buf, size_t bufsize, const char *path);
static int get_mime_default(char *buf, size_t bufsize, const char *mime);
@@ -140,6 +141,7 @@ static int make_mod();
static int make_access();
static int rm_file(const char *fname);
static int rename_file();
+static char *prompt_answer(char *buf, size_t size, const char *fmt, ...);
static char *cpstr(char *dest, const char *src);
static char *get_file_info(char *buf, const char *filepath);
static char *get_fullpath(char *buf, DirWin *dirwin, size_t index);