3 * $Id: sw_arch.c,v 1.2 2004/04/08 01:52:19 mdw Exp $
5 * Messing with architectures
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of sw-tools.
14 * sw-tools is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * sw-tools is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with sw-tools; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
39 #include <mLib/alloc.h>
40 #include <mLib/dstr.h>
41 #include <mLib/quis.h>
42 #include <mLib/report.h>
48 /*----- Static variables --------------------------------------------------*/
50 static archcons
*tab
= 0;
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @arch_readtab@ --- *
58 * Returns: The address of the archtab list.
60 * Use: Reads the archtab file (if necessary) and returns a list of
64 archcons
*arch_readtab(void)
70 /* --- Return the list if I've already got it --- */
75 /* --- Open the file --- */
77 if ((fp
= fopen(ARCHTAB
, "r")) == 0)
78 die(1, "couldn't open `%s': %s", ARCHTAB
, strerror(errno
));
80 /* --- Read it in line-by-line --- */
83 for (; dstr_putline(&d
, fp
) != EOF
; DRESET(&d
)) {
88 /* --- Pick out the architecture name --- */
90 while (isspace((unsigned char)*q
))
92 if (*q
== '#' || *q
== 0)
95 while (*q
&& !isspace((unsigned char)*q
))
100 /* --- Pick out the hostname --- */
102 while (isspace((unsigned char)*q
))
107 while (*q
&& !isspace((unsigned char)*q
))
112 /* --- Allocate a new link --- */
114 a
= xmalloc(sizeof(*a
));
115 a
->arch
= xstrdup(arch
);
116 a
->host
= xstrdup(host
);
121 if (strcmp(arch
, ARCH
) == 0)
122 a
->flags
|= archFlag_home
;
135 /* --- @arch_lookup@ --- *
137 * Arguments: @const char *arch@ = pointer to archtecture name
138 * @int abbrev@ = whether abbreviations are OK
140 * Returns: Pointer to archtab block, or null.
142 * Use: Translates an architecture name into the name of a host
143 * supporting that architecture.
146 archent
*arch_lookup(const char *arch
, int abbrev
)
148 size_t sz
= strlen(arch
);
152 for (a
= arch_readtab(); a
; a
= a
->cdr
) {
154 if (strncmp(arch
, e
->arch
, sz
) == 0) {
155 if (e
->arch
[sz
] == 0)
168 /* --- @arch_filter@ --- *
170 * Arguments: @archcons *a@ = input list to filter
171 * @const char *p@ = pointer to a textual list of architectures
172 * @unsigned and@, @unsigned xor@ = flags to look for
174 * Returns: A newly constructed architecture list containing only the
175 * listed architectures.
177 * Use: Filters the architecture list down to a few interesting
180 * If @p@ is non-null, it is a textual list of architecture
181 * names (possibly abbreviated), and separated by whitespace
182 * and/or commas: only the named architectures are included in
183 * the resulting list. If @p@ is null, all architectures are
186 * The list is further trimmed down by examining the flags words
187 * in each entry. Only entries with flags @f@ where @(f ^ xor)
188 * & and@ is zero are left in the resulting list. (To include
189 * all entries, clear @and@ to zero. To require a flag to be
190 * clear, set the corresponding bit in @and@. To require a flag
191 * to be set, set the corresponding bit in both @and@ and @xor@.
193 * (Don't try to filter on the @archFlag_touched@ flag. That
194 * flag is for the internal use of this routine.)
197 archcons
*arch_filter(archcons
*a
, const char *p
,
198 unsigned and, unsigned xor)
202 /* --- How this works --- *
204 * If I have a list of architecture names in @p@, then I scan the list and
205 * set the `touch' bit on matching entries, and clear it on nonmatching
206 * ones. Then I set the `touch' bit in the @and@ and @xor@ arguments.
207 * Otherwise I skip all of that, and clear the `touch' bit in @and@. After
208 * all that, I just scoop up the architectures with appropriate flags.
211 and &= ~archFlag_touched
;
214 /* --- Clear all the touch flags --- */
218 for (aa
= a
; aa
; aa
= aa
->cdr
)
219 aa
->car
->flags
&= ~archFlag_touched
;
222 /* --- Set flags for entries in my list --- */
225 char *q
= xstrdup(p
);
226 for (p
= strtok(q
, ", \t"); p
; p
= strtok(0, ", \t")) {
227 archent
*e
= arch_lookup(p
, 1);
229 die(1, "architecture `%s' not found", p
);
230 e
->flags
|= archFlag_touched
;
235 /* --- Only include touched entries --- */
237 and |= archFlag_touched
;
238 xor |= archFlag_touched
;
241 /* --- Now trundle through the list accumulating matching entries --- */
246 for (; a
; a
= a
->cdr
) {
247 if (((a
->car
->flags
^ xor) & and) == 0) {
248 archcons
*aa
= CREATE(archcons
);
262 /* --- @arch_free@ --- *
264 * Arguments: @archcons *a@ = pointer to a list
268 * Use: Contrary to anything you might have expected from the Lispy
269 * naming, old architecture lists don't get garbage collected.
270 * This routine throws away an old list when you don't want it
271 * any more. Don't call this on the main list! It will fail
272 * miserably, because the cons cells in the main list are
276 void arch_free(archcons
*a
)
279 archcons
*p
= a
->cdr
;
285 /* --- @arch_toText@ --- *
287 * Arguments: @dstr *d@ = pointer to dynamic string to build result in
288 * @archcons *a@ = list to write into the string
289 * @unsigned and@, @unsigned xor@ = flags to look for
293 * Use: Writes a textual list of architectures to a string. This can
294 * then be used (for example) as the `only-arch' list in the
295 * info file, by filling it into a skeleton and calling
296 * @swinfo_update@. The @and@ and @xor@ arguments work in the
297 * same way as with @arch_filter@.
300 void arch_toText(dstr
*d
, archcons
*a
, unsigned and, unsigned xor)
304 for (; a
; a
= a
->cdr
) {
306 if (((e
->flags
^ xor) & and) == 0) {
309 dstr_puts(d
, e
->arch
);
315 /*----- Subcommands -------------------------------------------------------*/
317 /* --- @sw_arch@ --- */
319 int sw_arch(int argc
, char *argv
[])
322 die(1, "Usage: arch");
327 /* --- @sw_host@ --- */
329 int sw_host(int argc
, char *argv
[])
333 die(1, "Usage: host ARCH");
334 if ((a
= arch_lookup(argv
[1], 1)) == 0)
335 die(1, "no host for architecture `%s'", argv
[1]);
340 /* --- @sw_listarch@ --- */
342 int sw_listarch(int argc
, char *argv
[])
346 die(1, "Usage: listarch");
347 for (a
= arch_readtab(); a
; a
= a
->cdr
)
352 /* --- @sw_all@ --- */
354 int sw_all(int argc
, char *argv
[])
358 die(1, "Usage: all");
359 if (swinfo_fetch(&sw
)) {
360 die(1, "couldn't read build status: %s (try running setup)",
371 /* --- @sw_only@ --- */
373 int sw_only(int argc
, char *argv
[])
377 /* --- Validate the arguments --- */
380 die(1, "Usage: only-arch ARCH,...");
382 /* --- Gather the arguments into one buffer --- */
386 for (i
= 1; i
< argc
; i
++) {
388 dstr_puts(&d
, argv
[i
]);
392 /* --- Convert into a canonical list --- */
395 archcons
*a
= arch_filter(arch_readtab(), d
.buf
, 0, 0);
397 arch_toText(&d
, a
, 0, 0);
401 /* --- Put the list into the information table --- */
406 if (swinfo_fetch(&sw
)) {
407 die(1, "couldn't read build status: %s (try running setup)",
412 skel
.only_arch
= d
.buf
;
413 swinfo_update(&sw
, &skel
);
422 /*----- That's all, folks -------------------------------------------------*/