File: Parse.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (945 lines) | stat: -rw-r--r-- 20,890 bytes parent folder | download
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
/* -*-c-*- */
/* 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, see: <http://www.gnu.org/licenses/>
 */

/*
** Parse.c: routines for parsing in fvwm & modules
*/

/* ---------------------------- included header files ---------------------- */

#include "config.h"

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

#include "fvwmlib.h"
#include "Strings.h"
#include "Parse.h"

/* ---------------------------- local definitions -------------------------- */

/* ---------------------------- local macros ------------------------------- */

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/* Copies a token beginning at src to a previously allocated area dest. dest
 * must be large enough to hold the token. Leading whitespace causes the token
 * to be NULL. */
/* NOTE: CopyToken can be called with dest == src. The token will be copied
 * back down over the src string. */
static char *CopyToken(
	char *src, char *dest, char *spaces, int snum, char *delims, int dnum,
	char *out_delim)
{
	int len = 0;
	char *t;

	while (*src != 0 && !(isspace((unsigned char)*src) ||
			      (snum && strchr(spaces, *src)) ||
			      (dnum && strchr(delims, *src))))
	{
		/* Check for quoted text */
		if (IsQuote(*src))
		{
			char c = *src;

			src++;
			while ((*src != c)&&(*src != 0))
			{
				if ((*src == '\\' && *(src+1) != 0))
				{
					/* Skip over backslashes */
					src++;
				}
				if (len < MAX_TOKEN_LENGTH - 1)
				{
					len++;
					*(dest++) = *(src++);
				}
				else
				{
					/* token too long, just skip rest */
					src++;
				}
			}
			if (*src == c)
			{
				src++;
			}
		}
		else
		{
			if ((*src == '\\' && *(src+1) != 0))
			{
				/* Skip over backslashes */
				src++;
			}
			if (len < MAX_TOKEN_LENGTH - 1)
			{
				len++;
				*(dest++) = *(src++);
			}
			else
			{
				/* token too long, just skip rest of token */
				src++;
			}
		}
	}
	if (out_delim)
	{
		*out_delim = *src;
	}
	*dest = 0;
	t = SkipSpaces(src, spaces, snum);
	if (*t != 0 && dnum && strchr(delims, *t) != NULL)
	{
		if (out_delim)
		{
			*out_delim = *t;
		}
		src = t + 1;
	}
	else if (*src != 0)
	{
		src++;
	}

	return src;
}

/* ---------------------------- interface functions ------------------------ */

/* This function escapes all occurences of the characters in the string qchars
 * in the string as with a preceding echar character.  The resulting string is
 * returned in a malloced memory area. */
char *EscapeString(char *s, const char *qchars, char echar)
{
	char *t;
	char *ret;
	int len;

	for (len = 1, t = s; *t ; t++, len++)
	{
		if (strchr(qchars, *t) != NULL)
		{
			len++;
		}
	}
	ret = (char *)safemalloc(len);
	for (t = ret; *s; s++, t++)
	{
		if (strchr(qchars, *s) != NULL)
		{
			*t = echar;
			t++;
		}
		*t = *s;
	}
	*t = 0;

	return ret;
}

/* If the string s begins with a quote chracter SkipQuote returns a pointer
 * to the first unquoted character or to the final '\0'. If it does not, a
 * pointer to the next character in the string is returned.
 * There are three possible types of quoting: a backslash quotes the next
 * character only. Long quotes like " " or ' ' quoting everything in
 * between and quote pairs like ( ) or { }.
 *
 * precedence:
 *
 * 1) Backslashes are honoured always, even inside long or pair quotes.
 * 2) long quotes do quote quoting pair characters but not simple quotes. All
 *    long quotes can quote all other types of long quotes).
 * 3) pair quotes none of the above. Text between a pair of quotes is treated
 *    as a single token.
 *
 * qlong   - string of long quoted (defaults to "'` )
 * qstart  - string of pair quote start characters (defaults to empty string)
 * qend	   - string of pair quote end characters (defaults to empty string)
 *
 * The defaults are used if NULL is passed for the corresponding string.
 */
