mirror of
git://projects.qi-hardware.com/eda-tools.git
synced 2024-12-02 03:24:59 +02:00
120 lines
2.0 KiB
C
120 lines
2.0 KiB
C
|
/*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
#define _GNU_SOURCE
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <dirent.h>
|
||
|
|
||
|
#include "util.h"
|
||
|
#include "libs.h"
|
||
|
|
||
|
|
||
|
struct entry {
|
||
|
const char *name;
|
||
|
struct entry *next;
|
||
|
};
|
||
|
|
||
|
static struct lib {
|
||
|
const char *path;
|
||
|
struct entry *comps;
|
||
|
struct lib *next;
|
||
|
} *libs = NULL;
|
||
|
|
||
|
|
||
|
const char *lookup_sym(const char *name, const char **canon)
|
||
|
{
|
||
|
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;
|
||
|
return lib->path;
|
||
|
}
|
||
|
return NULL;
|
||
|
}
|
||
|
|
||
|
|
||
|
void add_lib(const char *path)
|
||
|
{
|
||
|
FILE *file;
|
||
|
struct lib *lib;
|
||
|
struct entry *e;
|
||
|
char buf[1024]; /* @@@ */
|
||
|
char *spc;
|
||
|
|
||
|
|
||
|
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;
|
||
|
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);
|
||
|
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);
|
||
|
}
|