-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplex.c
148 lines (121 loc) · 3.37 KB
/
plex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* -MODULE----------------------------------------------------------------------
Phorward C/C++ Library
Copyright (C) 2006-2019 by Phorward Software Technologies, Jan Max Meyer
https://phorward.info ++ contact<at>phorward<dash>software<dot>com
All rights reserved. See LICENSE for more information.
File: plex.c
Usage: A plex object demonstration suite.
----------------------------------------------------------------------------- */
#include "local.h"
void help( char** argv )
{
printf( "Usage: %s OPTIONS patterns...\n\n"
" -b --begin STRING Use STRING as result begin separator\n"
" (' >' is default)\n"
" -d --dot Dump scanner as graph for visualizations in\n"
" Graphviz dot-format\n"
" -e --end STRING Use STRING as result end separator\n"
" ('<\\n' is default)\n"
" -f --file FILENAME Read input from FILENAME\n"
" -h --help Show this help, and exit.\n"
" -i --input INPUT Use string INPUT as input.\n"
" -V --version Show version info and exit.\n"
"\n", *argv );
}
int main( int argc, char** argv )
{
char* finput = (char*)NULL;
char* begin_sep = " >";
char* end_sep = "<\n";
char* start = (char*)NULL;
char* end;
plex* lex;
int id;
pboolean dot = FALSE;
int i;
int rc;
int next;
char opt [ 10 + 1 ];
char* param;
size_t size = 0;
/* Analyze command-line parameters */
for( i = 0; ( rc = pgetopt( opt, ¶m, &next, argc, argv,
"b:de:Df:hi:V",
"begin: dot end: file: "
"help input: version", i ) ) == 0; i++ )
{
if( !strcmp( opt, "begin" ) || !strcmp( opt, "b" ) )
begin_sep = pstrunescape( param );
else if( !strcmp( opt, "dot" ) || !strcmp( opt, "d" ) )
dot = TRUE;
else if( !strcmp( opt, "end" ) || !strcmp( opt, "e" ) )
end_sep = pstrunescape( param );
else if( !strcmp( opt, "file" ) || !strcmp( opt, "f" ) )
{
finput = pfree( finput );
if( !pfiletostr( &finput, param ) )
{
fprintf( stderr, "Unable to read input file '%s'\n", param );
return 1;
}
start = finput;
}
else if( !strcmp( opt, "help" ) || !strcmp( opt, "h" ) )
{
help( argv );
return 0;
}
else if( !strcmp( opt, "input" ) || !strcmp( opt, "i" ) )
start = param;
else if( !strcmp( opt, "version" ) || !strcmp( opt, "V" ) )
{
version( argv, "Lexical analysis command-line utility" );
return 0;
}
}
if( rc < 0 && param )
{
fprintf( stderr, "Unknown option '%s'\n", param );
help( argv );
return 1;
}
lex = plex_create( 0 );
for( i = 0; next < argc; next++, i++ )
plex_define( lex, argv[next], i + 1, 0 );
if( !i )
{
plex_free( lex );
if( argc > 1 )
fprintf( stderr, "Too less parameters given.\n" );
help( argv );
return 1;
}
/* Dump to dot */
if( dot )
{
plex_dump_dot( stdout, lex );
plex_free( lex );
return 0;
}
/* Read from stdin */
if( !start )
{
pgetline( &finput, &size, stdin );
start = finput;
}
/* Process */
while( start && *start )
{
if( !( start = plex_next( lex, start, &id, &end ) ) )
break;
/* Print match ID only when more than one token was defined */
if( i > 1 )
printf( "%d", id );
printf( "%s%.*s%s", begin_sep, (int)( end - start ), start, end_sep );
start = end;
}
fflush( stdout );
pfree( finput );
plex_free( lex );
return 0;
}