2 * Regular Pattern Analyzer Toolkit (RPA/Tk)
3 * Copyright (c) 2009-2012 Martin Stoilov
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Martin Stoilov <martin@rpasearch.com>
22 #include <sys/types.h>
24 #include <sys/types.h>
33 #include "rlib/rmem.h"
34 #include "rlib/rarray.h"
35 #include "rex/rexdfaconv.h"
36 #include "rex/rexdfa.h"
40 void rex_buffer_unmap_file(rbuffer_t *buf)
43 munmap(buf->s, buf->size);
49 rbuffer_t * rex_buffer_map_file(const char *filename)
55 int fd = open(filename, O_RDONLY);
59 if (fstat(fd, &st) < 0) {
63 buffer = (char*)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
64 if (buffer == (void*)-1) {
68 str = (rbuffer_t *)r_malloc(sizeof(rbuffer_t));
71 memset(str, 0, sizeof(*str));
73 str->size = st.st_size;
74 str->userdata = (void*)((unsigned long)fd);
75 str->alt_destroy = rex_buffer_unmap_file;
80 munmap(buffer, st.st_size);
86 int usage(int argc, const char *argv[])
88 fprintf(stderr, "REX Code Compiler - using REX library version: %s \n", rex_db_version());
89 fprintf(stderr, "Copyright (C) 2012 Martin Stoilov\n\n");
91 fprintf(stderr, "Usage: \n %s [OPTIONS] <filename>\n", argv[0]);
92 fprintf(stderr, " OPTIONS:\n");
93 fprintf(stderr, "\t-o <cfile> Output .c file.\n");
94 fprintf(stderr, "\t-d Dump regular expressions.\n");
95 fprintf(stderr, "\t-D Dump DFA states.\n");
96 fprintf(stderr, "\t-N Dump NFA states.\n");
97 fprintf(stderr, "\t-s Include substates.\n");
98 fprintf(stderr, "\t-t Display statistics.\n");
99 fprintf(stderr, "\t-v Display version information.\n");
100 fprintf(stderr, "\t-h, --help Display this help.\n");
106 int rexcc_buffer_realloc(rbuffer_t *buffer, unsigned long size)
110 s = (char *)r_realloc(buffer->s, size);
120 rbuffer_t *rexcc_buffer_loadfile(FILE *pFile)
122 unsigned long memchunk = 256;
123 long ret = 0, inputsize = 0;
126 buf = r_buffer_create(2 * memchunk);
131 if ((buf->size - inputsize) < memchunk) {
132 if (rexcc_buffer_realloc(buf, buf->size + memchunk) < 0) {
133 fprintf(stderr, "Out of memory!\n");
137 ret = (long)fread(&buf->s[inputsize], 1, memchunk - 1, pFile);
138 if ((ret <= 0) && ferror(pFile)) {
139 r_buffer_destroy(buf);
143 buf->s[inputsize] = '\0';
144 buf->size = inputsize;
145 } while (!feof(pFile));
151 int main(int argc, const char *argv[])
153 int i, ret = 0, dumponly = 0;;
155 int withsubstates = 0;
156 FILE *devnull = NULL;
157 rexdb_t *tempdb = NULL;
158 FILE *cfile = stdout;
161 pCC = rex_cc_create();
166 for (i = 1; i < argc; i++) {
167 if (strcmp(argv[i], "-t") == 0) {
171 for (i = 1; i < argc; i++) {
172 if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-help") == 0 || strcmp(argv[i], "/?") == 0 || strcmp(argv[i], "-h") == 0) {
177 for (i = 1; i < argc; i++) {
178 if (strcmp(argv[i], "-v") == 0) {
179 fprintf(stderr, "REXCC with REX library version: %s\n", rex_db_version());
183 for (i = 1; i < argc; i++) {
184 if (strcmp(argv[i], "-o") == 0) {
186 cfile = fopen(argv[i], "wb");
188 fprintf(stderr, "Failed to create file: %s, %s\n", argv[i], strerror(errno));
195 for (i = 1; i < argc; i++) {
196 if (strcmp(argv[i], "-s") == 0) {
200 for (i = 1; i < argc; i++) {
201 if (strcmp(argv[i], "-D") == 0) {
205 for (i = 1; i < argc; i++) {
206 if (strcmp(argv[i], "-N") == 0) {
210 for (i = 1; i < argc; i++) {
211 if (strcmp(argv[i], "-d") == 0) {
215 for (i = 1; i < argc; i++) {
216 if (argv[i][0] != '-') {
217 rbuffer_t *text = rex_buffer_map_file(argv[i]);
219 if (rex_cc_load_buffer(pCC, text) < 0) {
224 if (pCC->startuid >= 0) {
225 tempdb = rex_db_createdfa(pCC->nfa, pCC->startuid);
226 pCC->dfa = rex_db_todfa(tempdb, withsubstates);
227 rex_db_destroy(tempdb);
228 if (pCC->dfa && !dumponly)
229 rex_cc_output(pCC, cfile);
231 rex_cc_parseinfodump(pCC);
234 r_buffer_destroy(text);
241 } else if (argv[i][1] == 'o'){
246 for (i = 0; i < pCC->dfa->nstates; i++) {
247 rex_dfa_dumpstate(pCC->dfa, i);
250 } else if (dumponly == 2) {
251 rexdb_t *db = pCC->nfa;
252 for (i = 0; i < r_array_length(db->states); i++) {
253 rex_db_dumpstate(db, i);
261 fprintf(stdout, "memory: %ld KB (leaked %ld Bytes)\n", (long)r_debug_get_maxmem()/1024, (long)r_debug_get_allocmem());