From 15b4b5d63d18da25b9f6f168f27ca38c8fd13cfb Mon Sep 17 00:00:00 2001 From: Dave Reisner Date: Sat, 22 Nov 2014 10:13:24 -0500 Subject: apply ALPM style --- conf.c | 97 ++++++++++++++++----------- expac.c | 231 +++++++++++++++++++++++++++++++++++++++------------------------- util.h | 1 - 3 files changed, 200 insertions(+), 129 deletions(-) diff --git a/conf.c b/conf.c index a0248c8..976927b 100644 --- a/conf.c +++ b/conf.c @@ -1,32 +1,34 @@ #include #include +#include #include #include #include "conf.h" #include "util.h" -static size_t strtrim(char *str) { +static size_t strtrim(char *str) +{ char *left = str, *right; - if (!str || *str == '\0') { + if(!str || *str == '\0') { return 0; } - while (isspace((unsigned char)*left)) { + while(isspace((unsigned char)*left)) { left++; } - if (left != str) { + if(left != str) { memmove(str, left, (strlen(left) + 1)); left = str; } - if (*str == '\0') { + if(*str == '\0') { return 0; } right = strchr(str, '\0') - 1; - while (isspace((unsigned char)*right)) { + while(isspace((unsigned char)*right)) { right--; } *++right = '\0'; @@ -34,15 +36,17 @@ static size_t strtrim(char *str) { return right - left; } -int is_section(const char *s, int n) { +int is_section(const char *s, int n) +{ return s[0] == '[' && s[n-1] == ']'; } -static int config_add_repo(config_t *config, char *reponame) { +static int config_add_repo(config_t *config, char *reponame) +{ /* first time setup */ - if (config->repos == NULL) { + if(config->repos == NULL) { config->repos = calloc(10, sizeof(char*)); - if (config->repos == NULL) { + if(config->repos == NULL) { return -ENOMEM; } @@ -51,11 +55,11 @@ static int config_add_repo(config_t *config, char *reponame) { } /* grow when needed */ - if (config->size == config->capacity) { + if(config->size == config->capacity) { void *ptr; ptr = realloc(config->repos, config->capacity * 2.5 * sizeof(char*)); - if (ptr == NULL) { + if(ptr == NULL) { return -ENOMEM; } @@ -74,97 +78,109 @@ static int parse_include(config_t *config, const char *include, char **section) glob_t globbuf; int r = 0; - if (glob(include, GLOB_NOCHECK, NULL, &globbuf) != 0) { + if(glob(include, GLOB_NOCHECK, NULL, &globbuf) != 0) { fprintf(stderr, "warning: globbing failed on '%s': out of memory\n", include); return -ENOMEM; } - for (size_t i = 0; i < globbuf.gl_pathc; ++i) { + for(size_t i = 0; i < globbuf.gl_pathc; ++i) { r = parse_one_file(config, globbuf.gl_pathv[i], section); - if (r < 0) + if(r < 0) { break; + } } globfree(&globbuf); return r; } -static char *split_keyval(char *line, const char *sep) { +static char *split_keyval(char *line, const char *sep) +{ strsep(&line, sep); return line; } -static int parse_one_file(config_t *config, const char *filename, char **section) { +static int parse_one_file(config_t *config, const char *filename, char **section) +{ _cleanup_(fclosep) FILE *fp = NULL; _cleanup_free_ char *line = NULL; size_t n = 0; int in_options = 0; - if (*section) + if(*section) { in_options = strcmp(*section, "options") == 0; + } fp = fopen(filename, "r"); - if (fp == NULL) + if(fp == NULL) { return -errno; + } - for (;;) { + for(;;) { ssize_t len; errno = 0; len = getline(&line, &n, fp); - if (len < 0) { - if (errno != 0) + if(len < 0) { + if(errno != 0) { return -errno; + } /* EOF */ break; } len = strtrim(line); - if (len == 0 || line[0] == '#') + if(len == 0 || line[0] == '#') { continue; + } - if (is_section(line, len)) { + if(is_section(line, len)) { free(*section); *section = strndup(&line[1], len - 2); - if (*section == NULL) + if(*section == NULL) { return -ENOMEM; + } in_options = strcmp(*section, "options") == 0; - if (!in_options) { + if(!in_options) { int r; r = config_add_repo(config, *section); - if (r < 0) + if(r < 0) { return r; + } } continue; } - if (in_options && memchr(line, '=', len)) { + if(in_options && memchr(line, '=', len)) { char *val; val = split_keyval(line, "="); strtrim(line); - if (strcmp(line, "Include") == 0) { + if(strcmp(line, "Include") == 0) { int k; strtrim(val); k = parse_include(config, val, section); - if (k < 0) + if(k < 0) { return k; - } else if (strcmp(line, "DBPath") == 0) { + } + } else if(strcmp(line, "DBPath") == 0) { config->dbpath = strdup(val); - if (config->dbpath == NULL) + if(config->dbpath == NULL) { return -ENOMEM; - } else if (strcmp(line, "RootDir") == 0) { + } + } else if(strcmp(line, "RootDir") == 0) { config->dbroot = strdup(val); - if (config->dbpath == NULL) + if(config->dbpath == NULL) { return -ENOMEM; + } } } } @@ -172,20 +188,23 @@ static int parse_one_file(config_t *config, const char *filename, char **section return 0; } -void config_reset(config_t *config) { - if (config == NULL) +void config_reset(config_t *config) +{ + if(config == NULL) { return; + } - for (int i = 0; i < config->size; ++i) + for(int i = 0; i < config->size; ++i) { free(config->repos[i]); + } free(config->dbroot); free(config->dbpath); - free(config->repos); } -int config_parse(config_t *config, const char *filename) { +int config_parse(config_t *config, const char *filename) +{ _cleanup_free_ char *section = NULL; return parse_one_file(config, filename, §ion); diff --git a/expac.c b/expac.c index cc806ac..87fe2ff 100644 --- a/expac.c +++ b/expac.c @@ -66,16 +66,19 @@ int opt_pkgcounter = 0; typedef const char *(*extractfn)(void*); -static int is_valid_size_unit(char *u) { +static int is_valid_size_unit(char *u) +{ return u[0] != '\0' && u[1] == '\0' && memchr(SIZE_TOKENS, *u, strlen(SIZE_TOKENS)) != NULL; } -static const char *alpm_backup_get_name(alpm_backup_t *bkup) { +static const char *alpm_backup_get_name(alpm_backup_t *bkup) +{ return bkup->name; } -static double humanize_size(off_t bytes, const char target_unit, const char **label) +static double humanize_size(off_t bytes, const char target_unit, + const char **label) { static const char *labels[] = {"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"}; @@ -100,7 +103,8 @@ static double humanize_size(off_t bytes, const char target_unit, const char **la return val; } -static char *size_to_string(off_t pkgsize) { +static char *size_to_string(off_t pkgsize) +{ static char out[64]; if(opt_humansize == 'B') { @@ -112,21 +116,22 @@ static char *size_to_string(off_t pkgsize) { return out; } -static char *format_optdep(alpm_depend_t *optdep) { - char *out; +static char *format_optdep(alpm_depend_t *optdep) +{ + char *out = NULL; - if (asprintf(&out, "%s: %s", optdep->name, optdep->desc) < 0) { - return NULL; - } + asprintf(&out, "%s: %s", optdep->name, optdep->desc); return out; } -static const char *alpm_dep_get_name(void *dep) { - return ((alpm_depend_t*)dep)->name; +static const char *alpm_dep_get_name(alpm_depend_t *dep) +{ + return dep->name; } -static void usage(void) { +static void usage(void) +{ fprintf(stderr, "expac %s\n" "Usage: expac [options] target...\n\n", VERSION); fprintf(stderr, @@ -147,7 +152,8 @@ static void usage(void) { "For more details see expac(1).\n"); } -static int parse_options(int *argc, char **argv[]) { +static int parse_options(int *argc, char **argv[]) +{ static struct option opts[] = { {"readone", no_argument, 0, '1'}, {"delim", required_argument, 0, 'd'}, @@ -165,12 +171,13 @@ static int parse_options(int *argc, char **argv[]) { {0, 0, 0, 0} }; - for (;;) { + for(;;) { int opt; opt = getopt_long(*argc, *argv, "1l:d:gH:hf:pQSst:v", opts, NULL); - if (opt < 0) + if(opt < 0) { break; + } switch (opt) { case 'S': @@ -192,7 +199,7 @@ static int parse_options(int *argc, char **argv[]) { opt_listdelim = optarg; break; case 'H': - if (!is_valid_size_unit(optarg)) { + if(!is_valid_size_unit(optarg)) { fprintf(stderr, "error: invalid SI size formatter: %s\n", optarg); return 1; } @@ -224,9 +231,9 @@ static int parse_options(int *argc, char **argv[]) { } } - if (optind < *argc) + if(optind < *argc) { opt_format = (*argv)[optind++]; - else { + } else { fprintf(stderr, "error: missing format string (use -h for help)\n"); return -EINVAL; } @@ -237,12 +244,13 @@ static int parse_options(int *argc, char **argv[]) { return 0; } -static int print_escaped(const char *delim) { +static int print_escaped(const char *delim) +{ const char *f; int out = 0; - for (f = delim; *f != '\0'; f++) { - if (*f == '\\') { + for(f = delim; *f != '\0'; f++) { + if(*f == '\\') { switch (*++f) { case '\\': fputc('\\', stdout); @@ -288,27 +296,28 @@ static int print_escaped(const char *delim) { return out; } -static int print_list(alpm_list_t *list, extractfn fn) { +static int print_list(alpm_list_t *list, extractfn fn) +{ alpm_list_t *i; int out = 0; - if (!list) { - if (opt_verbose) { + if(!list) { + if(opt_verbose) { out += printf("None"); } return out; } i = list; - while (1) { - char *item = (char*)(fn ? fn(i->data) : i->data); - if (item == NULL) { + for(;;) { + const char *item = fn ? fn(i->data) : i->data; + if(item == NULL) { continue; } out += printf("%s", item); - if ((i = alpm_list_next(i))) { + if((i = i->next)) { out += print_escaped(opt_listdelim); } else { break; @@ -318,7 +327,8 @@ static int print_list(alpm_list_t *list, extractfn fn) { return out; } -static int print_allocated_list(alpm_list_t *list, extractfn fn) { +static int print_allocated_list(alpm_list_t *list, extractfn fn) +{ int out = print_list(list, fn); alpm_list_free(list); return out; @@ -328,9 +338,10 @@ static int print_time(time_t timestamp) { char buffer[64]; int out = 0; - if (!timestamp) { - if (opt_verbose) + if(!timestamp) { + if(opt_verbose) { out += printf("None"); + } return out; } @@ -341,11 +352,12 @@ static int print_time(time_t timestamp) { return out; } -static int print_filelist(alpm_filelist_t *filelist) { +static int print_filelist(alpm_filelist_t *filelist) +{ int out = 0; size_t i; - for (i = 0; i < filelist->count; i++) { + for(i = 0; i < filelist->count; i++) { out += printf("%s", (filelist->files + i)->name); out += print_escaped(opt_listdelim); } @@ -353,7 +365,8 @@ static int print_filelist(alpm_filelist_t *filelist) { return out; } -static bool backup_file_is_modified(const alpm_backup_t *backup_file) { +static bool backup_file_is_modified(const alpm_backup_t *backup_file) +{ char fullpath[PATH_MAX]; _cleanup_free_ char *md5sum = NULL; bool modified; @@ -371,7 +384,8 @@ static bool backup_file_is_modified(const alpm_backup_t *backup_file) { return modified; } -static alpm_list_t *get_modified_files(alpm_pkg_t *pkg) { +static alpm_list_t *get_modified_files(alpm_pkg_t *pkg) +{ alpm_list_t *i, *modified_files = NULL; for(i = alpm_pkg_get_backup(pkg); i; i = i->next) { @@ -384,7 +398,8 @@ static alpm_list_t *get_modified_files(alpm_pkg_t *pkg) { return modified_files; } -static alpm_list_t *get_validation_method(alpm_pkg_t *pkg) { +static alpm_list_t *get_validation_method(alpm_pkg_t *pkg) +{ alpm_list_t *validation = NULL; alpm_pkgvalidation_t v = alpm_pkg_get_validation(pkg); @@ -410,14 +425,15 @@ static alpm_list_t *get_validation_method(alpm_pkg_t *pkg) { return validation; } -static void print_pkg(alpm_pkg_t *pkg, const char *format) { +static void print_pkg(alpm_pkg_t *pkg, const char *format) +{ const char *f, *end; int out = 0; end = format + strlen(format); - for (f = format; f < end; f++) { - if (*f == '%') { + for(f = format; f < end; f++) { + if(*f == '%') { char fmt[64] = {0}; int l = 1; @@ -544,7 +560,7 @@ static void print_pkg(alpm_pkg_t *pkg, const char *format) { out++; break; } - } else if (*f == '\\') { + } else if(*f == '\\') { char esc[3] = { f[0], f[1], '\0' }; out += print_escaped(esc); ++f; @@ -555,37 +571,41 @@ static void print_pkg(alpm_pkg_t *pkg, const char *format) { } /* only print a delimeter if any package data was outputted */ - if (out > 0) + if(out > 0) { print_escaped(opt_delim); + } } -static alpm_list_t *all_packages(alpm_list_t *dbs) { +static alpm_list_t *all_packages(alpm_list_t *dbs) +{ alpm_list_t *i, *packages = NULL; - for (i = dbs; i; i = i->next) { + for(i = dbs; i; i = i->next) { packages = alpm_list_join(packages, alpm_list_copy(alpm_db_get_pkgcache(i->data))); } return packages; } -static alpm_list_t *search_packages(alpm_list_t *dbs, alpm_list_t *targets) { +static alpm_list_t *search_packages(alpm_list_t *dbs, alpm_list_t *targets) +{ alpm_list_t *i, *packages = NULL; - for (i = dbs; i; i = i->next) { + for(i = dbs; i; i = i->next) { packages = alpm_list_join(packages, alpm_db_search(i->data, targets)); } return packages; } -static alpm_list_t *search_groups(alpm_list_t *dbs, alpm_list_t *groupnames) { +static alpm_list_t *search_groups(alpm_list_t *dbs, alpm_list_t *groupnames) +{ alpm_list_t *i, *j, *packages = NULL; - for (i = groupnames; i; i = i->next) { - for (j = dbs; j; j = j->next) { + for(i = groupnames; i; i = i->next) { + for(j = dbs; j; j = j->next) { alpm_group_t *grp = alpm_db_get_group(j->data, i->data); - if (grp != NULL) { + if(grp != NULL) { packages = alpm_list_join(packages, alpm_list_copy(grp->packages)); } } @@ -594,66 +614,79 @@ static alpm_list_t *search_groups(alpm_list_t *dbs, alpm_list_t *groupnames) { return packages; } -static alpm_list_t *search_exact(alpm_list_t *dblist, alpm_list_t *targets) { +static alpm_list_t *search_exact(alpm_list_t *dblist, alpm_list_t *targets) +{ char *pkgname, *reponame; alpm_list_t *results = NULL; /* resolve each target individually from the repo pool */ - for (alpm_list_t *t = targets; t; t = t->next) { + for(alpm_list_t *t = targets; t; t = t->next) { alpm_pkg_t *pkg = NULL; alpm_list_t *r; int found = 0; pkgname = reponame = t->data; - if (strchr(pkgname, '/')) + if(strchr(pkgname, '/')) { strsep(&pkgname, "/"); - else + } else { reponame = NULL; + } - for (r = dblist; r; r = r->next) { + for(r = dblist; r; r = r->next) { alpm_db_t *repo = r->data; - if (reponame && strcmp(reponame, alpm_db_get_name(repo)) != 0) + if(reponame && strcmp(reponame, alpm_db_get_name(repo)) != 0) { continue; + } pkg = alpm_db_get_pkg(repo, pkgname); - if (pkg == NULL) + if(pkg == NULL) { continue; + } found = 1; results = alpm_list_add(results, pkg); - if (opt_readone) + if(opt_readone) { break; + } } - if (!found && opt_verbose) + if(!found && opt_verbose) { fprintf(stderr, "error: package `%s' not found\n", pkgname); + } } return results; } -static alpm_list_t *resolve_targets(alpm_list_t *dblist, alpm_list_t *targets) { - if (targets == NULL) +static alpm_list_t *resolve_targets(alpm_list_t *dblist, alpm_list_t *targets) +{ + if(targets == NULL) { return all_packages(dblist); + } - if (opt_what == SEARCH_REGEX) + if(opt_what == SEARCH_REGEX) { return search_packages(dblist, targets); + } - if (opt_what == SEARCH_GROUPS) + if(opt_what == SEARCH_GROUPS) { return search_groups(dblist, targets); + } return search_exact(dblist, targets); } -static void expac_free(expac_t *expac) { - if (expac == NULL) +static void expac_free(expac_t *expac) +{ + if(expac == NULL) { return; + } alpm_release(expac->alpm); } -static int expac_new(expac_t **expac, const char *config_file) { +static int expac_new(expac_t **expac, const char *config_file) +{ expac_t *e; enum _alpm_errno_t alpm_errno = 0; config_t config; @@ -662,27 +695,33 @@ static int expac_new(expac_t **expac, const char *config_file) { int r; e = calloc(1, sizeof(*e)); - if (e == NULL) + if(e == NULL) { return -ENOMEM; + } memset(&config, 0, sizeof(config)); r = config_parse(&config, config_file); - if (r < 0) + if(r < 0) { return r; + } - if (config.dbpath) + if(config.dbpath) { dbpath = config.dbpath; + } - if (config.dbroot) + if(config.dbroot) { dbroot = config.dbroot; + } e->alpm = alpm_initialize(dbroot, dbpath, &alpm_errno); - if (!e->alpm) + if(!e->alpm) { return -alpm_errno; + } - for (int i = 0; i < config.size; ++i) + for(int i = 0; i < config.size; ++i) { alpm_register_syncdb(e->alpm, config.repos[i], 0); + } config_reset(&config); @@ -691,14 +730,15 @@ static int expac_new(expac_t **expac, const char *config_file) { return 0; } -static alpm_list_t *expac_search_files(expac_t *expac, alpm_list_t *targets) { +static alpm_list_t *expac_search_files(expac_t *expac, alpm_list_t *targets) +{ alpm_list_t *i, *r = NULL; - for (i = targets; i; i = i->next) { + for(i = targets; i; i = i->next) { const char *path = i->data; alpm_pkg_t *pkg; - if (alpm_pkg_load(expac->alpm, path, 0, 0, &pkg) != 0) { + if(alpm_pkg_load(expac->alpm, path, 0, 0, &pkg) != 0) { fprintf(stderr, "error: %s: %s\n", path, alpm_strerror(alpm_errno(expac->alpm))); continue; @@ -710,7 +750,8 @@ static alpm_list_t *expac_search_files(expac_t *expac, alpm_list_t *targets) { return r; } -static alpm_list_t *expac_search_local(expac_t *expac, alpm_list_t *targets) { +static alpm_list_t *expac_search_local(expac_t *expac, alpm_list_t *targets) +{ alpm_list_t *dblist, *r; dblist = alpm_list_add(NULL, alpm_get_localdb(expac->alpm)); @@ -720,11 +761,13 @@ static alpm_list_t *expac_search_local(expac_t *expac, alpm_list_t *targets) { return r; } -static alpm_list_t *expac_search_sync(expac_t *expac, alpm_list_t *targets) { +static alpm_list_t *expac_search_sync(expac_t *expac, alpm_list_t *targets) +{ return resolve_targets(alpm_get_syncdbs(expac->alpm), targets); } -static alpm_list_t *expac_search(expac_t *expac, package_corpus_t corpus, alpm_list_t *targets) { +static alpm_list_t *expac_search(expac_t *expac, package_corpus_t corpus, alpm_list_t *targets) +{ switch (corpus) { case CORPUS_LOCAL: return expac_search_local(expac, targets); @@ -738,7 +781,8 @@ static alpm_list_t *expac_search(expac_t *expac, package_corpus_t corpus, alpm_l return NULL; } -static int read_targets_from_file(FILE *in, alpm_list_t **targets) { +static int read_targets_from_file(FILE *in, alpm_list_t **targets) +{ char line[BUFSIZ]; int i = 0, end = 0; while(!end) { @@ -767,51 +811,60 @@ static int read_targets_from_file(FILE *in, alpm_list_t **targets) { return 0; } -static alpm_list_t *process_targets(int argc, char **argv) { +static alpm_list_t *process_targets(int argc, char **argv) +{ alpm_list_t *r = NULL; int allow_stdin; allow_stdin = !isatty(STDIN_FILENO); - for (int i = 0; i < argc; ++i) { - if (allow_stdin && strcmp(argv[i], "-") == 0) { + for(int i = 0; i < argc; ++i) { + if(allow_stdin && strcmp(argv[i], "-") == 0) { int k; k = read_targets_from_file(stdin, &r); - if (k < 0) + if(k < 0) { return NULL; + } allow_stdin = 0; - } else + } else { r = alpm_list_add(r, strdup(argv[i])); + } } return r; } -int main(int argc, char *argv[]) { +int main(int argc, char *argv[]) +{ alpm_list_t *results = NULL, *targets = NULL; expac_t *expac; int r; r = parse_options(&argc, &argv); - if (r < 0) + if(r < 0) { return 1; + } targets = process_targets(argc, argv); - if (targets == NULL) + if(targets == NULL) { return 1; + } r = expac_new(&expac, opt_config_file); - if (r < 0) + if(r < 0) { return 1; + } results = expac_search(expac, opt_corpus, targets); - if (results == NULL) + if(results == NULL) { return 1; + } - for (alpm_list_t *i = results; i; i = i->next) + for(alpm_list_t *i = results; i; i = i->next) { print_pkg(i->data, opt_format); + } alpm_list_free_inner(targets, free); alpm_list_free(targets); diff --git a/util.h b/util.h index 1dcddb6..97e7ff7 100644 --- a/util.h +++ b/util.h @@ -1,7 +1,6 @@ #ifndef _UTIL_H #define _UTIL_H -#include #include #include -- cgit v1.2.3