view files.c @ 9:1fbcbd58742e

move destroyall to array.h
author David A. Holland
date Sun, 19 Dec 2010 19:19:02 -0500
parents 97243badae69
children 800f3a560a3b
line wrap: on
line source

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>

#include "array.h"
#include "place.h"
#include "files.h"

struct incdir {
	const char *name;
	bool issystem;
};

struct seenfile {
	struct place includedfrom;
	char *name;
	bool fromsystemdir;
};

DECLARRAY(incdir);
DECLARRAY(seenfile);
DEFARRAY(incdir, );
DEFARRAY(seenfile, );

static struct incdirarray quotepath, bracketpath;
static struct seenfilearray seenfiles;

////////////////////////////////////////////////////////////
// management

static
struct incdir *
incdir_create(const char *name, bool issystem)
{
	struct incdir *id;

	id = domalloc(sizeof(*id));
	id->name = name;
	id->issystem = issystem;
	return id;
}

static
void
incdir_destroy(struct incdir *id)
{
	free(id);
}

static
struct seenfile *
seenfile_create(const struct place *from, char *name, bool fromsystemdir)
{
	struct seenfile *sf;

	sf = domalloc(sizeof(*sf));
	sf->includedfrom = *from;
	sf->name = name;
	sf->fromsystemdir = fromsystemdir;
	return sf;
}

static
void
seenfile_destroy(struct seenfile *sf)
{
	free(sf->name);
	free(sf);
}

void
files_init(void)
{
	incdirarray_init(&quotepath);
	incdirarray_init(&bracketpath);
}

DESTROYALL_ARRAY(incdir, );
DESTROYALL_ARRAY(seenfile, );

void
files_cleanup(void)
{
	seenfilearray_destroyall(&seenfiles);
	seenfilearray_cleanup(&seenfiles);

	incdirarray_destroyall(&quotepath);
	incdirarray_cleanup(&quotepath);
	incdirarray_destroyall(&bracketpath);
	incdirarray_cleanup(&bracketpath);
}

////////////////////////////////////////////////////////////
// path setup

void
files_addquotepath(const char *dir, bool issystem)
{
	struct incdir *id;

	id = incdir_create(dir, issystem);
	incdirarray_add(&quotepath, id, NULL);
}

void
files_addbracketpath(const char *dir, bool issystem)
{
	struct incdir *id;

	id = incdir_create(dir, issystem);
	incdirarray_add(&bracketpath, id, NULL);
}

////////////////////////////////////////////////////////////
// seenfile functions exposed for places.c

const char *
seenfile_getname(const struct seenfile *file)
{
	return file->name;
}

const struct place *
seenfile_getincludeplace(const struct seenfile *file)
{
	return &file->includedfrom;
}

////////////////////////////////////////////////////////////
// parsing

void
file_read(struct seenfile *sf, int fd);

////////////////////////////////////////////////////////////
// path search

static
int
file_tryopen(const char *file)
{
	int fd;

	fd = open(file, O_RDONLY);
	if (fd < 0) {
		return -1;
	}
	/* XXX: do we need to do anything here or is this function pointless?*/
	return fd;
}

static
void
file_search(struct place *place, struct incdirarray *path, const char *name)
{
	unsigned i, num;
	struct incdir *id;
	struct seenfile *sf;
	char *file;
	int fd;

	assert(place != NULL);

	num = incdirarray_num(path);
	for (i=0; i<num; i++) {
		id = incdirarray_get(path, i);
		file = dostrdup3(id->name, "/", name);
		fd = file_tryopen(file);
		if (fd >= 0) {
			sf = seenfile_create(place, file, id->issystem);
			seenfilearray_add(&seenfiles, sf, NULL);
			file_read(sf, fd);
			close(fd);
			return;
		}
		free(file);
	}
	complain(place, "Include file %s not found", name);
	complain_fail();
}

void
file_readquote(struct place *place, const char *name)
{
	file_search(place, &quotepath, name);
}

void
file_readbracket(struct place *place, const char *name)
{
	file_search(place, &bracketpath, name);
}

void
file_readabsolute(struct place *place, const char *name)
{
	struct seenfile *sf;
	int fd;

	assert(place != NULL);

	fd = file_tryopen(name);
	if (fd < 0) {
		warn("%s", name);
		die();
	}
	sf = seenfile_create(place, dostrdup(name), false);
	seenfilearray_add(&seenfiles, sf, NULL);
	file_read(sf, fd);
	close(fd);
}