Debian Codegraph @

xpa/word.c

raw symbols SCIP
1 /*
2 * Copyright (c) 1999-2003 Smithsonian Astrophysical Observatory
3 */
4
5 /*
6 * word.c -- token parser, pattern matcher, macro expander,
7 * and other word-related routines
8 *
9 */
10
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include <word.h>
15
16 /* SAOMOD_CTYPE -- work around Slackware/RedHat incompatibility */
17 #ifdef linux
18 #ifdef isalnum
19 #undef isalnum
20 #define isalnum(c) (isalpha(c)||isdigit(c))
21 #endif
22 #endif
23
24 /* **************************************************************************
25 *
26 *
27 * PRIVATE ROUTINES AND DATA
28 *
29 *
30 * **************************************************************************/
31
32 /* word */
33 #define MAXDELIM 256
34 #define MAXDTABLES 1024
35 #define BUFINC 5000
36
37 static char lastd;
38 static char dtable[MAXDELIM];
39 static char *dtables[MAXDTABLES];
40 static int ndtable=0;
41
42 /* tmatch */
43 #define ALL '*'
44 #define ANY '?'
45 #define RANGE '['
46 #define ENDRANGE ']'
47 #define RANGEDELIM '-'
48 #define NOTRANGE '~'
49
50 /*
51 *----------------------------------------------------------------------------
52 *
53 * Routine: checkrange (from tmatch)
54 *
55 * Purpose: see if character is in specified range
56 *
57 * Returns: 1 if in range, otherwise 0
58 *
59 *----------------------------------------------------------------------------
60 */
61 #ifdef ANSI_FUNC
62 static int
63 checkrange (char *xtemplate, int *ptr, int c)
64 #else
65 static int checkrange(xtemplate, ptr, c)
66 char *xtemplate;
67 int *ptr;
68 int c;
69 #endif
70 {
71 int inrange, notrange;
72 char lorange, hirange;
73 int tptr;
74
75 tptr = *ptr;
76 /* make sure we have a close bracket */
77 if( strchr(&xtemplate[tptr], ENDRANGE) == NULL )
78 return(0);
79 /* check for negation - match if not in range */
80 if( xtemplate [tptr+1] == NOTRANGE ){
81 notrange = 1; tptr++;
82 }
83 else
84 notrange = 0;
85 /* start pessimistically */
86 inrange = 0;
87 /* point past RANGE character */
88 tptr++;
89 while( xtemplate[tptr] != ENDRANGE ){
90 /* get lo range */
91 lorange = xtemplate[tptr];
92 /* and hi range */
93 tptr++;
94 if( xtemplate[tptr] != RANGEDELIM )
95 hirange = lorange;
96 else{
97 tptr++;hirange = xtemplate[tptr];tptr++;
98 }
99 if( (c>=lorange) && (c<=hirange) ){
100 inrange = 1; break;
101 }
102 }
103 /* only exclusive OR of inrange and notrange is ok */
104 if( (inrange ^ notrange) ==0 )
105 return(0);
106 else{
107 *ptr = strchr(&xtemplate[tptr],']') - xtemplate + 1;
108 return(1);
109 }
110 }
111
112 /*
113 *----------------------------------------------------------------------------
114 *
115 * Routine: addstring (from macro)
116 *
117 * Purpose: add a string to a buffer
118 *
119 * Returns: none
120 *
121 *----------------------------------------------------------------------------
122 */
123 #ifdef ANSI_FUNC
124 static void
125 addstring (char **buf, int *blen, int *maxlen, char *str)
126 #else
127 static void addstring(buf, blen, maxlen, str)
128 char **buf;
129 int *blen;
130 int *maxlen;
131 char *str;
132 #endif
133 {
134 int slen;
135
136 slen = strlen(str) + 1;
137 while( (*blen + slen) >= *maxlen ){
138 *maxlen += BUFINC;
139 *buf = (char *)xrealloc(*buf, *maxlen);
140 }
141 strcat(*buf, str);
142 *blen += slen;
143 }
144
145 /*
146 *----------------------------------------------------------------------------
147 *
148 * Routine: addchar (from macro)
149 *
150 * Purpose: add a single char to a buffer
151 *
152 * Returns: none
153 *
154 *----------------------------------------------------------------------------
155 */
156 #ifdef ANSI_FUNC
157 static void
158 addchar (char **buf, int *blen, int *maxlen, int c)
159 #else
160 static void addchar(buf, blen, maxlen, c)
161 char **buf;
162 int *blen;
163 int *maxlen;
164 int c;
165 #endif
166 {
167 char tbuf[2];
168
169 tbuf[0] = c;
170 tbuf[1] = '\0';
171 addstring(buf, blen, maxlen, tbuf);
172 }
173
174 /*
175 *----------------------------------------------------------------------------
176 *
177 * Routine: lookupkeywords (from macro)
178 *
179 * Purpose: lookup a name in a list of keywords
180 *
181 * Returns: return the associated value or NULL
182 *
183 *----------------------------------------------------------------------------
184 */
185 #ifdef ANSI_FUNC
186 static char *
187 lookupkeywords (char *name, char **keyword, char **value, int nkey)
188 #else
189 static char *lookupkeywords(name, keyword, value, nkey)
190 char *name;
191 char **keyword;
192 char **value;
193 int nkey;
194 #endif
195 {
196 int i;
197 for(i=0; i<nkey; i++){
198 if( !strcmp(name, keyword[i]) )
199 return(value[i]);
200 }
201 return(NULL);
202 }
203
204 /*
205 *----------------------------------------------------------------------------
206 *
207 * Routine: hexval (from strtoul16)
208 *
209 * Purpose: return the int value corresponding to a hex character
210 *
211 * Returns: hex value or -1 if the character is not legal hex
212 *
213 *----------------------------------------------------------------------------
214 */
215 #ifdef ANSI_FUNC
216 static int
217 hexval (int c)
218 #else
219 static int hexval(c)
220 int c;
221 #endif
222 {
223 switch(c){
224 case '0': return 0;
225 case '1': return 1;
226 case '2': return 2;
227 case '3': return 3;
228 case '4': return 4;
229 case '5': return 5;
230 case '6': return 6;
231 case '7': return 7;
232 case '8': return 8;
233 case '9': return 9;
234 case 'A': return 10;
235 case 'a': return 10;
236 case 'B': return 11;
237 case 'b': return 11;
238 case 'C': return 12;
239 case 'c': return 12;
240 case 'D': return 13;
241 case 'd': return 13;
242 case 'E': return 14;
243 case 'e': return 14;
244 case 'F': return 15;
245 case 'f': return 15;
246 default: return -1;
247 }
248 }
249
250 /* **************************************************************************
251 *
252 *
253 * PUBLIC ROUTINES
254 *
255 *
256 * **************************************************************************/
257
258 /*
259 *----------------------------------------------------------------------------
260 *
261 * Routine: word
262 *
263 * Purpose: a simple spaced parser
264 *
265 * Returns: 1 if word was found, else 0
266 *
267 *----------------------------------------------------------------------------
268 */
269 #ifdef ANSI_FUNC
270 int
271 word (char *lbuf, char *tbuf, int *lptr)
272 #else
273 int word(lbuf, tbuf, lptr)
274 char *lbuf;
275 char *tbuf;
276 int *lptr;
277 #endif
278 {
279 int ip;
280 int i;
281 char quotes;
282
283 /* reset last delimiter */
284 lastd='\0';
285
286 /* null out the output string */
287 *tbuf = '\0';
288
289 /* if no string was specified, just return */
290 if( lbuf == NULL )
291 return(0);
292
293 /* just a more convenient pointer ... */
294 ip = *lptr;
295
296 /* if we are at the end of string, just return */
297 if( lbuf[ip] == '\0' )
298 return(0);
299
300 /* skip over white space */
301 while( isspace((int)lbuf[ip]) || (dtable[(int)lbuf[ip]]>0) ){
302 if( lbuf[ip] == '\0' ){
303 *lptr = ip;
304 return(0);
305 }
306 else
307 ip++;
308 }
309
310 /* check for an explicit quote */
311 quotes = '\0';
312 if( lbuf[ip] == '"' ){
313 quotes = '"';
314 lastd = '"';
315 }
316 if( lbuf[ip] == '\'' ){
317 quotes = '\'';
318 lastd = '\'';
319 }
320
321 /* grab next token */
322 if( quotes != '\0' ){
323 /* bump past quotes */
324 ip++;
325 /* grab up to next quotes -- but skip escaped quotes */
326 for(i=0; lbuf[ip] != '\0'; i++, ip++){
327 if( (lbuf[ip] == quotes) && (lbuf[ip-1] != '\\') )
328 break;
329 else
330 tbuf[i] = lbuf[ip];
331 }
332 }
333 else{
334 /* grab up to next whitespace */
335 for(i=0;
336 lbuf[ip] && !isspace((int)lbuf[ip]) && (dtable[(int)lbuf[ip]]==0);
337 i++, ip++)
338 tbuf[i] = lbuf[ip];
339 /* save this delimiter */
340 lastd = lbuf[ip];
341 }
342 /* bump past delimiter (but not null terminator) */
343 if( lbuf[ip] )
344 ip++;
345
346 /* null terminate */
347 tbuf[i] = '\0';
348
349 /* got something */
350 *lptr = ip;
351 return(1);
352 }
353
354 /*
355 *----------------------------------------------------------------------------
356 *
357 * Routine: newdtable
358 *
359 * Purpose: save the current delim table and init a new one
360 *
361 * Returns: 1 if another delim table can be allocated, 0 otherwise
362 *
363 *----------------------------------------------------------------------------
364 */
365 #ifdef ANSI_FUNC
366 int
367 newdtable (char *s)
368 #else
369 int newdtable(s)
370 char *s;
371 #endif
372 {
373 int i;
374 char *cur;
375
376 if( ndtable >= MAXDTABLES ){
377 fprintf(stderr, "ERROR: no more delimiter tables available\n");
378 return(0);
379 }
380 /* save another dtable */
381 ndtable++;
382 /* allocate new space for this table */
383 dtables[ndtable-1] = (char *)xmalloc(MAXDELIM);
384 cur = dtables[ndtable-1];
385 /* copy and zero the old table */
386 for(i=0; i<MAXDELIM; i++){
387 cur[i] = dtable[i];
388 dtable[i] = 0;
389 }
390 /* add delims to the new table */
391 if( s != NULL ){
392 for(; *s; s++)
393 dtable[(int)*s] = 1;
394 }
395 return(1);
396 }
397
398 /*
399 *----------------------------------------------------------------------------
400 *
401 * Routine: freedtable
402 *
403 * Purpose: restore last delim table as the current
404 *
405 * Returns: 1 if there is a table to restore, else 0
406 *
407 *----------------------------------------------------------------------------
408 */
409 #ifdef ANSI_FUNC
410 int
411 freedtable (void)
412 #else
413 int freedtable()
414 #endif
415 {
416 int i;
417 char *cur;
418
419 if( ndtable <= 0 ){
420 fprintf(stderr, "ERROR: no delimiter tables to restore\n");
421 return(0);
422 }
423 cur = dtables[ndtable-1];
424 /* copy the restored table into 'current' */
425 for(i=0; i<MAXDELIM; i++){
426 dtable[i] = cur[i];
427 }
428 /* free up this dtable */
429 xfree((void *)cur);
430 /* one less dtable to worry about */
431 ndtable--;
432 return(1);
433 }
434
435 /*
436 *----------------------------------------------------------------------------
437 *
438 * Routine: newdelim
439 *
440 * Purpose: add a string delimiters to the parse table
441 *
442 * Returns: none
443 *
444 *----------------------------------------------------------------------------
445 */
446 #ifdef ANSI_FUNC
447 void
448 newdelim (char *s)
449 #else
450 void newdelim(s)
451 char *s;
452 #endif
453 {
454 if( s != NULL ){
455 for(; *s; s++)
456 dtable[(int)*s] = 1;
457 }
458 }
459
460 /*
461 *----------------------------------------------------------------------------
462 *
463 * Routine: freedelim
464 *
465 * Purpose: remove delims from current delim table
466 *
467 * Returns: none
468 *
469 *----------------------------------------------------------------------------
470 */
471 #ifdef ANSI_FUNC
472 void
473 freedelim (char *s)
474 #else
475 void freedelim(s)
476 char *s;
477 #endif
478 {
479 int i;
480
481 if( s ){
482 for(; *s; s++)
483 if( dtable[(int)*s] > 0 )
484 dtable[(int)*s] -= 1;
485 }
486 else{
487 for(i=0; i<MAXDELIM; i++)
488 if( dtable[i] > 0 )
489 dtable[i] -= 1;
490 }
491 }
492
493 /*
494 *----------------------------------------------------------------------------
495 *
496 * Routine: lastdelim
497 *
498 * Purpose: return the last delimiter
499 *
500 * Returns: delim character
501 *
502 *----------------------------------------------------------------------------
503 */
504 #ifdef ANSI_FUNC
505 int
506 lastdelim (void)
507 #else
508 int lastdelim()
509 #endif
510 {
511 return((int)lastd);
512 }
513
514 /*
515 *----------------------------------------------------------------------------
516 *
517 * Routine: tmatch
518 *
519 * Purpose: match string to a template
520 *
521 * the legal meta characters in a template are just like the
522 * C-shell meta characters, i.e:
523 * ? match any character, but there must be one
524 * * match anything, or nothing
525 * [<c>...] match an inclusive set
526 *
527 *
528 * Returns: non-zero if match, zero otherwise
529
530 *
531 *----------------------------------------------------------------------------
532 */
533 #ifdef ANSI_FUNC
534 int
535 tmatch (char *string, char *xtemplate)
536 #else
537 int tmatch(string, xtemplate)
538 char *string;
539 char *xtemplate;
540 #endif
541 {
542 char *lastmeta=0;
543 char *nonabsorbed=0;
544 int sptr=0;
545 int tptr=0;
546
547 /* loop through string and template */
548 while( (xtemplate[tptr] != '\0') || (string[sptr] != '\0') ){
549 /* if exact match, just bump both pointers */
550 if( string[sptr] == xtemplate[tptr] ){
551 sptr++; tptr++; continue;
552 }
553 /* if range character, check ranges */
554 if( xtemplate[tptr] == RANGE ){
555 if( checkrange(xtemplate, &tptr, string[sptr]) == 0 ){
556 /* no match - was there a meta character before */
557 if( lastmeta == 0 ) return(0);
558 /* if so, back up to it and try again */
559 xtemplate = lastmeta; tptr=0;
560 /* begin checking at the non-absorbed point */
561 string = nonabsorbed; sptr=0;
562 continue;
563 }
564 /* got a match, so bump past */
565 else{
566 sptr++; continue;
567 }
568 }
569 /* if ANY, any character if fine, but there must be one */
570 if( xtemplate[tptr] == ANY ){
571 if( string[sptr] == '\0' )
572 return(0);
573 else{
574 sptr++; tptr++; continue;
575 }
576 }
577 /* if ALL, we can match anything */
578 if( xtemplate[tptr] == ALL ){
579 /* remember where the * is */
580 lastmeta = &xtemplate[tptr];
581 tptr++;
582 /* no more template after this means a win */
583 if( xtemplate[tptr] == '\0' ) return(1);
584 /* if the next template char is not a meta,
585 we skip up to its match in the string */
586 if( xtemplate[tptr] == RANGE){
587 while( checkrange(xtemplate, &tptr, string[sptr]) == 0 ){
588 /* missing the next template char */
589 if( string[sptr] == '\0' ) return(0);
590 sptr++;
591 }
592 /* remember the first non-absorbed character */
593 nonabsorbed = &string[sptr];nonabsorbed++;
594 sptr++;
595 continue;
596 }
597 /* skip past characters, if next template char is not a meta */
598 else if( xtemplate[tptr] != ANY && xtemplate[tptr] != ALL ){
599 while(string[sptr] != xtemplate[tptr]){
600 /* not finding the next template char
601 is bad */
602 if( string[sptr] == '\0' ) return(0);
603 sptr++;
604 }
605 /* remember the first non-absorbed character */
606 nonabsorbed = &string[sptr];nonabsorbed++;
607 continue;
608 }
609 else{
610 /* remember the first non-absorbed character */
611 nonabsorbed = &string[sptr];nonabsorbed++;
612 continue;
613 }
614 }
615 /* no match, no meta char - see if we once had a meta */
616 else{
617 if( lastmeta == 0 ) return(0);
618 /* if so, back up to it and try again */
619 xtemplate = lastmeta; tptr=0;
620 /* begin checking at the non-absorbed point */
621 string = nonabsorbed; sptr=0;
622 continue;
623 }
624 }
625 /* matched to the nulls - we win */
626 return(1);
627 }
628
629 /*
630 *----------------------------------------------------------------------------
631 *
632 * Routine: keyword
633 *
634 * Purpose: look for a keyword=<value> string inside another string,
635 * remove and return the <value> in another buffer
636 *
637 * Returns: len if keyword was found, 0 otherwise
638 *
639 * NB: ibuf cannot be static, as it is modified in place
640 *
641 *----------------------------------------------------------------------------
642 */
643 #ifdef ANSI_FUNC
644 int
645 keyword (char *ibuf, char *key, char *obuf, int maxlen)
646 #else
647 int keyword(ibuf, key, obuf, maxlen)
648 char *ibuf;
649 char *key;
650 char *obuf;
651 int maxlen;
652 #endif
653 {
654 int len;
655 int qlev;
656 char *s;
657 char *t;
658 char *u;
659 char *v;
660 char *iptr;
661 char quote;
662
663 /* if we have no input string, we are done */
664 if( (ibuf == NULL) || (*ibuf == '\0') ){
665 return(0);
666 }
667
668 /* start out pessimistically */
669 *obuf = '\0';
670 iptr = ibuf;
671
672 /* maxlen generally is 1 more than we can handle */
673 maxlen--;
674
675 /* keep trying */
676 while( *iptr ){
677 /* look for key from current position */
678 if( (s = (char *)strstr(iptr, key)) == NULL )
679 return(0);
680 /* if we found a key, we need to make sure ... */
681 /* it must be preceeded by beginning of string, beginning of bracket,
682 or by a "," from previous keyword */
683 if( (s == ibuf) || (*(s-1) == ',') || (*(s-1) == '[') ){
684 /* it can be followed by spaces ... */
685 t = s + strlen(key);
686 while( isspace((int)*t) )
687 t++;
688 /* but must be followed by an "=" */
689 if( *t == '=' ){
690 t++;
691 /* skip spaces again */
692 while( isspace((int)*t) )
693 t++;
694 /* this is where the actual value part of the string begins */
695 u = t;
696 /* this will be where it ends */
697 v = t;
698 /* gather up everything to the next "," or end of filter */
699 if( (*t == '"') || (*t == '\'') || (*t == '(') || (*t == '[') ){
700 switch(*t){
701 case '"':
702 case '\'':
703 quote = *t;
704 break;
705 case '(':
706 quote = ')';
707 break;
708 case '[':
709 quote = ']';
710 break;
711 }
712 /* bump past opening quote char */
713 t++; u++; v++;
714 while( *t && (*t != quote) ){
715 t++; v++;
716 }
717 if( *t == quote ){
718 t++;
719 }
720 }
721 else{
722 qlev = 0;
723 while( *t &&
724 ((qlev != 0) || (*t != ',')) &&
725 ((qlev != 0) || (*t != ']')) ){
726 if( *t == '[' )
727 qlev++;
728 else if( *t == ']' )
729 qlev--;
730 t++; v++;
731 }
732 }
733 len = MIN(maxlen, v - u);
734 strncpy(obuf, u, len);
735 obuf[len] = '\0';
736 /* remove keyword=value string from the original buffer */
737 /* first remove preceding comma, if necessary */
738 if( (s > ibuf) && (*(s-1) == ',') )
739 s--;
740 /* but leave 1 comma in place */
741 else if( *t == ',' )
742 t++;
743 /* now overwrite original from where the keyword started */
744 memmove(s, t, strlen(t)+1);
745 return(len);
746 }
747 }
748 /* start next search just past this one */
749 iptr = s+1;
750 }
751 return(0);
752 }
753
754 /*
755 *----------------------------------------------------------------------------
756 *
757 * Routine: macro
758 *
759 * Purpose: expand a macro using a client's callback
760 *
761 * Returns: expanded macro as an allocated string
762 *
763 *----------------------------------------------------------------------------
764 */
765 #ifdef ANSI_FUNC
766 char *
767 macro (char *icmd, char **keyword, char **value, int nkey,
768 MacroCB client_callback, void *client_data)
769 #else
770 char *macro(icmd, keyword, value, nkey, client_callback, client_data)
771 char *icmd;
772 char **keyword;
773 char **value;
774 int nkey;
775 MacroCB client_callback;
776 void *client_data;
777 #endif
778 {
779 int i, j;
780 int maxlen;
781 char brace;
782 char *result;
783 char tbuf[1000];
784 char tbuf1[1000];
785 char *s;
786 char *ip;
787 char *mip;
788
789 /* make a new string using the command as a base, but substituting
790 for "$" values as needed */
791 result = (char *)xmalloc(BUFINC+1);
792 maxlen = BUFINC;
793 *result = '\0';
794 for(i=0, ip=icmd; *ip; ip++){
795 if( *ip != '$' ){
796 addchar(&result, &i, &maxlen, *ip);
797 }
798 else{
799 /* save beginning of macro */
800 mip = ip;
801 /* skip past '$' */
802 ip++;
803 /* check for brace mode */
804 if( *ip == '{' ){
805 brace = '{';
806 ip++;
807 }
808 else if( *ip == '(' ){
809 brace = '(';
810 ip++;
811 }
812 else
813 brace = '\0';
814 /* get variable up to next non-alpha character or close brace */
815 for(*tbuf='\0', j=0; *ip; ip++ ){
816 /* if we are in brace mode, look for trailing brace */
817 if( brace && *ip == (brace == '(' ? ')' : '}') ){
818 ip++;
819 break;
820 }
821 /* else look for a non-alpha character */
822 else if( !isalnum((int)*ip) && *ip != '_'){
823 break;
824 }
825 else{
826 tbuf[j++] = *ip;
827 tbuf[j] = '\0';
828 }
829 }
830 /* back up so the outer loop adds this delimiting char to the output */
831 ip--;
832 /* search for keyword from the list */
833 if( (nkey > 0) &&
834 (s=lookupkeywords(tbuf, keyword, value, nkey)) != NULL ){
835 addstring(&result, &i, &maxlen, s);
836 }
837 /* execute the client routine to expand macros */
838 else if( (client_callback != NULL) &&
839 ((s=(*client_callback)(tbuf, client_data)) != NULL) ){
840 addstring(&result, &i, &maxlen, s);
841 }
842 /* look for an environment variable */
843 else if( (s = (char *)getenv(tbuf)) != NULL ){
844 addstring(&result, &i, &maxlen, s);
845 }
846 /* if we don't recognize this macro, put it back onto the string */
847 else{
848 int len;
849 len = ip - mip + 1;
850 strncpy(tbuf1, mip, len);
851 tbuf1[len] = '\0';
852 addstring(&result, &i, &maxlen, tbuf1);
853 }
854 }
855 }
856 /* null terminate and save the string */
857 result[i] = '\0';
858 result = (char *)xrealloc(result, i+1);
859 return(result);
860 }
861
862 /* **************************************************************************
863 *
864 * misc word routines
865 *
866 * **************************************************************************/
867
868 /*
869 *----------------------------------------------------------------------------
870 *
871 * Routine: cluc
872 *
873 * Purpose: convert lower to upper case string in place
874 *
875 * Returns: none
876 *
877 *----------------------------------------------------------------------------
878 */
879 #ifdef ANSI_FUNC
880 void
881 cluc (char *s)
882 #else
883 void cluc(s)
884 char *s;
885 #endif
886 {
887 while(*s){
888 if( islower((int)*s) )
889 *s = toupper(*s);
890 s++;
891 }
892 }
893
894 /*
895 *----------------------------------------------------------------------------
896 *
897 * Routine: culc
898 *
899 * Purpose: convert upper to lower case string in place
900 *
901 * Returns:
902 *
903 *----------------------------------------------------------------------------
904 */
905 #ifdef ANSI_FUNC
906 void
907 culc (char *s)
908 #else
909 void culc(s)
910 char *s;
911 #endif
912 {
913 while(*s){
914 if( isupper((int)*s) )
915 *s = tolower(*s);
916 s++;
917 }
918 }
919
920 /*
921 *----------------------------------------------------------------------------
922 *
923 * Routine: nowhite
924 *
925 * Purpose: removes all beginning and ending white space from string
926 *
927 * Returns: returns the number of characters
928 *
929 *----------------------------------------------------------------------------
930 */
931 #ifdef ANSI_FUNC
932 int
933 nowhite (
934 char *c, /* buffer to be cleaned */
935 char *cr /* buffer for returned string */
936 )
937 #else
938 int nowhite(c,cr)
939 char *c; /* buffer to be cleaned */
940 char *cr; /* buffer for returned string */
941 #endif
942 {
943 char *cr0; /* initial value of cr */
944 int n; /* the number of characters */
945
946 /* skip leading white space */
947 while(*c && isspace((int)*c))
948 c++;
949 /* copy up to the null */
950 cr0 = cr;
951 while(*c)
952 *cr++ = *c++;
953 n = cr - cr0; /* the number of characters */
954 *cr-- = '\0'; /* Null and point to the last character */
955 /* remove trailing white space */
956 while( n && isspace((int)*cr) ){
957 *cr-- = '\0';
958 n--;
959 }
960 return(n);
961 }
962
963 /*
964 *----------------------------------------------------------------------------
965 *
966 * Routine: nocr
967 *
968 * Purpose: remove trailing <CR> from a string (in place)
969 *
970 * Returns: none
971 *
972 *----------------------------------------------------------------------------
973 */
974 #ifdef ANSI_FUNC
975 void
976 nocr (char *s)
977 #else
978 void nocr(s)
979 char *s;
980 #endif
981 {
982 int len;
983
984 if( (s==NULL) || (*s=='\0') )
985 return;
986 len = strlen(s);
987 if( s[len-1] == '\n' )
988 s[len-1] = '\0';
989 }
990
991 /*
992 *----------------------------------------------------------------------------
993 *
994 * Routine: istrue
995 *
996 * Purpose: check if a string is "true" or "yes" or "on"
997 *
998 * Returns: 1 if true string, 0 otherwise
999 *
1000 *----------------------------------------------------------------------------
1001 */
1002 #ifdef ANSI_FUNC
1003 int
1004 istrue (char *s)
1005 #else
1006 int istrue(s)
1007 char *s;
1008 #endif
1009 {
1010 char *t;
1011 int result;
1012
1013 if( (s==NULL) || (*s=='\0') )
1014 return(0);
1015 t = (char *)xmalloc(strlen(s)+1);
1016 nowhite(s, t);
1017 culc(t);
1018 result = (!strcmp(t, "true") || !strcmp(t, "yes") ||
1019 !strcmp(t, "on") || !strcmp(t, "1") );
1020 xfree(t);
1021 return(result);
1022 }
1023
1024
1025 /*
1026 *----------------------------------------------------------------------------
1027 *
1028 * Routine: isfalse
1029 *
1030 * Purpose: check if a string is "false" or "no" or "off"
1031 *
1032 * Returns: 1 if false string, 0 otherwise
1033 *
1034 *----------------------------------------------------------------------------
1035 */
1036 #ifdef ANSI_FUNC
1037 int
1038 isfalse (char *s)
1039 #else
1040 int isfalse(s)
1041 char *s;
1042 #endif
1043 {
1044 char *t;
1045 int result;
1046
1047 if( (s==NULL) || (*s=='\0') )
1048 return(0);
1049 t = (char *)xmalloc(strlen(s)+1);
1050 nowhite(s, t);
1051 culc(t);
1052 result = (!strcmp(t, "false") || !strcmp(t, "no") ||
1053 !strcmp(t, "off") || !strcmp(t, "0") );
1054 xfree(t);
1055 return(result);
1056 }
1057
1058
1059 /*
1060 *----------------------------------------------------------------------------
1061 *
1062 * Routine: strtoul16
1063 *
1064 * Purpose: convert a string to an unsigned long hex value
1065 *
1066 * Returns: converted hex value (end of converted string is in t)
1067 *
1068 *----------------------------------------------------------------------------
1069 */
1070 #ifdef ANSI_FUNC
1071 unsigned long strtoul16 (char *s, char **t)
1072 #else
1073 unsigned long strtoul16(s, t)
1074 char *s;
1075 char **t;
1076 #endif
1077 {
1078 unsigned long v=0;
1079 int h;
1080
1081 while ( *s != ' ' &&
1082 *s != '\n' &&
1083 *s != '\r' &&
1084 *s != '\0' ){
1085 v *= 16;
1086 if( (h = hexval(*s)) >= 0 ){
1087 v += h;
1088 s++;
1089 }
1090 else{
1091 break;
1092 }
1093 }
1094 if( t != NULL )
1095 *t = s;
1096 return(v);
1097 }