summaryrefslogtreecommitdiff
path: root/Build/source/texk/xdvik/string-utils.c
blob: def6e27902462411717ea79a0c06c1fc7f121ba9 (plain)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 * miscellaneous string utility functions
 * 
 * Copyright (c) 2001-2022 the xdvik development team
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*  #define MYDEBUG 1 */

#include "xdvi-config.h"
#include "xdvi.h"
#include "string-utils.h"
#include "util.h"

#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <ctype.h>

/*------------------------------------------------------------
 *  str_is_prefix
 *
 *  Purpose:
 *	Return True if <str1> is a prefix of <str2>, else False.
 *
 *	If `case_sensitive' is set to False, it performs matching
 *	in a case-insensitive manner; in that case, str1 should
 *	be lowercase.
 *------------------------------------------------------------*/

Boolean
str_is_prefix(const char *str1, const char *str2, Boolean case_sensitive)
{
    int i;

    for (i = 0; *str1 != '\0' && *str2 != '\0'; i++) {
	if ((case_sensitive && *str1 != *str2) ||
	    (!case_sensitive && *str1 != tolower((int)*str2)))
	    return False;
	str1++;
	str2++;
    }
    return *str1 == '\0';
}



/*------------------------------------------------------------
 *  str_is_suffix
 *
 *  Purpose:
 *	Return True if str1 is a suffix of str2, else False.
 *	E.g. returns true if str1 = ".tex", str2 = "test.tex".
 *
 *	If `case_sensitive' is set to False, it performs matching
 *	in a case-insensitive manner; in that case, str1 should
 *	be lowercase.
 *------------------------------------------------------------*/

Boolean
str_is_suffix(const char *str1, const char *str2, Boolean case_sensitive)
{
    int len1 = strlen(str1);
    int len2 = strlen(str2);

    while (len2 > len1) {
	str2++;
	len2--;
    }
    if (case_sensitive)
	return strcmp(str1, str2) == 0;
    else
	/* also compare terminating 0; second string
	   is assumed to be all-lowercase! */
	return memicmp(str2, str1, len1 + 1) == 0;
}

/*
 * Like strstr(), but does case-insensitive matching: Brute-force algorithm
 * to find the first occurrence of subsrtring `needle' (which should be all-lowercase)
 * in string `haystack', ignoring case in (i.e. lowercasing) haystack. The terminating
 * '\0' characters are not compared.
 * Returns a pointer to the beginning of the substring if found, NULL else.
 *
 * This code was derived from public domain code (Stristr.c on www.snippets.org).
 * Currently unused.
 */
char *
my_stristr(const char *haystack, const char *needle)
{
    const char *curr;

    for (curr = haystack; *curr != '\0'; curr++) {
	const char *ptr1, *ptr2;
	/* search for first character */
	for (; ((*curr != '\0') && (tolower((int)*curr) != *needle)); curr++) { ; }

	if (*curr == '\0') /* not found */
	    return NULL;

	/* now compare other characters */
	ptr1 = needle;
	ptr2 = curr;
	while (tolower((int)*ptr2) == *ptr1) {
	    ptr2++;
	    ptr1++;
	    /* success if at end of needle */
	    if (*ptr1 == '\0')
		return (char *)curr;
	}
    }
    return NULL;
}

