2 * html.c: implementation of html.h.
24 size_t buflen
, bufsize
;
26 unsigned long long totalsize
, oldest
, newest
;
31 unsigned long long thresholds
[MAXCOLOUR
-1];
35 static void vhtprintf(struct html
*ctx
, char *fmt
, va_list ap
)
41 size
= vsnprintf(NULL
, 0, fmt
, ap2
);
44 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
45 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
46 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
48 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
50 assert(size
== size2
);
54 static void htprintf(struct html
*ctx
, char *fmt
, ...)
58 vhtprintf(ctx
, fmt
, ap
);
62 static unsigned long long round_and_format_age(struct html
*ctx
,
63 unsigned long long age
,
64 char *buf
, int direction
)
68 unsigned long long ret
, newret
;
71 static const int minutes
[] = { 5, 10, 15, 30, 45 };
73 tm
= *localtime(&ctx
->now
);
74 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
79 for (i
= 0; i
< lenof(minutes
); i
++) {
80 newret
= ctx
->now
- minutes
[i
] * 60;
81 sprintf(newbuf
, "%d minutes", minutes
[i
]);
88 for (i
= 1; i
< 24; i
++) {
89 newret
= ctx
->now
- i
* (60*60);
90 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
97 for (i
= 1; i
< 7; i
++) {
98 newret
= ctx
->now
- i
* (24*60*60);
99 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
106 for (i
= 1; i
< 4; i
++) {
107 newret
= ctx
->now
- i
* (7*24*60*60);
108 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
115 for (i
= 1; i
< 11; i
++) {
116 tm2
= tm
; /* structure copy */
117 tm2
.tm_year
= (ym
- i
) / 12;
118 tm2
.tm_mon
= (ym
- i
) % 12;
119 newret
= mktime(&tm2
);
120 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
128 tm2
= tm
; /* structure copy */
129 tm2
.tm_year
= (ym
- i
*12) / 12;
130 tm2
.tm_mon
= (ym
- i
*12) % 12;
131 newret
= mktime(&tm2
);
132 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
142 * Round toward newest, i.e. use the existing (buf,ret).
144 } else if (direction
< 0) {
146 * Round toward oldest, i.e. use (newbuf,newret);
154 if (ret
- age
> age
- newret
) {
162 static void get_indices(const void *t
, char *path
,
163 unsigned long *xi1
, unsigned long *xi2
)
165 size_t pathlen
= strlen(path
);
166 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
168 *xi1
= trie_before(t
, path
);
169 make_successor(path
);
170 *xi2
= trie_before(t
, path
);
173 path
[pathlen
-1] = c2
;
176 static unsigned long long fetch_size(const void *t
, char *path
,
177 unsigned long long atime
)
179 unsigned long xi1
, xi2
;
181 get_indices(t
, path
, &xi1
, &xi2
);
183 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
186 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
188 while (n
> 0 && *s
) {
189 unsigned char c
= (unsigned char)*s
++;
192 htprintf(ctx
, "&");
194 htprintf(ctx
, "<");
196 htprintf(ctx
, ">");
197 else if (c
>= ' ' && c
< '\177')
198 htprintf(ctx
, "%c", c
);
200 if (italics
) htprintf(ctx
, "<i>");
201 htprintf(ctx
, "[%02x]", c
);
202 if (italics
) htprintf(ctx
, "</i>");
209 static void begin_colour_bar(struct html
*ctx
)
211 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
212 " style=\"border:0\">\n<tr>\n");
215 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
220 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
221 r
= 255, g
= colour
, b
= 0;
222 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
223 r
= 511 - colour
, g
= 255, b
= 0;
224 else /* background grey */
228 /* no title text here */
229 } else if (colour
== 0) {
230 strcpy(buf
, "< ");
231 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
232 } else if (colour
== MAXCOLOUR
) {
233 strcpy(buf
, "> ");
234 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
236 unsigned long long midrange
=
237 (ctx
->thresholds
[colour
] + ctx
->thresholds
[colour
+1]) / 2;
238 round_and_format_age(ctx
, midrange
, buf
, 0);
242 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
243 "background-color:#%02x%02x%02x\"",
246 htprintf(ctx
, " title=\"%s\"", buf
);
247 htprintf(ctx
, "></td>\n");
251 static void end_colour_bar(struct html
*ctx
)
253 htprintf(ctx
, "</tr>\n</table>\n");
260 unsigned long long sizes
[MAXCOLOUR
+1];
263 int vec_compare(const void *av
, const void *bv
)
265 const struct vector
*a
= *(const struct vector
**)av
;
266 const struct vector
*b
= *(const struct vector
**)bv
;
268 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
270 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
272 else if (a
->want_href
< b
->want_href
)
274 else if (a
->want_href
> b
->want_href
)
276 else if (a
->want_href
)
277 return strcmp(a
->name
, b
->name
);
278 else if (a
->index
< b
->index
)
280 else if (a
->index
> b
->index
)
285 static struct vector
*make_vector(struct html
*ctx
, char *path
,
286 int want_href
, char *name
)
288 unsigned long xi1
, xi2
;
289 struct vector
*vec
= snew(struct vector
);
292 vec
->want_href
= want_href
;
293 vec
->name
= name ?
dupstr(name
) : NULL
;
295 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
299 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
300 unsigned long long atime
;
304 atime
= ctx
->thresholds
[i
];
305 vec
->sizes
[i
] = fetch_size(ctx
->t
, path
, atime
);
311 static void print_heading(struct html
*ctx
, const char *title
)
313 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
314 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
317 #define PIXEL_SIZE 600 /* FIXME: configurability? */
318 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
320 unsigned long long size
, asize
, divisor
;
325 * A line with literally zero space usage should not be
326 * printed at all if it's a link to a subdirectory (since it
327 * probably means the whole thing was excluded by some
328 * --exclude-path wildcard). If it's [files] or the top-level
329 * line, though, we must always print _something_, and in that
330 * case we must fiddle about to prevent divisions by zero in
333 if (!vec
->sizes
[MAXCOLOUR
] && vec
->want_href
)
335 divisor
= ctx
->totalsize
;
341 * Find the total size of this subdirectory.
343 size
= vec
->sizes
[MAXCOLOUR
];
344 htprintf(ctx
, "<tr>\n"
345 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
346 ((size
+ ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */
349 * Generate a colour bar.
351 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
352 begin_colour_bar(ctx
);
354 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
355 asize
= vec
->sizes
[i
];
356 newpix
= asize
* PIXEL_SIZE
/ divisor
;
357 add_to_colour_bar(ctx
, i
, newpix
- pix
);
360 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
362 htprintf(ctx
, "</td>\n");
365 * Output size as a percentage of totalsize.
367 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
368 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
371 * Output a subdirectory marker.
373 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
377 if (ctx
->format
&& vec
->want_href
) {
378 snprintf(ctx
->href
, ctx
->hreflen
, ctx
->format
, vec
->index
);
379 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
382 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
384 htprintf(ctx
, "</a>");
386 htprintf(ctx
, "</td>\n</tr>\n");
389 char *html_query(const void *t
, unsigned long index
,
390 const struct html_config
*cfg
)
392 struct html actx
, *ctx
= &actx
;
393 char *path
, *path2
, *p
, *q
, *href
;
394 char agebuf1
[80], agebuf2
[80];
395 size_t pathlen
, subdirpos
, hreflen
;
396 unsigned long index2
;
398 struct vector
**vecs
;
400 unsigned long xi1
, xi2
, xj1
, xj2
;
402 if (index
>= trie_count(t
))
406 ctx
->buflen
= ctx
->bufsize
= 0;
408 ctx
->format
= cfg
->format
;
409 htprintf(ctx
, "<html>\n");
411 path
= snewn(1+trie_maxpathlen(t
), char);
412 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
414 hreflen
= strlen(cfg
->format
) + 100;
415 href
= snewn(hreflen
, char);
420 ctx
->hreflen
= hreflen
;
426 htprintf(ctx
, "<head>\n");
427 trie_getpath(t
, index
, path
);
428 htprintf(ctx
, "<title>%s: ", PNAME
);
429 htescape(ctx
, path
, strlen(path
), 0);
430 htprintf(ctx
, "</title>\n");
431 htprintf(ctx
, "</head>\n");
434 * Begin BODY section.
436 htprintf(ctx
, "<body>\n");
437 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
438 " last-access time</h3>\n");
441 * Show the pathname we're centred on, with hyperlinks to
442 * parent directories where available.
444 htprintf(ctx
, "<p align=center>\n<code>");
446 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
451 * See if this path prefix exists in the trie. If so,
452 * generate a hyperlink.
455 if (p
== path
) /* special case for "/" at start */
462 index2
= trie_before(t
, path
);
463 trie_getpath(t
, index2
, path2
);
464 if (!strcmp(path
, path2
) && cfg
->format
) {
465 snprintf(href
, hreflen
, cfg
->format
, index2
);
466 if (!*href
) /* special case that we understand */
468 htprintf(ctx
, "<a href=\"%s\">", href
);
472 htescape(ctx
, q
, zp
- q
, 1);
474 htprintf(ctx
, "</a>");
475 htescape(ctx
, zp
, p
- zp
, 1);
478 htescape(ctx
, q
, strlen(q
), 1);
479 htprintf(ctx
, "</code>\n");
482 * Decide on the age limit of our colour coding, establish the
483 * colour thresholds, and write out a key.
485 ctx
->now
= time(NULL
);
487 ctx
->oldest
= index_order_stat(t
, 0.05);
488 ctx
->newest
= index_order_stat(t
, 1.0);
489 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
490 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
492 ctx
->oldest
= cfg
->oldest
;
493 ctx
->newest
= cfg
->newest
;
494 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
495 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
497 for (i
= 0; i
< MAXCOLOUR
-1; i
++) {
499 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ MAXCOLOUR
;
501 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
502 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
503 "margin-bottom:1em\"");
504 begin_colour_bar(ctx
);
505 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
506 for (i
= 0; i
< MAXCOLOUR
; i
++)
507 add_to_colour_bar(ctx
, i
, 1);
508 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
512 * Begin the main table.
514 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
517 * Find the total size of our entire subdirectory. We'll use
518 * that as the scale for all the colour bars in this report.
520 ctx
->totalsize
= fetch_size(t
, path
, ULLONG_MAX
);
523 * Generate a report line for the whole subdirectory.
526 vecs
= snewn(vecsize
, struct vector
*);
528 vecs
[0] = make_vector(ctx
, path
, 0, NULL
);
529 print_heading(ctx
, "Overall");
530 write_report_line(ctx
, vecs
[0]);
533 * Now generate report lines for all its children, and the
534 * files contained in it.
536 print_heading(ctx
, "Subdirectories");
538 vecs
[0]->name
= dupstr("[files]");
539 get_indices(t
, path
, &xi1
, &xi2
);
541 pathlen
= strlen(path
);
542 subdirpos
= pathlen
+ 1;
543 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
546 trie_getpath(t
, xi1
, path2
);
547 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
550 continue; /* skip individual files */
551 if (nvecs
>= vecsize
) {
552 vecsize
= nvecs
* 3 / 2 + 64;
553 vecs
= sresize(vecs
, vecsize
, struct vector
*);
555 assert(strlen(path2
) > pathlen
);
556 vecs
[nvecs
] = make_vector(ctx
, path2
, 1, path2
+ subdirpos
);
557 for (i
= 0; i
<= MAXCOLOUR
; i
++)
558 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
562 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
564 for (i
= 0; i
< nvecs
; i
++)
565 write_report_line(ctx
, vecs
[i
]);
568 * Close the main table.
570 htprintf(ctx
, "</table>\n");
573 * Finish up and tidy up.
575 htprintf(ctx
, "</body>\n");
576 htprintf(ctx
, "</html>\n");
580 for (i
= 0; i
< nvecs
; i
++) {
581 sfree(vecs
[i
]->name
);