-
Notifications
You must be signed in to change notification settings - Fork 3
/
FTPPARSE.C
100 lines (88 loc) · 1.58 KB
/
FTPPARSE.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* ftpparse: pftp parsing functions
*
* Copyright (c) 2013, 2018 Roger Burrows
*
* This file is distributed under the GPL, version 2 or at your
* option any later version. See LICENSE.TXT for details.
*/
#include "ftp.h"
/*
* function prototypes
*/
PRIVATE WORD get_next_arg(char **p,char **argv);
WORD parse_line(char *line,char **argv)
{
char *p, *temp;
WORD argc, rc;
p = line;
argc = 0;
while(1) {
rc = get_next_arg(&p,&temp);
switch(rc) {
case ARG_NORMAL:
argv[argc++] = temp;
break;
case NO_MORE_ARGS:
return argc;
break;
case QUOTING_ERROR:
cputs("error in quoted field\r\n");
return -1;
break;
default:
cputs("error parsing line\r\n");
return -1;
break;
}
}
return -1;
}
/*
* scans buffer for next arg, handles quoted args
*
* returns:
* 1 arg is normal
* 0 no more args
* -1 quoting error
*
* the buffer pointer is updated iff return code >= 0
*/
PRIVATE WORD get_next_arg(char **pp,char **arg)
{
char *p;
WORD inquotes = 0;
/*
* look for start of next arg
*/
for (p = *pp, *arg = NULL; *p; p++)
if (*p != ' ')
break;
if (!*p) { /* end of buffer */
*pp = p;
return NO_MORE_ARGS;
}
*arg = p;
if (*p == '"') {
inquotes = 1;
p++;
}
for ( ; *p; p++) {
if (*p == '"') {
if (!inquotes)
return QUOTING_ERROR;
inquotes = 0;
continue;
}
if (inquotes)
continue;
if (*p == ' ') {
*p++ = '\0';
break;
}
}
if (inquotes)
return QUOTING_ERROR;
*pp = p;
return ARG_NORMAL;
}