/*
  expand filename to include full path name;
  returns result in a freshly allocated string.
*/
char *
expand_filename(const char *filename, expandPathTypeT type)
{
    char *path_name = NULL;

    if (*filename == '/') /* already an absolute path */
	return xstrdup(filename);
    
    if (type == USE_CWD_PATH) {
	size_t path_name_len = 512;
	size_t len = strlen(filename) + 1;

	/* append to cwd if it's not already a full path */
	if (filename[0] != '/') {
	    for (;;) {
		char *tmp;
		path_name = xrealloc(path_name, path_name_len);
		if ((tmp = getcwd(path_name, path_name_len)) == NULL && errno == ERANGE) {
		    path_name_len *= 2;
		}
		else {
		    path_name = tmp;
		    break;
		}
	    }
	    len += strlen(path_name) + 1;	/* 1 for '/' */
	    path_name = xrealloc(path_name, len);
	    strcat(path_name, "/");
	    strcat(path_name, filename);
	}
	
	TRACE_HTEX((stderr,
		    "Expanding filename |%s| with CWD gives |%s|",
		    filename, path_name == NULL ? "<NULL>" : path_name));
	return path_name ? path_name : xstrdup(filename);
    }
    else {
	ASSERT(globals.dvi_file.dirname != NULL, "globals.dvi_file.dirname should have been initialized before");
	path_name = xstrdup(globals.dvi_file.dirname);
	path_name = xstrcat(path_name, filename);
	TRACE_HTEX((stderr,
		    "Expanding filename |%s| with globals.dvi_file.dirname |%s| gives |%s|",
		    filename, globals.dvi_file.dirname, path_name));
	return path_name;
    }
}

/* expand filename to include `.dvi' extension and full path name;
   returns malloc()ed string (caller is responsible for free()ing).
*/
char *
filename_append_dvi(const char *filename)
{
    char *expanded_filename = NULL;
    const char *orig_filename = filename;
    size_t len;
    char *p;
    
    /* skip over `file:' prefix if present */
    if (str_is_prefix("file:", filename, True)) {
	filename += strlen("file:");
	if (str_is_prefix("//", filename, True)) { /* is there a hostname following? */
	    char *tmp = strchr(filename+2, '/'); /* skip over host name */
	    if (tmp == NULL) {
		XDVI_WARNING((stderr, "Malformed hostname part in filename `%s'; not expanding file name",
			      orig_filename));
	    }
	    else {
		/* deal with multiple `//' after "file://localhost";
		   while the RFC seems to suggest that file://localhost/foo/bar defines a path
		   `foo/bar', most browsers (and actually, also libwww) will treat this path as absolute:
		   `/foo/bar'. So we treat
		   file://localhost//foo/bar
		   and
		   file://localhost/foo/bar
		   as equivalent.
		*/
		while (*(tmp+1) == '/') 
		    tmp++;
		filename = tmp;
	    }
	}
    }

    len = strlen(filename) + 5; /* 5 in case we need to add `.dvi\0' */

    expanded_filename = xmalloc(len);

    strcpy(expanded_filename, filename);

    /* Append ".dvi" extension if no extension is present.
       Only look at the filename part when trying to find a `.'.
    */
    if ((p = strrchr(expanded_filename, '/')) == NULL) {
	p = expanded_filename;
    }
    if ((p = strrchr(p, '.')) == NULL) {
	TRACE_HTEX((stderr, "appending .dvi extension to filename |%s|", expanded_filename));
	strcat(expanded_filename, ".dvi");
    }
    return expanded_filename;
}

char *
expand_filename_append_dvi(const char *filename, expandPathTypeT type, Boolean must_exist)
{
    char canonical_path[MAXPATHLEN + 1];
    char *normalized_fname = filename_append_dvi(filename);
    char *expanded_fname = expand_filename(normalized_fname, type);
    if (must_exist) {
	char *canonical_name = REALPATH(expanded_fname, canonical_path);
	free(normalized_fname);
	free(expanded_fname);
	return xstrdup(canonical_name);
    }
    else {
	free(normalized_fname);
	return expanded_fname;
    }
}

char *
format_arg(const char *fmt, const char *arg, int *match)
{
    char *tmp = xmalloc(strlen(fmt) + strlen(arg) + 1);
    if (strchr(fmt, '%') != NULL) {
	sprintf(tmp, fmt, arg);
	*match = 1;
    }
    else {
	strcpy(tmp, fmt);
	/* NOTE: don't reset *match to 0, leave that to caller */
    }
    return tmp;
}

