|
1 /* |
|
2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 #include "stdafx.h" |
|
23 #include <commandline.h> |
|
24 #include <sys/types.h> |
|
25 #include <sys/stat.h> |
|
26 #include <stdio.h> |
|
27 |
|
28 #define IN_LINE_MAX 4096 |
|
29 |
|
30 /////////////////////////////////////////////// |
|
31 // AUTO-GENERATED TEXT - DO NOT MODIFY!!! |
|
32 /////////////////////////////////////////////// |
|
33 #define S2P_VERSION "4.0" |
|
34 /////////////////////////////////////////////// |
|
35 |
|
36 DWORD nFileLength = 0; |
|
37 char *lpBuffer = NULL; |
|
38 char szConnection[3] = {0}; |
|
39 char szPlatform[20] = {0}; |
|
40 char szInputName[MAX_PATH + 1] = {0}; |
|
41 char szOutputName[MAX_PATH + 1] = {0}; |
|
42 |
|
43 void cleanup(char *message, UINT errorcode = 0); |
|
44 bool ReadFileIntoArray(const char *file); |
|
45 bool ProcessFileContents(const char *file); |
|
46 void ConvertBackslash(char *szCommand); |
|
47 void WriteHeader(HANDLE outfile); |
|
48 void WriteTrailer(HANDLE outfile); |
|
49 void SkipOverComment(DWORD *counter); |
|
50 |
|
51 int main(int argc, char* argv[]) |
|
52 { |
|
53 printf("\n------------------------------\n" |
|
54 "STAT2PERL Script Converter %s\n" |
|
55 "------------------------------\n" |
|
56 "Copyright Symbian Ltd 2002.\n" |
|
57 "---------------------------", S2P_VERSION); |
|
58 |
|
59 CommandLine oCmd; |
|
60 if (oCmd.GetNumberOfArgs() == 5) |
|
61 { |
|
62 strcpy(szConnection, oCmd.GetProgramArgument(1)); |
|
63 strcpy(szPlatform, oCmd.GetProgramArgument(2)); |
|
64 strcpy(szInputName, oCmd.GetProgramArgument(3)); |
|
65 strcpy(szOutputName, oCmd.GetProgramArgument(4)); |
|
66 |
|
67 if (ReadFileIntoArray(szInputName)) |
|
68 { |
|
69 if (ProcessFileContents(szOutputName)) |
|
70 { |
|
71 printf("\n\nFile converted successfully.\n"); |
|
72 return 1; |
|
73 } |
|
74 } |
|
75 else |
|
76 printf("\nERROR: Could not read [%s] into memory.\n", szInputName); |
|
77 } |
|
78 else |
|
79 printf("\n\nERROR: Incorrect Number of arguments.\nUsage: stat2perl <connection> <platform> <input file> <output file>\n Eg: stat2perl 3 COM1 file.txt file.pl\n"); |
|
80 |
|
81 return -1; |
|
82 } |
|
83 |
|
84 |
|
85 bool ReadFileIntoArray(const char *file) |
|
86 { |
|
87 HANDLE infile; |
|
88 DWORD bytes = 0; |
|
89 struct stat buf; |
|
90 |
|
91 if (stat(file, &buf) == 0) |
|
92 { |
|
93 nFileLength = buf.st_size; |
|
94 |
|
95 if ((infile = CreateFile(file, |
|
96 GENERIC_READ, |
|
97 0, |
|
98 NULL, |
|
99 OPEN_EXISTING, |
|
100 FILE_ATTRIBUTE_NORMAL, |
|
101 0)) |
|
102 != INVALID_HANDLE_VALUE) |
|
103 { |
|
104 lpBuffer = new char [nFileLength + 1]; |
|
105 if (lpBuffer) |
|
106 { |
|
107 if (ReadFile(infile, lpBuffer, nFileLength, &bytes, NULL) && bytes == nFileLength) |
|
108 { |
|
109 CloseHandle(infile); |
|
110 return true; |
|
111 } |
|
112 else |
|
113 printf("\nERROR: Failed to read entire file into memory."); |
|
114 |
|
115 } |
|
116 else |
|
117 printf("\nERROR: Could not allocate memory to read file."); |
|
118 |
|
119 CloseHandle(infile); |
|
120 } |
|
121 else |
|
122 printf("\nERROR: Could not open [%s] to read.", file); |
|
123 } |
|
124 else |
|
125 printf("\nERROR: [%s] does not exist.", file); |
|
126 |
|
127 return false; |
|
128 } |
|
129 |
|
130 bool ProcessFileContents(const char *file) |
|
131 { |
|
132 HANDLE outfile; |
|
133 DWORD bytes = 0; |
|
134 |
|
135 // open our output file |
|
136 if ((outfile = CreateFile(file, |
|
137 GENERIC_WRITE, |
|
138 0, |
|
139 NULL, |
|
140 CREATE_ALWAYS, |
|
141 FILE_ATTRIBUTE_NORMAL, |
|
142 0)) |
|
143 != INVALID_HANDLE_VALUE) |
|
144 { |
|
145 bool bCommand = false; |
|
146 DWORD counter = 0; |
|
147 DWORD actualstart = 0; |
|
148 DWORD actualend = 0; |
|
149 |
|
150 WriteHeader(outfile); |
|
151 |
|
152 // find the beginning and end |
|
153 while(counter < nFileLength) |
|
154 { |
|
155 // check command format is correct |
|
156 if(lpBuffer[counter] == '<') |
|
157 { |
|
158 counter++; |
|
159 if(lpBuffer[counter] == 'B') |
|
160 actualstart = counter + 2; |
|
161 |
|
162 if(lpBuffer[counter] == 'E') |
|
163 { |
|
164 actualend = counter - 2; |
|
165 break; |
|
166 } |
|
167 } |
|
168 else if (lpBuffer[counter] == '/') // start of comment or command |
|
169 { |
|
170 SkipOverComment(&counter); |
|
171 } |
|
172 |
|
173 counter++; |
|
174 } |
|
175 |
|
176 // only concerned with usable part of script |
|
177 counter = actualstart; |
|
178 nFileLength = actualend; |
|
179 |
|
180 // now parse it... |
|
181 while(counter < nFileLength) //while not reached end of the file |
|
182 { |
|
183 // check command format is correct |
|
184 if(lpBuffer[counter] == '<') |
|
185 { |
|
186 bCommand = true; |
|
187 } |
|
188 else if (lpBuffer[counter] == '/') // start of comment or command |
|
189 { |
|
190 SkipOverComment(&counter); |
|
191 } |
|
192 else if (bCommand) // start of comment or command |
|
193 { |
|
194 char szCommand[2048] = {0}; |
|
195 char szLine[IN_LINE_MAX + 1] = {0}; |
|
196 char *ptr = NULL; |
|
197 int cmdLength = 0; |
|
198 |
|
199 // extract the command |
|
200 while (lpBuffer[counter + cmdLength] != '>') |
|
201 cmdLength++; |
|
202 strncpy(szLine, lpBuffer + counter, cmdLength); |
|
203 *(szLine + counter + cmdLength) = (char)0; |
|
204 |
|
205 switch(lpBuffer[counter]) |
|
206 { |
|
207 case '#' : // set image directory - format: '#image <location> |
|
208 if ((ptr = strchr(szLine, ' ')) && strncmp(szLine, "#image", 6) == 0) |
|
209 { |
|
210 (*ptr++) = (char)0; |
|
211 sprintf(szCommand, "set_screenshot_dir(\"%s\") or goto scripterror;", ptr); |
|
212 } |
|
213 break; |
|
214 case 'D' : // retrieves information about connected device |
|
215 case 'd' : |
|
216 strcpy(szCommand, "get_device_info($hndl) or goto scripterror;"); |
|
217 break; |
|
218 case 'K' : //standard QWERTY Keypress command |
|
219 case 'k' : |
|
220 sprintf(szCommand, "basic_key_sequence($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
221 break; |
|
222 case 'L' : //used to send non-standard key data over (ie - Menu button, Home button, PageDown button) |
|
223 case 'l' : |
|
224 sprintf(szCommand, "control_keystroke($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
225 break; |
|
226 case 'M' : //used when combination keys are to be sent over |
|
227 case 'm' : |
|
228 sprintf(szCommand, "combination_keystroke($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
229 break; |
|
230 case 'H' : // hold key down for a while |
|
231 case 'h' : |
|
232 if (ptr = strchr(szLine, ',')) |
|
233 { |
|
234 (*ptr++) = (char)0; |
|
235 sprintf(szCommand, "hold_key($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
236 } |
|
237 break; |
|
238 case 'A' : //Fire up application (second character is a number which opens up the app (e.g - <A1>)) |
|
239 case 'a' : |
|
240 if (ptr = strchr(szLine, ',')) |
|
241 { |
|
242 (*ptr++) = (char)0; |
|
243 sprintf(szCommand, "start_application($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
244 } |
|
245 else |
|
246 sprintf(szCommand, "start_application($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
247 break; |
|
248 case 'F' : //Open file |
|
249 case 'f' : |
|
250 if (ptr = strchr(szLine, ',')) |
|
251 { |
|
252 (*ptr++) = (char)0; |
|
253 sprintf(szCommand, "open_application($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
254 } |
|
255 break; |
|
256 case 'T' : //used to transfer files from the PC to the EPOC device |
|
257 case 't' : |
|
258 if (ptr = strchr(szLine, ',')) |
|
259 { |
|
260 (*ptr++) = (char)0; |
|
261 sprintf(szCommand, "copy_testfile_to_target($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
262 } |
|
263 break; |
|
264 case 'R' : //used to retrieve files from the EPOC device |
|
265 case 'r' : |
|
266 if (ptr = strchr(szLine, ',')) |
|
267 { |
|
268 (*ptr++) = (char)0; |
|
269 sprintf(szCommand, "copy_from_target($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
270 } |
|
271 break; |
|
272 case 'X' : |
|
273 case 'x' : |
|
274 if (ptr = strchr(szLine, ',')) |
|
275 { |
|
276 (*ptr++) = (char)0; |
|
277 sprintf(szCommand, "move_from_target($hndl, \"%s\", \"%s\") or goto scripterror;", szLine + 1, ptr); |
|
278 } |
|
279 break; |
|
280 case 'S' : // Used for screen capture |
|
281 case 's' : |
|
282 if (cmdLength > 1) |
|
283 sprintf(szCommand, "get_screenshot($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
284 else |
|
285 strcpy(szCommand, "get_screenshot($hndl) or goto scripterror;"); |
|
286 break; |
|
287 case 'I' : //Icon coordinates |
|
288 case 'i' : |
|
289 if (ptr = strchr(szLine, ',')) |
|
290 { |
|
291 (*ptr++) = (char)0; |
|
292 sprintf(szCommand, "touch_screen($hndl, %s, %s) or goto scripterror;", szLine + 1, ptr); |
|
293 } |
|
294 break; |
|
295 case 'C' : //Close currently active application |
|
296 case 'c' : |
|
297 strcpy(szCommand, "close_application($hndl) or goto scripterror;"); |
|
298 break; |
|
299 case '!' : // Start ESHELL.EXE |
|
300 if (*(szLine + 1) != '!') |
|
301 strcpy(szCommand, "start_eshell($hndl) or goto scripterror;"); |
|
302 else |
|
303 strcpy(szCommand, "stop_eshell($hndl) or goto scripterror;"); |
|
304 break; |
|
305 case 'P' : //pause |
|
306 case 'p' : |
|
307 sprintf(szCommand, "pause($hndl, %s) or goto scripterror;", szLine + 1); |
|
308 break; |
|
309 case 'U' : // remove file |
|
310 case 'u' : |
|
311 sprintf(szCommand, "remove_file($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
312 break; |
|
313 case 'Y' : // create folder |
|
314 case 'y' : |
|
315 sprintf(szCommand, "create_folder($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
316 break; |
|
317 case 'Z' : // remove folder |
|
318 case 'z' : |
|
319 sprintf(szCommand, "remove_folder($hndl, \"%s\") or goto scripterror;", szLine + 1); |
|
320 break; |
|
321 } // switch |
|
322 |
|
323 // if command supported, write it to file |
|
324 if (*szCommand) |
|
325 { |
|
326 ConvertBackslash(szCommand); |
|
327 WriteFile(outfile, szCommand, strlen(szCommand), &bytes, NULL); |
|
328 WriteFile(outfile, "\r\n", 2, &bytes, NULL); |
|
329 (*szCommand) = (char)0; |
|
330 } |
|
331 |
|
332 counter += cmdLength; |
|
333 bCommand = false; |
|
334 } |
|
335 |
|
336 counter++; |
|
337 } |
|
338 |
|
339 WriteTrailer(outfile); |
|
340 CloseHandle(outfile); |
|
341 } |
|
342 else |
|
343 { |
|
344 printf("\nERROR: Could not open [%s] to write.", file); |
|
345 return false; |
|
346 } |
|
347 |
|
348 return true; |
|
349 } |
|
350 |
|
351 void SkipOverComment(DWORD *counter) |
|
352 { |
|
353 int iInComment = 0; |
|
354 |
|
355 // beginning of comment |
|
356 if (lpBuffer[(*counter) + 1] == '*') |
|
357 { |
|
358 iInComment++; |
|
359 (*counter)+=2; |
|
360 while(iInComment && (*counter) < nFileLength) //while not reached end of the file |
|
361 { |
|
362 if (lpBuffer[(*counter)] == '/') |
|
363 { |
|
364 // beginning of comment - support nested comments |
|
365 if (lpBuffer[(*counter) + 1] == '*') |
|
366 { |
|
367 iInComment++; |
|
368 (*counter)++; |
|
369 } |
|
370 |
|
371 // end of comment |
|
372 if (lpBuffer[(*counter) - 1] == '*') |
|
373 { |
|
374 iInComment--; |
|
375 } |
|
376 } |
|
377 |
|
378 (*counter)++; |
|
379 } |
|
380 } |
|
381 } |
|
382 |
|
383 void ConvertBackslash(char *szCommand) |
|
384 { |
|
385 char szCopy[IN_LINE_MAX + 1] = {0}; |
|
386 int iSrc = 0, iDest = 0; |
|
387 |
|
388 while(*(szCommand + iSrc)) |
|
389 { |
|
390 // need to substitute the special chars |
|
391 if (*(szCommand + iSrc) == '\r') |
|
392 { |
|
393 *(szCopy + iDest) = '\\'; |
|
394 iDest++; |
|
395 *(szCopy + iDest) = 'r'; |
|
396 iDest++; |
|
397 } |
|
398 else if (*(szCommand + iSrc) == '\n') |
|
399 { |
|
400 *(szCopy + iDest) = '\\'; |
|
401 iDest++; |
|
402 *(szCopy + iDest) = 'n'; |
|
403 iDest++; |
|
404 } |
|
405 else if (*(szCommand + iSrc) == '\t') |
|
406 { |
|
407 *(szCopy + iDest) = '\\'; |
|
408 iDest++; |
|
409 *(szCopy + iDest) = 't'; |
|
410 iDest++; |
|
411 } |
|
412 else if (*(szCommand + iSrc) == '@') |
|
413 { |
|
414 *(szCopy + iDest) = '\\'; |
|
415 iDest++; |
|
416 *(szCopy + iDest) = '@'; |
|
417 iDest++; |
|
418 } |
|
419 else |
|
420 { |
|
421 // append a backslash |
|
422 if (*(szCommand + iSrc) == '\\') |
|
423 { |
|
424 *(szCopy + iDest) = '\\'; |
|
425 iDest++; |
|
426 } |
|
427 |
|
428 // now copy the char |
|
429 *(szCopy + iDest) = *(szCommand + iSrc); |
|
430 iDest++; |
|
431 } |
|
432 |
|
433 // don't go too far |
|
434 if (iDest >= IN_LINE_MAX) |
|
435 { |
|
436 *(szCopy + iDest) = (char)0; |
|
437 break; |
|
438 } |
|
439 iSrc++; |
|
440 } |
|
441 |
|
442 strcpy(szCommand, szCopy); |
|
443 } |
|
444 |
|
445 void WriteHeader(HANDLE outfile) |
|
446 { |
|
447 DWORD bytes = 0; |
|
448 char szLine[2048] = {0}; |
|
449 |
|
450 WriteFile(outfile, "#########################################################################\r\n", 75, &bytes, NULL); |
|
451 WriteFile(outfile, "# STAT2PERL CONVERTED SCRIPT\r\n", 30, &bytes, NULL); |
|
452 WriteFile(outfile, "#\r\n", 3, &bytes, NULL); |
|
453 WriteFile(outfile, "# For STAT Perl Interface ", 26, &bytes, NULL); |
|
454 WriteFile(outfile, S2P_VERSION, strlen(S2P_VERSION), &bytes, NULL); |
|
455 WriteFile(outfile, " or higher\r\n#\r\n", 15, &bytes, NULL); |
|
456 WriteFile(outfile, "# Auto-Generated from script:\r\n", 31, &bytes, NULL); |
|
457 sprintf(szLine, "# %s\r\n#\r\n", szInputName); |
|
458 WriteFile(outfile, szLine, strlen(szLine), &bytes, NULL); |
|
459 WriteFile(outfile, "#########################################################################\r\n", 75, &bytes, NULL); |
|
460 WriteFile(outfile, "\r\n", 2, &bytes, NULL); |
|
461 WriteFile(outfile, "use Symbian::StatAPI30; # must include this library\r\n\r\n", 56, &bytes, NULL); |
|
462 WriteFile(outfile, "my $result = 1; # test result\r\n", 40, &bytes, NULL); |
|
463 WriteFile(outfile, "my $hndl; # handle to connection\r\n\r\n", 51, &bytes, NULL); |
|
464 WriteFile(outfile, "# set everything up\r\n", 21, &bytes, NULL); |
|
465 sprintf(szLine, "my @arglist = (\"STAT.DLL\", %c, \"%s\");\r\n", szConnection[0], _strupr(szPlatform)); |
|
466 WriteFile(outfile, szLine, strlen(szLine), &bytes, NULL); |
|
467 WriteFile(outfile, "initialise(@arglist) or goto scripterror;\r\n\r\n", 45, &bytes, NULL); |
|
468 WriteFile(outfile, "# connect to the board\r\n", 24, &bytes, NULL); |
|
469 WriteFile(outfile, "$hndl = connect_to_target();\r\n", 30, &bytes, NULL); |
|
470 WriteFile(outfile, "$hndl or goto scripterror;\r\n\r\n", 30, &bytes, NULL); |
|
471 WriteFile(outfile, "# send commands\r\n", 17, &bytes, NULL); |
|
472 } |
|
473 |
|
474 |
|
475 void WriteTrailer(HANDLE outfile) |
|
476 { |
|
477 DWORD bytes = 0; |
|
478 |
|
479 WriteFile(outfile, "\r\n\r\n#########################################################################\r\n", 79, &bytes, NULL); |
|
480 WriteFile(outfile, "# test succeeded\r\n", 18, &bytes, NULL); |
|
481 WriteFile(outfile, "goto endscript;\r\n\r\n\r\n", 21, &bytes, NULL); |
|
482 WriteFile(outfile, "#########################################################################\r\n", 75, &bytes, NULL); |
|
483 WriteFile(outfile, "# fail the test\r\n", 17, &bytes, NULL); |
|
484 WriteFile(outfile, "scripterror:\r\n", 14, &bytes, NULL); |
|
485 WriteFile(outfile, "print \"\\nScript aborted due to error:\\n\" . get_last_error($hndl) . \"\\n\";\r\n", 74, &bytes, NULL); |
|
486 WriteFile(outfile, "$result = 0;\r\n\r\n\r\n", 18, &bytes, NULL); |
|
487 WriteFile(outfile, "#########################################################################\r\n", 75, &bytes, NULL); |
|
488 WriteFile(outfile, "# finish\r\n", 10, &bytes, NULL); |
|
489 WriteFile(outfile, "endscript:\r\n", 12, &bytes, NULL); |
|
490 WriteFile(outfile, "$hndl and disconnect_from_target($hndl) and print \"Disconnected OK\\n\";\r\n\r\n\r\n", 76, &bytes, NULL); |
|
491 WriteFile(outfile, "#########################################################################\r\n", 75, &bytes, NULL); |
|
492 WriteFile(outfile, "# return value (zero=fail, non-zero=success)\r\n", 46, &bytes, NULL); |
|
493 WriteFile(outfile, "exit($result);\r\n", 16, &bytes, NULL); |
|
494 WriteFile(outfile, "\r\n", 2, &bytes, NULL); |
|
495 } |