3 * $Id: sw_info.c,v 1.3 1999/06/24 15:51:59 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.3 1999/06/24 15:51:59 mdw
33 * Add support for the `sw-precommit' script.
35 * Revision 1.2 1999/06/18 18:58:45 mdw
38 * Revision 1.1.1.1 1999/06/02 16:53:35 mdw
43 /*----- Header files ------------------------------------------------------*/
56 #include <sys/types.h>
62 extern char **environ
;
65 #include <mLib/alloc.h>
66 #include <mLib/dstr.h>
67 #include <mLib/lock.h>
68 #include <mLib/quis.h>
69 #include <mLib/report.h>
75 /*----- Static variables --------------------------------------------------*/
77 static struct swfield
{
82 { "package", offsetof(swinfo
, package
), "Package" },
83 { "version", offsetof(swinfo
, version
), "Version" },
84 { "maintainer", offsetof(swinfo
, maintainer
), "Maintained by" },
85 { "date", offsetof(swinfo
, date
), "Last modified" },
86 { "only-arch", offsetof(swinfo
, only_arch
), "Only build for" },
87 { "arch", offsetof(swinfo
, arch
), "Successfully built" },
90 typedef struct swfield swfield
;
92 #define SWINFO(sw, off) (*(char **)((char *)sw + off))
94 /*----- Main code ---------------------------------------------------------*/
96 /* --- @swinfo_clear@ --- *
98 * Arguments: @swinfo *sw@ = pointe to info block
102 * Use: Clears an info block so that it doesn't contain anything.
103 * This is mainly useful when building skeletons to apply using
107 void swinfo_clear(swinfo
*sw
)
110 for (f
= swfields
; f
->name
; f
++)
111 SWINFO(sw
, f
->off
) = 0;
114 /* --- @swinfo_fetch@ --- *
116 * Arguments: @swinfo *sw@ = pointer to info block to fill in
118 * Returns: Zero if OK, else nonzero.
120 * Use: Fills in the info block if it can.
123 int swinfo_fetch(swinfo
*sw
)
130 /* --- Initialize the various fields --- */
134 /* --- Open the data file --- */
136 if ((fp
= fopen(".sw-info", "r")) == 0)
139 /* --- Read the lines from the file --- */
141 for (; dstr_putline(&d
, fp
) != EOF
; DRESET(&d
)) {
146 /* --- Find the field name --- */
148 while (isspace((unsigned char)*p
))
150 if (*p
== 0 || *p
== '#')
153 /* --- Find the end of the field name --- */
156 while (*p
&& *p
!= '=' && !isspace((unsigned char)*p
))
159 /* --- Skip an optional `=' sign --- */
165 while (isspace((unsigned char)*p
))
167 if (*p
== '=' && ch
!= '=') {
169 while (isspace((unsigned char)*p
))
176 /* --- Look up the key in the table --- */
178 for (f
= swfields
; f
->name
; f
++) {
179 if (strcmp(key
, f
->name
) == 0)
182 moan("unknown key `%s' at line %i in `.sw-info'", key
, line
);
185 /* --- Put the value in --- */
188 SWINFO(sw
, f
->off
) = xstrdup(value
);
196 /* --- @swinfo_sanity@ --- *
198 * Arguments: @swinfo *sw@ = pointer to an info block
200 * Returns: Yes, if the block is OK.
202 * Use: Objects if the information in the info block is bad.
205 void swinfo_sanity(swinfo
*sw
)
208 die(1, "unknown package name. Try `%s setup PACKAGE VERSION'.", QUIS
);
210 die(1, "unknown package version. Try `%s setup PACKAGE VERSION'.",
213 die(1, "unknown maintainer. Try `%s setup PACKAGE VERSION'.", QUIS
);
215 die(1, "unknown date. Try `%s setup PACKAGE VERSION'.", QUIS
);
218 /* --- @swinfo_put@ --- *
220 * Arguments: @swinfo *sw@ = pointer to an info block
222 * Returns: Zero if it worked, nonzero if not.
224 * Use: Writes an info block to the appropriate file.
227 int swinfo_put(swinfo
*sw
)
233 /* --- Quick sanity check --- */
237 /* --- Write the new data out --- */
239 if ((fp
= fopen(".sw-info.new", "w")) == 0)
241 for (f
= swfields
; f
->name
; f
++) {
242 if (!SWINFO(sw
, f
->off
))
244 if (fprintf(fp
, "%s = %s\n", f
->name
, SWINFO(sw
, f
->off
)) == EOF
) {
250 if (fclose(fp
) == EOF
) {
255 /* --- Carefully! replace the old one --- *
257 * Keep an old one around just in case, unless the renames fail because
261 remove(".sw-info.older"); /* Don't care if this fails */
262 if (rename(".sw-info.old", ".sw-info.older") && errno
!= ENOENT
)
263 { e
= errno
; goto tidy_0
; }
264 if (rename(".sw-info", ".sw-info.old") && errno
!= ENOENT
)
265 { e
= errno
; goto tidy_1
; }
266 if (rename(".sw-info.new", ".sw-info"))
267 { e
= errno
; goto tidy_2
; }
268 remove(".sw-info.older"); /* Don't care if this fails */
271 /* --- Tidy up if anything went tits-up --- */
274 rename(".sw-info.old", ".sw-info");
276 rename(".sw-info.older", ".sw-info.old");
278 remove(".sw-info.new");
283 /* --- @swinfo_destroy@ --- *
285 * Arguments: @swinfo *sw@ = pointer to info block
289 * Use: Destroys an info block when it's not useful any more.
292 void swinfo_destroy(swinfo
*sw
)
296 for (f
= swfields
; f
->name
; f
++) {
297 if (SWINFO(sw
, f
->off
))
298 free(SWINFO(sw
, f
->off
));
302 /* --- @swinfo_update@ --- *
304 * Arguments: @swinfo *sw@ = pointer to info block
305 * @swinfo *skel@ = pointer to skeleton values
309 * Use: Updates the fields in an information block, except for the
310 * architecture names stuff. (I'll leave that for later,
311 * because it's rather more involved.
314 void swinfo_update(swinfo
*sw
, swinfo
*skel
)
320 /* --- Set up a default maintainer --- */
322 if (!skel
->maintainer
&& !sw
->maintainer
) {
323 char *u
= getenv("USER");
326 u
= getenv("LOGNAME");
328 struct passwd
*pw
= getpwuid(getuid());
330 moan("you don't seem to exist");
331 sprintf(ubuf
, "uid %i", (int)getuid());
336 skel
->maintainer
= u
;
339 /* --- Set up a default date --- */
343 struct tm
*tm
= localtime(&t
);
344 strftime(dbuf
, sizeof(dbuf
), "%Y-%m-%d", tm
);
348 /* --- Set a default architecture list --- */
350 if (!skel
->arch
&& !sw
->arch
)
353 /* --- Grind through all the fields --- */
355 for (f
= swfields
; f
->name
; f
++) {
356 if (!SWINFO(skel
, f
->off
) || strcmp(SWINFO(skel
, f
->off
), "-") == 0)
358 if (SWINFO(sw
, f
->off
))
359 free(SWINFO(sw
, f
->off
));
360 SWINFO(sw
, f
->off
) = xstrdup(SWINFO(skel
, f
->off
));
364 /*----- Subcommands -------------------------------------------------------*/
366 /* --- @sw_setup@ --- */
368 int sw_setup(int argc
, char *argv
[])
371 if (argc
< 3 || argc
> 4)
372 die(1, "Usage: setup PACKAGE VERSION [MAINTAINER]");
375 skel
.package
= argv
[1];
376 skel
.version
= argv
[2];
378 skel
.maintainer
= argv
[3];
379 swinfo_update(&sw
, &skel
);
384 /* --- @sw_status@ --- */
386 int sw_status(int argc
, char *argv
[])
390 if (swinfo_fetch(&sw
)) {
391 die(1, "couldn't read build status: %s (try running setup)",
394 for (f
= swfields
; f
->name
; f
++) {
395 if (SWINFO(&sw
, f
->off
))
396 printf("%s: %s\n", f
->desc
, SWINFO(&sw
, f
->off
));
401 /* --- @sw_commit@ --- */
403 int sw_commit(int argc
, char *argv
[])
408 die(1, "Usage: commit");
409 if (swinfo_fetch(&sw
)) {
410 die(1, "couldn't read build status: %s (try running setup)",
414 /* --- Make sure everything has been built properly --- */
418 archcons
*all
= arch_readtab();
421 for (a
= aa
= arch_filter(all
, sw
.arch
, 0, 0); a
; a
= a
->cdr
)
422 a
->car
->flags
|= archFlag_built
;
426 aa
= arch_filter(all
, sw
.only_arch
, archFlag_built
, 0);
428 const char *sep
= "";
429 fprintf(stderr
, "%s: not built for ", QUIS
);
430 for (a
= aa
; a
; a
= a
->cdr
) {
431 fprintf(stderr
, "%s%s", sep
, a
->car
->arch
);
440 /* --- Run the local precommit check script --- */
444 struct sigaction sa
, sa_int
, sa_quit
;
447 /* --- Make sure I can trap the child's death --- */
449 sa
.sa_handler
= SIG_IGN
;
450 sigemptyset(&sa
.sa_mask
);
452 if (sigaction(SIGINT
, &sa
, &sa_int
) || sigaction(SIGQUIT
, &sa
, &sa_quit
))
453 die(1, "couldn't set signal dispositions: %s", strerror(errno
));
455 /* --- Spawn off a child process --- */
460 die(1, "error from fork: %s", strerror(errno
));
464 /* --- Re-enable signals --- */
466 sigaction(SIGINT
, &sa_int
, 0);
467 sigaction(SIGQUIT
, &sa_quit
, 0);
469 /* --- Set up the environment --- */
472 env_import(&t
, environ
);
473 env_put(&t
, "SW_PACKAGE", sw
.package
);
474 env_put(&t
, "SW_VERSION", sw
.version
);
475 env_put(&t
, "SW_MAINTAINER", sw
.maintainer
);
476 env_put(&t
, "SW_DATE", sw
.date
);
477 env_put(&t
, "SW_ARCHLIST", sw
.arch
);
478 env_put(&t
, "SW_PREFIX", sw
.prefix
);
479 environ
= env_export(&t
);
481 /* --- Run the commit check script --- */
483 execl(PREFIX
"/share/sw-precommit", "sw-precommit",
484 sw
.package
, (void *)0);
487 die(1, "couldn't run " PREFIX
"/share/sw-precommit: %s",
491 /* --- Wait for the child to finish --- */
493 if (waitpid(kid
, &status
, 0) < 0)
494 die(1, "error waiting for child: %s", strerror(errno
));
495 if (!(WIFEXITED(status
) && WEXITSTATUS(status
) == 0))
496 die(1, "sw-precommit failed");
497 sigaction(SIGINT
, &sa_int
, 0);
498 sigaction(SIGQUIT
, &sa_quit
, 0);
501 /* --- Write to the index file --- */
504 FILE *fp
= fopen(PREFIX
"/sw-index", "a");
506 const char *sep
= "";
509 die(1, "couldn't open index file: %s", strerror(errno
));
510 if (lock_file(fileno(fp
), LOCK_EXCL
))
511 die(1, "couldn't obtain lock on index file: %s", strerror(errno
));
513 for (f
= swfields
; f
->name
; f
++) {
514 if (SWINFO(&sw
, f
->off
)) {
515 fprintf(fp
, "%s%s = %s", sep
, f
->name
, SWINFO(&sw
, f
->off
));
526 /*----- That's all, folks -------------------------------------------------*/