char *SkipQuote(
	char *s, const char *qlong, const char *qstart, const char *qend)
{
	char *t;

	if (s == NULL || *s == 0)
	{
		return s;
	}
	if (!qlong)
	{
		qlong = "\"'`";
	}
	if (!qstart)
	{
		qstart = "";
	}
	if (!qend)
	{
		qend = "";
	}

	if (*s == '\\' && s[1] != 0)
	{
		return s+2;
	}
	else if (*qlong && (t = strchr(qlong, *s)))
	{
		char c = *t;

		s++;
		while (*s && *s != c)
		{
			/* Skip over escaped text, ie \quote */
			if (*s == '\\' && *(s+1) != 0)
			{
				s++;
			}
			s++;
		}
		if (*s == c)
		{
			s++;
		}
		return s;
	}
	else if (*qstart && (t = strchr(qstart, *s)))
	{
		char c = *((t - qstart) + qend);

		while (*s && *s != c)
		{
			s = SkipQuote(s, qlong, "", "");
		}
		return (*s == c) ? ++s : s;
	}

	return ++s;
}

/* Returns a string up to the first character from the string delims in a
 * malloc'd area just like GetNextToken. Quotes are not removed from the
 * returned string. The returned string is stored in *sout, the return value
 * of this call is a pointer to the first character after the delimiter or
 * to the terminating '\0'. Quoting is handled like in SkipQuote. If sin is
 * NULL, the function returns NULL in *sout. */
char *GetQuotedString(
	char *sin, char **sout, const char *delims, const char *qlong,
	const char *qstart, const char *qend)
{
	char *t = sin;
	unsigned int len;

	if (!sout)
	{
		return NULL;
	}
	if (!sin)
	{
		*sout = NULL;
		return NULL;
	}

	while (*t && !strchr(delims, *t))
	{
		t = SkipQuote(t, qlong, qstart, qend);
	}
	len = t - sin;
	*sout = (char *)safemalloc(len + 1);
	memcpy(*sout, sin, len);
	(*sout)[len] = 0;
	if (*t)
	{
		t++;
	}

	return t;
}

/* SkipSpaces: returns a pointer to the first character in indata that is
 * neither a whitespace character nor contained in the string 'spaces'. snum
 * is the number of characters in 'spaces'. You must not pass a NULL pointer
 * in indata. */
char *SkipSpaces(char *indata, char *spaces, int snum)
{
	while (*indata != 0 && (isspace((unsigned char)*indata) ||
				(snum && strchr(spaces, *indata))))
	{
		indata++;
	}
	return indata;
}

/*
** DoPeekToken: returns next token from string, leaving string intact
**		(you must not free returned string)
**
** WARNING: The returned pointer points to a static array that will be
**	     overwritten in all functions in this file!
**
** For a description of the parameters see DoGetNextToken below. DoPeekToken
** is a bit faster.
*/
/* NOTE: If indata is the pointer returned by a previous call to PeekToken or
 * DoPeekToken, the input string will be destroyed. */
char *DoPeekToken(
	char *indata, char **token, char *spaces, char *delims, char *out_delim)
{
	char *end;
	int snum;
	int dnum;
	static char tmptok[MAX_TOKEN_LENGTH];

	snum = (spaces) ? strlen(spaces) : 0;
	dnum = (delims) ? strlen(delims) : 0;
	if (indata == NULL)
	{
		if (out_delim)
		{
			*out_delim = '\0';
		}
		*token = NULL;
		return NULL;
	}
	indata = SkipSpaces(indata, spaces, snum);
	end = CopyToken(indata, tmptok, spaces, snum, delims, dnum, out_delim);

	if (tmptok[0] == 0)
	{
		*token = NULL;
	}
	else
	{
		*token = tmptok;
	}

	return end;
}

/*
 * PeekToken takes the input string "indata" and returns a pointer to the
 * token, stored in a static char array.  The pointer is invalidated by
 * the next call to PeekToken.  If "outdata" is not NULL, the pointer
 * to the first character after the token is returned through
 * *outdata. (Note that outdata is a char **, not just a char *).
 */
char *PeekToken(char *indata, char **outdata)
{
	char *dummy;
	char *token;

	if (!outdata)
	{
		outdata = &dummy;
	}
	*outdata = DoPeekToken(indata, &token, NULL, NULL, NULL);

	return token;
}


/**** SMR: Defined but not used -- is this for the future or a relic of the
 **** past? ****/
/* domivogt (27-Jun-1999): It's intended for future use. I have no problem
 * commenting it out if it's not used. */

/* Tries to seek up to n tokens in indata. Returns the number of tokens
 * actually found (up to a maximum of n). */

#if 0
int CheckNTokens(char *indata, unsigned int n)
{
	unsigned int i;
	char *token;

	for (i = 0, token = (char *)1; i < n && token != NULL; i++)
	{
		token = PeekToken(indata, NULL);
	}

	return i;
}
#endif

