|
|
@ -1,3 +1,6 @@ |
|
|
|
|
|
|
|
#define _POSIX_C_SOURCE 200809L |
|
|
|
|
|
|
|
#define _DEFAULT_SOURCE |
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdlib.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <stdio.h> |
|
|
|
#include <string.h> |
|
|
|
#include <string.h> |
|
|
@ -6,6 +9,8 @@ |
|
|
|
#define ANSI_COLOR_RED "\x1b[31m" |
|
|
|
#define ANSI_COLOR_RED "\x1b[31m" |
|
|
|
#define ANSI_COLOR_RESET "\x1b[0m" |
|
|
|
#define ANSI_COLOR_RESET "\x1b[0m" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define parse_failure(str, line) {fprintf(stderr, "Error in config file at line %d: " str "\n", line); exit(EXIT_FAILURE);} |
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char const *argv[]) |
|
|
|
int main(int argc, char const *argv[]) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (argc < 2) |
|
|
|
if (argc < 2) |
|
|
@ -14,41 +19,66 @@ int main(int argc, char const *argv[]) |
|
|
|
fd = fopen(argv[1], "r"); |
|
|
|
fd = fopen(argv[1], "r"); |
|
|
|
if (!fd) |
|
|
|
if (!fd) |
|
|
|
return -1; |
|
|
|
return -1; |
|
|
|
for (;;) { |
|
|
|
for (int linenum = 1;; linenum++) { |
|
|
|
char *line = NULL; |
|
|
|
int fuzzy = 0, tmp; |
|
|
|
|
|
|
|
char *line = NULL, *keys = NULL, *command = NULL; |
|
|
|
size_t linelen = 0; |
|
|
|
size_t linelen = 0; |
|
|
|
|
|
|
|
|
|
|
|
if (getline(&line, &linelen, fd) == -1) |
|
|
|
if (getline(&line, &linelen, fd) == -1) |
|
|
|
break; |
|
|
|
break; |
|
|
|
if (linelen < 2) |
|
|
|
if (linelen < 2) |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
|
|
|
|
|
|
|
|
printf(ANSI_COLOR_RED "%s\n" ANSI_COLOR_RESET, line); |
|
|
|
printf(ANSI_COLOR_RED "Original line:\n%s\n" ANSI_COLOR_RESET, line); |
|
|
|
/* remove white spaces */ |
|
|
|
|
|
|
|
int inquotes = 0; |
|
|
|
// Remove leading spaces
|
|
|
|
int inapos = 0; |
|
|
|
while (isspace(line[0]) && linelen > 1) |
|
|
|
for (size_t i = 0; i < linelen; i++) { |
|
|
|
memmove(line, &line[1], --linelen); |
|
|
|
if (line[i] == '"' && !inapos) |
|
|
|
|
|
|
|
inquotes = !inquotes; |
|
|
|
// Skip comments and blank lines
|
|
|
|
if (line[i] == '\'' && !inquotes) |
|
|
|
if (line[0] == '#' || !line[0]) { |
|
|
|
inapos = !inapos; |
|
|
|
free(line); |
|
|
|
|
|
|
|
continue; |
|
|
|
if (isblank(line[i]) && !(inquotes || inapos)) { |
|
|
|
} |
|
|
|
memmove(&line[i], &line[i + 1], linelen - i); |
|
|
|
|
|
|
|
i -= i ? 1 : 0; |
|
|
|
printf("Valid line:\n%s\n", line); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: multiline commands, ending with "\\n"
|
|
|
|
|
|
|
|
// TODO: better error checks in order to remove unnecessary
|
|
|
|
|
|
|
|
// memmoves (line has to begin with cmment or "*-"), etc.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (line[0] == '*') |
|
|
|
|
|
|
|
fuzzy = 1; |
|
|
|
|
|
|
|
memmove(line, &line[1], --linelen); |
|
|
|
|
|
|
|
// Remove leading spaces
|
|
|
|
|
|
|
|
while (isspace(line[0]) && linelen > 1) |
|
|
|
|
|
|
|
memmove(line, &line[1], --linelen); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
keys = strtok(line, ":"); |
|
|
|
|
|
|
|
command = strtok(NULL, ":"); |
|
|
|
|
|
|
|
if (!command || !keys) |
|
|
|
|
|
|
|
parse_failure("No command or keys specified", linenum); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Remove whitespaces in keys
|
|
|
|
|
|
|
|
tmp = strlen(keys); |
|
|
|
|
|
|
|
for (int i = 0; i < tmp; i++) { |
|
|
|
|
|
|
|
if (isspace(keys[i])) { |
|
|
|
|
|
|
|
memmove(&line[i], &line[i + 1], --tmp); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if (line[0] == '#') |
|
|
|
|
|
|
|
continue; |
|
|
|
// Remove leading spaces in command
|
|
|
|
printf("%s\n", line); |
|
|
|
tmp = strlen(command); |
|
|
|
|
|
|
|
while (isspace(command[0]) && tmp > 1) |
|
|
|
char *token = NULL; |
|
|
|
memmove(command, &command[1], --tmp); |
|
|
|
token = strtok(line, "="); |
|
|
|
|
|
|
|
if (token) |
|
|
|
int x = 1; |
|
|
|
printf("%s\n", token); |
|
|
|
char *k = strtok(keys, ","); |
|
|
|
token = strtok(NULL, "="); |
|
|
|
do { |
|
|
|
if (token) |
|
|
|
printf("Key %d: %s\n", x++, k); |
|
|
|
printf("%s\n", token); |
|
|
|
} while ((k = strtok(NULL, ","))); |
|
|
|
free(line); |
|
|
|
|
|
|
|
|
|
|
|
printf("Command: %s\n", command); |
|
|
|
} |
|
|
|
} |
|
|
|
return 0; |
|
|
|
return 0; |
|
|
|
} |
|
|
|
} |
|
|
|