]>
Commit | Line | Data |
---|---|---|
c86edd1d Q |
1 | /* |
2 | * splitline: splits a line into a list of parameters. | |
3 | * | |
4 | * This function works "in place", i.e. the original input string is destroyed. | |
5 | * | |
6 | * This function splits lines on each space, up to a maximum of maxparams. | |
7 | * Spaces at the beginning/between words are replaced with '\0', as are all | |
8 | * \r or \n characters. | |
9 | * | |
10 | * If "coloncheck" is nonzero, a parameter beginning with ':' will be treated | |
11 | * as the last. The inevitable exception to this is the _first_ parameter... | |
12 | */ | |
13 | ||
14 | int splitline(char *inputstring, char **outputvector, int maxparams, int coloncheck) { | |
15 | char *c; | |
16 | int instr=0; /* State variable: 0 = between params, 1 = in param */ | |
17 | int paramcount=0; | |
18 | ||
19 | for (c=inputstring;*c;c++) { | |
20 | if (instr) { | |
21 | if (*c==' ') { | |
22 | /* Space in string -- end string, obliterate */ | |
23 | *c='\0'; | |
24 | instr=0; | |
25 | } | |
26 | } else { | |
27 | if (*c==' ') { | |
28 | /* Space when not in string: obliterate */ | |
29 | *c='\0'; | |
30 | } else { | |
31 | /* Non-space character, start new word. */ | |
32 | if (*c==':' && coloncheck && paramcount) { | |
33 | outputvector[paramcount++]=c+1; | |
34 | break; | |
35 | } else if (paramcount+1==maxparams) { | |
36 | outputvector[paramcount++]=c; | |
37 | break; | |
38 | } else { | |
39 | outputvector[paramcount++]=c; | |
40 | instr=1; | |
41 | } | |
42 | } | |
43 | } | |
44 | } | |
45 | ||
46 | return paramcount; | |
47 | } | |
48 | ||
49 | /* | |
50 | * This function reconnects extra arguments together with spaces. | |
51 | * | |
52 | * Multiple spaces will be not be removed | |
53 | * | |
54 | * USE WITH CARE -- you don't have to worry about the original string | |
55 | * being untrusted, but you must get the arguments right :) | |
56 | */ | |
57 | ||
58 | void rejoinline(char *input, int argstojoin) { | |
59 | int i=0; | |
60 | int inword=0; | |
61 | char *ch; | |
62 | ||
63 | if (argstojoin<2) { | |
64 | return; | |
65 | } | |
66 | ||
67 | for (ch=input;;ch++) { | |
68 | if (inword) { | |
69 | if (*ch=='\0') { | |
70 | i++; | |
71 | if (i==argstojoin) { | |
72 | /* We're done */ | |
73 | return; | |
74 | } else { | |
75 | *ch=' '; | |
76 | inword=0; | |
77 | } | |
78 | } | |
79 | } else { | |
80 | /* not in word.. */ | |
81 | if (*ch!='\0') { | |
82 | inword=1; | |
83 | } else { | |
84 | *ch=' '; | |
85 | } | |
86 | } | |
87 | } | |
88 | } | |
89 |