Created
August 21, 2011 16:11
-
-
Save dnaeon/1160798 to your computer and use it in GitHub Desktop.
print_info() - common interface for printing information from 'search' and 'info'
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/pkg/info.c b/pkg/info.c | |
index 6206f80..e554788 100644 | |
--- a/pkg/info.c | |
+++ b/pkg/info.c | |
@@ -2,7 +2,6 @@ | |
#include <err.h> | |
#include <inttypes.h> | |
-#include <libutil.h> | |
#include <pkg.h> | |
#include <stdbool.h> | |
#include <stdio.h> | |
@@ -11,6 +10,7 @@ | |
#include <unistd.h> | |
#include "info.h" | |
+#include "utils.h" | |
enum sign { | |
LT, | |
@@ -20,107 +20,11 @@ enum sign { | |
EQ | |
}; | |
-static int | |
-print_info(struct pkg * const pkg, unsigned int opt) | |
-{ | |
- struct pkg_dep *dep = NULL; | |
- struct pkg_file *file = NULL; | |
- struct pkg_category *cat = NULL; | |
- struct pkg_license *lic = NULL; | |
- struct pkg_option *option = NULL; | |
- char size[7]; | |
- | |
- if (opt & INFO_FULL) { | |
- printf("Name: %s\n", pkg_get(pkg, PKG_NAME)); | |
- printf("Version: %s\n", pkg_get(pkg, PKG_VERSION)); | |
- printf("Origin: %s\n", pkg_get(pkg, PKG_ORIGIN)); | |
- printf("Prefix: %s\n", pkg_get(pkg, PKG_PREFIX)); | |
- if (!pkg_list_isempty(pkg, PKG_CATEGORIES)) { | |
- printf("Categories:"); | |
- while (pkg_categories(pkg, &cat) == EPKG_OK) | |
- printf(" %s", pkg_category_name(cat)); | |
- printf("\n"); | |
- } | |
- if (!pkg_list_isempty(pkg, PKG_LICENSES)) { | |
- printf("Licenses: "); | |
- while (pkg_licenses(pkg, &lic) == EPKG_OK) { | |
- printf(" %s", pkg_license_name(lic)); | |
- if (pkg_licenselogic(pkg) != 1) | |
- printf(" %c", pkg_licenselogic(pkg)); | |
- else | |
- printf(" "); | |
- } | |
- printf("\b \n"); | |
- } | |
- printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER)); | |
- printf("WWW: %s\n", pkg_get(pkg, PKG_WWW)); | |
- printf("Comment: %s\n", pkg_get(pkg, PKG_COMMENT)); | |
- if (!pkg_list_isempty(pkg, PKG_OPTIONS)) { | |
- printf("Options: \n"); | |
- while (pkg_options(pkg, &option) == EPKG_OK) | |
- printf("\t%s: %s\n", pkg_option_opt(option), pkg_option_value(option)); | |
- } | |
- humanize_number(size, sizeof(size), pkg_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
- printf("Flat size: %s\n", size); | |
- printf("Description:\n%s\n", pkg_get(pkg, PKG_DESC)); | |
- printf("\n"); | |
- } else if (opt & INFO_PRINT_DEP) { | |
- if (!(opt & INFO_QUIET)) | |
- printf("%s-%s depends on:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
- | |
- while (pkg_deps(pkg, &dep) == EPKG_OK) { | |
- printf("%s-%s\n", pkg_dep_name(dep), pkg_dep_version(dep)); | |
- } | |
- | |
- if (!(opt & INFO_QUIET)) | |
- printf("\n"); | |
- } else if (opt & INFO_PRINT_RDEP) { | |
- if (!(opt & INFO_QUIET)) | |
- printf("%s-%s is required by:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
- | |
- while (pkg_rdeps(pkg, &dep) == EPKG_OK) { | |
- printf("%s-%s\n", pkg_dep_name(dep), pkg_dep_version(dep)); | |
- } | |
- | |
- if (!(opt & INFO_QUIET)) | |
- printf("\n"); | |
- } else if (opt & INFO_LIST_FILES) { | |
- if (!(opt & INFO_QUIET)) | |
- printf("%s-%s owns the following files:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
- | |
- while (pkg_files(pkg, &file) == EPKG_OK) { | |
- printf("%s\n", pkg_file_path(file)); | |
- } | |
- | |
- if (!(opt & INFO_QUIET)) | |
- printf("\n"); | |
- } else if (opt & INFO_SIZE) { | |
- humanize_number(size, sizeof(size), pkg_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
- printf("%s-%s size is %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), size); | |
- } else if (opt & INFO_ORIGIN) { | |
- if (opt & INFO_QUIET) | |
- printf("%s\n", pkg_get(pkg, PKG_ORIGIN)); | |
- else | |
- printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_ORIGIN)); | |
- } else if (opt & INFO_PREFIX) { | |
- if (opt & INFO_QUIET) | |
- printf("%s\n", pkg_get(pkg, PKG_PREFIX)); | |
- else | |
- printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_PREFIX)); | |
- } else { | |
- if (opt & INFO_QUIET) | |
- printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
- else | |
- printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_COMMENT)); | |
- } | |
- | |
- return (0); | |
-} | |
- | |
void | |
usage_info(void) | |
{ | |
- fprintf(stderr, "usage: pkg info -a\n"); | |
+ fprintf(stderr, "usage: pkg info <pkg-name>\n"); | |
+ fprintf(stderr, " pkg info -a\n"); | |
fprintf(stderr, " pkg info [-egxXdrlsqOf] <pkg-name>\n"); | |
fprintf(stderr, " pkg info [-drlsqf] -F <pkg-file>\n\n"); | |
fprintf(stderr, "For more information see 'pkg help info'.\n"); | |
diff --git a/pkg/search.c b/pkg/search.c | |
index 9715b73..8865191 100644 | |
--- a/pkg/search.c | |
+++ b/pkg/search.c | |
@@ -1,37 +1,37 @@ | |
#include <stdio.h> | |
#include <stdbool.h> | |
#include <unistd.h> | |
-#include <libutil.h> | |
#include <sysexits.h> | |
#include <pkg.h> | |
+#include "info.h" | |
#include "search.h" | |
+#include "utils.h" | |
void | |
usage_search(void) | |
{ | |
- fprintf(stderr, "usage, pkg search [-gxXcd] pattern\n"); | |
+ fprintf(stderr, "usage: pkg search <pkg-name>\n"); | |
+ fprintf(stderr, " pkg search [-fDsqop] <pkg-name>\n"); | |
+ fprintf(stderr, " pkg search [-gxXcdfDsqop] <pattern>\n\n"); | |
fprintf(stderr, "For more information see 'pkg help search'.\n"); | |
} | |
int | |
exec_search(int argc, char **argv) | |
{ | |
- char *pattern; | |
+ int retcode = EPKG_OK, ch; | |
+ int flags = PKG_LOAD_BASIC; | |
+ unsigned int opt = 0; | |
match_t match = MATCH_EXACT; | |
- int retcode = EPKG_OK; | |
pkgdb_field field = FIELD_NAME; | |
- int ch; | |
- char size[7]; | |
+ const char *pattern = NULL; | |
struct pkgdb *db = NULL; | |
struct pkgdb_it *it = NULL; | |
struct pkg *pkg = NULL; | |
- struct pkg_category *cat = NULL; | |
- struct pkg_license *lic = NULL; | |
- struct pkg_option *opt = NULL; | |
- while ((ch = getopt(argc, argv, "gxXcd")) != -1) { | |
+ while ((ch = getopt(argc, argv, "gxXcdfDsqop")) != -1) { | |
switch (ch) { | |
case 'g': | |
match = MATCH_GLOB; | |
@@ -48,6 +48,26 @@ exec_search(int argc, char **argv) | |
case 'd': | |
field = FIELD_DESC; | |
break; | |
+ case 'f': | |
+ opt |= INFO_FULL; | |
+ flags |= PKG_LOAD_CATEGORIES|PKG_LOAD_LICENSES|PKG_LOAD_OPTIONS; | |
+ break; | |
+ case 'D': | |
+ opt |= INFO_PRINT_DEP; | |
+ flags |= PKG_LOAD_DEPS; | |
+ break; | |
+ case 's': | |
+ opt |= INFO_SIZE; | |
+ break; | |
+ case 'q': | |
+ opt |= INFO_QUIET; | |
+ break; | |
+ case 'o': | |
+ opt |= INFO_ORIGIN; | |
+ break; | |
+ case 'p': | |
+ opt |= INFO_PREFIX; | |
+ break; | |
default: | |
usage_search(); | |
return (EX_USAGE); | |
@@ -64,54 +84,17 @@ exec_search(int argc, char **argv) | |
pattern = argv[0]; | |
- if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) { | |
- retcode = EPKG_FATAL; | |
- goto cleanup; | |
- } | |
+ if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) | |
+ return (EX_IOERR); | |
if ((it = pkgdb_rquery(db, pattern, match, field)) == NULL) { | |
- retcode = EPKG_FATAL; | |
- goto cleanup; | |
+ pkgdb_close(db); | |
+ return (1); | |
} | |
- while (( retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC|PKG_LOAD_CATEGORIES|PKG_LOAD_LICENSES|PKG_LOAD_OPTIONS)) == EPKG_OK) { | |
- printf("Name: %s\n", pkg_get(pkg, PKG_NAME)); | |
- printf("Version: %s\n", pkg_get(pkg, PKG_VERSION)); | |
- printf("Origin: %s\n", pkg_get(pkg, PKG_ORIGIN)); | |
- printf("Prefix: %s\n", pkg_get(pkg, PKG_PREFIX)); | |
- if (!pkg_list_isempty(pkg, PKG_CATEGORIES)) { | |
- printf("Categories:"); | |
- while (pkg_categories(pkg, &cat) == EPKG_OK) | |
- printf(" %s", pkg_category_name(cat)); | |
- printf("\n"); | |
- } | |
- if (!pkg_list_isempty(pkg, PKG_LICENSES)) { | |
- printf("Licenses: "); | |
- while (pkg_licenses(pkg, &lic) == EPKG_OK) { | |
- printf(" %s", pkg_license_name(lic)); | |
- if (pkg_licenselogic(pkg) != 1) | |
- printf(" %c", pkg_licenselogic(pkg)); | |
- else | |
- printf(" "); | |
- } | |
- printf("\b \n"); | |
- } | |
- printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER)); | |
- printf("WWW: %s\n", pkg_get(pkg, PKG_WWW)); | |
- printf("Comment: %s\n", pkg_get(pkg, PKG_COMMENT)); | |
- if (!pkg_list_isempty(pkg, PKG_OPTIONS)) { | |
- printf("Options: \n"); | |
- while (pkg_options(pkg, &opt) == EPKG_OK) | |
- printf("\t%s: %s\n", pkg_option_opt(opt), pkg_option_value(opt)); | |
- } | |
- humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
- printf("Flat size: %s\n", size); | |
- humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0); | |
- printf("Pkg size: %s\n", size); | |
- printf("\n"); | |
- } | |
+ while ((retcode = pkgdb_it_next(it, &pkg, flags)) == EPKG_OK) | |
+ print_info(pkg, opt); | |
- cleanup: | |
pkgdb_it_free(it); | |
pkgdb_close(db); | |
diff --git a/pkg/utils.c b/pkg/utils.c | |
index 7769f9a..47308a5 100644 | |
--- a/pkg/utils.c | |
+++ b/pkg/utils.c | |
@@ -3,9 +3,11 @@ | |
#include <err.h> | |
#include <errno.h> | |
+#include <libutil.h> | |
#include <string.h> | |
#include <pkg.h> | |
+#include "info.h" | |
#include "utils.h" | |
int | |
@@ -26,3 +28,119 @@ query_yesno(const char *msg) | |
return r; | |
} | |
+ | |
+int | |
+print_info(struct pkg * const pkg, unsigned int opt) | |
+{ | |
+ struct pkg_dep *dep = NULL; | |
+ struct pkg_file *file = NULL; | |
+ struct pkg_category *cat = NULL; | |
+ struct pkg_license *lic = NULL; | |
+ struct pkg_option *option = NULL; | |
+ char size[7]; | |
+ | |
+ if (opt & INFO_FULL) { | |
+ printf("Name: %s\n", pkg_get(pkg, PKG_NAME)); | |
+ printf("Version: %s\n", pkg_get(pkg, PKG_VERSION)); | |
+ printf("Origin: %s\n", pkg_get(pkg, PKG_ORIGIN)); | |
+ printf("Prefix: %s\n", pkg_get(pkg, PKG_PREFIX)); | |
+ | |
+ if (!pkg_list_isempty(pkg, PKG_CATEGORIES)) { | |
+ printf("Categories: "); | |
+ while (pkg_categories(pkg, &cat) == EPKG_OK) | |
+ printf("%s ", pkg_category_name(cat)); | |
+ printf("\n"); | |
+ } | |
+ | |
+ if (!pkg_list_isempty(pkg, PKG_LICENSES)) { | |
+ printf("Licenses: "); | |
+ while (pkg_licenses(pkg, &lic) == EPKG_OK) { | |
+ printf(" %s", pkg_license_name(lic)); | |
+ if (pkg_licenselogic(pkg) != 1) | |
+ printf(" %c", pkg_licenselogic(pkg)); | |
+ else | |
+ printf(" "); | |
+ } | |
+ printf("\b \n"); | |
+ } | |
+ printf("Maintainer: %s\n", pkg_get(pkg, PKG_MAINTAINER)); | |
+ printf("WWW: %s\n", pkg_get(pkg, PKG_WWW)); | |
+ printf("Comment: %s\n", pkg_get(pkg, PKG_COMMENT)); | |
+ | |
+ if (!pkg_list_isempty(pkg, PKG_OPTIONS)) { | |
+ printf("Options: \n"); | |
+ while (pkg_options(pkg, &option) == EPKG_OK) | |
+ printf("\t%s: %s\n", pkg_option_opt(option), pkg_option_value(option)); | |
+ } | |
+ | |
+ if (pkg_type(pkg) == PKG_INSTALLED) { | |
+ humanize_number(size, sizeof(size), pkg_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("Flat size: %s\n", size); | |
+ } else { | |
+ humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("Flat size: %s\n", size); | |
+ humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("Pkg size: %s\n", size); | |
+ } | |
+ | |
+ printf("Description:\n%s\n", pkg_get(pkg, PKG_DESC)); | |
+ printf("\n"); | |
+ } else if (opt & INFO_PRINT_DEP) { | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("%s-%s depends on:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
+ | |
+ while (pkg_deps(pkg, &dep) == EPKG_OK) { | |
+ printf("%s-%s\n", pkg_dep_name(dep), pkg_dep_version(dep)); | |
+ } | |
+ | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("\n"); | |
+ } else if (opt & INFO_PRINT_RDEP) { | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("%s-%s is required by:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
+ | |
+ while (pkg_rdeps(pkg, &dep) == EPKG_OK) { | |
+ printf("%s-%s\n", pkg_dep_name(dep), pkg_dep_version(dep)); | |
+ } | |
+ | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("\n"); | |
+ } else if (opt & INFO_LIST_FILES) { | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("%s-%s owns the following files:\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
+ | |
+ while (pkg_files(pkg, &file) == EPKG_OK) { | |
+ printf("%s\n", pkg_file_path(file)); | |
+ } | |
+ | |
+ if (!(opt & INFO_QUIET)) | |
+ printf("\n"); | |
+ } else if (opt & INFO_SIZE) { | |
+ if (pkg_type(pkg) == PKG_INSTALLED) { | |
+ humanize_number(size, sizeof(size), pkg_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("%s-%s size is: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), size); | |
+ } else { | |
+ humanize_number(size, sizeof(size), pkg_new_flatsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("%s-%s flat size is: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), size); | |
+ humanize_number(size, sizeof(size), pkg_new_pkgsize(pkg), "B", HN_AUTOSCALE, 0); | |
+ printf("%s-%s package size is: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), size); | |
+ } | |
+ } else if (opt & INFO_ORIGIN) { | |
+ if (opt & INFO_QUIET) | |
+ printf("%s\n", pkg_get(pkg, PKG_ORIGIN)); | |
+ else | |
+ printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_ORIGIN)); | |
+ } else if (opt & INFO_PREFIX) { | |
+ if (opt & INFO_QUIET) | |
+ printf("%s\n", pkg_get(pkg, PKG_PREFIX)); | |
+ else | |
+ printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_PREFIX)); | |
+ } else { | |
+ if (opt & INFO_QUIET) | |
+ printf("%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION)); | |
+ else | |
+ printf("%s-%s: %s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION), pkg_get(pkg, PKG_COMMENT)); | |
+ } | |
+ | |
+ return (0); | |
+} | |
diff --git a/pkg/utils.h b/pkg/utils.h | |
index 8548dd1..e0484a2 100644 | |
--- a/pkg/utils.h | |
+++ b/pkg/utils.h | |
@@ -2,6 +2,6 @@ | |
#define _UTILS_H | |
int query_yesno(const char *msg); | |
-int mkdirs(const char *path); | |
+int print_info(struct pkg * const pkg, unsigned int opt); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment