cex

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.wimdupont.com/cex.git
Log | Files | Refs | LICENSE

commit e1132bedbf11f776a7be6a04df9cd8726d35a751
parent a8a01707246d7949a5744668cf39a3dd399b73a7
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sun, 17 Nov 2024 20:10:32 +0100

cleanup

Diffstat:
Mcex.c | 251+++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------
Dcex.h | 152-------------------------------------------------------------------------------
Aconfig.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 233 insertions(+), 233 deletions(-)

diff --git a/cex.c b/cex.c @@ -13,7 +13,96 @@ #include <errno.h> #include <curses.h> -#include "cex.h" +#include "config.h" + +#define ctrl(x) ((x) & 0x1f) + +typedef enum {OTHER, LEFT, RIGHT, UP, DOWN} Direction; +typedef enum {CURRENT, PARENT, CHILD} WinType; +typedef enum {COPY, REMOVE, MOVE} SelAction; + +typedef struct _win_file { + char d_name[256]; + unsigned char d_type; + bool selected; +} WinFile; + +typedef struct _dir_win { + WinType wintype; + WINDOW *window; + WinFile *winfiles; + char path[PATH_MAX]; + char *message; + int maxx; + int maxy; + int startpr; + int highlight; + int filecount; + bool holdupdate; + bool usehighlight; +} DirWin; + +static void init_dirwins(void); +static void init_screen(void); +static void resize(void); +static void start(void); +static void wpath(const char *filename); +static void reset_flags(void); +static void set_startpr(DirWin *dirwin); +static void set_win_files(DirWin *dirwin); +static void set_win_message(DirWin *dirwin, char *message); +static void print_win(DirWin *dirwin); +static void update_child_win(void); +static void print_top_title(void); +static void print_bot_title(void); +static void print_content(void); +static void show_file_mime(void); +static void select_file(const char *path); +static void exe_selection(SelAction action, const char *askn); +static void clear_selected(void); +static void clear_search(void); +static void combo_key(int keypress); +static void combo_go(int keypress); +static void combo_inf(int keypress); +static void combo_open(int keypress); +static void combo_make(int keypress); +static void change_dir(const char *chdname, Direction direction, bool abspath); +static void change_parent_dir(Direction direction); +static void open_child(bool exec); +static void open_nohup_xdg(void); +static void search(void); +static void move_top(DirWin *dirwin); +static void move_bot(DirWin *dirwin); +static void move_page_up(DirWin *dirwin); +static void move_page_down(DirWin *dirwin); +static void move_up(DirWin *dirwin); +static void move_down(DirWin *dirwin); +static void next_search(void); +static void prev_search(void); +static void free_dirwin(DirWin *dirwin); +static void run_command(size_t size, const char *fmt, ...); +static void clean(void); +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(size_t size, 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); +static int read_command(char *buf, size_t bufsize, const char *cmd); +static int make_file(const char *path); +static int make_dir(const char *path); +static int make_mod(void); +static int make_access(void); +static int rm_file(const char *fname); +static int rename_file(const char *fname); +static char *prompt_answer(char *buf, size_t size, const char *question); +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); +static char *get_dirname(char *buf, const char *path); +static char *replace_home(char *buf, const char *path); static DirWin curwin, parwin, childwin; static char *userhome, *username, *editor, **selected, searchq[SEARCHLEN]; @@ -58,8 +147,8 @@ main(int argc, char **argv) clean(); } -static void -init_dirwins() +void +init_dirwins(void) { curwin.wintype = CURRENT; parwin.wintype = PARENT; @@ -83,8 +172,8 @@ init_dirwins() } } -static void -init_screen() +void +init_screen(void) { initscr(); noecho(); @@ -124,8 +213,8 @@ init_screen() print_win(&childwin); } -static void -resize() +void +resize(void) { int startx; @@ -152,8 +241,8 @@ resize() update_child_win(); } -static void -start() +void +start(void) { int c; char chdname[PATH_MAX]; @@ -284,7 +373,7 @@ start() } } -static void +void wpath(const char *filename) { FILE *fptr; @@ -295,8 +384,8 @@ wpath(const char *filename) fclose(fptr); } -static void -reset_flags() +void +reset_flags(void) { parwin.usehighlight = FALSE; curwin.usehighlight = TRUE; @@ -309,7 +398,7 @@ reset_flags() print_bot = TRUE; } -static void +void set_startpr(DirWin *dirwin) { if (dirwin->winfiles == NULL) { @@ -320,7 +409,7 @@ set_startpr(DirWin *dirwin) dirwin->startpr = MAX(dirwin->highlight - dirwin->maxy/2, 0); } -static void +void set_win_files(DirWin *dirwin) { struct dirent *ent; @@ -367,7 +456,7 @@ set_win_files(DirWin *dirwin) qsort(dirwin->winfiles, count, sizeof(WinFile), compare_file); } -static bool +bool is_selected(DirWin *dirwin, size_t index) { char buf[PATH_MAX]; @@ -385,7 +474,7 @@ is_selected(DirWin *dirwin, size_t index) return FALSE; } -static bool +bool remove_selected(const char *sel) { bool res = FALSE; @@ -414,14 +503,14 @@ remove_selected(const char *sel) return res; } -static void +void set_win_message(DirWin *dirwin, char *message) { free_dirwin(dirwin); dirwin->message = message; } -static void +void print_win(DirWin *dirwin) { if (dirwin->holdupdate) @@ -509,8 +598,8 @@ print_win(DirWin *dirwin) wrefresh(dirwin->window); } -static void -update_child_win() +void +update_child_win(void) { char pathbuf[PATH_MAX]; @@ -545,8 +634,8 @@ update_child_win() } } -static void -print_top_title() +void +print_top_title(void) { move(0, 0); clrtoeol(); @@ -555,8 +644,8 @@ print_top_title() attroff(COLOR_PAIR(TOP_TITLE_COLOR)); } -static void -print_bot_title() +void +print_bot_title(void) { char pathbuf[PATH_MAX], fileinf[maxx]; @@ -576,7 +665,7 @@ print_bot_title() attroff(COLOR_PAIR(BOT_TITLE_INFO_COLOR)); } -static char * +char * get_file_info(char *buf, const char *filepath) { struct stat statbuf; @@ -631,8 +720,8 @@ get_file_info(char *buf, const char *filepath) return buf; } -static void -print_content() +void +print_content(void) { FILE *fp = NULL; size_t size = childwin.maxx; @@ -663,8 +752,8 @@ print_content() wrefresh(childwin.window); } -static void -show_file_mime() +void +show_file_mime(void) { char buf[MIME_MAX]; @@ -676,7 +765,7 @@ show_file_mime() addstr(buf); } -static void +void combo_key(int keypress) { int c; @@ -712,7 +801,7 @@ combo_key(int keypress) clrtoeol(); } -static void +void combo_go(int key) { char pathbuf[PATH_MAX]; @@ -733,7 +822,7 @@ combo_go(int key) } } -static void +void combo_inf(int key) { switch (key) { @@ -745,7 +834,7 @@ combo_inf(int key) } } -static void +void combo_open(int key) { char chdname[PATH_MAX]; @@ -768,7 +857,7 @@ combo_open(int key) } } -static void +void combo_make(int key) { switch (key) { @@ -793,7 +882,7 @@ combo_make(int key) } } -static void +void change_dir(const char *chdname, Direction direction, bool abspath) { if (chdir(chdname) != 0) @@ -825,7 +914,7 @@ change_dir(const char *chdname, Direction direction, bool abspath) update_child_win(); } -static void +void change_parent_dir(Direction direction) { char pp[PATH_MAX]; @@ -846,7 +935,7 @@ change_parent_dir(Direction direction) change_dir(get_fullpath(pp, &parwin, parwin.highlight), direction, FALSE); } -static void +void open_child(bool exec) { sigset_t set; @@ -903,8 +992,8 @@ open_child(bool exec) update_child_win(); } -static void -open_nohup_xdg() +void +open_nohup_xdg(void) { char *cmdfmt = "nohup xdg-open \"%s\" > /dev/null 2>&1 &"; size_t size = PATH_MAX + strlen(cmdfmt); @@ -912,8 +1001,8 @@ open_nohup_xdg() run_command(size, cmdfmt, curwin.winfiles[curwin.highlight].d_name); } -static void -search() +void +search(void) { int c, y, x; @@ -953,47 +1042,47 @@ search() curs_set(0); } -static void +void move_top(DirWin *dirwin) { dirwin->highlight = 0; } -static void +void move_bot(DirWin *dirwin) { dirwin->highlight = MAX(dirwin->filecount-1, 0); } -static void +void move_page_up(DirWin *dirwin) { dirwin->highlight = MAX(dirwin->highlight - dirwin->maxy/2, 0); } -static void +void move_page_down(DirWin *dirwin) { dirwin->highlight = MIN(dirwin->highlight + dirwin->maxy/2, MAX(dirwin->filecount-1, 0)); } -static void +void move_up(DirWin *dirwin) { if (dirwin->highlight > 0) dirwin->highlight--; } -static void +void move_down(DirWin *dirwin) { if (dirwin->highlight < dirwin->filecount-1) dirwin->highlight++; } -static void -next_search() +void +next_search(void) { if (searchc == 0) return; @@ -1006,8 +1095,8 @@ next_search() } } -static void -prev_search() +void +prev_search(void) { if (searchc == 0) return; @@ -1020,7 +1109,7 @@ prev_search() } } -static void +void free_dirwin(DirWin *dirwin) { free(dirwin->winfiles); @@ -1029,8 +1118,8 @@ free_dirwin(DirWin *dirwin) dirwin->message = NULL; } -static void -clean() +void +clean(void) { wclear(curwin.window); wclear(parwin.window); @@ -1055,7 +1144,7 @@ clean() endwin(); } -static void +void fatal(const char *fmt, ...) { va_list ap; @@ -1069,7 +1158,7 @@ fatal(const char *fmt, ...) exit(EXIT_FAILURE); } -static bool +bool is_dir(DirWin *dirwin, size_t index) { struct stat statbuf; @@ -1089,7 +1178,7 @@ is_dir(DirWin *dirwin, size_t index) return FALSE; } -static char * +char * prompt_answer(char *buf, size_t size, const char *question) { move(maxy-1, 0); @@ -1108,7 +1197,7 @@ prompt_answer(char *buf, size_t size, const char *question) return buf; } -static bool +bool prompt_confirm(size_t size, const char *fmt, ...) { char response[1], question[size]; @@ -1123,7 +1212,7 @@ prompt_confirm(size_t size, const char *fmt, ...) return strcasecmp(response, "y") == 0; } -static int +int compare_file(const void *a, const void *b) { int typecompare; @@ -1136,7 +1225,7 @@ compare_file(const void *a, const void *b) return typecompare; } -static int +int get_mime(char *buf, size_t bufsize, const char *path) { char *cmdfmt = "xdg-mime query filetype \"%s\""; @@ -1148,7 +1237,7 @@ get_mime(char *buf, size_t bufsize, const char *path) return read_command(buf, bufsize, cmd); } -static int +int get_mime_default(char *buf, size_t bufsize, const char *mime) { char *cmdfmt = "xdg-mime query default \"%s\""; @@ -1160,7 +1249,7 @@ get_mime_default(char *buf, size_t bufsize, const char *mime) return read_command(buf, bufsize, cmd); } -static int +int read_command(char *buf, size_t bufsize, const char *cmd) { FILE *file; @@ -1183,7 +1272,7 @@ read_command(char *buf, size_t bufsize, const char *cmd) return 0; } -static void +void run_command(size_t size, const char *fmt, ...) { va_list ap; @@ -1196,7 +1285,7 @@ run_command(size_t size, const char *fmt, ...) system(cmd); } -static int +int make_dir(const char *path) { char name[PATH_MAX]; @@ -1205,7 +1294,7 @@ make_dir(const char *path) return mkdir(name, 0755); } -static int +int make_file(const char *path) { FILE *fptr; @@ -1220,7 +1309,7 @@ make_file(const char *path) } } -static int +int rm_file(const char *fname) { char *msg, *rmdirfmt = "rm -rf \"%s\"", *pfmt = "Remove %s? (y/N) "; @@ -1257,7 +1346,7 @@ rm_file(const char *fname) return res; } -static int +int rename_file(const char *fname) { char name[PATH_MAX]; @@ -1270,8 +1359,8 @@ rename_file(const char *fname) return 0; } -static int -make_mod() +int +make_mod(void) { char name[3]; bool isvalid; @@ -1293,8 +1382,8 @@ make_mod() return 0; } -static int -make_access() +int +make_access(void) { char pathbuf[PATH_MAX], *cmdfmt = "ln -s \"%s\" \"%s\"", *pfmt = "Make access \"%s\"? (y/N) "; size_t cmdsize = PATH_MAX + strlen(cmdfmt), psize = strlen(pfmt) + PATH_MAX; @@ -1303,7 +1392,7 @@ make_access() run_command(cmdsize, cmdfmt, get_fullpath(pathbuf, &curwin, curwin.highlight), LN_ACCESS_DIR); } -static void +void exe_selection(SelAction action, const char *askn) { char *pfmt = "%s selection (%d files) ? (y/N) "; @@ -1339,7 +1428,7 @@ exe_selection(SelAction action, const char *askn) update_child_win(); } -static void +void select_file(const char *path) { if (selc == 0 && ((selected = (char**) malloc(sizeof(char *)))) == NULL) @@ -1355,8 +1444,8 @@ select_file(const char *path) (selc)++; } -static void -clear_selected() +void +clear_selected(void) { for (size_t i = 0; i < selc; i++) free(selected[i]); @@ -1366,15 +1455,15 @@ clear_selected() selc = 0; } -static void -clear_search() +void +clear_search(void) { for (size_t i = 0; i < SEARCHLEN; i++) searchq[i] = '\0'; searchc = 0; } -static char * +char * cpstr(char *dest, const char *src) { size_t cplen = strlen(src); @@ -1383,7 +1472,7 @@ cpstr(char *dest, const char *src) return dest; } -static char * +char * get_fullpath(char *buf, DirWin *dirwin, size_t index) { index = MIN(index, dirwin->filecount-1); @@ -1397,7 +1486,7 @@ get_fullpath(char *buf, DirWin *dirwin, size_t index) return buf; } -static char * +char * replace_home(char *buf, const char *path) { char *envv = "$HOME"; @@ -1409,7 +1498,7 @@ replace_home(char *buf, const char *path) return buf; } -static char * +char * get_dirname(char *buf, const char *path) { cpstr(buf, path); diff --git a/cex.h b/cex.h @@ -1,152 +0,0 @@ -#ifndef DISCODL_H -#define DISCODL_H - -#define DEFAULT_HIDE TRUE -#define MIME_MAX 255 -#define MIME_APP_MAX 255 -#define BORDER_SPACE_SIZE 2 -#define SEARCHLEN 20 -#define LN_ACCESS_DIR "$HOME/access" - -#define KEY_QUIT 'q' -#define KEY_VUP 'k' -#define KEY_VDOWN 'j' -#define KEY_VPUP 'K' -#define KEY_VPDOWN 'J' -#define KEY_VLEFT 'h' -#define KEY_VRIGHT 'l' -#define KEY_VRIGHT_ABS 'L' -#define KEY_BOT 'G' -#define KEY_NEXT_SEARCH 'n' -#define KEY_PREV_SEARCH 'N' -#define KEY_HIDE '.' -#define KEY_SEL_FILE ' ' -#define KEY_CLEAR_SEL 'c' -#define KEY_CLEAR_SEARCH 'C' -#define KEY_MAKE_FILE 'f' -#define KEY_RM_FILE 'D' -#define KEY_RENAME_FILE 'r' -#define KEY_CP_SEL 'p' -#define KEY_RM_SEL 'd' -#define KEY_MV_SEL 'M' -#define KEY_SEARCH '/' - -#define KEY_COMBO_GO 'g' -#define KEY_COMBO_GO_TOP 'g' -#define KEY_COMBO_GO_HOME 'h' -#define KEY_COMBO_GO_ACCESS 'a' - -#define KEY_COMBO_INF 'i' -#define KEY_COMBO_INF_OPEN 'o' - -#define KEY_COMBO_OPEN 'o' -#define KEY_COMBO_OPEN_EXEC 'x' -#define KEY_COMBO_OPEN_NOHUP_XDG 'o' - -#define KEY_COMBO_MAKE 'm' -#define KEY_COMBO_MAKE_FILE 'f' -#define KEY_COMBO_MAKE_DIR 'd' -#define KEY_COMBO_MAKE_MOD 'm' -#define KEY_COMBO_MAKE_ACCESS 'a' - -#define DIR_COLOR COLOR_BLUE -#define LN_COLOR COLOR_CYAN -#define INVALID_LN_COLOR COLOR_RED -#define SEARCH_MATCH_COLOR COLOR_YELLOW -#define TOP_TITLE_COLOR COLOR_YELLOW -#define BOT_TITLE_COUNT_COLOR COLOR_BLUE -#define BOT_TITLE_INFO_COLOR COLOR_RED -#define PROMPT_COLOR COLOR_MAGENTA -#define CHILDWIN_MESSAGE_COLOR COLOR_MAGENTA -#define MARK_SELECTED_COLOR COLOR_GREEN - -#define ctrl(x) ((x) & 0x1f) - -typedef enum {OTHER, LEFT, RIGHT, UP, DOWN} Direction; -typedef enum {CURRENT, PARENT, CHILD} WinType; -typedef enum {COPY, REMOVE, MOVE} SelAction; - -typedef struct _win_file { - char d_name[256]; - unsigned char d_type; - bool selected; -} WinFile; - -typedef struct _dir_win { - WinType wintype; - WINDOW *window; - WinFile *winfiles; - char path[PATH_MAX]; - char *message; - int maxx; - int maxy; - int startpr; - int highlight; - int filecount; - bool holdupdate; - bool usehighlight; -} DirWin; - -static void init_dirwins(); -static void init_screen(); -static void resize(); -static void start(); -static void wpath(const char *filename); -static void reset_flags(); -static void set_startpr(DirWin *dirwin); -static void set_win_files(DirWin *dirwin); -static void set_win_message(DirWin *dirwin, char *message); -static void print_win(DirWin *dirwin); -static void update_child_win(); -static void print_top_title(); -static void print_bot_title(); -static void print_content(); -static void show_file_mime(); -static void select_file(const char *path); -static void exe_selection(SelAction action, const char *askn); -static void clear_selected(); -static void clear_search(); -static void combo_key(int keypress); -static void combo_go(int keypress); -static void combo_inf(int keypress); -static void combo_open(int keypress); -static void combo_make(int keypress); -static void change_dir(const char *chdname, Direction direction, bool abspath); -static void change_parent_dir(Direction direction); -static void open_child(bool exec); -static void open_nohup_xdg(); -static void search(); -static void move_top(DirWin *dirwin); -static void move_bot(DirWin *dirwin); -static void move_page_up(DirWin *dirwin); -static void move_page_down(DirWin *dirwin); -static void move_up(DirWin *dirwin); -static void move_down(DirWin *dirwin); -static void next_search(); -static void prev_search(); -static void free_dirwin(DirWin *dirwin); -static void run_command(size_t size, const char *fmt, ...); -static void clean(); -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(size_t size, 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); -static int read_command(char *buf, size_t bufsize, const char *cmd); -static int make_file(const char *path); -static int make_dir(const char *path); -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 *question); -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); -static char *get_dirname(char *buf, const char *path); -static char *replace_home(char *buf, const char *path); - -#endif diff --git a/config.h b/config.h @@ -0,0 +1,63 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define DEFAULT_HIDE TRUE +#define MIME_MAX 255 +#define MIME_APP_MAX 255 +#define BORDER_SPACE_SIZE 2 +#define SEARCHLEN 20 +#define LN_ACCESS_DIR "$HOME/access" + +#define KEY_QUIT 'q' +#define KEY_VUP 'k' +#define KEY_VDOWN 'j' +#define KEY_VPUP 'K' +#define KEY_VPDOWN 'J' +#define KEY_VLEFT 'h' +#define KEY_VRIGHT 'l' +#define KEY_VRIGHT_ABS 'L' +#define KEY_BOT 'G' +#define KEY_NEXT_SEARCH 'n' +#define KEY_PREV_SEARCH 'N' +#define KEY_HIDE '.' +#define KEY_SEL_FILE ' ' +#define KEY_CLEAR_SEL 'c' +#define KEY_CLEAR_SEARCH 'C' +#define KEY_MAKE_FILE 'f' +#define KEY_RM_FILE 'D' +#define KEY_RENAME_FILE 'r' +#define KEY_CP_SEL 'p' +#define KEY_RM_SEL 'd' +#define KEY_MV_SEL 'M' +#define KEY_SEARCH '/' + +#define KEY_COMBO_GO 'g' +#define KEY_COMBO_GO_TOP 'g' +#define KEY_COMBO_GO_HOME 'h' +#define KEY_COMBO_GO_ACCESS 'a' + +#define KEY_COMBO_INF 'i' +#define KEY_COMBO_INF_OPEN 'o' + +#define KEY_COMBO_OPEN 'o' +#define KEY_COMBO_OPEN_EXEC 'x' +#define KEY_COMBO_OPEN_NOHUP_XDG 'o' + +#define KEY_COMBO_MAKE 'm' +#define KEY_COMBO_MAKE_FILE 'f' +#define KEY_COMBO_MAKE_DIR 'd' +#define KEY_COMBO_MAKE_MOD 'm' +#define KEY_COMBO_MAKE_ACCESS 'a' + +#define DIR_COLOR COLOR_BLUE +#define LN_COLOR COLOR_CYAN +#define INVALID_LN_COLOR COLOR_RED +#define SEARCH_MATCH_COLOR COLOR_YELLOW +#define TOP_TITLE_COLOR COLOR_YELLOW +#define BOT_TITLE_COUNT_COLOR COLOR_BLUE +#define BOT_TITLE_INFO_COLOR COLOR_RED +#define PROMPT_COLOR COLOR_MAGENTA +#define CHILDWIN_MESSAGE_COLOR COLOR_MAGENTA +#define MARK_SELECTED_COLOR COLOR_GREEN + +#endif