1
0
mirror of git://projects.qi-hardware.com/eda-tools.git synced 2024-07-03 02:17:38 +03:00
eda-tools/genex/comp.c
Werner Almesberger 1b7ace9043 genex: added reading of files containing supplemental descriptions
Each entry has this structure:

component: text
  more text

  more text on a new line
next component: ...

If a component has multiple entries, a line break is placed between
them. Component names are case-insensitive.
2012-04-17 05:54:08 -03:00

240 lines
4.3 KiB
C

/*
* comp.c - Component hierarchy
*
* Copyright 2012 by Werner Almesberger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#define _GNU_SOURCE /* for strcasecmp */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "util.h"
#include "comp.h"
struct node *tree = NULL;
void read_tree(FILE *file)
{
char buf[1100]; /* more than enough */
struct node *last = NULL, *node;
int depth = -1;
const char *s, *p, *e;
char *name;
int n;
next:
while (fgets(buf, sizeof(buf), file)) {
n = 0;
s = buf;
while (1) {
switch (*s++) {
case ' ':
n++;
continue;
case '\t':
n = (n+8) & ~7;
continue;
case 0:
case '#':
goto next;
default:
break;
}
break;
}
for (p = e = s--; *p && *p != '#' && *p != '\n'; p++)
if (*p != ' ' && *p != '\t')
e = p;
if (!last && n) {
fprintf(stderr, "first entry must not be intended\n");
exit(1);
}
name = malloc(e-s+2);
if (!name) {
perror("malloc");
exit(1);
}
memcpy(name, s, e-s+1);
name[e-s+1] = 0;
node = alloc_type(struct node);
node->name = name;
node->lib = NULL;
node->comment = NULL;
node->indent = n;
node->child = node->next = NULL;
if (n > depth) {
if (last)
last->child = node;
else
tree = node;
node->parent = last;
} else {
while (last && last->indent != n)
last = last->parent;
if (!last && n) {
fprintf(stderr, "indentation error\n");
exit(1);
}
last->next = node;
node->parent = last->parent;
}
last = node;
depth = n;
}
}
static struct node *find_comp(struct node *node, const char *name)
{
struct node *found;
while (node) {
if (!strcasecmp(node->name, name))
return node;
found = find_comp(node->child, name);
if (found)
return found;
node = node->next;
}
return NULL;
}
static struct line *new_line(char *s)
{
struct line *line;
line = alloc_type(struct line);
line->s = stralloc(s);
line->next = NULL;
return line;
}
static void append_line(struct line *line, char *s)
{
int len1, len2;
len1 = strlen(line->s);
len2 = strlen(s);
line->s = realloc(line->s, len1+len2+1+1); /* separating space */
if (!line->s) {
perror("realloc");
exit(1);
}
line->s[len1] = ' ';
memcpy(line->s+len1+1, s, len2);
line->s[len1+len2+1] = 0;
}
void read_desc(FILE *file)
{
struct line **anchor = NULL;
char buf[1100]; /* more than enough */
struct node *node;
char *p, *end;
int lineno = 0;
while (fgets(buf, sizeof(buf), file)) {
lineno++;
p = strchr(buf, '\n');
if (p)
*p = 0;
p = buf;
if (*buf && !isspace(*buf)) {
/* tag is ^.*?:\s */
while (1) {
p = strchr(p, ':');
if (!p) {
fprintf(stderr, "no tag in line %d\n",
lineno);
exit(1);
}
if (!p[1] || isspace(p[1]))
break;
p++;
}
*p++ = 0;
node = find_comp(tree, buf);
if (!node) {
fprintf(stderr,
"component \"%s\" not found in line %d\n",
buf, lineno);
exit(1);
}
for (anchor = &node->comment; *anchor;
anchor = &(*anchor)->next);
}
/* remove leading whitespace */
while (*p && isspace(*p))
p++;
if (!*p) {
if (*anchor)
anchor = &(*anchor)->next;
continue;
}
/* remove training whitespace */
end = strrchr(p, 0);
while (isspace(end[-1]))
end--;
*end = 0;
if (*anchor)
append_line(*anchor, p);
else
*anchor = new_line(p);
}
}
void set_libs(struct node *node,
const char *(*find_lib)(const char *sym, const char **canon,
int *units))
{
while (node) {
if (!node->child) {
node->lib =
find_lib(node->name, &node->canon, &node->units);
if (!node->lib) {
fprintf(stderr, "symbol %s not found\n",
node->name);
exit(1);
}
}
set_libs(node->child, find_lib);
node = node->next;
}
}
static void dump_level(const struct node *tree, int level)
{
const struct node *n;
const struct line *line;
for (n = tree; n; n = n->next) {
printf("%*s%s\n", 4*level, "", n->name);
for (line = n->comment; line; line = line->next)
printf("%*s\"%s\"\n", 4*level+2, "", line->s);
dump_level(n->child, level+1);
}
}
void dump_tree(void)
{
dump_level(tree, 0);
}