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