summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason Woodward2018-07-03 19:47:48 -0400
committerJason Woodward2018-07-03 19:52:10 -0400
commit69ee231e6b93e29a27d6873f000c6432913e1d5d (patch)
tree5acaddde3f0c5372926bb3e139da07537195ca3a
parent69362c8058bb46ee1b47d0a89c631ae9bed4a76e (diff)
downloadslapt-get-69ee231e6b93e29a27d6873f000c6432913e1d5d.tar.gz
add/use foreach macros
-rw-r--r--src/action.c183
-rw-r--r--src/common.c14
-rw-r--r--src/common.h2
-rw-r--r--src/configuration.c18
-rw-r--r--src/configuration.h1
-rw-r--r--src/main.c12
-rw-r--r--src/package.c314
-rw-r--r--src/package.h3
-rw-r--r--src/transaction.c284
-rw-r--r--src/transaction.h1
10 files changed, 362 insertions, 470 deletions
diff --git a/src/action.c b/src/action.c
index 49fb02e..affd0de 100644
--- a/src/action.c
+++ b/src/action.c
@@ -24,7 +24,6 @@ static int cmp_pkg_arch(const char *a, const char *b);
void slapt_pkg_action_install(const slapt_rc_config *global_config,
const slapt_list_t *action_args)
{
- unsigned int i;
slapt_transaction_t *tran = NULL;
slapt_pkg_list_t *installed_pkgs = NULL;
slapt_pkg_list_t *avail_pkgs = NULL;
@@ -45,8 +44,7 @@ void slapt_pkg_action_install(const slapt_rc_config *global_config,
exit(EXIT_FAILURE);
}
- for (i = 0; i < action_args->count; ++i) {
- char *arg = action_args->items[i];
+ slapt_list_t_foreach(arg, action_args) {
slapt_pkg_info_t *pkg = NULL;
slapt_pkg_info_t *installed_pkg = NULL;
@@ -90,23 +88,20 @@ void slapt_pkg_action_install(const slapt_rc_config *global_config,
/* if there are conflicts, we schedule the conflicts for removal */
if (conflicts->pkg_count > 0 && global_config->ignore_dep != true) {
- unsigned int cindex = 0;
- for (cindex = 0; cindex < conflicts->pkg_count; cindex++) {
+ slapt_pkg_list_t_foreach(conflict, conflicts) {
/* make sure to remove the conflict's dependencies as well */
- unsigned int cdindex = 0;
slapt_pkg_list_t *conflict_deps = slapt_is_required_by(global_config,
avail_pkgs, installed_pkgs,
tran->install_pkgs, tran->remove_pkgs,
- conflicts->pkgs[cindex]);
- for (cdindex = 0; cdindex < conflict_deps->pkg_count; ++cdindex) {
- slapt_pkg_info_t *dep = conflict_deps->pkgs[cdindex];
- if (slapt_get_exact_pkg(installed_pkgs, dep->name, dep->version) != NULL) {
- slapt_add_remove_to_transaction(tran, dep);
+ conflict);
+ slapt_pkg_list_t_foreach(conflict_dep, conflict_deps) {
+ if (slapt_get_exact_pkg(installed_pkgs, conflict_dep->name, conflict_dep->version) != NULL) {
+ slapt_add_remove_to_transaction(tran, conflict_dep);
}
}
slapt_free_pkg_list(conflict_deps);
- slapt_add_remove_to_transaction(tran, conflicts->pkgs[cindex]);
+ slapt_add_remove_to_transaction(tran, conflict);
}
}
slapt_free_pkg_list(conflicts);
@@ -128,23 +123,20 @@ void slapt_pkg_action_install(const slapt_rc_config *global_config,
slapt_pkg_list_t *conflicts = slapt_is_conflicted(tran, avail_pkgs,
installed_pkgs, pkg);
if (conflicts->pkg_count > 0 && global_config->ignore_dep != true) {
- unsigned int cindex = 0;
- for (cindex = 0; cindex < conflicts->pkg_count; cindex++) {
+ slapt_pkg_list_t_foreach(conflict, conflicts) {
/* make sure to remove the conflict's dependencies as well */
- unsigned int cdindex = 0;
slapt_pkg_list_t *conflict_deps = slapt_is_required_by(global_config,
avail_pkgs, installed_pkgs,
tran->install_pkgs, tran->remove_pkgs,
- conflicts->pkgs[cindex]);
- for (cdindex = 0; cdindex < conflict_deps->pkg_count; ++cdindex) {
- slapt_pkg_info_t *dep = conflict_deps->pkgs[cdindex];
- if (slapt_get_exact_pkg(installed_pkgs, dep->name, dep->version) != NULL) {
- slapt_add_remove_to_transaction(tran, dep);
+ conflict);
+ slapt_pkg_list_t_foreach(conflict_dep, conflict_deps) {
+ if (slapt_get_exact_pkg(installed_pkgs, conflict_dep->name, conflict_dep->version) != NULL) {
+ slapt_add_remove_to_transaction(tran, conflict_dep);
}
}
slapt_free_pkg_list(conflict_deps);
- slapt_add_remove_to_transaction(tran, conflicts->pkgs[cindex]);
+ slapt_add_remove_to_transaction(tran, conflict);
}
}
@@ -185,25 +177,24 @@ void slapt_pkg_action_list(const int show)
{
slapt_pkg_list_t *pkgs = NULL;
slapt_pkg_list_t *installed_pkgs = NULL;
- unsigned int i;
pkgs = slapt_get_available_pkgs();
installed_pkgs = slapt_get_installed_pkgs();
if (show == LIST || show == AVAILABLE) {
- for (i = 0; i < pkgs->pkg_count; ++i) {
- unsigned int bool_installed = 0;
- char *short_description = slapt_gen_short_pkg_description(pkgs->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, pkgs) {
+ bool installed = false;
+ char *short_description = slapt_gen_short_pkg_description(pkg);
/* is it installed? */
- if (slapt_get_exact_pkg(installed_pkgs, pkgs->pkgs[i]->name,
- pkgs->pkgs[i]->version) != NULL)
- bool_installed = 1;
+ if (slapt_get_exact_pkg(installed_pkgs, pkg->name,
+ pkg->version) != NULL)
+ installed = true;
printf("%s-%s [inst=%s]: %s\n",
- pkgs->pkgs[i]->name,
- pkgs->pkgs[i]->version,
- bool_installed == 1
+ pkg->name,
+ pkg->version,
+ installed
? gettext("yes")
: gettext("no"),
(short_description == NULL) ? "" : short_description);
@@ -211,23 +202,20 @@ void slapt_pkg_action_list(const int show)
}
}
if (show == LIST || show == INSTALLED) {
- for (i = 0; i < installed_pkgs->pkg_count; ++i) {
+ slapt_pkg_list_t_foreach(pkg, installed_pkgs) {
char *short_description = NULL;
if (show == LIST) {
- if (slapt_get_exact_pkg(pkgs,
- installed_pkgs->pkgs[i]->name,
- installed_pkgs->pkgs[i]->version) != NULL) {
+ if (slapt_get_exact_pkg(pkgs, pkg->name, pkg->version) != NULL) {
continue;
}
}
- short_description =
- slapt_gen_short_pkg_description(installed_pkgs->pkgs[i]);
+ short_description = slapt_gen_short_pkg_description(pkg);
printf("%s-%s [inst=%s]: %s\n",
- installed_pkgs->pkgs[i]->name,
- installed_pkgs->pkgs[i]->version,
+ pkg->name,
+ pkg->version,
gettext("yes"),
(short_description == NULL) ? "" : short_description);
free(short_description);
@@ -242,7 +230,6 @@ void slapt_pkg_action_list(const int show)
void slapt_pkg_action_remove(const slapt_rc_config *global_config,
const slapt_list_t *action_args)
{
- unsigned int i;
slapt_pkg_list_t *installed_pkgs = NULL;
slapt_pkg_list_t *avail_pkgs = NULL;
slapt_regex_t *pkg_regex = NULL;
@@ -258,10 +245,8 @@ void slapt_pkg_action_remove(const slapt_rc_config *global_config,
exit(EXIT_FAILURE);
}
- for (i = 0; i < action_args->count; ++i) {
- unsigned int c;
+ slapt_list_t_foreach(arg, action_args) {
slapt_pkg_list_t *deps = NULL;
- char *arg = action_args->items[i];
slapt_pkg_info_t *pkg = NULL;
/* Use regex to see if they specified a particular version */
@@ -295,9 +280,7 @@ void slapt_pkg_action_remove(const slapt_rc_config *global_config,
tran->install_pkgs, tran->remove_pkgs,
pkg);
- for (c = 0; c < deps->pkg_count; ++c) {
- slapt_pkg_info_t *dep = deps->pkgs[c];
-
+ slapt_pkg_list_t_foreach(dep, deps) {
if (slapt_get_exact_pkg(installed_pkgs, dep->name, dep->version) != NULL) {
slapt_add_remove_to_transaction(tran, dep);
}
@@ -312,11 +295,11 @@ void slapt_pkg_action_remove(const slapt_rc_config *global_config,
slapt_pkg_list_t *obsolete = slapt_get_obsolete_pkgs(
global_config, avail_pkgs, installed_pkgs);
- for (i = 0; i < obsolete->pkg_count; ++i) {
- if (slapt_is_excluded(global_config, obsolete->pkgs[i]) != 1) {
- slapt_add_remove_to_transaction(tran, obsolete->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, obsolete) {
+ if (slapt_is_excluded(global_config, pkg)) {
+ slapt_add_remove_to_transaction(tran, pkg);
} else {
- slapt_add_exclude_to_transaction(tran, obsolete->pkgs[i]);
+ slapt_add_exclude_to_transaction(tran, pkg);
}
}
@@ -337,7 +320,6 @@ void slapt_pkg_action_remove(const slapt_rc_config *global_config,
/* search for a pkg (support extended POSIX regex) */
void slapt_pkg_action_search(const char *pattern)
{
- unsigned int i;
slapt_pkg_list_t *pkgs = NULL;
slapt_pkg_list_t *installed_pkgs = NULL;
slapt_pkg_list_t *matches = NULL, *i_matches = NULL;
@@ -349,34 +331,31 @@ void slapt_pkg_action_search(const char *pattern)
matches = slapt_search_pkg_list(pkgs, pattern);
i_matches = slapt_search_pkg_list(installed_pkgs, pattern);
- for (i = 0; i < matches->pkg_count; ++i) {
- char *short_description =
- slapt_gen_short_pkg_description(matches->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, matches) {
+ char *short_description = slapt_gen_short_pkg_description(pkg);
printf("%s-%s [inst=%s]: %s\n",
- matches->pkgs[i]->name,
- matches->pkgs[i]->version,
- (slapt_get_exact_pkg(installed_pkgs,
- matches->pkgs[i]->name, matches->pkgs[i]->version) != NULL)
+ pkg->name,
+ pkg->version,
+ (slapt_get_exact_pkg(installed_pkgs, pkg->name, pkg->version) != NULL)
? gettext("yes")
: gettext("no"),
short_description);
free(short_description);
}
- for (i = 0; i < i_matches->pkg_count; ++i) {
+ slapt_pkg_list_t_foreach(installed_pkg, i_matches) {
char *short_description = NULL;
- if (slapt_get_exact_pkg(matches, i_matches->pkgs[i]->name,
- i_matches->pkgs[i]->version) != NULL) {
+ if (slapt_get_exact_pkg(matches, installed_pkg->name, installed_pkg->version) != NULL) {
continue;
}
- short_description = slapt_gen_short_pkg_description(i_matches->pkgs[i]);
+ short_description = slapt_gen_short_pkg_description(installed_pkg);
printf("%s-%s [inst=%s]: %s\n",
- i_matches->pkgs[i]->name,
- i_matches->pkgs[i]->version,
+ installed_pkg->name,
+ installed_pkg->version,
gettext("yes"),
short_description);
free(short_description);
@@ -483,7 +462,6 @@ void slapt_pkg_action_show(const char *pkg_name)
/* upgrade all installed pkgs with available updates */
void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
{
- unsigned int i;
slapt_pkg_list_t *installed_pkgs = NULL;
slapt_pkg_list_t *avail_pkgs = NULL;
slapt_transaction_t *tran = NULL;
@@ -530,30 +508,26 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
}
/* loop through SLAPT_SLACK_BASE_SET_REGEX packages */
- for (i = 0; i < matches->pkg_count; ++i) {
+ slapt_pkg_list_t_foreach(pkg, matches) {
slapt_pkg_info_t *installed_pkg = NULL;
slapt_pkg_info_t *newer_avail_pkg = NULL;
slapt_pkg_info_t *slapt_upgrade_pkg = NULL;
- installed_pkg = slapt_get_newest_pkg(
- installed_pkgs,
- matches->pkgs[i]->name);
- newer_avail_pkg = slapt_get_newest_pkg(
- avail_pkgs,
- matches->pkgs[i]->name);
+ installed_pkg = slapt_get_newest_pkg(installed_pkgs, pkg->name);
+ newer_avail_pkg = slapt_get_newest_pkg(avail_pkgs, pkg->name);
/*
- * if there is a newer available version (such as from patches/)
- * use it instead
- */
- if (slapt_cmp_pkgs(matches->pkgs[i], newer_avail_pkg) < 0) {
+ * if there is a newer available version (such as from patches/)
+ * use it instead
+ */
+ if (slapt_cmp_pkgs(pkg, newer_avail_pkg) < 0) {
slapt_upgrade_pkg = newer_avail_pkg;
} else {
- slapt_upgrade_pkg = matches->pkgs[i];
+ slapt_upgrade_pkg = pkg;
}
/* add to install list if not already installed */
if (installed_pkg == NULL) {
- if (slapt_is_excluded(global_config, slapt_upgrade_pkg) == 1) {
+ if (slapt_is_excluded(global_config, slapt_upgrade_pkg)) {
slapt_add_exclude_to_transaction(tran, slapt_upgrade_pkg);
} else {
slapt_pkg_list_t *conflicts = slapt_is_conflicted(tran, avail_pkgs, installed_pkgs, slapt_upgrade_pkg);
@@ -577,7 +551,7 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
} else if (
(slapt_cmp_pkgs(installed_pkg, slapt_upgrade_pkg) <= 0) &&
strcmp(installed_pkg->version, slapt_upgrade_pkg->version) != 0) {
- if (slapt_is_excluded(global_config, installed_pkg) == 1 || slapt_is_excluded(global_config, slapt_upgrade_pkg) == 1) {
+ if (slapt_is_excluded(global_config, installed_pkg) || slapt_is_excluded(global_config, slapt_upgrade_pkg)) {
slapt_add_exclude_to_transaction(tran, slapt_upgrade_pkg);
} else {
slapt_pkg_list_t *conflicts = slapt_is_conflicted(tran, avail_pkgs, installed_pkgs,
@@ -606,15 +580,14 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
/* remove obsolete packages if prompted to */
if (global_config->remove_obsolete == true) {
- unsigned int r;
slapt_pkg_list_t *obsolete = slapt_get_obsolete_pkgs(
global_config, avail_pkgs, installed_pkgs);
- for (r = 0; r < obsolete->pkg_count; ++r) {
- if (slapt_is_excluded(global_config, obsolete->pkgs[r]) != 1) {
- slapt_add_remove_to_transaction(tran, obsolete->pkgs[r]);
+ slapt_pkg_list_t_foreach(obsolete_pkg, obsolete) {
+ if (!slapt_is_excluded(global_config, obsolete_pkg)) {
+ slapt_add_remove_to_transaction(tran, obsolete_pkg);
} else {
- slapt_add_exclude_to_transaction(tran, obsolete->pkgs[r]);
+ slapt_add_exclude_to_transaction(tran, obsolete_pkg);
}
}
@@ -632,48 +605,44 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
}
}
- for (i = 0; i < installed_pkgs->pkg_count; ++i) {
+ slapt_pkg_list_t_foreach(installed_pkg, installed_pkgs) {
slapt_pkg_info_t *update_pkg = NULL;
slapt_pkg_info_t *newer_installed_pkg = NULL;
/*
- we need to see if there is another installed
- package that is newer than this one
- */
+ we need to see if there is another installed
+ package that is newer than this one
+ */
if ((newer_installed_pkg =
- slapt_get_newest_pkg(installed_pkgs,
- installed_pkgs->pkgs[i]->name)) != NULL) {
- if (slapt_cmp_pkgs(installed_pkgs->pkgs[i], newer_installed_pkg) < 0) {
+ slapt_get_newest_pkg(installed_pkgs, installed_pkg->name)) != NULL) {
+ if (slapt_cmp_pkgs(installed_pkg, newer_installed_pkg) < 0) {
continue;
}
}
/* see if we have an available update for the pkg */
- update_pkg = slapt_get_newest_pkg(
- avail_pkgs,
- installed_pkgs->pkgs[i]->name);
+ update_pkg = slapt_get_newest_pkg(avail_pkgs, installed_pkg->name);
if (update_pkg != NULL) {
int cmp_r = 0;
/* if the update has a newer version, attempt to upgrade */
- cmp_r = slapt_cmp_pkgs(installed_pkgs->pkgs[i], update_pkg);
+ cmp_r = slapt_cmp_pkgs(installed_pkg, update_pkg);
if (
/* either it's greater, or we want to reinstall */
cmp_r < 0 || (global_config->re_install == true) ||
/*
- * or this is a dist upgrade and the versions are the save
- * except for the arch
- */
+ * or this is a dist upgrade and the versions are the save
+ * except for the arch
+ */
(
global_config->dist_upgrade == true &&
cmp_r == 0 &&
- cmp_pkg_arch(installed_pkgs->pkgs[i]->version,
+ cmp_pkg_arch(installed_pkg->version,
update_pkg->version) != 0)) {
- if ((slapt_is_excluded(global_config, update_pkg) == 1) || (slapt_is_excluded(global_config, installed_pkgs->pkgs[i]) == 1)) {
+ if ((slapt_is_excluded(global_config, update_pkg)) || (slapt_is_excluded(global_config, installed_pkg))) {
slapt_add_exclude_to_transaction(tran, update_pkg);
} else {
- slapt_pkg_list_t *conflicts = slapt_is_conflicted(tran,
- avail_pkgs, installed_pkgs, update_pkg);
+ slapt_pkg_list_t *conflicts = slapt_is_conflicted(tran, avail_pkgs, installed_pkgs, update_pkg);
/* if all deps are added and there is no conflicts, add on */
if (
@@ -681,9 +650,9 @@ void slapt_pkg_action_upgrade_all(const slapt_rc_config *global_config)
installed_pkgs, update_pkg) == 0) &&
(global_config->ignore_dep == true || (conflicts->pkg_count == 0))) {
if (cmp_r == 0)
- slapt_add_reinstall_to_transaction(tran, installed_pkgs->pkgs[i], update_pkg);
+ slapt_add_reinstall_to_transaction(tran, installed_pkg, update_pkg);
else
- slapt_add_upgrade_to_transaction(tran, installed_pkgs->pkgs[i], update_pkg);
+ slapt_add_upgrade_to_transaction(tran, installed_pkg, update_pkg);
} else {
/* otherwise exclude */
@@ -751,10 +720,10 @@ static int cmp_pkg_arch(const char *a, const char *b)
void slapt_pkg_action_add_keys(const slapt_rc_config *global_config)
{
int rc = 0;
- unsigned int s = 0, compressed = 0;
- for (s = 0; s < global_config->sources->count; s++) {
+ bool compressed = false;
+ slapt_source_list_t_foreach(source, global_config->sources) {
FILE *gpg_key = NULL;
- const char *source_url = global_config->sources->src[s]->url;
+ const char *source_url = source->url;
printf(gettext("Retrieving GPG key [%s]..."), source_url);
gpg_key = slapt_get_pkg_source_gpg_key(global_config, source_url, &compressed);
if (gpg_key != NULL) {
diff --git a/src/common.c b/src/common.c
index ac18814..dff01db 100644
--- a/src/common.c
+++ b/src/common.c
@@ -453,11 +453,9 @@ void slapt_remove_list_item(slapt_list_t *list, const char *item)
const char *slapt_search_list(slapt_list_t *list, const char *needle)
{
- unsigned int i;
-
- for (i = 0; i < list->count; i++) {
- if (strcmp(list->items[i], needle) == 0)
- return list->items[i];
+ slapt_list_t_foreach(i, list) {
+ if (strcmp(i, needle) == 0)
+ return i;
}
return NULL;
@@ -465,10 +463,8 @@ const char *slapt_search_list(slapt_list_t *list, const char *needle)
void slapt_free_list(slapt_list_t *list)
{
- unsigned int i;
-
- for (i = 0; i < list->count; ++i) {
- free(list->items[i]);
+ slapt_list_t_foreach(i, list) {
+ free(i);
}
free(list->items);
free(list);
diff --git a/src/common.h b/src/common.h
index fc55898..71d9857 100644
--- a/src/common.h
+++ b/src/common.h
@@ -71,6 +71,8 @@ typedef struct {
char **items;
uint32_t count;
} slapt_list_t;
+#define slapt_list_t_foreach(item, list) char *item; for (uint32_t item##_counter = 0; item = list->items[item##_counter], item##_counter < list->count; item##_counter++)
+
FILE *slapt_open_file(const char *file_name, const char *mode);
slapt_regex_t *slapt_init_regex(const char *regex_string);
diff --git a/src/configuration.c b/src/configuration.c
index f92b7a6..358a4ef 100644
--- a/src/configuration.c
+++ b/src/configuration.c
@@ -251,10 +251,8 @@ void slapt_remove_source(slapt_source_list_t *list, const char *s)
void slapt_free_source_list(slapt_source_list_t *list)
{
- unsigned int i;
-
- for (i = 0; i < list->count; ++i) {
- slapt_free_source(list->src[i]);
+ slapt_source_list_t_foreach(source, list) {
+ slapt_free_source(source);
}
free(list->src);
free(list);
@@ -392,21 +390,21 @@ int slapt_write_rc_config(const slapt_rc_config *global_config, const char *loca
fprintf(rc, "%s%s\n", SLAPT_WORKINGDIR_TOKEN, global_config->working_dir);
fprintf(rc, "%s", SLAPT_EXCLUDE_TOKEN);
- for (i = 0; i < global_config->exclude_list->count; ++i) {
+ slapt_list_t_foreach(exclude, global_config->exclude_list) {
if (i + 1 == global_config->exclude_list->count) {
- fprintf(rc, "%s", global_config->exclude_list->items[i]);
+ fprintf(rc, "%s", exclude);
} else {
- fprintf(rc, "%s,", global_config->exclude_list->items[i]);
+ fprintf(rc, "%s,", exclude);
}
+ i++;
}
fprintf(rc, "\n");
- for (i = 0; i < global_config->sources->count; ++i) {
- slapt_source_t *src = global_config->sources->src[i];
+ slapt_source_list_t_foreach(src, global_config->sources) {
SLAPT_PRIORITY_T priority = src->priority;
const char *token = SLAPT_SOURCE_TOKEN;
- if (global_config->sources->src[i]->disabled == true)
+ if (src->disabled == true)
token = SLAPT_DISABLED_SOURCE_TOKEN;
if (priority > SLAPT_PRIORITY_DEFAULT) {
diff --git a/src/configuration.h b/src/configuration.h
index 2bce6c9..7eb548c 100644
--- a/src/configuration.h
+++ b/src/configuration.h
@@ -33,6 +33,7 @@ typedef struct {
slapt_source_t **src;
uint32_t count;
} slapt_source_list_t;
+#define slapt_source_list_t_foreach(item, list) slapt_source_t *item; for (uint32_t item##_counter = 0; item = list->src[item##_counter], item##_counter < list->count; item##_counter++)
typedef struct {
char working_dir[SLAPT_WORKINGDIR_TOKEN_LEN];
diff --git a/src/main.c b/src/main.c
index 277f51a..3691504 100644
--- a/src/main.c
+++ b/src/main.c
@@ -328,12 +328,10 @@ int main(int argc, char *argv[])
slapt_free_list(paa);
break;
case INSTALL_DISK_SET: {
- unsigned int set_i;
slapt_pkg_list_t *set_pkgs = slapt_init_pkg_list();
slapt_pkg_list_t *avail_pkgs = slapt_get_available_pkgs();
while (optind < argc) {
- unsigned int search_i;
slapt_pkg_list_t *matches = NULL;
char *search = slapt_malloc(sizeof *search * (strlen(argv[optind]) + 3));
@@ -341,9 +339,9 @@ int main(int argc, char *argv[])
matches = slapt_search_pkg_list(avail_pkgs, search);
free(search);
- for (search_i = 0; search_i < matches->pkg_count; ++search_i) {
- if (slapt_is_excluded(global_config, matches->pkgs[search_i]) == 0) {
- slapt_add_pkg_to_pkg_list(set_pkgs, matches->pkgs[search_i]);
+ slapt_pkg_list_t_foreach(match, matches) {
+ if (!slapt_is_excluded(global_config, match)) {
+ slapt_add_pkg_to_pkg_list(set_pkgs, match);
}
}
@@ -353,8 +351,8 @@ int main(int argc, char *argv[])
paa = slapt_init_list();
- for (set_i = 0; set_i < set_pkgs->pkg_count; ++set_i) {
- slapt_add_list_item(paa, set_pkgs->pkgs[set_i]->name);
+ slapt_pkg_list_t_foreach(set_pkg, set_pkgs) {
+ slapt_add_list_item(paa, set_pkg->name);
}
slapt_free_pkg_list(set_pkgs);
diff --git a/src/package.c b/src/package.c
index f4a2dcb..14f87bd 100644
--- a/src/package.c
+++ b/src/package.c
@@ -680,19 +680,14 @@ slapt_pkg_list_t *slapt_get_installed_pkgs(void)
slapt_pkg_info_t *slapt_get_newest_pkg(slapt_pkg_list_t *pkg_list,
const char *pkg_name)
{
- unsigned int i;
- slapt_pkg_info_t *pkg = NULL;
-
- for (i = 0; i < pkg_list->pkg_count; ++i) {
- /* if pkg has same name as our requested pkg */
- if ((strcmp(pkg_list->pkgs[i]->name, pkg_name)) == 0) {
- if ((pkg == NULL) || (slapt_cmp_pkgs(pkg, pkg_list->pkgs[i]) < 0)) {
- pkg = pkg_list->pkgs[i];
- }
- }
+ slapt_pkg_info_t *found = NULL;
+ slapt_pkg_list_t_foreach(pkg, pkg_list) {
+ if (strcmp(pkg->name, pkg_name) != 0)
+ continue;
+ if ((found == NULL) || (slapt_cmp_pkgs(found, pkg) < 0))
+ found = pkg;
}
-
- return pkg;
+ return found;
}
slapt_pkg_info_t *slapt_get_exact_pkg(slapt_pkg_list_t *list,
@@ -728,12 +723,9 @@ slapt_pkg_info_t *slapt_get_exact_pkg(slapt_pkg_list_t *list,
}
} else {
- unsigned int i;
-
- for (i = 0; i < list->pkg_count; i++) {
- if ((strcmp(name, list->pkgs[i]->name) == 0) &&
- (strcmp(version, list->pkgs[i]->version) == 0)) {
- return list->pkgs[i];
+ slapt_pkg_list_t_foreach(pkg, list) {
+ if ((strcmp(name, pkg->name) == 0) && (strcmp(version, pkg->version) == 0)) {
+ return pkg;
}
}
}
@@ -859,10 +851,9 @@ void slapt_free_pkg(slapt_pkg_info_t *pkg)
void slapt_free_pkg_list(slapt_pkg_list_t *list)
{
- unsigned int i;
if (list->free_pkgs == true) {
- for (i = 0; i < list->pkg_count; i++) {
- slapt_free_pkg(list->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, list) {
+ slapt_free_pkg(pkg);
}
}
free(list->pkgs);
@@ -872,7 +863,6 @@ void slapt_free_pkg_list(slapt_pkg_list_t *list)
bool slapt_is_excluded(const slapt_rc_config *global_config,
slapt_pkg_info_t *pkg)
{
- unsigned int i, pkg_not_excluded = 0, pkg_slapt_is_excluded = 1;
int name_reg_ret = -1, version_reg_ret = -1, location_reg_ret = -1;
if (global_config->ignore_excludes == true)
@@ -882,7 +872,7 @@ bool slapt_is_excluded(const slapt_rc_config *global_config,
if (global_config->exclude_list->count == 0)
return false;
- for (i = 0; i < global_config->exclude_list->count; i++) {
+ slapt_list_t_foreach(exclude, global_config->exclude_list) {
slapt_regex_t *exclude_reg = NULL;
/* return if its an exact match */
@@ -890,12 +880,10 @@ bool slapt_is_excluded(const slapt_rc_config *global_config,
return true;
/*
- this regex has to be init'd and free'd within the loop b/c the regex is pulled
- from the exclude list
- */
- if ((exclude_reg =
- slapt_init_regex(global_config->exclude_list->items[i])) == NULL) {
- fprintf(stderr, "\n\nugh %s\n\n", global_config->exclude_list->items[i]);
+ this regex has to be init'd and free'd within the loop b/c the regex is pulled
+ from the exclude list
+ */
+ if ((exclude_reg = slapt_init_regex(exclude)) == NULL) {
continue;
}
@@ -924,7 +912,6 @@ void slapt_get_md5sums(slapt_pkg_list_t *pkgs, FILE *checksum_file)
ssize_t getline_read;
size_t getline_len = 0;
char *getline_buffer = NULL;
- unsigned int a;
if ((md5sum_regex = slapt_init_regex(SLAPT_MD5SUM_REGEX)) == NULL) {
exit(EXIT_FAILURE);
@@ -957,12 +944,12 @@ void slapt_get_md5sums(slapt_pkg_list_t *pkgs, FILE *checksum_file)
version = slapt_regex_extract_match(md5sum_regex, getline_buffer, 4);
/* see if we can match up name, version, and location */
- for (a = 0; a < pkgs->pkg_count; a++) {
+ slapt_pkg_list_t_foreach(pkg, pkgs) {
if (
- (strcmp(pkgs->pkgs[a]->name, name) == 0) &&
- (slapt_cmp_pkg_versions(pkgs->pkgs[a]->version, version) == 0) &&
- (strcmp(pkgs->pkgs[a]->location, location) == 0)) {
- memcpy(pkgs->pkgs[a]->md5, sum, SLAPT_MD5_STR_LEN);
+ (strcmp(pkg->name, name) == 0) &&
+ (slapt_cmp_pkg_versions(pkg->version, version) == 0) &&
+ (strcmp(pkg->location, location) == 0)) {
+ memcpy(pkg->md5, sum, SLAPT_MD5_STR_LEN);
break;
}
}
@@ -1206,41 +1193,38 @@ static struct slapt_pkg_version_parts *break_down_pkg_version(const char *versio
void slapt_write_pkg_data(const char *source_url, FILE *d_file,
slapt_pkg_list_t *pkgs)
{
- unsigned int i;
-
- for (i = 0; i < pkgs->pkg_count; i++) {
- fprintf(d_file, "PACKAGE NAME: %s-%s%s\n",
- pkgs->pkgs[i]->name, pkgs->pkgs[i]->version, pkgs->pkgs[i]->file_ext);
- if (pkgs->pkgs[i]->mirror != NULL && strlen(pkgs->pkgs[i]->mirror) > 0) {
- fprintf(d_file, "PACKAGE MIRROR: %s\n", pkgs->pkgs[i]->mirror);
+ slapt_pkg_list_t_foreach(pkg, pkgs) {
+ fprintf(d_file, "PACKAGE NAME: %s-%s%s\n", pkg->name, pkg->version, pkg->file_ext);
+ if (pkg->mirror != NULL && strlen(pkg->mirror) > 0) {
+ fprintf(d_file, "PACKAGE MIRROR: %s\n", pkg->mirror);
} else {
fprintf(d_file, "PACKAGE MIRROR: %s\n", source_url);
}
- fprintf(d_file, "PACKAGE PRIORITY: %d\n", pkgs->pkgs[i]->priority);
- fprintf(d_file, "PACKAGE LOCATION: %s\n", pkgs->pkgs[i]->location);
- fprintf(d_file, "PACKAGE SIZE (compressed): %d K\n", pkgs->pkgs[i]->size_c);
- fprintf(d_file, "PACKAGE SIZE (uncompressed): %d K\n", pkgs->pkgs[i]->size_u);
- fprintf(d_file, "PACKAGE REQUIRED: %s\n", pkgs->pkgs[i]->required);
- fprintf(d_file, "PACKAGE CONFLICTS: %s\n", pkgs->pkgs[i]->conflicts);
- fprintf(d_file, "PACKAGE SUGGESTS: %s\n", pkgs->pkgs[i]->suggests);
- fprintf(d_file, "PACKAGE MD5SUM: %s\n", pkgs->pkgs[i]->md5);
+ fprintf(d_file, "PACKAGE PRIORITY: %d\n", pkg->priority);
+ fprintf(d_file, "PACKAGE LOCATION: %s\n", pkg->location);
+ fprintf(d_file, "PACKAGE SIZE (compressed): %d K\n", pkg->size_c);
+ fprintf(d_file, "PACKAGE SIZE (uncompressed): %d K\n", pkg->size_u);
+ fprintf(d_file, "PACKAGE REQUIRED: %s\n", pkg->required);
+ fprintf(d_file, "PACKAGE CONFLICTS: %s\n", pkg->conflicts);
+ fprintf(d_file, "PACKAGE SUGGESTS: %s\n", pkg->suggests);
+ fprintf(d_file, "PACKAGE MD5SUM: %s\n", pkg->md5);
fprintf(d_file, "PACKAGE DESCRIPTION:\n");
/* do we have to make up an empty description? */
- if (strlen(pkgs->pkgs[i]->description) < strlen(pkgs->pkgs[i]->name)) {
- fprintf(d_file, "%s: no description\n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n", pkgs->pkgs[i]->name);
- fprintf(d_file, "%s: \n\n", pkgs->pkgs[i]->name);
+ if (strlen(pkg->description) < strlen(pkg->name)) {
+ fprintf(d_file, "%s: no description\n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n", pkg->name);
+ fprintf(d_file, "%s: \n\n", pkg->name);
} else {
- fprintf(d_file, "%s\n", pkgs->pkgs[i]->description);
+ fprintf(d_file, "%s\n", pkg->description);
}
}
}
@@ -1248,7 +1232,6 @@ void slapt_write_pkg_data(const char *source_url, FILE *d_file,
slapt_pkg_list_t *slapt_search_pkg_list(slapt_pkg_list_t *list,
const char *pattern)
{
- unsigned int i;
int name_r = -1, desc_r = -1, loc_r = -1, version_r = -1;
slapt_regex_t *search_regex = NULL;
slapt_pkg_list_t *matches = NULL;
@@ -1258,31 +1241,31 @@ slapt_pkg_list_t *slapt_search_pkg_list(slapt_pkg_list_t *list,
if ((search_regex = slapt_init_regex(pattern)) == NULL)
return matches;
- for (i = 0; i < list->pkg_count; i++) {
- if (strcmp(list->pkgs[i]->name, pattern) == 0) {
- slapt_add_pkg_to_pkg_list(matches, list->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, list) {
+ if (strcmp(pkg->name, pattern) == 0) {
+ slapt_add_pkg_to_pkg_list(matches, pkg);
continue;
}
- slapt_execute_regex(search_regex, list->pkgs[i]->name);
+ slapt_execute_regex(search_regex, pkg->name);
name_r = search_regex->reg_return;
- slapt_execute_regex(search_regex, list->pkgs[i]->version);
+ slapt_execute_regex(search_regex, pkg->version);
version_r = search_regex->reg_return;
- if (list->pkgs[i]->description != NULL) {
- slapt_execute_regex(search_regex, list->pkgs[i]->description);
+ if (pkg->description != NULL) {
+ slapt_execute_regex(search_regex, pkg->description);
desc_r = search_regex->reg_return;
}
- if (list->pkgs[i]->location != NULL) {
- slapt_execute_regex(search_regex, list->pkgs[i]->location);
+ if (pkg->location != NULL) {
+ slapt_execute_regex(search_regex, pkg->location);
loc_r = search_regex->reg_return;
}
/* search pkg name, pkg description, pkg location */
if (name_r == 0 || version_r == 0 || desc_r == 0 || loc_r == 0) {
- slapt_add_pkg_to_pkg_list(matches, list->pkgs[i]);
+ slapt_add_pkg_to_pkg_list(matches, pkg);
}
}
slapt_free_regex(search_regex);
@@ -1322,24 +1305,24 @@ int slapt_get_pkg_dependencies(const slapt_rc_config *global_config,
/* parse dep line */
dep_parts = slapt_parse_delimited_list(pkg->required, ',');
- for (i = 0; i < dep_parts->count; i++) {
+ slapt_list_t_foreach(part, dep_parts) {
slapt_pkg_info_t *tmp_pkg = NULL;
- if (strchr(dep_parts->items[i], '|') != NULL) {
- tmp_pkg = find_or_requirement(avail_pkgs, installed_pkgs, dep_parts->items[i]);
+ if (strchr(part, '|') != NULL) {
+ tmp_pkg = find_or_requirement(avail_pkgs, installed_pkgs, part);
} else {
- tmp_pkg = parse_meta_entry(avail_pkgs, installed_pkgs, dep_parts->items[i]);
+ tmp_pkg = parse_meta_entry(avail_pkgs, installed_pkgs, part);
}
if (tmp_pkg == NULL) {
/* if we can't find a required dep, return -1 */
- slapt_add_pkg_err_to_list(missing_err, pkg->name, dep_parts->items[i]);
+ slapt_add_pkg_err_to_list(missing_err, pkg->name, part);
slapt_free_list(dep_parts);
return -1;
}
/* if this pkg is excluded */
- if ((slapt_is_excluded(global_config, tmp_pkg) == 1) && (global_config->ignore_dep == false)) {
+ if ((slapt_is_excluded(global_config, tmp_pkg)) && (global_config->ignore_dep == false)) {
if (slapt_get_exact_pkg(installed_pkgs, tmp_pkg->name, tmp_pkg->version) == NULL) {
slapt_add_pkg_err_to_list(conflict_err, pkg->name, tmp_pkg->name);
slapt_free_list(dep_parts);
@@ -1459,7 +1442,6 @@ static slapt_pkg_info_t *parse_meta_entry(slapt_pkg_list_t *avail_pkgs,
slapt_pkg_list_t *installed_pkgs,
char *dep_entry)
{
- unsigned int i;
slapt_regex_t *parse_dep_regex = NULL;
char *tmp_pkg_name = NULL, *tmp_pkg_ver = NULL;
char tmp_pkg_cond[3];
@@ -1547,35 +1529,33 @@ static slapt_pkg_info_t *parse_meta_entry(slapt_pkg_list_t *avail_pkgs,
}
}
- for (i = 0; i < installed_pkgs->pkg_count; i++) {
- if (strcmp(tmp_pkg_name, installed_pkgs->pkgs[i]->name) != 0)
+ slapt_pkg_list_t_foreach(installed_pkg, installed_pkgs) {
+ if (strcmp(tmp_pkg_name, installed_pkg->name) != 0)
continue;
/* if condition is "=",">=", or "=<" and versions are the same */
if ((strchr(tmp_pkg_cond, '=') != NULL) &&
- (slapt_cmp_pkg_versions(tmp_pkg_ver, installed_pkgs->pkgs[i]->version) == 0)) {
+ (slapt_cmp_pkg_versions(tmp_pkg_ver, installed_pkg->version) == 0)) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return installed_pkgs->pkgs[i];
+ return installed_pkg;
}
/* if "<" */
if (strchr(tmp_pkg_cond, '<') != NULL) {
- if (slapt_cmp_pkg_versions(installed_pkgs->pkgs[i]->version,
- tmp_pkg_ver) < 0) {
+ if (slapt_cmp_pkg_versions(installed_pkg->version, tmp_pkg_ver) < 0) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return installed_pkgs->pkgs[i];
+ return installed_pkg;
}
}
/* if ">" */
if (strchr(tmp_pkg_cond, '>') != NULL) {
- if (slapt_cmp_pkg_versions(installed_pkgs->pkgs[i]->version,
- tmp_pkg_ver) > 0) {
+ if (slapt_cmp_pkg_versions(installed_pkg->version, tmp_pkg_ver) > 0) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return installed_pkgs->pkgs[i];
+ return installed_pkg;
}
}
}
@@ -1613,33 +1593,33 @@ static slapt_pkg_info_t *parse_meta_entry(slapt_pkg_list_t *avail_pkgs,
}
/* loop through avail_pkgs */
- for (i = 0; i < avail_pkgs->pkg_count; i++) {
- if (strcmp(tmp_pkg_name, avail_pkgs->pkgs[i]->name) != 0)
+ slapt_pkg_list_t_foreach(avail_pkg, avail_pkgs) {
+ if (strcmp(tmp_pkg_name, avail_pkg->name) != 0)
continue;
/* if condition is "=",">=", or "=<" and versions are the same */
if ((strchr(tmp_pkg_cond, '=') != NULL) &&
- (slapt_cmp_pkg_versions(tmp_pkg_ver, avail_pkgs->pkgs[i]->version) == 0)) {
+ (slapt_cmp_pkg_versions(tmp_pkg_ver, avail_pkg->version) == 0)) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return avail_pkgs->pkgs[i];
+ return avail_pkg;
}
/* if "<" */
if (strchr(tmp_pkg_cond, '<') != NULL) {
- if (slapt_cmp_pkg_versions(avail_pkgs->pkgs[i]->version, tmp_pkg_ver) < 0) {
+ if (slapt_cmp_pkg_versions(avail_pkg->version, tmp_pkg_ver) < 0) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return avail_pkgs->pkgs[i];
+ return avail_pkg;
}
}
/* if ">" */
if (strchr(tmp_pkg_cond, '>') != NULL) {
- if (slapt_cmp_pkg_versions(avail_pkgs->pkgs[i]->version, tmp_pkg_ver) > 0) {
+ if (slapt_cmp_pkg_versions(avail_pkg->version, tmp_pkg_ver) > 0) {
free(tmp_pkg_name);
free(tmp_pkg_ver);
- return avail_pkgs->pkgs[i];
+ return avail_pkg;
}
}
}
@@ -1706,7 +1686,6 @@ static void required_by(slapt_pkg_list_t *avail,
slapt_pkg_info_t *pkg,
slapt_pkg_list_t *required_by_list)
{
- unsigned int i;
slapt_regex_t *required_by_reg = NULL;
char *pkg_name = escape_package_name(pkg);
int reg_str_len = strlen(pkg_name) + 31;
@@ -1727,50 +1706,47 @@ static void required_by(slapt_pkg_list_t *avail,
free(pkg_name);
free(reg);
- for (i = 0; i < avail->pkg_count; i++) {
- slapt_pkg_info_t *pkg_ptr = avail->pkgs[i];
+ slapt_pkg_list_t_foreach(avail_pkg, avail) {
slapt_list_t *dep_list = NULL;
- unsigned int d = 0;
- if (strcmp(pkg_ptr->required, "") == 0)
+ if (strcmp(avail_pkg->required, "") == 0)
continue;
- if (strcmp(pkg_ptr->required, " ") == 0)
+ if (strcmp(avail_pkg->required, " ") == 0)
continue;
- if (strcmp(pkg_ptr->required, " ") == 0)
+ if (strcmp(avail_pkg->required, " ") == 0)
continue;
- if (strstr(pkg_ptr->required, pkg->name) == NULL)
+ if (strstr(avail_pkg->required, pkg->name) == NULL)
continue;
- slapt_execute_regex(required_by_reg, pkg_ptr->required);
+ slapt_execute_regex(required_by_reg, avail_pkg->required);
if (required_by_reg->reg_return != 0)
continue;
/* check for the offending dependency entry and see if we have an alternative */
- dep_list = slapt_parse_delimited_list(pkg_ptr->required, ',');
- for (d = 0; d < dep_list->count; d++) {
+ dep_list = slapt_parse_delimited_list(avail_pkg->required, ',');
+ slapt_list_t_foreach(part, dep_list) {
slapt_list_t *satisfies = NULL;
- unsigned int s = 0;
bool has_alternative = false, found = false;
/* found our package in the list of dependencies */
- if (strstr(dep_list->items[d], pkg->name) == NULL)
+ if (strstr(part, pkg->name) == NULL)
continue;
/* not an |or, just add it */
- if (strchr(dep_list->items[d], '|') == NULL) {
- if (strcmp(dep_list->items[d], pkg->name) != 0)
+ if (strchr(part, '|') == NULL) {
+ if (strcmp(part, pkg->name) != 0)
continue;
- if (slapt_get_exact_pkg(required_by_list, pkg_ptr->name, pkg_ptr->version) == NULL) {
- slapt_add_pkg_to_pkg_list(required_by_list, pkg_ptr);
- required_by(avail, installed_pkgs, pkgs_to_install, pkgs_to_remove, pkg_ptr, required_by_list);
+ if (slapt_get_exact_pkg(required_by_list, avail_pkg->name, avail_pkg->version) == NULL) {
+ slapt_add_pkg_to_pkg_list(required_by_list, avail_pkg);
+ required_by(avail, installed_pkgs, pkgs_to_install, pkgs_to_remove, avail_pkg, required_by_list);
}
break;
}
/* we need to find out if we have something else that satisfies the dependency */
- satisfies = slapt_parse_delimited_list(dep_list->items[d], '|');
- for (s = 0; s < satisfies->count; s++) {
- slapt_pkg_info_t *tmp_pkg = parse_meta_entry(avail, installed_pkgs, satisfies->items[s]);
+ satisfies = slapt_parse_delimited_list(part, '|');
+ slapt_list_t_foreach(satisfies_part, satisfies) {
+ slapt_pkg_info_t *tmp_pkg = parse_meta_entry(avail, installed_pkgs, satisfies_part);
if (tmp_pkg == NULL)
continue;
@@ -1793,9 +1769,9 @@ static void required_by(slapt_pkg_list_t *avail,
/* we couldn't find an installed pkg that satisfies the |or */
if (has_alternative == false && found == true) {
- if (slapt_get_exact_pkg(required_by_list, pkg_ptr->name, pkg_ptr->version) == NULL) {
- slapt_add_pkg_to_pkg_list(required_by_list, pkg_ptr);
- required_by(avail, installed_pkgs, pkgs_to_install, pkgs_to_remove, pkg_ptr, required_by_list);
+ if (slapt_get_exact_pkg(required_by_list, avail_pkg->name, avail_pkg->version) == NULL) {
+ slapt_add_pkg_to_pkg_list(required_by_list, avail_pkg);
+ required_by(avail, installed_pkgs, pkgs_to_install, pkgs_to_remove, avail_pkg, required_by_list);
}
}
}
@@ -1849,18 +1825,16 @@ slapt_pkg_info_t *slapt_get_pkg_by_details(slapt_pkg_list_t *list,
}
} else {
- unsigned int i;
-
- for (i = 0; i < list->pkg_count; i++) {
- if (strcmp(list->pkgs[i]->name, name) == 0) {
+ slapt_pkg_list_t_foreach(pkg, list) {
+ if (strcmp(pkg->name, name) == 0) {
if (version != NULL) {
- if (strcmp(list->pkgs[i]->version, version) == 0) {
+ if (strcmp(pkg->version, version) == 0) {
if (location != NULL) {
- if (strcmp(list->pkgs[i]->location, location) == 0) {
- return list->pkgs[i];
+ if (strcmp(pkg->location, location) == 0) {
+ return pkg;
}
} else {
- return list->pkgs[i];
+ return pkg;
}
}
}
@@ -1874,13 +1848,13 @@ slapt_pkg_info_t *slapt_get_pkg_by_details(slapt_pkg_list_t *list,
/* update package data from mirror url */
int slapt_update_pkg_cache(const slapt_rc_config *global_config)
{
- unsigned int i, source_dl_failed = 0;
+ uint32_t source_dl_failed = 0;
slapt_pkg_list_t *new_pkgs = slapt_init_pkg_list();
new_pkgs->free_pkgs = true;
/* go through each package source and download the meta data */
- for (i = 0; i < global_config->sources->count; i++) {
- unsigned int compressed = 0;
+ slapt_source_list_t_foreach(source, global_config->sources) {
+ bool compressed = false;
slapt_pkg_list_t *available_pkgs = NULL;
slapt_pkg_list_t *patch_pkgs = NULL;
FILE *tmp_checksum_f = NULL;
@@ -1888,10 +1862,10 @@ int slapt_update_pkg_cache(const slapt_rc_config *global_config)
FILE *tmp_signature_f = NULL;
FILE *tmp_checksum_to_verify_f = NULL;
#endif
- const char *source_url = global_config->sources->src[i]->url;
- SLAPT_PRIORITY_T source_priority = global_config->sources->src[i]->priority;
+ const char *source_url = source->url;
+ SLAPT_PRIORITY_T source_priority = source->priority;
- if (global_config->sources->src[i]->disabled == true)
+ if (source->disabled == true)
continue;
/* download our SLAPT_PKG_LIST */
@@ -1961,15 +1935,12 @@ int slapt_update_pkg_cache(const slapt_rc_config *global_config)
}
if (tmp_checksum_f != NULL) {
- unsigned int pkg_i;
-
/* now map md5 checksums to packages */
printf(gettext("Reading Package Lists..."));
slapt_get_md5sums(available_pkgs, tmp_checksum_f);
- for (pkg_i = 0; pkg_i < available_pkgs->pkg_count; ++pkg_i) {
- slapt_pkg_info_t *p = available_pkgs->pkgs[pkg_i];
+ slapt_pkg_list_t_foreach(p, available_pkgs) {
int mirror_len = -1;
/* honor the mirror if it was set in the PACKAGES.TXT */
@@ -1989,25 +1960,24 @@ int slapt_update_pkg_cache(const slapt_rc_config *global_config)
if (patch_pkgs) {
slapt_get_md5sums(patch_pkgs, tmp_checksum_f);
- for (pkg_i = 0; pkg_i < patch_pkgs->pkg_count; ++pkg_i) {
- slapt_pkg_info_t *p = patch_pkgs->pkgs[pkg_i];
+ slapt_pkg_list_t_foreach(patch_pkg, patch_pkgs) {
int mirror_len = -1;
/* honor the mirror if it was set in the PACKAGES.TXT */
- if (p->mirror == NULL || (mirror_len = strlen(p->mirror)) == 0) {
+ if (patch_pkg->mirror == NULL || (mirror_len = strlen(patch_pkg->mirror)) == 0) {
if (mirror_len == 0)
- free(p->mirror);
+ free(patch_pkg->mirror);
- p->mirror = strdup(source_url);
+ patch_pkg->mirror = strdup(source_url);
}
/* set the priority of the package based on the source, plus 1 for the patch priority */
if (global_config->use_priority == true)
- p->priority = source_priority + 1;
+ patch_pkg->priority = source_priority + 1;
else
- p->priority = source_priority;
+ patch_pkg->priority = source_priority;
- slapt_add_pkg_to_pkg_list(new_pkgs, p);
+ slapt_add_pkg_to_pkg_list(new_pkgs, patch_pkg);
}
patch_pkgs->free_pkgs = false;
}
@@ -2426,11 +2396,10 @@ static slapt_pkg_info_t *find_or_requirement(slapt_pkg_list_t *avail_pkgs,
char *required_str)
{
slapt_pkg_info_t *pkg = NULL;
- unsigned int i = 0;
slapt_list_t *alternates = slapt_parse_delimited_list(required_str, '|');
- for (i = 0; i < alternates->count; i++) {
- slapt_pkg_info_t *tmp_pkg = parse_meta_entry(avail_pkgs, installed_pkgs, alternates->items[i]);
+ slapt_list_t_foreach(alternate, alternates) {
+ slapt_pkg_info_t *tmp_pkg = parse_meta_entry(avail_pkgs, installed_pkgs, alternate);
if (tmp_pkg != NULL) {
/* installed packages are preferred */
@@ -2498,7 +2467,7 @@ void slapt_add_pkg_err_to_list(slapt_pkg_err_list_t *l,
{
slapt_pkg_err_t **tmp;
- if (slapt_search_pkg_err_list(l, pkg, err) == 1)
+ if (slapt_search_pkg_err_list(l, pkg, err))
return;
tmp = realloc(l->errs, sizeof *l->errs * (l->err_count + 1));
@@ -2514,27 +2483,24 @@ void slapt_add_pkg_err_to_list(slapt_pkg_err_list_t *l,
++l->err_count;
}
-int slapt_search_pkg_err_list(slapt_pkg_err_list_t *l,
+bool slapt_search_pkg_err_list(slapt_pkg_err_list_t *l,
const char *pkg, const char *err)
{
- unsigned int i, found = 1, not_found = 0;
- for (i = 0; i < l->err_count; ++i) {
- if (strcmp(l->errs[i]->pkg, pkg) == 0 &&
- strcmp(l->errs[i]->error, err) == 0) {
- return found;
+ slapt_pkg_err_list_t_foreach(error, l) {
+ if (strcmp(error->pkg, pkg) == 0 &&
+ strcmp(error->error, err) == 0) {
+ return true;
}
}
- return not_found;
+ return false;
}
void slapt_free_pkg_err_list(slapt_pkg_err_list_t *l)
{
- unsigned int i;
-
- for (i = 0; i < l->err_count; ++i) {
- free(l->errs[i]->pkg);
- free(l->errs[i]->error);
- free(l->errs[i]);
+ slapt_pkg_err_list_t_foreach(error, l) {
+ free(error->pkg);
+ free(error->error);
+ free(error);
}
free(l->errs);
free(l);
@@ -3265,13 +3231,11 @@ slapt_get_obsolete_pkgs(const slapt_rc_config *global_config,
slapt_pkg_list_t *avail_pkgs,
slapt_pkg_list_t *installed_pkgs)
{
- unsigned int r;
slapt_pkg_list_t *obsolete = slapt_init_pkg_list();
slapt_pkg_list_t *to_install = slapt_init_pkg_list();
slapt_pkg_list_t *to_remove = slapt_init_pkg_list();
- for (r = 0; r < installed_pkgs->pkg_count; ++r) {
- slapt_pkg_info_t *p = installed_pkgs->pkgs[r];
+ slapt_pkg_list_t_foreach(p, installed_pkgs) {
/*
* if we can't find the installed package in our available pkg list,
@@ -3279,16 +3243,14 @@ slapt_get_obsolete_pkgs(const slapt_rc_config *global_config,
*/
if (slapt_get_newest_pkg(avail_pkgs, p->name) == NULL) {
slapt_pkg_list_t *deps;
- unsigned int c;
/*
- any packages that require this package we are about to remove
- should be scheduled to remove as well
- */
+ any packages that require this package we are about to remove
+ should be scheduled to remove as well
+ */
deps = slapt_is_required_by(global_config, avail_pkgs, installed_pkgs, to_install, to_remove, p);
- for (c = 0; c < deps->pkg_count; ++c) {
- slapt_pkg_info_t *dep = deps->pkgs[c];
+ slapt_pkg_list_t_foreach(dep, deps) {
slapt_pkg_info_t *installed_dep = slapt_get_exact_pkg(installed_pkgs, dep->name, dep->version);
/* if it is installed, we add it to the list */
diff --git a/src/package.h b/src/package.h
index 03f341c..63852d8 100644
--- a/src/package.h
+++ b/src/package.h
@@ -75,6 +75,7 @@ typedef struct {
bool free_pkgs;
bool ordered;
} slapt_pkg_list_t;
+#define slapt_pkg_list_t_foreach(item, list) slapt_pkg_info_t *item; for (uint32_t item##_counter = 0; item = list->pkgs[item##_counter], item##_counter < list->pkg_count; item##_counter++)
typedef struct {
slapt_pkg_info_t *installed;
@@ -87,6 +88,7 @@ typedef struct {
uint32_t pkg_count;
uint32_t reinstall_count;
} slapt_pkg_upgrade_list_t;
+#define slapt_pkg_upgrade_list_t_foreach(item, list) slapt_pkg_upgrade_t *item; for (uint32_t item##_counter = 0; item = list->pkgs[item##_counter], item##_counter < list->pkg_count; item##_counter++)
typedef struct {
char *pkg;
@@ -97,6 +99,7 @@ typedef struct {
slapt_pkg_err_t **errs;
uint32_t err_count;
} slapt_pkg_err_list_t;
+#define slapt_pkg_err_list_t_foreach(item, list) slapt_pkg_err_t *item; for (uint32_t item##_counter = 0; item = list->errs[item##_counter], item##_counter < list->err_count; item##_counter++)
/* returns an empty package structure */
slapt_pkg_info_t *slapt_init_pkg(void);
diff --git a/src/transaction.c b/src/transaction.c
index 38be3ef..f4daec4 100644
--- a/src/transaction.c
+++ b/src/transaction.c
@@ -63,7 +63,7 @@ slapt_transaction_t *slapt_init_transaction(void)
int slapt_handle_transaction(const slapt_rc_config *global_config,
slapt_transaction_t *tran)
{
- unsigned int i, pkg_dl_count = 0, dl_counter = 0, len = 0;
+ uint32_t pkg_dl_count = 0, dl_counter = 0, len = 0;
double download_size = 0;
double already_download_size = 0;
double uncompressed_size = 0;
@@ -72,17 +72,16 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
/* show unmet dependencies */
if (tran->missing_err->err_count > 0) {
fprintf(stderr, gettext("The following packages have unmet dependencies:\n"));
- for (i = 0; i < tran->missing_err->err_count; ++i) {
- fprintf(stderr, gettext(" %s: Depends: %s\n"),
- tran->missing_err->errs[i]->pkg, tran->missing_err->errs[i]->error);
+ slapt_pkg_err_list_t_foreach(error, tran->missing_err) {
+ fprintf(stderr, gettext(" %s: Depends: %s\n"), error->pkg, error->error);
}
}
/* show conflicts */
if (tran->conflict_err->err_count > 0) {
- for (i = 0; i < tran->conflict_err->err_count; ++i) {
+ slapt_pkg_err_list_t_foreach(conflict_error, tran->conflict_err) {
fprintf(stderr, gettext("%s, which is required by %s, is excluded\n"),
- tran->conflict_err->errs[i]->error, tran->conflict_err->errs[i]->pkg);
+ conflict_error->error, conflict_error->pkg);
}
}
@@ -92,9 +91,7 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
printf(" ");
len = 0;
- for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
- const slapt_pkg_info_t *e = tran->exclude_pkgs->pkgs[i];
-
+ slapt_pkg_list_t_foreach(e, tran->exclude_pkgs) {
if (len + strlen(e->name) + 1 < MAX_LINE_LEN) {
printf("%s ", e->name);
len += strlen(e->name) + 1;
@@ -115,11 +112,8 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
printf(" ");
len = 0;
- for (i = 0; i < tran->suggests->count; ++i) {
- char *s = tran->suggests->items[i];
-
- /* don't show suggestion for something we already have
- in the transaction */
+ slapt_list_t_foreach(s, tran->suggests) {
+ /* don't show suggestion for something we already have in the transaction */
if (slapt_search_transaction(tran, s) == 1)
continue;
@@ -140,8 +134,7 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
printf(" ");
len = 0;
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];
+ slapt_pkg_list_t_foreach(p, tran->install_pkgs) {
size_t existing_file_size = 0;
if (len + strlen(p->name) + 1 < MAX_LINE_LEN) {
@@ -170,9 +163,7 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
printf(" ");
len = 0;
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- const slapt_pkg_info_t *r = tran->remove_pkgs->pkgs[i];
-
+ slapt_pkg_list_t_foreach(r, tran->remove_pkgs) {
if (len + strlen(r->name) + 1 < MAX_LINE_LEN) {
printf("%s ", r->name);
len += strlen(r->name) + 1;
@@ -196,9 +187,9 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
}
len = 0;
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
- slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->installed;
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ slapt_pkg_info_t *u = upgrade->upgrade;
+ slapt_pkg_info_t *p = upgrade->installed;
size_t existing_file_size = 0;
int line_len = len + strlen(u->name) + 1;
@@ -215,7 +206,7 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
uncompressed_size += u->size_u;
uncompressed_size -= p->size_u;
- if (tran->upgrade_pkgs->pkgs[i]->reinstall == true)
+ if (upgrade->reinstall == true)
continue;
if (line_len < MAX_LINE_LEN) {
@@ -235,11 +226,11 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
printf(" ");
len = 0;
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
+ slapt_pkg_upgrade_list_t_foreach(reinstall_upgrade, tran->upgrade_pkgs) {
+ slapt_pkg_info_t *u = reinstall_upgrade->upgrade;
int line_len = len + strlen(u->name) + 1;
- if (tran->upgrade_pkgs->pkgs[i]->reinstall == false)
+ if (reinstall_upgrade->reinstall == false)
continue;
if (line_len < MAX_LINE_LEN) {
@@ -344,40 +335,38 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
}
if (global_config->print_uris == true) {
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- const slapt_pkg_info_t *info = tran->install_pkgs->pkgs[i];
+ slapt_pkg_list_t_foreach(info, tran->install_pkgs) {
const char *location = info->location + strspn(info->location, "./");
printf("%s%s/%s-%s%s\n",
info->mirror, location, info->name,
info->version, info->file_ext);
}
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- const slapt_pkg_info_t *info = tran->upgrade_pkgs->pkgs[i]->upgrade;
- const char *location = info->location + strspn(info->location, "./");
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ const slapt_pkg_info_t *upgrade_info = upgrade->upgrade;
+ const char *location = upgrade_info->location + strspn(upgrade_info->location, "./");
printf("%s%s/%s-%s%s\n",
- info->mirror, location, info->name,
- info->version, info->file_ext);
+ upgrade_info->mirror, location, upgrade_info->name,
+ upgrade_info->version, upgrade_info->file_ext);
}
return 0;
}
/* if simulate is requested, just show what could happen and return */
if (global_config->simulate == true) {
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- const slapt_pkg_info_t *r = tran->remove_pkgs->pkgs[i];
+ slapt_pkg_list_t_foreach(r, tran->remove_pkgs) {
printf(gettext("%s-%s is to be removed\n"), r->name, r->version);
}
- for (i = 0; i < tran->queue->count; ++i) {
- if (tran->queue->pkgs[i]->type == INSTALL) {
+ slapt_queue_t_foreach(q, tran->queue) {
+ if (q->type == INSTALL) {
printf(gettext("%s-%s is to be installed\n"),
- tran->queue->pkgs[i]->pkg.i->name,
- tran->queue->pkgs[i]->pkg.i->version);
- } else if (tran->queue->pkgs[i]->type == UPGRADE) {
+ q->pkg.i->name,
+ q->pkg.i->version);
+ } else if (q->type == UPGRADE) {
printf(gettext("%s-%s is to be upgraded to version %s\n"),
- tran->queue->pkgs[i]->pkg.u->upgrade->name,
- tran->queue->pkgs[i]->pkg.u->installed->version,
- tran->queue->pkgs[i]->pkg.u->upgrade->version);
+ q->pkg.u->upgrade->name,
+ q->pkg.u->installed->version,
+ q->pkg.u->upgrade->version);
}
}
@@ -388,14 +377,14 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
pkg_dl_count = tran->install_pkgs->pkg_count + tran->upgrade_pkgs->pkg_count;
/* download pkgs */
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- unsigned int retry_count, failed = 1;
+ slapt_pkg_list_t_foreach(pkg, tran->install_pkgs) {
+ uint32_t failed = 1;
++dl_counter;
snprintf(dl_note, SLAPT_PKG_DL_NOTE_LEN, "%d/%d", dl_counter, pkg_dl_count);
- for (retry_count = 0; retry_count < global_config->retry; ++retry_count) {
- const char *err = slapt_download_pkg(global_config, tran->install_pkgs->pkgs[i], dl_note);
+ for (uint32_t retry_count = 0; retry_count < global_config->retry; ++retry_count) {
+ const char *err = slapt_download_pkg(global_config, pkg, dl_note);
if (err) {
fprintf(stderr, gettext("Failed to download: %s\n"), err);
failed = 1;
@@ -409,14 +398,14 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
exit(EXIT_FAILURE);
}
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- unsigned int retry_count, failed = 1;
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ uint32_t failed = 1;
++dl_counter;
snprintf(dl_note, SLAPT_PKG_DL_NOTE_LEN, "%d/%d", dl_counter, pkg_dl_count);
- for (retry_count = 0; retry_count < global_config->retry; ++retry_count) {
- const char *err = slapt_download_pkg(global_config, tran->upgrade_pkgs->pkgs[i]->upgrade, dl_note);
+ for (uint32_t retry_count = 0; retry_count < global_config->retry; ++retry_count) {
+ const char *err = slapt_download_pkg(global_config, upgrade->upgrade, dl_note);
if (err) {
fprintf(stderr, gettext("Failed to download: %s\n"), err);
failed = 1;
@@ -433,29 +422,29 @@ int slapt_handle_transaction(const slapt_rc_config *global_config,
/* run transaction, remove, install, and upgrade */
if (global_config->download_only == false) {
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- if (slapt_remove_pkg(global_config, tran->remove_pkgs->pkgs[i]) == -1) {
+ slapt_pkg_list_t_foreach(remove_pkg, tran->remove_pkgs) {
+ if (slapt_remove_pkg(global_config, remove_pkg) == -1) {
exit(EXIT_FAILURE);
}
}
- for (i = 0; i < tran->queue->count; ++i) {
- if (tran->queue->pkgs[i]->type == INSTALL) {
+ slapt_queue_t_foreach(q, tran->queue) {
+ if (q->type == INSTALL) {
printf(gettext("Preparing to install %s-%s\n"),
- tran->queue->pkgs[i]->pkg.i->name,
- tran->queue->pkgs[i]->pkg.i->version);
+ q->pkg.i->name,
+ q->pkg.i->version);
if (slapt_install_pkg(global_config,
- tran->queue->pkgs[i]->pkg.i) == -1) {
+ q->pkg.i) == -1) {
exit(EXIT_FAILURE);
}
- } else if (tran->queue->pkgs[i]->type == UPGRADE) {
+ } else if (q->type == UPGRADE) {
printf(gettext("Preparing to replace %s-%s with %s-%s\n"),
- tran->queue->pkgs[i]->pkg.u->upgrade->name,
- tran->queue->pkgs[i]->pkg.u->installed->version,
- tran->queue->pkgs[i]->pkg.u->upgrade->name,
- tran->queue->pkgs[i]->pkg.u->upgrade->version);
+ q->pkg.u->upgrade->name,
+ q->pkg.u->installed->version,
+ q->pkg.u->upgrade->name,
+ q->pkg.u->upgrade->version);
if (slapt_upgrade_pkg(global_config,
- tran->queue->pkgs[i]->pkg.u->upgrade) == -1) {
+ q->pkg.u->upgrade) == -1) {
exit(EXIT_FAILURE);
}
}
@@ -609,25 +598,25 @@ static void _slapt_add_upgrade_to_transaction(slapt_transaction_t *tran,
int slapt_search_transaction(slapt_transaction_t *tran, char *pkg_name)
{
- unsigned int i, found = 1, not_found = 0;
+ uint32_t found = 1, not_found = 0;
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- if (strcmp(pkg_name, tran->install_pkgs->pkgs[i]->name) == 0)
+ slapt_pkg_list_t_foreach(pkg, tran->install_pkgs) {
+ if (strcmp(pkg_name, pkg->name) == 0)
return found;
}
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- if (strcmp(pkg_name, tran->upgrade_pkgs->pkgs[i]->upgrade->name) == 0)
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ if (strcmp(pkg_name, upgrade->upgrade->name) == 0)
return found;
}
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- if (strcmp(pkg_name, tran->remove_pkgs->pkgs[i]->name) == 0)
+ slapt_pkg_list_t_foreach(remove_pkg, tran->remove_pkgs) {
+ if (strcmp(pkg_name, remove_pkg->name) == 0)
return found;
}
- for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
- if (strcmp(pkg_name, tran->exclude_pkgs->pkgs[i]->name) == 0)
+ slapt_pkg_list_t_foreach(exclude_pkg, tran->exclude_pkgs) {
+ if (strcmp(pkg_name, exclude_pkg->name) == 0)
return found;
}
@@ -637,12 +626,10 @@ int slapt_search_transaction(slapt_transaction_t *tran, char *pkg_name)
int slapt_search_upgrade_transaction(slapt_transaction_t *tran,
slapt_pkg_info_t *pkg)
{
- unsigned int i,
- found = 1,
- not_found = 0;
+ uint32_t found = 1, not_found = 0;
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- if (strcmp(pkg->name, tran->upgrade_pkgs->pkgs[i]->upgrade->name) == 0)
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ if (strcmp(pkg->name, upgrade->upgrade->name) == 0)
return found;
}
@@ -651,35 +638,33 @@ int slapt_search_upgrade_transaction(slapt_transaction_t *tran,
void slapt_free_transaction(slapt_transaction_t *tran)
{
- unsigned int i;
-
if (tran->install_pkgs->free_pkgs == true) {
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- slapt_free_pkg(tran->install_pkgs->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, tran->install_pkgs) {
+ slapt_free_pkg(pkg);
}
}
free(tran->install_pkgs->pkgs);
free(tran->install_pkgs);
if (tran->remove_pkgs->free_pkgs == true) {
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- slapt_free_pkg(tran->remove_pkgs->pkgs[i]);
+ slapt_pkg_list_t_foreach(remove_pkg, tran->remove_pkgs) {
+ slapt_free_pkg(remove_pkg);
}
}
free(tran->remove_pkgs->pkgs);
free(tran->remove_pkgs);
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- slapt_free_pkg(tran->upgrade_pkgs->pkgs[i]->upgrade);
- slapt_free_pkg(tran->upgrade_pkgs->pkgs[i]->installed);
- free(tran->upgrade_pkgs->pkgs[i]);
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ slapt_free_pkg(upgrade->upgrade);
+ slapt_free_pkg(upgrade->installed);
+ free(upgrade);
}
free(tran->upgrade_pkgs->pkgs);
free(tran->upgrade_pkgs);
if (tran->exclude_pkgs->free_pkgs == true) {
- for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
- slapt_free_pkg(tran->exclude_pkgs->pkgs[i]);
+ slapt_pkg_list_t_foreach(exclude_pkg, tran->exclude_pkgs) {
+ slapt_free_pkg(exclude_pkg);
}
}
free(tran->exclude_pkgs->pkgs);
@@ -698,7 +683,6 @@ void slapt_free_transaction(slapt_transaction_t *tran)
slapt_transaction_t *slapt_remove_from_transaction(slapt_transaction_t *tran,
slapt_pkg_info_t *pkg)
{
- unsigned int i;
slapt_transaction_t *new_tran = NULL;
if (slapt_search_transaction_by_pkg(tran, pkg) == 0)
@@ -712,35 +696,31 @@ slapt_transaction_t *slapt_remove_from_transaction(slapt_transaction_t *tran,
new_tran->exclude_pkgs = slapt_malloc(sizeof *new_tran->exclude_pkgs);
new_tran = slapt_init_transaction();
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];
-
+ slapt_pkg_list_t_foreach(installed_pkg, tran->install_pkgs) {
if (
- strcmp(pkg->name, p->name) == 0 &&
- strcmp(pkg->version, p->version) == 0 &&
- strcmp(pkg->location, p->location) == 0) {
+ strcmp(pkg->name, installed_pkg->name) == 0 &&
+ strcmp(pkg->version, installed_pkg->version) == 0 &&
+ strcmp(pkg->location, installed_pkg->location) == 0) {
continue;
}
- slapt_add_install_to_transaction(new_tran, p);
+ slapt_add_install_to_transaction(new_tran, installed_pkg);
}
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->remove_pkgs->pkgs[i];
-
+ slapt_pkg_list_t_foreach(remove_pkg, tran->remove_pkgs) {
if (
- strcmp(pkg->name, p->name) == 0 &&
- strcmp(pkg->version, p->version) == 0 &&
- strcmp(pkg->location, p->location) == 0) {
+ strcmp(pkg->name, remove_pkg->name) == 0 &&
+ strcmp(pkg->version, remove_pkg->version) == 0 &&
+ strcmp(pkg->location, remove_pkg->location) == 0) {
continue;
}
- slapt_add_remove_to_transaction(new_tran, tran->remove_pkgs->pkgs[i]);
+ slapt_add_remove_to_transaction(new_tran, remove_pkg);
}
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *u = tran->upgrade_pkgs->pkgs[i]->upgrade;
- slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->installed;
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ slapt_pkg_info_t *u = upgrade->upgrade;
+ slapt_pkg_info_t *p = upgrade->installed;
if (
strcmp(pkg->name, u->name) == 0 &&
@@ -752,17 +732,15 @@ slapt_transaction_t *slapt_remove_from_transaction(slapt_transaction_t *tran,
slapt_add_upgrade_to_transaction(new_tran, p, u);
}
- for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->exclude_pkgs->pkgs[i];
-
+ slapt_pkg_list_t_foreach(exclude_pkg, tran->exclude_pkgs) {
if (
- strcmp(pkg->name, p->name) == 0 &&
- strcmp(pkg->version, p->version) == 0 &&
- strcmp(pkg->location, p->location) == 0) {
+ strcmp(pkg->name, exclude_pkg->name) == 0 &&
+ strcmp(pkg->version, exclude_pkg->version) == 0 &&
+ strcmp(pkg->location, exclude_pkg->location) == 0) {
continue;
}
- slapt_add_exclude_to_transaction(new_tran, p);
+ slapt_add_exclude_to_transaction(new_tran, exclude_pkg);
}
return new_tran;
@@ -776,7 +754,6 @@ int slapt_add_deps_to_trans(const slapt_rc_config *global_config,
slapt_pkg_list_t *installed_pkgs,
slapt_pkg_info_t *pkg)
{
- unsigned int c;
int dep_return = -1;
slapt_pkg_list_t *deps = NULL;
@@ -801,22 +778,19 @@ int slapt_add_deps_to_trans(const slapt_rc_config *global_config,
}
/* loop through the deps */
- for (c = 0; c < deps->pkg_count; ++c) {
- unsigned int cindex = 0;
- slapt_pkg_info_t *dep = deps->pkgs[c];
+ slapt_pkg_list_t_foreach(dep, deps) {
slapt_pkg_info_t *dep_installed = NULL;
slapt_pkg_list_t *conflicts = NULL;
/*
- * the dep wouldn't get this far if it where excluded,
- * so we don't check for that here
- */
+ * the dep wouldn't get this far if it where excluded,
+ * so we don't check for that here
+ */
- conflicts =
- slapt_is_conflicted(tran, avail_pkgs, installed_pkgs, dep);
+ conflicts = slapt_is_conflicted(tran, avail_pkgs, installed_pkgs, dep);
- for (cindex = 0; cindex < conflicts->pkg_count; cindex++) {
- slapt_add_remove_to_transaction(tran, conflicts->pkgs[cindex]);
+ slapt_pkg_list_t_foreach(conflict, conflicts) {
+ slapt_add_remove_to_transaction(tran, conflict);
}
slapt_free_pkg_list(conflicts);
@@ -842,7 +816,6 @@ slapt_pkg_list_t *slapt_is_conflicted(slapt_transaction_t *tran,
slapt_pkg_list_t *installed_pkgs,
slapt_pkg_info_t *pkg)
{
- unsigned int i;
slapt_pkg_list_t *conflicts = NULL;
slapt_pkg_list_t *conflicts_in_transaction = slapt_init_pkg_list();
@@ -850,18 +823,16 @@ slapt_pkg_list_t *slapt_is_conflicted(slapt_transaction_t *tran,
or in the current transaction
*/
conflicts = slapt_get_pkg_conflicts(avail_pkgs, installed_pkgs, pkg);
- for (i = 0; i < conflicts->pkg_count; ++i) {
- slapt_pkg_info_t *p = conflicts->pkgs[i];
-
- if (slapt_search_upgrade_transaction(tran, p) == 1 || slapt_get_newest_pkg(tran->install_pkgs, p->name) != NULL) {
- printf(gettext("%s, which is to be installed, conflicts with %s\n"), p->name, pkg->name);
+ slapt_pkg_list_t_foreach(conflict, conflicts) {
+ if (slapt_search_upgrade_transaction(tran, conflict) == 1 || slapt_get_newest_pkg(tran->install_pkgs, conflict->name) != NULL) {
+ printf(gettext("%s, which is to be installed, conflicts with %s\n"), conflict->name, pkg->name);
- slapt_add_pkg_to_pkg_list(conflicts_in_transaction, p);
+ slapt_add_pkg_to_pkg_list(conflicts_in_transaction, conflict);
}
- if (slapt_get_newest_pkg(installed_pkgs, p->name) != NULL) {
- printf(gettext("Installed %s conflicts with %s\n"), p->name, pkg->name);
+ if (slapt_get_newest_pkg(installed_pkgs, conflict->name) != NULL) {
+ printf(gettext("Installed %s conflicts with %s\n"), conflict->name, pkg->name);
- slapt_add_pkg_to_pkg_list(conflicts_in_transaction, p);
+ slapt_add_pkg_to_pkg_list(conflicts_in_transaction, conflict);
}
}
@@ -873,7 +844,6 @@ slapt_pkg_list_t *slapt_is_conflicted(slapt_transaction_t *tran,
static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg)
{
slapt_list_t *suggests = NULL;
- unsigned int i = 0;
if (pkg->suggests == NULL || strlen(pkg->suggests) == 0) {
return;
@@ -881,12 +851,12 @@ static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg)
suggests = slapt_parse_delimited_list(pkg->suggests, ',');
- for (i = 0; i < suggests->count; i++) {
+ slapt_list_t_foreach(s, suggests) {
/* no need to add it if we already have it */
- if (slapt_search_transaction(tran, suggests->items[i]) == 1)
+ if (slapt_search_transaction(tran, s) == 1)
continue;
- slapt_add_list_item(tran->suggests, suggests->items[i]);
+ slapt_add_list_item(tran->suggests, s);
}
slapt_free_list(suggests);
@@ -895,33 +865,27 @@ static void add_suggestion(slapt_transaction_t *tran, slapt_pkg_info_t *pkg)
int slapt_search_transaction_by_pkg(slapt_transaction_t *tran,
slapt_pkg_info_t *pkg)
{
- unsigned int i, found = 1, not_found = 0;
+ uint32_t found = 1, not_found = 0;
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->install_pkgs->pkgs[i];
-
- if ((strcmp(pkg->name, p->name) == 0) && (strcmp(pkg->version, p->version) == 0))
+ slapt_pkg_list_t_foreach(install_pkg, tran->install_pkgs) {
+ if ((strcmp(pkg->name, install_pkg->name) == 0) && (strcmp(pkg->version, install_pkg->version) == 0))
return found;
}
- for (i = 0; i < tran->upgrade_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->upgrade_pkgs->pkgs[i]->upgrade;
+ slapt_pkg_upgrade_list_t_foreach(upgrade, tran->upgrade_pkgs) {
+ slapt_pkg_info_t *p = upgrade->upgrade;
if ((strcmp(pkg->name, p->name) == 0) && (strcmp(pkg->version, p->version) == 0))
return found;
}
- for (i = 0; i < tran->remove_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->remove_pkgs->pkgs[i];
-
- if ((strcmp(pkg->name, p->name) == 0) && (strcmp(pkg->version, p->version) == 0))
+ slapt_pkg_list_t_foreach(remove_pkg, tran->remove_pkgs) {
+ if ((strcmp(pkg->name, remove_pkg->name) == 0) && (strcmp(pkg->version, remove_pkg->version) == 0))
return found;
}
- for (i = 0; i < tran->exclude_pkgs->pkg_count; ++i) {
- slapt_pkg_info_t *p = tran->exclude_pkgs->pkgs[i];
-
- if ((strcmp(pkg->name, p->name) == 0) && (strcmp(pkg->version, p->version) == 0))
+ slapt_pkg_list_t_foreach(exclude_pkg, tran->exclude_pkgs) {
+ if ((strcmp(pkg->name, exclude_pkg->name) == 0) && (strcmp(pkg->version, exclude_pkg->version) == 0))
return found;
}
@@ -970,9 +934,8 @@ static void queue_add_upgrade(slapt_queue_t *t, slapt_pkg_upgrade_t *p)
static void queue_free(slapt_queue_t *t)
{
- unsigned int i;
- for (i = 0; i < t->count; ++i) {
- free(t->pkgs[i]);
+ slapt_queue_t_foreach(i, t) {
+ free(i);
}
free(t->pkgs);
free(t);
@@ -980,8 +943,7 @@ static void queue_free(slapt_queue_t *t)
void slapt_generate_suggestions(slapt_transaction_t *tran)
{
- unsigned int i;
- for (i = 0; i < tran->install_pkgs->pkg_count; ++i) {
- add_suggestion(tran, tran->install_pkgs->pkgs[i]);
+ slapt_pkg_list_t_foreach(pkg, tran->install_pkgs) {
+ add_suggestion(tran, pkg);
}
}
diff --git a/src/transaction.h b/src/transaction.h
index eed7711..0631671 100644
--- a/src/transaction.h
+++ b/src/transaction.h
@@ -34,6 +34,7 @@ typedef struct {
slapt_queue_i **pkgs;
uint32_t count;
} slapt_queue_t;
+#define slapt_queue_t_foreach(item, list) slapt_queue_i *item; for (uint32_t item##_counter = 0; item = list->pkgs[item##_counter], item##_counter < list->count; item##_counter++)
typedef struct {
slapt_pkg_list_t *install_pkgs;