/* escape single `%' characters in arg and return newly allocated string */
char *
escape_format_arg(const char *arg)
{
    char *ret, *ptr;
    ASSERT(arg != NULL, "");
    ret = xmalloc(strlen(arg) * 2 + 1); /* more than enuff */
    
    ptr = ret;
    while (*arg != '\0') {
	/* need to escape? */
	if (*arg == '%') { /* && (ptr == ret
			    || (ptr > ret && *(arg - 1) != '%'))) { */
	    *ptr++ = '%';
	}
	*ptr++ = *arg++;
    }
    *ptr = '\0';

    /* trim return buffer */
    return xrealloc(ret, strlen(ret) + 1);
}

char *
unquote_arg(const char *fmt, const char *arg, int *match, int *len)
{
    char *ptr;
    char c;
    
    c = fmt[0];
    fmt++;
    if ((ptr = strchr(fmt, c)) != NULL) {
	*ptr++ = '\0';
	while (*ptr == ' ' || *ptr == '\t') {
	    ptr++;
	}
	*len = ptr - fmt;
	return format_arg(fmt, arg, match);
    }
    else {
	*len = strlen(fmt);
	XDVI_WARNING((stderr, "Ignoring lonesome quote in string %s", fmt - 1));
	return format_arg(fmt, arg, match);
    }
}

/* Chop `source' into chunks separated by `sep', and save these
 * to newly allocated return list. The end of the list is indicated
 * by a NULL element (i.e. the returned list will always contain at
 * least 1 element). The caller is responsible for free()ing the returned
 * list.
 *
 * If `do_unquote' is True, separators inside single or double quotation marks will not be
 * treated as boundaries. The quotation marks surrounding the chunk will be removed
 * as well in that case.
 */
char **
get_separated_list(const char *source, const char *sep, Boolean do_unquote)
{
    /* allocate at least list of size 1, for NULL termination below */
    char **chunks = xmalloc(sizeof *chunks);
    const char *b_ptr, *e_ptr;
    size_t i = 0;
    
    b_ptr = source;

    while (*b_ptr != '\0' && strchr(sep, *b_ptr) != NULL)
	b_ptr++;
    
    for (i = 0; *b_ptr != '\0'; i++) {
	char *quote;
	char quotechar = 0;
	size_t len, chunk_len, alloc_len = 0;
	
	if ((len = strcspn(b_ptr, sep)) == 0) /* at end */
	    break;

	/* check for quoted chunk, in which case we don't want to treat
	   spaces as chunk separators */
	if (do_unquote && (quote = strchr("'\"", *b_ptr)) != NULL) {
	    const char *curr = b_ptr + 1;
	    quotechar = *quote;

	    for (;;) { /* find end of quote */
		char *maybe_end = strchr(curr, quotechar);
		if (maybe_end != NULL) {
		    if (maybe_end - curr > 0 &&
			*(maybe_end - 1) == '\\') { /* escaped quote */
			curr = ++maybe_end;
		    }
		    else { /* found */
			b_ptr++;
			len = maybe_end - b_ptr;
			break;
		    }
		}
		else { /* not found end - warn, and forget about this quote */
		    XDVI_WARNING((stderr, "Unmatched quote character in string `%s'", b_ptr));
		    break;
		}
	    }
	}
	
	e_ptr = b_ptr + len;
	chunk_len = e_ptr - b_ptr;
	while (i + 1 >= alloc_len) {
	    alloc_len++;
	}
	chunks = xrealloc(chunks, alloc_len * sizeof *chunks);
	chunks[i] = xmalloc(chunk_len + 1);
	memcpy(chunks[i], b_ptr, chunk_len);
	chunks[i][chunk_len] = '\0';

	/* skip trailing quotechar and spaces */
	b_ptr = e_ptr;
	if (do_unquote && quotechar != 0 && *b_ptr == quotechar)
	    b_ptr++;
	while (*b_ptr != '\0' && strchr(sep, *b_ptr) != NULL)
	    b_ptr++;
    }
    /* terminate list with NULL element */
    chunks[i] = NULL;
    return chunks;
}

