Remove __P macros from src/usr.bin and src/usr.sbin.
[dragonfly.git] / usr.sbin / pkg_install / info / 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 info module.
19  *
20  * $FreeBSD: src/usr.sbin/pkg_install/info/main.c,v 1.22.2.14 2002/08/20 06:35:08 obrien Exp $
21  * $DragonFly: src/usr.sbin/pkg_install/info/Attic/main.c,v 1.3 2003/11/03 19:31:39 eirikn Exp $
22  */
23
24 #include "lib.h"
25 #include "info.h"
26 #include <err.h>
27
28 static char Options[] = "acdDe:fgGhiIkl:LmoO:pqrRst:vVW:x";
29
30 int     Flags           = 0;
31 match_t MatchType       = MATCH_GLOB;
32 Boolean Quiet           = FALSE;
33 char *InfoPrefix        = (char *)(uintptr_t)"";
34 char PlayPen[FILENAME_MAX];
35 char *CheckPkg          = NULL;
36 char *LookUpOrigin      = NULL;
37 struct which_head *whead;
38
39 static void usage(void);
40
41 int
42 main(int argc, char **argv)
43 {
44     int ch;
45     char **pkgs, **start;
46     char *pkgs_split;
47
48     whead = malloc(sizeof(struct which_head));
49     if (whead == NULL)
50         err(2, NULL);
51     TAILQ_INIT(whead);
52
53     pkgs = start = argv;
54     if (argc == 1) {
55         MatchType = MATCH_ALL;
56         Flags = SHOW_INDEX;
57     }
58     else while ((ch = getopt(argc, argv, Options)) != -1) {
59         switch(ch) {
60         case 'a':
61             MatchType = MATCH_ALL;
62             break;
63
64         case 'v':
65             Verbose = TRUE;
66             /* Reasonable definition of 'everything' */
67             Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL |
68                 SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE;
69             break;
70
71         case 'I':
72             Flags |= SHOW_INDEX;
73             break;
74
75         case 'p':
76             Flags |= SHOW_PREFIX;
77             break;
78
79         case 'c':
80             Flags |= SHOW_COMMENT;
81             break;
82
83         case 'd':
84             Flags |= SHOW_DESC;
85             break;
86
87         case 'D':
88             Flags |= SHOW_DISPLAY;
89             break;
90
91         case 'f':
92             Flags |= SHOW_PLIST;
93             break;
94
95         case 'g':
96             Flags |= SHOW_CKSUM;
97             break;
98
99         case 'G':
100             MatchType = MATCH_EXACT;
101             break;
102
103         case 'i':
104             Flags |= SHOW_INSTALL;
105             break;
106
107         case 'k':
108             Flags |= SHOW_DEINSTALL;
109             break;
110
111         case 'r':
112             Flags |= SHOW_REQUIRE;
113             break;
114
115         case 'R':
116             Flags |= SHOW_REQBY;
117             break;
118
119         case 'L':
120             Flags |= SHOW_FILES;
121             break;
122
123         case 'm':
124             Flags |= SHOW_MTREE;
125             break;
126
127         case 's':
128             Flags |= SHOW_SIZE;
129             break;
130
131         case 'o':
132             Flags |= SHOW_ORIGIN;
133             break;
134
135         case 'O':
136             LookUpOrigin = strdup(optarg);
137             if (LookUpOrigin == NULL)
138                 err(2, NULL);
139             break;
140
141         case 'V':
142             Flags |= SHOW_FMTREV;
143             break;
144
145         case 'l':
146             InfoPrefix = optarg;
147             break;
148
149         case 'q':
150             Quiet = TRUE;
151             break;
152
153         case 't':
154             strlcpy(PlayPen, optarg, sizeof(PlayPen));
155             break;
156
157         case 'x':
158             MatchType = MATCH_REGEX;
159             break;
160
161         case 'e':
162             CheckPkg = optarg;
163             break;
164
165         case 'W':
166             {
167                 struct which_entry *entp;
168
169                 entp = calloc(1, sizeof(struct which_entry));
170                 if (entp == NULL)
171                     err(2, NULL);
172                 
173                 strlcpy(entp->file, optarg, PATH_MAX);
174                 entp->skip = FALSE;
175                 TAILQ_INSERT_TAIL(whead, entp, next);
176                 break;
177             }
178
179         case 'h':
180         case '?':
181         default:
182             usage();
183             break;
184         }
185     }
186
187     argc -= optind;
188     argv += optind;
189
190     /* Set some reasonable defaults */
191     if (!Flags)
192         Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY;
193
194     /* Get all the remaining package names, if any */
195     while (*argv) {
196         /* 
197          * Don't try to apply heuristics if arguments are regexs or if
198          * the argument refers to an existing file.
199          */
200         if (MatchType != MATCH_REGEX && !isfile(*argv))
201             while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) {
202                 *pkgs_split++ = '\0';
203                 /*
204                  * If character after the '/' is alphanumeric or shell
205                  * metachar, then we've found the package name.  Otherwise
206                  * we've come across a trailing '/' and need to continue our
207                  * quest.
208                  */
209                 if (isalpha(*pkgs_split) || ((MatchType == MATCH_GLOB) && \
210                     strpbrk(pkgs_split, "*?[]") != NULL)) {
211                     *argv = pkgs_split;
212                     break;
213                 }
214             }
215         *pkgs++ = *argv++;
216     }
217
218     /* If no packages, yelp */
219     if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg && 
220         TAILQ_EMPTY(whead) && LookUpOrigin == NULL)
221         warnx("missing package name(s)"), usage();
222     *pkgs = NULL;
223     return pkg_perform(start);
224 }
225
226 static void
227 usage()
228 {
229     fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
230         "usage: pkg_info [-cdDfGiIkLmopqrRsvVx] [-e package] [-l prefix]",
231         "                [-t template] [pkg-name ...]",
232         "       pkg_info [-q] -W filename",
233         "       pkg_info [-q] -O origin",
234         "       pkg_info -a [flags]");
235     exit(1);
236 }