1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
 * Copyright (C) 2000, Matias Atria
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>

#include "common.h"
#include "private.h"

static char *const messages[] = {
	_G("Ooops!"),
	_G("Aieeeee!!"),
	_G("Ouch!"),
	_G("Houston, we have a problem"),
	_G("3.. 2.. 1.. BOOM!"),
	_G("I'm history"),
	_G("I'm going down"),
	_G("I smell a rat")
};
#define NMSGS	(sizeof(messages) / sizeof(char *))

static FILE *logfile = NULL;
static int _mdvi_log_level;

int mdvi_set_logfile(const char *filename);
int mdvi_set_logstream(FILE *file);
int mdvi_set_loglevel(int level);

static void vputlog(int level, const char *head, const char *format, va_list ap)
{
	if(logfile != NULL && _mdvi_log_level >= level) {
		if(head != NULL)
			fprintf(logfile, "%s: ", head);
		vfprintf(logfile, format, ap);
	}
}

int mdvi_set_logfile(const char *filename)
{
	FILE	*f = NULL;

	if(filename && (f = fopen(filename, "w")) == NULL)
		return -1;
	if(logfile != NULL && !isatty(fileno(logfile))) {
		fclose(logfile);
		logfile = NULL;
	}
	if(filename)
		logfile = f;
	return 0;
}

int mdvi_set_logstream(FILE *file)
{
	if(logfile && !isatty(fileno(logfile))) {
		fclose(logfile);
		logfile = NULL;
	}
	logfile = file;
	return 0;
}

int mdvi_set_loglevel(int level)
{
	int	old = _mdvi_log_level;

	_mdvi_log_level = level;
	return old;
}

#ifndef NODEBUG
Uint32 _mdvi_debug_mask = 0;

void	__debug(int mask, const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	if(_mdvi_debug_mask & mask) {
		if(!DEBUGGING(SILENT)) {
			fprintf(stderr, "Debug: ");
			vfprintf(stderr, format, ap);
			fflush(stderr);
		}
#ifndef __GNUC__
		/* let's be portable */
		va_end(ap);
		va_start(ap, format);
#endif
		vputlog(LOG_DEBUG, "Debug", format, ap);
	}
	va_end(ap);
}
#endif

void	mdvi_message(const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	if(_mdvi_log_level >= LOG_INFO) {
		fprintf(stderr, "%s: ", program_name);
		vfprintf(stderr, format, ap);
#ifndef __GNUC__
		va_end(ap);
		va_start(ap, format);
#endif
	}
	vputlog(LOG_INFO, NULL, format, ap);
	va_end(ap);
}

void	mdvi_crash(const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	fprintf(stderr, "%s: %s: ",
		program_name,
		gettext(messages[(int)time(NULL) % NMSGS]));
	vfprintf(stderr, format, ap);
#ifndef __GNUC__
	/* let's be portable */
	va_end(ap);
	va_start(ap, format);
#endif
	vputlog(LOG_ERROR, _("Crashing"), format, ap);
	va_end(ap);
	abort();
}

void	mdvi_error(const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	fprintf(stderr, _("%s: Error: "), program_name);
	vfprintf(stderr, format, ap);
#ifndef __GNUC__
	/* let's be portable */
	va_end(ap);
	va_start(ap, format);
#endif
	vputlog(LOG_ERROR, _("Error"), format, ap);
	va_end(ap);
}

void	mdvi_warning(const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	fprintf(stderr, _("%s: Warning: "), program_name);
	vfprintf(stderr, format, ap);
#ifndef __GNUC__
	/* let's be portable */
	va_end(ap);
	va_start(ap, format);
#endif
	vputlog(LOG_WARN, _("Warning"), format, ap);
	va_end(ap);
}

void	mdvi_fatal(const char *format, ...)
{
	va_list	ap;

	va_start(ap, format);
	fprintf(stderr, _("%s: Fatal: "), program_name);
	vfprintf(stderr, format, ap);
#ifndef __GNUC__
	/* let's be portable */
	va_end(ap);
	va_start(ap, format);
#endif
	vputlog(LOG_ERROR, _("Fatal"), format, ap);
	va_end(ap);
#ifndef NODEBUG
	abort();
#else
	exit(EXIT_FAILURE);
#endif
}

void	*mdvi_malloc(size_t nelems)
{
	void	*ptr = malloc(nelems);

	if(ptr == NULL)
		mdvi_fatal(_("out of memory allocating %u bytes\n"),
			   (unsigned)nelems);
	return ptr;
}

