summaryrefslogtreecommitdiff
path: root/read_cfg.c
blob: c97b62c7caa9b7289268f2953c8ce65dfd55f3b0 (plain)
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
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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

//public
void cfg_interp(char *path);
void check_line(char *buffer);
int get_compmode();

//private
void handle_fname(char *path, char *group, bool recurs, bool force, char *name);
char *autoAlias(char *path);
int search_ch(char *str, char c);
int wild_cmp(char *wild, char *literal);

//allow for compatability with compatability layers such as WSL
int compmode = 0;
//0 -> none
//1 -> WSL
//maybe more later?

//set to true to automatically try to create a human readable name for an entry
bool hr = false;

void cfg_interp(char *path){
	FILE *fp;
	char buffer[BUF_LEN];
	GROUP **g;
	ENTRY **e;
	int count;
	int e_count;
	int i;
	int j;

	//TODO have this check in certain locations for a config file, give error message if "config" does not exist
	fp = fopen(path, "r");
	assert(fp != NULL);

	//Read each line of "config"
	while(fgets(buffer, BUF_LEN, fp)){
		check_line(buffer);
	}

	/*
	//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;
}

//TODO add support for "addR" recursive adding
//TODO add support for "addName" option for adding and aliasing on the same line
//TODO add support for "alias" option
//TODO add support for "hide" option
void check_line(char *buffer){
	char *delims = " \t\n";
	char *tok = strtok(buffer, delims);
	char args[MAX_ARGS][BUF_LEN];
	GROUP **g;
	int g_count;
	int i;

	//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){
				printf("Error: too many arguments\n");
				return;
			}
			strcpy(args[i], tok);
			//handle if an argument has spaces and is wrapped in quotes
			if(tok[0] == '"'){
				while(tok[strlen(tok)-1] != '"'){
					tok = strtok(NULL, delims);
					strcat(args[i], " ");
					strcat(args[i], tok);
				}
			}

			tok = strtok(NULL, delims);
			i++;
		}

		if(!(strcmp(args[0], "autoAlias"))){
			if(!(strcmp(args[1], "on"))) hr = true;
			else if(!(strcmp(args[1], "off"))) hr = false;
		}

		//add entry(ies) to a group: first arg is the file(s), second arg is the group to add to
		//TODO add potential dash functions
		//TODO add sorting functionality
		else if(!(strcmp(args[0], "add"))) handle_fname(args[1], args[2], 0, 0, NULL);
		//force add entry to a group: first arg is the file(s), second arg is the group to add to
		else if(!(strcmp(args[0], "addF"))) handle_fname(args[1], args[2], 0, 1, NULL);
		//add entry to a group: first arg is the name, second arg is the file, and third arg is the group to add to
		else if(!(strcmp(args[0], "addName"))) handle_fname(args[2], args[3], 0, 0, args[1]);
		//same as addName, but with force on
		else if(!(strcmp(args[0], "addNameF"))) handle_fname(args[2], args[3], 0, 1, args[1]);

		//create a new group
		else if(!(strcmp(args[0], "addGroup"))) group_add(args[1], NULL);

		//set compatability mode
		else if(!(strcmp(args[0], "compMode"))){
			if(!(strcmp(args[1], "WSL"))) compmode = 1;
			else printf("Error: Unknown Compatability Mode Argument \"%s\"\n", args[1]);
		}

		else{
			//remaining possibilities involve args[1] being a char* 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)
			if(!(strcmp(args[0], "setLauncher"))){
				//assert that a matching group was found
				if(i < g_count) set_gprog(g[i], args[2]);
				else printf("Error: Group \"%s\" does not exist\n", args[1]);
			}

			//set a group's launcher flags (like ./program -f file for fullscreen)
			else if(!(strcmp(args[0], "setFlags"))){
				//assert that a matching group was found
				if(i < g_count) set_gflags(g[i], args[2]);
				else printf("Error: Group \"%s\" does not exist\n", args[1]);
			}

			//args[0] is not a valid config option
			else printf("Error: Unknown config option \"%s\"\n", args[0]);
		}
	}

	return;
}

int get_compmode(){
	return compmode;
}

//TODO augment to involve recurs
//TODO could use some cleanup...
void handle_fname(char *path, char *group, bool recurs, bool force, char *name){
	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;

	assert(path != NULL && group != NULL);

	if(path[0] == '\0' || group[0] == '\0'){
		printf("Error: too few arguments for \"add\"\n");
		return;
	}

	//address potential quotes
	if(path[0] == '"'){
		strcpy(full_path_cpy, &path[1]);
		full_path_cpy[strlen(full_path_cpy) - 1] = '\0';
	}
	else strcpy(full_path_cpy, path);

	//don't check that the path arg is valid
	if(force){
		if(name != NULL) new = create_entry(name, full_path_cpy, force);
		else if(hr){
			strcpy(auto_name, autoAlias(full_path_cpy));
			new = create_entry(auto_name, full_path_cpy, force);
		}
		else new = create_entry(full_path_cpy, full_path_cpy, force);
		if(new != NULL) group_add(group, new);
	}


	//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] != '/' && (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))){

						//check if a name was given as argument
						if(name != NULL) new = create_entry(name, relative_path_cpy, force);
						//check if autoAlias is on. If it is, go to the autoAlias function
						else if(hr){
							strcpy(auto_name, autoAlias(relative_path_cpy));
							new = create_entry(auto_name, relative_path_cpy, force);
						}
						else new = create_entry(relative_path_cpy, relative_path_cpy, force);
						if(new != NULL) group_add(group, new);
					}
				}

				closedir(dp);
			}

			//directory is not real, report error to the user
			else printf("Error: \"%s\" bad path\n", dirname);
		}

		//path is not real, report error to the user
		else printf("Error: \"%s\" bad path\n", dirname);
	}

	//file name is okay
	//FIXME does not take into account whether the argument is a file (could be a directory, symlink, etc.)
	else{
		if(name != NULL) new = create_entry(name, full_path_cpy, force);
		else if(hr){
			strcpy(auto_name, autoAlias(full_path_cpy));
			new = create_entry(auto_name, full_path_cpy, force);
		}
		else new = create_entry(full_path_cpy, full_path_cpy, force);
		if(new != NULL){
			group_add(group, new);
		}
	}

	return;
}

//TODO figure out how to trim the extensions of files off
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
	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, '/');
	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;

			default:
				if(!stop){
					*p = *rpath;
					*p++;
				}
		}
		*rpath++;
	}
	
	//close the name
	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;
}