Titan Core Initial Contribution
[deliverable/titan.core.git] / repgen / parser.l
1 /******************************************************************************
2 * Copyright (c) 2000-2014 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 ******************************************************************************/
8 %option noyywrap
9 %option yylineno
10 %option never-interactive
11 %option nounput
12 %{
13
14 /* #define CHK_ACCESS */
15 #include <string.h>
16 #include <stdlib.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <errno.h>
20 #include "../common/version_internal.h"
21 #include "repgen.h"
22
23 #ifdef LICENSE
24 #include "../common/license.h"
25 #endif
26
27 #define T_TITLE 199
28 #define T_NAME 200
29 #define T_SHORT 201
30 #define T_LONG 202
31 #define T_STR 203
32 #define T_CODE 204
33 #define T_LOGS 205
34 #define T_DUMP 206
35 #define T_DEST 207
36 #define T_TABLEN 208
37 #define T_FILLCOL 209
38 #define T_ERROR 210
39
40 int token, tok;
41 struct listentry *first, *tclist;
42 char *str;
43 static char title[MAXLEN];
44 static char code_srcdir[MAXLEN], log_srcdir[MAXLEN], dump_srcdir[MAXLEN];
45 static char tcname[MAXLEN], dstdir[MAXLEN];
46 static int tablen, fillcol;
47 char ch;
48 %}
49
50 %x PP TITLE NAME SHORT LONG CODE LOGS DUMP DEST TABLEN FILLCOL ERROR
51
52 digit [0-9]
53 octdigit [0-7]
54 hexdigit [0-9a-fA-F]
55 digits {digit}+
56 alpha [a-zA-Z_]
57 alphanum {alpha}|{digit}
58 whitespace [ \t\f\v]
59 allwhite [ \t\f\b\v\r\n]
60 whitespace2 [ \t\f\v]
61 path [./~a-zA-Z_0-9\-]
62
63 pp_title ^{allwhite}*"#Title"{allwhite}*
64 pp_name ^{allwhite}*"#Testcase"{allwhite}*
65 pp_short ^{allwhite}*"#Purpose"{whitespace}*[\n]*
66 pp_long ^{allwhite}*"#Description"{whitespace}*[\n]*
67 pp_code ^{allwhite}*"#TTCN-3 code"{allwhite}*
68 pp_logs ^{allwhite}*"#TTCN-3 log"{allwhite}*
69 pp_dump ^{allwhite}*"#Other log"{allwhite}*
70 pp_dest ^{allwhite}*"#Destination"{allwhite}*
71 pp_tablen ^{allwhite}*"#Tab length"{allwhite}*
72 pp_fillcol ^{allwhite}*"#Column width"{allwhite}*
73 pp_error ^{allwhite}*"#"{alphanum}*
74
75 %%
76
77 <INITIAL>{pp_title} { BEGIN(TITLE); }
78 <TITLE>{alpha}*{alphanum}* { strcat(title, yytext); }
79 <TITLE>{whitespace}* { strcat(title, " "); }
80 <TITLE>[\n]* { BEGIN(INITIAL); return T_TITLE; }
81
82 <INITIAL>{pp_name} { BEGIN(NAME); }
83 <NAME>{alpha}*{alphanum}* { strcpy(tcname, yytext); return T_NAME; }
84 <NAME>{allwhite}* { BEGIN(INITIAL); }
85
86 <INITIAL>{pp_code} { BEGIN(CODE); }
87 <CODE>{path}*{alphanum}* { strcpy(code_srcdir, yytext); return T_CODE; }
88 <CODE>{allwhite}* { BEGIN(INITIAL); }
89
90 <INITIAL>{pp_logs} { BEGIN(LOGS); }
91 <LOGS>{path}*{alphanum}* { strcpy(log_srcdir, yytext); return T_LOGS; }
92 <LOGS>{allwhite}* { BEGIN(INITIAL); }
93
94 <INITIAL>{pp_dump} { BEGIN(DUMP); }
95 <DUMP>{path}*{alphanum}* { strcpy(dump_srcdir, yytext); return T_DUMP; }
96 <DUMP>{allwhite}* { BEGIN(INITIAL); }
97
98 <INITIAL>{pp_dest} { BEGIN(DEST); }
99 <DEST>{path}*{alphanum}* { strcpy(dstdir, yytext); return T_DEST; }
100 <DEST>{allwhite}* { BEGIN(INITIAL); }
101
102 <INITIAL>{pp_short} { return T_SHORT; }
103
104 <INITIAL>{pp_long} { return T_LONG; }
105
106 <INITIAL>{pp_tablen} { BEGIN(TABLEN); }
107 <TABLEN>{digit}* { tablen = atoi(yytext); return T_TABLEN; }
108 <TABLEN>{allwhite}* { BEGIN(INITIAL); }
109
110 <INITIAL>{pp_fillcol} { BEGIN(FILLCOL); }
111 <FILLCOL>{digit}* { fillcol = atoi(yytext); return T_FILLCOL; }
112 <FILLCOL>{allwhite}* { BEGIN(INITIAL); }
113
114 <INITIAL>.|\n { ch = yytext[0]; return T_STR; }
115
116 <INITIAL>{pp_error} |
117 <*>.|\n { return T_ERROR; }
118
119 %%
120 int main ( int argc, char *argv[] )
121 {
122 char sname[MAXLEN];
123 char lname[MAXLEN];
124 FILE *sfh = NULL;
125 FILE *lfh = NULL;
126 size_t i, offset;
127 #ifndef MINGW
128 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
129 #endif
130 #ifdef LICENSE
131 license_struct lstr;
132 #endif
133
134 if ( argc != 2 )
135 {
136 fprintf ( stderr, "Usage: %s inputfile [-h]\n", argv[0] );
137 return -1;
138 }
139
140 if ( strcmp ( argv[1], "-h" ) == 0 )
141 {
142
143 fputs("HTML Report Generator for the TTCN-3 Test Executor\n"
144 "Product number: " PRODUCT_NUMBER "\n"
145 "Build date: " __DATE__ " " __TIME__ "\n"
146 "Compiled with: " C_COMPILER_VERSION "\n\n"
147 COPYRIGHT_STRING "\n\n"
148 "Example input file is printed to standard output.\n", stderr);
149
150 puts ("#Title Sample\n"
151 "#Tab length 8\n"
152 "#Column width 80\n"
153 "#TTCN-3 code ./\n"
154 "#TTCN-3 log ./\n"
155 "#Other log ./\n"
156 "#Destination ./\n"
157 "\n"
158 "#Testcase testcase1\n"
159 "#Purpose\n"
160 "The purpose of the test case comes here.\n"
161 "#Description\n"
162 "This section describes the test case in detail.\n"
163 "\n"
164 "#Testcase testcase2\n"
165 "#Purpose\n"
166 "The purpose of the test case comes here.\n"
167 "#Description\n"
168 "This section describes the test case in detail.\n" );
169
170 return 0;
171 }
172
173 #ifdef LICENSE
174 init_openssl();
175 load_license(&lstr);
176 if (!verify_license(&lstr)) {
177 free_license(&lstr);
178 free_openssl();
179 exit(EXIT_FAILURE);
180 }
181 if (!check_feature(&lstr, FEATURE_LOGFORMAT)) {
182 fputs("The license key does not allow the usage of HTML "
183 "report generator.\n", stderr);
184 return 2;
185 }
186 free_license(&lstr);
187 free_openssl();
188 #endif
189
190 if ( ( yyin = fopen ( argv[1], "r" ) ) == 0 )
191 {
192 perror ( argv[1] );
193 exit ( 1 );
194 }
195
196 sprintf ( dstdir, "./" );
197
198 tclist = (struct listentry *)malloc ( sizeof ( struct listentry ) );
199 tclist->next = NULL;
200 first = tclist;
201
202 while ( ( token = yylex() ) )
203 {
204 switch(token)
205 {
206 case T_TITLE:
207 tok = token;
208 break;
209
210 case T_NAME:
211 strcpy ( tclist->tcname, tcname );
212 tclist->next = (struct listentry *)malloc ( sizeof ( struct listentry ) );
213 tclist = tclist->next;
214 tclist->next = NULL;
215
216 sprintf ( sname, "%s/%s.short", dstdir, tcname );
217 sprintf ( lname, "%s/%s.long", dstdir, tcname );
218
219 if ( sfh ) fclose ( sfh );
220 if ( ! ( sfh = fopen ( sname, "wt" ) ) )
221 {
222 perror ( sname );
223 return -1;
224 }
225
226 if ( lfh ) fclose ( lfh );
227 if ( ! ( lfh = fopen ( lname, "wt" ) ) )
228 {
229 perror ( lname );
230 return -1;
231 }
232 break;
233
234 case T_SHORT:
235 case T_LONG:
236 tok = token;
237 break;
238
239 case T_STR:
240 switch ( tok )
241 {
242 case T_SHORT:
243 putc(ch, sfh);
244 break;
245
246 case T_LONG:
247 putc(ch, lfh);
248 break;
249
250 case T_STR:
251 default:
252 break;
253 }
254 break;
255
256 case T_CODE:
257 case T_LOGS:
258 case T_DUMP:
259 case T_TABLEN:
260 case T_FILLCOL:
261 break;
262
263 case T_DEST:
264 if ( dstdir[strlen(dstdir)-1] != '/' ) dstdir[strlen(dstdir)] = '/';
265
266 if ( mkdir ( dstdir
267 #ifndef MINGW
268 , mode
269 #endif
270 ) == -1 )
271 {
272 if ( errno != EEXIST )
273 {
274 fprintf ( stderr, "Cannot create directory: %s\n", dstdir );
275 return -1;
276 }
277 /* else fprintf ( stderr, "Directory already exists: %s\n", dstdir ); */
278 }
279
280 offset = strlen ( dstdir );
281 for ( i = 0; i < strlen ( title ); i++ )
282 {
283 if ( title[i] == ' ' ) dstdir[offset+i] = '_';
284 else dstdir[offset+i] = title[i];
285 }
286 strcat ( dstdir, "-report" );
287
288 mkdir ( dstdir
289 #ifndef MINGW
290 , mode
291 #endif
292 );
293
294 break;
295
296 case T_ERROR:
297 default:
298 if ( yytext[strlen(yytext)-1] == '\n' ) yytext[strlen(yytext)-1] = '\0';
299 fprintf ( stderr, "Parse error at line %d: %s\n", yylineno, yytext );
300 return -1;
301 }
302 }
303
304 if ( sfh ) fclose ( sfh );
305 if ( lfh ) fclose ( lfh );
306
307 fclose ( yyin );
308
309 WriteCode ( first, code_srcdir, dstdir, tablen, fillcol );
310 WriteLog ( first, log_srcdir, dstdir );
311 WriteDump ( first, dump_srcdir, dstdir, tablen, fillcol );
312
313 Genhtml ( first, title, dstdir );
314
315 return 0;
316 }
This page took 0.039165 seconds and 5 git commands to generate.