X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/agedu/blobdiff_plain/742c1a7442763964546f79d76b615bfcb476d604..92d3b3263849ae660384adde0c4ad90fdd82ef4f:/html.c diff --git a/html.c b/html.c index 76ecd83..4390668 100644 --- a/html.c +++ b/html.c @@ -2,22 +2,12 @@ * html.c: implementation of html.h. */ -#include -#include -#include -#include -#include -#include -#include -#include - +#include "agedu.h" #include "html.h" -#include "malloc.h" +#include "alloc.h" #include "trie.h" #include "index.h" -#define lenof(x) ( sizeof((x)) / sizeof(*(x)) ) - #define MAXCOLOUR 511 struct html { @@ -29,7 +19,8 @@ struct html { char *href; size_t hreflen; const char *format; - unsigned long long thresholds[MAXCOLOUR-1]; + unsigned long long thresholds[MAXCOLOUR]; + char *titletexts[MAXCOLOUR+1]; time_t now; }; @@ -37,9 +28,16 @@ static void vhtprintf(struct html *ctx, char *fmt, va_list ap) { va_list ap2; int size, size2; + char testbuf[2]; va_copy(ap2, ap); - size = vsnprintf(NULL, 0, fmt, ap2); + /* + * Some C libraries (Solaris, I'm looking at you) don't like + * an output buffer size of zero in vsnprintf, but will return + * sensible values given any non-zero buffer size. Hence, we + * use testbuf to gauge the length of the string. + */ + size = vsnprintf(testbuf, 1, fmt, ap2); va_end(ap2); if (ctx->buflen + size >= ctx->bufsize) { @@ -164,21 +162,20 @@ static void get_indices(const void *t, char *path, unsigned long *xi1, unsigned long *xi2) { size_t pathlen = strlen(path); + int c1 = path[pathlen], c2 = (pathlen > 0 ? path[pathlen-1] : 0); *xi1 = trie_before(t, path); - path[pathlen] = '\001'; - path[pathlen+1] = '\0'; + make_successor(path); *xi2 = trie_before(t, path); - path[pathlen] = '\0'; + path[pathlen] = c1; + if (pathlen > 0) + path[pathlen-1] = c2; } -static unsigned long long fetch_size(const void *t, char *path, +static unsigned long long fetch_size(const void *t, + unsigned long xi1, unsigned long xi2, unsigned long long atime) { - unsigned long xi1, xi2; - - get_indices(t, path, &xi1, &xi2); - return index_query(t, xi2, atime) - index_query(t, xi1, atime); } @@ -214,7 +211,6 @@ static void begin_colour_bar(struct html *ctx) static void add_to_colour_bar(struct html *ctx, int colour, int pixels) { int r, g, b; - char buf[80]; if (colour >= 0 && colour < 256) /* red -> yellow fade */ r = 255, g = colour, b = 0; @@ -223,26 +219,12 @@ static void add_to_colour_bar(struct html *ctx, int colour, int pixels) else /* background grey */ r = g = b = 240; - if (colour < 0) { - /* no title text here */ - } else if (colour == 0) { - strcpy(buf, "< "); - round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0); - } else if (colour == MAXCOLOUR) { - strcpy(buf, "> "); - round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0); - } else { - unsigned long long midrange = - (ctx->thresholds[colour] + ctx->thresholds[colour+1]) / 2; - round_and_format_age(ctx, midrange, buf, 0); - } - if (pixels > 0) { htprintf(ctx, "= 0) - htprintf(ctx, " title=\"%s\"", buf); + htprintf(ctx, " title=\"%s\"", ctx->titletexts[colour]); htprintf(ctx, ">\n"); } } @@ -255,6 +237,7 @@ static void end_colour_bar(struct html *ctx) struct vector { int want_href; char *name; + int literal; /* should the name be formatted in fixed-pitch? */ unsigned long index; unsigned long long sizes[MAXCOLOUR+1]; }; @@ -282,7 +265,7 @@ int vec_compare(const void *av, const void *bv) } static struct vector *make_vector(struct html *ctx, char *path, - int want_href, char *name) + int want_href, char *name, int literal) { unsigned long xi1, xi2; struct vector *vec = snew(struct vector); @@ -290,6 +273,7 @@ static struct vector *make_vector(struct html *ctx, char *path, vec->want_href = want_href; vec->name = name ? dupstr(name) : NULL; + vec->literal = literal; get_indices(ctx->t, path, &xi1, &xi2); @@ -301,7 +285,7 @@ static struct vector *make_vector(struct html *ctx, char *path, atime = ULLONG_MAX; else atime = ctx->thresholds[i]; - vec->sizes[i] = fetch_size(ctx->t, path, atime); + vec->sizes[i] = fetch_size(ctx->t, xi1, xi2, atime); } return vec; @@ -321,11 +305,20 @@ static void write_report_line(struct html *ctx, struct vector *vec) int i; /* - * Prevent divisions by zero. + * A line with literally zero space usage should not be + * printed at all if it's a link to a subdirectory (since it + * probably means the whole thing was excluded by some + * --exclude-path wildcard). If it's [files] or the top-level + * line, though, we must always print _something_, and in that + * case we must fiddle about to prevent divisions by zero in + * the code below. */ + if (!vec->sizes[MAXCOLOUR] && vec->want_href) + return; divisor = ctx->totalsize; - if (!divisor) + if (!divisor) { divisor = 1; + } /* * Find the total size of this subdirectory. @@ -333,7 +326,7 @@ static void write_report_line(struct html *ctx, struct vector *vec) size = vec->sizes[MAXCOLOUR]; htprintf(ctx, "\n" "%lluMb\n", - ((size + ((1<<11)-1)) >> 11)); /* convert to Mb, rounding up */ + ((size + ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */ /* * Generate a colour bar. @@ -369,19 +362,36 @@ static void write_report_line(struct html *ctx, struct vector *vec) htprintf(ctx, "", ctx->href); doing_href = 1; } + if (vec->literal) + htprintf(ctx, ""); htescape(ctx, vec->name, strlen(vec->name), 1); + if (vec->literal) + htprintf(ctx, ""); if (doing_href) htprintf(ctx, ""); } htprintf(ctx, "\n\n"); } -char *html_query(const void *t, unsigned long index, const char *format) +int strcmptrailingpathsep(const char *a, const char *b) +{ + while (*a == *b && *a) + a++, b++; + + if ((*a == pathsep && !a[1] && !*b) || + (*b == pathsep && !b[1] && !*a)) + return 0; + + return (int)(unsigned char)*a - (int)(unsigned char)*b; +} + +char *html_query(const void *t, unsigned long index, + const struct html_config *cfg) { struct html actx, *ctx = &actx; char *path, *path2, *p, *q, *href; char agebuf1[80], agebuf2[80]; - size_t pathlen, hreflen; + size_t pathlen, subdirpos, hreflen; unsigned long index2; int i; struct vector **vecs; @@ -394,13 +404,13 @@ char *html_query(const void *t, unsigned long index, const char *format) ctx->buf = NULL; ctx->buflen = ctx->bufsize = 0; ctx->t = t; - ctx->format = format; + ctx->format = cfg->format; htprintf(ctx, "\n"); path = snewn(1+trie_maxpathlen(t), char); ctx->path2 = path2 = snewn(1+trie_maxpathlen(t), char); - if (format) { - hreflen = strlen(format) + 100; + if (cfg->format) { + hreflen = strlen(cfg->format) + 100; href = snewn(hreflen, char); } else { hreflen = 0; @@ -414,7 +424,7 @@ char *html_query(const void *t, unsigned long index, const char *format) */ htprintf(ctx, "\n"); trie_getpath(t, index, path); - htprintf(ctx, "agedu: "); + htprintf(ctx, "<title>%s: ", PNAME); htescape(ctx, path, strlen(path), 0); htprintf(ctx, "\n"); htprintf(ctx, "\n"); @@ -432,26 +442,37 @@ char *html_query(const void *t, unsigned long index, const char *format) */ htprintf(ctx, "

