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