/*
** MatchToken: does case-insensitive compare on next token in string, leaving
**	       string intact (returns true if matches, false otherwise)
*/
int MatchToken(char *pstr,char *tok)
{
	int rc=0;
	char *ntok;

	DoPeekToken(pstr, &ntok, NULL, NULL, NULL);
	if (ntok)
	{
		rc = (strcasecmp(tok,ntok)==0);
	}

	return rc;
}

/* unused at the moment */
/*
  function:	       XCmpToken
  description:	       compare 1st word of s to 1st word of t
  returns:	       < 0  if s < t
  = 0  if s = t
  > 0  if s > t
*/
int XCmpToken(const char *s, const char **t)
{
	register const char *w=*t;

	if (w==NULL)
	{
		return 1;
	}
	if (s==NULL)
	{
		return -1;
	}

	while (*w && (*s==*w || toupper(*s)==toupper(*w)) )
	{
		s++;
		w++;
	}

	if ((*s=='\0' && (ispunct(*w) || isspace(*w)))||
	    (*w=='\0' && (ispunct(*s) || isspace(*s))) )
	{
		return 0;			/* 1st word equal */
	}
	else
	{
		return toupper(*s)-toupper(*w); /* smaller/greater */
	}
}


/*
 *
 * Gets the next "word" of input from string indata.
 * "word" is a string with no spaces, or a qouted string.
 * Return value is ptr to indata,updated to point to text after the word
 * which is extracted.
 * token is the extracted word, which is copied into a malloced
 * space, and must be freed after use. DoGetNextToken *never* returns an
 * empty string or token. If the token consists only of whitespace or
 * delimiters, the returned token is NULL instead. If out_delim is given,
 * the character ending the string is returned therein.
 *
 * spaces = string of characters to treat as spaces
 * delims = string of characters delimiting token
 *
 * Use "spaces" and "delims" to define additional space/delimiter
 * characters (spaces are skipped before a token, delimiters are not).
 *
 */
char *DoGetNextToken(
	char *indata, char **token, char *spaces, char *delims, char *out_delim)
{
	char *tmptok;
	char *end;

	end = DoPeekToken(indata, &tmptok, spaces, delims, out_delim);
	if (tmptok == NULL)
	{
		*token = NULL;
	}
	else
	{
		*token = safestrdup(tmptok);
	}

	return end;
}

/*
 * GetNextToken works similarly to PeekToken, but: stores the token in
 * *token, & returns a pointer to the first character after the token
 * in *token. The memory in *token is allocated with malloc and the
 * calling function has to free() it.
 *
 * If possible, use PeekToken because it's faster and does not risk
 * creating memory leaks.
 */
char *GetNextToken(char *indata, char **token)
{
	return DoGetNextToken(indata, token, NULL, NULL, NULL);
}

/* fetch next token and stop at next ',' */
char *GetNextSimpleOption(char *indata, char **option)
{
	return DoGetNextToken(indata, option, NULL, ",", NULL);
}

/* read multiple tokens up to next ',' or end of line */
char *GetNextFullOption(char *indata, char **option)
{
	return GetQuotedString(indata, option, ",\n", NULL, NULL, NULL);
}

char *SkipNTokens(char *indata, unsigned int n)
{
	for ( ; n > 0 && indata != NULL && *indata != 0; n--)
	{
		PeekToken(indata, &indata);
	}

	return indata;
}

/*
 * convenience functions
 */

/*
 *
 * Works like GetNextToken, but with the following differences:
 *
 * If *indata begins with a "*" followed by the string module_name,
 * it returns the string following directly after module_name as the
 * new token. Otherwise NULL is returned.
 * e.g. GetModuleResource("*FvwmPagerGeometry", &token, "FvwmPager")
 * returns "Geometry" in token.
 *
 */
char *GetModuleResource(char *indata, char **resource, char *module_name)
{
	char *tmp;
	char *next;

	if (!module_name)
	{
		*resource = NULL;
		return indata;
	}
	tmp = PeekToken(indata, &next);
	if (!tmp)
	{
		return next;
	}

	if (tmp[0] != '*' ||
	    strncasecmp(tmp+1, module_name, strlen(module_name)))
	{
		*resource = NULL;
		return indata;
	}
	CopyString(resource, tmp+1+strlen(module_name));

	return next;
}


