Removed POSIX specific code, got variable values
This commit is contained in:
68
src/parse.c
68
src/parse.c
@@ -1,23 +1,56 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <parse.h>
|
||||
#include <string.h>
|
||||
#include <vector.h>
|
||||
#include <strings.h>
|
||||
|
||||
#include <config.h>
|
||||
|
||||
static int expandfile(struct string *expanded, char *filename, int level);
|
||||
struct var {
|
||||
struct string *var;
|
||||
struct string *value;
|
||||
};
|
||||
|
||||
struct expandfile {
|
||||
struct string *data;
|
||||
struct vector *vars;
|
||||
/* This is a vector of struct var */
|
||||
};
|
||||
|
||||
static int expandfile(struct expandfile *ret, char *filename, int level);
|
||||
static struct string *getstring(FILE *file, char end);
|
||||
|
||||
int parsefile(char *template, FILE *out) {
|
||||
struct string *expanded;
|
||||
expanded = newstring();
|
||||
int i;
|
||||
struct expandfile *expanded;
|
||||
expanded = malloc(sizeof *expanded);
|
||||
if (expanded == NULL)
|
||||
return 1;
|
||||
goto error1;
|
||||
expanded->data = newstring();
|
||||
if (expanded->data == NULL)
|
||||
goto error2;
|
||||
expanded->vars = newvector(struct var);
|
||||
if (expanded->vars == NULL)
|
||||
goto error3;
|
||||
if (expandfile(expanded, template, 0))
|
||||
return 1;
|
||||
fwrite(expanded->data, 1, expanded->len, stdout);
|
||||
goto error4;
|
||||
fwrite(expanded->data->data, 1, expanded->data->len, out);
|
||||
return 0;
|
||||
error4:
|
||||
for (i = 0; i < expanded->vars->len; ++i) {
|
||||
freestring(getvector(expanded->vars, struct var, i).var);
|
||||
freestring(getvector(expanded->vars, struct var, i).value);
|
||||
}
|
||||
freevector(expanded->vars);
|
||||
error3:
|
||||
freestring(expanded->data);
|
||||
error2:
|
||||
free(expanded);
|
||||
error1:
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int expandfile(struct string *expanded, char *filename, int level) {
|
||||
static int expandfile(struct expandfile *ret, char *filename, int level) {
|
||||
FILE *file;
|
||||
int c, linenum;
|
||||
file = fopen(filename, "r");
|
||||
@@ -39,28 +72,37 @@ static int expandfile(struct string *expanded, char *filename, int level) {
|
||||
c = fgetc(file);
|
||||
switch (c) {
|
||||
case ESCAPE_CHAR:
|
||||
if (appendchar(expanded, ESCAPE_CHAR))
|
||||
if (appendchar(ret->data, ESCAPE_CHAR))
|
||||
goto error;
|
||||
if (appendchar(ret->data, ESCAPE_CHAR))
|
||||
goto error;
|
||||
break;
|
||||
case VAR_CHAR:
|
||||
if (appendchar(expanded, ESCAPE_CHAR))
|
||||
if (appendchar(ret->data, ESCAPE_CHAR))
|
||||
goto error;
|
||||
for (;;) {
|
||||
if (c == EOF)
|
||||
goto error;
|
||||
if (appendchar(expanded, c))
|
||||
if (appendchar(ret->data, c))
|
||||
goto error;
|
||||
if (c == ESCAPE_CHAR)
|
||||
break;
|
||||
c = fgetc(file);
|
||||
}
|
||||
break;
|
||||
case SET_CHAR: {
|
||||
struct var var;
|
||||
var.var = getstring(file, ' ');
|
||||
var.value = getstring(file, ESCAPE_CHAR);
|
||||
addvector(ret->vars, &var);
|
||||
break;
|
||||
}
|
||||
case INCLUDE_CHAR: {
|
||||
struct string *inclname;
|
||||
inclname = getstring(file, ESCAPE_CHAR);
|
||||
if (inclname == NULL)
|
||||
goto error;
|
||||
if (expandfile(expanded, inclname->data,
|
||||
if (expandfile(ret, inclname->data,
|
||||
level + 1))
|
||||
return 1;
|
||||
freestring(inclname);
|
||||
@@ -78,7 +120,7 @@ static int expandfile(struct string *expanded, char *filename, int level) {
|
||||
case EOF:
|
||||
goto end;
|
||||
default: casedefault:
|
||||
if (appendchar(expanded, c))
|
||||
if (appendchar(ret->data, c))
|
||||
goto error;
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user