2 * html.c: implementation of html.h.
15 size_t buflen
, bufsize
;
17 unsigned long long totalsize
, oldest
, newest
;
22 unsigned long long thresholds
[MAXCOLOUR
];
23 char *titletexts
[MAXCOLOUR
+1];
27 static void vhtprintf(struct html
*ctx
, char *fmt
, va_list ap
)
35 * Some C libraries (Solaris, I'm looking at you) don't like
36 * an output buffer size of zero in vsnprintf, but will return
37 * sensible values given any non-zero buffer size. Hence, we
38 * use testbuf to gauge the length of the string.
40 size
= vsnprintf(testbuf
, 1, fmt
, ap2
);
43 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
44 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
45 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
47 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
49 assert(size
== size2
);
53 static void htprintf(struct html
*ctx
, char *fmt
, ...)
57 vhtprintf(ctx
, fmt
, ap
);
61 static unsigned long long round_and_format_age(struct html
*ctx
,
62 unsigned long long age
,
63 char *buf
, int direction
)
67 unsigned long long ret
, newret
;
70 static const int minutes
[] = { 5, 10, 15, 30, 45 };
72 tm
= *localtime(&ctx
->now
);
73 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
78 for (i
= 0; i
< lenof(minutes
); i
++) {
79 newret
= ctx
->now
- minutes
[i
] * 60;
80 sprintf(newbuf
, "%d minutes", minutes
[i
]);
87 for (i
= 1; i
< 24; i
++) {
88 newret
= ctx
->now
- i
* (60*60);
89 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
96 for (i
= 1; i
< 7; i
++) {
97 newret
= ctx
->now
- i
* (24*60*60);
98 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
105 for (i
= 1; i
< 4; i
++) {
106 newret
= ctx
->now
- i
* (7*24*60*60);
107 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
114 for (i
= 1; i
< 11; i
++) {
115 tm2
= tm
; /* structure copy */
116 tm2
.tm_year
= (ym
- i
) / 12;
117 tm2
.tm_mon
= (ym
- i
) % 12;
118 newret
= mktime(&tm2
);
119 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
127 tm2
= tm
; /* structure copy */
128 tm2
.tm_year
= (ym
- i
*12) / 12;
129 tm2
.tm_mon
= (ym
- i
*12) % 12;
130 newret
= mktime(&tm2
);
131 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
141 * Round toward newest, i.e. use the existing (buf,ret).
143 } else if (direction
< 0) {
145 * Round toward oldest, i.e. use (newbuf,newret);
153 if (ret
- age
> age
- newret
) {
161 static void get_indices(const void *t
, char *path
,
162 unsigned long *xi1
, unsigned long *xi2
)
164 size_t pathlen
= strlen(path
);
165 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
167 *xi1
= trie_before(t
, path
);
168 make_successor(path
);
169 *xi2
= trie_before(t
, path
);
172 path
[pathlen
-1] = c2
;
175 static unsigned long long fetch_size(const void *t
,
176 unsigned long xi1
, unsigned long xi2
,
177 unsigned long long atime
)
179 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
182 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
184 while (n
> 0 && *s
) {
185 unsigned char c
= (unsigned char)*s
++;
188 htprintf(ctx
, "&");
190 htprintf(ctx
, "<");
192 htprintf(ctx
, ">");
193 else if (c
>= ' ' && c
< '\177')
194 htprintf(ctx
, "%c", c
);
196 if (italics
) htprintf(ctx
, "<i>");
197 htprintf(ctx
, "[%02x]", c
);
198 if (italics
) htprintf(ctx
, "</i>");
205 static void begin_colour_bar(struct html
*ctx
)
207 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
208 " style=\"border:0\">\n<tr>\n");
211 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
215 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
216 r
= 255, g
= colour
, b
= 0;
217 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
218 r
= 511 - colour
, g
= 255, b
= 0;
219 else /* background grey */
223 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
224 "background-color:#%02x%02x%02x\"",
227 htprintf(ctx
, " title=\"%s\"", ctx
->titletexts
[colour
]);
228 htprintf(ctx
, "></td>\n");
232 static void end_colour_bar(struct html
*ctx
)
234 htprintf(ctx
, "</tr>\n</table>\n");
241 unsigned long long sizes
[MAXCOLOUR
+1];
244 int vec_compare(const void *av
, const void *bv
)
246 const struct vector
*a
= *(const struct vector
**)av
;
247 const struct vector
*b
= *(const struct vector
**)bv
;
249 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
251 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
253 else if (a
->want_href
< b
->want_href
)
255 else if (a
->want_href
> b
->want_href
)
257 else if (a
->want_href
)
258 return strcmp(a
->name
, b
->name
);
259 else if (a
->index
< b
->index
)
261 else if (a
->index
> b
->index
)
266 static struct vector
*make_vector(struct html
*ctx
, char *path
,
267 int want_href
, char *name
)
269 unsigned long xi1
, xi2
;
270 struct vector
*vec
= snew(struct vector
);
273 vec
->want_href
= want_href
;
274 vec
->name
= name ?
dupstr(name
) : NULL
;
276 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
280 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
281 unsigned long long atime
;
285 atime
= ctx
->thresholds
[i
];
286 vec
->sizes
[i
] = fetch_size(ctx
->t
, xi1
, xi2
, atime
);
292 static void print_heading(struct html
*ctx
, const char *title
)
294 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
295 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
298 #define PIXEL_SIZE 600 /* FIXME: configurability? */
299 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
301 unsigned long long size
, asize
, divisor
;
306 * A line with literally zero space usage should not be
307 * printed at all if it's a link to a subdirectory (since it
308 * probably means the whole thing was excluded by some
309 * --exclude-path wildcard). If it's [files] or the top-level
310 * line, though, we must always print _something_, and in that
311 * case we must fiddle about to prevent divisions by zero in
314 if (!vec
->sizes
[MAXCOLOUR
] && vec
->want_href
)
316 divisor
= ctx
->totalsize
;
322 * Find the total size of this subdirectory.
324 size
= vec
->sizes
[MAXCOLOUR
];
325 htprintf(ctx
, "<tr>\n"
326 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
327 ((size
+ ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */
330 * Generate a colour bar.
332 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
333 begin_colour_bar(ctx
);
335 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
336 asize
= vec
->sizes
[i
];
337 newpix
= asize
* PIXEL_SIZE
/ divisor
;
338 add_to_colour_bar(ctx
, i
, newpix
- pix
);
341 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
343 htprintf(ctx
, "</td>\n");
346 * Output size as a percentage of totalsize.
348 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
349 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
352 * Output a subdirectory marker.
354 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
358 if (ctx
->format
&& vec
->want_href
) {
359 snprintf(ctx
->href
, ctx
->hreflen
, ctx
->format
, vec
->index
);
360 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
363 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
365 htprintf(ctx
, "</a>");
367 htprintf(ctx
, "</td>\n</tr>\n");
370 int strcmptrailingpathsep(const char *a
, const char *b
)
372 while (*a
== *b
&& *a
)
375 if ((*a
== pathsep
&& !a
[1] && !*b
) ||
376 (*b
== pathsep
&& !b
[1] && !*a
))
379 return (int)(unsigned char)*a
- (int)(unsigned char)*b
;
382 char *html_query(const void *t
, unsigned long index
,
383 const struct html_config
*cfg
)
385 struct html actx
, *ctx
= &actx
;
386 char *path
, *path2
, *p
, *q
, *href
;
387 char agebuf1
[80], agebuf2
[80];
388 size_t pathlen
, subdirpos
, hreflen
;
389 unsigned long index2
;
391 struct vector
**vecs
;
393 unsigned long xi1
, xi2
, xj1
, xj2
;
395 if (index
>= trie_count(t
))
399 ctx
->buflen
= ctx
->bufsize
= 0;
401 ctx
->format
= cfg
->format
;
402 htprintf(ctx
, "<html>\n");
404 path
= snewn(1+trie_maxpathlen(t
), char);
405 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
407 hreflen
= strlen(cfg
->format
) + 100;
408 href
= snewn(hreflen
, char);
413 ctx
->hreflen
= hreflen
;
419 htprintf(ctx
, "<head>\n");
420 trie_getpath(t
, index
, path
);
421 htprintf(ctx
, "<title>%s: ", PNAME
);
422 htescape(ctx
, path
, strlen(path
), 0);
423 htprintf(ctx
, "</title>\n");
424 htprintf(ctx
, "</head>\n");
427 * Begin BODY section.
429 htprintf(ctx
, "<body>\n");
430 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
431 " last-access time</h3>\n");
434 * Show the pathname we're centred on, with hyperlinks to
435 * parent directories where available.
437 htprintf(ctx
, "<p align=center>\n<code>");
439 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
444 * See if this path prefix exists in the trie. If so,
445 * generate a hyperlink.
448 if (p
== path
) /* special case for "/" at start */
455 index2
= trie_before(t
, path
);
456 trie_getpath(t
, index2
, path2
);
457 if (!strcmptrailingpathsep(path
, path2
) && cfg
->format
) {
458 snprintf(href
, hreflen
, cfg
->format
, index2
);
459 if (!*href
) /* special case that we understand */
461 htprintf(ctx
, "<a href=\"%s\">", href
);
465 htescape(ctx
, q
, zp
- q
, 1);
467 htprintf(ctx
, "</a>");
468 htescape(ctx
, zp
, p
- zp
, 1);
471 htescape(ctx
, q
, strlen(q
), 1);
472 htprintf(ctx
, "</code>\n");
475 * Decide on the age limit of our colour coding, establish the
476 * colour thresholds, and write out a key.
478 ctx
->now
= time(NULL
);
480 ctx
->oldest
= index_order_stat(t
, 0.05);
481 ctx
->newest
= index_order_stat(t
, 1.0);
482 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
483 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
485 ctx
->oldest
= cfg
->oldest
;
486 ctx
->newest
= cfg
->newest
;
487 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
488 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
490 for (i
= 0; i
< MAXCOLOUR
; i
++) {
492 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ (MAXCOLOUR
-1);
494 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
498 strcpy(buf
, "< ");
499 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
500 } else if (i
== MAXCOLOUR
) {
501 strcpy(buf
, "> ");
502 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
504 unsigned long long midrange
=
505 (ctx
->thresholds
[i
-1] + ctx
->thresholds
[i
]) / 2;
506 round_and_format_age(ctx
, midrange
, buf
, 0);
509 ctx
->titletexts
[i
] = dupstr(buf
);
511 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
512 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
513 "margin-bottom:1em\"");
514 begin_colour_bar(ctx
);
515 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
516 for (i
= 0; i
< MAXCOLOUR
; i
++)
517 add_to_colour_bar(ctx
, i
, 1);
518 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
522 * Begin the main table.
524 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
527 * Find the total size of our entire subdirectory. We'll use
528 * that as the scale for all the colour bars in this report.
530 get_indices(t
, path
, &xi1
, &xi2
);
531 ctx
->totalsize
= fetch_size(t
, xi1
, xi2
, ULLONG_MAX
);
534 * Generate a report line for the whole subdirectory.
537 vecs
= snewn(vecsize
, struct vector
*);
539 vecs
[0] = make_vector(ctx
, path
, 0, NULL
);
540 print_heading(ctx
, "Overall");
541 write_report_line(ctx
, vecs
[0]);
544 * Now generate report lines for all its children, and the
545 * files contained in it.
547 print_heading(ctx
, "Subdirectories");
549 vecs
[0]->name
= dupstr("[files]");
550 get_indices(t
, path
, &xi1
, &xi2
);
552 pathlen
= strlen(path
);
553 subdirpos
= pathlen
+ 1;
554 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
557 trie_getpath(t
, xi1
, path2
);
558 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
561 continue; /* skip individual files */
562 if (nvecs
>= vecsize
) {
563 vecsize
= nvecs
* 3 / 2 + 64;
564 vecs
= sresize(vecs
, vecsize
, struct vector
*);
566 assert(strlen(path2
) > pathlen
);
567 vecs
[nvecs
] = make_vector(ctx
, path2
, 1, path2
+ subdirpos
);
568 for (i
= 0; i
<= MAXCOLOUR
; i
++)
569 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
573 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
575 for (i
= 0; i
< nvecs
; i
++)
576 write_report_line(ctx
, vecs
[i
]);
579 * Close the main table.
581 htprintf(ctx
, "</table>\n");
584 * Finish up and tidy up.
586 htprintf(ctx
, "</body>\n");
587 htprintf(ctx
, "</html>\n");
591 for (i
= 0; i
< nvecs
; i
++) {
592 sfree(vecs
[i
]->name
);