diff options
author | Ned Ludd <solar@gentoo.org> | 2005-06-07 02:17:24 +0000 |
---|---|---|
committer | Ned Ludd <solar@gentoo.org> | 2005-06-07 02:17:24 +0000 |
commit | 344e6e3452473cc0402842ab23b810b72888879b (patch) | |
tree | 44abbeeacbe1c80c25a8a4469434a83d79dfe26d /q.c | |
parent | rework the getopt stuff so its more consistent now (diff) | |
download | portage-utils-344e6e3452473cc0402842ab23b810b72888879b.tar.gz portage-utils-344e6e3452473cc0402842ab23b810b72888879b.tar.bz2 portage-utils-344e6e3452473cc0402842ab23b810b72888879b.zip |
- break applets up
Diffstat (limited to 'q.c')
-rw-r--r-- | q.c | 423 |
1 files changed, 8 insertions, 415 deletions
@@ -1,9 +1,10 @@ /* * Copyright 2005 Gentoo Foundation * Distributed under the terms of the GNU General Public License v2 - * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.9 2005/06/06 23:39:22 vapier Exp $ + * $Header: /var/cvsroot/gentoo-projects/portage-utils/q.c,v 1.10 2005/06/07 02:17:24 solar Exp $ * - * 2005 Ned Ludd <solar@gentoo.org> + * 2005 Ned Ludd - <solar@gentoo.org> + * 2005 Mike Frysinger - <vapier@gentoo.org> * ******************************************************************** * This program is free software; you can redistribute it and/or @@ -76,7 +77,7 @@ static char *argv0; # define DBG(fmt, args...) #endif -static const char *rcsid = "$Id: q.c,v 1.9 2005/06/06 23:39:22 vapier Exp $"; +static const char *rcsid = "$Id: q.c,v 1.10 2005/06/07 02:17:24 solar Exp $"; static char color = 1; static char exact = 0; @@ -243,418 +244,10 @@ char *initialize_portdir(void) return portdir; } -void qfile(char *path, char *fname) -{ - FILE *fp; - DIR *dir; - struct dirent *dentry; - char *p, *ptr; - size_t flen = strlen(fname); - int base = 0; - char buf[1024]; - - if (chdir(path) != 0 || (dir = opendir(path)) == NULL) - return; - - if (!strchr(fname, '/')) - base = 1; - else - base = 0; - - while ((dentry = readdir(dir))) { - if (*dentry->d_name == '.') - continue; - if ((asprintf(&p, "%s/%s/CONTENTS", path, dentry->d_name)) == (-1)) - continue; - if ((fp = fopen(p, "r")) == NULL) { - free(p); - continue; - } - free(p); - while ((fgets(buf, sizeof(buf), fp)) != NULL) { - /* FIXME: Need to port portage_versions.py to c to do this properly. */ - if ((p = strchr(buf, ' ')) == NULL) - continue; - *p++; - ptr = strdup(p); - if (!ptr) - continue; - if ((p = strchr(ptr, ' ')) != NULL) - *p++ = 0; - if (strncmp(base ? basename(ptr) : ptr, fname, flen) != 0 - || strlen(base ? basename(ptr) : ptr) != flen) { - free(ptr); - continue; - } - /* If the d_name is something like foo-3DVooDoo-0.0-r0 doing non - * exact matches would fail to display the name properly. - * /var/cvsroot/gentoo-x86/app-dicts/canna-2ch/ - * /var/cvsroot/gentoo-x86/net-dialup/intel-536ep/ - * /var/cvsroot/gentoo-x86/net-misc/cisco-vpnclient-3des/ - * /var/cvsroot/gentoo-x86/sys-cluster/openmosix-3dmon-stats/ - * /var/cvsroot/gentoo-x86/sys-cluster/openmosix-3dmon/ - */ - if (!exact && (p = strchr(dentry->d_name, '-')) != NULL) { - ++p; - if (*p >= '0' && *p <= '9') { - --p; - *p = 0; - } else { - /* tricky tricky.. I wish to advance to the second - */ - /* and repeat the first p strchr matching */ - char *q = strdup(p); - if (!q) { - free(ptr); - continue; - } - if ((p = strchr(q, '-')) != NULL) { - int l = 0; - ++p; - if (*p >= '0' && *p <= '9') { - --p; - *p = 0; - ++p; - l = strlen(dentry->d_name) - strlen(p) - 1; - dentry->d_name[l] = 0; - } - } - free(q); - } - } - - if (color) - printf("\e[0;01m%s/\e[36;01m%s\e[0m (%s)\n", basename(path), - dentry->d_name, ptr); - else - printf("%s/%s (%s)\n", basename(path), dentry->d_name, ptr); - - free(ptr); - fclose(fp); - closedir(dir); - found++; - return; - } - fclose(fp); - } - closedir(dir); - return; -} - -/* - * Taken from emerge manpage. - * --searchdesc (-S) - * Matches the search string against the description field as well as the package name. - * Take caution as the descriptions are also matched as regular expressions. - */ - -/* - * --search (-s) - * Searches for matches of the supplied string in the portage tree. The - * --search string is a regular expression. For example, - * emerge --search "^kde" searches for any package that starts with - * "kde"; emerge --search "gcc$" searches for any package that ends with - * "gcc"; emerge --search "office" searches for any package that - * contains the word "office". If you want to search the package - * descriptions as well, use the --searchdesc option. - */ - -#define QSEARCH_FLAGS "s" COMMON_FLAGS -static struct option const qsearch_long_opts[] = { - {"search", no_argument, NULL, 's'}, - {"searchdesc", a_argument, NULL, 'S'}, - COMMON_LONG_OPTS -}; -static const char *qsearch_opts_help[] = { - "Regex search package names", - "Regex search package descriptions", - COMMON_OPTS_HELP -}; -#define qsearch_usage(ret) usage(ret, QSEARCH_FLAGS, qsearch_long_opts, qsearch_opts_help, QSEARCH_IDX) - -int qsearch_main(int argc, char **argv) -{ - FILE *fp; - char buf[_POSIX_PATH_MAX]; - char ebuild[_POSIX_PATH_MAX]; - char last[126]; - char *p, *str, *q; - char *search_me; - char search_desc = 1; - int i; - - DBG("argc=%d argv[0]=%s argv[1]=%s", - argc, argv[0], argc > 1 ? argv[1] : "NULL?"); - - last[0] = 0; - initialize_ebuild_flat(); - fp = fopen(".ebuild.x", "r"); - if (!fp) - return 1; - - while ((i=getopt_long(argc, argv, QSEARCH_FLAGS, qsearch_long_opts, NULL)) != -1) { - switch (i) { - - case 'V': version_barf(); break; - case 'h': qsearch_usage(EXIT_SUCCESS); break; - - case 's': - search_desc = 0; - break; - case 'S': - search_desc = 1; - } - } - if (argc == optind) - qsearch_usage(EXIT_FAILURE); - search_me = argv[optind]; - - while ((fgets(ebuild, sizeof(ebuild), fp)) != NULL) { - - if ((p = strchr(ebuild, '\n')) != NULL) - *p = 0; - if (!ebuild[0]) - continue; - str = strdup(ebuild); - p = (char *) dirname(str); - - if ((strcmp(p, last)) != 0) { - FILE *newfp; - strncpy(last, p, sizeof(last)); - if (!search_desc) { - if ((rematch(search_me, basename(last), REG_EXTENDED | REG_ICASE)) == 0) - printf("%s\n", last); - continue; - } - if ((newfp = fopen(ebuild, "r")) != NULL) { - while ((fgets(buf, sizeof(buf), newfp)) != NULL) { - if ((strlen(buf) <= 13)) - continue; - if ((strncmp(buf, "DESCRIPTION=", 12)) == 0) { - if ((q = strrchr(buf, '"')) != NULL) - *q = 0; - if (strlen(buf) <= 12) - break; - q = buf + 13; - if (argc > 1) { - if ((rematch(search_me, q, REG_EXTENDED | REG_ICASE)) == 0) { - fprintf(stdout, "%s %s\n", p, q); - } - } else { - fprintf(stdout, "%s %s\n", p, q); - } - break; - } - } - fclose(newfp); - } else { - if (!reinitialize) - warnf("(cache update pending) %s : %s", ebuild, strerror(errno)); - reinitialize = 1; - } - } - free(str); - } - fclose(fp); - return EXIT_SUCCESS; -} - -int quse_main(int argc, char **argv) -{ - FILE *fp; - char *p; - char buf[_POSIX_PATH_MAX]; - char ebuild[_POSIX_PATH_MAX]; - - DBG("argc=%d argv[0]=%s argv[1]=%s", - argc, argv[0], argc > 1 ? argv[1] : "NULL?"); - - initialize_ebuild_flat(); /* sets our pwd to $PORTDIR */ - fp = fopen(".ebuild.x", "r"); - if (!fp) - return 1; - while ((fgets(ebuild, sizeof(ebuild), fp)) != NULL) { - FILE *newfp; - if ((p = strchr(ebuild, '\n')) != NULL) - *p = 0; - if ((newfp = fopen(ebuild, "r")) != NULL) { - while ((fgets(buf, sizeof(buf), newfp)) != NULL) { - if ((strncmp(buf, "IUSE=", 5)) == 0) { - if ((p = strrchr(&buf[6], '"')) != NULL) - *p = 0; - if (argc > 1) { - int i, ok = 0; - for (i = 1; i < argc; i++) { - if ((rematch(argv[i], &buf[6], REG_NOSUB)) != 0) { - ok = 1; - break; - } - } - if (ok == 0) - fprintf(stdout, "%s %s\n", ebuild, &buf[6]); - } else { - fprintf(stdout, "%s %s\n", ebuild, &buf[6]); - } - break; - } - } - fclose(newfp); - } else { - if (!reinitialize) - warnf("(cache update pending) %s : %s", ebuild, strerror(errno)); - reinitialize = 1; - } - } - fclose(fp); - return 0; -} - -#define QFILE_FLAGS "C" COMMON_FLAGS -static struct option const qfile_long_opts[] = { - {"exact", no_argument, NULL, 'e'}, - {"nocolor", no_argument, NULL, 'C'}, - COMMON_LONG_OPTS -}; -static const char *qfile_opts_help[] = { - "Exact match", - "Don't ouput color", - COMMON_OPTS_HELP -}; -#define qfile_usage(ret) usage(ret, QFILE_FLAGS, qfile_long_opts, qfile_opts_help, QFILE_IDX) - -int qfile_main(int argc, char **argv) -{ - DIR *dir; - struct dirent *dentry; - int i; - char *p; - const char *path = "/var/db/pkg"; - - DBG("argc=%d argv[0]=%s argv[1]=%s", - argc, argv[0], argc > 1 ? argv[1] : "NULL?"); - - while ((i=getopt_long(argc, argv, QFILE_FLAGS, qfile_long_opts, NULL)) != -1) { - switch (i) { - case 'V': version_barf(); break; - case 'h': qfile_usage(EXIT_SUCCESS); break; - } - } - if (argc == optind) - qfile_usage(EXIT_FAILURE); - - if ((chdir(path) == 0) && ((dir = opendir(path)))) { - while ((dentry = readdir(dir))) { - if (*dentry->d_name == '.') - continue; - for (i = 1; i < argc; i++) { - if (argv[i][0] != '-') { - if ((asprintf(&p, "%s/%s", path, dentry->d_name)) != (-1)) { - qfile(p, argv[i]); - free(p); - } - } else { - if (((strcmp(argv[i], "-nc")) == 0) - || ((strcmp(argv[i], "-C")) == 0)) - color = 0; - - if (((strcmp(argv[i], "-e")) == 0) - || ((strcmp(argv[i], "-exact")) == 0)) - exact = 1; - } - } - } - closedir(dir); - } - return (found ? EXIT_SUCCESS : EXIT_FAILURE); -} - -int qlist_main(int argc, char **argv) -{ - DIR *dir, *dirp; - struct dirent *dentry, *de; - char *cat, *p, *q; - const char *path = "/var/db/pkg"; - struct stat st; - size_t len = 0; - char buf[_POSIX_PATH_MAX]; - char buf2[_POSIX_PATH_MAX]; - - DBG("argc=%d argv[0]=%s argv[1]=%s", - argc, argv[0], argc > 1 ? argv[1] : "NULL?"); - - if (chdir(path) != 0 || (dir = opendir(path)) == NULL) - return 1; - - p = q = cat = NULL; - - if (argc > 1) { - cat = strchr(argv[1], '/'); - len = strlen(argv[1]); - } - while ((dentry = readdir(dir))) { - if (*dentry->d_name == '.') - continue; - if ((strchr((char *) dentry->d_name, '-')) == 0) - continue; - stat(dentry->d_name, &st); - if (!(S_ISDIR(st.st_mode))) - continue; - chdir(dentry->d_name); - if ((dirp = opendir(".")) == NULL) - continue; - while ((de = readdir(dirp))) { - if (*de->d_name == '.') - continue; - if (argc > 1) { - if (cat != NULL) { - snprintf(buf, sizeof(buf), "%s/%s", dentry->d_name, - de->d_name); - /*if ((rematch(argv[1], buf, REG_EXTENDED)) != 0)*/ - if ((strncmp(argv[1], buf, len)) != 0) - continue; - } else { - /* if ((rematch(argv[1], de->d_name, REG_EXTENDED)) != 0)*/ - if ((strncmp(argv[1], de->d_name, len)) != 0) - continue; - } - } - if (argc < 2) - printf("%s/%s\n", dentry->d_name, de->d_name); - else { - FILE *fp; - snprintf(buf, sizeof(buf), "/var/db/pkg/%s/%s/CONTENTS", - dentry->d_name, de->d_name); - if ((fp = fopen(buf, "r")) == NULL) - continue; - while ((fgets(buf, sizeof(buf), fp)) != NULL) { - if ((p = strchr(buf, ' ')) == NULL) - continue; - ++p; - switch (*buf) { - case '\n': /* newline */ - break; - case 'd': /* dir */ - /*printf("%s", p);*/ - break; - case 'o': /* obj */ - case 's': /* sym */ - strcpy(buf2, p); - if ((p = strchr(buf2, ' ')) != NULL) - *p = 0; - printf("%s\n", buf2); - break; - default: - break; - } - } - fclose(fp); - } - } - closedir(dirp); - chdir(".."); - } - closedir(dir); - return 0; -} +#include "qfile.c" +#include "qlist.c" +#include "qsearch.c" +#include "quse.c" void initialize_ebuild_flat(void) { |