File: parser.c

package info (click to toggle)
aewm 1.2.5-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 304 kB
  • ctags: 280
  • sloc: ansic: 2,229; makefile: 146; sh: 24
file content (46 lines) | stat: -rw-r--r-- 1,256 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
/* aewm - a minimalist X11 window mananager. vim:sw=4:ts=4:et
 * Copyright 1998-2004 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 of words in double quotes (backslashes are permitted in
 * either case). 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)
{
    int quoted, nchars = 0;

    while (**src && isspace(**src)) (*src)++;

    if (**src == '"') {
        quoted = 1;
        (*src)++;
    } else {
        quoted = 0;
    }

    while (**src) {
        if (quoted) {
            if (**src == '"') {
                (*src)++;
                break;
            }
        } else {
            if (isspace(**src)) break;
        }
        if (**src == '\\') (*src)++;
        *dest++ = *(*src)++;
        nchars++;
    }

    *dest = '\0';
    return nchars || quoted;
}