1 |
/* |
2 |
* tokenize.C: Tokenization routines. |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id: tokenize.C,v 1.3 2007-07-21 13:23:22 pippijn Exp $"; |
9 |
|
10 |
#include "atheme.h" |
11 |
#include "pmodule.h" |
12 |
|
13 |
int |
14 |
sjtoken (char *message, char delimiter, char **parv, int limit) |
15 |
{ |
16 |
char *next; |
17 |
int count; |
18 |
|
19 |
if (!message) |
20 |
return 0; |
21 |
|
22 |
/* now we take the beginning of the message and find all the spaces... |
23 |
* set them to \0 and use 'next' to go through the string |
24 |
*/ |
25 |
next = message; |
26 |
|
27 |
/* eat any additional delimiters */ |
28 |
while (*next == delimiter) |
29 |
next++; |
30 |
|
31 |
parv[0] = next; |
32 |
count = 1; |
33 |
|
34 |
while (*next) |
35 |
{ |
36 |
/* this is fine here, since we don't have a :delimited |
37 |
* parameter like tokenize |
38 |
*/ |
39 |
|
40 |
if (count == limit) |
41 |
{ |
42 |
/* we've reached our limit */ |
43 |
if (count == 256) |
44 |
slog (LG_DEBUG, "sjtoken(): reached param limit"); |
45 |
return count; |
46 |
} |
47 |
|
48 |
if (*next == delimiter) |
49 |
{ |
50 |
*next = '\0'; |
51 |
next++; |
52 |
/* eat any additional delimiters */ |
53 |
while (*next == delimiter) |
54 |
next++; |
55 |
/* if it's the end of the string, it's simply |
56 |
** an extra space at the end. here we break. |
57 |
*/ |
58 |
if (*next == '\0') |
59 |
break; |
60 |
|
61 |
/* if it happens to be a stray \r, break too */ |
62 |
if (*next == '\r') |
63 |
break; |
64 |
|
65 |
parv[count] = next; |
66 |
count++; |
67 |
} |
68 |
else |
69 |
next++; |
70 |
} |
71 |
|
72 |
return count; |
73 |
} |
74 |
|
75 |
/* this splits apart a message with origin and command picked off already */ |
76 |
int |
77 |
tokenize (char *message, char **parv) |
78 |
{ |
79 |
char *pos = NULL; |
80 |
char *next; |
81 |
unsigned int count = 0; |
82 |
|
83 |
if (!message) |
84 |
return -1; |
85 |
|
86 |
/* first we fid out of there's a : in the message, save that string |
87 |
* somewhere so we can set it to the last param in parv |
88 |
* also make sure there's a space before it... if not then we're screwed |
89 |
*/ |
90 |
pos = message; |
91 |
while (true) |
92 |
{ |
93 |
if ((pos = strchr (pos, ':'))) |
94 |
{ |
95 |
pos--; |
96 |
if (*pos != ' ') |
97 |
{ |
98 |
pos += 2; |
99 |
continue; |
100 |
} |
101 |
*pos = '\0'; |
102 |
pos++; |
103 |
*pos = '\0'; |
104 |
pos++; |
105 |
break; |
106 |
} |
107 |
else |
108 |
break; |
109 |
} |
110 |
|
111 |
/* now we take the beginning of the message and find all the spaces... |
112 |
* set them to \0 and use 'next' to go through the string |
113 |
*/ |
114 |
|
115 |
next = message; |
116 |
parv[0] = message; |
117 |
count = 1; |
118 |
|
119 |
while (*next) |
120 |
{ |
121 |
if (count == MAXPARC) |
122 |
{ |
123 |
/* we've reached one less than our max limit |
124 |
* to handle the parameter we already ripped off |
125 |
*/ |
126 |
slog (LG_DEBUG, "tokenize(): reached para limit"); |
127 |
return count; |
128 |
} |
129 |
if (*next == ' ') |
130 |
{ |
131 |
*next = '\0'; |
132 |
next++; |
133 |
/* eat any additional spaces */ |
134 |
while (*next == ' ') |
135 |
next++; |
136 |
/* if it's the end of the string, it's simply |
137 |
* an extra space before the :parameter. break. |
138 |
*/ |
139 |
if (*next == '\0') |
140 |
break; |
141 |
parv[count] = next; |
142 |
count++; |
143 |
} |
144 |
else |
145 |
next++; |
146 |
} |
147 |
|
148 |
if (pos) |
149 |
{ |
150 |
parv[count] = pos; |
151 |
count++; |
152 |
} |
153 |
|
154 |
return count; |
155 |
} |