+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <limits.h>
+#include <signal.h>
+
+#define LINE_SIZE 4096
+
+struct config {
+ char *obj_path;
+ char *tool_path;
+ char *src_path;
+ FILE *conf;
+ FILE *term;
+} *gcfg;
+
+struct sigaction sigact;
+static void sigh(int);
+static void sigc(void);
+
+static void __dead
+usage(void)
+{
+ fprintf(stderr, "usage: puffcrash [-c config] [serial console log]\n");
+ exit(1);
+}
+
+void
+sigh(int sig)
+{
+ if (sig == SIGINT) sigc();
+}
+
+void
+sigc(void)
+{
+ if (gcfg->obj_path) free(gcfg->obj_path);
+ if (gcfg->tool_path) free(gcfg->tool_path);
+ if (gcfg->src_path) free(gcfg->src_path);
+
+ if (gcfg->conf) fclose(gcfg->conf);
+ if (gcfg->term && gcfg->term != stdin) fclose(gcfg->term);
+
+ exit(1);
+}
+
+int
+parse_config(char *config_path, struct config *cfg)
+{
+ char *line, *i;
+ int check = 0;
+
+ cfg->conf = fopen(config_path, "r");
+
+ if(!(line = calloc(1, 256)) || !cfg->conf)
+ return 1;
+
+ while ((fgets(line, 256, cfg->conf))) {
+ if (strstr(line, "obj_path")) {
+ i = strstr(line, "=") + 2;
+ strlcpy(cfg->obj_path, i, PATH_MAX);
+ check |= (1 << 0);
+ } else if (strstr(line, "tool_path")) {
+ i = strstr(line, "=") + 2;
+ strlcpy(cfg->tool_path, i, PATH_MAX);
+ check |= (1 << 1);
+ } else if (strstr(line, "src_path")) {
+ i = strstr(line, "=") + 2;
+ strlcpy(cfg->src_path, i, PATH_MAX);
+ check |= (1 << 2);
+ }
+ }
+
+ free(line);
+
+ if (check == 0x7)
+ return 0;
+ else
+ return 1;
+}
+
+void
+watch_serial(void)
+{
+ char *line;
+
+ if (!(line = calloc(1, LINE_SIZE))) {
+ printf("could not allocate memory\n");
+ sigc();
+ }
+
+ while ((fgets(line, LINE_SIZE, gcfg->term))) {
+ printf("%s", line);
+ }
+}