Root/genkicat/comp.c

1/*
2 * comp.c - Component 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#include <stdlib.h>
13#include <stdio.h>
14#include <ctype.h>
15#include <string.h>
16
17#include "run.h"
18#include "libs.h"
19
20
21static const char *field(const char *s, int n)
22{
23    while (n-- && *s) {
24        while (*s && !isspace(*s))
25            s++;
26        while (*s && isspace(*s))
27            s++;
28    }
29    return *s ? s : NULL;
30}
31
32
33static void comp_add_lib(struct lib *lib, const char *path)
34{
35    FILE *file;
36    struct entry *e = NULL;
37    char buf[1024]; /* @@@ */
38    char *name, *spc;
39    const char *s;
40
41    file = fopen(path, "r");
42    if (!file) {
43        perror(path);
44        exit(1);
45    }
46
47    while (fgets(buf, sizeof(buf), file)) {
48        if (!strncmp(buf, "ALIAS ", 6) && e)
49            add_name(e, buf+6);
50        if (strncmp(buf, "DEF ", 4))
51            continue;
52        s = field(buf, 7);
53        if (!s) {
54            fprintf(stderr, "DEF record lacks units field in %s\n",
55                 path);
56            exit(1);
57        }
58        name = buf+4;
59        if (*name == '~')
60            name++;
61        spc = strchr(name, ' ');
62        if (!spc) {
63            fprintf(stderr, "invalid DEF line in %s\n", path);
64            exit(1);
65        }
66        *spc = 0;
67        e = new_entry(lib, atoi(s));
68        if (!e->units) {
69            fprintf(stderr, "invalid number of units in %s\n",
70                path);
71            exit(1);
72        }
73        add_name(e, name);
74    }
75
76    fclose(file);
77}
78
79
80static void comp_ps_entry(FILE *file, const struct lib *lib,
81    const struct entry *e, int unit, int landscape)
82{
83    if (!landscape)
84        fprintf(file, "-120 700 translate -90 rotate\n");
85    run_cmd("sym2xps '%s' '%s' %d '%s' '%s'",
86        e->file->path, e->names->s, unit+1, "tmp", "tmp.ps");
87    cat(file, "tmp.ps");
88}
89
90
91struct lib comp_lib = {
92    .ext = ".lib",
93    .add_lib = comp_add_lib,
94    .ps_entry = comp_ps_entry,
95};
96

Archive Download this file

Branches:
master



interactive