/*
 *
 * This function uses GetNextToken to parse action for up to num integer
 * arguments. The number of values actually found is returned.
 * If ret_action is non-NULL, a pointer to the next token is returned there.
 * The suffixlist parameter points to a string of possible suffixes for the
 * integer values. The index of the matched suffix is returned in
 * ret_suffixnum (0 = no suffix, 1 = first suffix in suffixlist ...).
 *
 */
static int _get_suffixed_integer_arguments(
	char *action, char **ret_action, int *retvals, int num,
	char *suffixlist, int *ret_suffixnum, char *parsestring)
{
	int i;
	int j;
	int n;
	char *token;
	int suffixes;

	/* initialize */
	suffixes = 0;
	if (suffixlist != 0)
	{
		/* if passed a suffixlist save its length */
		suffixes = strlen(suffixlist);
	}

	for (i = 0; i < num && action; i++)
	{
		token = PeekToken(action, &action);
		if (token == NULL)
		{
			break;
		}
		if (sscanf(token, parsestring, &(retvals[i]), &n) < 1)
		{
			break;
		}
		if (suffixes != 0 && ret_suffixnum != NULL)
		{
			int len;
			char c;

			len = strlen(token) - 1;
			c = token[len];
			if (isupper(c))
			{
				c = tolower(c);
			}
			for (j = 0; j < suffixes; j++)
			{
				char c2 = suffixlist[j];

				if (isupper(c2))
				{
					c2 = tolower(c2);
				}
				if (c == c2)
				{
					ret_suffixnum[i] = j+1;
					break;
				}
			}
			if (j == suffixes)
			{
				ret_suffixnum[i] = 0;
			}
		}
		else if (token[n] != 0 && !isspace(token[n]))
		{
			/* there is a suffix but no suffix list was specified */
			break;
		}
	}
	if (ret_action != NULL)
	{
		*ret_action = action;
	}

	return i;
}

int GetSuffixedIntegerArguments(
	char *action, char **ret_action, int *retvals, int num,
	char *suffixlist, int *ret_suffixnum)
{
	return _get_suffixed_integer_arguments(
		action, ret_action, retvals, num, suffixlist, ret_suffixnum,
		"%d%n");
}

/*
 *
 * This function converts the suffix/number pairs returned by
 * GetSuffixedIntegerArguments into pixels. The unit_table is an array of
 * integers that determine the factor to multiply with in hundredths of
 * pixels. I.e. a unit of 100 means: translate the value into pixels,
 * 50 means divide value by 2 to get the number of pixels and so on.
 * The unit used is determined by the suffix which is taken as the index
 * into the table. No size checking of the unit_table is done, so make sure
 * it is big enough before calling this function.
 *
 */
int SuffixToPercentValue(int value, int suffix, int *unit_table)
{
	return (value * unit_table[suffix]) / 100;
}

/*
 *
 * This function uses GetNextToken to parse action for up to num integer
 * arguments. The number of values actually found is returned.
 * If ret_action is non-NULL, a pointer to the next token is returned there.
 *
 */
int GetIntegerArguments(char *action, char **ret_action, int *retvals,int num)
{
	return _get_suffixed_integer_arguments(
		action, ret_action, retvals, num, NULL, NULL, "%d%n");
}

/*
 *
 * Same as above, but supports hexadecimal and octal integers via 0x and 0
 * prefixes.
 *
 */
int GetIntegerArgumentsAnyBase(
	char *action, char **ret_action, int *retvals,int num)
{
	return _get_suffixed_integer_arguments(
		action, ret_action, retvals, num, NULL, NULL, "%i%n");
}

/*
 *
 * This function tries to match a token with a list of strings and returns
 * the position of token in the array or -1 if no match is found. The last
 * entry in the list must be NULL.
 *
 * len = 0 : only exact matches
 * len < 0 : match, if token begins with one of the strings in list
 * len > 0 : match, if the first len characters do match
 *
 * if next is non-NULL, *next will be set to point to the first character
 * in token after the match.
 *
 */
int GetTokenIndex(char *token, char **list, int len, char **next)
{
	int i;
	int l;
	int k;

	if (!token || !list)
	{
		if (next)
		{
			*next = NULL;
		}
		return -1;
	}
	l = (len) ? len : strlen(token);
	for (i = 0; list[i] != NULL; i++)
	{
		k = strlen(list[i]);
		if (len < 0)
		{
			l = k;
		}
		else if (len == 0 && k != l)
		{
			continue;
		}
		if (!strncasecmp(token, list[i], l))
		{
			break;
		}
	}
	if (next)
	{
		*next = (list[i]) ? token + l : token;
	}

	return (list[i]) ? i : -1;
}

