summaryrefslogtreecommitdiff
path: root/src/unix/read_cfg.c
diff options
context:
space:
mode:
authorLouie Shprung <lshprung@tutanota.com>2024-08-30 16:14:26 -0400
committerLouie Shprung <lshprung@tutanota.com>2024-08-30 16:14:26 -0400
commit615c64bcd69eea3660f0a35396255c2eb45fa283 (patch)
tree8e4d8da22240f1ab8da4ff097f74177fcbf24f50 /src/unix/read_cfg.c
parent9cd631a391568c3e75d6f9ee65707dea0bea6d9a (diff)
parenta3d9eb21789bf3587f176e6d155d610b8bc69dab (diff)
Merge branch 'testing'
Diffstat (limited to 'src/unix/read_cfg.c')
-rw-r--r--src/unix/read_cfg.c210
1 files changed, 63 insertions, 147 deletions
diff --git a/src/unix/read_cfg.c b/src/unix/read_cfg.c
index b40dd48..602c750 100644
--- a/src/unix/read_cfg.c
+++ b/src/unix/read_cfg.c
@@ -20,8 +20,8 @@ char *find_config(){
int check_count = 2;
int i;
- sprintf(choices[0], "%s%c.config%cterminal-media-launcher%cconfig", home, sep, sep, sep);
- sprintf(choices[1], "%s%c.terminal-media-launcher%cconfig", home, sep, sep);
+ sprintf(choices[0], "%s%c.config%cterminal-media-launcher%cconfig.lua", home, sep, sep, sep);
+ sprintf(choices[1], "%s%c.terminal-media-launcher%cconfig.lua", home, sep, sep);
for(i = 0; i < check_count; i++){
strcpy(path, choices[i]);
@@ -69,7 +69,7 @@ void mkconfig_wizard(char *path){
sprintf(path, "%s%c.config%cterminal-media-launcher%c", home, sep, sep, sep);
mkdir(path, 0755);
- sprintf(path, "%s%c.config%cterminal-media-launcher%cconfig", home, sep, sep, sep);
+ sprintf(path, "%s%c.config%cterminal-media-launcher%cconfig.lua", home, sep, sep, sep);
//open file for writing, make sure non-NULL
fp = fopen(path, "w");
@@ -78,56 +78,66 @@ void mkconfig_wizard(char *path){
exit(1);
}
- //write to file
- fprintf(fp, "# This file was auto-generated by terminal-media-launcher. See docs/terminal-media-launcher-config.md or terminal-media-launcher-config(5) for documentation\n"
- "# The default launcher is set to \"xdg-open\" which will open files based on the relevant default application set through xdg\n\n"
- "# Recursively add files from %s%cMusic%c to Music group\n"
- "addGroup Music\n"
- "setLauncher Music xdg-open\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.aac Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.aiff Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.alac Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.au Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.flac Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.m4a Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.mp3 Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.ogg Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.pcm Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.wav Music\n", home, sep, sep);
- fprintf(fp, "addR %s%cMusic%c*.wma Music\n\n", home, sep, sep);
- fprintf(fp, "# Recursively add files from %s%cPictures%c to Pictures group\n"
- "addGroup Pictures\n"
- "setLauncher Pictures xdg-open\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.epi Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.eps Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.eps2 Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.eps3 Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.epsf Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.epsi Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.ept Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.gif Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.gfa Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.giff Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.jpeg Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.jpg Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.png Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.svg Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.svgz Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.tif Pictures\n", home, sep, sep);
- fprintf(fp, "addR %s%cPictures%c*.tiff Pictures\n\n", home, sep, sep);
- fprintf(fp, "# Recursively add files from %s%cVideos%c to Videos group\n"
- "addGroup Videos\n"
- "setLauncher Videos xdg-open\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.asf Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.avi Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.flv Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.mk3d Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.mkv Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.mov Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.mp4 Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.qt Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.webm Videos\n", home, sep, sep);
- fprintf(fp, "addR %s%cVideos%c*.wmv Videos\n", home, sep, sep);
+ fprintf(fp,
+ "-- This file was auto-generated by terminal-media-launcher. See docs/terminal-media-launcher-config.md or terminal-media-launcher-config(5) for documentation\n"
+ "-- The default launcher is set to \"xdg-open\" which will open files based on the relevant default application set through xdg\n\n"
+ "local lfs = require \"lfs\"\n"
+ "\n"
+ "local function addGroup(name, launcher, flags)\n"
+ " assert(type(name) == \"string\")\n"
+ "\n"
+ " -- create Groups table if needed\n"
+ " if Groups == nil then\n"
+ " Groups = {}\n"
+ " end\n"
+ "\n"
+ " local new_group = {}\n"
+ " new_group.name = name\n"
+ " new_group.Entries = {}\n"
+ " if launcher ~= nil then\n"
+ " new_group.Launcher = launcher\n"
+ " end\n"
+ " if flags ~= nil then\n"
+ " new_group.Flags = flags\n"
+ " end\n"
+ "\n"
+ " table.insert(Groups, new_group)\n"
+ " return new_group\n"
+ "end\n"
+ "\n"
+ "local function addEntries(parentGroup, startDir, filePattern, recursive)\n"
+ " -- recursive arg is a boolean for whether or not to descend into subdirectories (false by default)\n"
+ " assert(type(parentGroup) == \"table\")\n"
+ " assert(type(parentGroup.Entries) == \"table\")\n"
+ " assert(type(startDir) == \"string\")\n"
+ " assert(type(filePattern) == \"string\")\n"
+ "\n"
+ " for file in lfs.dir(startDir) do\n"
+ " local fullFilePath = startDir .. \"/\" .. file\n"
+ " if file ~= \".\" and file ~= \"..\" then\n"
+ " -- descend into subdirectory if recursive is set to true\n"
+ " if lfs.attributes(fullFilePath).mode == \"directory\" and recursive == true then\n"
+ " addEntries(parentGroup, fullFilePath, filePattern, recursive)\n"
+ " elseif lfs.attributes(fullFilePath).mode == \"file\" then\n"
+ " if string.match(file, filePattern) then\n"
+ " table.insert(parentGroup.Entries, {\n"
+ " name = file,\n"
+ " path = fullFilePath\n"
+ " })\n"
+ " end\n"
+ " end\n"
+ " end\n"
+ " end\n"
+ "end\n"
+ "\n"
+ "local music = addGroup(\"Music\", \"xdg-open\")\n"
+ "addEntries(music, os.getenv(\"HOME\") .. \"/Music\", \".*\", true)\n"
+ "\n"
+ "local pictures = addGroup(\"Pictures\", \"xdg-open\")\n"
+ "addEntries(pictures, os.getenv(\"HOME\") .. \"/Pictures\", \".*\", true)\n"
+ "\n"
+ "local videos = addGroup(\"Videos\", \"xdg-open\")\n"
+ "addEntries(videos, os.getenv(\"HOME\") .. \"/Videos\", \".*\", true)\n");
fclose(fp);
printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]");
@@ -137,97 +147,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;
-}