author | Mahlon E. Smith <mahlon@martini.nu> |
Fri, 04 Nov 2011 20:34:28 -0700 | |
changeset 14 | 51eb85ae4de4 |
parent 12 | 191b3c25974a |
child 16 | e6a640ad2cc2 |
permissions | -rw-r--r-- |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
1 |
/* vim: set noet nosta sw=4 ts=4 ft=c : */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
2 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
3 |
Copyright (c) 2011, Mahlon E. Smith <mahlon@martini.nu> |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
4 |
All rights reserved. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
5 |
Redistribution and use in source and binary forms, with or without |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
6 |
modification, are permitted provided that the following conditions are met: |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
7 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
8 |
* Redistributions of source code must retain the above copyright |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
9 |
notice, this list of conditions and the following disclaimer. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
10 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
11 |
* Redistributions in binary form must reproduce the above copyright |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
12 |
notice, this list of conditions and the following disclaimer in the |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
13 |
documentation and/or other materials provided with the distribution. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
14 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
15 |
* Neither the name of Mahlon E. Smith nor the names of his |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
16 |
contributors may be used to endorse or promote products derived |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
17 |
from this software without specific prior written permission. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
18 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
19 |
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
20 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
21 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
22 |
DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
23 |
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
24 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
26 |
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
27 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
28 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
29 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
30 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
31 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
32 |
#include "volta.h" |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
33 |
|
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
34 |
|
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
35 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
36 |
* Output basic usage information. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
37 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
38 |
void |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
39 |
usage( char *prg ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
40 |
{ |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
41 |
printf( "%s [-vh] [-f <filename>] [-d <level>]\n", prg ); |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
42 |
printf( " -d <level> Show debug information on stderr\n" ); |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
43 |
printf( " -c <filename> Create the volta database from a rules file\n" ); |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
44 |
printf( " -f <filename> Specify the database file to use (default is './volta.db')\n"); |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
45 |
printf( " -h Usage (you're lookin' at it)\n" ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
46 |
printf( " -v Display version\n" ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
47 |
printf( "\n" ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
48 |
return; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
49 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
50 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
51 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
52 |
/* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
53 |
* Debug function, only output to stderr if the debug level is |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
54 |
* equal or greater to the set output level. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
55 |
* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
56 |
* level: The minimum debug level that must be set for the |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
57 |
* line to be logged. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
58 |
* file: The current code file that is emitting the log |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
59 |
* line: The line number of the code file that is emitting the log |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
60 |
* ... : any printf style strings and formats that constitute the log message |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
61 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
62 |
void |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
63 |
debug( int level, char *file, int line, const char *fmt, ... ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
64 |
{ |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
65 |
if ( v.debugmode < level ) return; |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
66 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
67 |
char timestamp[20]; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
68 |
time_t t = time( NULL ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
69 |
struct tm *now = localtime( &t ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
70 |
strftime( timestamp, 20, "%F %T", now ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
71 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
72 |
va_list args; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
73 |
va_start( args, fmt ); |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
74 |
fprintf( stderr, "%s [%s] #%d %d (%s:%d): ", |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
75 |
PROG, timestamp, getpid(), level, file, line ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
76 |
vfprintf( stderr, fmt, args ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
77 |
va_end( args ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
78 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
79 |
return; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
80 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
81 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
82 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
83 |
/* |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
84 |
* Output to stdout for squid, unless the debug level is at or above 5. |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
85 |
*/ |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
86 |
void |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
87 |
out( const char *str ) |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
88 |
{ |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
89 |
if ( v.debugmode >= 5 ) return; |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
90 |
fprintf( stdout, "%s", str ); |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
91 |
return; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
92 |
} |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
93 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
94 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
95 |
/* |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
96 |
* Given a string, reverse it in place. |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
97 |
*/ |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
98 |
void |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
99 |
reverse_str( char *str ) |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
100 |
{ |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
101 |
int i = 0; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
102 |
int tmp = 0; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
103 |
int j = strlen( str ) - 1; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
104 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
105 |
while ( i < j ) { |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
106 |
tmp = str[i]; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
107 |
str[i] = str[j]; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
108 |
str[j] = tmp; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
109 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
110 |
i++; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
111 |
j--; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
112 |
} |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
113 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
114 |
return; |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
115 |
} |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
116 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
117 |
|
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
118 |
/* |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
119 |
* Lowercase a string in place. |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
120 |
* |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
121 |
*/ |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
122 |
void |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
123 |
lowercase_str( char *str, unsigned short int len ) |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
124 |
{ |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
125 |
unsigned short int i = 0; |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
126 |
char c; |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
127 |
|
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
128 |
for ( ; i < len; i++ ) { |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
129 |
c = str[i]; |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
130 |
str[i] = tolower( c ); |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
131 |
} |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
132 |
|
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
133 |
return; |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
134 |
} |
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
135 |
|
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
136 |
|
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
137 |
/* |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
138 |
* Append 'buf' to the end of 'line', a la strcat, except dynamically |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
139 |
* grow memory for the target string. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
140 |
* |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
141 |
* 'buf' should be null terminated. Returns the modified line. |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
142 |
*/ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
143 |
char * |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
144 |
extend_line( char *line, const char *buf ) |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
145 |
{ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
146 |
char *line_realloc; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
147 |
unsigned short int offset; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
148 |
size_t new_len; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
149 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
150 |
/* find offset and lengths, first assignment */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
151 |
if ( line == NULL ) { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
152 |
offset = 0; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
153 |
new_len = strlen( buf ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
154 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
155 |
/* find offset and lengths, append to existing string */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
156 |
else { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
157 |
offset = strlen( line ); /* not including '\0' */ |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
158 |
new_len = offset + LINE_BUFSIZE; |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
159 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
160 |
|
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
161 |
debug( 5, LOC, "Extending line %d to %d bytes at offset %d\n", v.timer.lines+1, new_len, offset ); |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
162 |
if ( new_len > LINE_MAX || (line_realloc = realloc(line, sizeof(char) * new_len)) == NULL ) { |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
163 |
debug( 5, LOC, "Ignoring line %d, error while allocating memory: %s\n", |
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
164 |
v.timer.lines+1, (line_realloc == NULL ? strerror(errno) : "Line too large") ); |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
165 |
free( line ), line = NULL; |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
166 |
printf( "\n" ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
167 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
168 |
else { |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
169 |
line = line_realloc; |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
170 |
memcpy( line + offset, buf, LINE_BUFSIZE ); |
2
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
171 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
172 |
|
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
173 |
return( line ); |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
174 |
} |
8c88756f81b0
Ensure that parsing can't be subverted by requests larger than the
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff
changeset
|
175 |
|
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
176 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
177 |
/* |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
178 |
* Read an entire file into memory, returning a pointer to the contents. |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
179 |
* Returns NULL on error. |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
180 |
* |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
181 |
*/ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
182 |
char * |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
183 |
slurp_file( char *file ) |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
184 |
{ |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
185 |
FILE *fh = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
186 |
char *contents = NULL; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
187 |
struct stat sb; |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
188 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
189 |
if ( stat( file, &sb ) != 0 ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
190 |
debug( 1, LOC, "Unable to stat() file '%s': %s\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
191 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
192 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
193 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
194 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
195 |
if ( (contents = malloc( sb.st_size + 1 )) == NULL ) { |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
196 |
debug( 5, LOC, "Unable to allocate memory for file '%s': %s\n", |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
197 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
198 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
199 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
200 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
201 |
if ( (fh = fopen( file, "r" )) == NULL ) { |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
202 |
debug( 1, LOC, "Could not open file for reading '%s': %s\n", |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
203 |
file, strerror(errno) ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
204 |
return( NULL ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
205 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
206 |
|
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
207 |
if ( fread( contents, sizeof(char), sb.st_size, fh ) != (unsigned int)sb.st_size ) { |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
208 |
debug( 5, LOC, "Short read for file '%s'?: %s\n", file ); |
5
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
209 |
fclose( fh ); |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
210 |
return( NULL ); |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
211 |
} |
7718f04c8cd1
Check the return value of fread() when loading SQL migration files.
Mahlon E. Smith <mahlon@martini.nu>
parents:
4
diff
changeset
|
212 |
|
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
213 |
fclose( fh ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
214 |
return( contents ); |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
215 |
} |
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
216 |
|
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
217 |
|
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
218 |
/* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
219 |
* Allocate memory and copy +length+ bytes (plus 1 for null) from the given |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
220 |
* +string+ into a new string, returning a pointer to it. |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
221 |
* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
222 |
*/ |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
223 |
char * |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
224 |
copy_string_token( char *string, unsigned short int length ) |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
225 |
{ |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
226 |
char *alloc_ptr = NULL; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
227 |
if ( string == NULL || length == 0 ) return ( NULL ); |
4
5701b7859a31
Groundwork for automatic database initialization and schema upgrades.
Mahlon E. Smith <mahlon@laika.com>
parents:
2
diff
changeset
|
228 |
|
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
229 |
if ( (alloc_ptr = calloc( length + 1, sizeof(char) )) == NULL ) { |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
230 |
debug( 5, LOC, "Unable to allocate memory for token: %s\n", strerror(errno) ); |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
231 |
return( NULL ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
232 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
233 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
234 |
(void)memcpy( alloc_ptr, string, length ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
235 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
236 |
return( alloc_ptr ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
237 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
238 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
239 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
240 |
/* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
241 |
* Allocate memory and copy +length+ bytes from the given dotted quad style |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
242 |
* +ip_string+ into an in_addr struct, returning a pointer to it. |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
243 |
* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
244 |
*/ |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
245 |
/* |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
246 |
struct in_addr * |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
247 |
copy_ipv4_token( char *ip_string, unsigned short int length ) |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
248 |
{ |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
249 |
struct in_addr *alloc_ptr = NULL; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
250 |
char c_ip[ INET_ADDRSTRLEN ]; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
251 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
252 |
if ( ip_string == NULL ) return ( NULL ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
253 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
254 |
(void)strncpy( c_ip, ip_string, length ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
255 |
c_ip[ length ] = '\0'; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
256 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
257 |
if ( (alloc_ptr = calloc( length, sizeof(struct in_addr) )) == NULL ) { |
12
191b3c25974a
Clean up redundant parser actions via preprocessor macros, add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
10
diff
changeset
|
258 |
debug( 5, LOC, "Unable to allocate memory for ip '%s': %s\n", |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
259 |
c_ip, strerror(errno) ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
260 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
261 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
262 |
if ( inet_pton( AF_INET, c_ip, alloc_ptr ) < 1 ) { |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
263 |
debug( 1, LOC, "Unable to create in_addr struct for client ip '%s': %s\n", |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
264 |
c_ip, strerror(errno) ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
265 |
free( alloc_ptr ), alloc_ptr = NULL; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
266 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
267 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
268 |
return( alloc_ptr ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
269 |
} |
14
51eb85ae4de4
There isn't a fast way to look up ( value exists or null ) for every
Mahlon E. Smith <mahlon@martini.nu>
parents:
12
diff
changeset
|
270 |
*/ |
10
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
271 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
272 |
/* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
273 |
* Report how many lines were processed per second. |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
274 |
* |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
275 |
*/ |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
276 |
void |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
277 |
report_speed( void ) |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
278 |
{ |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
279 |
if ( v.debugmode < 3 ) return; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
280 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
281 |
time_t end_time = time( NULL ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
282 |
double elapsed = difftime( end_time, v.timer.start ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
283 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
284 |
if ( elapsed > 0 ) { |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
285 |
debug( 3, LOC, "Processed %lu lines in %0.1f seconds. (%0.1f lines/sec)\n", |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
286 |
v.timer.lines, elapsed, v.timer.lines/elapsed ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
287 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
288 |
else { |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
289 |
debug( 3, LOC, "Processed %lu lines in under a second.\n", v.timer.lines ); |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
290 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
291 |
|
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
292 |
return; |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
293 |
} |
d07309450285
Get the ragel line parser properly tokenizing the input lines. Add a
Mahlon E. Smith <mahlon@martini.nu>
parents:
5
diff
changeset
|
294 |