2012-03-31 00:53:51 +03:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-04-17 11:39:32 +03:00
|
|
|
#define _GNU_SOURCE /* for strcasecmp */
|
2012-03-31 00:53:51 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-04-17 11:39:32 +03:00
|
|
|
#include <ctype.h>
|
2012-03-31 00:53:51 +03:00
|
|
|
|
|
|
|
#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;
|
2012-04-17 11:39:32 +03:00
|
|
|
node->comment = NULL;
|
2012-03-31 00:53:51 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-17 11:39:32 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-31 00:53:51 +03:00
|
|
|
void set_libs(struct node *node,
|
2012-04-12 05:10:42 +03:00
|
|
|
const char *(*find_lib)(const char *sym, const char **canon,
|
|
|
|
int *units))
|
2012-03-31 00:53:51 +03:00
|
|
|
{
|
|
|
|
while (node) {
|
|
|
|
if (!node->child) {
|
2012-04-12 05:10:42 +03:00
|
|
|
node->lib =
|
|
|
|
find_lib(node->name, &node->canon, &node->units);
|
2012-03-31 00:53:51 +03:00
|
|
|
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;
|
2012-04-17 11:39:32 +03:00
|
|
|
const struct line *line;
|
2012-03-31 00:53:51 +03:00
|
|
|
|
|
|
|
for (n = tree; n; n = n->next) {
|
|
|
|
printf("%*s%s\n", 4*level, "", n->name);
|
2012-04-17 11:39:32 +03:00
|
|
|
for (line = n->comment; line; line = line->next)
|
|
|
|
printf("%*s\"%s\"\n", 4*level+2, "", line->s);
|
2012-03-31 00:53:51 +03:00
|
|
|
dump_level(n->child, level+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void dump_tree(void)
|
|
|
|
{
|
|
|
|
dump_level(tree, 0);
|
|
|
|
}
|