main.c revision 12219
1#ifndef lint
2static char *rcsid = "$Id: main.c,v 1.8 1995/10/25 15:37:47 jkh Exp $";
3#endif
4
5/*
6 *
7 * FreeBSD install - a package for the installation and maintainance
8 * of non-core utilities.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * Jordan K. Hubbard
20 * 18 July 1993
21 *
22 * This is the add module.
23 *
24 */
25
26#include <sys/param.h>
27#include "lib.h"
28#include "add.h"
29
30static char Options[] = "hvIRfnp:SMt:";
31
32char	*Prefix		= NULL;
33Boolean	NoInstall	= FALSE;
34Boolean	NoRecord	= FALSE;
35Boolean	Force		= FALSE;
36
37char	*Mode		= NULL;
38char	*Owner		= NULL;
39char	*Group		= NULL;
40char	*PkgName	= NULL;
41char	*Directory	= NULL;
42char	FirstPen[FILENAME_MAX];
43add_mode_t AddMode	= NORMAL;
44
45#define MAX_PKGS	20
46char	pkgnames[MAX_PKGS][MAXPATHLEN];
47char	*pkgs[MAX_PKGS];
48
49int
50main(int argc, char **argv)
51{
52    int ch, err;
53    char **start;
54    char *prog_name = argv[0], *cp;
55
56    start = argv;
57    while ((ch = getopt(argc, argv, Options)) != EOF) {
58	switch(ch) {
59	case 'v':
60	    Verbose = TRUE;
61	    break;
62
63	case 'p':
64	    Prefix = optarg;
65	    break;
66
67	case 'I':
68	    NoInstall = TRUE;
69	    break;
70
71	case 'R':
72	    NoRecord = TRUE;
73	    break;
74
75	case 'f':
76	    Force = TRUE;
77	    break;
78
79	case 'n':
80	    Fake = TRUE;
81	    Verbose = TRUE;
82	    break;
83
84	case 't':
85	    strcpy(FirstPen, optarg);
86	    break;
87
88	case 'S':
89	    AddMode = SLAVE;
90	    break;
91
92	case 'M':
93	    AddMode = MASTER;
94	    break;
95
96	case 'h':
97	case '?':
98	default:
99	    usage(prog_name, NULL);
100	    break;
101	}
102    }
103    argc -= optind;
104    argv += optind;
105
106    if (argc > MAX_PKGS) {
107	whinge("Too many packages (max %d).", MAX_PKGS);
108	return(1);
109    }
110
111    if (AddMode != SLAVE) {
112	for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
113
114	/* Get all the remaining package names, if any */
115	for (ch = 0; *argv; ch++, argv++) {
116	    if (isURL(*argv))	/* preserve URLs */
117		pkgs[ch] = strcpy(pkgnames[ch], *argv);
118	    else {			/* expand all pathnames to fullnames */
119		if (fexists(*argv)) /* refers to a file directly */
120		    pkgs[ch] = realpath(*argv, pkgnames[ch]);
121		else {		/* look for the file in the expected places */
122		    if (!(cp = fileFindByPath(NULL, *argv)))
123			whinge("Can't find package '%s'.", *argv);
124		    else
125			pkgs[ch] = strcpy(pkgnames[ch], cp);
126		}
127	    }
128	}
129    }
130    /* If no packages, yelp */
131    else if (!ch)
132	usage(prog_name, "Missing package name(s)");
133    else if (ch > 1 && AddMode == MASTER)
134	usage(prog_name, "Only one package name may be specified with master mode");
135    if ((err = pkg_perform(pkgs)) != NULL) {
136	if (Verbose)
137	    fprintf(stderr, "%d package addition(s) failed.\n", err);
138	return err;
139    }
140    else
141	return 0;
142}
143
144void
145usage(const char *name, const char *fmt, ...)
146{
147    va_list args;
148
149    va_start(args, fmt);
150    if (fmt) {
151	fprintf(stderr, "%s: ", name);
152	vfprintf(stderr, fmt, args);
153	fprintf(stderr, "\n\n");
154    }
155    va_end(args);
156    fprintf(stderr, "Usage: %s [args] pkg [ .. pkg ]\n", name);
157    fprintf(stderr, "Where args are one or more of:\n\n");
158    fprintf(stderr, "-v         verbose\n");
159    fprintf(stderr, "-p arg     override prefix with arg\n");
160    fprintf(stderr, "-I         don't execute pkg install script, if any\n");
161    fprintf(stderr, "-R         don't record installation (can't delete!)\n");
162    fprintf(stderr, "-n         don't actually install, just show steps\n");
163    fprintf(stderr, "-t temp    use temp as template for mktemp()\n");
164    fprintf(stderr, "-S         run in SLAVE mode\n");
165    fprintf(stderr, "-M         run in MASTER mode\n");
166    exit(1);
167}
168