comparison readpstlog.c @ 0:6b1b602514db libpst_0_5

Initial revision
author carl
date Fri, 09 Jul 2004 07:26:16 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6b1b602514db
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4
5 #ifndef _WIN32
6 # include <unistd.h>
7 #endif
8
9 #ifndef __GNUC__
10 # include "XGetopt.h"
11 #endif
12
13 #include "define.h"
14
15 #define BUF_SIZE 4096
16
17 int usage();
18 size_t get(void * buf, int size, unsigned int count, FILE *fp);
19 int split_args(char *args, int **targ);
20 int is_in(int a, int *b, int c);
21
22 int main(int argc, char** argv) {
23 int *i=NULL, x, ptr, stop=0, flag;
24 char *fname, *buf, format, rec_type;
25 unsigned char version;
26 int *show_type=NULL, show_size=0;
27 int *ex_type=NULL, ex_size=0;
28 unsigned int funcname, filename, text, end, dtype, line, c;
29 FILE *fp;
30 struct _debug_file_rec_m mfile_rec;
31 struct _debug_file_rec_l lfile_rec;
32
33 while ((c = getopt(argc, argv, "f:t:x:")) != -1) {
34 switch(c) {
35 case 'f':
36 // change the output format
37 format = toupper(optarg[0]);
38 break;
39 case 't':
40 //change the type of statements shown
41 show_size = split_args(optarg, &show_type);
42 // type = atoi(optarg);
43 break;
44 case 'x':
45 // change the type of statements excluded
46 ex_size = split_args(optarg, &ex_type);
47 break;
48 }
49 }
50 if (argc > optind) {
51 fname = argv[optind++];
52 } else {
53 usage();
54 exit(2);
55 }
56
57 fp = fopen(fname, "rb");
58 if (fp == NULL) {
59 printf("Error. couldn't open debug file\n");
60 return 2;
61 }
62 if (get(&version, sizeof(char), 1, fp)==0) {
63 printf("Error. could not read version byte from front of file");
64 return 3;
65 }
66
67 if (version > DEBUG_VERSION) {
68 printf("Version number is higher than the format I know about.");
69 return 4;
70 }
71
72 buf = (char*) xmalloc(BUF_SIZE);
73
74 while (!stop) {
75 if (fread(&x, sizeof(int), 1, fp)<=0) {
76 break;
77 }
78 ptr = 0;
79 if (x > 0) {
80 if (i != NULL)
81 free(i);
82 i = (int*)xmalloc(sizeof(int)*(x+1));
83 // plus 1 cause we want to read the offset of the next index
84 if (get(i, sizeof(int), x+1, fp)==0) {
85 // we have reached the end of the debug file
86 printf("oh dear. we must now end\n");
87 break;
88 }
89 while (ptr < x) {
90 fseek(fp,i[ptr++], SEEK_SET);
91 get(&rec_type, 1, sizeof(char), fp);
92 if (rec_type == 'L') {
93 get(&lfile_rec, sizeof(lfile_rec), 1, fp);
94 funcname=lfile_rec.funcname;
95 filename=lfile_rec.filename;
96 text = lfile_rec.text;
97 end = lfile_rec.end;
98 dtype = lfile_rec.type;
99 line = lfile_rec.line;
100 } else if (rec_type == 'M') {
101 get(&mfile_rec, sizeof(mfile_rec), 1, fp);
102 funcname = mfile_rec.funcname;
103 filename = mfile_rec.filename;
104 text = mfile_rec.text;
105 end = mfile_rec.end;
106 dtype = mfile_rec.type;
107 line = mfile_rec.line;
108 }
109 if ((show_type == NULL || is_in(dtype, show_type, show_size))
110 && (ex_type == NULL || !is_in(dtype, ex_type, ex_size))) {
111 c = 0; flag = 0;
112 while (c < end) {
113 if (c + (BUF_SIZE-1) < end) {
114 get(buf, 1, BUF_SIZE-1, fp);
115 buf[BUF_SIZE-1] = '\0';
116 c += BUF_SIZE-1;
117 } else {
118 get(buf, 1, end-c, fp);
119 buf[end-c] = '\0';
120 c = end;
121 }
122 if (flag == 0) {
123 if (format=='T') { // text format
124 printf("%s[%d]: %s", &buf[funcname], line, &buf[text]);
125 } else {
126 printf("Type: %d\nFile[line]: %s[%d]\nFunction:%s\nText:%s", dtype,
127 &buf[filename], line, &buf[funcname], &buf[text]);
128 }
129 flag = 1;
130 } else {
131 printf("%s", buf);
132 }
133 }
134 printf("\n");
135 }
136 }
137 if (fseek(fp, i[ptr], SEEK_SET)==-1) {
138 printf("finished\n");
139 break;
140 }
141 } else {
142 printf("...no more items\n");
143 break;
144 }
145 }
146 free(buf);
147 fclose(fp);
148 return 0;
149 }
150
151 size_t get(void * buf, int size, unsigned int count, FILE *fp) {
152 size_t z;
153 if ((z=fread(buf,size, count, fp)) < count) {
154 printf("Read Failed! (size=%d, count=%d,z=%ld)\n", size, count, (long)z);
155 exit(1);
156 }
157 return z;
158 }
159
160 int usage() {
161 printf("readlog -t[show_type] -x[exclude_type] -f[format] filename\n");
162 printf("\tformat:\n\t\tt: Text log format\n");
163 printf("\tshow_type:\n\t\tcomma separated list of types to show "
164 "[ie, 2,4,1,6]\n");
165 printf("\texclude_type:\n\t\tcomma separated list of types to exclude "
166 "[ie, 1,5,3,7]\n");
167 return 0;
168 }
169
170 int split_args(char *args, int **targ) {
171 int count = 1, *i, x, z;
172 char *tmp = args, *y;
173 if (*targ != NULL) {
174 free(*targ);
175 }
176 // find the number of tokens in the string. Starting
177 // from 1 cause there will always be one
178 while ((tmp = strchr(tmp, ',')) != NULL) {
179 tmp++; count++;
180 }
181 *targ = (int*)xmalloc(count * sizeof(int));
182 i = *targ; // for convienience
183 tmp = args;
184 z = 0;
185 for (x = 0; x < count; x++) {
186 y = strtok(tmp, ",");
187 tmp = NULL; // must be done after first call
188 if (y != NULL) {
189 i[x] = atoi(y);
190 z++;
191 }
192 }
193 return z;
194 }
195
196 // checks to see if the first arg is in the array of the second arg,
197 // the size of which is specified with the third arg. If the second
198 // arg is NULL, then it is obvious that it ain't there.
199 int is_in(int a, int *b, int c){
200 int d = 0;
201 if (b == NULL || c == 0) { // no array or no items in array
202 return 0;
203 }
204 while (d < c) {
205 if (a == b[d])
206 return 1;
207 d++;
208 }
209 return 0;
210 }