forked from alandipert/ncsa-mosaic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTFWriter.c
555 lines (471 loc) · 12.9 KB
/
HTFWriter.c
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
/* FILE WRITER HTFWrite.h
** ===========
**
** This version of the stream object just writes to a C file.
** The file is assumed open and left open.
**
** Bugs:
** strings written must be less than buffer size.
*/
#include "../config.h"
#include <string.h>
#include <unistd.h>
#include "HTFWriter.h"
#include "HTFormat.h"
#include "HTAlert.h"
#include "HTFile.h"
#include "HText.h"
#include "tcp.h"
#include "HTCompressed.h"
#include "../libhtmlw/HTML.h"
#include "../src/mosaic.h"
#include "../src/gui-dialogs.h"
#include "../src/img.h"
extern char *currentURL;
int imageViewInternal=0;
#ifndef DISABLE_TRACE
extern int www2Trace;
#endif
/* Stream Object
** ------------
*/
struct _HTStream {
WWW_CONST HTStreamClass * isa;
FILE * fp;
char * fnam;
char * end_command;
int compressed;
int interrupted;
int write_error;
char *mime_type;
};
/* MOSAIC: We now pick up some external variables, handled
in src/mo-www.c: */
extern int force_dump_to_file;
extern char *force_dump_filename;
/* If force_dump_to_file is high, we know we want to dump the
data into a file already named by force_dump_filename and not
do anything else. */
/* If this is high, then we just want to dump the thing to a file;
the file is named by force_dump_filename. */
extern int binary_transfer;
/*_________________________________________________________________________
**
** A C T I O N R O U T I N E S
*/
/* Character handling
** ------------------
*/
PRIVATE void HTFWriter_put_character ARGS2(HTStream *, me, char, c)
{
int rv;
if (me->write_error)
return;
/* Make sure argument to putc is in range 0-255, to avoid weirdness
with rv == -1 == EOF when it's not supposed to. */
rv = putc ((int)(unsigned char)c, me->fp);
if (rv == EOF)
{
HTProgress ("Error writing to temporary file.");
me->write_error = 1;
}
}
/* String handling
** ---------------
**
** Strings must be smaller than this buffer size.
*/
PRIVATE void HTFWriter_put_string ARGS2(HTStream *, me, WWW_CONST char*, s)
{
int rv;
if (me->write_error)
return;
rv = fputs(s, me->fp);
if (rv == EOF)
{
HTProgress ("Error writing to temporary file.");
me->write_error = 1;
}
}
/* Buffer write. Buffers can (and should!) be big.
** ------------
*/
PRIVATE void HTFWriter_write ARGS3(HTStream *, me, WWW_CONST char*, s, int, l)
{
int rv;
if (me->write_error)
return;
rv = fwrite(s, 1, l, me->fp);
if (rv != l)
{
HTProgress ("Error writing to temporary file.");
me->write_error = 1;
}
}
char *supportedTypes[]={
"image/gif",
"image/jpeg",
"image/jpg",
"image/png",
"image/x-png",
"image/x-pcd-jpeg",
"image/x-pcd-jycc",
"image/xpm",
"image/xbm",
"image/xpixmap",
"image/xbitmap",
"image/x-xpixmap",
"image/x-xbitmap",
"\n"
};
int supportedImageType(char *mt) {
int i;
if (!mt || !*mt) {
return(0);
}
for (i=0; supportedTypes[i][0]!='\n'; i++) {
if (!strcmp(supportedTypes[i],mt)) {
return(1);
}
}
return(0);
}
/* Free an HTML object
** -------------------
**
** Note that the SGML parsing context is freed, but the created
** object is not,
** as it takes on an existence of its own unless explicitly freed.
*/
PRIVATE void HTFWriter_free ARGS1(HTStream *, me)
{
HText *text;
static char *envbuf1=NULL;
static char *envbuf2=NULL;
/* I dunno if this is necessary... */
if (me->interrupted)
{
free (me->fnam);
free (me);
return;
}
if (me->write_error)
{
/*
char *cmd = (char *)malloc ((strlen (me->fnam) + 32));
sprintf (cmd, "/bin/rm -f %s &", me->fnam);
system (cmd);
free (cmd);
*/
/*ddt*/unlink(me->fnam);
HTProgress ("Insufficient temporary disk space; could not transfer data.");
free (me->fnam);
free (me);
return;
}
fflush (me->fp);
fclose (me->fp);
/* We do want to be able to handle compressed inlined images,
but we don't want transparent uncompression to take place
in binary transfer mode. */
if (!binary_transfer && me->compressed != COMPRESSED_NOT)
{
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf (stderr, "[HTFWriter] Hi there; compressed is %d, fnam is '%s'\n",
me->compressed, me->fnam);
#endif
HTCompressedFileToFile (me->fnam, me->compressed);
}
if (force_dump_to_file)
{
if (!binary_transfer)
goto done;
}
/* Now, me->end_command can either be something starting with
"<mosaic-internal-reference" or it can be a real command.
Deal with appropriately. */
if (me->end_command)
{
/* Check for forced dump condition. The left paren comes
from the construction of me->end_command as a compound shell
command below. */
if (strstr (me->end_command, "mosaic-internal-dump")) {
rename_binary_file (me->fnam);
}
else if (!strstr (me->end_command, "mosaic-internal-reference")) {
if (imageViewInternal && supportedImageType(me->mime_type)) {
char *newHTML="<html>\n<head>\n<title>Mosaic's Internal Image Display</title>\n</head>\n<body>\n<img align=center src=\"%s\">\n</body>\n</html>\n";
char *buf;
buf=(char *)calloc((strlen(currentURL)+strlen(newHTML)+5),sizeof(char));
sprintf(buf,newHTML,currentURL);
text=HText_new();
HText_beginAppend(text);
HText_appendText(text,buf);
HText_endAppend(text);
free(buf);
buf=(char *)calloc((strlen(currentURL)+strlen(me->fnam)+5),sizeof(char));
sprintf(buf,"%s\n%s",me->fnam,currentURL);
ImageResolve(NULL,buf,0,NULL,NULL);
free(buf);
goto done;
}
HTProgress("Spawning external viewer.");
/*
* Have to dance around putenv since it makes "envbuf*" part
* of the actual environment string...*sigh* What a mess!
*/
if (envbuf1) {
envbuf2=(char *)calloc((strlen(currentURL)+
strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf2,"MOSAIC_CURRENT_URL=%s",currentURL);
putenv(envbuf2);
free(envbuf1);
envbuf1=NULL;
}
else if (envbuf2) {
envbuf1=(char *)calloc((strlen(currentURL)+
strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf1,"MOSAIC_CURRENT_URL=%s",currentURL);
putenv(envbuf1);
free(envbuf2);
envbuf2=NULL;
}
else { /* Likely it is the first time */
envbuf1=(char *)calloc((strlen(currentURL)+
strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf1,"MOSAIC_CURRENT_URL=%s",currentURL);
putenv(envbuf1);
}
system (me->end_command);
if (envbuf1) {
envbuf2=(char *)calloc((strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf2,"MOSAIC_CURRENT_URL=");
putenv(envbuf2);
free(envbuf1);
envbuf1=NULL;
}
else if (envbuf2) {
envbuf1=(char *)calloc((strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf1,"MOSAIC_CURRENT_URL=");
putenv(envbuf1);
free(envbuf2);
envbuf2=NULL;
}
else { /* Likely it is the first time */
envbuf1=(char *)calloc((strlen("MOSAIC_CURRENT_URL=")+
2),
sizeof(char));
sprintf(envbuf1,"MOSAIC_CURRENT_URL=");
putenv(envbuf1);
}
}
else {
/* Internal reference, aka HDF file. Just close output file. */
}
}
else
{
/* No me->end_command; just close the file. */
}
/* Construct dummy HText thingie so Mosaic knows
not to try to access this "document". */
text = HText_new ();
HText_beginAppend (text);
/* If it's a real internal reference, tell Mosaic. */
if (me->end_command) {
if (strstr (me->end_command, "mosaic-internal-reference")) {
HText_appendText (text, me->end_command);
}
else {
HText_appendText (text, "<mosaic-access-override>\n");
}
free (me->end_command);
}
else {
/* No me->end_command; just override the access. */
HText_appendText (text, "<mosaic-access-override>\n");
}
HText_endAppend (text);
done:
if (binary_transfer)
rename_binary_file (me->fnam);
free (me->fnam);
if (me->mime_type) {
free(me->mime_type);
}
free (me);
return;
}
/* End writing
*/
PRIVATE void HTFWriter_end_document ARGS1(HTStream *, me)
{
if (me->interrupted || me->write_error)
return;
fflush(me->fp);
}
PRIVATE void HTFWriter_handle_interrupt ARGS1(HTStream *, me)
{
/* char *cmd;*/
if (me->write_error)
goto outtahere;
/* Close the file, then kill it. */
fclose (me->fp);
/*
cmd = (char *)malloc ((strlen (me->fnam) + 32) * sizeof (char));
sprintf (cmd, "/bin/rm -f %s &", me->fnam);
system (cmd);
free (cmd);
*/
/*ddt*/unlink(me->fnam);
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf (stderr, "*** HTFWriter interrupted; killed '%s'\n", me->fnam);
#endif
outtahere:
me->interrupted = 1;
return;
}
/* Structured Object Class
** -----------------------
*/
PRIVATE WWW_CONST HTStreamClass HTFWriter = /* As opposed to print etc */
{
"FileWriter",
HTFWriter_free,
HTFWriter_end_document,
HTFWriter_put_character, HTFWriter_put_string,
HTFWriter_write,
HTFWriter_handle_interrupt
};
/* Take action using a system command
** ----------------------------------
**
** Creates temporary file, writes to it, executes system command
** on end-document. The suffix of the temp file can be given
** in case the application is fussy, or so that a generic opener can
** be used.
**
** WARNING: If force_dump_to_file is high, pres may be NULL
** (as we may get called directly from HTStreamStack).
*/
PUBLIC HTStream* HTSaveAndExecute ARGS5(
HTPresentation *, pres,
HTParentAnchor *, anchor, /* Not used */
HTStream *, sink,
HTFormat, format_in,
int, compressed) /* Not used */
{
char *command;
WWW_CONST char * suffix;
HTStream* me;
me = (HTStream*)malloc(sizeof(*me));
me->isa = &HTFWriter;
me->interrupted = 0;
me->write_error = 0;
me->fnam = NULL;
me->end_command = NULL;
me->compressed = compressed;
if (!format_in || !format_in->name || !*(format_in->name)) {
me->mime_type=NULL;
}
else {
if (!strncmp(format_in->name,"image",5)) {
me->mime_type=strdup(format_in->name);
}
else {
me->mime_type=NULL;
}
}
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf (stderr, "[HTSaveAndExecute] me->compressed is '%d'\n",
me->compressed);
#endif
/* Save the file under a suitably suffixed name */
if (!force_dump_to_file)
{
extern char *mo_tmpnam (char *);
suffix = HTFileSuffix(pres->rep);
me->fnam = mo_tmpnam(anchor->address);
if (suffix)
{
char *freeme = me->fnam;
me->fnam = (char *)malloc (strlen (me->fnam) + strlen (suffix) + 8);
strcpy(me->fnam, freeme);
strcat(me->fnam, suffix);
free (freeme);
}
}
else
{
me->fnam = strdup (force_dump_filename);
}
me->fp = fopen (me->fnam, "w");
if (!me->fp)
{
HTProgress("Can't open temporary file -- serious problem.");
me->write_error = 1;
return me;
}
/* If force_dump_to_file is high, we're done here. */
if (!force_dump_to_file)
{
if (!strstr (pres->command, "mosaic-internal-reference"))
{
/* If there's a "%s" in the command, or if the command
is magic... */
#ifndef DISABLE_TRACE
if (www2Trace)
fprintf (stderr, "HTFWriter: pres->command is '%s'\n",
pres->command);
#endif
if (strstr (pres->command, "%s") ||
strstr (pres->command, "mosaic-internal"))
{
/* Make command to process file */
command = (char *)malloc
((strlen (pres->command) + 10 + 3*strlen(me->fnam)) *
sizeof (char));
/* Cute. pres->command will be something like "xv %s"; me->fnam
gets filled in as many times as appropriate. */
sprintf (command, pres->command, me->fnam, me->fnam, me->fnam);
me->end_command = (char *)malloc
((strlen (command) + 32 + strlen(me->fnam)) * sizeof (char));
sprintf (me->end_command, "(%s ; /bin/rm -f %s) &",
command, me->fnam);
free (command);
}
else
{
/* Make command to process file -- but we have to cat
to the viewer's stdin. */
me->end_command = (char *)malloc
((strlen (pres->command) + 64 + (2 * strlen(me->fnam))) *
sizeof (char));
sprintf (me->end_command, "((cat %s | %s); /bin/rm -f %s) &",
me->fnam, pres->command, me->fnam);
}
}
else
{
/* Overload me->end_command to be what we should write out as text
to communicate back to client code. */
me->end_command = (char *)malloc
(strlen ("mosaic-internal-reference") + strlen (me->fnam) + 32);
sprintf (me->end_command, "<%s \"%s\">\n", "mosaic-internal-reference", me->fnam);
}
}
return me;
}