\n"); q = path; - for (p = strchr(path, '/'); p; p = strchr(p+1, '/')) { + for (p = strchr(path, pathsep); p && p[1]; p = strchr(p, pathsep)) { int doing_href = 0; + char c, *zp; + /* * See if this path prefix exists in the trie. If so, * generate a hyperlink. */ - *p = '\0'; + zp = p; + if (p == path) /* special case for "/" at start */ + zp++; + + p++; + + c = *zp; + *zp = '\0'; index2 = trie_before(t, path); trie_getpath(t, index2, path2); - if (!strcmp(path, path2) && format) { - snprintf(href, hreflen, format, index2); + if (!strcmptrailingpathsep(path, path2) && cfg->format) { + snprintf(href, hreflen, cfg->format, index2); + if (!*href) /* special case that we understand */ + strcpy(href, "./"); htprintf(ctx, "", href); doing_href = 1; } - *p = '/'; - htescape(ctx, q, p - q, 1); - q = p + 1; + *zp = c; + htescape(ctx, q, zp - q, 1); if (doing_href) htprintf(ctx, ""); - htprintf(ctx, "/"); + htescape(ctx, zp, p - zp, 1); + q = p; } htescape(ctx, q, strlen(q), 1); htprintf(ctx, "\n"); @@ -460,14 +481,38 @@ char *html_query(const void *t, unsigned long index, const char *format) * Decide on the age limit of our colour coding, establish the * colour thresholds, and write out a key. */ - ctx->oldest = index_order_stat(t, 0.05); /* FIXME: configurability? */ - ctx->newest = index_order_stat(t, 1.0); ctx->now = time(NULL); - ctx->oldest = round_and_format_age(ctx, ctx->oldest, agebuf1, -1); - ctx->newest = round_and_format_age(ctx, ctx->newest, agebuf2, +1); - for (i = 0; i < MAXCOLOUR-1; i++) { + if (cfg->autoage) { + ctx->oldest = index_order_stat(t, 0.05); + ctx->newest = index_order_stat(t, 1.0); + ctx->oldest = round_and_format_age(ctx, ctx->oldest, agebuf1, -1); + ctx->newest = round_and_format_age(ctx, ctx->newest, agebuf2, +1); + } else { + ctx->oldest = cfg->oldest; + ctx->newest = cfg->newest; + ctx->oldest = round_and_format_age(ctx, ctx->oldest, agebuf1, 0); + ctx->newest = round_and_format_age(ctx, ctx->newest, agebuf2, 0); + } + for (i = 0; i < MAXCOLOUR; i++) { ctx->thresholds[i] = - ctx->oldest + (ctx->newest - ctx->oldest) * i / MAXCOLOUR; + ctx->oldest + (ctx->newest - ctx->oldest) * i / (MAXCOLOUR-1); + } + for (i = 0; i <= MAXCOLOUR; i++) { + char buf[80]; + + if (i == 0) { + strcpy(buf, "< "); + round_and_format_age(ctx, ctx->thresholds[0], buf+5, 0); + } else if (i == MAXCOLOUR) { + strcpy(buf, "> "); + round_and_format_age(ctx, ctx->thresholds[MAXCOLOUR-1], buf+5, 0); + } else { + unsigned long long midrange = + (ctx->thresholds[i-1] + ctx->thresholds[i]) / 2; + round_and_format_age(ctx, midrange, buf, 0); + } + + ctx->titletexts[i] = dupstr(buf); } htprintf(ctx, "

