File: wvHtml.c

package info (click to toggle)
abiword 0.7.7-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 20,604 kB
  • ctags: 18,358
  • sloc: cpp: 88,791; ansic: 66,296; sh: 7,777; makefile: 3,397; xml: 687; perl: 361; awk: 273; sed: 36; csh: 28
file content (576 lines) | stat: -rw-r--r-- 13,358 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "wv.h"
/*
Released under GPL, written by Caolan.McNamara@ul.ie.

Copyright (C) 1998,1999 
	Caolan McNamara

Real Life: Caolan McNamara           *  Doing: MSc in HCI
Work: Caolan.McNamara@ul.ie          *  Phone: +353-86-8790257
URL: http://skynet.csn.ul.ie/~caolan *  Sig: an oblique strategy
How would you have done it?
*/

/*
returns 1 for not an ole doc
2 ole but not word doc
-1 for an error of some unknown kind
0 on success
*/

int myelehandler(wvParseStruct *ps,wvTag tag, void *props, int dirty);
int mydochandler(wvParseStruct *ps,wvTag tag);
int myCharProc(wvParseStruct *ps,U16 eachchar,U8 chartype,U16 lid);
int mySpecCharProc(wvParseStruct *ps,U16 eachchar,CHP *achp);

FILE *wvOpenConfig(char *config);

void usage( void )
	{
	printf("Usage: wvHtml [--config config.xml] [--charset charset] [--password password] [--dir dir] filename.doc\n");
	exit(-1);
	}

char *charset=NULL;

int main(int argc,char **argv)
	{
	FILE *input;
	char *password=NULL;
	char *config=NULL;
	char *dir=NULL;
	int ret;
	state_data myhandle;
	expand_data expandhandle;
	wvParseStruct ps;
	int c,index=0;
	static struct option long_options[] =
        {
        {"charset",1,0,'c'},
        {"config",1,0,'x'},
        {"password",1,0,'p'},
        {"dir",1,0,'d'},
        {0,0,0,0}
        };

	wvInitError();

	if (argc < 2) 
		usage();

	 while (1)
        { 
        c = getopt_long (argc, argv, "c:x:p:d:", long_options, &index);
        if (c == -1)
            break;
		switch(c)
			{
			case 'c':
				if (optarg)
					charset = optarg;
				else
					wvError(("No argument given to charset"));
				break;
			case 'x':
				if (optarg)
					config = optarg;
				else
					wvError(("No config file given to config option"));
				break;
			case 'p':
				if (optarg)
					password = optarg;
				else
					wvError(("No password given to password option"));
				break;
			case 'd':
				if (optarg)
					dir = optarg;
				else
					wvError(("No directory given to dir option"));
				break;
			default:
                usage();
                break;
			}
		}

	input = fopen(argv[optind],"rb");
	if (!input)
		{
		fprintf(stderr,"Failed to open %s: %s\n",argv[optind],strerror(errno));
		return(-1);
		}

	ret = wvInitParser(&ps,input);
	ps.filename = argv[optind];
	ps.dir = dir;

	if (ret & 0x8000)
		{
		if ( (ret & 0x7fff) == WORD8)
			{
			ret = 0;
			if (password == NULL)
				{
				fprintf(stderr,"Password required, this is an encrypted document\n");
				return(-1);
				}
			else
				{
				wvSetPassword(password,&ps);
				if (wvDecrypt97(&ps))
					{
					wvError(("Incorrect Password\n"));
					return(-1);
					}
				}
			}
		else if ( ( (ret & 0x7fff) == WORD7) || ( (ret & 0x7fff) == WORD6))
			{
			ret=0;
			if (password == NULL)
				{
				fprintf(stderr,"Password required, this is an encrypted document\n");
				return(-1);
				}
			else
				{
				wvSetPassword(password,&ps);
				if (wvDecrypt95(&ps))
					{
					wvError(("Incorrect Password\n"));
					return(-1);
					}
				}
			}
		}

	if (ret)
		{
		wvError(("startup error\n"));
		wvOLEFree();
		return(-1);
		}

	wvSetElementHandler(myelehandler);
	wvSetDocumentHandler(mydochandler);
	wvSetCharHandler(myCharProc);
	wvSetSpecialCharHandler(mySpecCharProc);

	wvInitStateData(&myhandle);
    myhandle.fp = wvOpenConfig(config);
    if (myhandle.fp == NULL)
		{
        wvError(("config file not found\n"));
		return(-1);
		}
    else
		{
		wvTrace(("x for FILE is %x\n",myhandle.fp));
        ret = wvParseConfig(&myhandle);
		}

	if (!ret)	
		{
		expandhandle.sd = &myhandle;
		ps.userData = &expandhandle;
		ret = wvHtml(&ps);
		}
	wvReleaseStateData(&myhandle);

	if (ret == 2)
		return(2);
	else if (ret != 0)
		ret = -1;
	wvOLEFree();
	return(ret);
	}

