File: parser.c

package info (click to toggle)
aewm 1.2.0-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 264 kB
  • ctags: 259
  • sloc: ansic: 2,017; makefile: 139; sh: 22
file content (51 lines) | stat: -rw-r--r-- 1,370 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
/* aewm - a minimalist X11 window mananager. vim:noet:sw=4:ts=4
 * Copyright 1998-2001 Decklin Foster <decklin@red-bean.com>
 * This program is free software; see LICENSE for details. */

#include "parser.h"
#include <string.h>
#include <ctype.h>

/* Our crappy parser. A token is either a whitespace-delimited word,
 * or a bunch or words in double quotes. Inside "", \" may be used.
 * src points to somewhere in a buffer -- the caller must save the
 * location of this buffer, because we update src to point past all
 * the tokens found so far. If we find a token, we write it into dest
 * (caller is responsible for allocating storage) and return 1.
 * Otherwise return 0. */

int get_token(char **src, char *dest)
{
	char *tok_start, *tok_end, *p;
	int quoted, nchars = 0;

	/* skip initial spaces */
	while (**src && isspace(**src)) (*src)++;

	if (*src[0] == '"') {
		quoted = 1;
		tok_start = *src+1;
		tok_end = strchr(tok_start, '"');
		if (!tok_end) return 0; /* unterminated quote */
		while (*(tok_end-1) == '\\')
			tok_end = strchr(tok_end+1, '"');
	} else {
		quoted = 0;
		tok_start = *src;
		tok_end = strpbrk(*src, " \t\n");
	}

	p = tok_start;
	while (tok_end ? p < tok_end : *p) {
		if (quoted && *p == '\\' && *(p+1) == '"') {
			p++;
		} else {
			nchars++;
			*dest++ = *p++;
		}
	}
	*dest = '\0';

	*src = ++tok_end;
	return nchars || quoted;
}