1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-11-26 13:13:45 +02:00

eeshow/sch-parse.c (recurse_sheet): move parent/related logic to file.c

This commit is contained in:
Werner Almesberger 2016-08-02 16:27:08 -03:00
parent 4721cdea83
commit 24522c45e0
3 changed files with 69 additions and 37 deletions

View File

@ -28,8 +28,69 @@ bool file_cat(const struct file *file, void *user, const char *line)
}
void file_open(struct file *file, const char *name,
const struct file *related)
static bool try_related(struct file *file)
{
const char *related;
const char *slash;
char *tmp;
unsigned len;
if (!file->related)
return 0;
if (*file->name == '/')
return 0;
related = file->related->name;
slash = strrchr(related, '/');
if (!slash)
return 0;
len = slash + 1 - related;
tmp = alloc_size(len + strlen(file->name) + 1);
memcpy(tmp, related, len);
strcpy(tmp + len, file->name);
file->file = fopen(tmp, "r");
if (!file->file) {
free(tmp);
return 0;
}
if (verbose)
fprintf(stderr, "reading %s\n", tmp);
free((char *) file->name);
file->name = tmp;
return 1;
}
/*
* @@@ logic isn't quite complete yet. It should go something like this:
*
* - if there is no related item,
* - try file,
* - if there is a colon, try VCS,
* - give up
* - if there is a related item,
* - if it is a VCS,
* - try the revision matching or predating (if different repo) that of the
* related repo,
( - fall through and try as if it was a file
* - try opening as file. If this fails,
* - if the name of the file to open is absolute, give up
* - try `dirname related`/file_ope_open
* - give up
*
* @@@ should we see if non-VCS file is in a VCS ? E.g.,
* /home/my-libs/foo.lib 1234:/home/my-project/foo.sch
* or maybe use : as indictor for VCS, i.e.,
* :/home/my-libs/foo.lib ...
*
* @@@ explicit revision should always win over related.
*/
void file_open(struct file *file, const char *name, const struct file *related)
{
char *colon, *tmp;
@ -45,6 +106,9 @@ void file_open(struct file *file, const char *name,
return;
}
if (try_related(file))
return;
if (verbose)
perror(name);

View File

@ -296,42 +296,20 @@ static struct sheet *new_sheet(struct sch_ctx *ctx)
static bool parse_line(const struct file *file, void *user, const char *line);
static void recurse_sheet(struct sch_ctx *ctx, const struct file *parent_file)
static void recurse_sheet(struct sch_ctx *ctx, const struct file *related)
{
struct sch_obj **saved_next_obj = ctx->next_obj;
const char *parent = parent_file->name;
const char *name = ctx->obj.u.sheet.file;
char *tmp = NULL;
struct file file;
struct sch_file dsc = {
.parent = ctx->file,
};
/* @@@ clean this up */
if (access(name, R_OK)) {
const char *slash;
slash = strrchr(parent, '/');
if (slash) {
unsigned len = slash + 1 - parent;
tmp = alloc_size(len + strlen(name) + 1);
memcpy(tmp, parent, len);
strcpy(tmp + len, name);
name = tmp;
}
}
/* @@@ clean this up: saved_next_obj is still pending */
new_sheet(ctx);
ctx->file = &dsc;
ctx->state = sch_descr;
file_open(&file, name, NULL);
file_open(&file, name, related);
file_read(&file, parse_line, ctx);
file_close(&file);
ctx->file = dsc.parent;
ctx->next_obj = saved_next_obj;
free(tmp);
}
@ -541,11 +519,7 @@ static bool parse_line(const struct file *file, void *user, const char *line)
void sch_parse(struct sch_ctx *ctx, const char *name, const struct lib *lib)
{
struct file file;
struct sch_file dsc = {
.parent = NULL,
};
ctx->file = &dsc;
ctx->lib = lib;
file_open(&file, name, NULL);
file_read(&file, parse_line, ctx);

View File

@ -91,10 +91,6 @@ struct sheet {
struct sheet *next;
};
struct sch_file {
struct sch_file *parent;
};
struct sch_ctx {
enum sch_state state;
@ -107,8 +103,6 @@ struct sch_ctx {
struct sheet **next_sheet;
const struct lib *lib;
struct sch_file *file;
};