// Parse a quoted name and value,
// and add them as env vars to the Setup environment.
static int parse_env(TestSetup* setup, const char* line, int line_len) {
char* name = parse_qstring(&line);
char* value = parse_qstring(&line);
env_addpair(&setup->env, name, value);
free(name);
free(value);
return 1;
}
void free_test_setup(TestSetup* setup) {
if (setup->textfile != NULL) {
unlink(setup->textfile);
free(setup->textfile);
}
int i;
for (i = 1; i < setup->argc; ++i)
free(setup->argv[i]);
free((void*)setup->argv);
free(setup);
}
// Read a newline-terminated line from a file and store it
// as a null-terminated string without the newline.
int read_zline(FILE* fd, char* line, int line_len) {
int nread = 0;
while (nread < line_len-1) {
int ch = fgetc(fd);
if (ch == EOF) return -1;
if (ch == '\n') break;
line[nread++] = (char) ch;
}
line[nread] = '\0';
return nread;
}
// Read the header of a .lt file (up to the R line).
TestSetup* read_test_setup(FILE* fd, const char* less) {
TestSetup* setup = new_test_setup();
int hdr_complete = 0;
while (!hdr_complete) {
char line[10000];
int line_len = read_zline(fd, line, sizeof(line));
if (line_len < 0)
break;
if (line_len < 1)
continue;
switch (line[0]) {
case '!': // file header
break;
case 'T': // test header
break;
case 'R': // end of test header; start run
hdr_complete = 1;
break;
case 'E': // environment variable
if (!parse_env(setup, line+1, line_len-1)) {
free_test_setup(setup);
return NULL;
}
break;
case 'F': // text file
if (!parse_textfile(setup, line+1, line_len-1, fd)) {
free_test_setup(setup);
return NULL;
}
break;
case 'A': // less cmd line parameters
if (!parse_command(setup, less, line+1, line_len-1)) {
free_test_setup(setup);
return NULL;
}
break;
default:
break;
}
}
if (setup->textfile == NULL || setup->argv == NULL) {
free_test_setup(setup);
return NULL;
}
if (verbose) { fprintf(stderr, "setup: textfile %s\n", setup->textfile); print_strings("argv:", setup->argv); }
return setup;
}