void	*mdvi_realloc(void *data, size_t newsize)
{
	void	*ptr;

	if(newsize == 0)
		mdvi_crash(_("attempted to reallocate with zero size\n"));
	ptr = realloc(data, newsize);
	if(ptr == NULL)
		mdvi_fatal(_("failed to reallocate %u bytes\n"), (unsigned)newsize);
	return ptr;
}

void	*mdvi_calloc(size_t nmemb, size_t size)
{
	void	*ptr;

	if(nmemb == 0)
		mdvi_crash(_("attempted to callocate 0 members\n"));
	if(size == 0)
		mdvi_crash(_("attempted to callocate %u members with size 0\n"),
			(unsigned)nmemb);
	ptr = calloc(nmemb, size);
	if(ptr == 0)
		mdvi_fatal(_("failed to allocate %ux%u bytes\n"),
			   (unsigned)nmemb, (unsigned)size);
	return ptr;
}

void	mdvi_free(void *ptr)
{
	if(ptr == NULL)
		mdvi_crash(_("attempted to free NULL pointer\n"));
	free(ptr);
}

char	*mdvi_strdup(const char *string)
{
	int	length;
	char	*ptr;

	length = strlen(string) + 1;
	ptr = (char *)mdvi_malloc(length);
	memcpy(ptr, string, length);
	return ptr;
}

/* `to' should have room for length+1 bytes */
char	*mdvi_strncpy(char *to, const char *from, size_t length)
{
	strncpy(to, from, length);
	to[length] = '\0';
	return to;
}

char	*mdvi_strndup(const char *string, size_t length)
{
	int	n;
	char	*ptr;

	n = strlen(string);
	if(n > length)
		n = length;
	ptr = (char *)mdvi_malloc(n + 1);
	memcpy(ptr, string, n);
	return ptr;
}

void	*mdvi_memdup(const void *data, size_t length)
{
	void	*ptr = mdvi_malloc(length);

	memcpy(ptr, data, length);
	return ptr;
}

char   *mdvi_strrstr (const char *haystack, const char *needle)
{
	size_t i;
	size_t needle_len;
	size_t haystack_len;
	const char *p;

	needle_len = strlen (needle);
	haystack_len = strlen (haystack);

	if (needle_len == 0)
		return NULL;

	if (haystack_len < needle_len)
		return (char *)haystack;

	p = haystack + haystack_len - needle_len;
	while (p >= haystack) {
		for (i = 0; i < needle_len; i++)
			if (p[i] != needle[i])
				goto next;

		return (char *)p;

	next:
		p--;
	}

	return NULL;
}

char   *mdvi_build_path_from_cwd (const char *path)
{
	char  *ptr;
	char  *buf = NULL;
	size_t buf_size = 512;

	while (1) {
		buf = mdvi_realloc (buf, buf_size);
		if ((ptr = getcwd (buf, buf_size)) == NULL && errno == ERANGE) {
			buf_size *= 2;
		} else {
			buf = ptr;
			break;
		}
	}

	buf = mdvi_realloc (buf, strlen (buf) + strlen (path) + 2);
	strcat (buf, "/");
	strncat (buf, path, strlen (path));

	return buf;
}

double	unit2pix_factor(const char *spec)
{
	double	val;
	double	factor;
	const char *p, *q;
	static const char *units = "incmmmmtptpcddccspbpftydcs";

	val = 0.0;

	for(p = spec; *p >= '0' && *p <= '9'; p++)
		val = 10.0 * val + (double)(*p - '0');
	if(*p == '.') {
		p++;
		factor = 0.1;
		while(*p && *p >= '0' && *p <= '9') {
			val += (*p++ - '0') * factor;
			factor = factor * 0.1;
		}
	}
	factor = 1.0;
	for(q = units; *q; q += 2) {
		if(p[0] == q[0] && p[1] == q[1])
			break;
	}
	switch((int)(q - units)) {
	/*in*/	case 0:  factor = 1.0; break;
	/*cm*/	case 2:  factor = 1.0 / 2.54; break;
	/*mm*/	case 4:  factor = 1.0 / 25.4; break;
	/*mt*/	case 6:  factor = 1.0 / 0.0254; break;
	/*pt*/	case 8:  factor = 1.0 / 72.27; break;
	/*pc*/	case 10: factor = 12.0 / 72.27; break;
	/*dd*/	case 12: factor = (1238.0 / 1157.0) / 72.27; break;
	/*cc*/	case 14: factor = 12 * (1238.0 / 1157.0) / 72.27; break;
	/*sp*/	case 16: factor = 1.0 / (72.27 * 65536); break;
	/*bp*/	case 18: factor = 1.0 / 72.0; break;
	/*ft*/  case 20: factor = 12.0; break;
	/*yd*/  case 22: factor = 36.0; break;
	/*cs*/	case 24: factor = 1.0 / 72000.0; break;
		default: factor = 1.0;
	}
	return factor * val;
}

