summaryrefslogtreecommitdiff
path: root/src/read_cfg.c
blob: 6c334ac595ec7cd950b798baffd637256593a7af (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
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
#include <assert.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
#if defined _WIN32 || defined _WIN64
#include "windows/read_cfg.h"
#else
#include "unix/read_cfg.h"
#endif
*/

#include "include/entry.h"
#include "include/group.h"
#include "include/read_cfg.h"
#define MAX_ARGS 5
#define OPTION_CNT 14

//private
//void check_line(char *buffer, char **options, int ln);
//int check_option(char *arg, char **options);
void get_settings(lua_State *L); // gets settings from Settings global variable
int get_group_count(lua_State *L); // counts the number of valid groups
int get_entry_count(lua_State *L); // counts the number of valid entries for a group
void add_groups(lua_State *L, GROUP ***g);
void add_entries(lua_State *L, GROUP *g);
void stack_debug(lua_State *L);
void table_debug(lua_State *L, int print_depth);

//turn on or off sorting for entries (A-Z); On by default
// TODO allow specifying whether to sort groups or entries (or both, or none)
bool sort = true;

//return false if invalid path
GROUP **cfg_interp(char *path, int *group_count){
	FILE *fp;
	char buffer[BUF_LEN];
	GROUP **g;
	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 NULL;
	}
	fclose(fp);

	// load lua configuration
	lua_State *L = luaL_newstate();
	// new lua stack
	luaL_openlibs(L); // allow for standard library to be used
	int config_load_status = luaL_dofile(L, path);
	if(config_load_status != 0) {
		printf("Error: could not load configuration \"%s\"\n", path);
		lua_error(L);
		exit(1);
	}

	// set up base configuration variables
	// TODO set helper variables and functions (e.g., so that Groups table doesn't need to be manually created in the user's config)
	//lua_newtable(L);
	//lua_setglobal(L, "Groups");
	//lua_pcall(L, 0, 0, 0);

	// open Settings table
	lua_getglobal(L, "Settings");
	// stack now contains: -1 => Settings
	if(lua_type(L, -1) == LUA_TTABLE) {
		// parse settings
		get_settings(L);
	}
	lua_pop(L, 1);
	// empty stack

	// open Groups table
	lua_getglobal(L, "Groups");
	// stack now contains: -1 => Groups
	if(lua_type(L, -1) != LUA_TTABLE) {
		printf("Error in config: 'Groups' should be Table, is actually %s\n", lua_typename(L, lua_type(L, -1)));
		exit(1);
	}

	// create the group array
	*group_count = get_group_count(L);
	if(*group_count <= 0) {
		printf("Error: No Groups!\n");
		g = NULL;
	}
	else {
		g = malloc(sizeof(GROUP *) * (*group_count));

		// add each group (which also adds each entry to each group)
		add_groups(L, &g);
	}
	lua_close(L);
	return g;
}

void refer_to_doc(){
	printf("Refer to documentation on how to create terminal-media-launcher config file\n");
	return;
}

void get_settings(lua_State *L) {
	// stack now contains: -1 => table

	bool *setting_vars[] = {
		&sort
	};

	char *setting_strings[] = {
		"sort"
	};

	int count = 1;
	int i;

	// looking at table Settings

	// check if autoAlias is set
	for(i = 0; i < count; ++i) {
		lua_pushstring(L, setting_strings[i]);
		// stack now contains: -1 => setting_string; -2 => table
		lua_gettable(L, -2);
		// stack now contains: -1 => string_value; -2 => table
		if(lua_type(L, -1) == LUA_TBOOLEAN) {
			*(setting_vars[i]) = lua_toboolean(L, -1);
		}
		lua_pop(L, 1);
		// stack now contains: -1 => table
	}
}

int get_group_count(lua_State *L) {
	int output = 0;

	// stack now contains: -1 => table
	lua_pushnil(L);
	// stack now contains: -1 => nil; -2 => table
	while (lua_next(L, -2)) {
		// stack now contains: -1 => value; -2 => key; -3 => table
		// copy the key so that lua_tostring does not modify the original
		lua_pushvalue(L, -2);
		// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		if(lua_type(L, -1) == LUA_TNUMBER && lua_type(L, -2) == LUA_TTABLE) {
			lua_pushstring(L, "name");
			// stack now contains: -1 => "name"; -2 => key; -3 => value; -4 => key; -5 => table
			lua_gettable(L, -3);
			// stack now contains: -1 => name; -2 => key; -3 => value; -4 => key; -5 => table
			if(lua_type(L, -1) == LUA_TSTRING) {
				// check that the Entries table for this group is not empty
				lua_pushstring(L, "Entries");
				// stack now contains: -1 => "Entries"; -2 => name; -3 => key; -4 => value; -5 => key; -6 => table
				lua_gettable(L, -4);
				// stack now contains: -1 => Entries; -2 => name; -3 => key; -4 => value; -5 => key; -6 => table
				if(lua_type(L, -1) == LUA_TTABLE && get_entry_count(L) > 0)
					++output;
				lua_pop(L, 1);
				// stack now contains: -1 => name; -2 => key; -3 => value; -4 => key; -5 => table
			}
			lua_pop(L, 1);
			// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		}
		lua_pop(L, 2);
		// stack now contains: -1 => key; -2 => table
	}
	// stack now contains: -1 => table (when lua_next returns 0 it pops the key
	// but does not push anything.)

	return output;
}

