Mercurial > libpst
annotate src/lspst.c @ 133:2ec889a79e51
patch from Fridrich Strba, some systems need extra library for regex
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Sat, 07 Feb 2009 07:57:32 -0800 |
parents | fc11b1d1ad34 |
children | fdc58ad2c758 |
rev | line source |
---|---|
16 | 1 /*** |
2 * lspst.c | |
3 * Part of the LibPST project | |
4 * Author: Joe Nahmias <joe@nahmias.net> | |
5 * Based on readpst.c by by David Smith <dave.s@earthcorp.com> | |
6 * | |
7 */ | |
8 | |
122
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
120
diff
changeset
|
9 #include "define.h" |
bdb38b434c0a
more changes from Fridrich Strba to avoid installing our config.h
Carl Byington <carl@five-ten-sg.com>
parents:
120
diff
changeset
|
10 |
16 | 11 struct file_ll { |
43 | 12 char *dname; |
13 int32_t stored_count; | |
14 int32_t email_count; | |
15 int32_t skip_count; | |
16 int32_t type; | |
16 | 17 }; |
43 | 18 |
19 | |
16 | 20 void canonicalize_filename(char *fname); |
21 void debug_print(char *fmt, ...); | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
22 void usage(char *prog_name); |
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
23 void version(); |
43 | 24 |
25 // global settings | |
26 pst_file pstfile; | |
27 | |
16 | 28 |
43 | 29 void create_enter_dir(struct file_ll* f, pst_item *item) |
30 { | |
31 f->email_count = 0; | |
32 f->skip_count = 0; | |
33 f->type = item->type; | |
34 f->stored_count = (item->folder) ? item->folder->email_count : 0; | |
35 f->dname = (char*) xmalloc(strlen(item->file_as)+1); | |
36 strcpy(f->dname, item->file_as); | |
37 } | |
16 | 38 |
39 | |
43 | 40 void close_enter_dir(struct file_ll *f) |
41 { | |
42 free(f->dname); | |
43 } | |
16 | 44 |
45 | |
43 | 46 void process(pst_item *outeritem, pst_desc_ll *d_ptr) |
47 { | |
48 struct file_ll ff; | |
49 pst_item *item = NULL; | |
50 | |
51 DEBUG_ENT("process"); | |
52 memset(&ff, 0, sizeof(ff)); | |
53 create_enter_dir(&ff, outeritem); | |
16 | 54 |
43 | 55 while (d_ptr) { |
56 DEBUG_MAIN(("main: New item record, d_ptr = %p.\n", d_ptr)); | |
57 if (!d_ptr->desc) { | |
58 DEBUG_WARN(("main: ERROR ?? item's desc record is NULL\n")); | |
59 ff.skip_count++; | |
60 } | |
61 else { | |
62 DEBUG_MAIN(("main: Desc Email ID %x [d_ptr->id = %x]\n", d_ptr->desc->id, d_ptr->id)); | |
16 | 63 |
46 | 64 item = pst_parse_item(&pstfile, d_ptr); |
43 | 65 DEBUG_MAIN(("main: About to process item @ %p.\n", item)); |
66 if (item) { | |
67 if (item->message_store) { | |
68 // there should only be one message_store, and we have already done it | |
69 DIE(("main: A second message_store has been found. Sorry, this must be an error.\n")); | |
70 } | |
16 | 71 |
43 | 72 if (item->folder && d_ptr->child) { |
73 // if this is a folder, we want to recurse into it | |
74 printf("Folder \"%s\"\n", item->file_as); | |
75 process(item, d_ptr->child); | |
16 | 76 |
43 | 77 } else if (item->contact && (item->type == PST_TYPE_CONTACT)) { |
78 // Process Contact item | |
79 if (ff.type != PST_TYPE_CONTACT) { | |
80 DEBUG_MAIN(("main: I have a contact, but the folder isn't a contacts folder. Processing anyway\n")); | |
81 } | |
82 printf("Contact"); | |
50 | 83 if (item->contact->fullname) |
43 | 84 printf("\t%s", pst_rfc2426_escape(item->contact->fullname)); |
85 printf("\n"); | |
16 | 86 |
110
7133b39975f7
patch from David Cuadrado to process emails with type PST_TYPE_OTHER
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
87 } else if (item->email && (item->type == PST_TYPE_NOTE || item->type == PST_TYPE_REPORT || item->type == PST_TYPE_OTHER)) { |
43 | 88 // Process Email item |
110
7133b39975f7
patch from David Cuadrado to process emails with type PST_TYPE_OTHER
Carl Byington <carl@five-ten-sg.com>
parents:
73
diff
changeset
|
89 if ((ff.type != PST_TYPE_NOTE) && (ff.type != PST_TYPE_REPORT) && (ff.type != PST_TYPE_OTHER)) { |
43 | 90 DEBUG_MAIN(("main: I have an email, but the folder isn't an email folder. Processing anyway\n")); |
91 } | |
92 printf("Email"); | |
50 | 93 if (item->email->outlook_sender_name) |
43 | 94 printf("\tFrom: %s", item->email->outlook_sender_name); |
50 | 95 if (item->email->subject && item->email->subject->subj) |
43 | 96 printf("\tSubject: %s", item->email->subject->subj); |
97 printf("\n"); | |
16 | 98 |
43 | 99 } else if (item->journal && (item->type == PST_TYPE_JOURNAL)) { |
100 // Process Journal item | |
101 if (ff.type != PST_TYPE_JOURNAL) { | |
102 DEBUG_MAIN(("main: I have a journal entry, but folder isn't specified as a journal type. Processing...\n")); | |
103 } | |
50 | 104 if (item->email && item->email->subject && item->email->subject->subj) |
105 printf("Journal\t%s\n", pst_rfc2426_escape(item->email->subject->subj)); | |
16 | 106 |
43 | 107 } else if (item->appointment && (item->type == PST_TYPE_APPOINTMENT)) { |
108 // Process Calendar Appointment item | |
109 DEBUG_MAIN(("main: Processing Appointment Entry\n")); | |
110 if (ff.type != PST_TYPE_APPOINTMENT) { | |
111 DEBUG_MAIN(("main: I have an appointment, but folder isn't specified as an appointment type. Processing...\n")); | |
112 } | |
113 printf("Appointment"); | |
50 | 114 if (item->email && item->email->subject) |
43 | 115 printf("\tSUMMARY: %s", pst_rfc2426_escape(item->email->subject->subj)); |
50 | 116 if (item->appointment->start) |
117 printf("\tSTART: %s", pst_rfc2445_datetime_format(item->appointment->start)); | |
118 if (item->appointment->end) | |
119 printf("\tEND: %s", pst_rfc2445_datetime_format(item->appointment->end)); | |
120 printf("\tALL DAY: %s", (item->appointment->all_day==1 ? "Yes" : "No")); | |
43 | 121 printf("\n"); |
16 | 122 |
43 | 123 } else { |
124 ff.skip_count++; | |
125 DEBUG_MAIN(("main: Unknown item type. %i. Ascii1=\"%s\"\n", | |
126 item->type, item->ascii_type)); | |
127 } | |
46 | 128 pst_freeItem(item); |
43 | 129 } else { |
130 ff.skip_count++; | |
131 DEBUG_MAIN(("main: A NULL item was seen\n")); | |
132 } | |
133 d_ptr = d_ptr->next; | |
134 } | |
135 } | |
136 close_enter_dir(&ff); | |
52 | 137 DEBUG_RET(); |
43 | 138 } |
16 | 139 |
43 | 140 |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
141 void usage(char *prog_name) { |
50 | 142 DEBUG_ENT("usage"); |
143 version(); | |
144 printf("Usage: %s [OPTIONS] {PST FILENAME}\n", prog_name); | |
145 printf("OPTIONS:\n"); | |
146 printf("\t-d <filename> \t- Debug to file. This is a binary log. Use readlog to print it\n"); | |
147 printf("\t-h\t- Help. This screen\n"); | |
148 printf("\t-V\t- Version. Display program version\n"); | |
149 DEBUG_RET(); | |
150 } | |
151 | |
152 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
153 void version() { |
50 | 154 DEBUG_ENT("version"); |
155 printf("lspst / LibPST v%s\n", VERSION); | |
156 #if BYTE_ORDER == BIG_ENDIAN | |
157 printf("Big Endian implementation being used.\n"); | |
158 #elif BYTE_ORDER == LITTLE_ENDIAN | |
159 printf("Little Endian implementation being used.\n"); | |
160 #else | |
161 # error "Byte order not supported by this library" | |
162 #endif | |
163 #ifdef __GNUC__ | |
164 printf("GCC %d.%d : %s %s\n", __GNUC__, __GNUC_MINOR__, __DATE__, __TIME__); | |
165 #endif | |
166 DEBUG_RET(); | |
167 } | |
168 | |
169 | |
118
0f1492b7fe8b
patch from Fridrich Strba for building on mingw and general cleanup of autoconf files
Carl Byington <carl@five-ten-sg.com>
parents:
110
diff
changeset
|
170 int main(int argc, char* const* argv) { |
43 | 171 pst_item *item = NULL; |
172 pst_desc_ll *d_ptr; | |
173 char *temp = NULL; //temporary char pointer | |
50 | 174 int c; |
43 | 175 char *d_log = NULL; |
16 | 176 |
50 | 177 while ((c = getopt(argc, argv, "d:hV"))!= -1) { |
178 switch (c) { | |
179 case 'd': | |
180 d_log = optarg; | |
181 break; | |
182 case 'h': | |
183 usage(argv[0]); | |
184 exit(0); | |
185 break; | |
186 case 'V': | |
187 version(); | |
188 exit(0); | |
189 break; | |
190 default: | |
191 usage(argv[0]); | |
192 exit(1); | |
193 break; | |
194 } | |
195 } | |
43 | 196 |
48 | 197 #ifdef DEBUG_ALL |
198 // force a log file | |
199 if (!d_log) d_log = "lspst.log"; | |
200 #endif // defined DEBUG_ALL | |
43 | 201 DEBUG_INIT(d_log); |
202 DEBUG_REGISTER_CLOSE(); | |
203 DEBUG_ENT("main"); | |
204 | |
50 | 205 if (argc <= optind) { |
206 usage(argv[0]); | |
207 exit(2); | |
208 } | |
209 | |
43 | 210 // Open PST file |
59
7d5c637aaafb
General cleanup and code fixes.
Carl Byington <carl@five-ten-sg.com>
parents:
52
diff
changeset
|
211 if (pst_open(&pstfile, argv[optind])) DIE(("Error opening File\n")); |
16 | 212 |
43 | 213 // Load PST index |
214 if (pst_load_index(&pstfile)) DIE(("Index Error\n")); | |
215 | |
216 pst_load_extended_attributes(&pstfile); | |
16 | 217 |
43 | 218 d_ptr = pstfile.d_head; // first record is main record |
46 | 219 item = pst_parse_item(&pstfile, d_ptr); |
43 | 220 if (!item || !item->message_store) { |
221 DEBUG_RET(); | |
222 DIE(("main: Could not get root record\n")); | |
223 } | |
16 | 224 |
43 | 225 // default the file_as to the same as the main filename if it doesn't exist |
226 if (!item->file_as) { | |
227 if (!(temp = strrchr(argv[1], '/'))) | |
228 if (!(temp = strrchr(argv[1], '\\'))) | |
229 temp = argv[1]; | |
230 else | |
231 temp++; // get past the "\\" | |
232 else | |
233 temp++; // get past the "/" | |
234 item->file_as = (char*)xmalloc(strlen(temp)+1); | |
235 strcpy(item->file_as, temp); | |
236 } | |
237 fprintf(stderr, "item->file_as = '%s'.\n", item->file_as); | |
16 | 238 |
43 | 239 d_ptr = pst_getTopOfFolders(&pstfile, item); |
240 if (!d_ptr) DIE(("Top of folders record not found. Cannot continue\n")); | |
241 DEBUG_MAIN(("d_ptr(TOF) = %p.\n", d_ptr)); | |
16 | 242 |
43 | 243 process(item, d_ptr->child); // do the childred of TOPF |
46 | 244 pst_freeItem(item); |
43 | 245 pst_close(&pstfile); |
16 | 246 |
43 | 247 DEBUG_RET(); |
248 return 0; | |
16 | 249 } |
43 | 250 |
251 | |
252 // This function will make sure that a filename is in cannonical form. That | |
16 | 253 // is, it will replace any slashes, backslashes, or colons with underscores. |
254 void canonicalize_filename(char *fname) { | |
43 | 255 DEBUG_ENT("canonicalize_filename"); |
256 if (fname == NULL) { | |
257 DEBUG_RET(); | |
258 return; | |
259 } | |
73
3cb02cb1e6cd
Patch from Robert Simpson to fix doubly-linked list in the cache_ptr code, and allow arrays of unicode strings (without converting them).
Carl Byington <carl@five-ten-sg.com>
parents:
59
diff
changeset
|
260 while ((fname = strpbrk(fname, "/\\:"))) |
43 | 261 *fname = '_'; |
262 DEBUG_RET(); | |
16 | 263 } |
43 | 264 |
265 | |
16 | 266 void debug_print(char *fmt, ...) { |
43 | 267 // shamlessly stolen from minprintf() in K&R pg. 156 |
268 va_list ap; | |
269 char *p, *sval; | |
270 void *pval; | |
271 int ival; | |
272 double dval; | |
273 FILE *fp = stderr; | |
16 | 274 |
43 | 275 va_start(ap, fmt); |
276 for(p = fmt; *p; p++) { | |
277 if (*p != '%') { | |
278 fputc(*p, fp); | |
279 continue; | |
280 } | |
281 switch (tolower(*++p)) { | |
282 case 'd': case 'i': | |
283 ival = va_arg(ap, int); | |
284 fprintf(fp, "%d", ival); | |
285 break; | |
286 case 'f': | |
287 dval = va_arg(ap, double); | |
288 fprintf(fp, "%f", dval); | |
289 break; | |
290 case 's': | |
291 for (sval = va_arg(ap, char *); *sval; ++sval) | |
292 fputc(*sval, fp); | |
293 break; | |
294 case 'p': | |
295 pval = va_arg(ap, void *); | |
296 fprintf(fp, "%p", pval); | |
297 break; | |
298 case 'x': | |
299 ival = va_arg(ap, int); | |
300 fprintf(fp, "%#010x", ival); | |
301 break; | |
302 default: | |
303 fputc(*p, fp); | |
304 break; | |
305 } | |
306 } | |
307 va_end(ap); | |
16 | 308 } |
309 | |
310 |