59d9308b7df80a196a00a8bedc6c91f397475544
3 * $Id: sw_info.c,v 1.2 1999/06/18 18:58:45 mdw Exp $
5 * Maintenance of `.sw-info' files
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 /*----- Revision history --------------------------------------------------*
32 * Revision 1.2 1999/06/18 18:58:45 mdw
35 * Revision 1.1.1.1 1999/06/02 16:53:35 mdw
40 /*----- Header files ------------------------------------------------------*/
52 #include <sys/types.h>
56 #include <mLib/alloc.h>
57 #include <mLib/dstr.h>
58 #include <mLib/lock.h>
59 #include <mLib/quis.h>
60 #include <mLib/report.h>
65 /*----- Static variables --------------------------------------------------*/
67 static struct swfield
{
72 { "package", offsetof(swinfo
, package
), "Package" },
73 { "version", offsetof(swinfo
, version
), "Version" },
74 { "maintainer", offsetof(swinfo
, maintainer
), "Maintained by" },
75 { "date", offsetof(swinfo
, date
), "Last modified" },
76 { "only-arch", offsetof(swinfo
, only_arch
), "Only build for" },
77 { "arch", offsetof(swinfo
, arch
), "Successfully built" },
80 typedef struct swfield swfield
;
82 #define SWINFO(sw, off) (*(char **)((char *)sw + off))
84 /*----- Main code ---------------------------------------------------------*/
86 /* --- @swinfo_clear@ --- *
88 * Arguments: @swinfo *sw@ = pointe to info block
92 * Use: Clears an info block so that it doesn't contain anything.
93 * This is mainly useful when building skeletons to apply using
97 void swinfo_clear(swinfo
*sw
)
100 for (f
= swfields
; f
->name
; f
++)
101 SWINFO(sw
, f
->off
) = 0;
104 /* --- @swinfo_fetch@ --- *
106 * Arguments: @swinfo *sw@ = pointer to info block to fill in
108 * Returns: Zero if OK, else nonzero.
110 * Use: Fills in the info block if it can.
113 int swinfo_fetch(swinfo
*sw
)
120 /* --- Initialize the various fields --- */
124 /* --- Open the data file --- */
126 if ((fp
= fopen(".sw-info", "r")) == 0)
129 /* --- Read the lines from the file --- */
131 for (; dstr_putline(&d
, fp
) != EOF
; DRESET(&d
)) {
136 /* --- Find the field name --- */
138 while (isspace((unsigned char)*p
))
140 if (*p
== 0 || *p
== '#')
143 /* --- Find the end of the field name --- */
146 while (*p
&& *p
!= '=' && !isspace((unsigned char)*p
))
149 /* --- Skip an optional `=' sign --- */
155 while (isspace((unsigned char)*p
))
157 if (*p
== '=' && ch
!= '=') {
159 while (isspace((unsigned char)*p
))
166 /* --- Look up the key in the table --- */
168 for (f
= swfields
; f
->name
; f
++) {
169 if (strcmp(key
, f
->name
) == 0)
172 moan("unknown key `%s' at line %i in `.sw-info'", key
, line
);
175 /* --- Put the value in --- */
178 SWINFO(sw
, f
->off
) = xstrdup(value
);
186 /* --- @swinfo_sanity@ --- *
188 * Arguments: @swinfo *sw@ = pointer to an info block
190 * Returns: Yes, if the block is OK.
192 * Use: Objects if the information in the info block is bad.
195 void swinfo_sanity(swinfo
*sw
)
198 die(1, "unknown package name. Try `%s setup PACKAGE VERSION'.", QUIS
);
200 die(1, "unknown package version. Try `%s setup PACKAGE VERSION'.",
203 die(1, "unknown maintainer. Try `%s setup PACKAGE VERSION'.", QUIS
);
205 die(1, "unknown date. Try `%s setup PACKAGE VERSION'.", QUIS
);
208 /* --- @swinfo_put@ --- *
210 * Arguments: @swinfo *sw@ = pointer to an info block
212 * Returns: Zero if it worked, nonzero if not.
214 * Use: Writes an info block to the appropriate file.
217 int swinfo_put(swinfo
*sw
)
223 /* --- Quick sanity check --- */
227 /* --- Write the new data out --- */
229 if ((fp
= fopen(".sw-info.new", "w")) == 0)
231 for (f
= swfields
; f
->name
; f
++) {
232 if (!SWINFO(sw
, f
->off
))
234 if (fprintf(fp
, "%s = %s\n", f
->name
, SWINFO(sw
, f
->off
)) == EOF
) {
240 if (fclose(fp
) == EOF
) {
245 /* --- Carefully! replace the old one --- *
247 * Keep an old one around just in case, unless the renames fail because
251 remove(".sw-info.older"); /* Don't care if this fails */
252 if (rename(".sw-info.old", ".sw-info.older") && errno
!= ENOENT
)
253 { e
= errno
; goto tidy_0
; }
254 if (rename(".sw-info", ".sw-info.old") && errno
!= ENOENT
)
255 { e
= errno
; goto tidy_1
; }
256 if (rename(".sw-info.new", ".sw-info"))
257 { e
= errno
; goto tidy_2
; }
258 remove(".sw-info.older"); /* Don't care if this fails */
261 /* --- Tidy up if anything went tits-up --- */
264 rename(".sw-info.old", ".sw-info");
266 rename(".sw-info.older", ".sw-info.old");
268 remove(".sw-info.new");
273 /* --- @swinfo_destroy@ --- *
275 * Arguments: @swinfo *sw@ = pointer to info block
279 * Use: Destroys an info block when it's not useful any more.
282 void swinfo_destroy(swinfo
*sw
)
286 for (f
= swfields
; f
->name
; f
++) {
287 if (SWINFO(sw
, f
->off
))
288 free(SWINFO(sw
, f
->off
));
292 /* --- @swinfo_update@ --- *
294 * Arguments: @swinfo *sw@ = pointer to info block
295 * @swinfo *skel@ = pointer to skeleton values
299 * Use: Updates the fields in an information block, except for the
300 * architecture names stuff. (I'll leave that for later,
301 * because it's rather more involved.
304 void swinfo_update(swinfo
*sw
, swinfo
*skel
)
310 /* --- Set up a default maintainer --- */
312 if (!skel
->maintainer
&& !sw
->maintainer
) {
313 char *u
= getenv("USER");
316 u
= getenv("LOGNAME");
318 struct passwd
*pw
= getpwuid(getuid());
320 moan("you don't seem to exist");
321 sprintf(ubuf
, "uid %i", (int)getuid());
326 skel
->maintainer
= u
;
329 /* --- Set up a default date --- */
333 struct tm
*tm
= localtime(&t
);
334 strftime(dbuf
, sizeof(dbuf
), "%Y-%m-%d", tm
);
338 /* --- Set a default architecture list --- */
340 if (!skel
->arch
&& !sw
->arch
)
343 /* --- Grind through all the fields --- */
345 for (f
= swfields
; f
->name
; f
++) {
346 if (!SWINFO(skel
, f
->off
) || strcmp(SWINFO(skel
, f
->off
), "-") == 0)
348 if (SWINFO(sw
, f
->off
))
349 free(SWINFO(sw
, f
->off
));
350 SWINFO(sw
, f
->off
) = xstrdup(SWINFO(skel
, f
->off
));
354 /*----- Subcommands -------------------------------------------------------*/
356 /* --- @sw_setup@ --- */
358 int sw_setup(int argc
, char *argv
[])
361 if (argc
< 3 || argc
> 4)
362 die(1, "Usage: setup PACKAGE VERSION [MAINTAINER]");
365 skel
.package
= argv
[1];
366 skel
.version
= argv
[2];
368 skel
.maintainer
= argv
[3];
369 swinfo_update(&sw
, &skel
);
374 /* --- @sw_status@ --- */
376 int sw_status(int argc
, char *argv
[])
380 if (swinfo_fetch(&sw
)) {
381 die(1, "couldn't read build status: %s (try running setup)",
384 for (f
= swfields
; f
->name
; f
++) {
385 if (SWINFO(&sw
, f
->off
))
386 printf("%s: %s\n", f
->desc
, SWINFO(&sw
, f
->off
));
391 /* --- @sw_commit@ --- */
393 int sw_commit(int argc
, char *argv
[])
398 die(1, "Usage: commit");
399 if (swinfo_fetch(&sw
)) {
400 die(1, "couldn't read build status: %s (try running setup)",
404 /* --- Make sure everything has been built properly --- */
408 archcons
*all
= arch_readtab();
411 for (a
= aa
= arch_filter(all
, sw
.arch
, 0, 0); a
; a
= a
->cdr
)
412 a
->car
->flags
|= archFlag_built
;
416 aa
= arch_filter(all
, sw
.only_arch
, archFlag_built
, 0);
418 const char *sep
= "";
419 fprintf(stderr
, "%s: not built for ", QUIS
);
420 for (a
= aa
; a
; a
= a
->cdr
) {
421 fprintf(stderr
, "%s%s", sep
, a
->car
->arch
);
430 /* --- Write to the index file --- */
433 FILE *fp
= fopen(PREFIX
"/sw-index", "a");
435 const char *sep
= "";
438 die(1, "couldn't open index file: %s", strerror(errno
));
439 if (lock_file(fileno(fp
), LOCK_EXCL
))
440 die(1, "couldn't obtain lock on index file: %s", strerror(errno
));
442 for (f
= swfields
; f
->name
; f
++) {
443 if (SWINFO(&sw
, f
->off
)) {
444 fprintf(fp
, "%s%s = %s", sep
, f
->name
, SWINFO(&sw
, f
->off
));
455 /*----- That's all, folks -------------------------------------------------*/