/*
 *
 * This function does roughly the same as GetTokenIndex but reads the
 * token from string action with GetNextToken. The index is returned
 * in *index. The return value is a pointer to the character after the
 * token (just like the return value of GetNextToken).
 *
 */
char *GetNextTokenIndex(char *action, char **list, int len, int *index)
{
	char *token;
	char *next;

	if (!index)
	{
		return action;
	}

	token = PeekToken(action, &next);
	if (!token)
	{
		*index = -1;
		return action;
	}
	*index = GetTokenIndex(token, list, len, NULL);

	return (*index == -1) ? action : next;
}


/*
 *
 * Parses two integer arguments given in the form <int><character><int>.
 * character can be ' ' or 'x', but any other character is allowed too
 * (except 'p' or 'P').
 *
 */
int GetRectangleArguments(char *action, int *width, int *height)
{
	char *token;
	int n;

	token = PeekToken(action, NULL);
	if (!token)
	{
		return 0;
	}
	n = sscanf(token, "%d%*c%d", width, height);

	return (n == 2) ? 2 : 0;
}

/* unit_io is input as well as output. If action has a postfix 'p' or 'P',
 * *unit_io is set to 100, otherwise it is left untouched. */
int GetOnePercentArgument(char *action, int *value, int *unit_io)
{
	unsigned int len;
	char *token;
	int n;

	*value = 0;
	if (!action)
	{
		return 0;
	}
	token = PeekToken(action, NULL);
	if (!token)
	{
		return 0;
	}
	len = strlen(token);
	/* token never contains an empty string, so this is ok */
	if (token[len - 1] == 'p' || token[len - 1] == 'P')
	{
		*unit_io = 100;
		token[len - 1] = '\0';
	}
	n = sscanf(token, "%d", value);

	return n;
}


int GetTwoPercentArguments(
	char *action, int *val1, int *val2, int *val1_unit, int *val2_unit)
{
	char *tok1;
	char *tok2;
	char *next;
	int n = 0;

	*val1 = 0;
	*val2 = 0;

	tok1 = PeekToken(action, &next);
	action = GetNextToken(action, &tok1);
	if (!tok1)
	{
		return 0;
	}
	GetNextToken(action, &tok2);
	if (GetOnePercentArgument(tok2, val2, val2_unit) == 1 &&
	    GetOnePercentArgument(tok1, val1, val1_unit) == 1)
	{
		free(tok1);
		free(tok2);
		return 2;
	}

	/* now try MxN style number, specifically for DeskTopSize: */
	n = GetRectangleArguments(tok1, val1, val2);
	free(tok1);
	if (tok2)
	{
		free(tok2);
	}

	return n;
}


/* Parses the next token in action and returns 1 if it is "yes", "true", "y",
 * "t" or "on", zero if it is "no", "false", "n", "f" or "off" and -1 if it is
 * "toggle". A pointer to the first character in action behind the token is
 * returned through ret_action in this case. ret_action may be NULL. If the
 * token matches none of these strings the default_ret value is returned and
 * the action itself is passed back in ret_action. If the no_toggle flag is
 * non-zero, the "toggle" string is handled as no match. */
int ParseToggleArgument(
	char *action, char **ret_action, int default_ret, char no_toggle)
{
	int index;
	int rc;
	char *next;
	char *optlist[] = {
		"toggle",
		"yes", "no",
		"true", "false",
		"on", "off",
		"t", "f",
		"y", "n",
		NULL
	};

	next = GetNextTokenIndex(action, optlist, 0, &index);
	if (index == 0 && no_toggle == 0)
	{
		/* toggle requested explicitly */
		rc = -1;
	}
	else if (index == -1 || (index == 0 && no_toggle))
	{
		/* nothing selected, use default and don't modify action */
		rc = default_ret;
		next = action;
	}
	else
	{
		/* odd numbers denote True, even numbers denote False */
		rc = (index & 1);
	}
	if (ret_action)
	{
		*ret_action = next;
	}

	return rc;
}

/* Strips the path from 'path' and returns the last component in a malloc'ed
 * area. */
char *GetFileNameFromPath(char *path)
{
	char *s;
	char *name;

	/* we get rid of the path from program name */
	s = strrchr(path, '/');
	if (s)
	{
		s++;
	}
	else
	{
		s = path;
	}
	name = (char *)safemalloc(strlen(s)+1);
	strcpy(name, s);

	return name;
}