Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / pkg_install / add / main.c
1 /*
2  *
3  * FreeBSD install - a package for the installation and maintainance
4  * of non-core utilities.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * Jordan K. Hubbard
16  * 18 July 1993
17  *
18  * This is the add module.
19  *
20  * $FreeBSD: src/usr.sbin/pkg_install/add/main.c,v 1.29.2.20 2002/10/08 05:35:46 bmah Exp $
21  * $DragonFly: src/usr.sbin/pkg_install/add/Attic/main.c,v 1.2 2003/06/17 04:29:59 dillon Exp $
22  */
23
24 #include <err.h>
25 #include <sys/param.h>
26 #include <sys/utsname.h>
27 #include "lib.h"
28 #include "add.h"
29
30 static char Options[] = "hvIRfnrp:SMt:";
31
32 char    *Prefix         = NULL;
33 Boolean NoInstall       = FALSE;
34 Boolean NoRecord        = FALSE;
35 Boolean Remote          = FALSE;
36
37 char    *Mode           = NULL;
38 char    *Owner          = NULL;
39 char    *Group          = NULL;
40 char    *PkgName        = NULL;
41 char    *Directory      = NULL;
42 char    FirstPen[FILENAME_MAX];
43 add_mode_t AddMode      = NORMAL;
44
45 #define MAX_PKGS        200
46 char    pkgnames[MAX_PKGS][MAXPATHLEN];
47 char    *pkgs[MAX_PKGS];
48
49 struct {
50         int lowver;     /* Lowest version number to match */
51         int hiver;      /* Highest version number to match */
52         const char *directory;  /* Directory it lives in */
53 } releases[] = {
54         { 410000, 410000, "/packages-4.1-release" },
55         { 420000, 420000, "/packages-4.2-release" },
56         { 430000, 430000, "/packages-4.3-release" },
57         { 440000, 440000, "/packages-4.4-release" },
58         { 450000, 450000, "/packages-4.5-release" },
59         { 460000, 460001, "/packages-4.6-release" },
60         { 460002, 460099, "/packages-4.6.2-release" },
61         { 470000, 470099, "/packages-4.7-release" },
62         { 300000, 399000, "/packages-3-stable" },
63         { 400000, 499000, "/packages-4-stable" },
64         { 510000, 599000, "/packages-5-stable" },
65         { 0, 9999999, "/packages-current" },
66         { 0, 0, NULL }
67 };
68
69 static char *getpackagesite(void);
70 int getosreldate(void);
71
72 static void usage __P((void));
73
74 int
75 main(int argc, char **argv)
76 {
77     int ch, error;
78     char **start;
79     char *cp, *packagesite = NULL, *remotepkg = NULL, *ptr;
80     static char temppackageroot[MAXPATHLEN];
81
82     start = argv;
83     while ((ch = getopt(argc, argv, Options)) != -1) {
84         switch(ch) {
85         case 'v':
86             Verbose = TRUE;
87             break;
88
89         case 'p':
90             Prefix = optarg;
91             break;
92
93         case 'I':
94             NoInstall = TRUE;
95             break;
96
97         case 'R':
98             NoRecord = TRUE;
99             break;
100
101         case 'f':
102             Force = TRUE;
103             break;
104
105         case 'n':
106             Fake = TRUE;
107             Verbose = TRUE;
108             break;
109
110         case 'r':
111             Remote = TRUE;
112             break;
113
114         case 't':
115             if (strlcpy(FirstPen, optarg, sizeof(FirstPen)) >= sizeof(FirstPen))
116                 errx(1, "-t Argument too long.");
117             break;
118
119         case 'S':
120             AddMode = SLAVE;
121             break;
122
123         case 'M':
124             AddMode = MASTER;
125             break;
126
127         case 'h':
128         case '?':
129         default:
130             usage();
131             break;
132         }
133     }
134     argc -= optind;
135     argv += optind;
136
137     if (argc > MAX_PKGS) {
138         errx(1, "too many packages (max %d)", MAX_PKGS);
139     }
140
141     if (AddMode != SLAVE) {
142         for (ch = 0; ch < MAX_PKGS; pkgs[ch++] = NULL) ;
143
144         /* Get all the remaining package names, if any */
145         for (ch = 0; *argv; ch++, argv++) {
146             if (Remote) {
147                 if ((packagesite = getpackagesite()) == NULL)
148                     errx(1, "package name too long");
149                 if (strlcpy(temppackageroot, packagesite,
150                     sizeof(temppackageroot)) >= sizeof(temppackageroot))
151                     errx(1, "package name too long");
152                 if (strlcat(temppackageroot, *argv, sizeof(temppackageroot))
153                     >= sizeof(temppackageroot))
154                     errx(1, "package name too long");
155                 remotepkg = temppackageroot;
156                 if (!((ptr = strrchr(remotepkg, '.')) && ptr[1] == 't' && 
157                         (ptr[2] == 'b' || ptr[2] == 'g') && ptr[3] == 'z' &&
158                         !ptr[4]))
159                     if (strlcat(remotepkg, ".tgz", sizeof(temppackageroot))
160                         >= sizeof(temppackageroot))
161                         errx(1, "package name too long");
162             }
163             if (!strcmp(*argv, "-"))    /* stdin? */
164                 (const char *)pkgs[ch] = "-";
165             else if (isURL(*argv)) {    /* preserve URLs */
166                 if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
167                     >= sizeof(pkgnames[ch]))
168                     errx(1, "package name too long");
169                 pkgs[ch] = pkgnames[ch];
170             }
171             else if ((Remote) && isURL(remotepkg)) {
172                 if (strlcpy(pkgnames[ch], remotepkg, sizeof(pkgnames[ch]))
173                     >= sizeof(pkgnames[ch]))
174                     errx(1, "package name too long");
175                 pkgs[ch] = pkgnames[ch];
176             } else {                    /* expand all pathnames to fullnames */
177                 if (fexists(*argv)) /* refers to a file directly */
178                     pkgs[ch] = realpath(*argv, pkgnames[ch]);
179                 else {          /* look for the file in the expected places */
180                     if (!(cp = fileFindByPath(NULL, *argv))) {
181                         /* let pkg_do() fail later, so that error is reported */
182                         if (strlcpy(pkgnames[ch], *argv, sizeof(pkgnames[ch]))
183                             >= sizeof(pkgnames[ch]))
184                             errx(1, "package name too long");
185                         pkgs[ch] = pkgnames[ch];
186                     } else {
187                         if (strlcpy(pkgnames[ch], cp, sizeof(pkgnames[ch]))
188                             >= sizeof(pkgnames[ch]))
189                             errx(1, "package name too long");
190                         pkgs[ch] = pkgnames[ch];
191                     }
192                 }
193             }
194             if (packagesite != NULL)
195                 packagesite[0] = '\0';
196         }
197     }
198     /* If no packages, yelp */
199     else if (!ch) {
200         warnx("missing package name(s)");
201         usage();
202     }
203     else if (ch > 1 && AddMode == MASTER) {
204         warnx("only one package name may be specified with master mode");
205         usage();
206     }
207     /* Make sure the sub-execs we invoke get found */
208     setenv("PATH", 
209            "/sbin:/usr/sbin:/bin:/usr/bin:/usr/X11R6/bin",
210            1);
211
212     /* Set a reasonable umask */
213     umask(022);
214
215     if ((error = pkg_perform(pkgs)) != 0) {
216         if (Verbose)
217             warnx("%d package addition(s) failed", error);
218         return error;
219     }
220     else
221         return 0;
222 }
223
224 static char *
225 getpackagesite(void)
226 {
227     int reldate, i;
228     static char sitepath[MAXPATHLEN];
229     struct utsname u;
230
231     if (getenv("PACKAGESITE")) {
232         if (strlcpy(sitepath, getenv("PACKAGESITE"), sizeof(sitepath))
233             >= sizeof(sitepath))
234             return NULL;
235         return sitepath;
236     }
237
238     if (getenv("PACKAGEROOT")) {
239         if (strlcpy(sitepath, getenv("PACKAGEROOT"), sizeof(sitepath))
240             >= sizeof(sitepath))
241             return NULL;
242     } else {
243         if (strlcat(sitepath, "ftp://ftp.freebsd.org", sizeof(sitepath))
244             >= sizeof(sitepath))
245             return NULL;
246     }
247
248     if (strlcat(sitepath, "/pub/FreeBSD/ports/", sizeof(sitepath))
249         >= sizeof(sitepath))
250         return NULL;
251
252     uname(&u);
253     if (strlcat(sitepath, u.machine, sizeof(sitepath)) >= sizeof(sitepath))
254         return NULL;
255
256     reldate = getosreldate();
257     for(i = 0; releases[i].directory != NULL; i++) {
258         if (reldate >= releases[i].lowver && reldate <= releases[i].hiver) {
259             if (strlcat(sitepath, releases[i].directory, sizeof(sitepath))
260                 >= sizeof(sitepath))
261                 return NULL;
262             break;
263         }
264     }
265
266     if (strlcat(sitepath, "/Latest/", sizeof(sitepath)) >= sizeof(sitepath))
267         return NULL;
268
269     return sitepath;
270
271 }
272
273 static void
274 usage()
275 {
276     fprintf(stderr, "%s\n%s\n",
277                 "usage: pkg_add [-vInrfRMS] [-t template] [-p prefix]",
278                 "               pkg-name [pkg-name ...]");
279     exit(1);
280 }