int get_entry_count(lua_State *L) {
	int output = 0;

	// stack now contains: -1 => table
	lua_pushnil(L);
	// stack now contains: -1 => nil; -2 => table
	while (lua_next(L, -2)) {
		// stack now contains: -1 => value; -2 => key; -3 => table
		// copy the key so that lua_tostring does not modify the original
		lua_pushvalue(L, -2);
		// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		if(lua_type(L, -1) == LUA_TNUMBER && lua_type(L, -2) == LUA_TTABLE) {
			// check that the entry has a valid name
			lua_pushstring(L, "name");
			// stack now contains: -1 => "name"; -2 => key; -3 => value; -4 => key; -5 => table
			lua_gettable(L, -3);
			// stack now contains: -1 => name; -2 => key; -3 => value; -4 => key; -5 => table
			if(lua_type(L, -1) == LUA_TSTRING) ++output;
			lua_pop(L, 1);
			// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		}
		lua_pop(L, 2);
		// stack now contains: -1 => key; -2 => table
	}
	// stack now contains: -1 => table (when lua_next returns 0 it pops the key
	// but does not push anything.)

	return output;
}

void add_groups(lua_State *L, GROUP ***g) {
	const char *group_name;
	int group_table_stack_index; // index of Groups.TABLE_NAME
	int entry_table_stack_index; // index of Groups.TABLE_NAME.Entries
	int entry_count;
	char sort_cmd_lua[BUF_LEN]; // used to store the lua call to table.sort
	int i = 1; // index in lua table
	int count = 0; // index in C struct

	// stack now contains: -1 => table
	lua_pushnil(L);
	// stack now contains: -1 => nil; -2 => table
	while (lua_next(L, -2)) {
		// stack now contains: -1 => value; -2 => key; -3 => table
		// copy the key so that lua_tostring does not modify the original
		lua_pushvalue(L, -2);
		// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		if(lua_type(L, -1) == LUA_TNUMBER && lua_type(L, -2) == LUA_TTABLE) {
			lua_pushstring(L, "name");
			// stack now contains: -1 => "name"; -2 => key; -3 => value; -4 => key; -5 => table
			lua_gettable(L, -3);
			// stack now contains: -1 => name; -2 => key; -3 => value; -4 => key; -5 => table
			group_name = lua_tostring(L, -1);
			lua_pop(L, 1);
			// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
			if(group_name != NULL) {
				// push the Entries table on the stack (to get entry information)
				lua_pushstring(L, "Entries");
				// stack now contains: -1 => "Entries"; -2 => key; -3 => value; -4 => key; -5 => table
				lua_gettable(L, -3);
				// stack now contains: -1 => Entries; -2 => key; -3 => value; -4 => key; -5 => table
				if(lua_type(L, -1) != LUA_TTABLE) {
					printf("Error in config: in group '%s': 'Entries' should be Table, is actually %s\n", group_name, lua_typename(L, lua_type(L, -1)));
					exit(1);
				}

				entry_count = get_entry_count(L);

				// check that the group has at least 1 entry
				if(entry_count <= 0) {
					printf("Skipping empty group '%s'\n", group_name);
					lua_pop(L, 1);
					// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
				}
				else {
					(*g)[count] = create_group(group_name, entry_count);

					// sort the entries if necessary
					if(sort) {
						sprintf(sort_cmd_lua, "table.sort(Groups[%d].Entries, function(a, b) return a.name < b.name end)", i);
						luaL_dostring(L, sort_cmd_lua);
					}

					// add entries to this group
					add_entries(L, (*g)[count]);
					lua_pop(L, 1);
					// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table

					// set the launcher, if applicable
					lua_pushstring(L, "Launcher");
					// stack now contains: -1 => "Launcher"; -2 => key; -3 => value; -4 => key; -5 => table
					lua_gettable(L, -3);
					// stack now contains: -1 => Launcher; -2 => key; -3 => value; -4 => key; -5 => table
					if(lua_type(L, -1) == LUA_TSTRING) {
						set_gprog((*g)[count], lua_tostring(L, -1));
					}
					lua_pop(L, 1);
					// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table

					// set the launcher flags, if applicable
					lua_pushstring(L, "Flags");
					// stack now contains: -1 => "Flags"; -2 => key; -3 => value; -4 => key; -5 => table
					lua_gettable(L, -3);
					// stack now contains: -1 => Flags; -2 => key; -3 => value; -4 => key; -5 => table
					if(lua_type(L, -1) == LUA_TSTRING) {
						set_gflags((*g)[count], lua_tostring(L, -1));
					}
					lua_pop(L, 1);
					// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table

					++count;
				}
			}
		}
		lua_pop(L, 2);
		// stack now contains: -1 => key; -2 => table
		++i;
	}
	// stack now contains: -1 => table (when lua_next returns 0 it pops the key
	// but does not push anything.)
	// Pop table
	lua_pop(L, 1);
	// Stack is now the same as it was on entry to this function	
}