Key to colour coding (mouse over for more detail):\n"); htprintf(ctx, "

totalsize = fetch_size(t, path, ULLONG_MAX); + get_indices(t, path, &xi1, &xi2); + ctx->totalsize = fetch_size(t, xi1, xi2, ULLONG_MAX); /* * Generate a report line for the whole subdirectory. @@ -496,7 +542,7 @@ char *html_query(const void *t, unsigned long index, const char *format) vecsize = 64; vecs = snewn(vecsize, struct vector *); nvecs = 1; - vecs[0] = make_vector(ctx, path, 0, NULL); + vecs[0] = make_vector(ctx, path, 0, NULL, 0); print_heading(ctx, "Overall"); write_report_line(ctx, vecs[0]); @@ -510,6 +556,9 @@ char *html_query(const void *t, unsigned long index, const char *format) get_indices(t, path, &xi1, &xi2); xi1++; pathlen = strlen(path); + subdirpos = pathlen + 1; + if (pathlen > 0 && path[pathlen-1] == pathsep) + subdirpos--; while (xi1 < xi2) { trie_getpath(t, xi1, path2); get_indices(t, ctx->path2, &xj1, &xj2); @@ -521,7 +570,7 @@ char *html_query(const void *t, unsigned long index, const char *format) vecs = sresize(vecs, vecsize, struct vector *); } assert(strlen(path2) > pathlen); - vecs[nvecs] = make_vector(ctx, path2, 1, path2 + pathlen + 1); + vecs[nvecs] = make_vector(ctx, path2, 1, path2 + subdirpos, 1); for (i = 0; i <= MAXCOLOUR; i++) vecs[0]->sizes[i] -= vecs[nvecs]->sizes[i]; nvecs++;