This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvmgcsfx.c
2276 lines (1951 loc) · 58.2 KB
/
xvmgcsfx.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* $Id: xvmgcsfx.c,v 1.23 95/11/27 19:03:36 tin329 Exp Locker: tin329 $
* xvmgcsfx.c - Use the filters as input and output method.
*
* Features
* ========
* Use the filters as input and output method for load and save unsupported
* image format file. The filter command is recognized by definition of
* magic number or suffix in "~/.xv_mgcsfx" .
*
* Bugs
* ====
* There are many bugs.
* Let's go hunting for insects with an insect net. (it's all joke.)
*
* Author
* ======
* Tetsuya INOUE <[email protected]>
*/
/*
* Known Bugs and Todo / $B$"$l$3$l5$$K$J$k$3$H(B
*
* ~/.xv_mgcsfx $BFb(B
* $B!&Dj5A$,IT40A4$@$H%(%i!<(B (':'$B$N?t(B)$B!#(B
* $B!&%G%j%_%?$H$7$F(B ':' $B$r;H$&$N$G!"%9%?!<%H%"%C%W%U%!%$%kFb$G(B
* ':' $B$rMQ$$$FDj5A$O$G$-$J$$!#(B'\:'$B$G$b%@%a!#(B
* $B!&(B magic $B%?%$%W$G!"#8?J?t$O#37eJ,#0!A#7$rD4$Y!"#1#6?J?t$O(B
* isxdigit $B$,??$rJV$94VCf=hM}$5$l$k!#$7$+$7!"#1#b#y#t#e$H(B
* $B$7$F$7$+I>2A$5$l$J$$!#(B
* $B!&%W%j%W%m%;%C%5$r;H$&$H$-$O!"%3%a%s%H$N=q$-J}$KCm0U$7$J$1$l$P$J(B
* $B$i$J$$!#%W%j%W%m%;%C%5$K$h$C$F$O%3%a%s%H$,%(%i!<$K$J$k!#(B
* $B!&%Q%$%W$X$NF~=PNO$N%U%)!<%^%C%H$N<oN`$,(B PNM $B$N$_(B
* $BF~NO(B
* $B%U%!%$%k%]%$%s%?$r(B seek $B$7$F$O$$$1$J$$(B
* $B%U%!%$%k%5%$%:$rMQ$$$F$O$$$1$J$$(B
* $B=PNO(B
* $B%U%!%$%k%]%$%s%?$r(B seek $B$7$F$O$$$1$J$$(B
* exec $B$G$-$J$/$F=*N;$7$?%W%m%;%9$K=q$-9~$_IT2D(B
* $B!&%5%U%#%C%/%9$H%^%8%C%/%J%s%P!<$N;H$$J,$1$r$I$&$9$k$+!#(B
* $B%^%8%C%/%J%s%P!<$,F1$8$G!"%5%U%#%C%/%9$,0[$J$k>l9g$rG'$a$k$+!)(B
* $B!&(Bcompress(gzip)$B$N%U%!%$%k$O%F%s%]%i%j$G$O(B xvtmp??? $B$H$$$&L>A0$J(B
* $B$N$G(B suffix $B$G$O<1JL$G$-$J$$!#(B
*
* $BG'<1$9$k;~$K(B MACBINARY $B$K$OIi$1$k(B(in xv.c)$B!#(B
*
* $BB?=E$K(B pipe $B$rDL$9$3$H$,$G$-$J$$!#(B(pipe $B$,(B seek $B$G$-$J$$$+$i(B)
* $B!&(Bsocketpair $B$G!"(Brecv $B$K(B MSG_PEEK $B%U%i%0$r$D$+$C$F6uFI$_$9$k!#(B
* $B!&$3$l$r$d$k$H%U%!%$%k$NG'<1$,$a$A$c$a$A$cCY$/$J$k!#(B
*
* $B%j%=!<%9$G@_Dj(B
* $B!&%j%=!<%9$G@_Dj$9$kJ}$,LLE]$/$5$$(B
*
* $B%^%8%C%/%J%s%P!<$N@_Dj$K@55,I=8=(B
*
* $B%;!<%VMQ%W%m%;%9$,<:GT$9$k>l9g$NBP:v$,:#0l$D(B
*
* DEC OSF/1 V3.0 $B$G$O!"%Q%$%W$K%G!<%?$,$^$@$J$$;~$KFI$_9~$b$&$H$9$k$H!"(B
* read $B$,IT40A4$K$J$k!#(B(in xvpbm.c)
* $BF1MM$K=q$-9~$_;~$K$bLdBj$,@8$8$k$+$b$7$l$J$$!#(B
*/
#define NEEDSDIR /* for stat() */
#include "xv.h"
#ifdef HAVE_MGCSFX
#ifdef __osf__
# ifdef __alpha
# define ARCHITECTURE64 1
# endif /* __alpha */
#endif /* __osf__ */
#ifdef ARCHITECTURE64
typedef short int16;
typedef int int32;
typedef long int64;
#else
typedef short int16;
typedef long int32;
#endif /* ARCHITECTURE64 */
#ifdef sgi
# define vfork fork
#endif
#define USE_SIGCHLD
#if 0
# undef USE_SIGCHLD
#endif
#ifdef USE_SIGCHLD
# include <sys/wait.h>
#endif
typedef struct _mgcsfxtab
{
struct _mgcsfxtab *next;
char *description;
int mgcsfx_type;
int offset;
union{
int16 int16_data;
int32 int32_data;
char *string_data;
} dt;
int string_len;
char *suffix;
int input_image_type;
char *input_command;
int output_image_type;
char *output_command;
} mgcsfxtab;
#ifndef MGCSFXDIR
# define MGCSFXDIR "/usr/local/lib"
#endif
#ifndef SYSCONFDIR
# define SYSCONFDIR MGCSFXDIR
#endif
#ifndef MGCSFX_SITE_RC
# define MGCSFX_SITE_RC "xv_mgcsfx"
#endif
#ifndef MGCSFX_RC
# define MGCSFX_RC ".xv_mgcsfx"
#endif
#ifdef USE_MGCSFX_PREPROCESSOR
# ifndef MGCSFX_PREPROCESSOR
# define MGCSFX_PREPROCESSOR "/usr/lib/cpp"
# endif
#endif
/* Check type for Magic number and Suffix */
enum {T_UNKNOWN,
T_MAGIC, T_SUFFIX,
T_BEINT16, T_BEINT32, T_BEINT64,
T_LEINT16, T_LEINT32, T_LEINT64};
/* Image Type for input and output format */
enum {IT_UNKNOWN,
#ifdef HAVE_MGCSFX_AUTO
IT_AUTO,
#endif /* HAVE_MGCSFX_AUTO */
IT_PNM, IT_PPM, IT_PGM, IT_PBM,
IT_PNM_RAW, IT_PPM_RAW, IT_PGM_RAW, IT_PBM_RAW,
IT_PNM_ASCII, IT_PPM_ASCII, IT_PGM_ASCII, IT_PBM_ASCII,
IT_GIF, IT_JPEG, IT_TIFF, IT_JFIF, /* IT_PS, IT_COMPRESS,*/
IT_XBM, IT_XPM, IT_BMP, IT_SUNRAS, IT_IRIS, IT_XWD,
/* IT_TARGA, IT_FITS, IT_PM, IT_UTAHRLE, IT_PCX, IT_PDSVICAR, IT_IFF, */
IT_MAG, IT_MAKI, IT_PI, IT_PIC, IT_PIC2 /* , IT_PCD */};
/*--------------------------------------------------------------------------*/
void mgcsfx_handler PARM((int));
void mgcsfx_handler_setup PARM((void));
#ifdef USE_MGCSFX_PREPROCESSOR
static char *get_tmp_fname PARM((void));
static char *make_preprocessed_file PARM((char *));
#endif /* USE_MGCSFX_PREPROCESSOR */
int is_mgcsfx PARM((char *, unsigned char *, int));
char *mgcsfx_auto_input_com PARM((char *));
static mgcsfxtab *free_mgcsfx PARM((mgcsfxtab *));
static char *fgettoken PARM((FILE*, int));
static int string_fin PARM((char *));
static int type_mgcsfx PARM((char *));
static int type_image PARM((char *));
static void read_mgcsfx PARM((mgcsfxtab **, char *));
static void init_mgcsfx PARM((void));
static mgcsfxtab *find_mgcsfx PARM((char *, unsigned char *, int));
int LoadMGCSFX PARM((char *, PICINFO *));
#ifdef SVR4
typedef void Sigfunc(int);
static Sigfunc *xv_signal PARM((int , Sigfunc *));
#endif
/*--------------------------------------------------------------------------*/
mgcsfxtab *mgcsfx_table = NULL;
int mgcsfx_setup_flag = 0;
int nitem_mgcsfx = 0;
int desc_width = 0;
int max_offset_mgcsfx = 0;
int max_length_mgcsfx = 0;
int need_buf_size = 0;
static char input_command_ex[1024];
static int input_command_ex_flag = 0;
#ifdef USE_SIGCHLD
static int w_p_fail=0;
#endif
/*--------------------------------------------------------------------------*/
/***************************************************/
void mgcsfx_handler(sig)
int sig;
{
#ifdef USE_SIGCHLD
int pid, pst;
#endif
#if defined(SYSV) || defined(SVR4)
sighold(sig);
#else
sigblock(sigmask(sig));
#endif
#ifdef USE_SIGCHLD
if(w_p_fail == 1){
/*
* At this point, process write to broken pipe.
* Probably external command was can't exec.
*/
w_p_fail = 2;
pid = wait(&pst);
}
#endif
return;
/* Quit(1); */ /*exit(1);*/
}
void mgcsfx_handler_setup()
{
#ifdef SVR4
xv_signal(SIGPIPE, (void (*)PARM((int))) mgcsfx_handler);
xv_signal(SIGCHLD, (void (*)PARM((int))) mgcsfx_handler);
#else
# ifdef SYSV
sigset(SIGPIPE, (void (*)PARM((int))) mgcsfx_handler);
sigset(SIGCHLD, (void (*)PARM((int))) mgcsfx_handler);
# else
signal(SIGPIPE, (void (*)PARM((int))) mgcsfx_handler);
signal(SIGCHLD, (void (*)PARM((int))) mgcsfx_handler);
# endif
#endif
}
/***************************************************/
#ifdef USE_MGCSFX_PREPROCESSOR
static char *get_tmp_fname()
{
static char tmp[MAXPATHLEN+1];
#ifndef VMS
sprintf(tmp, "%s/xvmgcsfxXXXXXX",tmpdir);
#else
/* sprintf(tmp, "Sys$Scratch:xvmgcsfxXXXXXX"); */
strcpy(tmp, "[]xvmgcsfxXXXXXX");
#endif /* VMS */
#ifdef USE_MKSTEMP
close(mkstemp(tmp));
#else
mktemp(tmp);
#endif
return tmp;
}
static char *make_preprocessed_file(fname)
char *fname;
{
char buf[512];
char *tmp_name;
tmp_name = get_tmp_fname();
#ifndef VMS
sprintf(buf,"%s %s > %s", MGCSFX_PREPROCESSOR, fname, tmp_name);
#else
sprintf(buf,"%s %s > %s", MGCSFX_PREPROCESSOR, fname, tmp_name); /* really OK? */
#endif
SetISTR(ISTR_INFO, "Preprocessing '%s'...", BaseName(fname));
#ifndef VMS
if (system(buf))
#else
if (!system(buf))
#endif
{
SetISTR(ISTR_INFO, "Unable to preprocess '%s'.", BaseName(fname));
Warning();
return NULL;
}
return tmp_name;
}
#endif /* USE_MGCSFX_PREPROCESSOR */
/***************************************************/
/* $BG'<1$G$-$k%U%!%$%k$+$I$&$+D4$Y$k(B */
int is_mgcsfx(fname,buffer,size)
char *fname;
unsigned char *buffer;
int size;
{
mgcsfxtab *magic;
FILE *fp;
unsigned char *buf;
int s;
if(nomgcsfx){
return 0;
}else{
if(size < need_buf_size){
if((buf = (unsigned char *)calloc(need_buf_size, sizeof(char)))==NULL){
fprintf(stderr,"Can't allocate memory\n");
return 0;
}
if((fp = xv_fopen(fname, "r"))==NULL){
fprintf(stderr,"Can't open file %s\n", fname);
free(buf);
return 0;
}
s = fread(buf, 1, need_buf_size, fp);
if((magic = find_mgcsfx(fname, buf, s))!=NULL &&
magic->input_command != NULL){
free(buf);
fclose(fp);
return 1;
}else{
free(buf);
fclose(fp);
return 0;
}
}else{
if((magic = find_mgcsfx(fname, buffer, size))!=NULL &&
magic->input_command != NULL){
return 1;
}else{
return 0;
}
}
}
}
#ifdef HAVE_MGCSFX_AUTO
char *mgcsfx_auto_input_com(fname)
char *fname;
{
static char command[1024];
mgcsfxtab *magic;
char *ptr;
FILE *fp;
unsigned char *buf;
int s;
if((buf = (unsigned char *)calloc(need_buf_size, sizeof(char)))==NULL){
fprintf(stderr,"Can't allocate memory\n");
return NULL;
}
if((fp = xv_fopen(fname, "r"))==NULL){
fprintf(stderr,"Can't open file %s\n", fname);
free(buf);
return NULL;
}
s = fread(buf, 1, need_buf_size, fp);
if((magic = find_mgcsfx(fname, buf, s))!=NULL &&
magic->input_command != NULL && magic->input_image_type == IT_AUTO){
if ((ptr = strstr(magic->input_command, "%s"))){
sprintf(command, magic->input_command, fname);
}else{
sprintf(command, "%s < %s", magic->input_command, fname);
}
free(buf);
fclose(fp);
return command;
}else{
free(buf);
fclose(fp);
return NULL;
}
}
#endif /* HAVE_MGCSFX_AUTO */
/***************************************************/
static mgcsfxtab *free_mgcsfx(m)
mgcsfxtab *m;
{
mgcsfxtab *next;
if(m == NULL) return NULL;
next = m->next;
if(m->description != NULL) free(m->description);
if(m->mgcsfx_type == T_MAGIC && m->dt.string_data != NULL)
free(m->dt.string_data);
if(m->suffix != NULL) free(m->suffix);
if(m->input_command != NULL) free(m->input_command);
if(m->output_command != NULL) free(m->output_command);
free(m);
return next;
}
/***************************************************/
/* char c $B$^$?$O(B '\n' $B$G6h@Z$i$l$?J8;zNs$r<h$j=P$9(B
* $B%U%!%$%k$N:G8e$^$GFI$s$@$i(B NULL $B$rJV$9(B
* $B2~9T$J$i2~9T$rJV$9(B($B2~9T$G6h@Z$i$l$?>l9g$O(B '\n' $B$r%9%H%j!<%`$KLa$9(B)
*/
#define CBUF_SIZE 1024
static char *fgettoken(fp, c)
FILE *fp;
int c; /* Real mean is char */
{
char *buf;
char *buf2;
int i;
int n=0;
int max=0;
int count = 1;
char *ss;
char *se;
if((buf = (char *)calloc(CBUF_SIZE, sizeof(char))) == NULL){
fprintf(stderr,"Can't allocate memory\n");
exit(1);
}
max = CBUF_SIZE;
count = 2;
do{
if((i = getc(fp))==EOF || i == '\n' || i == c) break;
buf[n] = (char)i;
if(i != c && n == max-1){
buf[max] = '\0';
if((buf2 = (char *)calloc(CBUF_SIZE * count, sizeof(char))) == NULL){
fprintf(stderr,"Can't allocate memory\n");
exit(1);
}
strcpy(buf2, buf);
free(buf);
buf = buf2;
buf2 = NULL;
max = CBUF_SIZE * count;
count++;
}
n++;
}while(i != c);
buf[n] = '\0';
/* $B:G=i$H:G8e$N6uGrJ8;z$r@Z$j5M$a$k(B */
ss = buf + strspn(buf, " \t\b\r\n"); /* find the first non-white space */
se = buf + strlen(buf); /* find the end of the string */
/* strip from the end first */
while ((--se >= ss) && strchr(" \t\b\r\n", *se));
*(++se) = '\0';
if(i == EOF && strlen(ss)==0){ /* EOF $B$J$i(B NULL $B$rJV$9(B */
free(buf);
return NULL;
}else if(i == '\n' && strlen(ss)==0){ /* $B2~9T$N$_$N>l9g(B */
static char cr[2] = {'\n','\0'};
buf2 = strdup(cr);
free(buf);
return buf2;
}else{ /* $BDL>o(B */
if(i == '\n' && strlen(ss)>0) ungetc(i,fp);
buf2 = strdup(ss);
free(buf);
return buf2;
}
}
/***************************************************/
/* $BJ8;zNsCf$NFC<l5-9f(B(\)$B$r@5$7$$$b$N$K$9$k(B
*/
static int string_fin(string_data)
char *string_data;
{
char *cptr;
char *ptr;
int length;
/* Change all the \xx sequences into a single character */
cptr = string_data;
for (ptr = cptr; *ptr; ++ptr){
if (*ptr != '\\'){
*cptr = *ptr;
}else{
switch (*(++ptr)){
#if defined(__STDC__)
case 'a': /* Audible alert (terminal bell) */
*cptr = '\007';
break;
case '?': /* Question mark */
*cptr = '\?';
break;
#endif
case 'b': /* Backspace */
*cptr = '\b';
break;
case 'f': /* Form feed */
*cptr = '\f';
break;
case 'n': /* Line feed */
*cptr = '\n';
break;
case 'r': /* Carriage return */
*cptr = '\r';
break;
case 't': /* Horizontal tab */
*cptr = '\t';
break;
case 'v': /* Vertical tab */
*cptr = '\v';
break;
case '\\': /* Backslash */
*cptr = '\\';
break;
case '\'': /* Single quote */
*cptr = '\'';
break;
case '"': /* Double quote */
*cptr = '\"';
break;
case '0': /* Octal constant \0 ... \377 */
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
if ((ptr[1] >= '0') && (ptr[1] <= '7')){
if ((ptr[2] >= '0') && (ptr[2] <= '7')){ /* \000 ...\377 */
*cptr = ((*ptr - '0') * 64) +((ptr[1] - '0') * 8) +(ptr[1] - '0');
ptr += 2;
}else{ /* \00 ...\77 */
*cptr = ((*ptr - '0') * 8) + (ptr[1] - '0');
++ptr;
}
}else{ /* \0 ...\7 */
*cptr = *ptr - '0';
}
break;
case 'x': /* Hexadecimal constant \x0 .. \xff */
if (isxdigit (ptr[1])){
*cptr = 0;
while (isxdigit (*(++ptr)))
*cptr = (*cptr * 16) +
(*ptr > '9' ? tolower (*ptr) - ('a' - 10) : *ptr - '0');
--ptr;
break;
}
default:
/* *(cptr++) = '\\'; No use for treat '\z' as 'z' */
*cptr = *ptr;
break;
}
}
++cptr;
}
*cptr = '\0';
length = cptr - string_data;
return length;
}
/***************************************************/
static int type_mgcsfx(str)
char *str;
{
if(str == NULL){
return T_UNKNOWN;
}else if(!strcmp(str, "magic") || !strcmp(str, "MAGIC")){
return T_MAGIC;
}else if(!strcmp(str, "string") || !strcmp(str, "STRING")){
return T_MAGIC;
}else if(!strcmp(str, "suffix") || !strcmp(str, "SUFFIX")){
return T_SUFFIX;
}else if(!strcmp(str, "beint16") || !strcmp(str, "BEINT16")){
return T_BEINT16;
}else if(!strcmp(str, "leint16") || !strcmp(str, "LEINT16")){
return T_LEINT16;
}else if(!strcmp(str, "beint32") || !strcmp(str, "BEINT32")){
return T_BEINT32;
}else if(!strcmp(str, "leint32") || !strcmp(str, "LEINT32")){
return T_LEINT32;
}else{
return T_UNKNOWN;
}
}
/***************************************************/
static int type_image(str)
char *str;
{
if(str == NULL){
return IT_UNKNOWN;
#ifdef HAVE_MGCSFX_AUTO
}else if(!strcmp(str, "auto") || !strcmp(str, "AUTO")){
return IT_AUTO;
#endif /* HAVE_MGCSFX_AUTO */
}else if(!strcmp(str, "pnm") || !strcmp(str, "PNM")){
return IT_PNM;
}else if(!strcmp(str, "ppm") || !strcmp(str, "PPM")){
return IT_PPM;
}else if(!strcmp(str, "pgm") || !strcmp(str, "PGM")){
return IT_PGM;
}else if(!strcmp(str, "pbm") || !strcmp(str, "PBM")){
return IT_PBM;
}else if(!strcmp(str, "pnm_raw") || !strcmp(str, "PNM_RAW")){
return IT_PNM_RAW;
}else if(!strcmp(str, "ppm_raw") || !strcmp(str, "PPM_RAW")){
return IT_PPM_RAW;
}else if(!strcmp(str, "pgm_raw") || !strcmp(str, "PGM_RAW")){
return IT_PGM_RAW;
}else if(!strcmp(str, "pbm_raw") || !strcmp(str, "PBM_RAW")){
return IT_PBM_RAW;
}else if(!strcmp(str, "pnm_ascii") || !strcmp(str, "PNM_ASCII")){
return IT_PNM_ASCII;
}else if(!strcmp(str, "ppm_ascii") || !strcmp(str, "PPM_ASCII")){
return IT_PPM_ASCII;
}else if(!strcmp(str, "pgm_ascii") || !strcmp(str, "PGM_ASCII")){
return IT_PGM_ASCII;
}else if(!strcmp(str, "pbm_ascii") || !strcmp(str, "PBM_ASCII")){
return IT_PBM_ASCII;
}else if(!strcmp(str, "gif") || !strcmp(str, "GIF")){
return IT_GIF;
}else if(!strcmp(str, "jpeg") || !strcmp(str, "JPEG")){
return IT_JPEG;
}else if(!strcmp(str, "tiff") || !strcmp(str, "TIFF")){
return IT_TIFF;
}else if(!strcmp(str, "jfif") || !strcmp(str, "JFIF")){
return IT_JFIF;
}else if(!strcmp(str, "xbm") || !strcmp(str, "XBM")){
return IT_XBM;
}else if(!strcmp(str, "xpm") || !strcmp(str, "XPM")){
return IT_XPM;
}else if(!strcmp(str, "bmp") || !strcmp(str, "BMP")){
return IT_BMP;
}else if(!strcmp(str, "sunras") || !strcmp(str, "SUNRAS")){
return IT_SUNRAS;
}else if(!strcmp(str, "iris") || !strcmp(str, "IRIS")){
return IT_IRIS;
}else if(!strcmp(str, "xwd") || !strcmp(str, "XWD")){
return IT_XWD;
}else if(!strcmp(str, "mag") || !strcmp(str, "MAG")){
return IT_MAG;
}else if(!strcmp(str, "maki") || !strcmp(str, "MAKI")){
return IT_MAKI;
}else if(!strcmp(str, "pi") || !strcmp(str, "PI")){
return IT_PI;
}else if(!strcmp(str, "pic") || !strcmp(str, "PIC")){
return IT_PIC;
}else if(!strcmp(str, "pic2") || !strcmp(str, "PIC2")){
return IT_PIC2;
}else{
return IT_UNKNOWN;
}
}
/*--------------------------------------------------------------------------*/
#define mgcsfx_read_error(FILENAME, LINENUM, AFTERFIELD) \
fprintf (stderr,\
"%s: line %d: missing fields of %s field\n",\
FILENAME, LINENUM, AFTERFIELD);
#define magic_type_error(FILENAME, LINENUM, MAGICNUMBER) \
fprintf (stderr,\
"%s: line %d: invalid <magic type> field '%s'\n",\
FILENAME, LINENUM, MAGICNUMBER);
/*--------------------------------------------------------------------------*/
/***************************************************/
static void read_mgcsfx(mgcsfx_table, fname)
mgcsfxtab **mgcsfx_table;
char *fname;
{
FILE *fp;
char *s;
int line_number = 0;
int str_len;
int reach_end;
int def_err;
char *description;
char *mgcsfx_type;
char *offset;
char *magic;
char *suffix;
char *i_img;
char *i_com;
char *o_img;
char *o_com;
mgcsfxtab *ent;
mgcsfxtab **entry;
if((fp=fopen(fname, "r"))==NULL){
/* fprintf(stderr, "Can't open %s\n",fname); */
return;
}
while(1){
retry:
line_number++;
def_err = 0;
s= NULL;
description = mgcsfx_type = offset = magic = suffix
= i_img = i_com = o_img = o_com = NULL;
reach_end = 0;
if((s = fgettoken(fp, ':'))==NULL) break; /* EOF $B$J$i=*$j(B */
if(*s == '#'){/* $B@hF,$,(B '#' $B$J$iFI$_$H$P$9(B */
while((s = fgettoken(fp, '\n'))!=NULL){
if(*s == '\n'){
free(s);
goto retry;
}
free(s);
}
if(s == NULL) break;
}else if(*s == '\n'){/* $B6u9T$OL5;k(B */
free(s);
goto retry;
}
if(strlen(s) > 0) description = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "data type");
goto next;
}
if(strlen(s) > 0) mgcsfx_type = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "byte offset");
goto next;
}
if(strlen(s) > 0) offset = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "magic number");
goto next;
}
if(strlen(s) > 0) magic = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "suffix");
goto next;
}
if(strlen(s) > 0) suffix = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "input image type");
goto next;
}
if(strlen(s) > 0) i_img = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "input command");
goto next;
}
if(strlen(s) > 0) i_com = s;
else free(s);
if((s = fgettoken(fp, ':'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
if(s != NULL) free(s);
mgcsfx_read_error(fname, line_number, "output image type");
goto next;
}
if(strlen(s) > 0) o_img = s;
else free(s);
if((s = fgettoken(fp, '#'))==NULL || *s == '\n'){/* $B2?$b$J$$$J$i@_Dj%_%9(B */
/*
free(s);
mgcsfx_read_error(fname, line_number, "output command");
goto next;
*/
if(s != NULL){
*s = '\0';
reach_end = 1;
}
}
if(s != NULL){
if(strlen(s) > 0) o_com = s;
else free(s);
}
if(reach_end == 0){
while((s = fgettoken(fp, '\n'))!=NULL){/* $B9TKv$N%4%_$r<N$F$k(B */
if(*s == '\n'){
free(s);
break; /* goto next; */
}
free(s);
}
}else{
reach_end = 0;
}
/* --------------------------------------------------------------------- */
next:;
if(DEBUG){
fprintf(stderr,"Read: file %s: line %d.\n", fname, line_number);
fprintf(stderr,"Description : %s\n",
description ? description : "-- error --");
fprintf(stderr,"Type : %s\n",
mgcsfx_type ? mgcsfx_type : "-- error --");
fprintf(stderr,"Offset : %s\n", offset ? offset : "--+--");
fprintf(stderr,"Magic : %s\n", magic ? magic : "--+--");
fprintf(stderr,"Suffix : %s\n", suffix ? suffix : "--+--");
fprintf(stderr,"i Image : %s\n", i_img ? i_img : "--+--");
fprintf(stderr,"i Command : %s\n", i_com ? i_com : "--+--");
fprintf(stderr,"o Image : %s\n", o_img ? o_img : "--+--");
fprintf(stderr,"o Command : %s\n", o_com ? o_com : "--+--");
fprintf(stderr,"\n");
}
/* create mgcsfxtab */
if((ent = (mgcsfxtab *) malloc (sizeof (mgcsfxtab)))==NULL){
fprintf(stderr,"Can't allocate memory\n");
exit(1);
}
ent->next = NULL;
ent->description = NULL;
ent->mgcsfx_type = T_UNKNOWN;
ent->offset = 0;
ent->string_len = 0;
ent->suffix = NULL;
ent->input_image_type = IT_UNKNOWN;
ent->input_command = NULL;
ent->output_image_type = IT_UNKNOWN;
ent->output_command = NULL;
if(description != NULL){
ent->description = description;
description = NULL;
}else{
fprintf (stderr,"%s: line %d: undefined <description> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
if(mgcsfx_type == NULL){
fprintf (stderr,"%s: line %d: undefined <mgcsfx type> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
ent->mgcsfx_type = type_mgcsfx(mgcsfx_type);
switch(ent->mgcsfx_type){
case T_SUFFIX:
if(suffix == NULL){
fprintf (stderr,
"%s: line %d: conflict definition : undefined <suffix> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
break;
case T_BEINT16:
if (sscanf(magic, "%hi", &(ent->dt.int16_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
case T_LEINT16:
if (sscanf(magic, "%hi", &(ent->dt.int16_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
#ifdef ARCHITECTURE64
case T_BEINT32:
if (sscanf(magic, "%i", &(ent->dt.int32_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
case T_LEINT32:
if (sscanf(magic, "%i", &(ent->dt.int32_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
#else
case T_BEINT32:
if (sscanf(magic, "%li", &(ent->dt.int32_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
case T_LEINT32:
if (sscanf(magic, "%li", &(ent->dt.int32_data)) != 1){
magic_type_error(fname, line_number, magic);
def_err ++;
goto next2;
}
break;
#endif /* ARCHITECTURE64 */
case T_MAGIC:
if(magic == NULL){
fprintf (stderr,"%s: line %d: undefined <magic> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
if((str_len = string_fin(magic))<=0){
fprintf (stderr,"%s: line %d: invalid <magic> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
ent->string_len = str_len;
if((ent->dt.string_data = (char *)malloc(str_len + 1))==NULL){
fprintf(stderr,"Can't allocate memory\n");
exit(1);
}
memcpy(ent->dt.string_data, magic, str_len + 1);
break;
case T_UNKNOWN:
default:
fprintf (stderr,"%s: line %d: invalid <mgcsfx type> field.\n",
fname, line_number);
def_err ++;
goto next2;
break;
};
if(offset == NULL){
if(ent->mgcsfx_type == T_MAGIC ||
ent->mgcsfx_type == T_BEINT16 ||
ent->mgcsfx_type == T_LEINT16 ||
ent->mgcsfx_type == T_BEINT32 ||
ent->mgcsfx_type == T_LEINT32){
fprintf (stderr,
"%s: line %d: conflict definition : undefined <offset> field.\n",
fname, line_number);
def_err ++;
goto next2;
}
}else{
if(ent->mgcsfx_type != T_SUFFIX) sscanf(offset, "%i", &(ent->offset));
}
if(suffix != NULL){
ent->suffix = suffix;
suffix = NULL;
}
if((i_img == NULL && i_com == NULL) && (o_img == NULL || o_com == NULL)){
fprintf (stderr,"%s: line %d: invalid definition.\n",
fname, line_number);