const char *
find_format_str(const char *input, const char *fmt)
{
    const char *ptr = input;
    while ((ptr = strstr(ptr, fmt)) != NULL) {
	if (ptr > input && *(ptr - 1) == '\\') {
	    ptr++;
	    continue;
	}
	else
	    return ptr;
    }
    return NULL;
}

/* return directory component of `path', or NULL if path is a filename only */
char *
get_dir_component(const char *path)
{
    char *ret = NULL;
    char *p;
    
    if ((p = strrchr(path, '/')) != NULL) {
	/* copy, chop off filename (but not the '/') */
	ret = xstrdup(path);
	*(ret + (p - path) + 1) = '\0';
	TRACE_CLIENT((stderr, "get_dir_component of |%s| is |%s|\n", path, ret));
    }
    return ret;
}

/*
  If `src' is an absolute path, compare it with `target', ignoring `.tex' extension in
  both strings. Else, compare the concatenation of `src' with `dvi_path' and `target',
  in the same way.
  Since efficiency is a concern here, we don't copy any strings; instead, we loop through
  `src' and `target' from the end of both strings (which makes expanding `../' easier, and
  will terminate earlier for non-equal files), replacing `src' with `dvi_path' when
  dropping off the beginning of `src'.
*/
Boolean
src_compare(const char *src, int src_len, const char *target, const char *dvi_path, size_t dvi_path_len)
{
    int i, j;
    Boolean matched = True;

    /* This macro sets the `src' pointer to `dvi_path' after having
       dropped off the beginning of src, or returns False if dvi_path
       is NULL (which either means that `src' was an absolute path, or
       that the dvi_path has been used up already).
    */
#define CHK_USE_DIR(i)								\
	if (i == -1) {								\
	    if (dvi_path == NULL) /* already done */				\
		return False;							\
	    src = dvi_path;							\
	    dvi_path = NULL;							\
	    i = dvi_path_len - 1;						\
	    MYTRACE((stderr, "swapped, now using: |%s| of len %d", src, i));	\
	}

    /* ignore path in both filenames if one of them does not contain a path */
    {
	char *src_p = strrchr(src, '/');
	char *target_p = strrchr(target, '/');

	if (src_p == NULL) {
	    dvi_path = NULL;
	    if (target_p != NULL)
		target = target_p + 1;
	}

	if (target_p == NULL) {
	    dvi_path = NULL;
	    if (src_p != NULL)
		src = src_p + 1;
	}
    }

    /* don't prepend dvi_path if src is absolute */
    if (*src == '/') { 
	dvi_path = NULL;
    }
    
    /* skip `.tex' suffix in strings if present */
    i = src_len;
    MYTRACE((stderr, "len of |%s| %d", src, i));
    if (i >= 4 && strcmp(src + (i - 4), ".tex") == 0) {
	MYTRACE((stderr, "src |%s| has .tex suffix!", src));
	i -= 4;
    }

    j = strlen(target);
    MYTRACE((stderr, "len of |%s| %d", target, j));
    if (j >= 4 && strcmp(target + (j - 4), ".tex") == 0) {
	MYTRACE((stderr, "target |%s| has .tex suffix!", target));
	j -= 4;
    }

    /* start with index of last char */
    i--;
    j--;

    while (i >= 0 && j >= 0) {
	int skip_dirs = 0;
	/*  	fprintf(stderr, "check: %d[%c]\n", i, src[i]); */
	while (src[i] == '.' && src[i + 1] == '/') { /* normalize `../' and `./' */
	    MYTRACE((stderr, "check2: %d[%c]", i, src[i]));

	    if (i >= 2 && src[i - 1] == '.' && src[i - 2] == '/') {
		MYTRACE((stderr, "case /.."));
		i -= 3;
		skip_dirs++;
	    }
	    else if (i == 1) { /* `../' or `/./' at start of src */
		if (src[0] == '.') { /* `../' */
		    MYTRACE((stderr, "../ at start"));
		    i -= 2;
		    skip_dirs++;
		}
		else if (src[0] == '/') { /* `/./' */
		    MYTRACE((stderr, "/./ at start"));
		    i -= 1;
		}
		else /* something else */
		    break;
	    }
	    else if (i == 0 || (i > 1 && src[i - 1] == '/')) { /* ./ at start, or /./ somewhere */
		i -= 1;
	    }
	    else /* might be `abc./' or `abc../' (strange but legal) */
		break;

	    CHK_USE_DIR(i);
	    while (i >= 0 && src[i] == '/') i--;
	    CHK_USE_DIR(i);
	    
	    while (src[i] != '.' && skip_dirs-- > 0) { /* unless there are subsequent `../' */
		/* skip directories backwards */
		while (i >= 0 && src[i] != '/') {
		    MYTRACE((stderr, "non-slash: %d,%c", i, src[i]));
		    i--;
		}
		CHK_USE_DIR(i);
		while (i >= 0 && src[i] == '/') {
		    MYTRACE((stderr, "slash: %d,%c", i, src[i]));
		    i--;
		}
		CHK_USE_DIR(i);
		MYTRACE((stderr, "skipped backwards: %d,%c", i, src[i]));
	    }
	}

	/* skip multiple '/'. No need to fall off the beginning of src and use
	   CHK_USE_DIR() here, since with (multiple) '/' at the beginning, src
	   must be an absolute path. */
	while (i > 0 && src[i] == '/' && src[i - 1] == '/') {
	    i--;
	}
	
	MYTRACE((stderr, "comparing: %d[%c] - %d[%c]", i, src[i], j, target[j]));
	if (src[i] != target[j]) {
	    matched = False;
	    break;
	}
	if (i == 0 && j == 0) /* at start of both strings */
	    break;
	i--;
	j--;
	CHK_USE_DIR(i);
    }

    if (i != 0 || j != 0) /* not at start of both strings, no match */
	return False;
    else
	return matched;
#undef CHK_USE_DIR
}


