main.c revision 24428
1#ifndef lint
2static char *rcsid = "$Id: main.c,v 1.13 1997/02/22 16:09:18 peter 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;
35
36char	*Mode		= NULL;
37char	*Owner		= NULL;
38char	*Group		= NULL;
39char	*PkgName	= NULL;
40char	*Directory	= NULL;
41char	FirstPen[FILENAME_MAX];
42add_mode_t AddMode	= NORMAL;
43
44#define MAX_PKGS	20
45char	pkgnames[MAX_PKGS][MAXPATHLEN];
46char	*pkgs[MAX_PKGS];
47
48int
49main(int argc, char **argv)
50{
51    int ch, err;
52    char **start;
53    char *prog_name = argv[0], *cp;
54
55    start = argv;
56    while ((ch = getopt(argc, argv, Options)) != -1) {
57	switch(ch) {
58	case 'v':
59	    Verbose = TRUE;
60	    break;
61
62	case 'p':
63	    Prefix = optarg;
64	    break;
65
66	case 'I':
67	    NoInstall = TRUE;
68	    break;
69
70	case 'R':
71	    NoRecord = TRUE;
72	    break;
73
74	case 'f':
75	    Force = TRUE;
76	    break;
77
78	case 'n':
79	    Fake = TRUE;
80	    Verbose = TRUE;
81	    break;
82
83	case 't':
84	    strcpy(FirstPen, optarg);
85	    break;
86
87	case 'S':
88	    AddMode = SLAVE;
89	    break;
90
91	case 'M':
92	    AddMode = MASTER;
93	    break;
94
95	case 'h':
96	case '?':
97	default:
98	    usage(prog_name, NULL);
99	    break;
100	}
101    }
102    argc -= optind;
103    argv += optind;
104
105    if (argc > MAX_PKGS) {
106	whinge("Too many packages (max %d).", MAX_PKGS);
107	return(1);
108    }
109
110    if (AddMode != SLAVE) {
111	for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
112
113	/* Get all the remaining package names, if any */
114	for (ch = 0; *argv; ch++, argv++) {
115	    if (!strcmp(*argv, "-"))	/* stdin? */
116		pkgs[ch] = "-";
117	    else if (isURL(*argv))	/* preserve URLs */
118		pkgs[ch] = strcpy(pkgnames[ch], *argv);
119	    else {			/* expand all pathnames to fullnames */
120		if (fexists(*argv)) /* refers to a file directly */
121		    pkgs[ch] = realpath(*argv, pkgnames[ch]);
122		else {		/* look for the file in the expected places */
123		    if (!(cp = fileFindByPath(NULL, *argv)))
124			whinge("Can't find package '%s'.", *argv);
125		    else
126			pkgs[ch] = strcpy(pkgnames[ch], cp);
127		}
128	    }
129	}
130    }
131    /* If no packages, yelp */
132    else if (!ch)
133	usage(prog_name, "Missing package name(s)");
134    else if (ch > 1 && AddMode == MASTER)
135	usage(prog_name, "Only one package name may be specified with master mode");
136    if ((err = pkg_perform(pkgs)) != NULL) {
137	if (Verbose)
138	    fprintf(stderr, "%d package addition(s) failed.\n", err);
139	return err;
140    }
141    else
142	return 0;
143}
144
145void
146usage(const char *name, const char *fmt, ...)
147{
148    va_list args;
149
150    va_start(args, fmt);
151    if (fmt) {
152	fprintf(stderr, "%s: ", name);
153	vfprintf(stderr, fmt, args);
154	fprintf(stderr, "\n\n");
155    }
156    va_end(args);
157    fprintf(stderr, "Usage: %s [args] pkg [ .. pkg ]\n", name);
158    fprintf(stderr, "Where args are one or more of:\n\n");
159    fprintf(stderr, "-v         verbose\n");
160    fprintf(stderr, "-p arg     override prefix with arg\n");
161    fprintf(stderr, "-I         don't execute pkg install script, if any\n");
162    fprintf(stderr, "-R         don't record installation (can't delete!)\n");
163    fprintf(stderr, "-n         don't actually install, just show steps\n");
164    fprintf(stderr, "-t temp    use temp as template for mktemp()\n");
165    fprintf(stderr, "-S         run in SLAVE mode\n");
166    fprintf(stderr, "-M         run in MASTER mode\n");
167    exit(1);
168}
169