Home | History | Annotate | Line # | Download | only in create
perform.c revision 1.1
      1  1.1  joerg /*	$NetBSD: perform.c,v 1.1 2008/09/30 19:00:26 joerg Exp $	*/
      2  1.1  joerg 
      3  1.1  joerg #if HAVE_CONFIG_H
      4  1.1  joerg #include "config.h"
      5  1.1  joerg #endif
      6  1.1  joerg #include <nbcompat.h>
      7  1.1  joerg #if HAVE_SYS_CDEFS_H
      8  1.1  joerg #include <sys/cdefs.h>
      9  1.1  joerg #endif
     10  1.1  joerg #ifndef lint
     11  1.1  joerg #if 0
     12  1.1  joerg static const char *rcsid = "from FreeBSD Id: perform.c,v 1.38 1997/10/13 15:03:51 jkh Exp";
     13  1.1  joerg #else
     14  1.1  joerg __RCSID("$NetBSD: perform.c,v 1.1 2008/09/30 19:00:26 joerg Exp $");
     15  1.1  joerg #endif
     16  1.1  joerg #endif
     17  1.1  joerg 
     18  1.1  joerg /*
     19  1.1  joerg  * FreeBSD install - a package for the installation and maintainance
     20  1.1  joerg  * of non-core utilities.
     21  1.1  joerg  *
     22  1.1  joerg  * Redistribution and use in source and binary forms, with or without
     23  1.1  joerg  * modification, are permitted provided that the following conditions
     24  1.1  joerg  * are met:
     25  1.1  joerg  * 1. Redistributions of source code must retain the above copyright
     26  1.1  joerg  *    notice, this list of conditions and the following disclaimer.
     27  1.1  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     28  1.1  joerg  *    notice, this list of conditions and the following disclaimer in the
     29  1.1  joerg  *    documentation and/or other materials provided with the distribution.
     30  1.1  joerg  *
     31  1.1  joerg  * Jordan K. Hubbard
     32  1.1  joerg  * 18 July 1993
     33  1.1  joerg  *
     34  1.1  joerg  * This is the main body of the create module.
     35  1.1  joerg  *
     36  1.1  joerg  */
     37  1.1  joerg 
     38  1.1  joerg #include "lib.h"
     39  1.1  joerg #include "create.h"
     40  1.1  joerg 
     41  1.1  joerg #if HAVE_ERR_H
     42  1.1  joerg #include <err.h>
     43  1.1  joerg #endif
     44  1.1  joerg #if HAVE_UNISTD_H
     45  1.1  joerg #include <unistd.h>
     46  1.1  joerg #endif
     47  1.1  joerg 
     48  1.1  joerg static void
     49  1.1  joerg sanity_check(void)
     50  1.1  joerg {
     51  1.1  joerg 	if (!Comment)
     52  1.1  joerg 		errx(2, "required package comment string is missing (-c comment)");
     53  1.1  joerg 	if (!Desc)
     54  1.1  joerg 		errx(2, "required package description string is missing (-d desc)");
     55  1.1  joerg 	if (!Contents)
     56  1.1  joerg 		errx(2, "required package contents list is missing (-f [-]file)");
     57  1.1  joerg }
     58  1.1  joerg 
     59  1.1  joerg static void
     60  1.1  joerg register_depends(package_t *plist, char *deps, int build_only)
     61  1.1  joerg {
     62  1.1  joerg 	char *cp;
     63  1.1  joerg 
     64  1.1  joerg 	if (Verbose && !PlistOnly) {
     65  1.1  joerg 		if (build_only)
     66  1.1  joerg 			printf("Registering build depends:");
     67  1.1  joerg 		else
     68  1.1  joerg 			printf("Registering depends:");
     69  1.1  joerg 	}
     70  1.1  joerg 	while (deps) {
     71  1.1  joerg 		cp = strsep(&deps, " \t\n");
     72  1.1  joerg 		if (*cp) {
     73  1.1  joerg 			char *best_installed;
     74  1.1  joerg 			best_installed = find_best_matching_installed_pkg(cp);
     75  1.1  joerg 			if (best_installed != NULL) {
     76  1.1  joerg 				add_plist(plist, PLIST_BLDDEP, best_installed);
     77  1.1  joerg 				if (Verbose && !PlistOnly && build_only)
     78  1.1  joerg 					printf(" %s", cp);
     79  1.1  joerg 			} else
     80  1.1  joerg 				warnx("No matching package installed for %s", cp);
     81  1.1  joerg 			free(best_installed);
     82  1.1  joerg 			if (!build_only) {
     83  1.1  joerg 				add_plist(plist, PLIST_PKGDEP, cp);
     84  1.1  joerg 				if (Verbose && !PlistOnly)
     85  1.1  joerg 					printf(" %s", cp);
     86  1.1  joerg 			}
     87  1.1  joerg 		}
     88  1.1  joerg 	}
     89  1.1  joerg 	if (Verbose && !PlistOnly)
     90  1.1  joerg 		printf(".\n");
     91  1.1  joerg }
     92  1.1  joerg 
     93  1.1  joerg /*
     94  1.1  joerg  * Get a string parameter as a file spec or as a "contents follow -" spec
     95  1.1  joerg  */
     96  1.1  joerg static void
     97  1.1  joerg get_dash_string(char **s)
     98  1.1  joerg {
     99  1.1  joerg 	if (**s == '-')
    100  1.1  joerg 		*s = strdup(*s + 1);
    101  1.1  joerg 	else
    102  1.1  joerg 		*s = fileGetContents(*s);
    103  1.1  joerg }
    104  1.1  joerg 
    105  1.1  joerg int
    106  1.1  joerg pkg_perform(const char *pkg)
    107  1.1  joerg {
    108  1.1  joerg 	char   *cp;
    109  1.1  joerg 	FILE   *pkg_in;
    110  1.1  joerg 	package_t plist;
    111  1.1  joerg 	const char *full_pkg, *suffix;
    112  1.1  joerg 	char *allocated_pkg;
    113  1.1  joerg 	int retval;
    114  1.1  joerg 
    115  1.1  joerg 	/* Break the package name into base and desired suffix (if any) */
    116  1.1  joerg 	if ((cp = strrchr(pkg, '.')) != NULL) {
    117  1.1  joerg 		if ((allocated_pkg = malloc(cp - pkg + 1)) == NULL)
    118  1.1  joerg 			err(2, "malloc failed");
    119  1.1  joerg 		memcpy(allocated_pkg, pkg, cp - pkg);
    120  1.1  joerg 		allocated_pkg[cp - pkg] = '\0';
    121  1.1  joerg 		suffix = cp + 1;
    122  1.1  joerg 		full_pkg = pkg;
    123  1.1  joerg 		pkg = allocated_pkg;
    124  1.1  joerg 	} else {
    125  1.1  joerg 		allocated_pkg = NULL;
    126  1.1  joerg 		full_pkg = pkg;
    127  1.1  joerg 		suffix = "tgz";
    128  1.1  joerg 	}
    129  1.1  joerg 
    130  1.1  joerg 	/* Preliminary setup */
    131  1.1  joerg 	sanity_check();
    132  1.1  joerg 	if (Verbose && !PlistOnly)
    133  1.1  joerg 		printf("Creating package %s\n", pkg);
    134  1.1  joerg 	get_dash_string(&Comment);
    135  1.1  joerg 	get_dash_string(&Desc);
    136  1.1  joerg 	if (IS_STDIN(Contents))
    137  1.1  joerg 		pkg_in = stdin;
    138  1.1  joerg 	else {
    139  1.1  joerg 		pkg_in = fopen(Contents, "r");
    140  1.1  joerg 		if (!pkg_in)
    141  1.1  joerg 			errx(2, "unable to open contents file '%s' for input", Contents);
    142  1.1  joerg 	}
    143  1.1  joerg 	plist.head = plist.tail = NULL;
    144  1.1  joerg 
    145  1.1  joerg 	/* If a SrcDir override is set, add it now */
    146  1.1  joerg 	if (SrcDir) {
    147  1.1  joerg 		if (Verbose && !PlistOnly)
    148  1.1  joerg 			printf("Using SrcDir value of %s\n", (realprefix) ? realprefix : SrcDir);
    149  1.1  joerg 		add_plist(&plist, PLIST_SRC, SrcDir);
    150  1.1  joerg 	}
    151  1.1  joerg 
    152  1.1  joerg 	/* Stick the dependencies, if any, at the top */
    153  1.1  joerg 	if (Pkgdeps)
    154  1.1  joerg 		register_depends(&plist, Pkgdeps, 0);
    155  1.1  joerg 
    156  1.1  joerg 	/*
    157  1.1  joerg 	 * Put the build dependencies after the dependencies.
    158  1.1  joerg 	 * This works due to the evaluation order in pkg_add.
    159  1.1  joerg 	 */
    160  1.1  joerg 	if (BuildPkgdeps)
    161  1.1  joerg 		register_depends(&plist, BuildPkgdeps, 1);
    162  1.1  joerg 
    163  1.1  joerg 	/* Put the conflicts directly after the dependencies, if any */
    164  1.1  joerg 	if (Pkgcfl) {
    165  1.1  joerg 		if (Verbose && !PlistOnly)
    166  1.1  joerg 			printf("Registering conflicts:");
    167  1.1  joerg 		while (Pkgcfl) {
    168  1.1  joerg 			cp = strsep(&Pkgcfl, " \t\n");
    169  1.1  joerg 			if (*cp) {
    170  1.1  joerg 				add_plist(&plist, PLIST_PKGCFL, cp);
    171  1.1  joerg 				if (Verbose && !PlistOnly)
    172  1.1  joerg 					printf(" %s", cp);
    173  1.1  joerg 			}
    174  1.1  joerg 		}
    175  1.1  joerg 		if (Verbose && !PlistOnly)
    176  1.1  joerg 			printf(".\n");
    177  1.1  joerg 	}
    178  1.1  joerg 
    179  1.1  joerg 	/* Slurp in the packing list */
    180  1.1  joerg 	append_plist(&plist, pkg_in);
    181  1.1  joerg 
    182  1.1  joerg 	if (pkg_in != stdin)
    183  1.1  joerg 		fclose(pkg_in);
    184  1.1  joerg 
    185  1.1  joerg 	/* Prefix should override the packing list */
    186  1.1  joerg 	if (Prefix) {
    187  1.1  joerg 		delete_plist(&plist, FALSE, PLIST_CWD, NULL);
    188  1.1  joerg 		add_plist_top(&plist, PLIST_CWD, Prefix);
    189  1.1  joerg 	}
    190  1.1  joerg 	/*
    191  1.1  joerg          * Run down the list and see if we've named it, if not stick in a name
    192  1.1  joerg          * at the top.
    193  1.1  joerg          */
    194  1.1  joerg 	if (find_plist(&plist, PLIST_NAME) == NULL) {
    195  1.1  joerg 		add_plist_top(&plist, PLIST_NAME, basename_of(pkg));
    196  1.1  joerg 	}
    197  1.1  joerg 
    198  1.1  joerg 	/* Make first "real contents" pass over it */
    199  1.1  joerg 	check_list(&plist, basename_of(pkg));
    200  1.1  joerg 
    201  1.1  joerg 	/*
    202  1.1  joerg          * We're just here for to dump out a revised plist for the FreeBSD ports
    203  1.1  joerg          * hack.  It's not a real create in progress.
    204  1.1  joerg          */
    205  1.1  joerg 	if (PlistOnly) {
    206  1.1  joerg 		write_plist(&plist, stdout, realprefix);
    207  1.1  joerg 		retval = TRUE;
    208  1.1  joerg 	} else {
    209  1.1  joerg #ifdef BOOTSTRAP
    210  1.1  joerg 		warnx("Package building is not supported in bootstrap mode");
    211  1.1  joerg 		retval = FALSE;
    212  1.1  joerg #else
    213  1.1  joerg 		retval = pkg_build(pkg, full_pkg, suffix, &plist);
    214  1.1  joerg #endif
    215  1.1  joerg 	}
    216  1.1  joerg 
    217  1.1  joerg 	/* Cleanup */
    218  1.1  joerg 	free(Comment);
    219  1.1  joerg 	free(Desc);
    220  1.1  joerg 	free_plist(&plist);
    221  1.1  joerg 
    222  1.1  joerg 	free(allocated_pkg);
    223  1.1  joerg 
    224  1.1  joerg 	return retval;
    225  1.1  joerg }
    226