-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.c
66 lines (55 loc) · 975 Bytes
/
lex.c
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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <cassandra.h>
char *
readline(char *prompt)
{
char *buf;
int c, pos;
buf = malloc(BUFSIZE);
if (buf == NULL)
return NULL;
memset(buf, 0, BUFSIZE);
printf("%s", prompt);
for (pos = 0; pos < BUFSIZE - 1; pos++) {
c = getchar();
if (c == '\n')
break;
buf[pos] = c;
}
return buf;
}
static int
issep(char *sep, char ch)
{
int i, len;
if (sep == NULL)
return 0;
for (len = strlen(sep), i = 0; i < len; i++)
if (ch == sep[i])
return 1;
return 0;
}
void
nextarg(char *ln, int *pos, char *sep, char *arg)
{
char *s;
char ch;
if (ln == NULL || pos == NULL || arg == NULL)
return;
s = arg;
/* Skip whitespace */
ch = ln[*pos];
while (isspace(ch))
ch = ln[++(*pos)];
/* Fill in arg until a separator is reached */
strcpy(s, "");
while (ch != '\0' && !issep(sep, ch)) {
*(s++) = ch;
ch = ln[++(*pos)];
};
*s = '\0';
}