-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_words.c
66 lines (55 loc) · 1.23 KB
/
split_words.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
/*
** split_words.c for zssh
**
** Made by Matthieu Lucotte
** Login <gounter@users.sourceforge.net>
**
** Started on Thu Jun 29 19:11:06 2000 Matthieu Lucotte
** Last update Sat Oct 6 23:40:56 2001 Matthieu Lucotte
*/
#include "zssh.h"
void pc_new_tab(int *argc, char ***argv)
{
char **pt;
pt = smalloc((*argc + TAB_STEP) * sizeof(char *));
memcpy(pt, *argv, *argc * sizeof(char *));
free(*argv);
*argv = pt;
}
void pc_mk_word(char **str, int *pos, int *argc, char ***argv)
{
int len;
if ((len = *pos)) {
(*argv)[(*argc)++] = str_n_dup(*str, len);
if (!(*argc % TAB_STEP))
pc_new_tab(argc, argv);
*str += len;
*pos = 0;
}
while (**str && mi_is_whitespace(**str))
(*str)++;
}
int pc_ok_split(char *comm, int pos, int i)
{
if ((!pos || mi_is_whitespace(comm[pos - 1])))
return 1;
return 0;
}
void pc_split_words(char *comm, int *argc, char ***argv)
{
int pos;
int i;
*argv = smalloc(TAB_STEP * sizeof(char *));
*argc = 0;
while (mi_is_whitespace(*comm))
comm++;
for (pos = 0; comm[pos];)
if ((i = pc_escape_multi(comm, &pos, ESC_COMMON)) <= 0) {
if (mi_is_whitespace(comm[pos]))
pc_mk_word(&comm, &pos, argc, argv);
else
pos++;
}
pc_mk_word(&comm, &pos, argc, argv);
(*argv)[(*argc)++] = 0;
}