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