int	unit2pix(int dpi, const char *spec)
{
	double	factor = unit2pix_factor(spec);

	return (int)(factor * dpi + 0.5);
}

Ulong	get_mtime(int fd)
{
	struct stat st;

	if(fstat(fd, &st) == 0)
		return (Ulong)st.st_mtime;
	return 0;
}

char	*xstradd(char *dest, size_t *size, size_t n, const char *src, size_t m)
{
	if(m == 0)
		m = strlen(src);
	if(n + m >= *size) {
		dest = mdvi_realloc(dest, n + m + 1);
		*size = n + m + 1;
	}
	memcpy(dest + n, src, m);
	dest[n + m] = 0;
	return dest;
}

char	*getword(char *string, const char *delim, char **end)
{
	char *ptr;
	char *word;

	/* skip leading delimiters */
	for(ptr = string; *ptr && strchr(delim, *ptr); ptr++);

	if(*ptr == 0)
		return NULL;
	word = ptr++;
	/* skip non-delimiters */
	while(*ptr && !strchr(delim, *ptr))
		ptr++;
	*end = (char *)ptr;
	return word;
}

char	*getstring(char *string, const char *delim, char **end)
{
	char *ptr;
	char *word;
	int	quoted = 0;

	/* skip leading delimiters */
	for(ptr = string; *ptr && strchr(delim, *ptr); ptr++);

	if(ptr == NULL)
		return NULL;
	quoted = (*ptr == '"');
	if(quoted)
		for(word = ++ptr; *ptr && *ptr != '"'; ptr++);
	else
		for(word = ptr; *ptr && !strchr(delim, *ptr); ptr++);
	*end = (char *)ptr;
	return word;
}

static long pow2(size_t n)
{
	long	x = 8; /* don't bother allocating less than this */

	while(x < n)
		x <<= 1L;
	return x;
}

void	dstring_init(Dstring *dstr)
{
	dstr->data = NULL;
	dstr->size = 0;
	dstr->length = 0;
}

int	dstring_append(Dstring *dstr, const char *string, int len)
{
	if(len < 0)
		len = strlen(string);
	if(len) {
		if(dstr->length + len >= dstr->size) {
			dstr->size = pow2(dstr->length + len + 1);
			dstr->data = mdvi_realloc(dstr->data, dstr->size);
		}
		memcpy(dstr->data + dstr->length, string, len);
		dstr->length += len;
		dstr->data[dstr->length] = 0;
	} else if(dstr->size == 0) {
		ASSERT(dstr->data == NULL);
		dstr->size = 8;
		dstr->data = mdvi_malloc(8);
		dstr->data[0] = 0;
	}

	return dstr->length;
}

int	dstring_copy(Dstring *dstr, int pos, const char *string, int len)
{
	ASSERT(pos >= 0);
	if(len < 0)
		len = strlen(string);
	if(len) {
		if(pos + len >= dstr->length) {
			dstr->length = pos;
			return dstring_append(dstr, string, len);
		}
		memcpy(dstr->data + pos, string, len);
	}
	return dstr->length;
}

int	dstring_insert(Dstring *dstr, int pos, const char *string, int len)
{
	ASSERT(pos >= 0);
	if(pos == dstr->length)
		return dstring_append(dstr, string, len);
	if(len < 0)
		len = strlen(string);
	if(len) {
		if(dstr->length + len >= dstr->size) {
			dstr->size = pow2(dstr->length + len + 1);
			dstr->data = mdvi_realloc(dstr->data, dstr->size);
		}
		/* make room */
		memmove(dstr->data + pos, dstr->data + pos + len, len);
		/* now copy */
		memcpy(dstr->data + pos, string, len);
		dstr->length += len;
		dstr->data[dstr->length] = 0;
	}
	return dstr->length;
}

int	dstring_new(Dstring *dstr, const char *string, int len)
{
	if(len < 0)
		len = strlen(string);
	if(len) {
		dstr->size = pow2(len + 1);
		dstr->data = mdvi_malloc(dstr->size * len);
		memcpy(dstr->data, string, len);
	} else
		dstring_init(dstr);
	return dstr->length;
}

void	dstring_reset(Dstring *dstr)
{
	if(dstr->data)
		mdvi_free(dstr->data);
	dstring_init(dstr);
}