aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/config.c197
-rw-r--r--src/config.h8
-rw-r--r--src/main.c217
3 files changed, 222 insertions, 200 deletions
diff --git a/src/config.c b/src/config.c
index f9d5481..66f53d6 100644
--- a/src/config.c
+++ b/src/config.c
@@ -28,117 +28,124 @@
struct compile_cmd_t ***ccmds;
size_t ccmds_size = 0;
-struct compile_cmd_t *compile_cmd_init()
+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;
+ 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);
+ 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)
+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;
+ 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 = compile_cmd_init();
- struct compile_cmd_t **ccmds_tmp = realloc (*ccmds, sizeof (ccmds)+sizeof (struct compile_cmd_t));
- if (ccmds_tmp)
- *ccmds = ccmds_tmp;
+ 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;
- FILE *cfg = fopen (fpath, "r");
+ FILE *cfg = fopen (fpath, "r");
- int CMD_SET = 1;
- int FILEEXT_SET = 2;
+ int CMD_SET = 1;
+ int FILEEXT_SET = 2;
- int filled = 0;
- while (filled != (CMD_SET|FILEEXT_SET)) {
- char *line = strdup ("");
- int tmp_char = '\0';
- while ((tmp_char = fgetc (cfg))) {
- char *line_tmp = realloc (line, strlen (line) + 8);
- if (line_tmp) line = line_tmp;
- sprintf (line, "%s%c", line, tmp_char);
- if (tmp_char == '\n' || tmp_char == '\0')
- break;
- }
- char *seperator_fnd = strstr (line, "=");
- if (!seperator_fnd)
- 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 (value, NULL, NULL);
- filled = filled | CMD_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 (value, NULL, NULL);
- filled = filled | FILEEXT_SET;
- }
- else {
- fprintf (stderr, "warn: invalid configuration: %s\n", fpath);
- free (identifier);
- free (value);
- free (line);
- fclose (cfg);
- free (ccmd);
- return 1;
- }
+ int filled = 0;
+ while (filled != (CMD_SET | FILEEXT_SET)) {
+ char *line = strdup ("");
+ int tmp_char = '\0';
+ while ((tmp_char = fgetc (cfg))) {
+ char *line_tmp = realloc (line, strlen (line) + 8);
+ if (line_tmp)
+ line = line_tmp;
+ sprintf (line, "%s%c", line, tmp_char);
+ if (tmp_char == '\n' || tmp_char == '\0')
+ break;
+ }
+ char *seperator_fnd = strstr (line, "=");
+ if (!seperator_fnd)
+ 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 (value, NULL, NULL);
+ filled = filled | CMD_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 (value, NULL, NULL);
+ filled = filled | FILEEXT_SET;
+ }
+ else {
+ fprintf (stderr, "warn: invalid configuration: %s\n", fpath);
+ free (identifier);
+ free (value);
+ free (line);
+ fclose (cfg);
+ free (ccmd);
+ return 1;
+ }
- free (identifier);
- free (value);
+ free (identifier);
+ free (value);
+ }
+ free (line);
}
- free (line);
- }
- fclose (cfg);
- ccmds_tmp[ccmds_size] = ccmd;
- ccmds_size += 1;
- return 0;
+ fclose (cfg);
+ ccmds_tmp[ccmds_size] = ccmd;
+ ccmds_size += 1;
+ return 0;
}
-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) {
- fprintf (stderr, "warn: HOME not found. Unable to read home configuration files.\n");
- }
- char *paths[2] = {"/etc/runc.d", NULL};
- if (home) {
- paths[1] = malloc (strlen (home)+strlen ("/.config/runc")+1);
- sprintf (paths[1], "%s/.config/runc", home);
- }
- for (int i = 0; i < 2; i++) {
- if (!paths[i])
- break;
- nftw (paths[i], parse_config, 10, FTW_DEPTH|FTW_MOUNT|FTW_PHYS);
- if (i==1)
- free (paths[i]);
- }
- return ccmds_size;
+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) {
+ fprintf (stderr,
+ "warn: HOME not found. Unable to read home configuration files.\n");
+ }
+ char *paths[2] = { "/etc/runc.d", NULL };
+ if (home) {
+ paths[1] = malloc (strlen (home) + strlen ("/.config/runc") + 1);
+ sprintf (paths[1], "%s/.config/runc", home);
+ }
+ for (int i = 0; i < 2; i++) {
+ if (!paths[i])
+ break;
+ nftw (paths[i], parse_config, 10, FTW_DEPTH | FTW_MOUNT | FTW_PHYS);
+ if (i == 1)
+ free (paths[i]);
+ }
+ return ccmds_size;
}
diff --git a/src/config.h b/src/config.h
index 250e3f3..a89fa84 100644
--- a/src/config.h
+++ b/src/config.h
@@ -19,11 +19,11 @@
#include <stdlib.h>
struct compile_cmd_t {
- char *cmd;
- char *args;
- char *fileext;
+ char *cmd;
+ char *args;
+ char *fileext;
};
void compile_cmd_free (struct compile_cmd_t *self);
-size_t get_commands(struct compile_cmd_t ***ccmds_glob);
+size_t get_commands (struct compile_cmd_t ***ccmds_glob);
diff --git a/src/main.c b/src/main.c
index 1978409..8dba990 100644
--- a/src/main.c
+++ b/src/main.c
@@ -26,119 +26,134 @@
#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];
+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;
+ return NULL;
}
-void remove_shebang(char **file, char *tmpdir, char *file_extension) {
- char *new_file_path = malloc (strlen (tmpdir)+strlen ("/source.")+strlen(file_extension)+1);
- sprintf (new_file_path, "%s/source.%s", tmpdir, file_extension);
- FILE *new_file = fopen (new_file_path, "w");
- FILE *old_file = fopen (*file, "r");
- char buffer[1024];
- fgets (buffer, 1024, old_file);
- fcopy (old_file, new_file);
- fclose (new_file);
- fclose (old_file);
- free (*file);
- *file = strdup (new_file_path);
- free (new_file_path);
+void
+remove_shebang (char **file, char *tmpdir, char *file_extension)
+{
+ char *new_file_path = malloc (strlen (tmpdir) + strlen ("/source.") +
+ strlen (file_extension) + 1);
+ sprintf (new_file_path, "%s/source.%s", tmpdir, file_extension);
+ FILE *new_file = fopen (new_file_path, "w");
+ FILE *old_file = fopen (*file, "r");
+ char buffer[1024];
+ fgets (buffer, 1024, old_file);
+ fcopy (old_file, new_file);
+ fclose (new_file);
+ fclose (old_file);
+ free (*file);
+ *file = strdup (new_file_path);
+ free (new_file_path);
}
-int main (int argc, char *argv[]) {
- 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])];
+int
+main (int argc, char *argv[])
+{
+ 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])];
+ char *extra_args = NULL;
- 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);
+ if (argc < 2) {
+ fprintf (stderr, "usage: %s <source file>", argv[0]);
+ errno = 1;
+ goto EXIT;
+ }
+ else if (argc > 2) {
+ extra_args = join_str(argv, argc-1, ' ');
+ }
+ compile_file = strdup (argv[argc - 1]);
+ ccmds_len = get_commands (&ccmds);
- tmpdir = mkdtemp(tmpdir);
- if (!tmpdir) {
- errno = 1;
- goto EXIT;
- }
+ tmpdir = mkdtemp (tmpdir);
+ if (!tmpdir) {
+ errno = 1;
+ goto EXIT;
+ }
- 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;
- }
+ 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;
+ }
- matching_cmd = find_cmd(fileparts[parts_len-1], ccmds, ccmds_len);
- if (!matching_cmd) {
- fprintf (stderr, "error: no complier for file extension '%s' found\n", fileparts[parts_len-1]);
- errno = 1;
- goto EXIT;
- }
+ matching_cmd = find_cmd (fileparts[parts_len - 1], ccmds, ccmds_len);
+ if (!matching_cmd) {
+ fprintf (stderr, "error: no complier for file extension '%s' found\n",
+ fileparts[parts_len - 1]);
+ errno = 1;
+ goto EXIT;
+ }
- FILE *source = fopen (compile_file, "r");
- // char *is_shebang = malloc (strlen ("#!")+1);
- char is_shebang[3];
- fread (&is_shebang, 1, 2, source);
- fclose (source);
- if (strcmp (is_shebang, "#!") == 0)
- remove_shebang (&compile_file, tmpdir, fileparts[parts_len-1]);
+ FILE *source = fopen (compile_file, "r");
+ char is_shebang[3];
+ fread (&is_shebang, 1, 2, source);
+ fclose (source);
+ if (strcmp (is_shebang, "#!") == 0)
+ remove_shebang (&compile_file, tmpdir, fileparts[parts_len - 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;
- }
+ cmd = malloc (strlen (matching_cmd->cmd) +
+ strlen (matching_cmd->args) +
+ strlen (compile_file) +
+ strlen (extra_args == NULL ? "" : extra_args + strlen (argv[0]) + 1) +
+ 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 %s -o %s/exec", matching_cmd->cmd,
+ matching_cmd->args, extra_args == NULL ? "" : extra_args + strlen (argv[0]) + 1, 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);
- goto EXIT;
- }
+ sprintf (excmd, "%s/exec", tmpdir);
+ if ((errno = system (excmd), errno != 0)) {
+ fprintf (stderr, "error: binary failed with code %d\n", errno);
+ goto EXIT;
+ }
- if ((errno = rrmdir(tmpdir)), errno != 0)
- fprintf (stderr, "error: failed to remove temporary directory %s\n", tmpdir);
+ if ((errno = rrmdir (tmpdir)), errno != 0)
+ fprintf (stderr, "error: failed to remove temporary directory %s\n",
+ tmpdir);
- EXIT:
- for (int i = 0; i < ccmds_len; i++)
- compile_cmd_free (ccmds[i]);
- free (excmd);
- free (ccmds);
- free (tmpdir);
- free (cmd);
- free (compile_file);
- exit(errno % 255);
+ EXIT:
+ for (int i = 0; i < ccmds_len; i++)
+ compile_cmd_free (ccmds[i]);
+ free (excmd);
+ free (ccmds);
+ free (tmpdir);
+ free (cmd);
+ free (compile_file);
+ free (extra_args);
+ exit (errno % 255);
}