aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--config/c3
-rw-r--r--config/f902
-rw-r--r--config/rs2
-rw-r--r--src/config.c73
-rw-r--r--src/config.h2
-rw-r--r--src/main.c111
6 files changed, 121 insertions, 72 deletions
diff --git a/config/c b/config/c
new file mode 100644
index 0000000..4981b37
--- /dev/null
+++ b/config/c
@@ -0,0 +1,3 @@
+compiler=gcc
+args=-fsanitize=undefined,address
+fileext=c
diff --git a/config/f90 b/config/f90
new file mode 100644
index 0000000..b6350ba
--- /dev/null
+++ b/config/f90
@@ -0,0 +1,2 @@
+compiler=gfortran
+fileext=f90
diff --git a/config/rs b/config/rs
new file mode 100644
index 0000000..3a34b4d
--- /dev/null
+++ b/config/rs
@@ -0,0 +1,2 @@
+compiler=rustc
+fileext=rs
diff --git a/src/config.c b/src/config.c
index 292e22d..e8a0443 100644
--- a/src/config.c
+++ b/src/config.c
@@ -9,13 +9,34 @@
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)
+struct compile_cmd_t *compile_cmd_init()
+{
+ struct compile_cmd_t *self = malloc (sizeof (struct compile_cmd_t));
+ self->cmd = NULL;
+ self->args = strdup ("");
+ self->fileext = NULL;
+ return self;
+}
+
+void
+compile_cmd_free (struct compile_cmd_t *self)
+{
+ if (self->cmd) free (self->cmd);
+ if (self->args) free (self->args);
+ if (self->fileext) free (self->fileext);
+ free (self);
+}
+
+int
+parse_config(const char *fpath, const struct stat *sb,
+ int typeflag, struct FTW *ftwbuf)
{
if (!S_ISREG (sb->st_mode))
return 0;
+ if (sb->st_size < 12) // config file that doesnt even include "compiler=xx" can be ignored
+ return 0;
- struct compile_cmd_t *ccmd = malloc (sizeof (struct compile_cmd_t));
+ struct compile_cmd_t *ccmd = compile_cmd_init();
struct compile_cmd_t **ccmds_tmp = realloc (*ccmds, sizeof (ccmds)+sizeof (struct compile_cmd_t));
if (ccmds_tmp)
*ccmds = ccmds_tmp;
@@ -23,13 +44,12 @@ int parse_config(const char *fpath, const struct stat *sb,
FILE *cfg = fopen (fpath, "r");
int CMD_SET = 1;
- int ARGS_SET = 2;
- int FILEEXT_SET = 4;
+ int FILEEXT_SET = 2;
int filled = 0;
- while (filled != (CMD_SET|ARGS_SET|FILEEXT_SET)) {
+ while (filled != (CMD_SET|FILEEXT_SET)) {
char *line = strdup ("");
- int tmp_char;
+ int tmp_char = '\0';
while ((tmp_char = fgetc (cfg))) {
char *line_tmp = realloc (line, strlen (line) + 8);
if (line_tmp) line = line_tmp;
@@ -39,29 +59,33 @@ int parse_config(const char *fpath, const struct stat *sb,
}
char *seperator_fnd = strstr (line, "=");
if (!seperator_fnd)
- puts ("invalid config line");
+ fprintf (stderr, "warn: invalid configuration: %s\n", fpath);
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);
+ ccmd->cmd = trim (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, "args") == 0) {
+ free (ccmd->args);
+ ccmd->args = trim (value, NULL, NULL);
}
else if (strcmp (identifier, "fileext") == 0 && !(filled & FILEEXT_SET)) {
- ccmd->fileext = trim (strdup (value), NULL, NULL);
+ ccmd->fileext = trim (value, NULL, NULL);
filled = filled | FILEEXT_SET;
}
else {
- printf ("warn: invalid configuration: %s\n", fpath);
- exit (1);
+ fprintf (stderr, "warn: invalid configuration: %s\n", fpath);
+ free (identifier);
+ free (value);
+ free (line);
+ fclose (cfg);
+ free (ccmd);
+ return 1;
}
free (identifier);
@@ -80,20 +104,17 @@ size_t get_commands(struct compile_cmd_t ***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.");
+ fprintf (stderr, "warn: HOME not found. Unable to read home configuration files.\n");
}
- char **paths = malloc (sizeof (char*)*4);
- paths[0] = strdup ("/etc/runc.d");
- paths[1] = strdup ("/usr/share/runc");
+ char *paths[2] = {"/etc/runc.d", NULL};
if (home) {
- paths[2] = malloc (strlen (home)+strlen ("/.local/share/runc")+1);
- sprintf (paths[2], "%s/.local/share/runc", home);
+ paths[1] = malloc (strlen (home)+strlen ("/.config/runc")+1);
+ sprintf (paths[1], "%s/.config/runc", home);
}
- for (int i = 0; i < 3; i++) {
+ for (int i = 0; i < 2; i++) {
if (!paths[i]) break;
- int err = nftw (paths[i], parse_config, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
- free (paths[i]);
+ nftw (paths[i], parse_config, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
+ if (i==1) free (paths[i]);
}
- free (paths);
return ccmds_size;
}
diff --git a/src/config.h b/src/config.h
index f73d2b3..f0dd7f1 100644
--- a/src/config.h
+++ b/src/config.h
@@ -5,4 +5,6 @@ struct compile_cmd_t {
char *fileext;
};
+void compile_cmd_free (struct compile_cmd_t *self);
+
size_t get_commands(struct compile_cmd_t ***ccmds_glob);
diff --git a/src/main.c b/src/main.c
index 16069c4..d32b1db 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,7 +1,6 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <errno.h>
#include <unistd.h>
#define _XOPEN_SOURCE 500
@@ -19,62 +18,82 @@ struct compile_cmd_t *find_cmd(char *file_extension, struct compile_cmd_t **ccmd
}
int main (int argc, char *argv[]) {
- struct compile_cmd_t **ccmds;
- size_t ccmds_len;
+ int errno = 0;
+ size_t parts_len = 0;
+ size_t ccmds_len = 0;
+ struct compile_cmd_t **ccmds = NULL;
+ struct compile_cmd_t *matching_cmd = NULL;
+ char *tmpdir = strdup ("/tmp/XXXXXX");;
+ char *compile_file = NULL;
+ char *ap = NULL;
+ char *cmd = NULL;
+ char *excmd = NULL;
+ char *fileparts[strlen (argv[argc-1])];
+ if (argc < 2) {
+ fprintf (stderr, "usage: %s <source file>", argv[0]);
+ errno = 1;
+ goto EXIT;
+ }
+ compile_file = strdup(argv[argc-1]);
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;
- }
+ for (int i = 0; (ap = strsep (&argv[argc-1], ".")); i++) {
+ fileparts[i] = ap;
+ parts_len += 1;
+ }
+ if (parts_len == 0) {
+ fprintf (stderr, "error: no file extension found\n");
+ errno = 1;
+ goto EXIT;
}
- struct compile_cmd_t *matching_cmd = find_cmd(fileparts[parts_len-1], ccmds, ccmds_len);
+ matching_cmd = find_cmd(fileparts[parts_len-1], ccmds, ccmds_len);
if (!matching_cmd) {
- puts ("error: no complier for file extension found");
- return 1;
+ fprintf (stderr, "error: no complier for file extension '%s' found\n", fileparts[parts_len-1]);
+ errno = 1;
+ goto EXIT;
}
- char *tmplate = strdup ("/tmp/XXXXXX");
- if (!tmplate)
- exit (1);
- char *tmpdir = mkdtemp(tmplate);
- if (!tmpdir)
- exit (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);
+ tmpdir = mkdtemp(tmpdir);
+ if (!tmpdir) {
+ errno = 1;
+ goto EXIT;
+ }
- 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;
+ cmd = malloc (strlen (matching_cmd->cmd)+
+ strlen (matching_cmd->args)+
+ strlen (compile_file)+
+ strlen (" -o ")+
+ strlen (tmpdir)+
+ strlen ("/exec")+1);
+ if (!cmd) {
+ errno = 1;
+ goto EXIT;
}
+ excmd = malloc (strlen (tmpdir)+strlen ("/exec")+1);
+ if (!excmd) {
+ errno = 1;
+ goto EXIT;
+ };
+ sprintf (cmd, "%s %s %s -o %s/exec", matching_cmd->cmd, matching_cmd->args, compile_file, tmpdir);
+ if ((errno = system (cmd), errno != 0)) {
+ fprintf(stderr, "error: compiler failed with code %d\n", errno);
+ goto EXIT;
+ }
+
sprintf (excmd, "%s/exec", tmpdir);
+ if ((errno = system (excmd), errno != 0))
+ fprintf (stderr, "error: binary failed with code %d\n", errno);
- int sys_rtrn = system (excmd);
- if (sys_rtrn != 0) {
- fprintf (stderr, "error: binary failed");
- }
- for (int i = 0; i < ccmds_len; i++) {
- free (ccmds[i]->cmd);
- free (ccmds[i]);
- }
- free (ccmds);
- free (tmpdir);
- free (cmd);
- free (excmd);
- return 0;
+ EXIT:
+ for (int i = 0; i < ccmds_len; i++)
+ compile_cmd_free (ccmds[i]);
+ if (excmd) free (excmd);
+ if (ccmds) free (ccmds);
+ if (tmpdir) free (tmpdir);
+ if (cmd) free (cmd);
+ if (compile_file) free (compile_file);
+ exit(errno % 255);
}