main.c revision 151188
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 151188 2005-10-10 08:38:21Z krion $"); 2493520Sobrien 25327Sjkh#include "lib.h" 26327Sjkh#include "info.h" 2774699Ssobomax#include <err.h> 28327Sjkh 29131280Seikstatic char Options[] = "abcdDe:EfgGhiIjkl: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; 3974699Ssobomaxstruct which_head *whead; 40327Sjkh 4130221Scharnierstatic void usage __P((void)); 4230221Scharnier 43327Sjkhint 44327Sjkhmain(int argc, char **argv) 45327Sjkh{ 46327Sjkh int ch; 47327Sjkh char **pkgs, **start; 4856001Sdan char *pkgs_split; 49327Sjkh 5074699Ssobomax whead = malloc(sizeof(struct which_head)); 5174699Ssobomax if (whead == NULL) 5274699Ssobomax err(2, NULL); 5374699Ssobomax TAILQ_INIT(whead); 5474699Ssobomax 55327Sjkh pkgs = start = argv; 5646105Sjkh if (argc == 1) { 5772174Ssobomax MatchType = MATCH_ALL; 5846105Sjkh Flags = SHOW_INDEX; 5946105Sjkh } 6046105Sjkh else while ((ch = getopt(argc, argv, Options)) != -1) { 61327Sjkh switch(ch) { 62327Sjkh case 'a': 6372174Ssobomax MatchType = MATCH_ALL; 64327Sjkh break; 65327Sjkh 66112579Smdodd case 'b': 67112579Smdodd UseBlkSz = TRUE; 68112579Smdodd break; 69112579Smdodd 70327Sjkh case 'v': 71327Sjkh Verbose = TRUE; 72327Sjkh /* Reasonable definition of 'everything' */ 73327Sjkh Flags = SHOW_COMMENT | SHOW_DESC | SHOW_PLIST | SHOW_INSTALL | 744996Sjkh SHOW_DEINSTALL | SHOW_REQUIRE | SHOW_DISPLAY | SHOW_MTREE; 75327Sjkh break; 76327Sjkh 77131275Seik case 'E': 78131275Seik Flags |= SHOW_PKGNAME; 79131275Seik break; 80131275Seik 81327Sjkh case 'I': 82327Sjkh Flags |= SHOW_INDEX; 83327Sjkh break; 84327Sjkh 85327Sjkh case 'p': 86327Sjkh Flags |= SHOW_PREFIX; 87327Sjkh break; 88327Sjkh 89327Sjkh case 'c': 90327Sjkh Flags |= SHOW_COMMENT; 91327Sjkh break; 92327Sjkh 93327Sjkh case 'd': 94327Sjkh Flags |= SHOW_DESC; 95327Sjkh break; 96327Sjkh 974996Sjkh case 'D': 984996Sjkh Flags |= SHOW_DISPLAY; 994996Sjkh break; 1004996Sjkh 101327Sjkh case 'f': 102327Sjkh Flags |= SHOW_PLIST; 103327Sjkh break; 104327Sjkh 10571965Sjkh case 'g': 10671965Sjkh Flags |= SHOW_CKSUM; 10771965Sjkh break; 10871965Sjkh 10972174Ssobomax case 'G': 11072174Ssobomax MatchType = MATCH_EXACT; 11172174Ssobomax break; 11272174Ssobomax 113327Sjkh case 'i': 114327Sjkh Flags |= SHOW_INSTALL; 115327Sjkh break; 116327Sjkh 117131280Seik case 'j': 118131280Seik Flags |= SHOW_REQUIRE; 119131280Seik break; 120131280Seik 121327Sjkh case 'k': 122327Sjkh Flags |= SHOW_DEINSTALL; 123327Sjkh break; 124327Sjkh 125327Sjkh case 'r': 126131280Seik Flags |= SHOW_DEPEND; 127327Sjkh break; 128327Sjkh 1294996Sjkh case 'R': 1304996Sjkh Flags |= SHOW_REQBY; 1314996Sjkh break; 1324996Sjkh 133411Sjkh case 'L': 134411Sjkh Flags |= SHOW_FILES; 135411Sjkh break; 136411Sjkh 1374996Sjkh case 'm': 1384996Sjkh Flags |= SHOW_MTREE; 1394996Sjkh break; 1404996Sjkh 14172174Ssobomax case 's': 14272174Ssobomax Flags |= SHOW_SIZE; 14372174Ssobomax break; 14462775Ssobomax 14567454Ssobomax case 'o': 14667454Ssobomax Flags |= SHOW_ORIGIN; 14767454Ssobomax break; 14867454Ssobomax 14996030Ssobomax case 'O': 15096030Ssobomax LookUpOrigin = strdup(optarg); 15196030Ssobomax if (LookUpOrigin == NULL) 15296030Ssobomax err(2, NULL); 15396030Ssobomax break; 15496030Ssobomax 15584750Ssobomax case 'V': 15684750Ssobomax Flags |= SHOW_FMTREV; 15784750Ssobomax break; 15884750Ssobomax 159379Sjkh case 'l': 160379Sjkh InfoPrefix = optarg; 161379Sjkh break; 162379Sjkh 163411Sjkh case 'q': 164411Sjkh Quiet = TRUE; 165411Sjkh break; 166411Sjkh 167112572Smdodd case 'Q': 168112572Smdodd Quiet = TRUE; 169112572Smdodd QUIET = TRUE; 170112572Smdodd break; 171112572Smdodd 172383Sjkh case 't': 17385019Ssobomax strlcpy(PlayPen, optarg, sizeof(PlayPen)); 174383Sjkh break; 175383Sjkh 17672174Ssobomax case 'x': 17772174Ssobomax MatchType = MATCH_REGEX; 17872174Ssobomax break; 17972174Ssobomax 180131275Seik case 'X': 181131275Seik MatchType = MATCH_EREGEX; 182131275Seik break; 183131275Seik 184392Sjkh case 'e': 185392Sjkh CheckPkg = optarg; 186392Sjkh break; 187392Sjkh 18874699Ssobomax case 'W': 18974699Ssobomax { 19074699Ssobomax struct which_entry *entp; 19174699Ssobomax 19274699Ssobomax entp = calloc(1, sizeof(struct which_entry)); 19374699Ssobomax if (entp == NULL) 19474699Ssobomax err(2, NULL); 19574699Ssobomax 19674699Ssobomax strlcpy(entp->file, optarg, PATH_MAX); 19774699Ssobomax entp->skip = FALSE; 19874699Ssobomax TAILQ_INSERT_TAIL(whead, entp, next); 19974699Ssobomax break; 20074699Ssobomax } 20174699Ssobomax 202103149Ssobomax case 'P': 203103149Ssobomax Flags = SHOW_PTREV; 204103149Ssobomax break; 205103149Ssobomax 206327Sjkh case 'h': 207327Sjkh case '?': 208327Sjkh default: 20930221Scharnier usage(); 210327Sjkh break; 211327Sjkh } 21246105Sjkh } 213327Sjkh 2148857Srgrimes argc -= optind; 215327Sjkh argv += optind; 216327Sjkh 217103149Ssobomax if (Flags & SHOW_PTREV) { 218103149Ssobomax if (!Quiet) 219103149Ssobomax printf("Package tools revision: "); 220103149Ssobomax printf("%d\n", PKG_INSTALL_VERSION); 221103149Ssobomax exit(0); 222103149Ssobomax } 223103149Ssobomax 224327Sjkh /* Set some reasonable defaults */ 225327Sjkh if (!Flags) 2264996Sjkh Flags = SHOW_COMMENT | SHOW_DESC | SHOW_REQBY; 227327Sjkh 228327Sjkh /* Get all the remaining package names, if any */ 22960563Ssteve while (*argv) { 23095934Ssobomax /* 23195934Ssobomax * Don't try to apply heuristics if arguments are regexs or if 23295934Ssobomax * the argument refers to an existing file. 23395934Ssobomax */ 234131275Seik if (MatchType != MATCH_REGEX && MatchType != MATCH_EREGEX && !isfile(*argv)) 23572174Ssobomax while ((pkgs_split = strrchr(*argv, (int)'/')) != NULL) { 23672174Ssobomax *pkgs_split++ = '\0'; 23772174Ssobomax /* 23872174Ssobomax * If character after the '/' is alphanumeric or shell 23972174Ssobomax * metachar, then we've found the package name. Otherwise 24072174Ssobomax * we've come across a trailing '/' and need to continue our 24172174Ssobomax * quest. 24272174Ssobomax */ 243151188Skrion if (isalnum(*pkgs_split) || ((MatchType == MATCH_GLOB) && \ 24472174Ssobomax strpbrk(pkgs_split, "*?[]") != NULL)) { 24572174Ssobomax *argv = pkgs_split; 24672174Ssobomax break; 24772174Ssobomax } 24860563Ssteve } 24960563Ssteve *pkgs++ = *argv++; 25056001Sdan } 251327Sjkh 252327Sjkh /* If no packages, yelp */ 25374699Ssobomax if (pkgs == start && MatchType != MATCH_ALL && !CheckPkg && 25496030Ssobomax TAILQ_EMPTY(whead) && LookUpOrigin == NULL) 25530221Scharnier warnx("missing package name(s)"), usage(); 256327Sjkh *pkgs = NULL; 257327Sjkh return pkg_perform(start); 258327Sjkh} 259327Sjkh 26030221Scharnierstatic void 26130221Scharnierusage() 262327Sjkh{ 26396067Ssobomax fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n", 264131280Seik "usage: pkg_info [-bcdDEfgGiIjkLmopPqQrRsvVxX] [-e package] [-l prefix]", 265127641Scperciva " [-t template] -a | pkg-name ...", 266127641Scperciva " pkg_info [-qQ] -W filename", 267127641Scperciva " pkg_info [-qQ] -O origin", 268127641Scperciva " pkg_info"); 269327Sjkh exit(1); 270327Sjkh} 271