/*
  Contributed by ZLB: Return a canonicalized version of path (with `../'
  and `./' resolved and `//' reduced to '/') in a freshly malloc()ed
  buffer, which the caller is responsible for free()ing. Cannot fail.
*/
char *
canonicalize_path(const char *path)
{
    char *p, *q, *start;
    char c;
    size_t len = strlen(path);

    assert(path != NULL);
    assert(*path == '/');

    start = q = p = xstrdup(path);

    /* len is the length of string */
    while (p < start + len) {
	if ((c = p[1]) == '/') {
	    /* remove multiple '/' in pathname */
	    memmove(p + 1, p + 2, len - (p + 2 - start) + 1);
	    len--;
	    continue;
	}
	else if (c == '.') {
	    if ((c = p[2]) == '/') {
		/* p = '/.' in pathname */
		memmove(p + 1, p + 3, len - (p + 3 - start) + 1);
		len -= 2;
		continue;
	    }
	    else if (c == '.' && ((c = p[3]) == '/' || c == '\0')) {
		/* p == "/.." */
		memmove(q, p + 3, len - (p + 3 - start) + 1);
		len -= (p - q) + 3;
		p = q;
		/* check if the new dirname at p is "//" or './' or '../' */
		if ((c = p[1]) == '/')
		    continue;
		else if (c == '.') {
		    if ((c = p[2]) == '/')
			continue;
		    else if (c == '.' && ((c = p[3]) == '/' || c == '\0')) {
			while (--q >= start && *q != '/');
			if (q < start)
			    q = start;
			continue;
		    }
		}
	    }
	}

	/* search next '/' */
	q = p;
	while (++p <= start + len && *p != '/');
    }

    return start;
}

