2 * Copyright (c) 2017 Ian Sutton <ian@ce.gl>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #define LINE_SIZE 4096
34 struct sigaction sigact
;
35 static void sigh(int);
36 static void sigc(void);
41 fprintf(stderr
, "usage: puffcrash [-c config] [serial console log]\n");
48 if (sig
== SIGINT
) sigc();
54 if (gcfg
->obj_path
) free(gcfg
->obj_path
);
55 if (gcfg
->tool_path
) free(gcfg
->tool_path
);
56 if (gcfg
->src_path
) free(gcfg
->src_path
);
58 if (gcfg
->conf
) fclose(gcfg
->conf
);
59 if (gcfg
->term
&& gcfg
->term
!= stdin
) fclose(gcfg
->term
);
65 parse_config(char *config_path
, struct config
*cfg
)
70 cfg
->conf
= fopen(config_path
, "r");
72 if(!(line
= calloc(1, 256)) || !cfg
->conf
)
75 while ((fgets(line
, 256, cfg
->conf
))) {
76 if (strstr(line
, "obj_path")) {
77 i
= strstr(line
, "=") + 2;
78 strlcpy(cfg
->obj_path
, i
, PATH_MAX
);
80 } else if (strstr(line
, "tool_path")) {
81 i
= strstr(line
, "=") + 2;
82 strlcpy(cfg
->tool_path
, i
, PATH_MAX
);
84 } else if (strstr(line
, "src_path")) {
85 i
= strstr(line
, "=") + 2;
86 strlcpy(cfg
->src_path
, i
, PATH_MAX
);
104 if (!(line
= calloc(1, LINE_SIZE
))) {
105 printf("could not allocate memory\n");
109 while ((fgets(line
, LINE_SIZE
, gcfg
->term
))) {
115 main(int argc
, char *argv
[])
119 char term_path
[PATH_MAX
], config_path
[PATH_MAX
];
121 bzero(&cfg
, sizeof(struct config
));
122 strlcpy(config_path
, "/etc/puffcrash.conf", PATH_MAX
);
125 sigact
.sa_handler
= sigh
;
126 sigemptyset(&sigact
.sa_mask
);
128 sigaction(SIGINT
, &sigact
, (struct sigaction
*) NULL
);
130 while ((ch
= getopt(argc
, argv
, "c:")) != -1) {
133 strlcpy(config_path
, optarg
, PATH_MAX
);
144 cfg
.obj_path
= calloc(1, PATH_MAX
);
145 cfg
.tool_path
= calloc(1, PATH_MAX
);
146 cfg
.src_path
= calloc(1, PATH_MAX
);
150 if (parse_config(config_path
, &cfg
)) {
151 printf("failed to parse config file %s.\n", config_path
);
156 strlcpy(term_path
, argv
[0], PATH_MAX
);
157 cfg
.term
= fopen(term_path
, "r");
162 printf("failed to open serial console output logfile \"%s\"\n", argv
[0]);