5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 #include <sys/types.h>
28 bool opts_memchk = true;
29 bool opts_debug = false;
35 #define TASK_COMPILE 0
36 #define TASK_EXECUTE 1
39 * Task template system:
40 * templates are rules for a specific test, used to create a "task" that
41 * is executed with those set of rules (arguments, and what not). Tests
42 * that don't have a template with them cannot become tasks, since without
43 * the information for that test there is no way to properly "test" them.
44 * Rules for these templates are described in a template file, using a
45 * task template language.
47 * The language is a basic finite statemachine, top-down single-line
48 * description language.
50 * The languge is composed entierly of "tags" which describe a string of
51 * text for a task. Think of it much like a configuration file. Except
52 * it's been designed to allow flexibility and future support for prodecual
55 * The following "tags" are suported by the language
58 * Used to set a description of the current test, this must be
59 * provided, this tag is NOT optional.
62 * Used to set a failure message, this message will be displayed
63 * if the test fails, this tag is optional
66 * Used to set a success message, this message will be displayed
67 * if the test succeeds, this tag is optional.
70 * Used to set the procedure for the given task, there are two
73 * This simply performs compilation only
75 * This will perform compilation and execution
77 * This must be provided, this tag is NOT optional.
80 * Used to set the compilation flags for the given task, this
81 * must be provided, this tag is NOT optional.
84 * Used to set the execution flags for the given task. This tag
85 * must be provided if T == -execute, otherwise it's erroneous
86 * as compilation only takes place.
89 * Used to describe a string of text that should be matched from
90 * the output of executing the task. If this doesn't match the
91 * task fails. This tag must be provided if T == -execute, otherwise
92 * it's erroneous as compilation only takes place.
95 * Used to specify the INPUT source file to operate on, this must be
96 * provided, this tag is NOT optional.
99 * These tags have one-time use, using them more than once will result
100 * in template compilation errors.
102 * Lines beginning with # or // in the template file are comments and
103 * are ignored by the template parser.
105 * Whitespace is optional, with exception to the colon ':' between the
106 * tag and it's assignment value/
108 * The template compiler will detect erronrous tags (optional tags
109 * that need not be set), as well as missing tags, and error accordingly
110 * this will result in the task failing.
114 char *failuremessage;
115 char *successmessage;
125 * This is very much like a compiler code generator :-). This generates
126 * a value from some data observed from the compiler.
128 bool task_template_generate(task_template_t *template, char tag, const char *file, size_t line, const char *value) {
129 char **destval = NULL;
135 case 'D': destval = &template->description; break;
136 case 'F': destval = &template->failuremessage; break;
137 case 'S': destval = &template->successmessage; break;
138 case 'T': destval = &template->proceduretype; break;
139 case 'C': destval = &template->compileflags; break;
140 case 'E': destval = &template->executeflags; break;
141 case 'M': destval = &template->comparematch; break;
142 case 'I': destval = &template->sourcefile; break;
144 con_printmsg(LVL_ERROR, __FILE__, __LINE__, "internal error",
145 "invalid tag `%c:` during code generation\n",
152 * Ensure if for the given tag, there already exists a
156 con_printmsg(LVL_ERROR, file, line, "compile error",
157 "tag `%c:` already assigned value: %s\n",
164 * Strip any whitespace that might exist in the value for assignments
167 if (value && *value && (*value == ' ' || *value == '\t'))
171 * Value will contain a newline character at the end, we need to strip
172 * this otherwise kaboom, seriously, kaboom :P
174 *strrchr(value, '\n')='\0';
177 * Now allocate and set the actual value for the specific tag. Which
178 * was properly selected and can be accessed with *destval.
180 *destval = util_strdup(value);
185 bool task_template_parse(const char *file, task_template_t *template, FILE *fp) {
194 /* top down parsing */
195 while (util_getline(&back, &size, fp) != EOF) {
196 /* skip whitespace */
198 if (*data && (*data == ' ' || *data == '\t'))
203 * Handle comments inside task template files. We're strict
204 * about the language for fun :-)
207 if (data[1] != '/') {
208 con_printmsg(LVL_ERROR, file, line, "template parse error",
209 "invalid character `/`, perhaps you meant `//` ?");
218 * Empty newlines are acceptable as well, so we handle that here
219 * despite being just odd since there should't be that many
220 * empty lines to begin with.
228 * Now begin the actual "tag" stuff. This works as you expect
239 if (data[1] != ':') {
240 con_printmsg(LVL_ERROR, file, line, "template parse error",
241 "expected `:` after `%c`",
246 if (!task_template_generate(template, *data, file, line, &data[3])) {
247 con_printmsg(LVL_ERROR, file, line, "template compile error",
248 "failed to generate for given task\n"
255 con_printmsg(LVL_ERROR, file, line, "template parse error",
256 "invalid tag `%c`", *data
259 /* no break required */
262 /* update line and free old sata */
278 * Nullifies the template data: used during initialization of a new
281 void task_template_nullify(task_template_t *template) {
285 template->description = NULL;
286 template->failuremessage = NULL;
287 template->successmessage = NULL;
288 template->proceduretype = NULL;
289 template->compileflags = NULL;
290 template->executeflags = NULL;
291 template->comparematch = NULL;
292 template->sourcefile = NULL;
293 template->tempfilename = NULL;
296 task_template_t *task_template_compile(const char *file, const char *dir) {
297 /* a page should be enough */
299 FILE *tempfile = NULL;
300 task_template_t *template = NULL;
302 memset (fullfile, 0, sizeof(fullfile));
303 snprintf(fullfile, sizeof(fullfile), "%s/%s", dir, file);
305 tempfile = fopen(fullfile, "r");
306 template = mem_a(sizeof(task_template_t));
307 task_template_nullify(template);
310 * Esnure the file even exists for the task, this is pretty useless
314 con_err("template file: %s does not exist or invalid permissions\n",
320 if (!task_template_parse(file, template, tempfile)) {
321 con_err("template parse error: error during parsing\n");
326 * Regardless procedure type, the following tags must exist:
332 if (!template->description) {
333 con_err("template compile error: %s missing `D:` tag\n", file);
336 if (!template->proceduretype) {
337 con_err("template compile error: %s missing `T:` tag\n", file);
340 if (!template->compileflags) {
341 con_err("template compile error: %s missing `C:` tag\n", file);
344 if (!template->sourcefile) {
345 con_err("template compile error: %s missing `I:` tag\n", file);
350 * Now lets compile the template, compilation is really just
351 * the process of validating the input.
353 if (!strcmp(template->proceduretype, "-compile")) {
354 if (template->executeflags)
355 con_err("template compile warning: %s erroneous tag `E:` when only compiling\n", file);
356 if (template->comparematch)
357 con_err("template compile warning: %s erroneous tag `M:` when only compiling\n", file);
359 } else if (!strcmp(template->proceduretype, "-execute")) {
360 if (!template->executeflags) {
361 con_err("template compile error: %s missing `E:` tag (use `$null` for exclude)\n", file);
364 if (!template->comparematch) {
365 con_err("template compile error: %s missing `M:` tag (use `$null` for exclude)\n", file);
369 con_err("template compile error: %s invalid procedure type: %s\n", file, template->proceduretype);
379 * The file might not exist and we jump here when that doesn't happen
380 * so the check to see if it's not null here is required.
389 void task_template_destroy(task_template_t **template) {
393 if ((*template)->description) mem_d((*template)->description);
394 if ((*template)->failuremessage) mem_d((*template)->failuremessage);
395 if ((*template)->successmessage) mem_d((*template)->successmessage);
396 if ((*template)->proceduretype) mem_d((*template)->proceduretype);
397 if ((*template)->compileflags) mem_d((*template)->compileflags);
398 if ((*template)->executeflags) mem_d((*template)->executeflags);
399 if ((*template)->comparematch) mem_d((*template)->comparematch);
400 if ((*template)->sourcefile) mem_d((*template)->sourcefile);
403 * Nullify all the template members otherwise NULL comparision
404 * checks will fail if template pointer is reused.
407 task_template_nullify(*template);
412 * Now comes the task manager, this system allows adding tasks in and out
413 * of a task list. This is the executor of the tasks essentially as well.
416 task_template_t *template;
420 task_t *task_tasks = NULL;
423 * Read a directory and searches for all template files in it
424 * which is later used to run all tests.
426 bool task_propogate(const char *curdir) {
429 struct dirent *files;
430 struct stat directory;
433 dir = opendir(curdir);
435 while ((files = readdir(dir))) {
436 memset (buffer, 0,sizeof(buffer));
437 snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
439 if (stat(buffer, &directory) == -1) {
440 con_err("internal error: stat failed, aborting\n");
444 /* skip directories */
445 if (S_ISDIR(directory.st_mode))
449 * We made it here, which concludes the file/directory is not
450 * actually a directory, so it must be a file :)
452 if (strstr(files->d_name, ".tmpl")) {
453 con_out("compiling task template: %s/%s\n", curdir, files->d_name);
454 task_template_t *template = task_template_compile(files->d_name, curdir);
456 con_err("error compiling task template: %s\n", files->d_name);
461 * Generate a temportary file name for the output binary
462 * so we don't trample over an existing one.
464 template->tempfilename = tempnam(curdir, "TMPDAT");
468 * Generate the command required to open a pipe to a process
469 * which will be refered to with a handle in the task for
470 * reading the data from the pipe.
472 char buf[4096]; /* one page should be enough */
473 memset (buf,0,sizeof(buf));
474 snprintf(buf, sizeof(buf), "%s %s/%s %s -o %s",
475 task_bins[TASK_COMPILE],
477 template->sourcefile,
478 template->compileflags,
479 template->tempfilename
483 * The task template was compiled, now lets create a task from
484 * the template data which has now been propogated.
487 task.template = template;
488 if (!(task.handle = popen(buf, "r"))) {
489 con_err("error opening pipe to process for test: %s\n", template->description);
493 con_out("executing test: `%s` [%s]\n", template->description, buf);
495 vec_push(task_tasks, task);
504 * Removes all temporary 'progs.dat' files created during compilation
507 void task_cleanup(const char *curdir) {
509 struct dirent *files;
512 dir = opendir(curdir);
514 while ((files = readdir(dir))) {
515 memset(buffer, 0, sizeof(buffer));
516 if (strstr(files->d_name, "TMP")) {
517 snprintf(buffer, sizeof(buffer), "%s/%s", curdir, files->d_name);
519 con_err("error removing temporary file: %s\n", buffer);
521 con_out("removed temporary file: %s\n", buffer);
528 void task_destroy(const char *curdir) {
530 * Free all the data in the task list and finally the list itself
531 * then proceed to cleanup anything else outside the program like
535 for (i = 0; i < vec_size(task_tasks); i++)
536 task_template_destroy(&task_tasks[i].template);
537 vec_free(task_tasks);
540 * Cleanup outside stuff like temporary files.
542 task_cleanup(curdir);
546 * This executes the QCVM task for a specificly compiled progs.dat
547 * using the template passed into it for call-flags and user defined
550 bool task_execute(task_template_t *template) {
551 bool success = false;
554 memset (buffer,0,sizeof(buffer));
557 * Drop the execution flags for the QCVM if none where
558 * actually specified.
560 if (!strcmp(template->executeflags, "$null")) {
561 snprintf(buffer, sizeof(buffer), "%s %s",
562 task_bins[TASK_EXECUTE],
563 template->tempfilename
566 snprintf(buffer, sizeof(buffer), "%s %s %s",
567 task_bins[TASK_EXECUTE],
568 template->executeflags,
569 template->tempfilename
573 con_out("executing qcvm: `%s` [%s]\n",
574 template->description,
578 execute = popen(buffer, "r");
583 * Now lets read the lines and compare them to the matches we expect
584 * and handle accordingly.
589 while (util_getline(&data, &size, execute) != EOF) {}
591 if (!strcmp(data, "No main function found\n")) {
592 con_err("test failure: `%s` [%s] (No main function found)\n",
593 template->description,
594 (template->failuremessage) ?
595 template->failuremessage : "unknown"
602 * Trim newlines from data since they will just break our
603 * ability to properly validate matches.
605 if (strrchr(data, '\n'))
606 *strrchr(data, '\n') = '\0';
609 if (!strcmp(template->comparematch, "$null"))
613 * We only care about the last line from the output for now
614 * implementing multi-line match is TODO.
616 if (!strcmp(data, template->comparematch))
624 * This schedualizes all tasks and actually runs them individually
625 * this is generally easy for just -compile variants. For compile and
626 * execution this takes more work since a task needs to be generated
627 * from thin air and executed INLINE.
629 void task_schedualize(const char *curdir) {
630 bool execute = false;
636 for (i = 0; i < vec_size(task_tasks); i++) {
638 * Generate a task from thin air if it requires execution in
641 if (!strcmp(task_tasks[i].template->proceduretype, "-execute"))
644 while (util_getline(&data, &size, task_tasks[i].handle) != EOF) {
646 /* chances are we want to print errors */
647 if (strstr(data, "error")) {
648 con_out("compile failed: %s\n",
649 /* strip the newline from the end */
650 (*strrchr(data, '\n')='\0')
654 * The compilation failed which means it cannot be executed
655 * as the file simply will not exist.
665 * If we can execute we do so after all data has been read and
666 * this paticular task has coupled execution in its procedure type
672 * If we made it here that concludes the task is to be executed
673 * in the virtual machine.
675 if (!task_execute(task_tasks[i].template)) {
676 con_err("test failure: `%s` [%s]\n",
677 task_tasks[i].template->description,
678 (task_tasks[i].template->failuremessage) ?
679 task_tasks[i].template->failuremessage : "unknown"
684 con_out("test succeed: `%s` [%s]\n",
685 task_tasks[i].template->description,
686 (task_tasks[i].template->successmessage) ?
687 task_tasks[i].template->successmessage : "unknown"
694 int main(int argc, char **argv) {
696 if (!task_propogate("tests")) {
697 con_err("error: failed to propogate tasks\n");
698 task_destroy("tests");
702 * If we made it here all tasks where propogated from their resultant
703 * template file. So we can start the FILO scheduler, this has been
704 * designed in the most thread-safe way possible for future threading
705 * it's designed to prevent lock contention, and possible syncronization
708 task_schedualize("tests");
709 task_destroy("tests");