int myelehandler(wvParseStruct *ps,wvTag tag, void *props, int dirty)
    {
	static PAP *ppap;
    expand_data *data = (expand_data *)ps->userData;
    data->anSttbfAssoc = &ps->anSttbfAssoc;
	data->lfo = &ps->lfo;
	data->lfolvl = ps->lfolvl;
	data->lvl = ps->lvl;
	data->nolfo = &ps->nolfo;
	data->nooflvl = &ps->nooflvl;
	data->stsh = &ps->stsh;
	data->lst = &ps->lst;
	data->noofLST = &ps->noofLST;
	data->liststartnos = &ps->liststartnos;
	data->listnfcs = &ps->listnfcs;
	data->finallvl = &ps->finallvl;
	data->fib = &ps->fib;
	data->intable = &ps->intable;
	data->cellbounds = &ps->cellbounds;
	data->nocellbounds = &ps->nocellbounds;
	data->endcell = &ps->endcell;
	data->vmerges = &ps->vmerges;
	data->norows = &ps->norows;
	data->nextpap = &ps->nextpap;
	if (charset == NULL)
		{
    	data->charset = wvAutoCharset(ps);
		charset=data->charset;
		}
	else
		data->charset = charset;
    data->props = props;

    switch (tag)
        {
        case PARABEGIN:
			{
            S16 tilfo=0;
            /* test begin */
            if (*(data->endcell))
                {
                tilfo = ((PAP *)(data->props))->ilfo;
                ((PAP *)(data->props))->ilfo=0;
                }
            /* test end */
			ppap = (PAP *)data->props;
			wvTrace(("fore back is %d %d\n",((PAP *)(data->props))->shd.icoFore,((PAP *)(data->props))->shd.icoBack));
            wvBeginPara(data);
			if (tilfo)
                ((PAP *)(data->props))->ilfo=tilfo;
            }
            break;
        case PARAEND:
			{
			S16 tilfo=0;
            /* test begin */
            if (*(data->endcell))
                {
                tilfo = ((PAP *)(data->props))->ilfo;
                ((PAP *)(data->props))->ilfo=0;
                }
            /* test end */
            wvEndCharProp(data);	/* danger will break in the future */
            wvEndPara(data);
			if (tilfo)
                ((PAP *)(data->props))->ilfo=tilfo;
			wvCopyPAP(&data->lastpap,(PAP*)(data->props));
			}
            break;
        case CHARPROPBEGIN:
            wvBeginCharProp(data,ppap);
            break;
        case CHARPROPEND:
            wvEndCharProp(data);
            break;
		case SECTIONBEGIN:
			wvBeginSection(data);
			break;
		case SECTIONEND:
			wvEndSection(data);
			break;
		case COMMENTBEGIN:
			wvBeginComment(data);
			break;
		case COMMENTEND:
			wvEndComment(data);
			break;
        default:
            break;
        }
    return(0);
    }

int mydochandler(wvParseStruct *ps,wvTag tag)
    {
	static int i;
    expand_data *data = (expand_data *)ps->userData;
    data->anSttbfAssoc = &ps->anSttbfAssoc;
	data->lfo = &ps->lfo;
	data->lfolvl = ps->lfolvl;
	data->lvl = ps->lvl;
	data->nolfo = &ps->nolfo;
	data->nooflvl = &ps->nooflvl;
	data->stsh = &ps->stsh;
	data->lst = &ps->lst;
	data->noofLST = &ps->noofLST;
	data->liststartnos = &ps->liststartnos;
	data->listnfcs = &ps->listnfcs;
	data->finallvl = &ps->finallvl;
	data->fib = &ps->fib;
	data->intable = &ps->intable;
	data->cellbounds = &ps->cellbounds;
	data->nocellbounds = &ps->nocellbounds;
	data->endcell = &ps->endcell;
	data->vmerges = &ps->vmerges;
	data->norows = &ps->norows;
	if (i==0)
		{
		data->filename = ps->filename;
		data->whichcell=0;
		data->whichrow=0;
		data->asep = NULL;
		i++;
		wvInitPAP(&data->lastpap);
		data->nextpap=NULL;
		data->ps=ps;
		}

	if (charset == NULL)
		{
	    data->charset = wvAutoCharset(ps);
		charset = data->charset;
		}
	else
		data->charset = charset;

    switch (tag)
        {
        case DOCBEGIN:
            wvBeginDocument(data);
            break;
        case DOCEND:
            wvEndDocument(data);
            break;
        default:
            break;
        }
    return(0);
    }