void add_entries(lua_State *L, GROUP *g) {
	const char *entry_name;
	const char *entry_path;
	int entry_table_stack_index;
	int i = 1; // index in lua table
	int count = 0; // index in C struct

	// stack now contains: -1 => table
	lua_pushnil(L);
	// stack now contains: -1 => nil; -2 => table
	while (lua_next(L, -2)) {
		// stack now contains: -1 => value; -2 => key; -3 => table
		// copy the key so that lua_tostring does not modify the original
		lua_pushvalue(L, -2);
		// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		if(lua_type(L, -1) == LUA_TNUMBER && lua_type(L, -2) == LUA_TTABLE) {
			// check that the entry has a valid name
			lua_pushstring(L, "name");
			// stack now contains: -1 => "name"; -2 => key; -3 => value; -4 => key; -5 => table
			lua_gettable(L, -3);
			// stack now contains: -1 => name; -2 => key; -3 => value; -4 => key; -5 => table
			entry_name = lua_tostring(L, -1);
			lua_pop(L, 1);
			// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table

			if(entry_name != NULL) {
				// get this entry's path, if applicable
				lua_pushstring(L, "path");
				// stack now contains: -1 => "path"; -2 => key; -3 => value; -4 => key; -5 => table
				lua_gettable(L, -3);
				// stack now contains: -1 => path; -2 => key; -3 => value; -4 => key; -5 => table
				if(lua_type(L, -1) == LUA_TSTRING) entry_path = lua_tostring(L, -1);
				else entry_path = entry_name;
				lua_pop(L, 1);
				// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table

				set_gentry(g, count, create_entry(entry_name, entry_path, true));
				++count;
			}
		}
		lua_pop(L, 2);
		// stack now contains: -1 => key; -2 => table
	}
	// stack now contains: -1 => table (when lua_next returns 0 it pops the key
}

void stack_debug(lua_State *L) {
	int i;

	printf("DEBUGGING STACK:\n");
	for(i = 1; i <= lua_gettop(L); ++i) {
		printf("\t%d - %s", i, lua_typename(L, lua_type(L, i)));

		switch(lua_type(L, i)) {
			case LUA_TSTRING:
				printf(" - %s\n", lua_tostring(L, i));
				break;
				
			//case LUA_TTABLE:
			//	printf("\n");
			//	table_debug(L, 2);
			//	break;

			default:
				printf("\n");
		}
	}
}

// FIXME WIP debugging function
void table_debug(lua_State *L, int print_depth) {
	int i;

	// stack now contains: -1 => table
	lua_pushnil(L);
	// stack now contains: -1 => nil; -2 => table
	while (lua_next(L, -2)) {
		// stack now contains: -1 => value; -2 => key; -3 => table
		// copy the key so that lua_tostring does not modify the original
		lua_pushvalue(L, -2);
		// stack now contains: -1 => key; -2 => value; -3 => key; -4 => table
		const char *key = lua_tostring(L, -1);
		const char *value = lua_typename(L, lua_type(L, -2));
		for(i = 0; i < print_depth; ++i) {
			printf("\t");
		}
		printf("%s => %s", key, value);
		if(lua_type(L, -2) == LUA_TSTRING) {
			printf(" - %s", lua_tostring(L, -2));
		}
		printf("\n");
		// pop value + copy of key, leaving original key
		lua_pop(L, 2);
		// stack now contains: -1 => key; -2 => table
	}
	// stack now contains: -1 => table (when lua_next returns 0 it pops the key
	// but does not push anything.)
	// Pop table
	lua_pop(L, 1);
	// Stack is now the same as it was on entry to this function	
}