/* Escape shell metacharacters in str, hopefully making it safe to pass
   str to system(), i.e., /bin/sh -c, without further quoting. Return
   result in a newly allocated string, which the caller is responsible
   to free() after use.
*/
char *
shell_escape_string(const char *str)
{
    size_t len = strlen(str);
    char *new_str = xmalloc(len * 2 + 1); /* safe amount, since each char will be doubled at most */
    
    const char *src_ptr = str;
    char *target_ptr = new_str;
    while (*src_ptr != '\0') {
	if (*src_ptr == '\\'
	    || *src_ptr == '`'
	    || *src_ptr == '\''
	    || *src_ptr == '"'
	    || *src_ptr == '(' || *src_ptr == ')'
	    || *src_ptr == '{' || *src_ptr == '}'
	    || *src_ptr == '[' || *src_ptr == ']'
	    || *src_ptr == '<' || *src_ptr == '>'
	    || *src_ptr == '&'
	    || *src_ptr == '|'
	    || *src_ptr == '!'
	    || *src_ptr == '$'
	    || *src_ptr == '*'
	    || *src_ptr == '?'
	    || *src_ptr == ';'
	    || *src_ptr == ' ' || *src_ptr == '\t' || *src_ptr == '\n') {
#if 0
	    /* only if not yet escaped? */
 	    && (src_ptr == str || (src_ptr > str && *(src_ptr - 1) != '\\'))) {
#endif
	    *target_ptr++ = '\\';
	}
	*target_ptr++ = *src_ptr++;
    }
    *target_ptr = '\0'; /* terminate */
    return new_str;
}

/* Get a pointer to the extension of the filename 'fname'
   (ie. into the existing string),
   or NULL if fname doens't have an extension.
 */
const char *
get_extension(const char *fname)
{
    char *sep, *tmp;
    /* does filename have a directory component?
       If so, be careful with dots within this component.
    */
    if ((sep = strrchr(fname, '/')) != NULL) {
	tmp = sep;
	if ((sep = strrchr(tmp, '.')) != NULL) {
	    return sep;
	}
	else {
	    return NULL;
	}
    }
    else if ((sep = strrchr(fname, '.')) != NULL) {
	return sep;
    }
    else {
	return NULL;
    }
}

void
replace_extension(const char *fname, const char *extension,
		  char *buf, size_t buf_len)
{
    char *sep;
    if ((sep = strrchr(fname, '.')) != NULL) {
	size_t len = strlen(extension);
	if (len + (sep - fname) > buf_len)
	    return;
	memcpy(buf, fname, sep - fname);
	strcpy(buf + (sep - fname), extension);
    }
    return;
}

#if 0
/*
 * Estimate the string length needed for %p conversion. Currently unused,
 * since we use the more general VSNPRINTF() approach.
 */
#define PTR_CONVERSION_LEN_GUESS sizeof(void *) * CHAR_BIT
#endif

   
/*
 * Return a formatted string in newly allocated memory.
 */
char *
get_string_va(const char *fmt, ...)
{
    char *buf = NULL;
    XDVI_GET_STRING_ARGP(buf, fmt);
    return buf;
}

/* Wrapper for atof() for strtod()-like error checking,
 * with an XDVI_WARNING if the conversion of <str> wasn't complete.
 */
double
my_atof(const char *str)
{
    char *ptr;
    double f;
    
    f = strtod(str, (char **)&ptr);
    if (*ptr != '\0') {
	XDVI_WARNING((stderr, "strtod: incomplete conversion of %s to %f", str, f));
    }
    return f;
}

/* return length of a string representation of the integer n */
int
length_of_int(int n)
{
    int ret = 1;

    if (n < 0) {
	ret++;
	n *= -1;
    }
    while (n >= 10) {
	n /= 10;
	ret++;
    }

    return ret;
}

Boolean
is_spaces_only(const char *ptr)
{
    for (; *ptr; ptr++) {
	if (!isspace((int)*ptr))
	    return False;
    }
    return True;
}