| 1 |
/* |
| 2 |
* misc.c: miscellaneous useful items |
| 3 |
*/ |
| 4 |
#include <string.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <time.h> |
| 7 |
#include "halibut.h" |
| 8 |
|
| 9 |
struct stackTag { |
| 10 |
void **data; |
| 11 |
int sp; |
| 12 |
int size; |
| 13 |
}; |
| 14 |
|
| 15 |
stack stk_new(void) |
| 16 |
{ |
| 17 |
stack s; |
| 18 |
|
| 19 |
s = mknew(struct stackTag); |
| 20 |
s->sp = 0; |
| 21 |
s->size = 0; |
| 22 |
s->data = NULL; |
| 23 |
|
| 24 |
return s; |
| 25 |
} |
| 26 |
|
| 27 |
void stk_free(stack s) |
| 28 |
{ |
| 29 |
sfree(s->data); |
| 30 |
sfree(s); |
| 31 |
} |
| 32 |
|
| 33 |
void stk_push(stack s, void *item) |
| 34 |
{ |
| 35 |
if (s->size <= s->sp) |
| 36 |
{ |
| 37 |
s->size = s->sp + 32; |
| 38 |
s->data = resize(s->data, s->size); |
| 39 |
} |
| 40 |
s->data[s->sp++] = item; |
| 41 |
} |
| 42 |
|
| 43 |
void *stk_pop(stack s) |
| 44 |
{ |
| 45 |
if (s->sp > 0) |
| 46 |
return s->data[--s->sp]; |
| 47 |
else |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Small routines to amalgamate a string from an input source. |
| 53 |
*/ |
| 54 |
const rdstring empty_rdstring = { 0, 0, NULL }; |
| 55 |
const rdstringc empty_rdstringc = { 0, 0, NULL }; |
| 56 |
|
| 57 |
void rdadd(rdstring * rs, wchar_t c) |
| 58 |
{ |
| 59 |
if (rs->pos >= rs->size - 1) |
| 60 |
{ |
| 61 |
rs->size = rs->pos + 128; |
| 62 |
rs->text = resize(rs->text, rs->size); |
| 63 |
} |
| 64 |
rs->text[rs->pos++] = c; |
| 65 |
rs->text[rs->pos] = 0; |
| 66 |
} |
| 67 |
|
| 68 |
void rdadds(rdstring * rs, wchar_t * p) |
| 69 |
{ |
| 70 |
int len = ustrlen(p); |
| 71 |
if (rs->pos >= rs->size - len) |
| 72 |
{ |
| 73 |
rs->size = rs->pos + len + 128; |
| 74 |
rs->text = resize(rs->text, rs->size); |
| 75 |
} |
| 76 |
ustrcpy(rs->text + rs->pos, p); |
| 77 |
rs->pos += len; |
| 78 |
} |
| 79 |
|
| 80 |
wchar_t *rdtrim(rdstring * rs) |
| 81 |
{ |
| 82 |
rs->text = resize(rs->text, rs->pos + 1); |
| 83 |
return rs->text; |
| 84 |
} |
| 85 |
|
| 86 |
void rdaddc(rdstringc * rs, char c) |
| 87 |
{ |
| 88 |
if (rs->pos >= rs->size - 1) |
| 89 |
{ |
| 90 |
rs->size = rs->pos + 128; |
| 91 |
rs->text = resize(rs->text, rs->size); |
| 92 |
} |
| 93 |
rs->text[rs->pos++] = c; |
| 94 |
rs->text[rs->pos] = 0; |
| 95 |
} |
| 96 |
|
| 97 |
void rdaddsc(rdstringc * rs, char *p) |
| 98 |
{ |
| 99 |
int len = strlen(p); |
| 100 |
if (rs->pos >= rs->size - len) |
| 101 |
{ |
| 102 |
rs->size = rs->pos + len + 128; |
| 103 |
rs->text = resize(rs->text, rs->size); |
| 104 |
} |
| 105 |
strcpy(rs->text + rs->pos, p); |
| 106 |
rs->pos += len; |
| 107 |
} |
| 108 |
|
| 109 |
char *rdtrimc(rdstringc * rs) |
| 110 |
{ |
| 111 |
rs->text = resize(rs->text, rs->pos + 1); |
| 112 |
return rs->text; |
| 113 |
} |
| 114 |
|
| 115 |
int compare_wordlists(word * a, word * b) |
| 116 |
{ |
| 117 |
int t; |
| 118 |
while (a && b) |
| 119 |
{ |
| 120 |
if (a->type != b->type) |
| 121 |
return (a->type < b->type ? -1 : +1); /* FIXME? */ |
| 122 |
t = a->type; |
| 123 |
if ((t != word_Normal && t != word_Code && |
| 124 |
t != word_WeakCode && t != word_Emph) || a->alt || b->alt) |
| 125 |
{ |
| 126 |
int c; |
| 127 |
if (a->text && b->text) |
| 128 |
{ |
| 129 |
c = ustricmp(a->text, b->text); |
| 130 |
if (c) |
| 131 |
return c; |
| 132 |
} |
| 133 |
c = compare_wordlists(a->alt, b->alt); |
| 134 |
if (c) |
| 135 |
return c; |
| 136 |
a = a->next; |
| 137 |
b = b->next; |
| 138 |
} else |
| 139 |
{ |
| 140 |
wchar_t *ap = a->text, *bp = b->text; |
| 141 |
while (*ap && *bp) |
| 142 |
{ |
| 143 |
wchar_t ac = utolower(*ap), bc = utolower(*bp); |
| 144 |
if (ac != bc) |
| 145 |
return (ac < bc ? -1 : +1); |
| 146 |
if (!*++ap && a->next && a->next->type == t && !a->next->alt) |
| 147 |
a = a->next, ap = a->text; |
| 148 |
if (!*++bp && b->next && b->next->type == t && !b->next->alt) |
| 149 |
b = b->next, bp = b->text; |
| 150 |
} |
| 151 |
if (*ap || *bp) |
| 152 |
return (*ap ? +1 : -1); |
| 153 |
a = a->next; |
| 154 |
b = b->next; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if (a || b) |
| 159 |
return (a ? +1 : -1); |
| 160 |
else |
| 161 |
return 0; |
| 162 |
} |
| 163 |
|
| 164 |
void mark_attr_ends(paragraph * sourceform) |
| 165 |
{ |
| 166 |
paragraph *p; |
| 167 |
word *w, *wp; |
| 168 |
for (p = sourceform; p; p = p->next) |
| 169 |
{ |
| 170 |
wp = NULL; |
| 171 |
for (w = p->words; w; w = w->next) |
| 172 |
{ |
| 173 |
if (isattr(w->type)) |
| 174 |
{ |
| 175 |
int before = (wp && isattr(wp->type) && |
| 176 |
sameattr(wp->type, w->type)); |
| 177 |
int after = (w->next && isattr(w->next->type) && |
| 178 |
sameattr(w->next->type, w->type)); |
| 179 |
w->aux |= (before ? |
| 180 |
(after ? attr_Always : attr_Last) : |
| 181 |
(after ? attr_First : attr_Only)); |
| 182 |
} |
| 183 |
wp = w; |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
wrappedline *wrap_para(word * text, int width, int subsequentwidth, |
| 189 |
int (*widthfn) (word *)) |
| 190 |
{ |
| 191 |
wrappedline *head = NULL, **ptr = &head; |
| 192 |
int nwords, wordsize; |
| 193 |
struct wrapword { |
| 194 |
word *begin, *end; |
| 195 |
int width; |
| 196 |
int spacewidth; |
| 197 |
int cost; |
| 198 |
int nwords; |
| 199 |
} *wrapwords; |
| 200 |
int i, j, n; |
| 201 |
|
| 202 |
/* |
| 203 |
* Break the line up into wrappable components. |
| 204 |
*/ |
| 205 |
nwords = wordsize = 0; |
| 206 |
wrapwords = NULL; |
| 207 |
while (text) |
| 208 |
{ |
| 209 |
if (nwords >= wordsize) |
| 210 |
{ |
| 211 |
wordsize = nwords + 64; |
| 212 |
wrapwords = srealloc(wrapwords, wordsize * sizeof(*wrapwords)); |
| 213 |
} |
| 214 |
wrapwords[nwords].width = 0; |
| 215 |
wrapwords[nwords].begin = text; |
| 216 |
while (text) |
| 217 |
{ |
| 218 |
wrapwords[nwords].width += widthfn(text); |
| 219 |
wrapwords[nwords].end = text->next; |
| 220 |
if (text->next && (text->next->type == word_WhiteSpace || |
| 221 |
text->next->type == word_EmphSpace || |
| 222 |
text->breaks)) |
| 223 |
break; |
| 224 |
text = text->next; |
| 225 |
} |
| 226 |
if (text && text->next && (text->next->type == word_WhiteSpace || |
| 227 |
text->next->type == word_EmphSpace)) |
| 228 |
{ |
| 229 |
wrapwords[nwords].spacewidth = widthfn(text->next); |
| 230 |
text = text->next; |
| 231 |
} else |
| 232 |
{ |
| 233 |
wrapwords[nwords].spacewidth = 0; |
| 234 |
} |
| 235 |
nwords++; |
| 236 |
if (text) |
| 237 |
text = text->next; |
| 238 |
} |
| 239 |
|
| 240 |
/* |
| 241 |
* Perform the dynamic wrapping algorithm: work backwards from |
| 242 |
* nwords-1, determining the optimal wrapping for each terminal |
| 243 |
* subsequence of the paragraph. |
| 244 |
*/ |
| 245 |
for (i = nwords; i--;) |
| 246 |
{ |
| 247 |
int best = -1; |
| 248 |
int bestcost = 0; |
| 249 |
int cost; |
| 250 |
int linelen = 0, spacewidth = 0; |
| 251 |
int seenspace; |
| 252 |
int thiswidth = (i == 0 ? width : subsequentwidth); |
| 253 |
|
| 254 |
j = 0; |
| 255 |
seenspace = 0; |
| 256 |
while (i + j < nwords) |
| 257 |
{ |
| 258 |
/* |
| 259 |
* See what happens if we put j+1 words on this line. |
| 260 |
*/ |
| 261 |
if (spacewidth) |
| 262 |
seenspace = 1; |
| 263 |
linelen += spacewidth + wrapwords[i + j].width; |
| 264 |
spacewidth = wrapwords[i + j].spacewidth; |
| 265 |
j++; |
| 266 |
if (linelen > thiswidth) |
| 267 |
{ |
| 268 |
/* |
| 269 |
* If we're over the width limit, abandon ship, |
| 270 |
* _unless_ there is no best-effort yet (which will |
| 271 |
* only happen if the first word is too long all by |
| 272 |
* itself). |
| 273 |
*/ |
| 274 |
if (best > 0) |
| 275 |
break; |
| 276 |
} |
| 277 |
if (i + j == nwords) |
| 278 |
{ |
| 279 |
/* |
| 280 |
* Special case: if we're at the very end of the |
| 281 |
* paragraph, we don't score penalty points for the |
| 282 |
* white space left on the line. |
| 283 |
*/ |
| 284 |
cost = 0; |
| 285 |
} else |
| 286 |
{ |
| 287 |
cost = (thiswidth - linelen) * (thiswidth - linelen); |
| 288 |
cost += wrapwords[i + j].cost; |
| 289 |
} |
| 290 |
/* |
| 291 |
* We compare bestcost >= cost, not bestcost > cost, |
| 292 |
* because in cases where the costs are identical we |
| 293 |
* want to try to look like the greedy algorithm, |
| 294 |
* because readers are likely to have spent a lot of |
| 295 |
* time looking at greedy-wrapped paragraphs and |
| 296 |
* there's no point violating the Principle of Least |
| 297 |
* Surprise if it doesn't actually gain anything. |
| 298 |
*/ |
| 299 |
if (best < 0 || bestcost >= cost) |
| 300 |
{ |
| 301 |
bestcost = cost; |
| 302 |
best = j; |
| 303 |
} |
| 304 |
} |
| 305 |
/* |
| 306 |
* Now we know the optimal answer for this terminal |
| 307 |
* subsequence, so put it in wrapwords. |
| 308 |
*/ |
| 309 |
wrapwords[i].cost = bestcost; |
| 310 |
wrapwords[i].nwords = best; |
| 311 |
} |
| 312 |
|
| 313 |
/* |
| 314 |
* We've wrapped the paragraph. Now build the output |
| 315 |
* `wrappedline' list. |
| 316 |
*/ |
| 317 |
i = 0; |
| 318 |
while (i < nwords) |
| 319 |
{ |
| 320 |
wrappedline *w = mknew(wrappedline); |
| 321 |
*ptr = w; |
| 322 |
ptr = &w->next; |
| 323 |
w->next = NULL; |
| 324 |
|
| 325 |
n = wrapwords[i].nwords; |
| 326 |
w->begin = wrapwords[i].begin; |
| 327 |
w->end = wrapwords[i + n - 1].end; |
| 328 |
|
| 329 |
/* |
| 330 |
* Count along the words to find nspaces and shortfall. |
| 331 |
*/ |
| 332 |
w->nspaces = 0; |
| 333 |
w->shortfall = width; |
| 334 |
for (j = 0; j < n; j++) |
| 335 |
{ |
| 336 |
w->shortfall -= wrapwords[i + j].width; |
| 337 |
if (j < n - 1 && wrapwords[i + j].spacewidth) |
| 338 |
{ |
| 339 |
w->nspaces++; |
| 340 |
w->shortfall -= wrapwords[i + j].spacewidth; |
| 341 |
} |
| 342 |
} |
| 343 |
i += n; |
| 344 |
} |
| 345 |
|
| 346 |
sfree(wrapwords); |
| 347 |
|
| 348 |
return head; |
| 349 |
} |
| 350 |
|
| 351 |
void wrap_free(wrappedline * w) |
| 352 |
{ |
| 353 |
while (w) |
| 354 |
{ |
| 355 |
wrappedline *t = w->next; |
| 356 |
sfree(w); |
| 357 |
w = t; |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
unsigned long getutcunixtime() |
| 362 |
{ |
| 363 |
#ifndef _WIN32 |
| 364 |
struct timespec ts; |
| 365 |
ts.tv_sec = 0; |
| 366 |
/* gettimeofday()? */ |
| 367 |
#if (_XOPEN_SOURCE >= 500 || _POSIX_C_SOURCE >= 199309L) |
| 368 |
if (0 == clock_gettime(CLOCK_REALTIME, &ts)) |
| 369 |
return ts.tv_sec; |
| 370 |
#endif |
| 371 |
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(TIME_UTC) |
| 372 |
if (timespec_get(&ts, TIME_UTC)) /* implementation defined epoch :( */ |
| 373 |
return ts.tv_sec; |
| 374 |
#endif |
| 375 |
#endif /*~ _WIN32 */ |
| 376 |
return (unsigned long) time(NULL); |
| 377 |
} |
| 378 |
|
| 379 |
/* |
| 380 |
* Wrapper around the standard C time() function, which allows its |
| 381 |
* return value to be overridden by the environment variable |
| 382 |
* SOURCE_DATE_EPOCH, used to achieve reproducible builds by avoiding |
| 383 |
* baking different datestamps into repetitions of what ought to be |
| 384 |
* the same build. |
| 385 |
*/ |
| 386 |
time_t current_time(void) |
| 387 |
{ |
| 388 |
const char *epoch = getenv("SOURCE_DATE_EPOCH"); |
| 389 |
if (epoch) |
| 390 |
return atol(epoch); |
| 391 |
return time(NULL); |
| 392 |
} |