int mySpecCharProc(wvParseStruct *ps,U16 eachchar,CHP *achp)
	{
	static int message,state;
	PICF picf;
	FSPA *fspa;
    expand_data *data = (expand_data *)ps->userData;

	switch(eachchar)
		{
		case 19:
			wvTrace(("field began\n"));
			ps->fieldstate++;
			ps->fieldmiddle=0;
			fieldCharProc(ps,eachchar,0,0x400);	/* temp */
			return(0);
			break;
		case 20:
			wvTrace(("field middle\n"));
			fieldCharProc(ps,eachchar,0,0x400);	/* temp */
			ps->fieldmiddle=1;
			return(0);
			break;
		case 21:
			wvTrace(("field end\n"));
			ps->fieldstate--;
			ps->fieldmiddle=0;
			fieldCharProc(ps,eachchar,0,0x400);	/* temp */
			return(0);
			break;
		}

	if (ps->fieldstate)
		{
		if (fieldCharProc(ps,eachchar,0,0x400))
			return(0);
		}

	switch(eachchar)
		{
		case 0x05:
			/* this should be handled by the COMMENTBEGIN and COMMENTEND events */
			return(0);
			break;
		case 0x01:
			{
			FILE *f;
			Blip blip;
			char *name;
			long p = ftell(ps->data);
			wvError(("picture 0x01 here, at offset %x in Data Stream, obj is %d, ole is %d\n",achp->fcPic_fcObj_lTagObj,achp->fObj,achp->fOle2));
			if (achp->fOle2)
				exit(139);
			fseek(ps->data,achp->fcPic_fcObj_lTagObj,SEEK_SET);
			wvGetPICF(wvQuerySupported(&ps->fib,NULL),&picf,ps->data);
			f = (FILE *)picf.rgb;
			if (wv0x01(&blip,f,picf.lcb-picf.cbHeader))
				{
				wvTrace(("Here\n"));
				name = wvHtmlGraphic(ps,&blip);
				printf("<img alt=\"0x01 graphic\" src=\"%s\"><br>",name);
				wvFree(name);
				}
			else
				{
				wvError(("Strange No Graphic Data in the 0x01 graphic\n"));
				printf("<img alt=\"0x01 graphic\" src=\"%s\"><br>","StrangeNoGraphicData");
				}
			fseek(ps->data,p,SEEK_SET);
			return(0);
			}
		case 0x08:
			{
			Blip blip;
			char *name;
			if (wvQuerySupported(&ps->fib,NULL) == WORD8)
				{
				fspa = wvGetFSPAFromCP(ps->currentcp,ps->fspa,ps->fspapos,ps->nooffspa);
				data->props = fspa;
				if (wv0x08(&blip,fspa->spid,ps))
					{
					wvTrace(("Here\n"));
					name = wvHtmlGraphic(ps,&blip);
					printf("<img alt=\"0x08 graphic\" src=\"%s\"><br>",name);
					wvFree(name);
					}
				else
					{
					wvError(("Strange No Graphic Data in the 0x01 graphic\n"));
					printf("<img alt=\"0x08 graphic\" src=\"%s\"><br>","StrangeNoGraphicData");
					}
				}
			else
				{
				FDOA *fdoa;
				wvError(("pre word8 0x08 graphic, unsupported at the moment\n"));
				fdoa = wvGetFDOAFromCP(ps->currentcp,ps->fdoa,ps->fdoapos,ps->nooffdoa);
				data->props = fdoa;
				}





#if 0
			if ( (fspa) && (data->sd != NULL) && (data->sd->elements[TT_PICTURE].str) && (data->sd->elements[TT_PICTURE].str[0] != NULL) )
				{
				wvExpand(data,data->sd->elements[TT_PICTURE].str[0],
					strlen(data->sd->elements[TT_PICTURE].str[0]));
				if (data->retstring)
					{
					wvTrace(("picture string is now %s",data->retstring));
					printf("%s",data->retstring);
					wvFree(data->retstring);
					}
				}
#endif
			return(0);
			}
		case 0x28:
			{
			U16 symbol[6] = {'S','y','m','b','o','l'};
			U16 wingdings[9] = {'W','i','n','g','d','i','n','g','s'};
			wvTrace(("no of strings %d %d\n",ps->fonts.nostrings,achp->ftcSym));
			if (0 == memcmp(symbol,ps->fonts.ffn[achp->ftcSym].xszFfn,12))
				{
				if ( (!message) && (strcasecmp("UTF-8",charset)) )
					{
					wvWarning("Symbol font detected (too late sorry!), rerun wvHtml with option --charset utf-8\n\
option to support correct symbol font conversion to a viewable format.\n");
					message++;
					}
				wvTrace(("symbol char %d %x %c, using font %d %s\n",achp->xchSym,achp->xchSym,achp->xchSym,achp->ftcSym,wvWideStrToMB(ps->fonts.ffn[achp->ftcSym].xszFfn) ));
				wvTrace(("symbol char ends up as a unicode %x\n",wvConvertSymbolToUnicode(achp->xchSym-61440)));
				wvOutputFromUnicode(wvConvertSymbolToUnicode(achp->xchSym-61440),charset);
				return(0);
				}
			else if (0 == memcmp(wingdings,ps->fonts.ffn[achp->ftcSym].xszFfn,18))
				{
				if (!message)
					{
					wvError(("I have yet to do a wingdings to unicode mapping table, if you know of one tell me\n"));
					message++;
					}
				}
			else
				{
				if (!message)
					{
					char *fontname = wvWideStrToMB(ps->fonts.ffn[achp->ftcSym].xszFfn);
					wvError(("Special font %s, i need a mapping table to unicode for this\n",fontname));
					wvFree(fontname);
					printf("*");
					}
				return(0);
				}
			}
		default:
			return(0);
		}



	return(0);
	}

	
