1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
|
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "entry.h"
#include "group.h"
#define BUF_LEN 1024 //maybe move this line to the header file
#define MAX_ARGS 5
#define OPTION_CNT 14
//public
#if defined _WIN32 || defined _WIN64
char *find_config_win();
#else
char *find_config();
#endif
void cfg_interp(char *path);
int get_compmode();
bool get_sort();
bool get_case_sensitivity();
void refer_to_doc();
//private
void mkconfig_wizard(char *path);
void check_line(char *buffer, char **options, int ln);
int check_option(char *arg, char **options);
void handle_fname(char *path, char *group, bool recurs, bool force, char *name, int ln);
void addme(char *path, char *group, bool force, char *name);
char *autoAlias(char *path);
int search_ch(char *str, char c);
int wild_cmp(char *wild, char *literal);
char *strip_quotes(char *str);
void error_mes(int ln, char *message);
//turn on or off sorting (A-Z); On by default
bool sort = true;
//set to true to automatically try to create a human readable name for an entry
bool hr = false;
//turn foldCase (insensitive case searching) on or off; On by default
bool fold_case = true;
#if defined _WIN32 || defined _WIN64
//for Windows Compatability, this will be '\\' (otherwise '/')
char sep = '\\';
#else
char sep = '/';
#endif
#if defined _WIN32 || defined _WIN64
char *find_config_win(){
char *appdata = getenv("APPDATA");
char *path = malloc(sizeof(char) * BUF_LEN);
int check_count = 1;
char choices[check_count][BUF_LEN];
int i;
sprintf(choices[0], "%s%ctml%cconfig", appdata, sep, sep);
for(i = 0; i < check_count; i++){
path = choices[i];
printf("Checking for config at %s: ", choices[i]);
if(access(path, R_OK) == 0){
printf("Using config \"%s\"\n\n", path);
return path;
}
else printf("File does not exist\n");
}
//TODO no config exists, ask user if they want to autogenerate one
mkconfig_wizard(choices[0]);
path = choices[0];
return path;
}
#else
char *find_config(){
char *home = getenv("HOME");
char *path = malloc(sizeof(char) * BUF_LEN);
char choices[2][BUF_LEN];
int check_count = 2;
int i;
sprintf(choices[0], "%s%c.config%ctml%cconfig", home, sep, sep, sep);
sprintf(choices[1], "%s%c.tml%cconfig", home, sep, sep);
for(i = 0; i < check_count; i++){
path = choices[i];
printf("Checking for config at %s: ", choices[i]);
if(access(path, R_OK) == 0){
printf("Using config \"%s\"\n\n", path);
return path;
}
else printf("File does not exist\n");
}
//TODO no config exists, ask user if they want to autogenerate one
mkconfig_wizard(choices[0]);
path = choices[0];
return path;
}
#endif
void cfg_interp(char *path){
FILE *fp;
char buffer[BUF_LEN];
GROUP **g;
ENTRY **e;
int count;
int e_count;
int i=0;
int j;
fp = fopen(path, "r");
assert(fp != NULL);
//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)){
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;
}
bool get_sort(){
return sort;
}
bool get_case_sensitivity(){
return fold_case;
}
void refer_to_doc(){
printf("Refer to documentation on how to create tml config file\n");
return;
}
void mkconfig_wizard(char *path){
char input;
FILE *fp;
#if defined _WIN32 || defined _WIN64
char *home = getenv("USERPROFILE");
char *appdata = getenv("APPDATA");
#else
char *home = getenv("HOME");
#endif
printf("\nNo configuration file found. Auto-generate one now at \"%s\"? [Y/n] ", path);
fflush(stdout);
scanf(" %c", &input);
if(input == 'n'){
printf("Configuration will not be auto-generated\n");
refer_to_doc();
exit(0);
}
printf("Generating configuration file at \"%s\"...\n", path);
//ensure directories have been created
#if defined _WIN32 || defined _WIN64
if(appdata == NULL){
printf("Failed: \%APPDATA\% is NULL\n");
exit(1);
}
if(home == NULL){
printf("Failed: \%USERPROFILE\% is NULL\n");
exit(1);
}
sprintf(path, "%s%ctml%c", appdata, sep, sep);
mkdir(path);
sprintf(path, "%s%ctml%cconfig", appdata, sep, sep);
#else
if(home == NULL){
printf("Failed: HOME is NULL\n");
exit(1);
}
sprintf(path, "%s%c.config%c", home, sep, sep);
mkdir(path, 0755);
sprintf(path, "%s%c.config%ctml%c", home, sep, sep, sep);
mkdir(path, 0755);
sprintf(path, "%s%c.config%ctml%cconfig", home, sep, sep, sep);
#endif
//open file for writing, make sure non-NULL
fp = fopen(path, "w");
if(fp == NULL){
printf("Failed: \"%s\" could not be open for writing\n", path);
exit(1);
}
//write to file (in Windows, executing a file will open it in its default application)
#if defined _WIN32 || defined _WIN64
fprintf(fp, "# This file was auto-generated by tml. See docs/tml-config.md or tml-config(5) for documentation\n"
"# By default, no launcher is specified for any group. When no launcher is specified on the Windows build of tml, media files will be opened with their default application.\n"
"# It is generally recommended to specify a launcher for groups containing media files using the \"setLauncher\" command\n\n"
"# Recursively add files from %s%cMusic%c to Music group\n"
"addGroup Music\n"
"addR %s%cMusic%c* Music\n\n"
"# Recursively add files from %s%cPictures%c to Pictures group\n"
"addGroup Pictures\n"
"addR %s%cPictures%c* Pictures\n\n"
"# Recursively add files from %s%cVideos%c to Videos group\n"
"addGroup Videos\n"
"addR %s%cVideos%c* Videos", home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep);
#else
fprintf(fp, "# This file was auto-generated by tml. See docs/tml-config.md or tml-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"
"addR %s%cMusic%c* Music\n\n"
"# Recursively add files from %s%cPictures%c to Pictures group\n"
"addGroup Pictures\n"
"setLauncher Pictures xdg-open\n"
"addR %s%cPictures%c* Pictures\n\n"
"# Recursively add files from %s%cVideos%c to Videos group\n"
"addGroup Videos\n"
"setLauncher Videos xdg-open\n"
"addR %s%cVideos%c* Videos", home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep, home, sep, sep);
#endif
fclose(fp);
printf("done\nIt is highly recommended to further tweak the configuration file! [press any key to continue]");
fflush(stdout);
getchar();
getchar();
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++;
}
}
*arg_p = '\0';
}
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;
}
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;
}
//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;
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);
#if defined _WIN32 || defined _WIN64
//Windows cannot tell file types (TODO), so just add relatively indiscriminantly
if(!(wild_cmp(&arg_cpy[i+1], fname->d_name))) addme(relative_path_cpy, group, force, name);
#else
//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 '.'
//FIXME this may still need some work...
else if(recurs && fname->d_type == DT_DIR && fname->d_name[0] != '.'){
strcat(relative_path_cpy, "/*");
handle_fname(relative_path_cpy, group, 1, 0, NULL, ln);
}
#endif
}
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;
}
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;
}
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++;
}
}
*rpath++;
}
//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;
}
int search_ch(char *str, char c){
int i = 0;
while(str[i] != '\0'){
if(str[i] == c) return i;
i++;
}
return -1;
}
//return 0 if match, 1 if not
//TODO only supports one wildcard per entry
int wild_cmp(char *wild, char *literal){
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;
}
|