Mercurial > libpst
annotate src/readpstlog.c @ 137:d9d4ae17a2c3 stable-0-6-27
fix for const correctness on Fedora 11
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sat, 07 Feb 2009 10:21:42 -0800 |
parents | fc11b1d1ad34 |
children | 6954d315aaa8 |
rev | line source |
---|---|
122
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
118
diff
changeset
|
1 |
48 | 2 #include "define.h" |
3 | |
16 | 4 #define BUF_SIZE 4096 |
5 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
6 void usage(); |
16 | 7 size_t get(void * buf, int size, unsigned int count, FILE *fp); |
8 int split_args(char *args, int **targ); | |
9 int is_in(int a, int *b, int c); | |
10 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
11 int main(int argc, char* const* argv) { |
52 | 12 int identity = 0; |
13 int level = 0; | |
14 int x, ptr, stop=0, flag; | |
15 char *fname, *buf, rec_type; | |
16 unsigned char version; | |
17 int *show_type=NULL, show_size=0; | |
18 int *ex_type=NULL, ex_size=0; | |
70
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
19 unsigned int funcname=0, filename=0, text=0, end=0, dtype=0, line=0; |
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
20 int c; |
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
21 char ch; |
52 | 22 FILE *fp; |
23 struct pst_debug_file_rec_m mfile_rec; | |
24 struct pst_debug_file_rec_l lfile_rec; | |
25 char format = 'D'; // default | |
26 while ((c = getopt(argc, argv, "f:t:x:")) != -1) { | |
70
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
27 ch = c; |
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
28 switch(ch) { |
52 | 29 case 'f': |
30 // change the output format | |
31 format = toupper(optarg[0]); | |
32 break; | |
33 case 't': | |
34 //change the type of statements shown | |
35 show_size = split_args(optarg, &show_type); | |
36 // type = atoi(optarg); | |
37 break; | |
38 case 'x': | |
39 // change the type of statements excluded | |
40 ex_size = split_args(optarg, &ex_type); | |
41 break; | |
42 } | |
43 } | |
44 if (argc > optind) { | |
45 fname = argv[optind++]; | |
46 } else { | |
47 usage(); | |
48 exit(2); | |
49 } | |
16 | 50 |
52 | 51 fp = fopen(fname, "rb"); |
52 if (fp == NULL) { | |
53 printf("Error. couldn't open debug file\n"); | |
54 return 2; | |
55 } | |
56 if (get(&version, sizeof(char), 1, fp)==0) { | |
57 printf("Error. could not read version byte from front of file"); | |
58 return 3; | |
59 } | |
16 | 60 |
52 | 61 if (version > DEBUG_VERSION) { |
62 printf("Version number is higher than the format I know about."); | |
63 return 4; | |
64 } | |
16 | 65 |
52 | 66 buf = (char*) xmalloc(BUF_SIZE); |
16 | 67 |
52 | 68 while (!stop) { |
125
23a36ac0514d
recover dropped pragma pack line, use int64_t rather than off_t to avoid forcing users of the shared library to enable large file support.
Carl Byington <carl@five-ten-sg.com>
parents:
123
diff
changeset
|
69 int64_t temp; |
23a36ac0514d
recover dropped pragma pack line, use int64_t rather than off_t to avoid forcing users of the shared library to enable large file support.
Carl Byington <carl@five-ten-sg.com>
parents:
123
diff
changeset
|
70 if (fread(&temp, sizeof(int64_t), 1, fp)<=0) break; |
48 | 71 x = (int)temp; |
52 | 72 ptr = 0; |
73 if (x > 0) { | |
125
23a36ac0514d
recover dropped pragma pack line, use int64_t rather than off_t to avoid forcing users of the shared library to enable large file support.
Carl Byington <carl@five-ten-sg.com>
parents:
123
diff
changeset
|
74 int64_t i[x+1]; // plus 1 because we want to read the offset of the next index |
23a36ac0514d
recover dropped pragma pack line, use int64_t rather than off_t to avoid forcing users of the shared library to enable large file support.
Carl Byington <carl@five-ten-sg.com>
parents:
123
diff
changeset
|
75 if (get(i, sizeof(int64_t), x+1, fp)==0) { |
52 | 76 // we have reached the end of the debug file |
77 printf("oh dear. we must now end\n"); | |
78 break; | |
79 } | |
80 while (ptr < x) { | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
70
diff
changeset
|
81 fseeko(fp, i[ptr++], SEEK_SET); |
52 | 82 get(&rec_type, 1, sizeof(char), fp); |
83 if (rec_type == 'L') { | |
84 get(&lfile_rec, sizeof(lfile_rec), 1, fp); | |
85 funcname=lfile_rec.funcname; | |
86 filename=lfile_rec.filename; | |
87 text = lfile_rec.text; | |
88 end = lfile_rec.end; | |
89 dtype = lfile_rec.type; | |
90 line = lfile_rec.line; | |
91 } else if (rec_type == 'M') { | |
92 get(&mfile_rec, sizeof(mfile_rec), 1, fp); | |
93 funcname = mfile_rec.funcname; | |
94 filename = mfile_rec.filename; | |
95 text = mfile_rec.text; | |
96 end = mfile_rec.end; | |
97 dtype = mfile_rec.type; | |
98 line = mfile_rec.line; | |
99 } | |
100 if (dtype == DEBUG_FUNCENT_NO) level++; | |
101 if ((show_type == NULL || is_in(dtype, show_type, show_size)) && | |
102 (ex_type == NULL || !is_in(dtype, ex_type, ex_size))) { | |
70
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
103 unsigned int c = 0; |
b12f4e50e2e8
Patch from Joachim Metz <joachim.metz@gmail.com> for 64 bit compile.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
104 flag = 0; |
52 | 105 while (c < end) { |
106 int ii = (level-1) * 4; | |
107 if (ii < 0) ii = 0; | |
108 if (ii > 64) ii = 64; | |
109 char indent[ii+1]; | |
110 memset(indent, ' ', ii); | |
111 indent[ii] = '\0'; | |
112 if (c + (BUF_SIZE-1) < end) { | |
113 get(buf, 1, BUF_SIZE-1, fp); | |
114 buf[BUF_SIZE-1] = '\0'; | |
115 c += BUF_SIZE-1; | |
116 } else { | |
117 get(buf, 1, end-c, fp); | |
118 buf[end-c] = '\0'; | |
119 c = end; | |
120 } | |
121 if (flag == 0) { | |
122 if (format == 'I') { // indented text format | |
123 char *b = buf+text; | |
124 identity++; | |
125 //printf("%s %d %s/%s[%d]: ", indent, identity, &buf[filename], &buf[funcname], line); | |
126 printf("%s %s/%s[%d]: ", indent, &buf[filename], &buf[funcname], line); | |
127 while (b) { | |
128 char *p = strchr(b, '\n'); | |
129 if (p) { | |
130 *p = '\0'; | |
131 printf("%s\n%s ", b, indent); | |
132 b = p + 1; | |
133 } | |
134 else { | |
135 printf("%s", b); | |
136 b = NULL; | |
137 } | |
138 } | |
139 } | |
140 else if (format == 'T') { // text format | |
141 printf("%s/%s[%d]: %s", &buf[filename], &buf[funcname], line, &buf[text]); | |
142 } else { | |
143 printf("Type: %d\nFile[line]: %s[%d]\nFunction:%s\nText:%s", dtype, | |
144 &buf[filename], line, &buf[funcname], &buf[text]); | |
145 } | |
146 flag = 1; | |
147 } else { | |
148 if (format == 'I') { | |
149 char *b = buf; | |
150 while (b) { | |
151 char *p = strchr(b, '\n'); | |
152 if (p) { | |
153 *p = '\0'; | |
154 printf("%s\n%s ", b, indent); | |
155 b = p + 1; | |
156 } | |
157 else { | |
158 printf("%s", b); | |
159 b = NULL; | |
160 } | |
161 } | |
162 } | |
163 else printf("%s", buf); | |
164 } | |
165 } | |
166 printf("\n"); | |
167 } | |
168 if (dtype == DEBUG_FUNCRET_NO) level--; | |
169 } | |
75
987aa872294e
Use ftello/fseeko to properly handle large files.
Carl Byington <carl@five-ten-sg.com>
parents:
70
diff
changeset
|
170 if (fseeko(fp, i[ptr], SEEK_SET)==-1) { |
52 | 171 printf("finished\n"); |
172 break; | |
173 } | |
174 } else { | |
175 printf("...no more items\n"); | |
176 break; | |
177 } | |
178 } | |
179 free(buf); | |
180 fclose(fp); | |
181 if (format == 'I') printf("final indent level = %i\n", level); | |
182 return 0; | |
16 | 183 } |
36 | 184 |
43 | 185 size_t get(void *buf, int size, unsigned int count, FILE *fp) { |
52 | 186 size_t z; |
187 if ((z=fread(buf, size, count, fp)) < count) { | |
188 printf("Read Failed! (size=%d, count=%d,z=%ld)\n", size, count, (long)z); | |
189 exit(1); | |
190 } | |
191 return z; | |
16 | 192 } |
193 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
75
diff
changeset
|
194 void usage() { |
52 | 195 printf("readlog -t[show_type] -x[exclude_type] -f[format] filename\n"); |
196 printf("\tformat:\n\t\tt: text log format\n"); | |
197 printf("\t\ti: indented text log format\n"); | |
198 printf("\tshow_type:\n\t\tcomma separated list of types to show " | |
199 "[ie, 2,4,1,6]\n"); | |
200 printf("\texclude_type:\n\t\tcomma separated list of types to exclude " | |
201 "[ie, 1,5,3,7]\n"); | |
16 | 202 } |
203 | |
36 | 204 |
16 | 205 int split_args(char *args, int **targ) { |
52 | 206 int count = 1, *i, x, z; |
207 char *tmp = args, *y; | |
208 if (*targ != NULL) { | |
209 free(*targ); | |
210 } | |
211 // find the number of tokens in the string. Starting | |
212 // from 1 cause there will always be one | |
213 while ((tmp = strchr(tmp, ',')) != NULL) { | |
214 tmp++; count++; | |
215 } | |
216 *targ = (int*)xmalloc(count * sizeof(int)); | |
217 i = *targ; // for convienience | |
218 tmp = args; | |
219 z = 0; | |
220 for (x = 0; x < count; x++) { | |
221 y = strtok(tmp, ","); | |
222 tmp = NULL; // must be done after first call | |
223 if (y != NULL) { | |
224 i[x] = atoi(y); | |
225 z++; | |
226 } | |
227 } | |
228 return z; | |
16 | 229 } |
230 | |
36 | 231 |
16 | 232 // checks to see if the first arg is in the array of the second arg, |
233 // the size of which is specified with the third arg. If the second | |
36 | 234 // arg is NULL, then it is obvious that it is not there. |
16 | 235 int is_in(int a, int *b, int c){ |
52 | 236 int d = 0; |
237 if (b == NULL || c == 0) { // no array or no items in array | |
238 return 0; | |
239 } | |
240 while (d < c) { | |
241 if (a == b[d]) return 1; | |
242 d++; | |
243 } | |
244 return 0; | |
16 | 245 } |