コード例 #1
0
ファイル: vtc.c プロジェクト: BMDan/Varnish-Cache
struct vsb *
macro_expand(struct vtclog *vl, const char *text)
{
	struct vsb *vsb;
	const char *p, *q;
	char *m;

	vsb = VSB_new_auto();
	AN(vsb);
	while (*text != '\0') {
		p = strstr(text, "${");
		if (p == NULL) {
			VSB_cat(vsb, text);
			break;
		}
		VSB_bcat(vsb, text, p - text);
		q = strchr(p, '}');
		if (q == NULL) {
			VSB_cat(vsb, text);
			break;
		}
		assert(p[0] == '$');
		assert(p[1] == '{');
		assert(q[0] == '}');
		p += 2;
		m = macro_get(p, q);
		if (m == NULL) {
			VSB_delete(vsb);
			vtc_log(vl, 0, "Macro ${%.*s} not found", (int)(q - p),
			    p);
			return (NULL);
		}
		VSB_printf(vsb, "%s", m);
		free(m);
		text = q + 1;
	}
	AZ(VSB_finish(vsb));
	return (vsb);
}
コード例 #2
0
ファイル: include_file.c プロジェクト: vhelin/wla-dx
int incbin_file(char *name, int *id, int *swap, int *skip, int *read, struct macro_static **macro) {

    struct incbin_file_data *ifd;
    char *in_tmp, *n, *tmp_c;
    int file_size, q;
    FILE *f;


    /* create the full output file name */
    if (use_incdir == YES)
        tmp_c = ext_incdir;
    else
        tmp_c = include_dir;

    if (create_full_name(tmp_c, name) == FAILED)
        return FAILED;

    f = fopen(full_name, "rb");
    q = 0;

    if (f == NULL && (tmp_c == NULL || tmp_c[0] == 0)) {
        sprintf(emsg, "Error opening file \"%s\".\n", name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* if not found in ext_incdir silently try the include directory */
    if (f == NULL && use_incdir == YES) {
        if (create_full_name(include_dir, name) == FAILED)
            return FAILED;

        f = fopen(full_name, "rb");
        q = 0;

        if (f == NULL && (include_dir == NULL || include_dir[0] == 0)) {
            sprintf(emsg, "Error opening file \"%s\".\n", name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* if failed try to find the file in the current directory */
    if (f == NULL) {
        fprintf(stderr, "%s:%d: ", get_file_name(active_file_info_last->filename_id), active_file_info_last->line_current);
        fprintf(stderr, "INCBIN_FILE: Could not open \"%s\", trying \"%s\"... ", full_name, name);
        f = fopen(name, "rb");
        q = 1;
    }

    if (f == NULL) {
        fprintf(stderr, "not found.\n");
        sprintf(emsg, "Error opening file \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    if (q == 1) {
        fprintf(stderr, "found.\n");
        strcpy(full_name, name);
    }

    fseek(f, 0, SEEK_END);
    file_size = ftell(f);
    fseek(f, 0, SEEK_SET);

    ifd = (struct incbin_file_data *)malloc(sizeof(struct incbin_file_data));
    n = malloc(sizeof(char) * (strlen(full_name)+1));
    in_tmp = (char *)malloc(sizeof(char) * file_size);
    if (ifd == NULL || n == NULL || in_tmp == NULL) {
        if (ifd != NULL)
            free(ifd);
        if (n != NULL)
            free(n);
        if (in_tmp != NULL)
            free(in_tmp);
        sprintf(emsg, "Out of memory while allocating data structure for \"%s\".\n", full_name);
        print_error(emsg, ERROR_INB);
        return FAILED;
    }

    /* read the whole file into a buffer */
    fread(in_tmp, 1, file_size, f);
    fclose(f);

    /* complete the structure */
    ifd->next = NULL;
    ifd->size = file_size;
    strcpy(n, full_name);
    ifd->name = n;
    ifd->data = in_tmp;

    /* find the index */
    q = 0;
    if (incbin_file_data_first != NULL) {
        ifd_tmp = incbin_file_data_first;
        while (ifd_tmp->next != NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp = ifd_tmp->next;
            q++;
        }
        if (ifd_tmp->next == NULL && strcmp(ifd_tmp->name, full_name) != 0) {
            ifd_tmp->next = ifd;
            q++;
        }
    }
    else
        incbin_file_data_first = ifd;

    *id = q;

    /* SKIP bytes? */
    if (compare_next_token("SKIP", 4) == FAILED)
        *skip = 0;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of skipped bytes.\n", ERROR_DIR);
            return FAILED;
        }

        *skip = d;

        if (d >= file_size) {
            sprintf(emsg, "SKIP value (%d) is more than the size (%d) of file \"%s\".\n", d, file_size, full_name);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* READ bytes? */
    if (compare_next_token("READ", 4) == FAILED)
        *read = file_size - *skip;
    else {
        skip_next_token();
        inz = input_number();
        if (inz != SUCCEEDED) {
            print_error(".INCBIN needs the amount of bytes for reading.\n", ERROR_DIR);
            return FAILED;
        }

        *read = d;

        if (*skip + *read > file_size) {
            sprintf(emsg, "Overreading file \"%s\" by %d bytes.\n", full_name, *skip + *read - file_size);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }

    /* SWAP bytes? */
    if (compare_next_token("SWAP", 4) == FAILED)
        *swap = 0;
    else {
        if ((*read & 1) == 1) {
            sprintf(emsg, "The read size of file \"%s\" is odd (%d)! Cannot perform SWAP.\n", full_name, *read);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
        *swap = 1;
        skip_next_token();
    }

    /* FSIZE? */
    if (compare_next_token("FSIZE", 5) == SUCCEEDED) {
        skip_next_token();

        /* get the definition label */
        if (get_next_token() == FAILED)
            return FAILED;

        add_a_new_definition(tmp, (double)file_size, NULL, DEFINITION_TYPE_VALUE, 0);
    }

    /* FILTER? */
    if (compare_next_token("FILTER", 6) == SUCCEEDED) {
        skip_next_token();

        /* get the filter macro name */
        if (get_next_token() == FAILED)
            return FAILED;

        *macro = macro_get(tmp);

        if (*macro == NULL) {
            sprintf(emsg, "No MACRO \"%s\" defined.\n", tmp);
            print_error(emsg, ERROR_INB);
            return FAILED;
        }
    }
    else
        *macro = NULL;

    return SUCCEEDED;
}