From 4e68f637300c0e360d49f8f672d0675d42da0d1f Mon Sep 17 00:00:00 2001 From: Louie Shprung Date: Sun, 11 Aug 2024 17:49:28 -0400 Subject: (Currently broken) reimplementation of group and entry --- src/draw.c | 27 ++- src/entry.c | 84 +------- src/group.c | 173 ++-------------- src/include/entry.h | 18 +- src/include/group.h | 24 +-- src/include/read_cfg.h | 17 +- src/read_cfg.c | 551 ++++++------------------------------------------- src/unix/read_cfg.c | 94 --------- 8 files changed, 121 insertions(+), 867 deletions(-) diff --git a/src/draw.c b/src/draw.c index 15a9289..1775329 100644 --- a/src/draw.c +++ b/src/draw.c @@ -42,7 +42,6 @@ int true_hover = 0; //0 = hovering on groups, 1 = hovering on entries GROUP **g; ENTRY **e; int g_count; -int e_count; int g_offset = 0; int *e_offset; @@ -70,15 +69,18 @@ int main(int argc, char **argv){ //Fill Groups //read the contents of the cfg file; print help message if invalid - if(!cfg_interp(cfg_path)){ + g = cfg_interp(cfg_path, &g_count); + if(g == NULL) { print_help(argv[0]); return 1; } + /* //Remove Empty Groups from the Array clean_groups(); g = get_groups(); //retrieve results of cfg_interp g_count = get_gcount(); //retrieve number of groups in g (only do this after removing empty groups) + */ //check that there are is at least one valid group if(g_count == 0){ @@ -91,7 +93,7 @@ int main(int argc, char **argv){ e_offset = calloc(g_count, sizeof(int)); //load cached data - load_cache(g_count, &g_hover, &e_hover, &e_offset, &true_hover, cfg_path); + //load_cache(g_count, &g_hover, &e_hover, &e_offset, &true_hover, cfg_path); //reopen stdout for drawing menu freopen("/dev/tty", "w", stdout); @@ -125,6 +127,7 @@ int main(int argc, char **argv){ update_display(true); //drawing is done, now run a while loop to receive input (ESC ends this loop) + input = 0; while(input != 27){ input = getch(); @@ -151,12 +154,12 @@ int main(int argc, char **argv){ case KEY_NPAGE: //case KEY_SDOWN: - trav_col((true_hover ? e_count : g_count)-1); + trav_col((true_hover ? get_ecount(g[g_hover]) : g_count)-1); break; case KEY_F(3): //jump to random group/entry - trav_col(rand() % (true_hover ? e_count : g_count)); + trav_col(rand() % (true_hover ? get_ecount(g[g_hover]) : g_count)); break; case KEY_F(5): @@ -333,13 +336,16 @@ void fill_col(int mode){ int i; WINDOW *col = (mode ? entry_win : group_win); - int count = (mode ? e_count : g_count); + int count = (mode ? get_ecount(g[g_hover]) : g_count); int offset = (mode ? e_offset[g_hover] : g_offset); int max_len = getmaxx(col)-2; //longest possible string length that can be displayed in the window int ycoord = 1; int max_y = HEIGHT-(6+GAP_SIZE); char *name; + mvwprintw(col, 0, 0, "i: %d\n", offset); + if(offset < 0) offset = 0; + for(i = 0+offset; i < count; i++){ if(ycoord >= max_y) break; //reached the bottom of the terminal window, stop drawing name = (mode ? get_ename(e[i]) : get_gname(g[i])); @@ -421,8 +427,7 @@ void update_col(int mode, int y_hl, bool resize){ break; case 1: - e_count = get_ecount(g[g_hover]); - e = get_entries(get_ghead(g[g_hover]), e_count); + e = get_gentries(g[g_hover]); fill_col(1); if(!resize) mvwchgat(entry_win, y_hl, 1, getmaxx(entry_win)-2, A_DIM, 1, NULL); else mvwchgat(entry_win, 1+e_hover[g_hover]-e_offset[g_hover], 1, getmaxx(entry_win)-2, A_DIM, (true_hover ? 2 : 1), NULL); @@ -464,7 +469,7 @@ void switch_col(){ void trav_col(int new_i){ int *focus = (true_hover ? &(e_hover[g_hover]) : &g_hover); //make it easy to know which column we are looking at int *offset = (true_hover ? &(e_offset[g_hover]) : &g_offset); - int count = (true_hover ? e_count : g_count); + int count = (true_hover ? get_ecount(g[g_hover]) : g_count); int max_hl = HEIGHT-(3+GAP_SIZE); //for some reason, this works int min_hl = 5; int oob_flag = 0; //0 = none, 1 = bottom, 2 = top @@ -520,7 +525,7 @@ int locateChar(char input){ if(fold_case && input >= 97 && input <= 122) input -= 32; if(true_hover){ //hovering on entries - for(i = location+1; i < e_count; i++){ + for(i = location+1; i < get_ecount(g[g_hover]); i++){ first_char = get_ename(e[i])[0]; if(fold_case && first_char >= 97 && first_char <= 122) first_char -= 32; if(input == first_char){ @@ -548,7 +553,7 @@ char *get_launch(){ char *program = get_gprog(g[g_hover]); char *flags = get_gflags(g[g_hover]); char *path = get_epath(e[e_hover[g_hover]]); - bool quotes = get_gquotes(g[g_hover]); + bool quotes = false; char *full_command = malloc(sizeof(char) * BUF_LEN); full_command[0] = '\0'; diff --git a/src/entry.c b/src/entry.c index 6dc5699..dceb424 100644 --- a/src/entry.c +++ b/src/entry.c @@ -7,18 +7,15 @@ #include #include "include/entry.h" -#include "include/group.h" #include "include/read_cfg.h" typedef struct entry{ char name[BUF_LEN]; char path[BUF_LEN]; bool path_force; - bool hidden; - struct entry *next; } ENTRY; -ENTRY *create_entry(char *new_name, char *new_path, bool force){ +ENTRY *create_entry(const char *new_name, const char *new_path, const bool force){ ENTRY *new; new = malloc(sizeof(ENTRY)); @@ -26,85 +23,15 @@ ENTRY *create_entry(char *new_name, char *new_path, bool force){ strcpy(new->name, new_name); strcpy(new->path, new_path); new->path_force = force; - new->hidden = false; - new->next = NULL; return new; } -void entry_rm(ENTRY *e, ENTRY *prev){ - assert(e != NULL); - if(prev != NULL) prev->next = e->next; //maintain linked structure - free(e); -} - -void clear_entries(ENTRY *head){ - ENTRY *temp; - - while(head != NULL){ - temp = head; - head = head->next; - free(temp); - } - - return; -} - -//returns 0 if in the middle, 1 if new head, 2 if new tail, or 3 if both new head and tail -//TODO this is kind of a stupid way of handling things -int entry_add(ENTRY *head, ENTRY *tail, ENTRY *add){ - assert(add != NULL); - ENTRY *ahead; - - //Empty group (no need to sort) - if(head == NULL) return 3; - - //add is the new tail - if(!get_sort() || strcmp(tail->name, add->name) <= 0){ - tail->next = add; - return 2; - } - - //add is the new head - if(strcmp(add->name, head->name) <= 0){ - add->next = head; - return 1; - } - - ahead = head->next; - - while(ahead != NULL){ - if(strcmp(head->name, add->name) <= 0 && strcmp(add->name, ahead->name) <= 0) break; - head = head->next; - ahead = ahead->next; - } - - head->next = add; - add->next = ahead; - - return 0; -} - -ENTRY **get_entries(ENTRY *head, int count){ - ENTRY **arr = malloc(sizeof(ENTRY *) * count); - ENTRY *trav = head; - int i = 0; - - while(i < count){ - if(!trav->hidden){ - arr[i] = trav; - i++; - } - trav = trav->next; - } - - return arr; -} - char *get_ename(ENTRY *e){ assert(e != NULL); return e->name; } + char *get_epath(ENTRY *e){ assert(e != NULL); return e->path; @@ -115,11 +42,7 @@ bool get_eforce(ENTRY *e){ return e->path_force; } -void set_hide(ENTRY *e, bool status){ - assert(e != NULL); - e->hidden = true; -} - +/* void entry_debug(ENTRY *trav){ while(trav != NULL){ @@ -129,3 +52,4 @@ void entry_debug(ENTRY *trav){ return; } +*/ diff --git a/src/group.c b/src/group.c index 0c0d5a0..edf4d7c 100644 --- a/src/group.c +++ b/src/group.c @@ -13,166 +13,27 @@ typedef struct group{ char name[BUF_LEN]; char program[BUF_LEN]; char flags[BUF_LEN]; - struct entry *head; - struct entry *tail; - struct group *next; + struct entry **entries; int entry_count; - bool launcher_quotes; //set by a group option whether or not the launcher should be wrapped by quotes + //bool launcher_quotes; //set by a group option whether or not the launcher should be wrapped by quotes } GROUP; -GROUP *groups_head; -GROUP *gp; //pointer to remember last group that was looked at int group_count = 0; int total_count = 0; -GROUP *create_group(char *new_name){ +GROUP *create_group(const char *new_name, const int entry_count){ GROUP *new = malloc(sizeof(GROUP)); strcpy(new->name, new_name); //by default, group name is equivalent to the path strcpy(new->program, "./"); //by default, launch an entry by executing it new->flags[0] = '\0'; //by default, no command line flags - new->head = NULL; - new->tail = NULL; - new->next = NULL; - new->entry_count = 0; - new->launcher_quotes = true; + new->entries = malloc(sizeof(ENTRY *) * entry_count); + new->entry_count = entry_count; group_count++; return new; } -//add an entry to a group or add a new empty group -//FIXME maybe make this function part of a seperate file to handle a tree (AVL?) -//for now, simple linked list implementation -void group_add(char *gname, ENTRY *addme){ - int i; - GROUP *new; - GROUP *last = NULL; //last element in an existing group list (NULL to start) - - //only adding a new group - if(addme == NULL){ - gp = groups_head; - while(gp != NULL){ - if(!(strcmp(gp->name, gname))){ - printf("config error: %s is already a group!\n", gname); - return; - } - - last = gp; - gp = gp->next; - } - } - - //The previous group is not the same as the new group to add to - if(!(gp != NULL && (!(strcmp(gp->name, gname))))){ - gp = groups_head; - while(gp != NULL){ - //gname matches groups[i]'s name, add entry here - if(!(strcmp(gp->name, gname))) break; - - last = gp; - gp = gp->next; - } - } - - //was unable to find a matching existing group - //need to create new group to insert the entry into - if(gp == NULL){ - new = create_group(gname); - - //first group - if(last == NULL) groups_head = new; - - //add to the end of the groups - else last->next = new; - - gp = new; - } - - //add the entry to the list of entries in the group - if(addme != NULL){ - i = entry_add(gp->head, gp->tail, addme); - switch(i){ - case 1: - gp->head = addme; - break; - - case 2: - gp->tail = addme; - break; - - case 3: - gp->head = addme; - gp->tail = addme; - break; - - } - - gp->entry_count++; - total_count++; - } - - return; -} - -void group_rm(GROUP *g){ - - clear_entries(g->head); - - free(g); - group_count--; - return; -} - -void clean_groups(){ - GROUP *dummy_head; - GROUP *trav; - GROUP *hold; - - if(group_count == 0){ - printf("Error: no groups! "); - refer_to_doc(); - exit(0); - } - - else{ - dummy_head = create_group("dummy"); - dummy_head->next = groups_head; - trav = dummy_head; - - while(trav != NULL){ - //found empty group for removal - if(trav->next != NULL && trav->next->entry_count < 1){ - printf("Omitting empty group \"%s\"\n", trav->next->name); - hold = trav->next; - trav->next = trav->next->next; - group_rm(hold); - } - else trav = trav->next; - } - } - - //ensure groups->head is still correct - groups_head = dummy_head->next; - group_rm(dummy_head); - return; - -} - - -GROUP **get_groups(){ - GROUP **arr = malloc(sizeof(GROUP *) * group_count); - GROUP *trav = groups_head; - int i; - - for(i = 0; i < group_count; i++){ - arr[i] = trav; - trav = trav->next; - } - - return arr; -} - char *get_gname(GROUP *g){ assert(g != NULL); return g->name; @@ -199,9 +60,14 @@ void set_gflags(GROUP *g, char *p){ strcpy(g->flags, p); } -ENTRY *get_ghead(GROUP *g){ +ENTRY **get_gentries(GROUP *g) { + assert(g != NULL); + return g->entries; +} + +void set_gentry(GROUP *g, int entry_index, ENTRY *new_entry) { assert(g != NULL); - return g->head; + g->entries[entry_index] = new_entry; } int get_ecount(GROUP *g){ @@ -214,19 +80,7 @@ void set_ecount(GROUP *g, int new_count){ g->entry_count = new_count; } -void set_gquotes(GROUP *g, bool b){ - assert(g != NULL); - g->launcher_quotes = b; -} - -bool get_gquotes(GROUP *g){ - return g->launcher_quotes; -} - -int get_gcount(){ - return group_count; -} - +/* void group_debug(){ GROUP *trav = groups_head; @@ -238,3 +92,4 @@ void group_debug(){ return; } +*/ diff --git a/src/include/entry.h b/src/include/entry.h index 51e43ca..a46dc46 100644 --- a/src/include/entry.h +++ b/src/include/entry.h @@ -1,17 +1,11 @@ +#include + #ifndef ENTRY_H #define ENTRY_H typedef struct entry ENTRY; -ENTRY *create_entry(char *new_name, char *new_path, bool force); - -void entry_rm(ENTRY *e, ENTRY *prev); - -void clear_entries(ENTRY *head); - -int entry_add(ENTRY *head, ENTRY *tail, ENTRY *add); - -ENTRY **get_entries(ENTRY *head, int count); +ENTRY *create_entry(const char *new_name, const char *new_path, const bool force); char *get_ename(ENTRY *e); @@ -19,10 +13,4 @@ char *get_epath(ENTRY *e); bool get_eforce(ENTRY *e); -void set_hide(ENTRY *e, bool status); - -bool get_hide(ENTRY *e); - -void entry_debug(ENTRY *trav); - #endif diff --git a/src/include/group.h b/src/include/group.h index ab6f409..dc74bcc 100644 --- a/src/include/group.h +++ b/src/include/group.h @@ -1,17 +1,13 @@ #ifndef GROUP_H #define GROUP_H -typedef struct group GROUP; - -GROUP *create_group(char *new_name); - -void group_add(char *gname, ENTRY *addme); +#include -void group_rm(GROUP *g); +#include "entry.h" -void clean_groups(); //remove empty groups from linked list +typedef struct group GROUP; -GROUP **get_groups(); +GROUP *create_group(const char *new_name, const int entry_count); char *get_gname(GROUP *g); @@ -23,18 +19,12 @@ char *get_gflags(GROUP *g); void set_gflags(GROUP *g, char *p); -ENTRY *get_ghead(GROUP *g); +ENTRY **get_gentries(GROUP *g); + +void set_gentry(GROUP *g, int entry_index, ENTRY *new_entry); int get_ecount(GROUP *g); void set_ecount(GROUP *g, int new_count); //for use in hiding entries -void set_gquotes(GROUP *g, bool b); - -bool get_gquotes(GROUP *g); - -int get_gcount(); - -void group_debug(); //debug function to output all groups - #endif diff --git a/src/include/read_cfg.h b/src/include/read_cfg.h index a76e55e..fc40cc0 100644 --- a/src/include/read_cfg.h +++ b/src/include/read_cfg.h @@ -3,23 +3,24 @@ #include +#include "group.h" + #define BUF_LEN 1024 -bool cfg_interp(char *path); +GROUP **cfg_interp(char *path, int *group_count); bool get_sort(); bool get_case_sensitivity(); void refer_to_doc(); -void addme(char *path, char *group, bool force, char *name); -int search_ch(char *str, char c); -int search_last_ch(char *str, char c); -int wild_cmp(char *wild, char *literal); -char *strip_quotes(char *str); -void error_mes(int ln, char *message); +//void addme(char *path, char *group, bool force, char *name); +//int search_ch(char *str, char c); +//int search_last_ch(char *str, char c); +//int wild_cmp(char *wild, char *literal); +//char *strip_quotes(char *str); +//void error_mes(int ln, char *message); //functions that differ by os extern char sep; char *find_config(); void mkconfig_wizard(char *path); -void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln); #endif diff --git a/src/read_cfg.c b/src/read_cfg.c index a931a11..b9ac8bc 100644 --- a/src/read_cfg.c +++ b/src/read_cfg.c @@ -22,9 +22,11 @@ #define OPTION_CNT 14 //private -void check_line(char *buffer, char **options, int ln); -int check_option(char *arg, char **options); -char *autoAlias(char *path); +//void check_line(char *buffer, char **options, int ln); +//int check_option(char *arg, char **options); +int key_count(lua_State *L, int table_stack_index); // counts the number of keys in a table +void add_groups(lua_State *L, int table_stack_index, GROUP ***g); +void add_entries(lua_State *L, int table_stack_index, GROUP *g); //turn on or off sorting (A-Z); On by default bool sort = true; @@ -36,21 +38,20 @@ bool hr = false; bool fold_case = true; //return false if invalid path -bool cfg_interp(char *path){ +GROUP **cfg_interp(char *path, int *group_count){ FILE *fp; char buffer[BUF_LEN]; GROUP **g; - ENTRY **e; - int count; - int e_count; - int i=0; + const char *group_name; + //ENTRY **e; + int i; int j; // check if file path exists fp = fopen(path, "r"); if(fp == NULL){ printf("Error: Invalid Configuration Path \"%s\"\n", path); - return false; + return NULL; } fclose(fp); @@ -61,7 +62,7 @@ bool cfg_interp(char *path){ if(config_load_status != 0) { printf("Error: could not load configuration \"%s\"\n", path); lua_error(L); - return false; + exit(1); } // set up base configuration variables @@ -75,95 +76,17 @@ bool cfg_interp(char *path){ i = lua_gettop(L); if(lua_type(L, i) != LUA_TTABLE) { printf("Error in config: 'Groups' should be Table, is actually %s\n", lua_typename(L, lua_type(L, i))); - return 1; - } - // add each group - const char *group_name; - lua_pushnil(L); - while(lua_next(L, i)) { - // the key is index -2, value is -1 - if(lua_type(L, -2) != LUA_TSTRING) continue; // skip if invalid key - group_name = lua_tostring(L, -2); - group_add(group_name, NULL); - // check for a launcher - if(lua_type(L, -1) != LUA_TTABLE) continue; // skip if invalid value - lua_pushstring(L, "Launcher"); - lua_gettable(L, -2); - if(lua_type(L, -1) == LUA_TSTRING) { - // FIXME the groups '''API''' I built might need some improvements in order for this to not be a headache - //set_gprog(GROUP *g, lua_tostring(L, -1)) - } - lua_pop(L, 1); - // add each entry - lua_pushstring(L, "Entries"); - lua_gettable(L, -2); - j = lua_gettop(L); - lua_pushnil(L); - while(lua_next(L, j)) { - if(lua_type(L, -1) != LUA_TSTRING) continue; // skip if invalid value - handle_fname(lua_tostring(L, -1), group_name, false, true, NULL, -1); - lua_pop(L, 1); - } - lua_pop(L, 2); + exit(1); } - //lua_getfield(L, -1, "Pictures"); - //lua_getfield(L, -1, "Entries"); - //lua_rawgeti(L, -1, 1); - //printf("message: %d\n", (int)lua_tonumber(L, -1)); - //lua_rawgeti(L, -2, 2); - //printf("message: %d\n", (int)lua_tonumber(L, -1)); - //lua_rawgeti(L, -3, 3); - //printf("message: %d\n", (int)lua_tonumber(L, -1)); + // create the group array + *group_count = key_count(L, i); + g = malloc(sizeof(GROUP *) * (*group_count)); + // add each group (which also adds each entry to each group) + add_groups(L, i, &g); lua_close(L); - return true; - - /* --- Old code --- */ - //build the options array - char **options = malloc(sizeof(char *) * OPTION_CNT); - options[0] = "add"; - options[1] = "addF"; - options[2] = "addGroup"; - options[3] = "addName"; - options[4] = "addNameF"; - options[5] = "addR"; - options[6] = "autoAlias"; - options[7] = "foldCase"; - options[8] = "hide"; - options[9] = "hideFile"; - options[10] = "setFlags"; - options[11] = "setLauncher"; - options[12] = "setLauncherRaw"; - options[13] = "sort"; - - //Read each line of "config" - while(fgets(buffer, BUF_LEN, fp)){ - printf("%d\n", i); - i++; - check_line(buffer, options, i); - } - - //cleanup - free(options); - - /* - //DEBUG: test to see if the list was added to properly - g = get_groups(); - count = get_gcount(); - for(i = 0; i < count; i++){ - printf("Looking at group %s\n", get_gname(g[i])); - e_count = get_ecount(g[i]); - e = get_entries(get_ghead(g[i]), e_count); - for(j = 0; j < e_count; j++){ - printf("\t%s\n", get_ename(e[j])); - } - } - //END DEBUG - */ - - fclose(fp); - return true; + return g; } bool get_sort(){ @@ -179,409 +102,71 @@ void refer_to_doc(){ return; } -void addme(char *path, char *group, bool force, char *name){ - ENTRY *new; - char auto_name[BUF_LEN]; - - //check if a name was given as argument - if(name != NULL){ - //strip quotes from the name - name = strip_quotes(name); - new = create_entry(name, path, force); - } - - //check if autoAlias is on. If it is, go to the autoAlias function - else if(hr){ - strcpy(auto_name, autoAlias(path)); - new = create_entry(auto_name, path, force); - } - - else new = create_entry(path, path, force); - if(new != NULL) group_add(group, new); - - return; -} - -int search_ch(char *str, char c){ - int i = 0; - - while(str[i] != '\0'){ - if(str[i] == c) return i; - i++; - } - - return -1; -} - -int search_last_ch(char *str, char c){ - int i = 0; - int last_i = -1; +int key_count(lua_State *L, int table_stack_index) { + int output = 0; - while(str[i] != '\0'){ - if(str[i] == c) last_i = i; - i++; + lua_pushnil(L); + while(lua_next(L, table_stack_index)) { + // uses 'key' (at index -2) and 'value' (at index -1) + if(lua_type(L, -2) == LUA_TSTRING) ++output; + lua_pop(L, 1); } - return last_i; + return output; } -//return 0 if match, 1 if not -//TODO only supports one wildcard per entry -int wild_cmp(char *wild, char *literal){ +void add_groups(lua_State *L, int table_stack_index, GROUP ***g) { + const char *group_name; + int entry_table_stack_index; int i; - - while(*wild != '\0'){ - //traverse until wildcard - if(*wild != '*'){ - if(*wild != *literal) return 1; - wild++; - literal++; - } - - //found wildcard, find the end of both names and comapre from the back - else{ - i = 0; - wild++; - while(*wild != '\0'){ - i++; - wild++; - } - while(*literal != '\0'){ - literal++; - } - - while(i > 0){ - wild--; - literal--; - if(*wild != *literal) return 1; - i--; - } - - return 0; - } - } - - return 0; -} - - -char *strip_quotes(char *str){ - char *stripped_str = malloc(sizeof(char) * BUF_LEN); - - if(str[0] == '"'){ - stripped_str = &str[1]; - stripped_str[strlen(stripped_str) - 1] = '\0'; - return stripped_str; - } - - return str; -} - -void error_mes(int ln, char *message){ - - assert(message != NULL); - - printf("Configuration File Error:\nOn line %d: %s\n\n", ln, message); - - return; -} - -//TODO add support for "addR" recursive adding (still needs work...) -//TODO add support for "alias" option -//TODO add support for "hide" option -void check_line(char *buffer, char **options, int ln){ - char *delims = " \t\n"; - char *tok = strtok(buffer, delims); - char args[MAX_ARGS][BUF_LEN]; - GROUP **g; - ENTRY **e; - char *tok_p; - char *arg_p; - int g_count; - int e_count; - int search_res; - int i, j; - char *error_p; //helper for complex error messages - - //ensure line is not blank or commented out - if(tok != NULL && tok[0] != '#' && tok[0] != '\0'){ - //initialize args to 0 - for(i = 0; i < MAX_ARGS; i++){ - args[i][0] = '\0'; - } - - i = 0; - //record all arguments in the line - while(tok != NULL){ - if(i >= MAX_ARGS){ - error_mes(ln, "Too many arguments"); - return; - } - strcpy(args[i], tok); - //handle if an argument has spaces and is wrapped in quotes - if(tok[0] == '"'){ - arg_p = &args[i][0]; - tok_p = &tok[1]; - - while(*tok_p != '"'){ - switch(*tok_p){ - - - case '\0': - tok = strtok(NULL, delims); - tok_p = &tok[0]; - *arg_p = ' '; - arg_p++; - break; - case '\\': - tok_p++; - - default: - *arg_p = *tok_p; - tok_p++; - arg_p++; - - } + lua_pushnil(L); + i = 0; + while(lua_next(L, table_stack_index)) { + // uses 'key' (at index -2) and 'value' (at index -1) + // looking at Groups.TABLE_NAME + if(lua_type(L, -2) == LUA_TSTRING && lua_type(L, -1) == LUA_TTABLE) { + group_name = lua_tostring(L, -2); + if(group_name != NULL) { + // push the Entries table on the stack (to get entry information) + lua_pushstring(L, "Entries"); + // get table Groups.TABLE_NAME.Entries + lua_gettable(L, -2); + entry_table_stack_index = lua_gettop(L); + // check that 'Entries' is a table + if(lua_type(L, entry_table_stack_index) != LUA_TTABLE) { + printf("Error in config: 'Entries' should be Table, is actually %s\n", lua_typename(L, lua_type(L, entry_table_stack_index))); + exit(1); } - - *arg_p = '\0'; - + (*g)[i] = create_group(group_name, key_count(L, entry_table_stack_index)); + // add entries to this group + add_entries(L, entry_table_stack_index, (*g)[i]); } - - tok = strtok(NULL, delims); - i++; - } - - //optimally check which option was specified - search_res = check_option(args[0], options); - - switch(search_res){ - - case 0: //add - //add entry(ies) to a group: first arg is the file(s), second arg is the group to add to - //TODO add sorting functionality - handle_fname(args[1], args[2], 0, 0, NULL, ln); - break; - - case 1: //addF - //force add entry to a group: first arg is the file(s), second arg is the group to add to - handle_fname(args[1], args[2], 0, 1, NULL, ln); - break; - - case 2: //addGroup - //create a new group - group_add(strip_quotes(args[1]), NULL); - break; - - case 3: //addName - //add entry to a group: first arg is the name, second arg is the file, and third arg is the group to add to - handle_fname(args[2], args[3], 0, 0, args[1], ln); - break; - - case 4: //addNameF - //same as addName, but with force on - handle_fname(args[2], args[3], 0, 1, args[1], ln); - break; - - case 5: //addR - //recursively add: that is, also search directories in the given path - //NOTE: experimental - handle_fname(args[1], args[2], 1, 0, NULL, ln); - break; - - case 6: //autoAlias - if(!(strcmp(args[1], "on"))) hr = true; - else if(!(strcmp(args[1], "off"))) hr = false; - break; - - case 7: //foldCase (case insensitive) - if(!(strcmp(args[1], "on"))) fold_case = true; - else if(!(strcmp(args[1], "off"))) fold_case = false; - break; - - //TODO consider having this call handle_fname instead so that '*' can be used - case 8: //hide - case 9: //hideFile - //args[2] is referring to a group - g = get_groups(); - g_count = get_gcount(); - - //look for matching existing group - for(i = 0; i < g_count; i++){ - if(!(strcmp(get_gname(g[i]), args[2]))) break; - } - - if(i < g_count){ - e_count = get_ecount(g[i]); - e = get_entries(get_ghead(g[i]), e_count); - - for(j = 0; j < e_count; j++){ - if(!strcmp((search_res == 8 ? get_ename(e[j]) : get_epath(e[j])), strip_quotes(args[1]))) break; - } - - if(j < e_count){ - set_hide(e[j], true); - set_ecount(g[i], get_ecount(g[i])-1); - } - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "Entry \"%s\" does not exist", args[1]); - error_mes(ln, error_p); - free(error_p); - } - } - - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "Group \"%s\" does not exist", args[2]); - error_mes(ln, error_p); - free(error_p); - } - break; - - case 10: //setFlags - //args[1] is referring to a group - g = get_groups(); - g_count = get_gcount(); - - //look for matching existing group - for(i = 0; i < g_count; i++){ - if(!(strcmp(get_gname(g[i]), args[1]))) break; - } - - //set a group's launcher flags (like ./program -f file for fullscreen) - //assert that a matching group was found - if(i < g_count) set_gflags(g[i], strip_quotes(args[2])); - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "Group \"%s\" does not exist", args[1]); - error_mes(ln, error_p); - free(error_p); - } - break; - - case 11: //setLauncher - case 12: //setLauncherRaw - //args[1] is referring to a group - g = get_groups(); - g_count = get_gcount(); - - //look for matching existing group - for(i = 0; i < g_count; i++){ - if(!(strcmp(get_gname(g[i]), args[1]))) break; - } - - //set a group's launcher (this requires pulling down the existing groups and finding the one that args[1] mentions) - //assert that a matching group was found - if(i < g_count){ - set_gprog(g[i], strip_quotes(args[2])); - if(search_res == 12) set_gquotes(g[i], false); //FIXME don't forget to change this line if adding more options!!! - } - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "Group \"%s\" does not exist", args[1]); - error_mes(ln, error_p); - free(error_p); - } - break; - - case 13: //sort - if(!(strcmp(args[1], "on"))) sort = true; - else if(!(strcmp(args[1], "off"))) sort = false; - break; - - default: - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "Unknown config option \"%s\"", args[0]); - error_mes(ln, error_p); - free(error_p); - } - - } - - return; -} - -int check_option(char *arg, char **options){ - int min = 0; - int max = OPTION_CNT-1; - int hover; - int comp_res; - - while(max - min > 1){ - hover = min + (max-min)/2; - comp_res = strcmp(arg, options[hover]); - - if(comp_res > 0) min = hover; - else if(comp_res < 0) max = hover; - else return hover; + lua_pop(L, 1); + ++i; } - - if(max == OPTION_CNT-1 && strcmp(arg, options[max]) == 0) return max; - else if(min == 0 && strcmp(arg, options[min]) == 0) return min; - - return -1; } +void add_entries(lua_State *L, int table_stack_index, GROUP *g) { + const char *entry_name; + int i; -char *autoAlias(char *path){ - char *hr_name = malloc(sizeof(char) * BUF_LEN); - char *p = hr_name; - char *rpath; //necessary so as not to touch the actual path - char *last_dot = NULL; //used to trim the file extension (if there is one) - bool stop = false; //stop when you don't want to add a series of chars to the output - - //get to the relative path name - rpath = strrchr(path, sep); - if(rpath == NULL) rpath = path; - else rpath++; - - while(*rpath != '\0'){ - switch(*rpath){ - case '(': - stop = true; - break; - - case ')': - stop = false; - break; - - case '-': - case '_': - if(*(p-1) != ' ' && !stop){ - *p = ' '; - *p++; - } - break; - - case ' ': - if(*(p-1) != ' ' && !stop){ - *p = *rpath; - *p++; - } - break; - - case '.': - last_dot = p; - - default: - if(!stop){ - *p = *rpath; - *p++; - } + lua_pushnil(L); + i = 0; + while(lua_next(L, table_stack_index)) { + // uses 'key' (at index -2) and 'value' (at index -1) + // looking at Groups.TABLE_NAME.Entries[i] + if(lua_type(L, -1) == LUA_TSTRING) { + entry_name = lua_tostring(L, -1); + if(entry_name != NULL) { + set_gentry(g, i, create_entry(entry_name, entry_name, true)); + } } - *rpath++; + lua_pop(L, 1); + ++i; } - - //close the name - if(last_dot != NULL) *last_dot = '\0'; - else if(*path == '"') *(p-1) = '\0'; //close early to avoid including closing quote - else *p = '\0'; - return hr_name; + // one last pop to pop the key off + lua_pop(L, 1); } - - - diff --git a/src/unix/read_cfg.c b/src/unix/read_cfg.c index 4e58863..6f75ddc 100644 --- a/src/unix/read_cfg.c +++ b/src/unix/read_cfg.c @@ -137,97 +137,3 @@ void mkconfig_wizard(char *path){ return; } - -//TODO augment to involve recurs -//TODO could use some cleanup... -void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln){ - ENTRY *new; - char *search; //pointer for traversing path - char full_path_cpy[BUF_LEN]; - char relative_path_cpy[BUF_LEN]; - char arg_cpy[BUF_LEN]; - char auto_name[BUF_LEN]; - int plen = strlen(path); - char *dirname; - char *local_arg; //for use in addR - DIR *dp; - struct dirent *fname; - int i; - char *error_p; //helper for complex error messages - - assert(path != NULL && group != NULL); - - if(path[0] == '\0' || group[0] == '\0'){ - error_mes(ln, "Too few arguments for \"add\""); - return; - } - - //address potential quotes - strcpy(full_path_cpy, strip_quotes(path)); - - //don't check that the path arg is valid when forced - if(force) addme(full_path_cpy, group, force, name); - - //file is not recognized, perhaps it has a wildcard? - //TODO finish rewriting a more robust wildcard thingy - else if(access(full_path_cpy, F_OK) == -1){ - i = search_ch(full_path_cpy, '*'); - if(i > -1){ - //look for a directory - while(full_path_cpy[i] != sep && (i >= 0)){ - i--; - } - dirname = full_path_cpy; - strcpy(arg_cpy, full_path_cpy); - dirname[i+1] = '\0'; - dp = opendir(dirname); - - //the directory is real - if(dp != NULL){ - while((fname = readdir(dp))){ - relative_path_cpy[0] = '\0'; - strcat(relative_path_cpy, dirname); - strcat(relative_path_cpy, fname->d_name); - - //check if path is a file (and not a directory/symlink/etc.) and regex matches - if(fname->d_type == DT_REG && !(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name); - - //if the recursive option was specified and the path is a directory, run handle_fname on this directory, but for security reasons, do not consider directories that start with a '.' - else if(recurs && fname->d_type == DT_DIR && fname->d_name[0] != '.'){ - i = search_last_ch(arg_cpy, sep); - local_arg = &arg_cpy[i+1]; - strcat(relative_path_cpy, &sep); - strcat(relative_path_cpy, local_arg); - handle_fname(relative_path_cpy, group, 1, 0, NULL, ln); - } - - } - - closedir(dp); - } - - //directory is not real, report error to the user - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "\"%s\" bad path", dirname); - error_mes(ln, error_p); - free(error_p); - //printf("Error: \"%s\" bad path\n", dirname); - } - } - - //path is not real, report error to the user - else{ - error_p = malloc(sizeof(char) * 1024); - sprintf(error_p, "\"%s\" bad path", full_path_cpy); - error_mes(ln, error_p); - free(error_p); - } - } - - //file name is okay - //FIXME does not take into account whether the argument is a file (could be a directory, symlink, etc.) - else addme(full_path_cpy, group, force, name); - - return; -} -- cgit