FreeCalypso > hg > ueda-linux
comparison ueda/utils/instfileelem.c @ 0:cd92449fdb51
initial import of ueda and ifctf-part-lib from ifctfvax CVS
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Mon, 20 Jul 2015 00:24:37 +0000 |
parents | |
children | e2130f1ef720 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cd92449fdb51 |
---|---|
1 /* | |
2 * This program instantiates a file element, i.e., locates it by name and emits | |
3 * it on stdout with the refdes filled in. The value can also be filled in | |
4 * (optionally). | |
5 */ | |
6 | |
7 #include <sys/param.h> | |
8 #include <ctype.h> | |
9 #include <stdio.h> | |
10 #include <strings.h> | |
11 | |
12 char dirlist_pathname[] = "/usr/local/eda/file-element-dirs"; | |
13 char *fpname, *refdes, *value; | |
14 char fileelem_pathname[MAXPATHLEN]; | |
15 | |
16 main(argc, argv) | |
17 char **argv; | |
18 { | |
19 register FILE *lf, *ef; | |
20 char linebuf[1024]; | |
21 register char *cp; | |
22 | |
23 if (argc < 3 || argc > 4) { | |
24 fprintf(stderr, "usage: %s footprint refdes [value]\n", | |
25 argv[0]); | |
26 exit(1); | |
27 } | |
28 fpname = argv[1]; | |
29 refdes = argv[2]; | |
30 value = argv[3]; | |
31 | |
32 lf = fopen(dirlist_pathname, "r"); | |
33 if (!lf) { | |
34 perror(dirlist_pathname); | |
35 exit(1); | |
36 } | |
37 for (ef = NULL; fgets(linebuf, sizeof linebuf, lf); ) { | |
38 if (linebuf[0] == '\0' || isspace(linebuf[0])) | |
39 continue; | |
40 cp = index(linebuf, '\0'); | |
41 while (isspace(cp[-1])) | |
42 cp--; | |
43 *cp = '\0'; | |
44 sprintf(fileelem_pathname, "%s/%s", linebuf, fpname); | |
45 if (ef = fopen(fileelem_pathname, "r")) | |
46 break; | |
47 sprintf(fileelem_pathname, "%s/%s.fp", linebuf, fpname); | |
48 if (ef = fopen(fileelem_pathname, "r")) | |
49 break; | |
50 } | |
51 fclose(lf); | |
52 if (!ef) { | |
53 fprintf(stderr, "No footprint named %s\n", fpname); | |
54 exit(1); | |
55 } | |
56 | |
57 while (fgets(linebuf, sizeof linebuf, ef)) { | |
58 for (cp = linebuf; isspace(*cp); cp++) | |
59 ; | |
60 if (!strncmp(cp, "Element", 7) && | |
61 (cp[7] == '(' || cp[7] == '[' || isspace(cp[7]))) | |
62 do_element_line(cp + 7); | |
63 else | |
64 fputs(linebuf, stdout); | |
65 } | |
66 fclose(ef); | |
67 exit(0); | |
68 } | |
69 | |
70 do_element_line(cp) | |
71 register char *cp; | |
72 { | |
73 char opench, closech; | |
74 char *fields[11], refdesq[64], valueq[64]; | |
75 int nfields; | |
76 register int c; | |
77 char *err; | |
78 | |
79 while (isspace(*cp)) | |
80 cp++; | |
81 opench = *cp++; | |
82 switch (opench) { | |
83 case '(': | |
84 closech = ')'; | |
85 break; | |
86 case '[': | |
87 closech = ']'; | |
88 break; | |
89 default: | |
90 err = "no valid opening char"; | |
91 inv: fprintf(stderr, "%s: invalid Element line: %s\n", | |
92 fileelem_pathname, err); | |
93 exit(1); | |
94 } | |
95 | |
96 for (nfields = 0; ; ) { | |
97 while (isspace(*cp)) | |
98 cp++; | |
99 if (!*cp) { | |
100 badeol: err = "missing closing char"; | |
101 goto inv; | |
102 } | |
103 if (*cp == closech) { | |
104 cp++; | |
105 break; | |
106 } | |
107 if (nfields >= 11) { | |
108 err = "too many fields"; | |
109 goto inv; | |
110 } | |
111 fields[nfields++] = cp; | |
112 while ((c = *cp) && !isspace(c) && c != closech) | |
113 cp++; | |
114 if (!c) | |
115 goto badeol; | |
116 *cp++ = '\0'; | |
117 if (c == closech) | |
118 break; | |
119 } | |
120 | |
121 sprintf(refdesq, "\"%s\"", refdes); | |
122 if (value) | |
123 sprintf(valueq, "\"%s\"", value); | |
124 if (nfields == 11 || nfields == 9) { | |
125 fields[2] = refdesq; | |
126 if (value) | |
127 fields[3] = valueq; | |
128 } else if (nfields == 8) | |
129 fields[2] = refdesq; | |
130 else if (nfields == 7) | |
131 fields[1] = refdesq; | |
132 else { | |
133 err = "unrecognized format"; | |
134 goto inv; | |
135 } | |
136 | |
137 printf("Element%c", opench); | |
138 for (c = 0; c < nfields; c++) { | |
139 if (c) | |
140 putchar(' '); | |
141 fputs(fields[c], stdout); | |
142 } | |
143 putchar(closech); | |
144 fputs(cp, stdout); | |
145 } |