html.c (3370B)
1 #include <sys/types.h> 2 #include <assert.h> 3 #include <err.h> 4 #include <stdarg.h> 5 #include <stdint.h> 6 #include <stdio.h> 7 #include <string.h> 8 #include <time.h> 9 #include <kcgi.h> 10 #include <kcgihtml.h> 11 12 #include "page.h" 13 14 enum kcgi_err 15 htmlwithin(struct pagedata *pd, enum kelem e, char *text) 16 { 17 enum kcgi_err status; 18 19 if ((status = khtml_elem(&pd->html, e)) != KCGI_OK || 20 (status = khtml_puts(&pd->html, text)) != KCGI_OK || 21 (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 22 return status; 23 return KCGI_OK; 24 } 25 26 enum kcgi_err 27 htmllink(struct pagedata *pd, char *link, char *text) 28 { 29 enum kcgi_err status; 30 31 if (strcmp(link, pd->pages[pd->req.page]) == 0) { 32 status = khtml_attr(&pd->html, KELEM_A, 33 KATTR_HREF, link, KATTR_CLASS, "active", KATTR__MAX); 34 } else { 35 status = khtml_attr(&pd->html, KELEM_A, 36 KATTR_HREF, link, KATTR__MAX); 37 } 38 39 if (status != KCGI_OK || 40 (status = khtml_puts(&pd->html, text)) != KCGI_OK || 41 (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 42 return status; 43 return KCGI_OK; 44 } 45 46 enum kcgi_err 47 htmlscript(struct pagedata *pd, char *src) 48 { 49 enum kcgi_err status; 50 51 if ((status = khtml_attr(&pd->html, KELEM_SCRIPT, 52 KATTR_SRC, src, KATTR__MAX)) != KCGI_OK || 53 (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 54 return status; 55 return KCGI_OK; 56 } 57 58 enum kcgi_err 59 htmldatetime(struct pagedata *pd, char *datetime, char *text) 60 { 61 enum kcgi_err status; 62 63 if (!text) 64 text = datetime; 65 else if (!datetime) 66 datetime = text; 67 68 assert(datetime && text); 69 70 if ((status = khtml_attr(&pd->html, KELEM_TIME, 71 KATTR_DATETIME, datetime, KATTR__MAX)) != KCGI_OK || 72 (status = khtml_puts(&pd->html, text)) != KCGI_OK || 73 (status = khtml_closeelem(&pd->html, 1))) 74 return status; 75 return KCGI_OK; 76 } 77 78 enum kcgi_err 79 htmldate(struct pagedata *pd, time_t time) 80 { 81 char buf[11]; 82 struct tm *tm; 83 84 tm = gmtime(&time); 85 if (!tm || strftime(buf, sizeof(buf), "%F", tm) >= sizeof(buf)) 86 return KCGI_SYSTEM; 87 return htmldatetime(pd, buf, NULL); 88 } 89 90 /* 91 * Not a pretty function, unfortunately kcgihtml(3) isn't built 92 * using arrays instead of variable arguments so I can't think of 93 * an elegant way of provding the same ability 94 */ 95 enum kcgi_err 96 htmlduration(struct pagedata *pd, time_t duration, char *id, int showseconds) 97 { 98 enum kcgi_err status; 99 time_t hr, mn, sc; 100 char datetime[13]; /* allows up to four digits for hours */ 101 102 assert(duration >= 0); 103 104 mn = duration / 60; 105 sc = duration % 60; 106 hr = mn / 60; 107 mn = mn % 60; 108 109 if (snprintf(datetime, sizeof(datetime), "%lldh%lldm%llds", 110 hr, mn, sc) >= (int)sizeof(datetime)) { 111 err(1, "snprintf (%lldh %lldm %llds)", hr, mn, sc); 112 } 113 114 if (id) 115 status = khtml_attr(&pd->html, KELEM_TIME, 116 KATTR_ID, id, KATTR_DATETIME, datetime, KATTR__MAX); 117 else 118 status = khtml_attr(&pd->html, KELEM_TIME, 119 KATTR_DATETIME, datetime, KATTR__MAX); 120 121 if (status != KCGI_OK) 122 return status; 123 124 if (showseconds) 125 status = khtml_printf(&pd->html, "%.2lld:%.2lld:%.2lld", hr, mn, sc); 126 else 127 status = khtml_printf(&pd->html, "%.2lld:%.2lld", hr, mn); 128 129 if (status != KCGI_OK || (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK) 130 return status; 131 return KCGI_OK; 132 }