diff options
author | Jan200101 <sentrycraft123@gmail.com> | 2021-02-12 14:34:12 +0100 |
---|---|---|
committer | Jan200101 <sentrycraft123@gmail.com> | 2021-03-26 18:53:26 +0100 |
commit | 63e5e87d71e42f6f252b97468d2e4e7f3b95277a (patch) | |
tree | ceeb342a032b3b588f9c8cba55d895a5494e1b33 | |
parent | c7f30c98540de3ce8ad0a34b9e162801de0e257b (diff) | |
download | polecat-63e5e87d71e42f6f252b97468d2e4e7f3b95277a.tar.gz polecat-63e5e87d71e42f6f252b97468d2e4e7f3b95277a.zip |
a chunk of changes
- replace bool with ints,
- give enums a t suffix
- fix edge case in variable parser where the end of the buffer is not null
- make parseVar take an allocated variable list instead of a static one
- set the first argument of all tasks to be the prefix
- correctly parse chmodx directives
- add GAMEDIR variable to the variable list
- output known variables on installer info
- implement basic task handling
- check if file exists before redownloading
- remove debug function
- make noprogress argument into progress and convert it to int
-rw-r--r-- | src/common.c | 6 | ||||
-rw-r--r-- | src/common.h | 5 | ||||
-rw-r--r-- | src/dxvk.c | 2 | ||||
-rw-r--r-- | src/lutris.c | 163 | ||||
-rw-r--r-- | src/lutris.h | 10 | ||||
-rw-r--r-- | src/net.c | 12 | ||||
-rw-r--r-- | src/net.h | 4 | ||||
-rw-r--r-- | src/wine.c | 11 |
8 files changed, 134 insertions, 79 deletions
diff --git a/src/common.c b/src/common.c index 151e3a7..36cd2d9 100644 --- a/src/common.c +++ b/src/common.c @@ -1,7 +1,5 @@ - #include <stdio.h> #include <string.h> -#include <stdbool.h> #include <sys/stat.h> #include <unistd.h> #include <sys/types.h> @@ -39,14 +37,14 @@ struct stat getStat(const char* path) return sb; } -bool isFile(const char* path) +int isFile(const char* path) { struct stat sb = getStat(path); return S_ISREG(sb.st_mode) || S_ISLNK(sb.st_mode); } -bool isDir(const char* path) +int isDir(const char* path) { struct stat sb = getStat(path); diff --git a/src/common.h b/src/common.h index a77d0a2..b206f7b 100644 --- a/src/common.h +++ b/src/common.h @@ -4,7 +4,6 @@ #include <stdlib.h> #include <stddef.h> #include <stdint.h> -#include <stdbool.h> #include <sys/stat.h> #define ARRAY_LEN(arr) sizeof(arr) / sizeof(arr[0]) @@ -63,8 +62,8 @@ struct Command { void print_help(const struct Command*, size_t); struct stat getStat(const char* path); -bool isFile(const char*); -bool isDir(const char*); +int isFile(const char*); +int isDir(const char*); int makeDir(const char* path); int removeDir(const char *path); @@ -64,7 +64,7 @@ COMMAND(dxvk, download) fprintf(stderr, "Downloading %s...\n", name); - archive = downloadToRam(json_object_get_string(temp), 0L); + archive = downloadToRam(json_object_get_string(temp), 1); if (archive) { fprintf(stderr, "Extracting %s\n", name); diff --git a/src/lutris.c b/src/lutris.c index 412dbe4..7527f25 100644 --- a/src/lutris.c +++ b/src/lutris.c @@ -2,6 +2,7 @@ #include <stddef.h> #include <stdio.h> #include <string.h> +#include <unistd.h> #include <linux/limits.h> #include <libgen.h> @@ -11,38 +12,16 @@ static const struct Command lutris_commands[] = { #ifdef DEBUG { .name = "install", .func = lutris_install, .description = "install a lutris script" }, - { .name = "debug", .func = lutris_debug, .description = "" }, #endif { .name = "info", .func = lutris_info, .description = "show information about a lutris script" }, }; -char* func() -{ - char* str = malloc(5); - strncpy(str, "FUNC", 4+1); - - return str; -} -COMMAND(lutris, debug) +char* getpwd() { - struct list_t variables[] = { - { .key = "string", .type = value_string, .value.str = "STRING" }, - { .key = "func", .type = value_function, .value.func = func }, - }; - - char* str = malloc(255); - - strcpy(str, "$string $str $func $fun"); - - printf("Input: %s\n", str); - parseVar(&str, variables, ARRAY_LEN(variables)); - printf("Output: %s\n", str); - - - free(str); - - return 0; + char* pwd = malloc(255); + pwd = getcwd(pwd, 255); + return pwd; } COMMAND_GROUP_FUNC(lutris) @@ -55,7 +34,7 @@ COMMAND(lutris, install) if (installer.error == NONE) { - printf("Install %s - %s to the current directory?\nThis may download files and install wine versions\n(y/n)\n", installer.name, installer.version); + printf("Install %s - %s to the current directory? (y/n)\n", installer.name, installer.version); if (getchar() == 'y') { @@ -63,8 +42,15 @@ COMMAND(lutris, install) for (size_t i = 0; i < installer.filecount; ++i) { char* filename = basename(installer.files[i]->url); - printf("Dowloading %s...\n", filename); - downloadToFile(installer.files[i]->url, filename); + if (isFile(filename)) + { + printf("%s found. Not Downloading\n", filename); + } + else + { + printf("Dowloading %s\n", filename); + downloadToFile(installer.files[i]->url, filename, 1); + } } @@ -102,7 +88,12 @@ COMMAND(lutris, install) break; case INPUT_MENU: - // TODO + installer.variablecount++; + installer.variables = realloc(installer.variables, installer.variablecount * sizeof(void*)); + installer.variables[installer.variablecount-1] = malloc(sizeof(struct list_t)); + installer.variables[installer.variablecount-1]->key = installer.directives[i]->arguments[0]; + installer.variables[installer.variablecount-1]->type = value_string; + installer.variables[installer.variablecount-1]->value.str = installer.directives[i]->arguments[1]; break; case INSERT_DISC: @@ -110,7 +101,42 @@ COMMAND(lutris, install) break; case TASK: - // TODO + + parseVar(&installer.directives[i]->arguments[0], installer.variables, installer.variablecount); + setenv("WINEPREFIX", installer.directives[i]->arguments[0], 1); + switch(installer.directives[i]->task) + { + case WINEEXEC: + printf("WINEEXEC\n"); + break; + + case WINETRICKS: + printf("WINETRICKS\n"); + break; + + case CREATE_PREFIX: + printf("CREATE_PREFIX\n"); + setenv("WINEDEBUG", "-all", 1); + system("wineboot"); + unsetenv("WINEDEBUG"); + break; + + case SET_REGEDIT: + printf("SET_REGEDIT\n"); + break; + + case WINEKILL: + printf("WINEKILL\n"); + break; + + case TASKKEYWORDMAX: + case NO_TASK: + break; + + case UNKNOWN_TASK: + printf("Unknown Task\n"); + break; + } break; case UNKNOWN_DIRECTIVE: @@ -168,6 +194,30 @@ COMMAND(lutris, info) if (installer.description) puts(installer.description); if (installer.notes) puts(installer.notes); + + if (installer.variablecount) + { + puts("\nVariables:"); + for (size_t i = 0; i < installer.variablecount; ++i) + { + printf("\t%s -> ", installer.variables[i]->key); + switch (installer.variables[i]->type) + { + case value_function: + { + char* out = installer.variables[i]->value.func(); + puts(out); + free(out); + } + break; + + case value_string: + puts(installer.variables[i]->value.str); + break; + } + } + } + if (installer.filecount) { puts("\nFiles:"); @@ -276,7 +326,7 @@ struct script_t lutris_getInstaller(char* installername) { if(!strcmp(runnerstr, runnerStr[i])) { - installer.runner = i; + installer.runner = (enum runner_t)i; break; } } @@ -311,7 +361,8 @@ struct script_t lutris_getInstaller(char* installername) if (json_object_object_get_ex(script, "files", &files)) { installer.filecount = json_object_array_length(files); - installer.variablecount = installer.filecount; + // we need to assign variables for all files and for the GAMEDIR + installer.variablecount = installer.filecount + 1; installer.files = malloc(installer.filecount * sizeof(void*)); installer.variables = malloc(installer.variablecount * sizeof(void*)); @@ -352,8 +403,12 @@ struct script_t lutris_getInstaller(char* installername) installer.variables[i]->key = installer.files[i]->name; installer.variables[i]->type = value_string; installer.variables[i]->value.str = basename(installer.files[i]->url); - } + + installer.variables[installer.variablecount-1] = malloc(sizeof(struct list_t)); + installer.variables[installer.variablecount-1]->key = "GAMEDIR"; + installer.variables[installer.variablecount-1]->type = value_function; + installer.variables[installer.variablecount-1]->value.func = getpwd; } if (json_object_object_get_ex(script, "installer", &scriptinstall)) @@ -392,6 +447,7 @@ struct script_t lutris_getInstaller(char* installername) break; case CHMODX: + options[0] = directive; installer.directives[i]->size = 1; break; @@ -442,13 +498,14 @@ struct script_t lutris_getInstaller(char* installername) switch(k) { case WINEEXEC: - json_object_object_get_ex(directive, "executable", &options[1]); - installer.directives[i]->size = 1; + json_object_object_get_ex(directive, "prefix", &options[1]); + json_object_object_get_ex(directive, "executable", &options[2]); + installer.directives[i]->size = 2; break; case WINETRICKS: - json_object_object_get_ex(directive, "app", &options[1]); - json_object_object_get_ex(directive, "prefix", &options[2]); + json_object_object_get_ex(directive, "prefix", &options[1]); + json_object_object_get_ex(directive, "app", &options[2]); installer.directives[i]->size = 2; break; @@ -459,19 +516,20 @@ struct script_t lutris_getInstaller(char* installername) break; case SET_REGEDIT: - json_object_object_get_ex(directive, "path", &options[1]); - json_object_object_get_ex(directive, "key", &options[2]); - json_object_object_get_ex(directive, "value", &options[3]); - installer.directives[i]->size = 3; + json_object_object_get_ex(directive, "prefix", &options[1]); + json_object_object_get_ex(directive, "path", &options[2]); + json_object_object_get_ex(directive, "key", &options[3]); + json_object_object_get_ex(directive, "value", &options[4]); + installer.directives[i]->size = 4; break; } - installer.directives[i]->task = k; + installer.directives[i]->task = (enum task_t)k; break; } } break; } - installer.directives[i]->command = l; + installer.directives[i]->command = (enum keyword_t)l; const char* str; uint8_t offset = 0; @@ -553,7 +611,7 @@ void lutris_freeInstaller(struct script_t* installer) } } -size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) +size_t parseVar(char** pvar, struct list_t** variables, size_t variable_count) { char* var = *pvar; char* head = var; @@ -593,21 +651,22 @@ size_t parseVar(char** pvar, struct list_t* variables, size_t variable_count) if (keylen > 0) { // resolve variable key and store it - buf = malloc(keylen); - strncpy(buf, head, keylen); + buf = malloc(keylen+1); + strncpy(buf, head, keylen+1); + buf[keylen] = '\0'; for (size_t i = 0; i < variable_count; ++i) { - if (strncmp(variables[i].key, buf, keylen+1) == 0) + if (strncmp(variables[i]->key, buf, keylen+1) == 0) { - switch (variables[i].type) + switch (variables[i]->type) { case value_string: - value = malloc(strlen(variables[i].value.str) + 1); - strncpy(value, variables[i].value.str, strlen(variables[i].value.str) + 1); + value = malloc(strlen(variables[i]->value.str) + 1); + strncpy(value, variables[i]->value.str, strlen(variables[i]->value.str) + 1); break; case value_function: - value = variables[i].value.func(); + value = variables[i]->value.func(); break; default: diff --git a/src/lutris.h b/src/lutris.h index 7e009b0..cc92370 100644 --- a/src/lutris.h +++ b/src/lutris.h @@ -6,7 +6,7 @@ #define VARIABLESIGN '$' -enum keyword { +enum keyword_t { MOVE = 0, MERGE, EXTRACT, @@ -39,7 +39,7 @@ static const char keywordstr[KEYWORDMAX][0xF] = { "task", }; -enum task { +enum task_t { WINEEXEC = 0, WINETRICKS, CREATE_PREFIX, @@ -90,8 +90,8 @@ enum errors { }; struct directive_t { - enum keyword command; - enum task task; + enum keyword_t command; + enum task_t task; char** arguments; size_t size; }; @@ -150,6 +150,6 @@ void lutris_getInstallerURL(char*, char*, size_t); struct script_t lutris_getInstaller(char*); void lutris_freeInstaller(struct script_t*); -size_t parseVar(char**, struct list_t*, size_t); +size_t parseVar(char**, struct list_t**, size_t); #endif @@ -43,13 +43,13 @@ static inline int xferinfo(UNUSED void *p, curl_off_t dltotal, curl_off_t dlnow, return 0; } -struct MemoryStruct* downloadToRam(const char* URL, long noprogress) +struct MemoryStruct* downloadToRam(const char* URL, int progress) { CURL* curl_handle; CURLcode res = CURLE_OK; if (!isatty(STDERR_FILENO)) - noprogress = 1L; + progress = 0; struct MemoryStruct* chunk = malloc(sizeof(struct MemoryStruct)); @@ -69,7 +69,7 @@ struct MemoryStruct* downloadToRam(const char* URL, long noprogress) curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, USER_AGENT); curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl_handle, CURLOPT_XFERINFOFUNCTION, xferinfo); - curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, noprogress); + curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, !progress); res = curl_easy_perform(curl_handle); @@ -99,9 +99,9 @@ struct MemoryStruct* downloadToRam(const char* URL, long noprogress) return chunk; } -void downloadToFile(const char* URL, const char* path) +void downloadToFile(const char* URL, const char* path, int progress) { - struct MemoryStruct* chunk = downloadToRam(URL, 1L); + struct MemoryStruct* chunk = downloadToRam(URL, progress); if (chunk) { @@ -120,7 +120,7 @@ void downloadToFile(const char* URL, const char* path) struct json_object* fetchJSON(const char* URL) { - struct MemoryStruct* chunk = downloadToRam(URL, 1L); + struct MemoryStruct* chunk = downloadToRam(URL, 0); struct json_object* json = NULL; @@ -7,8 +7,8 @@ #define TIMEOPT CURLINFO_TOTAL_TIME_T size_t WriteMemoryCallback(void*, size_t, size_t, void*); -struct MemoryStruct* downloadToRam(const char* URL, long); -void downloadToFile(const char*, const char*); +struct MemoryStruct* downloadToRam(const char* URL, int); +void downloadToFile(const char*, const char*, int); struct json_object* fetchJSON(const char*); #endif @@ -1,7 +1,6 @@ #include <stdlib.h> #include <stdio.h> -#include <stdbool.h> #include <string.h> #include <json.h> #include <libgen.h> @@ -36,12 +35,12 @@ COMMAND(wine, download) if (runner) { struct json_object* versions, *value, *temp; - bool found; + int found; json_object_object_get_ex(runner, "versions", &versions); for (int i = 1; i < argc; ++i) { - found = false; + found = 0; char* choice = argv[i]; @@ -51,7 +50,7 @@ COMMAND(wine, download) json_object_object_get_ex(value, "version", &temp); if (strcmp(json_object_get_string(temp), choice) == 0) { - found = true; + found = 1; break; } } @@ -70,7 +69,7 @@ COMMAND(wine, download) fprintf(stderr, "Downloading %s...\n", name); - archive = downloadToRam(json_object_get_string(temp), 0L); + archive = downloadToRam(json_object_get_string(temp), 1); if (archive) { fprintf(stderr, "Extracting %s\n", name); @@ -282,7 +281,7 @@ COMMAND(wine, env) if (argc > 1) { // instead of creating redundant copies we just check for fish - bool fish_env = (strcmp(argv[0], "fish-env") == 0); + int fish_env = (strcmp(argv[0], "fish-env") == 0); char winepath[PATH_MAX]; char* winebinloc = NULL; // to be set by the wine type check |