2012-03-31 00:53:51 +03:00
|
|
|
/*
|
|
|
|
* libs.c - Component libraries
|
|
|
|
*
|
|
|
|
* 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-12 05:10:42 +03:00
|
|
|
#define _GNU_SOURCE /* for strcasecmp */
|
2012-03-31 00:53:51 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2012-04-12 05:10:42 +03:00
|
|
|
#include <ctype.h>
|
2012-03-31 00:53:51 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "libs.h"
|
|
|
|
|
|
|
|
|
|
|
|
struct entry {
|
|
|
|
const char *name;
|
2012-04-12 05:10:42 +03:00
|
|
|
int units;
|
2012-03-31 00:53:51 +03:00
|
|
|
struct entry *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct lib {
|
|
|
|
const char *path;
|
|
|
|
struct entry *comps;
|
|
|
|
struct lib *next;
|
|
|
|
} *libs = NULL;
|
|
|
|
|
|
|
|
|
2012-04-12 05:10:42 +03:00
|
|
|
const char *lookup_sym(const char *name, const char **canon, int *units)
|
2012-03-31 00:53:51 +03:00
|
|
|
{
|
|
|
|
const struct lib *lib;
|
|
|
|
const struct entry *e;
|
|
|
|
|
|
|
|
for (lib = libs; lib; lib = lib->next)
|
|
|
|
for (e = lib->comps; e; e = e->next)
|
|
|
|
if (!strcasecmp(e->name, name)) {
|
|
|
|
if (canon)
|
|
|
|
*canon = e->name;
|
2012-04-12 05:10:42 +03:00
|
|
|
if (units)
|
|
|
|
*units = e->units;
|
2012-03-31 00:53:51 +03:00
|
|
|
return lib->path;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-12 05:10:42 +03:00
|
|
|
static const char *field(const char *s, int n)
|
|
|
|
{
|
|
|
|
while (n-- && *s) {
|
|
|
|
while (*s && !isspace(*s))
|
|
|
|
s++;
|
|
|
|
while (*s && isspace(*s))
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return *s ? s : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-31 00:53:51 +03:00
|
|
|
void add_lib(const char *path)
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
struct lib *lib;
|
|
|
|
struct entry *e;
|
|
|
|
char buf[1024]; /* @@@ */
|
|
|
|
char *spc;
|
2012-04-12 05:10:42 +03:00
|
|
|
const char *s;
|
2012-03-31 00:53:51 +03:00
|
|
|
|
|
|
|
file = fopen(path, "r");
|
|
|
|
if (!file) {
|
|
|
|
perror(path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
lib = alloc_type(struct lib);
|
|
|
|
lib->path = stralloc(path);
|
|
|
|
lib->comps = NULL;
|
|
|
|
lib->next = libs;
|
|
|
|
libs = lib;
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), file)) {
|
|
|
|
if (strncmp(buf, "DEF ", 4))
|
|
|
|
continue;
|
2012-04-12 05:10:42 +03:00
|
|
|
s = field(buf, 7);
|
|
|
|
if (!s) {
|
|
|
|
fprintf(stderr, "DEF record lacks units field in %s\n",
|
|
|
|
path);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-31 00:53:51 +03:00
|
|
|
spc = strchr(buf+4, ' ');
|
|
|
|
if (!spc) {
|
|
|
|
fprintf(stderr, "invalid DEF line in %s\n", path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
*spc = 0;
|
|
|
|
e = alloc_type(struct entry);
|
|
|
|
e->name = stralloc(buf+4);
|
2012-04-12 05:10:42 +03:00
|
|
|
e->units = atoi(s);
|
|
|
|
if (!e->units) {
|
|
|
|
fprintf(stderr, "invalid number of units in %s\n",
|
|
|
|
path);
|
|
|
|
exit(1);
|
|
|
|
}
|
2012-03-31 00:53:51 +03:00
|
|
|
e->next = lib->comps;
|
|
|
|
lib->comps = e;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void add_libdir(const char *path)
|
|
|
|
{
|
|
|
|
DIR *dir;
|
|
|
|
const struct dirent *de;
|
|
|
|
size_t len;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
dir = opendir(path);
|
|
|
|
if (!dir) {
|
|
|
|
perror(path);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
de = readdir(dir);
|
|
|
|
if (!de)
|
|
|
|
break;
|
|
|
|
len = strlen(de->d_name);
|
|
|
|
if (len < 4)
|
|
|
|
continue;
|
|
|
|
if (strcmp(de->d_name+len-4, ".lib"))
|
|
|
|
continue;
|
|
|
|
if (asprintf(&tmp, "%s/%s", path, de->d_name) < 0) {
|
|
|
|
perror("asprintf");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
add_lib(tmp);
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
}
|