Home | History | Annotate | Line # | Download | only in add
      1 /*	$NetBSD: main.c,v 1.5 2024/06/11 09:26:57 wiz Exp $	*/
      2 
      3 #if HAVE_CONFIG_H
      4 #include "config.h"
      5 #endif
      6 #include <nbcompat.h>
      7 #if HAVE_SYS_CDEFS_H
      8 #include <sys/cdefs.h>
      9 #endif
     10 __RCSID("$NetBSD: main.c,v 1.5 2024/06/11 09:26:57 wiz Exp $");
     11 
     12 /*
     13  *
     14  * FreeBSD install - a package for the installation and maintainance
     15  * of non-core utilities.
     16  *
     17  * Redistribution and use in source and binary forms, with or without
     18  * modification, are permitted provided that the following conditions
     19  * are met:
     20  * 1. Redistributions of source code must retain the above copyright
     21  *    notice, this list of conditions and the following disclaimer.
     22  * 2. Redistributions in binary form must reproduce the above copyright
     23  *    notice, this list of conditions and the following disclaimer in the
     24  *    documentation and/or other materials provided with the distribution.
     25  *
     26  * Jordan K. Hubbard
     27  * 18 July 1993
     28  *
     29  * This is the add module.
     30  *
     31  */
     32 
     33 #if HAVE_ERR_H
     34 #include <err.h>
     35 #endif
     36 #include "lib.h"
     37 #include "add.h"
     38 
     39 static char Options[] = "AC:DIK:P:RVfhm:np:t:Uuv";
     40 
     41 char   *Destdir = NULL;
     42 char   *OverrideMachine = NULL;
     43 char   *OverrideOpsys = NULL;
     44 char   *OverrideOSVersion = NULL;
     45 char   *Prefix = NULL;
     46 Boolean NoInstall = FALSE;
     47 Boolean NoRecord = FALSE;
     48 Boolean Automatic = FALSE;
     49 Boolean ForceDepends = FALSE;
     50 /*
     51  * Normally, updating fails if the dependencies of a depending package
     52  * are not satisfied by the package to be updated.  ForceDepending
     53  * turns that failure into a warning.
     54  */
     55 Boolean ForceDepending = FALSE;
     56 
     57 int	LicenseCheck = 0;
     58 int     Replace = 0;
     59 int	ReplaceSame = 0;
     60 
     61 static void
     62 usage(void)
     63 {
     64 	(void) fprintf(stderr, "%s\n%s\n%s\n",
     65 	    "usage: pkg_add [-AfhInRuVv] [-C config] [-P destdir] [-K pkg_dbdir]",
     66 	    "               [-m machine] [-p prefix]",
     67 	    "               [[ftp|http]://[user[:password]@]host[:port]][/path/]pkg-name ...");
     68 	exit(1);
     69 }
     70 
     71 int
     72 main(int argc, char **argv)
     73 {
     74 	int     ch, error=0;
     75 	lpkg_head_t pkgs;
     76 
     77 	setprogname(argv[0]);
     78 	while ((ch = getopt(argc, argv, Options)) != -1) {
     79 		switch (ch) {
     80 		case 'A':
     81 			Automatic = TRUE;
     82 			break;
     83 
     84 		case 'C':
     85 			config_file = optarg;
     86 			break;
     87 
     88 		case 'D':
     89 			ForceDepending = TRUE;
     90 			break;
     91 
     92 		case 'P':
     93 			Destdir = optarg;
     94 			break;
     95 
     96 		case 'f':
     97 			Force = TRUE;
     98 			ForceDepends = TRUE;
     99 			ForceDepending = TRUE;
    100 			break;
    101 
    102 		case 'I':
    103 			NoInstall = TRUE;
    104 			break;
    105 
    106 		case 'K':
    107 			pkgdb_set_dir(optarg, 3);
    108 			break;
    109 
    110 		case 'R':
    111 			NoRecord = TRUE;
    112 			break;
    113 
    114 		case 'm':
    115 			parse_cross(optarg, &OverrideMachine, &OverrideOpsys,
    116 			    &OverrideOSVersion);
    117 			break;
    118 
    119 		case 'n':
    120 			Fake = TRUE;
    121 			Verbose = TRUE;
    122 			break;
    123 
    124 		case 'p':
    125 			Prefix = optarg;
    126 			break;
    127 
    128 		case 'U':
    129 			ReplaceSame = 1;
    130 			Replace = 1;
    131 			break;
    132 
    133 		case 'u':
    134 			Replace = 1;
    135 			break;
    136 
    137 		case 'V':
    138 			show_version();
    139 			/* NOTREACHED */
    140 
    141 		case 'v':
    142 			Verbose = TRUE;
    143 			break;
    144 
    145 		case 'h':
    146 		case '?':
    147 		default:
    148 			usage();
    149 			break;
    150 		}
    151 	}
    152 	argc -= optind;
    153 	argv += optind;
    154 
    155 	pkg_install_config();
    156 
    157 	if (Destdir != NULL) {
    158 		char *pkgdbdir;
    159 
    160 		pkgdbdir = xasprintf("%s/%s", Destdir, config_pkg_dbdir);
    161 		pkgdb_set_dir(pkgdbdir, 4);
    162 		free(pkgdbdir);
    163 	}
    164 
    165 #ifndef BOOTSTRAP
    166 	process_pkg_path();
    167 #endif
    168 
    169 	TAILQ_INIT(&pkgs);
    170 
    171 	if (argc == 0) {
    172 		/* If no packages, yelp */
    173 		warnx("missing package name(s)");
    174 		usage();
    175 	}
    176 
    177 #ifndef BOOTSTRAP
    178 	if (strcasecmp(do_license_check, "no") == 0)
    179 		LicenseCheck = 0;
    180 	else if (strcasecmp(do_license_check, "yes") == 0)
    181 		LicenseCheck = 1;
    182 	else if (strcasecmp(do_license_check, "always") == 0)
    183 		LicenseCheck = 2;
    184 	else
    185 		errx(1, "Unknown value of the configuration variable"
    186 		    "CHECK_LICENSE");
    187 
    188 	if (LicenseCheck)
    189 		load_license_lists();
    190 #endif
    191 
    192 	/* Get all the remaining package names, if any */
    193 	for (; argc > 0; --argc, ++argv) {
    194 		lpkg_t *lpp;
    195 
    196 		if (IS_STDIN(*argv))
    197 			lpp = alloc_lpkg("-");
    198 		else
    199 			lpp = alloc_lpkg(*argv);
    200 
    201 		TAILQ_INSERT_TAIL(&pkgs, lpp, lp_link);
    202 	}
    203 
    204 	error += pkg_perform(&pkgs);
    205 	if (error != 0) {
    206 		warnx("%d package addition%s failed", error, error == 1 ? "" : "s");
    207 		exit(1);
    208 	}
    209 	exit(0);
    210 }
    211