Root/genkicat/libs.c

1/*
2 * libc.h - Component or module libraries
3 *
4 * Copyright 2012 by Werner Almesberger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#define _GNU_SOURCE /* for strcasecmp */
13#include <stdlib.h>
14#include <stdio.h>
15#include <string.h>
16#include <sys/types.h>
17#include <dirent.h>
18#include <assert.h>
19
20#include "util.h"
21#include "libs.h"
22
23
24const struct entry *lookup_sym(const struct lib *lib, const char *name)
25{
26    const struct file *file;
27    const struct entry *e;
28
29    for (file = lib->files; file; file = file->next)
30        for (e = file->entries; e; e = e->next)
31            if (!strcasecmp(e->names->s, name))
32                return e;
33    return NULL;
34}
35
36
37void add_name(struct entry *e, char *s)
38{
39    char *nl;
40    struct name **p;
41
42    nl = strchr(s, '\n');
43    if (nl)
44        *nl = 0;
45    for (p = &e->names; *p; p = &(*p)->next);
46    *p = alloc_type(struct name);
47    (*p)->s = stralloc(s);
48    (*p)->next = NULL;
49}
50
51
52struct entry *new_entry(struct lib *lib, int units)
53{
54    struct entry *e;
55
56    e = alloc_type(struct entry);
57    e->names = NULL;
58    e->units = units;
59    e->file = lib->files;
60    e->next = lib->files->entries;
61    lib->files->entries = e;
62    return e;
63}
64
65
66void add_lib(struct lib *lib, const char *path)
67{
68    struct file *file;
69
70    file = alloc_type(struct file);
71    file->path = stralloc(path);
72    file->entries = NULL;
73    file->lib = lib;
74    file->next = lib->files;
75    lib->files = file;
76
77    lib->add_lib(lib, path);
78}
79
80
81void add_libdir(struct lib *lib, const char *path)
82{
83    DIR *dir;
84    const struct dirent *de;
85    size_t len;
86    char *tmp;
87
88    assert(strlen(lib->ext) == 4);
89    dir = opendir(path);
90    if (!dir) {
91        perror(path);
92        exit(1);
93    }
94    while (1) {
95        de = readdir(dir);
96        if (!de)
97            break;
98        if (strchr(de->d_name, '~'))
99            continue;
100        len = strlen(de->d_name);
101        if (len < 4)
102            continue;
103        if (strcmp(de->d_name+len-4, lib->ext))
104            continue;
105        if (asprintf(&tmp, "%s/%s", path, de->d_name) < 0) {
106            perror("asprintf");
107            exit(1);
108        }
109        add_lib(lib, tmp);
110    }
111    closedir(dir);
112}
113

Archive Download this file

Branches:
master



interactive