1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-12-23 19:07:42 +02:00

sch2lib/: avoid global variables for libraries

This commit is contained in:
Werner Almesberger 2016-08-01 11:47:39 -03:00
parent a03e79de7c
commit c6713dfbd8
6 changed files with 66 additions and 53 deletions

View File

@ -20,13 +20,6 @@
#include "lib.h" #include "lib.h"
struct comp *comps = NULL;
static struct comp *curr_comp; /* current component */
static struct comp **next_comp = &comps;
static struct lib_obj **next_obj;
/* ----- Text -------------------------------------------------------------- */ /* ----- Text -------------------------------------------------------------- */
@ -63,7 +56,7 @@ static bool parse_poly(struct lib_poly *poly, const char *line, int points)
/* ----- Definitions ------------------------------------------------------- */ /* ----- Definitions ------------------------------------------------------- */
static bool parse_def(const char *line) static bool parse_def(struct lib *lib, const char *line)
{ {
char *s; char *s;
char draw_num, draw_name; char draw_num, draw_name;
@ -74,23 +67,23 @@ static bool parse_def(const char *line)
&s, &name_offset, &draw_num, &draw_name, &units) != 5) &s, &name_offset, &draw_num, &draw_name, &units) != 5)
return 0; return 0;
curr_comp = alloc_type(struct comp); lib->curr_comp = alloc_type(struct comp);
if (*s == '~') if (*s == '~')
s++; s++;
curr_comp->name = s; lib->curr_comp->name = s;
curr_comp->units = units; lib->curr_comp->units = units;
curr_comp->visible = 0; lib->curr_comp->visible = 0;
curr_comp->show_pin_name = draw_name == 'Y'; lib->curr_comp->show_pin_name = draw_name == 'Y';
curr_comp->show_pin_num = draw_num == 'Y'; lib->curr_comp->show_pin_num = draw_num == 'Y';
curr_comp->name_offset = name_offset; lib->curr_comp->name_offset = name_offset;
curr_comp->objs = NULL; lib->curr_comp->objs = NULL;
next_obj = &curr_comp->objs; lib->next_obj = &lib->curr_comp->objs;
curr_comp->next = NULL; lib->curr_comp->next = NULL;
*next_comp = curr_comp; *lib->next_comp = lib->curr_comp;
next_comp = &curr_comp->next; lib->next_comp = &lib->curr_comp->next;
return 1; return 1;
} }
@ -136,7 +129,7 @@ static bool parse_arc(struct lib_obj *obj, const char *line)
/* ----- Library parser ---------------------------------------------------- */ /* ----- Library parser ---------------------------------------------------- */
bool lib_parse(struct lib_ctx *ctx, const char *line) bool lib_parse(struct lib *lib, const char *line)
{ {
int n = 0; int n = 0;
unsigned points; unsigned points;
@ -145,39 +138,39 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
unsigned zero1, zero2; unsigned zero1, zero2;
char vis; char vis;
ctx->lineno++; lib->lineno++;
switch (ctx->state) { switch (lib->state) {
case lib_skip: case lib_skip:
if (parse_def(line)) { if (parse_def(lib, line)) {
ctx->state = lib_def; lib->state = lib_def;
return 1; return 1;
} }
return 1; return 1;
case lib_def: case lib_def:
if (sscanf(line, "DRAW%n", &n) == 0 && n) { if (sscanf(line, "DRAW%n", &n) == 0 && n) {
ctx->state = lib_draw; lib->state = lib_draw;
return 1; return 1;
} }
if (sscanf(line, "F%d \"\" %*d %*d %*d %*c %c", &n, &vis) == 2 if (sscanf(line, "F%d \"\" %*d %*d %*d %*c %c", &n, &vis) == 2
|| sscanf(line, "F%d \"%*[^\"]\" %*d %*d %*d %*c %c", || sscanf(line, "F%d \"%*[^\"]\" %*d %*d %*d %*c %c",
&n, &vis) == 2) { &n, &vis) == 2) {
if (vis == 'V') if (vis == 'V')
curr_comp->visible |= 1 << n; lib->curr_comp->visible |= 1 << n;
return 1; return 1;
} }
/* @@@ explicitly ignore FPLIST */ /* @@@ explicitly ignore FPLIST */
return 1; return 1;
case lib_draw: case lib_draw:
if (sscanf(line, "ENDDRAW%n", &n) == 0 && n) { if (sscanf(line, "ENDDRAW%n", &n) == 0 && n) {
ctx->state = lib_skip; lib->state = lib_skip;
return 1; return 1;
} }
obj = alloc_type(struct lib_obj); obj = alloc_type(struct lib_obj);
obj->next = NULL; obj->next = NULL;
*next_obj = obj; *lib->next_obj = obj;
next_obj = &obj->next; lib->next_obj = &obj->next;
if (sscanf(line, "P %u %u %u %u %n", if (sscanf(line, "P %u %u %u %u %n",
&points, &obj->unit, &obj->convert, &obj->u.poly.thick, &points, &obj->unit, &obj->convert, &obj->u.poly.thick,
@ -233,7 +226,7 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
if (n == 12) { if (n == 12) {
if (zero1) { if (zero1) {
fprintf(stderr, "%u: only understand 0 x x\n" fprintf(stderr, "%u: only understand 0 x x\n"
"\"%s\"\n", ctx->lineno, line); "\"%s\"\n", lib->lineno, line);
exit(1); exit(1);
} }
obj->u.text.style = decode_style(style); obj->u.text.style = decode_style(style);
@ -253,13 +246,22 @@ bool lib_parse(struct lib_ctx *ctx, const char *line)
default: default:
abort(); abort();
} }
fprintf(stderr, "%u: cannot parse\n\"%s\"\n", ctx->lineno, line); fprintf(stderr, "%u: cannot parse\n\"%s\"\n", lib->lineno, line);
exit(1); exit(1);
} }
void lib_init(struct lib_ctx *ctx) void lib_reset(struct lib *lib)
{ {
ctx->state = lib_skip; lib->state = lib_skip;
ctx->lineno = 0; lib->lineno = 0;
}
void lib_init(struct lib *lib)
{
lib_reset(lib);
lib->comps = NULL;
lib->next_comp = &lib->comps;
} }

View File

@ -364,11 +364,11 @@ static void draw(const struct comp *comp, const struct lib_obj *obj,
} }
const struct comp *lib_find(const char *name) const struct comp *lib_find(const struct lib *lib, const char *name)
{ {
const struct comp *comp; const struct comp *comp;
for (comp = comps; comp; comp = comp->next) for (comp = lib->comps; comp; comp = comp->next)
if (!strcmp(comp->name, name)) if (!strcmp(comp->name, name))
return comp; return comp;
fprintf(stderr, "\"%s\" not found\n", name); fprintf(stderr, "\"%s\" not found\n", name);

View File

@ -23,11 +23,6 @@ enum lib_state {
lib_draw, /* in drawings */ lib_draw, /* in drawings */
}; };
struct lib_ctx {
enum lib_state state;
unsigned lineno;
};
struct lib_obj { struct lib_obj {
enum lib_obj_type { enum lib_obj_type {
lib_obj_poly, lib_obj_poly,
@ -103,15 +98,28 @@ struct comp {
struct comp *next; struct comp *next;
}; };
struct lib {
enum lib_state state;
unsigned lineno;
struct comp *comps;
struct comp *curr_comp; /* current component */
struct comp **next_comp;
struct lib_obj **next_obj;
};
extern struct comp *comps; extern struct comp *comps;
const struct comp *lib_find(const char *name); const struct comp *lib_find(const struct lib *lib, const char *name);
bool lib_field_visible(const struct comp *comp, int n); bool lib_field_visible(const struct comp *comp, int n);
void lib_render(const struct comp *comp, unsigned unit, const int m[6]); void lib_render(const struct comp *comp, unsigned unit, const int m[6]);
bool lib_parse(struct lib_ctx *ctx, const char *line); bool lib_parse(struct lib *lib, const char *line);
void lib_init(struct lib_ctx *ctx); void lib_init(struct lib *lib);
void lib_reset(struct lib *lib);
#endif /* !LIB_H */ #endif /* !LIB_H */

View File

@ -57,6 +57,7 @@ void usage(const char *name)
int main(int argc, char *const *argv) int main(int argc, char *const *argv)
{ {
struct lib lib;
struct sch_ctx sch_ctx; struct sch_ctx sch_ctx;
bool recurse = 0; bool recurse = 0;
char c; char c;
@ -81,11 +82,10 @@ int main(int argc, char *const *argv)
if (dashdash - optind < 1) if (dashdash - optind < 1)
usage(*argv); usage(*argv);
lib_init(&lib);
for (arg = optind; arg != dashdash - 1; arg++) { for (arg = optind; arg != dashdash - 1; arg++) {
struct lib_ctx ctx; lib_reset(&lib);
file_read(argv[arg], do_lib_parse, &lib);
lib_init(&ctx);
file_read(argv[arg], do_lib_parse, &ctx);
} }
if (dashdash == argc) { if (dashdash == argc) {
@ -112,7 +112,7 @@ found:
optind = 0; /* reset getopt */ optind = 0; /* reset getopt */
sch_init(&sch_ctx, recurse); sch_init(&sch_ctx, recurse);
sch_parse(&sch_ctx, argv[dashdash - 1]); sch_parse(&sch_ctx, argv[dashdash - 1], &lib);
gfx_init(*ops, gfx_argc, gfx_argv); gfx_init(*ops, gfx_argc, gfx_argv);
if (recurse) { if (recurse) {
const struct sheet *sheet; const struct sheet *sheet;

View File

@ -461,7 +461,7 @@ static bool parse_line(void *user, const char *line)
return 1; return 1;
} }
if (sscanf(line, "L %ms", &s) == 1) { if (sscanf(line, "L %ms", &s) == 1) {
obj->u.comp.comp = lib_find(s); obj->u.comp.comp = lib_find(ctx->lib, s);
free(s); free(s);
return 1; return 1;
} }
@ -538,7 +538,7 @@ static bool parse_line(void *user, const char *line)
} }
void sch_parse(struct sch_ctx *ctx, const char *file) void sch_parse(struct sch_ctx *ctx, const char *file, const struct lib *lib)
{ {
struct sch_file dsc = { struct sch_file dsc = {
.name = file, .name = file,
@ -547,6 +547,7 @@ void sch_parse(struct sch_ctx *ctx, const char *file)
}; };
ctx->file = &dsc; ctx->file = &dsc;
ctx->lib = lib;
file_read(file, parse_line, ctx); file_read(file, parse_line, ctx);
} }

View File

@ -108,6 +108,8 @@ struct sch_ctx {
struct sheet *sheets; struct sheet *sheets;
struct sheet **next_sheet; struct sheet **next_sheet;
const struct lib *lib;
struct sch_file *file; struct sch_file *file;
}; };
@ -115,7 +117,7 @@ struct sch_ctx {
void decode_alignment(struct text *txt, char hor, char vert); void decode_alignment(struct text *txt, char hor, char vert);
void sch_render(const struct sheet *sheet); void sch_render(const struct sheet *sheet);
void sch_parse(struct sch_ctx *ctx, const char *line); void sch_parse(struct sch_ctx *ctx, const char *line, const struct lib *lib);
void sch_init(struct sch_ctx *ctx, bool recurse); void sch_init(struct sch_ctx *ctx, bool recurse);
#endif /* !SCH_H */ #endif /* !SCH_H */