Fixed: all possible malloc, sprintf and strcpy problems
[profile/ivi/dlt-daemon.git] / src / console / dlt-convert.c
1 /**
2  * @licence app begin@
3  * Copyright (C) 2012  BMW AG
4  *
5  * This file is part of GENIVI Project Dlt - Diagnostic Log and Trace console apps.
6  *
7  * Contributions are licensed to the GENIVI Alliance under one or more
8  * Contribution License Agreements.
9  *
10  * \copyright
11  * This Source Code Form is subject to the terms of the
12  * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
13  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  *
16  * \author Alexander Wenzel <alexander.aw.wenzel@bmw.de> BMW 2011-2012
17  *
18  * \file dlt-convert.c
19  * For further information see http://www.genivi.org/.
20  * @licence end@
21  */
22
23
24 /*******************************************************************************
25 **                                                                            **
26 **  SRC-MODULE: dlt-convert.cpp                                               **
27 **                                                                            **
28 **  TARGET    : linux                                                         **
29 **                                                                            **
30 **  PROJECT   : DLT                                                           **
31 **                                                                            **
32 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
33 **              Markus Klein                                                  **
34 **                                                                            **
35 **  PURPOSE   :                                                               **
36 **                                                                            **
37 **  REMARKS   :                                                               **
38 **                                                                            **
39 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
40 **                                                                            **
41 **  TO BE CHANGED BY USER [yes/no]: no                                        **
42 **                                                                            **
43 *******************************************************************************/
44
45 /*******************************************************************************
46 **                      Author Identity                                       **
47 ********************************************************************************
48 **                                                                            **
49 ** Initials     Name                       Company                            **
50 ** --------     -------------------------  ---------------------------------- **
51 **  aw          Alexander Wenzel           BMW                                **
52 **  mk          Markus Klein               Fraunhofer ESK                     **
53 *******************************************************************************/
54
55 /*******************************************************************************
56 **                      Author Identity                                       **
57 ********************************************************************************
58 **                                                                            **
59 ** Initials     Name                       Company                            **
60 ** --------     -------------------------  ---------------------------------- **
61 **  aw          Alexander Wenzel           BMW                                **
62 *******************************************************************************/
63
64 /*******************************************************************************
65 **                      Revision Control History                              **
66 *******************************************************************************/
67
68 /*
69  * $LastChangedRevision: 1670 $
70  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
71  * $LastChangedBy$
72  Initials    Date         Comment
73  aw          13.01.2010   initial
74  */
75 #include <stdio.h>
76 #include <stdlib.h>
77 #include <unistd.h>
78 #include <string.h>
79 #include <ctype.h>
80
81 #include <sys/stat.h>
82 #include <fcntl.h>
83
84 #include <sys/uio.h> /* writev() */
85
86 #include "dlt_common.h"
87
88 #define DLT_CONVERT_TEXTBUFSIZE  10024   /* Size of buffer for text output */
89
90 /**
91  * Print usage information of tool.
92  */
93 void usage()
94 {
95     char version[DLT_CONVERT_TEXTBUFSIZE];
96
97     dlt_get_version(version,255);
98
99     printf("Usage: dlt-convert [options] [commands] file1 [file2]\n");
100     printf("Read DLT files, print DLT messages as ASCII and store the messages again.\n");
101     printf("Use filters to filter DLT messages.\n");
102     printf("Use Ranges and Output file to cut DLT files.\n");
103     printf("Use two files and Output file to join DLT files.\n");
104     printf("%s \n", version);
105     printf("Commands:\n");
106     printf("  -h            Usage\n");
107     printf("  -a            Print DLT file; payload as ASCII\n");
108     printf("  -x            Print DLT file; payload as hex\n");
109     printf("  -m            Print DLT file; payload as hex and ASCII\n");
110     printf("  -s            Print DLT file; only headers\n");
111     printf("  -o filename   Output messages in new DLT file\n");
112     printf("Options:\n");
113     printf("  -v            Verbose mode\n");
114     printf("  -c            Count number of messages\n");
115     printf("  -f filename   Enable filtering of messages\n");
116     printf("  -b number     First messages to be handled\n");
117     printf("  -e number     Last message to be handled\n");
118     printf("  -w            Follow dlt file while file is increasing\n");
119 }
120
121 /**
122  * Main function of tool.
123  */
124 int main(int argc, char* argv[])
125 {
126     int vflag = 0;
127     int cflag = 0;
128     int aflag = 0;
129     int sflag = 0;
130     int xflag = 0;
131     int mflag = 0;
132     int wflag = 0;
133     char *fvalue = 0;
134     char *bvalue = 0;
135     char *evalue = 0;
136     char *ovalue = 0;
137
138     int index;
139     int c;
140
141         DltFile file;
142         DltFilter filter;
143
144         int ohandle=-1;
145
146         int num, begin, end;
147
148         char text[DLT_CONVERT_TEXTBUFSIZE];
149
150         struct iovec iov[2];
151     int bytes_written;
152
153     opterr = 0;
154
155     while ((c = getopt (argc, argv, "vcashxmwf:b:e:o:")) != -1)
156         switch (c)
157         {
158         case 'v':
159                         {
160                 vflag = 1;
161                 break;
162                         }
163         case 'c':
164                         {
165                 cflag = 1;
166                 break;
167                         }
168         case 'a':
169                         {
170                 aflag = 1;
171                 break;
172                         }
173         case 's':
174                         {
175                 sflag = 1;
176                 break;
177                         }
178         case 'x':
179                         {
180                 xflag = 1;
181                 break;
182                         }
183         case 'm':
184                         {
185                 mflag = 1;
186                 break;
187                         }
188         case 'w':
189                         {
190                 wflag = 1;
191                 break;
192                         }
193         case 'h':
194                         {
195                 usage();
196                 return -1;
197                         }
198         case 'f':
199                         {
200                 fvalue = optarg;
201                 break;
202                         }
203         case 'b':
204                         {
205                 bvalue = optarg;
206                 break;
207                         }
208         case 'e':
209                         {
210                 evalue = optarg;
211                 break;
212                         }
213         case 'o':
214                         {
215                 ovalue = optarg;
216                 break;
217                         }
218         case '?':
219                         {
220                         if (optopt == 'f' || optopt == 'b' || optopt == 'e' || optopt == 'o')
221                                 {
222                             fprintf (stderr, "Option -%c requires an argument.\n", optopt);
223                                 }
224                                 else if (isprint (optopt))
225                                 {
226                             fprintf (stderr, "Unknown option `-%c'.\n", optopt);
227                                 }
228                                 else
229                                 {
230                             fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
231                                 }
232                         /* unknown or wrong option used, show usage information and terminate */
233                         usage();
234                         return -1;
235                         }
236         default:
237                         {
238                 abort();
239                 return -1;//for parasoft
240                         }
241         }
242
243     /* initialise structure to use DLT file */
244     dlt_file_init(&file,vflag);
245
246     /* first parse filter file if filter parameter is used */
247     if (fvalue)
248     {
249         if (dlt_filter_load(&filter,fvalue,vflag)<0)
250         {
251             dlt_file_free(&file,vflag);
252             return -1;
253         }
254
255         dlt_file_set_filter(&file,&filter,vflag);
256     }
257
258     if (ovalue)
259     {
260         ohandle = open(ovalue,O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* mode: wb */
261         if (ohandle == -1)
262         {
263             dlt_file_free(&file,vflag);
264             fprintf(stderr,"ERROR: Output file %s cannot be opened!\n",ovalue);
265             return -1;
266         }
267
268     }
269
270     for (index = optind; index < argc; index++)
271     {
272         /* load, analyse data file and create index list */
273         if (dlt_file_open(&file,argv[index],vflag)>=0)
274         {
275             while (dlt_file_read(&file,vflag)>=0)
276             {
277             }
278         }
279
280         if (aflag || sflag || xflag || mflag || ovalue)
281         {
282             if (bvalue)
283                         {
284                 begin = atoi(bvalue);
285             }
286                         else
287                         {
288                 begin = 0;
289                         }
290
291             if (evalue && (wflag==0))
292                         {
293                 end = atoi(evalue);
294             }
295                         else
296                         {
297                 end = file.counter-1;
298                         }
299
300             if (begin<0 || begin>=file.counter)
301             {
302                 fprintf(stderr,"ERROR: Selected first message %d is out of range!\n",begin);
303                 return -1;
304             }
305             if (end<0 || end>=file.counter || end<begin)
306             {
307                 fprintf(stderr,"ERROR: Selected end message %d is out of range!\n",end);
308                 return -1;
309             }
310             for (num = begin; num <= end ;num++)
311             {
312                 dlt_file_message(&file,num,vflag);
313
314                 if (xflag)
315                 {
316                     printf("%d ",num);
317                     dlt_message_print_hex(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
318                 }
319                 else if (aflag)
320                 {
321                     printf("%d ",num);
322
323                     dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
324
325                     printf("%s ",text);
326
327                     dlt_message_payload(&file.msg,text,DLT_CONVERT_TEXTBUFSIZE,DLT_OUTPUT_ASCII,vflag);
328
329                     printf("[%s]\n",text);
330                 }
331                 else if (mflag)
332                 {
333                     printf("%d ",num);
334                     dlt_message_print_mixed_plain(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
335                 }
336                 else if (sflag)
337                 {
338                     printf("%d ",num);
339
340                     dlt_message_header(&(file.msg),text,DLT_CONVERT_TEXTBUFSIZE,vflag);
341
342                     printf("%s \n",text);
343                 }
344
345                 /* if file output enabled write message */
346                 if (ovalue)
347                 {
348                     iov[0].iov_base = file.msg.headerbuffer;
349                     iov[0].iov_len = file.msg.headersize;
350                     iov[1].iov_base = file.msg.databuffer;
351                     iov[1].iov_len = file.msg.datasize;
352
353                     bytes_written = writev(ohandle, iov, 2);
354                     if (0 > bytes_written){
355                             printf("in main: writev(ohandle, iov, 2); returned an error!" );
356                             dlt_file_free(&file,vflag);
357                             return -1;
358                     }
359                 }
360
361                 /* check for new messages if follow flag set */
362                 if (wflag && num==end)
363                 {
364                     while (1)
365                     {
366                         while (dlt_file_read(&file,0)>=0)
367                         {
368                         }
369                         if (end == (file.counter-1))
370                                                 {
371                             /* Sleep if no new message was received */
372                             sleep(1);
373                         }
374                                                 else
375                         {
376                             /* set new end of log file and continue reading */
377                             end = file.counter-1;
378                             break;
379                         }
380                     }
381                 }
382             }
383         }
384         if (cflag)
385         {
386             printf("Total number of messages: %d\n",file.counter_total);
387             if (file.filter)
388                         {
389                 printf("Filtered number of messages: %d\n",file.counter);
390                         }
391         }
392     }
393     if (ovalue)
394     {
395         close(ohandle);
396     }
397     if (index == optind)
398     {
399         /* no file selected, show usage and terminate */
400         fprintf(stderr,"ERROR: No file selected\n");
401         usage();
402         return -1;
403     }
404
405     dlt_file_free(&file,vflag);
406
407     return 0;
408 }