Skip to content

Commit

Permalink
frontend: eliminate the use of utlist
Browse files Browse the repository at this point in the history
  • Loading branch information
bapt committed Dec 14, 2021
1 parent 5b6ba7f commit f9ad73c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 27 deletions.
4 changes: 1 addition & 3 deletions src/Makefile.autosetup
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ SRCS= add.c \
version.c \
which.c

LOCAL_CFLAGS= -I$(top_srcdir)/external/uthash \
-I$(top_srcdir)/compat \
-I$(top_srcdir)/external/libucl/klib \
LOCAL_CFLAGS= -I$(top_srcdir)/compat \
-I$(top_srcdir)/external/libucl/include \
-I$(top_srcdir)/external/include \
-I$(top_builddir)/ \
Expand Down
11 changes: 5 additions & 6 deletions src/audit.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <utlist.h>
#include <ucl.h>

#ifdef HAVE_SYS_CAPSICUM_H
Expand Down Expand Up @@ -129,7 +128,7 @@ print_issue(struct pkg *p, struct pkg_audit_issue *issue)
e = issue->audit;
if (version == NULL) {
printf(" Affected versions:\n");
LL_FOREACH(e->versions, vers) {
ll_foreach(e->versions, vers) {
if (vers->v1.type > 0 && vers->v2.type > 0)
printf(" %s %s : %s %s\n",
vop_names[vers->v1.type], vers->v1.version,
Expand All @@ -143,7 +142,7 @@ print_issue(struct pkg *p, struct pkg_audit_issue *issue)
}
}
printf(" %s\n", e->desc);
LL_FOREACH(e->cve, cve) {
ll_foreach(e->cve, cve) {
printf(" CVE: %s\n", cve->cvename);
}
if (e->url)
Expand All @@ -167,7 +166,7 @@ format_issue(struct pkg *p, struct pkg_audit_issue *issue, ucl_object_t *array)

e = issue->audit;
ucl_object_insert_key(o, affected_versions, "Affected versions", 17, false);
LL_FOREACH(e->versions, vers) {
ll_foreach(e->versions, vers) {
char *ver;
if (vers->v1.type > 0 && vers->v2.type > 0)
xasprintf(&ver, "%s %s : %s %s",
Expand All @@ -185,7 +184,7 @@ format_issue(struct pkg *p, struct pkg_audit_issue *issue, ucl_object_t *array)
ucl_object_insert_key(o, ucl_object_fromstring(e->desc), "description", 11, false);
if (e->cve) {
ucl_object_t *acve = ucl_object_typed_new(UCL_ARRAY);
LL_FOREACH(e->cve, cve) {
ll_foreach(e->cve, cve) {
ucl_array_append(acve, ucl_object_fromstring(cve->cvename));
}
ucl_object_insert_key(o, acve, "cve", 3, false);
Expand Down Expand Up @@ -428,7 +427,7 @@ exec_audit(int argc, char **argv)

if (top != NULL)
array = ucl_object_typed_new(UCL_ARRAY);
LL_FOREACH(issues->issues, issue) {
ll_foreach(issues->issues, issue) {
if (top == NULL)
print_issue(pkg, issue);
else
Expand Down
1 change: 1 addition & 0 deletions src/pkgcli.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <bsd_compat.h>

#define pkg_warnx(fmt, ...) pkg_fprintf(stderr, "%S: " fmt, getprogname(), __VA_ARGS__, -1)
#define ll_foreach(head, el) for (el=head; el != NULL; el = (el)->next)

extern bool quiet;
extern int nbactions;
Expand Down
47 changes: 29 additions & 18 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
#include <errno.h>
#include <pwd.h>
#include <pkg.h>
#include <tllist.h>
#include <xmalloc.h>

#include <bsd_compat.h>

#include "utlist.h"
#include "pkgcli.h"

struct jobs_sum_number {
Expand Down Expand Up @@ -682,25 +683,26 @@ enum pkg_display_type {
PKG_DISPLAY_FETCH,
PKG_DISPLAY_MAX
};
struct pkg_solved_display_item {
struct pkg_solved_display {
struct pkg *new, *old;
enum pkg_display_type display_type;
pkg_solved_t solved_type;
struct pkg_solved_display_item *prev, *next;
};

typedef tll(struct pkg_solved_display *) pkg_solved_display_t;

static void
set_jobs_summary_pkg(struct pkg_jobs *jobs, struct pkg *new_pkg,
struct pkg *old_pkg, pkg_solved_t type, int64_t *oldsize,
int64_t *newsize, int64_t *dlsize, struct pkg_solved_display_item **disp,
int64_t *newsize, int64_t *dlsize, pkg_solved_display_t *disp,
struct jobs_sum_number *sum)
{
const char *oldversion, *repopath, *destdir;
char path[MAXPATHLEN];
int ret;
struct stat st;
int64_t flatsize, oldflatsize, pkgsize;
struct pkg_solved_display_item *it;
struct pkg_solved_display *it;

flatsize = oldflatsize = pkgsize = 0;
oldversion = NULL;
Expand All @@ -713,7 +715,7 @@ set_jobs_summary_pkg(struct pkg_jobs *jobs, struct pkg *new_pkg,
it = malloc(sizeof (*it));
if (it == NULL) {
fprintf(stderr, "malloc failed for "
"pkg_solved_display_item: %s", strerror (errno));
"pkg_solved_display: %s", strerror (errno));
return;
}
it->new = new_pkg;
Expand All @@ -723,7 +725,7 @@ set_jobs_summary_pkg(struct pkg_jobs *jobs, struct pkg *new_pkg,

if (old_pkg != NULL && pkg_is_locked(old_pkg)) {
it->display_type = PKG_DISPLAY_LOCKED;
DL_APPEND(disp[it->display_type], it);
tll_push_back(disp[it->display_type], it);
return;
}

Expand Down Expand Up @@ -808,11 +810,11 @@ set_jobs_summary_pkg(struct pkg_jobs *jobs, struct pkg *new_pkg,

break;
}
DL_APPEND(disp[it->display_type], it);
tll_push_back(disp[it->display_type], it);
}

static void
display_summary_item(struct pkg_solved_display_item *it, int64_t dlsize)
display_summary_item(struct pkg_solved_display *it, int64_t dlsize)
{
const char *why;
int64_t pkgsize;
Expand Down Expand Up @@ -919,10 +921,12 @@ static const char* pkg_display_messages[PKG_DISPLAY_MAX + 1] = {
};

static int
namecmp(struct pkg_solved_display_item *a, struct pkg_solved_display_item *b)
namecmp(const void *a, const void *b)
{
const struct pkg_solved_display *sda = *(const struct pkg_solved_display **) a;
const struct pkg_solved_display *sdb = *(const struct pkg_solved_display **) b;

return (pkg_namecmp(a->new, b->new));
return (pkg_namecmp(sda->new, sdb->new));
}

int
Expand All @@ -934,14 +938,15 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
va_list ap;
int type, displayed = 0;
int64_t dlsize, oldsize, newsize;
struct pkg_solved_display_item *disp[PKG_DISPLAY_MAX], *cur, *tmp;
pkg_solved_display_t disp[PKG_DISPLAY_MAX];
struct pkg_solved_display **displays;
bool first = true;
size_t bytes_change, limbytes;
struct jobs_sum_number sum;

dlsize = oldsize = newsize = 0;
type = pkg_jobs_type(jobs);
memset(disp, 0, sizeof(disp));
memset(disp, 0, sizeof(*disp) * PKG_DISPLAY_MAX);
memset(&sum, 0, sizeof(sum));

nbtodl = 0;
Expand All @@ -951,7 +956,7 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
}

for (type = 0; type < PKG_DISPLAY_MAX; type ++) {
if (disp[type] != NULL) {
if (tll_length(disp[type]) != 0) {
/* Space between each section. */
if (!first)
puts("");
Expand All @@ -965,12 +970,18 @@ print_jobs_summary(struct pkg_jobs *jobs, const char *msg, ...)
msg = NULL;
}
printf("%s:\n", pkg_display_messages[type]);
DL_SORT(disp[type], namecmp);
DL_FOREACH_SAFE(disp[type], cur, tmp) {
display_summary_item(cur, dlsize);
displays = xcalloc(tll_length(disp[type]), sizeof(*displays));
size_t i = 0;
tll_foreach(disp[type], d) {
displays[i++] = d->item;
}
qsort(displays, i, sizeof(displays[0]), namecmp);
for (i = 0; i < tll_length(disp[type]); i++) {
display_summary_item(displays[i], dlsize);
displayed ++;
free(cur);
}
tll_free_and_free(disp[type], free);
free(displays);
}
}

Expand Down

0 comments on commit f9ad73c

Please sign in to comment.