annotate readpstlog.c @ 2:8dd68d722fa8

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