int myCharProc(wvParseStruct *ps,U16 eachchar,U8 chartype,U16 lid)
	{
	switch(eachchar)
		{
		case 19:
			wvTrace(("field began\n"));
			ps->fieldstate++;
			ps->fieldmiddle=0;
			fieldCharProc(ps,eachchar,chartype,lid);	/* temp */
			return(0);
			break;
		case 20:
			wvTrace(("field middle\n"));
			fieldCharProc(ps,eachchar,chartype,lid);
			ps->fieldmiddle=1;
			return(0);
			break;
		case 21:
			wvTrace(("field began\n"));
			ps->fieldmiddle=0;
			ps->fieldstate--;
			fieldCharProc(ps,eachchar,chartype,lid);	/* temp */
			return(0);
			break;
		case 0x08:
			wvError(("hmm did we loose the fSpec flag ?, this is possibly a bug\n"));
			break;
		}

	if (ps->fieldstate)
		{
		if (fieldCharProc(ps,eachchar,chartype,lid))
			return(0);
		}

	wvTrace(("charset is %s, lid is %x, type is %d, char is %x\n",charset,lid,chartype,eachchar));

	if ( (chartype) && (wvQuerySupported(&ps->fib,NULL) == WORD8) )
		wvTrace(("lid is %x\n",lid));
	
	if (charset != NULL)
		wvOutputHtmlChar(eachchar,chartype,charset,lid);
	else
		wvOutputHtmlChar(eachchar,chartype,wvAutoCharset(ps),lid);
	return(0);
	}


FILE *wvOpenConfig(char *config)
	{
	FILE *tmp;
	int i=0;
	if (config == NULL)
		config = "wvHtml.xml";
	else
		i=1;
    tmp = fopen(config,"rb");
	if (tmp == NULL)
		{
		if (i) wvError(("Attempt to open %s failed, using %s\n",config,HTMLCONFIG));
		config = HTMLCONFIG;
	    tmp = fopen(config,"rb");
		}
	return(tmp);
	}