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
-1];
26 static void vhtprintf(struct html
*ctx
, char *fmt
, va_list ap
)
34 * Some C libraries (Solaris, I'm looking at you) don't like
35 * an output buffer size of zero in vsnprintf, but will return
36 * sensible values given any non-zero buffer size. Hence, we
37 * use testbuf to gauge the length of the string.
39 size
= vsnprintf(testbuf
, 1, fmt
, ap2
);
42 if (ctx
->buflen
+ size
>= ctx
->bufsize
) {
43 ctx
->bufsize
= (ctx
->buflen
+ size
) * 3 / 2 + 1024;
44 ctx
->buf
= sresize(ctx
->buf
, ctx
->bufsize
, char);
46 size2
= vsnprintf(ctx
->buf
+ ctx
->buflen
, ctx
->bufsize
- ctx
->buflen
,
48 assert(size
== size2
);
52 static void htprintf(struct html
*ctx
, char *fmt
, ...)
56 vhtprintf(ctx
, fmt
, ap
);
60 static unsigned long long round_and_format_age(struct html
*ctx
,
61 unsigned long long age
,
62 char *buf
, int direction
)
66 unsigned long long ret
, newret
;
69 static const int minutes
[] = { 5, 10, 15, 30, 45 };
71 tm
= *localtime(&ctx
->now
);
72 ym
= tm
.tm_year
* 12 + tm
.tm_mon
;
77 for (i
= 0; i
< lenof(minutes
); i
++) {
78 newret
= ctx
->now
- minutes
[i
] * 60;
79 sprintf(newbuf
, "%d minutes", minutes
[i
]);
86 for (i
= 1; i
< 24; i
++) {
87 newret
= ctx
->now
- i
* (60*60);
88 sprintf(newbuf
, "%d hour%s", i
, i
==1 ?
"" : "s");
95 for (i
= 1; i
< 7; i
++) {
96 newret
= ctx
->now
- i
* (24*60*60);
97 sprintf(newbuf
, "%d day%s", i
, i
==1 ?
"" : "s");
104 for (i
= 1; i
< 4; i
++) {
105 newret
= ctx
->now
- i
* (7*24*60*60);
106 sprintf(newbuf
, "%d week%s", i
, i
==1 ?
"" : "s");
113 for (i
= 1; i
< 11; i
++) {
114 tm2
= tm
; /* structure copy */
115 tm2
.tm_year
= (ym
- i
) / 12;
116 tm2
.tm_mon
= (ym
- i
) % 12;
117 newret
= mktime(&tm2
);
118 sprintf(newbuf
, "%d month%s", i
, i
==1 ?
"" : "s");
126 tm2
= tm
; /* structure copy */
127 tm2
.tm_year
= (ym
- i
*12) / 12;
128 tm2
.tm_mon
= (ym
- i
*12) % 12;
129 newret
= mktime(&tm2
);
130 sprintf(newbuf
, "%d year%s", i
, i
==1 ?
"" : "s");
140 * Round toward newest, i.e. use the existing (buf,ret).
142 } else if (direction
< 0) {
144 * Round toward oldest, i.e. use (newbuf,newret);
152 if (ret
- age
> age
- newret
) {
160 static void get_indices(const void *t
, char *path
,
161 unsigned long *xi1
, unsigned long *xi2
)
163 size_t pathlen
= strlen(path
);
164 int c1
= path
[pathlen
], c2
= (pathlen
> 0 ? path
[pathlen
-1] : 0);
166 *xi1
= trie_before(t
, path
);
167 make_successor(path
);
168 *xi2
= trie_before(t
, path
);
171 path
[pathlen
-1] = c2
;
174 static unsigned long long fetch_size(const void *t
, char *path
,
175 unsigned long long atime
)
177 unsigned long xi1
, xi2
;
179 get_indices(t
, path
, &xi1
, &xi2
);
181 return index_query(t
, xi2
, atime
) - index_query(t
, xi1
, atime
);
184 static void htescape(struct html
*ctx
, const char *s
, int n
, int italics
)
186 while (n
> 0 && *s
) {
187 unsigned char c
= (unsigned char)*s
++;
190 htprintf(ctx
, "&");
192 htprintf(ctx
, "<");
194 htprintf(ctx
, ">");
195 else if (c
>= ' ' && c
< '\177')
196 htprintf(ctx
, "%c", c
);
198 if (italics
) htprintf(ctx
, "<i>");
199 htprintf(ctx
, "[%02x]", c
);
200 if (italics
) htprintf(ctx
, "</i>");
207 static void begin_colour_bar(struct html
*ctx
)
209 htprintf(ctx
, "<table cellspacing=0 cellpadding=0"
210 " style=\"border:0\">\n<tr>\n");
213 static void add_to_colour_bar(struct html
*ctx
, int colour
, int pixels
)
218 if (colour
>= 0 && colour
< 256) /* red -> yellow fade */
219 r
= 255, g
= colour
, b
= 0;
220 else if (colour
>= 256 && colour
<= 511) /* yellow -> green fade */
221 r
= 511 - colour
, g
= 255, b
= 0;
222 else /* background grey */
226 /* no title text here */
227 } else if (colour
== 0) {
228 strcpy(buf
, "< ");
229 round_and_format_age(ctx
, ctx
->thresholds
[0], buf
+5, 0);
230 } else if (colour
== MAXCOLOUR
) {
231 strcpy(buf
, "> ");
232 round_and_format_age(ctx
, ctx
->thresholds
[MAXCOLOUR
-1], buf
+5, 0);
234 unsigned long long midrange
=
235 (ctx
->thresholds
[colour
] + ctx
->thresholds
[colour
+1]) / 2;
236 round_and_format_age(ctx
, midrange
, buf
, 0);
240 htprintf(ctx
, "<td style=\"width:%dpx; height:1em; "
241 "background-color:#%02x%02x%02x\"",
244 htprintf(ctx
, " title=\"%s\"", buf
);
245 htprintf(ctx
, "></td>\n");
249 static void end_colour_bar(struct html
*ctx
)
251 htprintf(ctx
, "</tr>\n</table>\n");
258 unsigned long long sizes
[MAXCOLOUR
+1];
261 int vec_compare(const void *av
, const void *bv
)
263 const struct vector
*a
= *(const struct vector
**)av
;
264 const struct vector
*b
= *(const struct vector
**)bv
;
266 if (a
->sizes
[MAXCOLOUR
] > b
->sizes
[MAXCOLOUR
])
268 else if (a
->sizes
[MAXCOLOUR
] < b
->sizes
[MAXCOLOUR
])
270 else if (a
->want_href
< b
->want_href
)
272 else if (a
->want_href
> b
->want_href
)
274 else if (a
->want_href
)
275 return strcmp(a
->name
, b
->name
);
276 else if (a
->index
< b
->index
)
278 else if (a
->index
> b
->index
)
283 static struct vector
*make_vector(struct html
*ctx
, char *path
,
284 int want_href
, char *name
)
286 unsigned long xi1
, xi2
;
287 struct vector
*vec
= snew(struct vector
);
290 vec
->want_href
= want_href
;
291 vec
->name
= name ?
dupstr(name
) : NULL
;
293 get_indices(ctx
->t
, path
, &xi1
, &xi2
);
297 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
298 unsigned long long atime
;
302 atime
= ctx
->thresholds
[i
];
303 vec
->sizes
[i
] = fetch_size(ctx
->t
, path
, atime
);
309 static void print_heading(struct html
*ctx
, const char *title
)
311 htprintf(ctx
, "<tr style=\"padding: 0.2em; background-color:#e0e0e0\">\n"
312 "<td colspan=4 align=center>%s</td>\n</tr>\n", title
);
315 #define PIXEL_SIZE 600 /* FIXME: configurability? */
316 static void write_report_line(struct html
*ctx
, struct vector
*vec
)
318 unsigned long long size
, asize
, divisor
;
323 * A line with literally zero space usage should not be
324 * printed at all if it's a link to a subdirectory (since it
325 * probably means the whole thing was excluded by some
326 * --exclude-path wildcard). If it's [files] or the top-level
327 * line, though, we must always print _something_, and in that
328 * case we must fiddle about to prevent divisions by zero in
331 if (!vec
->sizes
[MAXCOLOUR
] && vec
->want_href
)
333 divisor
= ctx
->totalsize
;
339 * Find the total size of this subdirectory.
341 size
= vec
->sizes
[MAXCOLOUR
];
342 htprintf(ctx
, "<tr>\n"
343 "<td style=\"padding: 0.2em; text-align: right\">%lluMb</td>\n",
344 ((size
+ ((1<<20)-1)) >> 20)); /* convert to Mb, rounding up */
347 * Generate a colour bar.
349 htprintf(ctx
, "<td style=\"padding: 0.2em\">\n");
350 begin_colour_bar(ctx
);
352 for (i
= 0; i
<= MAXCOLOUR
; i
++) {
353 asize
= vec
->sizes
[i
];
354 newpix
= asize
* PIXEL_SIZE
/ divisor
;
355 add_to_colour_bar(ctx
, i
, newpix
- pix
);
358 add_to_colour_bar(ctx
, -1, PIXEL_SIZE
- pix
);
360 htprintf(ctx
, "</td>\n");
363 * Output size as a percentage of totalsize.
365 htprintf(ctx
, "<td style=\"padding: 0.2em; text-align: right\">"
366 "%.2f%%</td>\n", (double)size
/ divisor
* 100.0);
369 * Output a subdirectory marker.
371 htprintf(ctx
, "<td style=\"padding: 0.2em\">");
375 if (ctx
->format
&& vec
->want_href
) {
376 snprintf(ctx
->href
, ctx
->hreflen
, ctx
->format
, vec
->index
);
377 htprintf(ctx
, "<a href=\"%s\">", ctx
->href
);
380 htescape(ctx
, vec
->name
, strlen(vec
->name
), 1);
382 htprintf(ctx
, "</a>");
384 htprintf(ctx
, "</td>\n</tr>\n");
387 char *html_query(const void *t
, unsigned long index
,
388 const struct html_config
*cfg
)
390 struct html actx
, *ctx
= &actx
;
391 char *path
, *path2
, *p
, *q
, *href
;
392 char agebuf1
[80], agebuf2
[80];
393 size_t pathlen
, subdirpos
, hreflen
;
394 unsigned long index2
;
396 struct vector
**vecs
;
398 unsigned long xi1
, xi2
, xj1
, xj2
;
400 if (index
>= trie_count(t
))
404 ctx
->buflen
= ctx
->bufsize
= 0;
406 ctx
->format
= cfg
->format
;
407 htprintf(ctx
, "<html>\n");
409 path
= snewn(1+trie_maxpathlen(t
), char);
410 ctx
->path2
= path2
= snewn(1+trie_maxpathlen(t
), char);
412 hreflen
= strlen(cfg
->format
) + 100;
413 href
= snewn(hreflen
, char);
418 ctx
->hreflen
= hreflen
;
424 htprintf(ctx
, "<head>\n");
425 trie_getpath(t
, index
, path
);
426 htprintf(ctx
, "<title>%s: ", PNAME
);
427 htescape(ctx
, path
, strlen(path
), 0);
428 htprintf(ctx
, "</title>\n");
429 htprintf(ctx
, "</head>\n");
432 * Begin BODY section.
434 htprintf(ctx
, "<body>\n");
435 htprintf(ctx
, "<h3 align=center>Disk space breakdown by"
436 " last-access time</h3>\n");
439 * Show the pathname we're centred on, with hyperlinks to
440 * parent directories where available.
442 htprintf(ctx
, "<p align=center>\n<code>");
444 for (p
= strchr(path
, pathsep
); p
&& p
[1]; p
= strchr(p
, pathsep
)) {
449 * See if this path prefix exists in the trie. If so,
450 * generate a hyperlink.
453 if (p
== path
) /* special case for "/" at start */
460 index2
= trie_before(t
, path
);
461 trie_getpath(t
, index2
, path2
);
462 if (!strcmp(path
, path2
) && cfg
->format
) {
463 snprintf(href
, hreflen
, cfg
->format
, index2
);
464 if (!*href
) /* special case that we understand */
466 htprintf(ctx
, "<a href=\"%s\">", href
);
470 htescape(ctx
, q
, zp
- q
, 1);
472 htprintf(ctx
, "</a>");
473 htescape(ctx
, zp
, p
- zp
, 1);
476 htescape(ctx
, q
, strlen(q
), 1);
477 htprintf(ctx
, "</code>\n");
480 * Decide on the age limit of our colour coding, establish the
481 * colour thresholds, and write out a key.
483 ctx
->now
= time(NULL
);
485 ctx
->oldest
= index_order_stat(t
, 0.05);
486 ctx
->newest
= index_order_stat(t
, 1.0);
487 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, -1);
488 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, +1);
490 ctx
->oldest
= cfg
->oldest
;
491 ctx
->newest
= cfg
->newest
;
492 ctx
->oldest
= round_and_format_age(ctx
, ctx
->oldest
, agebuf1
, 0);
493 ctx
->newest
= round_and_format_age(ctx
, ctx
->newest
, agebuf2
, 0);
495 for (i
= 0; i
< MAXCOLOUR
-1; i
++) {
497 ctx
->oldest
+ (ctx
->newest
- ctx
->oldest
) * i
/ MAXCOLOUR
;
499 htprintf(ctx
, "<p align=center>Key to colour coding (mouse over for more detail):\n");
500 htprintf(ctx
, "<p align=center style=\"padding: 0; margin-top:0.4em; "
501 "margin-bottom:1em\"");
502 begin_colour_bar(ctx
);
503 htprintf(ctx
, "<td style=\"padding-right:1em\">%s</td>\n", agebuf1
);
504 for (i
= 0; i
< MAXCOLOUR
; i
++)
505 add_to_colour_bar(ctx
, i
, 1);
506 htprintf(ctx
, "<td style=\"padding-left:1em\">%s</td>\n", agebuf2
);
510 * Begin the main table.
512 htprintf(ctx
, "<p align=center>\n<table style=\"margin:0; border:0\">\n");
515 * Find the total size of our entire subdirectory. We'll use
516 * that as the scale for all the colour bars in this report.
518 ctx
->totalsize
= fetch_size(t
, path
, ULLONG_MAX
);
521 * Generate a report line for the whole subdirectory.
524 vecs
= snewn(vecsize
, struct vector
*);
526 vecs
[0] = make_vector(ctx
, path
, 0, NULL
);
527 print_heading(ctx
, "Overall");
528 write_report_line(ctx
, vecs
[0]);
531 * Now generate report lines for all its children, and the
532 * files contained in it.
534 print_heading(ctx
, "Subdirectories");
536 vecs
[0]->name
= dupstr("[files]");
537 get_indices(t
, path
, &xi1
, &xi2
);
539 pathlen
= strlen(path
);
540 subdirpos
= pathlen
+ 1;
541 if (pathlen
> 0 && path
[pathlen
-1] == pathsep
)
544 trie_getpath(t
, xi1
, path2
);
545 get_indices(t
, ctx
->path2
, &xj1
, &xj2
);
548 continue; /* skip individual files */
549 if (nvecs
>= vecsize
) {
550 vecsize
= nvecs
* 3 / 2 + 64;
551 vecs
= sresize(vecs
, vecsize
, struct vector
*);
553 assert(strlen(path2
) > pathlen
);
554 vecs
[nvecs
] = make_vector(ctx
, path2
, 1, path2
+ subdirpos
);
555 for (i
= 0; i
<= MAXCOLOUR
; i
++)
556 vecs
[0]->sizes
[i
] -= vecs
[nvecs
]->sizes
[i
];
560 qsort(vecs
, nvecs
, sizeof(vecs
[0]), vec_compare
);
562 for (i
= 0; i
< nvecs
; i
++)
563 write_report_line(ctx
, vecs
[i
]);
566 * Close the main table.
568 htprintf(ctx
, "</table>\n");
571 * Finish up and tidy up.
573 htprintf(ctx
, "</body>\n");
574 htprintf(ctx
, "</html>\n");
578 for (i
= 0; i
< nvecs
; i
++) {
579 sfree(vecs
[i
]->name
);