File: strtokq.c

package info (click to toggle)
filter 2.6.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, jessie, jessie-kfreebsd, lenny, sarge, squeeze, wheezy
  • size: 728 kB
  • ctags: 706
  • sloc: ansic: 4,033; sh: 3,932; makefile: 198
file content (62 lines) | stat: -rw-r--r-- 1,657 bytes parent folder | download | duplicates (3)
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
/* strtokq -- part of "filter", the email filter program 
 * Bug reports, patches, comments, suggestions should be sent to:
 *
 *	Philip Brown    filter@bolthole.com
 */

/* Like strtok, but returns quoted strings as one token (quotes removed)
 * if flag is non-null. Quotes and backslahes can be escaped with backslash.
 */

#include <stdio.h>

char *strtokq(source, keys, flag)
char *source, *keys;
int flag;
{
	register int  last_ch;
	static   char *sourceptr = NULL;
		 char *return_value;

	if (source != NULL)
	  sourceptr = source;
	
	if (sourceptr == NULL || *sourceptr == '\0') 
	  return(NULL);		/* we hit end-of-string last time!? */

	sourceptr += strspn(sourceptr, keys);	/* skip leading crap */
	
	if (*sourceptr == '\0') 
	  return(NULL);		/* we've hit end-of-string */

	if (flag)
	  if (*sourceptr == '"' || *sourceptr == '\'') {   /* quoted string */
	    register char *sp;
	    char quote = *sourceptr++;

	    for (sp = sourceptr; *sp != '\0' && *sp != quote; sp++)
	      if (*sp == '\\') sp++;	/* skip escaped characters */
					/* expand_macros will fix them later */

	    return_value = sourceptr;
	    sourceptr = sp;
	    if (*sourceptr != '\0') sourceptr++;
	    *sp = '\0';				/* zero at end */

	    return return_value;
	  }

	last_ch = strcspn(sourceptr, keys);	/* end of good stuff */

	return_value = sourceptr;		/* and get the ret   */

	sourceptr += last_ch;			/* ...value 	     */

	if (*sourceptr != '\0')	/* don't forget if we're at END! */
	  sourceptr++;		   /* and skipping for next time */

	return_value[last_ch] = '\0';		/* ..ending right    */
	
	return((char *) return_value);		/* and we're outta here! */
}