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
|
/* aewm - Copyright 1998-2007 Decklin Foster <decklin@red-bean.com>.
* This program is free software; please see LICENSE for details. */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "common.h"
#include "parser.h"
/* If the user specifies an rc file, return NULL immediately if it's not
* found; otherwise, search for the usual suspects. */
FILE *open_rc(char *rcfile, char *def)
{
FILE *rc;
char buf[BUF_SIZE];
if (rcfile) {
return fopen(rcfile, "r");
} else {
snprintf(buf, sizeof buf, "%s/.aewm/%s", getenv("HOME"), def);
if ((rc = fopen(buf, "r"))) {
return rc;
} else {
snprintf(buf, sizeof buf, "%s/%s", SYS_RC_DIR, def);
return fopen(buf, "r");
}
}
}
char *get_rc_line(char *s, int size, FILE *stream)
{
while (fgets(s, size, stream)) {
if (s[0] == '#' || s[0] == '\n')
continue;
else
return s;
}
return NULL;
}
/* 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 = 0, nchars = 0;
while (**src && isspace(**src)) (*src)++;
if (**src == '"') {
quoted = 1;
(*src)++;
}
while (**src) {
if (quoted) {
if (**src == '"') {
(*src)++;
break;
}
} else {
if (isspace(**src))
break;
}
if (**src == '\\') (*src)++;
*dest++ = *(*src)++;
nchars++;
}
*dest = '\0';
return nchars || quoted;
}
|