main.c revision 178246
1327Sjkh/* 2327Sjkh * 3327Sjkh * FreeBSD install - a package for the installation and maintainance 4327Sjkh * of non-core utilities. 5327Sjkh * 6327Sjkh * Redistribution and use in source and binary forms, with or without 7327Sjkh * modification, are permitted provided that the following conditions 8327Sjkh * are met: 9327Sjkh * 1. Redistributions of source code must retain the above copyright 10327Sjkh * notice, this list of conditions and the following disclaimer. 11327Sjkh * 2. Redistributions in binary form must reproduce the above copyright 12327Sjkh * notice, this list of conditions and the following disclaimer in the 13327Sjkh * documentation and/or other materials provided with the distribution. 14327Sjkh * 15327Sjkh * Jordan K. Hubbard 16327Sjkh * 18 July 1993 17327Sjkh * 1831997Shoek * This is the info module. 19327Sjkh * 20327Sjkh */ 21327Sjkh 2293520Sobrien#include <sys/cdefs.h> 2393520Sobrien__FBSDID("$FreeBSD: head/usr.sbin/pkg_install/info/main.c 178246 2008-04-16 13:05:35Z flz $"); 2493520Sobrien 25327Sjkh#include "lib.h" 26327Sjkh#include "info.h" 2774699Ssobomax#include <err.h> 28327Sjkh 29154145Sflzstatic char Options[] = "abcdDe:EfgGhiIjkKl:LmoO:pPqQrRst:vVW:xX"; 3055567Sphantom 31327Sjkhint Flags = 0; 3272174Ssobomaxmatch_t MatchType = MATCH_GLOB; 33112572SmdoddBoolean QUIET = FALSE; 34112579SmdoddBoolean UseBlkSz = FALSE; 3584745Ssobomaxchar *InfoPrefix = (char *)(uintptr_t)""; 3611780Sjkhchar PlayPen[FILENAME_MAX]; 37392Sjkhchar *CheckPkg = NULL; 3896030Ssobomaxchar *LookUpOrigin = NULL; 39154145SflzBoolean KeepPackage = FALSE; 4074699Ssobomaxstruct which_head *whead; 41327Sjkh 42173412Skevlostatic void usage(void); 4330221Scharnier 44327Sjkhint 45327Sjkhmain(int argc, char **argv) 46327Sjkh{ 47327Sjkh int ch; 48327Sjkh char **pkgs, **start; 4956001Sdan char *pkgs_split; 50327Sjkh 5174699Ssobomax whead = malloc(sizeof(struct which_head)); 5274699Ssobomax if (whead == NULL) 5374699Ssobomax err(2, NULL); 5474699Ssobomax TAILQ_INIT(whead); 5574699Ssobomax 56327Sjkh pkgs = start = argv; 5746105Sjkh if (argc == 1) { 5872174Ssobomax MatchType = MATCH_ALL; 5946105Sjkh Flags = SHOW_INDEX; 6046105Sjkh } 6146105Sjkh else while ((ch = getopt(argc, argv, Options)) != -1) { 62327Sjkh switch(ch) { 63327Sjkh case 'a': 6472174Ssobomax MatchType = MATCH_ALL; 65327Sjkh break; 66327Sjkh 67112579Smdodd case 'b': 68112579Smdodd UseBlkSz = TRUE; 69112579Smdodd break; 70112579Smdodd 71327Sjkh case 'v': 72159554Sobrien Verbose++; 73327Sjkh /* Reasonable definition of 'everything' */ 74327Sjkh Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL | 754996Sjkh SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE; 76327Sjkh break; 77327Sjkh 78131275Seik case 'E': 79131275Seik Flags |= SHOW_PKGNAME; 80131275Seik break; 81131275Seik 82327Sjkh case 'I': 83327Sjkh Flags |= SHOW_INDEX; 84327Sjkh break; 85327Sjkh 86327Sjkh case 'p': 87327Sjkh Flags |= SHOW_PREFIX; 88327Sjkh break; 89327Sjkh 90327Sjkh case 'c': 91327Sjkh Flags |= SHOW_COMMENT; 92327Sjkh break; 93327Sjkh 94327Sjkh case 'd': 95327Sjkh Flags |= SHOW_DESC; 96327Sjkh break; 97327Sjkh 984996Sjkh case 'D': 994996Sjkh Flags |= SHOW_DISPLAY; 1004996Sjkh break; 1014996Sjkh 102327Sjkh case 'f': 103327Sjkh Flags |= SHOW_PLIST; 104327Sjkh break; 105327Sjkh 10671965Sjkh case 'g': 10771965Sjkh Flags |= SHOW_CKSUM; 10871965Sjkh break; 10971965Sjkh 11072174Ssobomax case 'G': 11172174Ssobomax MatchType = MATCH_EXACT; 11272174Ssobomax break; 11372174Ssobomax 114327Sjkh case 'i': 115327Sjkh Flags |= SHOW_INSTALL; 116327Sjkh break; 117327Sjkh 118131280Seik case 'j': 119131280Seik Flags |= SHOW_REQUIRE; 120131280Seik break; 121131280Seik 122327Sjkh case 'k': 123327Sjkh Flags |= SHOW_DEINSTALL; 124327Sjkh break; 125327Sjkh 126154145Sflz case 'K': 127154145Sflz KeepPackage = TRUE; 128154145Sflz break; 129154145Sflz 130327Sjkh case 'r': 131131280Seik Flags |= SHOW_DEPEND; 132327Sjkh break; 133327Sjkh 1344996Sjkh case 'R': 1354996Sjkh Flags |= SHOW_REQBY; 1364996Sjkh break; 1374996Sjkh 138411Sjkh case 'L': 139411Sjkh Flags |= SHOW_FILES; 140411Sjkh break; 141411Sjkh 1424996Sjkh case 'm': 1434996Sjkh Flags |= SHOW_MTREE; 1444996Sjkh break; 1454996Sjkh 14672174Ssobomax case 's': 14772174Ssobomax Flags |= SHOW_SIZE; 14872174Ssobomax break; 14962775Ssobomax 15067454Ssobomax case 'o': 15167454Ssobomax Flags |= SHOW_ORIGIN; 15267454Ssobomax break; 15367454Ssobomax 15496030Ssobomax case 'O': 15596030Ssobomax LookUpOrigin = strdup(optarg); 15696030Ssobomax if (LookUpOrigin == NULL) 15796030Ssobomax err(2, NULL); 15896030Ssobomax break; 15996030Ssobomax 16084750Ssobomax case 'V': 16184750Ssobomax Flags |= SHOW_FMTREV; 16284750Ssobomax break; 16384750Ssobomax 164379Sjkh case 'l': 165379Sjkh InfoPrefix = optarg; 166379Sjkh break; 167379Sjkh 168411Sjkh case 'q': 169411Sjkh Quiet = TRUE; 170411Sjkh break; 171411Sjkh 172112572Smdodd case 'Q': 173112572Smdodd Quiet = TRUE; 174112572Smdodd QUIET = TRUE; 175112572Smdodd break; 176112572Smdodd 177383Sjkh case 't': 17885019Ssobomax strlcpy(PlayPen, optarg, sizeof(PlayPen)); 179383Sjkh break; 180383Sjkh 18172174Ssobomax case 'x': 18272174Ssobomax MatchType = MATCH_REGEX; 18372174Ssobomax break; 18472174Ssobomax 185131275Seik case 'X': 186131275Seik MatchType = MATCH_EREGEX; 187131275Seik break; 188131275Seik 189392Sjkh case 'e': 190392Sjkh CheckPkg = optarg; 191392Sjkh break; 192392Sjkh 19374699Ssobomax case 'W': 19474699Ssobomax { 19574699Ssobomax struct which_entry *entp; 19674699Ssobomax 19774699Ssobomax entp = calloc(1, sizeof(struct which_entry)); 19874699Ssobomax if (entp == NULL) 19974699Ssobomax err(2, NULL); 20074699Ssobomax 20174699Ssobomax strlcpy(entp->file, optarg, PATH_MAX); 20274699Ssobomax entp->skip = FALSE; 20374699Ssobomax TAILQ_INSERT_TAIL(whead, entp, next); 20474699Ssobomax break; 20574699Ssobomax } 20674699Ssobomax 207103149Ssobomax case 'P': 208103149Ssobomax Flags = SHOW_PTREV; 209103149Ssobomax break; 210103149Ssobomax 211327Sjkh case 'h': 212327Sjkh case '?': 213327Sjkh default: 21430221Scharnier usage(); 215327Sjkh break; 216327Sjkh } 21746105Sjkh } 218327Sjkh 2198857Srgrimes argc -= optind; 220327Sjkh argv += optind; 221327Sjkh 222103149Ssobomax if (Flags & SHOW_PTREV) { 223103149Ssobomax if (!Quiet) 224103149Ssobomax printf("Package tools revision: "); 225103149Ssobomax printf("%d\n", PKG_INSTALL_VERSION); 226103149Ssobomax exit(0); 227103149Ssobomax } 228103149Ssobomax 229327Sjkh /* Set some reasonable defaults */ 230327Sjkh if (!Flags) 2314996Sjkh Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY; 232327Sjkh 233327Sjkh /* Get all the remaining package names, if any */ 23460563Ssteve while (*argv) { 23595934Ssobomax /* 23695934Ssobomax * Don't try to apply heuristics if arguments are regexs or if 23795934Ssobomax * the argument refers to an existing file. 23895934Ssobomax */ 239178246Sflz if (MatchType != MATCH_REGEX && MatchType != MATCH_EREGEX && !isfile(*argv) && !isURL(*argv)) 24072174Ssobomax while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) { 24172174Ssobomax *pkgs_split++ = '\0'; 24272174Ssobomax /* 24372174Ssobomax * If character after the '/' is alphanumeric or shell 24472174Ssobomax * metachar, then we've found the package name. Otherwise 24572174Ssobomax * we've come across a trailing '/' and need to continue our 24672174Ssobomax * quest. 24772174Ssobomax */ 248151188Skrion if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \ 24972174Ssobomax strpbrk(pkgs_split, "*?[]") != NULL)) { 25072174Ssobomax *argv = pkgs_split; 25172174Ssobomax break; 25272174Ssobomax } 25360563Ssteve } 25460563Ssteve *pkgs++ = *argv++; 25556001Sdan } 256327Sjkh 257327Sjkh /* If no packages, yelp */ 25874699Ssobomax if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg && 25996030Ssobomax TAILQ_EMPTY(whead) && LookUpOrigin == NULL) 26030221Scharnier warnx("missing package name(s)"), usage(); 261327Sjkh *pkgs = NULL; 262327Sjkh return pkg_perform(start); 263327Sjkh} 264327Sjkh 26530221Scharnierstatic void 26630221Scharnierusage() 267327Sjkh{ 26896067Ssobomax fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 269154145Sflz "usage: pkg_info [-bcdDEfgGiIjkKLmopPqQrRsvVxX] [-e package] [-l prefix]", 270127641Scperciva " [-t template] -a | pkg-name ...", 271127641Scperciva " pkg_info [-qQ] -W filename", 272127641Scperciva " pkg_info [-qQ] -O origin", 273127641Scperciva " pkg_info"); 274327Sjkh exit(1); 275327Sjkh} 276