diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 60 | ||||
-rw-r--r-- | src/config.h | 3 | ||||
-rw-r--r-- | src/main.c | 65 |
3 files changed, 102 insertions, 26 deletions
diff --git a/src/config.c b/src/config.c index b456c50..292e22d 100644 --- a/src/config.c +++ b/src/config.c @@ -6,8 +6,9 @@ #include <string.h> #include "config.h" -struct compile_cmd_t **ccmds; -int ccmds_size = 0; +struct compile_cmd_t ***ccmds; +size_t ccmds_size = 0; + int parse_config(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) { @@ -15,14 +16,18 @@ int parse_config(const char *fpath, const struct stat *sb, return 0; struct compile_cmd_t *ccmd = malloc (sizeof (struct compile_cmd_t)); - struct compile_cmd_t **ccmds_tmp = realloc (ccmds, sizeof (ccmds)+sizeof (struct compile_cmd_t)); + struct compile_cmd_t **ccmds_tmp = realloc (*ccmds, sizeof (ccmds)+sizeof (struct compile_cmd_t)); if (ccmds_tmp) - ccmds = ccmds_tmp; + *ccmds = ccmds_tmp; FILE *cfg = fopen (fpath, "r"); + int CMD_SET = 1; + int ARGS_SET = 2; + int FILEEXT_SET = 4; + int filled = 0; - while (filled == 0) { + while (filled != (CMD_SET|ARGS_SET|FILEEXT_SET)) { char *line = strdup (""); int tmp_char; while ((tmp_char = fgetc (cfg))) { @@ -32,7 +37,36 @@ int parse_config(const char *fpath, const struct stat *sb, if (tmp_char == '\n' || tmp_char == '\0') break; } - printf ("%s", line); + char *seperator_fnd = strstr (line, "="); + if (!seperator_fnd) + puts ("invalid config line"); + else { + char *value = strdup (seperator_fnd+1); + int identifier_len = strlen (line) - strlen (value) - 1; + char *identifier = malloc (identifier_len + 1); + strncpy(identifier, line, identifier_len); + identifier[identifier_len] = '\0'; + + if (strcmp (identifier, "compiler") == 0 && !(filled & CMD_SET)) { + ccmd->cmd = trim (strdup (value), NULL, NULL); + filled = filled | CMD_SET; + } + else if (strcmp (identifier, "args") == 0 && !(filled & ARGS_SET)) { + ccmd->args = trim (strdup (value), NULL, NULL); + filled = filled | ARGS_SET; + } + else if (strcmp (identifier, "fileext") == 0 && !(filled & FILEEXT_SET)) { + ccmd->fileext = trim (strdup (value), NULL, NULL); + filled = filled | FILEEXT_SET; + } + else { + printf ("warn: invalid configuration: %s\n", fpath); + exit (1); + } + + free (identifier); + free (value); + } free (line); } fclose (cfg); @@ -41,9 +75,9 @@ int parse_config(const char *fpath, const struct stat *sb, return 0; } -void get_commands() { - // struct compile_cmd_t **ccmds; - ccmds = malloc (sizeof (struct compile_cmd_t)); +size_t get_commands(struct compile_cmd_t ***ccmds_glob) { + ccmds = ccmds_glob; + *ccmds = malloc (sizeof (struct compile_cmd_t)); char *home = getenv ("HOME"); if (!home) { puts ("warn: HOME not found. Unable to read home configuration files."); @@ -61,11 +95,5 @@ void get_commands() { free (paths[i]); } free (paths); - - for (int i = 0; i < ccmds_size; i++) { - puts (ccmds[i]->cmd); - free (ccmds[i]->cmd); - free (ccmds[i]); - } - free (ccmds); + return ccmds_size; } diff --git a/src/config.h b/src/config.h index f27b17f..f73d2b3 100644 --- a/src/config.h +++ b/src/config.h @@ -1,7 +1,8 @@ +#include <stdlib.h> struct compile_cmd_t { char *cmd; char *args; char *fileext; }; -void get_commands(); +size_t get_commands(struct compile_cmd_t ***ccmds_glob); @@ -2,34 +2,81 @@ #include <stdio.h> #include <string.h> #include <errno.h> +#include <unistd.h> #define _XOPEN_SOURCE 500 #include <extlib.h> #include "config.h" + +struct compile_cmd_t *find_cmd(char *file_extension, struct compile_cmd_t **ccmds, size_t ccmds_len) { + for (int i = 0; i < ccmds_len; i++) { + if (strcmp (ccmds[i]->fileext, file_extension) == 0) { + return ccmds[i]; + } + } + return NULL; +} + int main (int argc, char *argv[]) { - char *compiler = "gcc"; + struct compile_cmd_t **ccmds; + size_t ccmds_len; + + ccmds_len = get_commands(&ccmds); + + size_t arglength = strlen (argv[argc-1]); + char **ap, *fileparts[arglength], *inputstring; + inputstring = strdup (argv[argc-1]); + size_t parts_len = 0; + for (ap = fileparts; (*ap = strsep (&inputstring, ".")) != NULL;) { + if (**ap != '\0') { + if (++ap >= &inputstring) + break; + parts_len += 1; + } + } + + struct compile_cmd_t *matching_cmd = find_cmd(fileparts[parts_len-1], ccmds, ccmds_len); + if (!matching_cmd) { + puts ("error: no complier for file extension found"); + return 1; + } + char *tmplate = strdup ("/tmp/XXXXXX"); if (!tmplate) exit (1); char *tmpdir = mkdtemp(tmplate); if (!tmpdir) exit (1); - char *cmd = malloc (strlen (compiler)+strlen(argv[argc-1])+strlen (" -o ")+strlen (tmpdir)+strlen ("/meow")+1); - char *excmd = malloc (strlen (tmplate)+strlen ("/meow")+1); + char *cmd = malloc (strlen (matching_cmd->cmd)+strlen(matching_cmd->args)+strlen(argv[argc-1])+strlen (" -o ")+strlen (tmpdir)+strlen ("/exec")+1); + char *excmd = malloc (strlen (tmplate)+strlen ("/exec")+1); if (!cmd) exit (1); if (!excmd) exit (1); puts (tmpdir); - sprintf (cmd, "%s %s -o %s/meow", compiler, argv[argc-1], tmpdir); - system (cmd); - sprintf (excmd, "%s/meow", tmpdir); - system (excmd); - int rtrn = rrmdir (tmpdir); + sprintf (cmd, "%s %s %s -o %s/exec", matching_cmd->cmd, matching_cmd->args, argv[argc-1], tmpdir); + int cmp_rtrn = system (cmd); + if (cmp_rtrn != 0) { + fprintf(stderr, "error: compiler failed"); + return 1; + } + sprintf (excmd, "%s/exec", tmpdir); + puts (cmd); + puts (excmd); + int sys_rtrn = system (excmd); + if (sys_rtrn != 0) { + fprintf (stderr, "error: binary failed"); + } + /* int rtrn = rrmdir (tmpdir); if (rtrn != 0) printf ("%d %d\n", rtrn, errno); - get_commands(); + */ + for (int i = 0; i < ccmds_len; i++) { + free (ccmds[i]->cmd); + free (ccmds[i]); + } + free (ccmds); free (tmpdir); free (cmd); free (excmd); |