forked from math-atlas/math-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.c
4264 lines (3949 loc) · 125 KB
/
extract.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
/************************************************************************/
/* Extract v4.0.0 */
/* (C) Copyright 1994,2015 R. Clint Whaley ([email protected]). */
/* This program is distributed under the terms of the Gnu */
/* General Public License (GPL), with the following two exceptions: */
/* (1) Clause (9), dealing with updating the GPL automatically, is */
/* specifically disallowed by the author. The author will */
/* determine if a newer GPL version is still appropriate. */
/* (2) The basefiles extract accepts as input, and the extracted */
/* files it produces as output, are specifically designated as */
/* as outside the scope if this license (i.e. they are *not* */
/* required by this license to be GPL). */
/* The full, unaltered, text of the GPL is included at the end of */
/* the program source listing. */
/* ------------------------------------------------------------------ */
/* Last modified by the author on 10/21/16. */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#define NAMLEN 1024
#define LNLEN 8192
#define HANLEN 1024
#define SUBLEN 8192
#define F_nFlags 16
#define F_Case 0
#define F_RemBlank 1
#define F_Lang 2
#define F_Ugly 3
#define F_Verb 4
#define F_RepTab 5
#define F_LnLen 6
#define F_Append 7
#define F_TransNum 8
#define F_Query 9
#define F_AddKeys 10
#define F_Clint 11
#define F_FragMac 12
#define F_FragFlag 13
#define F_LLWarn 14
#define F_LocalProcs 15
#define EC_nExtCmnds 20
#define EC_Extract 0
#define EC_Skip 1
#define EC_Define 2
#define EC_Indent 3
#define EC_Key 4
#define EC_Abort 5
#define EC_MacSub 6
#define EC_EndExt 7
#define EC_Print 8
#define EC_Exp 9
#define EC_While 10
#define EC_Ifdef 11
#define EC_Dec 12
#define EC_AddKey 13
#define EC_Echo 14
#define EC_Iwhile 15
#define EC_Proc 16
#define EC_Output 17
#define EC_Iif 18
#define EC_Mif 19
#define MODE_IIF 1 /* integer if mode */
#define MODE_MIF 2 /* macro if mode */
/*
* Mode count numbers
*/
#define NMC 2
#define MCIIF 0
#define MCMIF 1
/*
* KILLMODE must be INMODE or it sets instead
*/
#define INMODE(modenv_, which_) ( (modenv_) | (which_) == (modenv_) )
#define SETMODE(modenv_, which_) (modenv_) = (modenv_) | (which_)
#define KILLMODE(modenv_, which_) (modenv_) = (modenv_) ^ (which_)
enum LANG {LangC, LangF77, LangF90, LangMake};
typedef struct fIlE2 FILE2;
struct fIlE2
{
char Fnam[NAMLEN];
unsigned int LineNo;
FILE *Fp;
FILE2 *prev;
};
typedef struct aRgStAcK ARGSTACK;
struct aRgStAcK
{
char KeyArgs[LNLEN];
int Not;
ARGSTACK *next;
};
typedef struct eXtMaC EXTMAC;
struct eXtMaC
{
int HanLen, SubLen;
char *Handle;
char *Sub;
EXTMAC *next;
};
typedef struct pUnYmAc PUNYMAC;
struct pUnYmAc
{
EXTMAC *mp;
PUNYMAC *next;
};
typedef struct kEyS KEYS;
struct kEyS
{
char *KeyHandle, *Match;
int HanLen;
EXTMAC *KeyMac;
ARGSTACK *MyArgs, *ArgStack;
KEYS *next;
};
typedef struct InDeNt INDENT;
struct InDeNt
{
int start;
int nspac;
INDENT *next;
INDENT *prev;
};
typedef struct wOrDs WORDS;
struct wOrDs
{
char *word;
WORDS *next;
};
typedef struct eXtPrOc EXTPROC;
struct eXtPrOc
{
char *ProcNam, *FileNam;
int nargs;
WORDS *argnams;
EXTPROC *next;
};
/*
* If localprocs is set, I kill not only all my procedures, but all those
* defined by subservient extracts, and all proc macros become puny
*/
typedef struct eXtEnV EXTENV;
struct eXtEnV
{
FILE2 *FpIn, *FpOut;
char Flags[F_nFlags];/* logical flag array */
char ExtCmndOn[EC_nExtCmnds];
KEYS *KeyBase; /* pointer to the list of keys */
EXTPROC *MyProcs; /* Pointer to first procedure I've defined */
EXTPROC *MyFuncs; /* Pointer to first function I've defined */
EXTMAC *MyMacBeg;
PUNYMAC *MyPunyMacs; /* Puny macros */
INDENT *clindent; /* command-line indent */
int Joining; /* are we in a mode where joining lines is OK */
};
FILE *Warn;
EXTPROC *AllProcs=NULL, *AllFuncs=NULL;
EXTMAC *MacroBase=NULL;
INDENT *indbase=NULL;
int LnCount=0, nLnNums, *LnNums;
int ExtDone=0;
char *JoinLn=NULL;
unsigned int mode=0;
unsigned int modedepth[NMC] = {0,0};
char *version = "4.0.0";
/*
* Prototypes of the functions used before their definition
*/
void HandleLine(EXTENV *EE, char *line);
void ApplyFlags(EXTENV *EE, char *line);
#define Mupcase(C) ( ((C) > 96 && (C) < 123) ? (C) & 0xDF : (C) )
#define Mlowcase(C) ( ((C) > 64 && (C) < 91) ? (C) | 32 : (C) )
#define Mciswspace(C) ( (((C) > 8) && ((C) < 14)) || ((C) == 32) )
#define Mcisnum(C) ( ((C) > 47) && ((C) < 58) )
#define Mabs(x) ( (x) < 0 ? (x) * -1 : (x) )
void *Wmalloc(size_t len)
{
void *p;
p = malloc(len);
assert(p != NULL);
return(p);
}
/*===========================================================================*/
/* The following routines are string handling routines of various flavors */
/* Before the C purists attack: I am well aware some of these routines */
/* duplicate functionality in the ANSI C standard libraries. However, I */
/* wrote the first version of extract a long time ago, and I ran into a */
/* platform where the ANSI C string libraries did not work properly . . . */
/*===========================================================================*/
int Wstrcmp(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: compares two strings pointed at by p1 & p2. */
/* RETURNS: 0 if they are not the same, else non-zero. */
/*****************************************************************************/
{
if ( (*p1) && (*p1 == *p2) )
{
do
{
p1++;
p2++;
}
while ( (*p1 == *p2) && (*p1) );
}
return( !(*p1 - *p2) );
} /* end Wstrcmp */
int WstrcmpN(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Compares 1st N characters of strings pointed to by p1 & p2. */
/* RETURNS: nonzero if they are equal, else 0 */
/*****************************************************************************/
{
while( N && (*p1++ == *p2++) ) N--;
return(!N);
}
int Wfnd_substr(char *str, char *sub)
/*****************************************************************************/
/* PURPOSE: finds if a given string (sub) is contained in another (str). */
/* RETURNS: Position of start of string (starting from 1), 0 if string */
/* is not found. */
/*****************************************************************************/
{
char ch=(*sub);
char *str2, *sub2;
int i=0;
sub++;
if (*str)
{
do
{
i++;
if (*str++ == ch) /* 1st char matches */
{
str2 = str;
sub2 = sub;
while ( (*str2) && (*str2 == *sub2) ) {str2++; sub2++;}
if ( !(*sub2) ) return(i);
}
}
while (*str);
return(0);
}
else if (ch) return(0);
else return(1);
}
int Wstrlen(char *p1)
/*****************************************************************************/
/* PURPOSE: finds the number of characters in a string, not including \0. */
/* RETURNS: Length of string, not including null terminator. */
/*****************************************************************************/
{
int length=0;
if (*p1) do length++; while (*(++p1));
return (length);
}
int Wstrcpy(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
if (*p1++ = *p2++) do i++; while(*p1++ = *p2++);
return(i);
}
void WstrcpyN(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1. */
/*****************************************************************************/
{
if (N) do *p1++ = *p2++; while(--N);
}
void WstrcpyN_LOW(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1 while lowcasing. */
/*****************************************************************************/
{
if (N)
{
do
{
*p1++ = Mlowcase(*p2);
p2++;
}
while(--N);
}
}
void WstrcpyN_UP(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1 while upcasing. */
/*****************************************************************************/
{
if (N)
{
do
{
*p1++ = Mupcase(*p2);
p2++;
}
while(--N);
}
}
int WstrcpyUP(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1, and upcases. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
while( *p1++ = Mupcase(*p2) )
{
i++;
p2++;
}
return(i);
}
int WstrcpyLOW(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1, and lowcases. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
while( *p1++ = Mlowcase(*p2) )
{
i++;
p2++;
}
return(i);
}
int Wsafe_slowcase(char line[], int maxlen)
/*****************************************************************************/
/* PURPOSE: Changes string to lowercase, not touching quoted strings. */
/* RETURNS: -1 if there is an unmatched quote, else 0. */
/* NOTE : algorithm fails if have two strings: 'ST1' STUFF 'ST2' */
/* if you wanna fix, need to know the language */
/*****************************************************************************/
{
int i, ERROR=0;
char quote;
for (i=0; (line[i] && (i < maxlen)); i++)
{
line[i] = Mlowcase(line[i]);
if (line[i] == 34 || line[i] == 39 || line[i] == '`') /* is a " or ' */
{
quote = line[i++];
while(line[i]) i++;
while(line[i] != quote) i--;
if (i > maxlen) ERROR = -1;
}
}
return(ERROR);
}
int Wsafe_supcase(char line[], int maxlen)
/*****************************************************************************/
/* PURPOSE: Changes line to uppercase, not touching quoted strings. */
/* RETURNS: -1 if there is an unmatched quote, else 0. */
/* NOTE : algorithm fails if have two strings: 'ST1' STUFF 'ST2' */
/* if you wanna fix, need to know the language */
/*****************************************************************************/
{
int i, ERROR=0;
char quote;
for (i=0; (line[i] && (i < maxlen)); i++)
{
line[i] = Mupcase(line[i]);
if (line[i] == 34 || line[i] == 39 || line[i] == '`') /* is a " or ' */
{
quote = line[i++];
while ( (line[i] - quote) && (line[i]) && (i < maxlen) ) i++;
if ( line[i] != quote ) ERROR = -1; /* unmatched quote */
}
}
return(ERROR);
}
void Wslowcase(char *p1)
/*****************************************************************************/
/* PURPOSE: Changes to lowercase entire string pointed to by p1. */
/*****************************************************************************/
{
while(*p1 = Mlowcase(*p1)) p1++;
}
void Wsupcase(char *p1)
/*****************************************************************************/
/* PURPOSE: Changes to uppercase entire string pointed to by p1. */
/*****************************************************************************/
{
while(*p1 = Mupcase(*p1)) p1++;
}
void Wsstrip(char *p1)
/*****************************************************************************/
/* PURPOSE: strip all white spaces from string */
/*****************************************************************************/
{
char *p2=p1;
do
{
if (Mciswspace(*p2)) p2++;
else *p1++ = *p2++;
}
while (*p1);
}
void Wtab2spcs(char *p, int N)
/*****************************************************************************/
/* PURPOSE: replace all tabs in string p with N spaces */
/*****************************************************************************/
{
char tline[256];
int i;
int Wstrcpy(char *p1, char *p2);
while (*p)
{
if (*p == '\t')
{
Wstrcpy(tline, p+1);
i = N;
while (N--) *p++ = ' ';
Wstrcpy(p, tline);
}
else p++;
}
}
int Wstr2int(char *str, int *iptr)
/*****************************************************************************/
/* PURPOSE: read an integer from string str into *iptr */
/* RETURNS: number of characters read in */
/*****************************************************************************/
{
int i, j;
sscanf(str, "%d", iptr);
for (i=0, j=Mabs(*iptr); j > 0; i++) j /= 10;
if (*iptr == 0) i=1;
if (str[0] == '+' || str[0] == '-') i++;
return(i);
}
void Wsafe_mws2spc(char *p1)
/*****************************************************************************/
/* PURPOSE: replaces contiguous white spaces with 1 space, skip quotes. */
/*****************************************************************************/
{
char *p2;
p2 = p1;
while (*p2)
{
if (*p1 == '"')
{
do *p2++ = *p1++; while(*p1 != '"');
*p2++ = *p1++;
continue;
}
if (Mciswspace(*p1))
{
*p2++ = ' ';
while (Mciswspace(*p1)) p1++;
continue;
}
while ( (*p2) && !(Mciswspace(*p1)) ) *p2++ = *p1++;
}
}
void Wremove_trailing_blanks(char *p)
/*****************************************************************************/
/* PURPOSE: Remove trailing whitespace from string p. */
/*****************************************************************************/
{
char *st=p;
if ( !(*p) ) return;
do p++; while (*p);
p--;
while ( (Mciswspace(*p)) && (p != st) ) p--;
if ( Mciswspace(*p) )
{
*p = '\n';
p[1] = '\0';
}
else
{
p[1] = '\n';
p[2] = '\0';
}
}
void CharSet(int N, char *x, char val)
/*****************************************************************************/
/* Set N elements starting at x to val */
/*****************************************************************************/
{
int i;
for (i=0; i < N; i++) x[i] = val;
}
int commandcpy(char *out, char *in)
/*****************************************************************************/
/* PURPOSE: copy in to out, remove all leading spaces, put 1 space after */
/* first word, lowcase first word, then copy rest string unchanged. */
/* RETURNS: Length of first word (the possible command). */
/*****************************************************************************/
{
int i=0;
while( Mciswspace(*in) ) in++; /* skip leading white spaces */
while( (*in) && !Mciswspace(*in) ) /* copy first word */
{
*out++ = Mlowcase(*in);
in++;
i++;
}
/*
* Only 1 space follows word, and copy rest of line unaltered
*/
*out++ = ' ';
while( Mciswspace(*in) ) in++;
while (*out++ = *in++);
return(i);
}
void keycpy(char *out, char *in)
/*****************************************************************************/
/* PURPOSE: copy in to out, remove all leading spaces, lowcase and put 1 */
/* space after each word, and leave stuff in ` ` alone. */
/*****************************************************************************/
{
char *p, *ch;
p = out;
while (*in)
{
while( Mciswspace(*in) ) in++; /* skip leading spaces */
if (*in == '`') /* don't mess with ` ` quotes */
{
ch = in;
while (*ch) ch++;
while (*ch != '`') ch--;
while (in != ch) *out++ = *in++;
*out++ = *in++;
}
while( (*in) && !Mciswspace(*in) ) /* copy word */
{
*out++ = Mlowcase(*in);
in++;
}
*out++ = ' '; /* white spaces collapse to 1 space */
if (*in) in++;
}
*out = '\0';
}
/*===========================================================================*/
/* These functions print out error and warning messages from extract */
/*===========================================================================*/
void ExtWarn(EXTENV *EE, char *form, ...)
{
FILE2 *fp;
va_list argptr;
char cline[256];
int i;
va_start(argptr, form);
vsprintf(cline, form, argptr);
va_end(argptr);
/*
* Remove newlines from cline
*/
for (i=0; cline[i]; i++);
i--;
if (cline[i] == '\n') cline[i--] = '\0';
if (EE)
{
fflush(EE->FpOut->Fp);
fprintf(stderr, "\nExtract warning \'%s\'.\n", cline);
fprintf(stderr, "Input file chain:\n");
fprintf(stderr, " Warning occured on line %d of file %s.\n",
EE->FpIn->LineNo, EE->FpIn->Fnam);
for (fp=EE->FpIn->prev; fp; fp = fp->prev)
fprintf(stderr, " Which was extracted from line %d of file %s.\n",
fp->LineNo, fp->Fnam);
fprintf(stderr, "\nWarning occured after line %d of output file %s.\n",
EE->FpOut->LineNo, EE->FpOut->Fnam);
}
else fprintf(stderr, "\nExtract warning \'%s\'\n", cline);
}
void ExtErr(EXTENV *EE, char *form, ...)
{
FILE2 *fp;
va_list argptr;
char cline[256];
int i;
va_start(argptr, form);
vsprintf(cline, form, argptr);
va_end(argptr);
/*
* Remove newlines from cline
*/
for (i=0; cline[i]; i++);
i--;
if (cline[i] == '\n') cline[i--] = '\0';
if (EE)
{
if (EE->FpOut && EE->FpOut->Fp) fflush(EE->FpOut->Fp);
fprintf(stderr, "\nExtract ERROR \'%s\'.\n", cline);
if (EE->FpIn)
{
fprintf(stderr, "Input file chain:\n");
fprintf(stderr, " Error occured on line %d of file %s.\n",
EE->FpIn->LineNo, EE->FpIn->Fnam);
for (fp=EE->FpIn->prev; fp; fp = fp->prev)
fprintf(stderr,
" Which was extracted from line %d of file %s.\n",
fp->LineNo, fp->Fnam);
fprintf(stderr, "\nError occured after line %d of output file %s.\n\n",
EE->FpOut->LineNo, EE->FpOut->Fnam);
}
}
else fprintf(stderr, "\nExtract ERROR \'%s\'\n\n", cline);
exit(1);
}
void HandleAtEscape(char *ln)
{
int i;
while (i = Wfnd_substr(ln, "@(@)"))
{
Wstrcpy(ln+i, ln+i+3);
ln += i;
}
}
/*===========================================================================*/
/* These routines do basic file stuff */
/*===========================================================================*/
void PutLn(EXTENV *EE, char *line)
{
int ExtLn, h, i=0, k;
static int j=0;
char *tln;
/*
* Check if line needs to be joined with next line
*/
if (EE->Joining)
{
while(line[i]) i++;
if (i)
{
i--;
while(Mciswspace(line[i]) && i) i--;
}
ExtLn = (line[i] == '\\') && (i);
if (ExtLn) ExtLn = line[i-1] == '@';
/*
* If we are extending a line, or have an extended line in memory,
* save the present line, and merge it with any previously stored lines
*/
if ( ExtLn || JoinLn )
{
tln = malloc(i + j);
for (k=0; k != j; k++) tln[k] = JoinLn[k];
if (ExtLn) h = i + j - 1;
else h = i + j + 1;
for (; k < h; k++) tln[k] = line[k-j];
tln[k] = '\n';
tln[k+1] = '\0';
j = h;
if (JoinLn) free(JoinLn);
JoinLn = tln;
}
else tln = line;
}
else
{
tln = line;
ExtLn = 0;
}
/*
* If this line wasn't extended, can finally dump to file
*/
if (!ExtLn)
{
EE->FpOut->LineNo++;
if (!LnCount)
{
ApplyFlags(EE, tln);
HandleAtEscape(tln);
fputs(tln, EE->FpOut->Fp);
}
else if (EE->FpOut->LineNo == LnNums[nLnNums])
{
fprintf(Warn,
"\nLine %d of extracted file corresponds to line %d of \'%s\'\n",
EE->FpOut->LineNo, EE->FpIn->LineNo, EE->FpIn->Fnam);
nLnNums--;
if (nLnNums == 0)
{
fprintf(Warn, "\n\nEXTRACT finished translating line numbers.\n\n");
exit(0);
}
}
if (JoinLn) free(JoinLn);
JoinLn = NULL;
j = 0;
}
}
int GetLn(EXTENV *EE, char *line)
{
if (ExtDone) return(0);
EE->FpIn->LineNo++;
/* return( (int) fgets(line, LNLEN, EE->FpIn->Fp) ); */
return( (fgets(line, LNLEN, EE->FpIn->Fp) != NULL) );
}
FILE2 *OpenFile(EXTENV *EE, char *Fnam, char *mode)
{
char yorn;
FILE2 *fp;
fp = (FILE2 *) malloc(sizeof(*fp));
Wstrcpy(fp->Fnam, Fnam);
fp->LineNo = 0;
if (mode[0] == 'w')
{
fp->prev = EE->FpOut;
if ( Wstrcmp(Fnam, "stdout") )
{
fp->Fp = stdout;
return(fp);
}
else if ( Wstrcmp(Fnam, "stderr") )
{
fp->Fp = stderr;
return(fp);
}
if (EE->Flags[F_Verb] > 2) fprintf(Warn, "Creating file %s.\n", Fnam);
if (EE->Flags[F_Query])
{
if ( !access(Fnam, 0) )
{
fprintf(Warn, "File %s already exists. Overwrite?", Fnam);
fflush(stdin);
yorn = getchar();
if ( (yorn != 'y') && (yorn != 'Y') ) exit(0);
}
}
}
else if ( (mode[0] == 'r') && (mode[1] == '\0') )
{
fp->prev = EE->FpIn;
if ( Wstrcmp(Fnam, "stdin") )
{
fp->Fp = stdin;
return(fp);
}
if (EE->Flags[F_Verb] > 2)
fprintf(Warn, "Opening file \'%s\' for input . . .\n", Fnam);
if ( access(Fnam, 0) )
{
fprintf(Warn, "Cannot find input file \'%s\'\n", Fnam);
exit(1);
}
}
fp->Fp = fopen(Fnam, mode);
if (fp->Fp == NULL) ExtErr(EE, "Error opening file %s",Fnam);
return(fp);
}
void CloseFile(EXTENV *EE, FILE2 *fp)
{
if (JoinLn) if (EE->FpOut == fp) PutLn(EE, "\0");
if (EE->Flags[F_Verb] > 2) fprintf(Warn, "Closing file \'%s\'.\n", fp->Fnam);
if ( (fp->Fp != stderr) && (fp->Fp != stdin) && (fp->Fp != stdout) )
{
if ( fclose(fp->Fp) == EOF )
ExtErr(EE, "Error closing file %s", fp->Fnam);
}
free(fp);
} /* end close_file */
/*===========================================================================*/
/* These functions all deal with breaking lines into their respective words */
/*===========================================================================*/
static WORDS *GetWord(char *wrd)
/*****************************************************************************/
/* PURPOSE: Safely allocates word of correct length */
/* RETURNS: Allocated WORD. */
/*****************************************************************************/
{
WORDS *wp;
wp = malloc(sizeof(WORDS));
wp->word = malloc( (Wstrlen(wrd)+1)*sizeof(char) );
Wstrcpy(wp->word, wrd);
wp->next = NULL;
return(wp);
}
static WORDS *KillWord(WORDS *wbase, WORDS *wkill)
/*****************************************************************************/
/* PURPOSE: Kills single word wkill in list of words starting with wbase. */
/* RETURNS: Possibly new wbase. */
/*****************************************************************************/
{
WORDS *wptr;
if (wkill == wbase) wbase = wbase->next;
else
{
for (wptr=wbase; wptr->next != wkill; wptr = wptr->next);
wptr->next = wkill->next;
}
free(wkill->word);
free(wkill);
return(wbase);
}
static void KillWords(WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: Frees space used by words, starting at wbase. */
/*****************************************************************************/
{
while(wbase) wbase = KillWord(wbase, wbase);
}
void PrintWords(WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: print words pointed to by wbase (debugging routine) */
/*****************************************************************************/
{
printf("Words: ");
while(wbase)
{
printf("'%s' ",wbase->word);
wbase = wbase->next;
}
printf("\n");
}
WORDS *GetVars(char *line, char Ab, char Ae)
/*****************************************************************************/
/* PURPOSE: splits a line into its seperate variables for @declare. Ab and */
/* Ae are the array beginning and ending characters for your */
/* language ([] for C, and () for Fortran). Variables are */
/* seperated by whitespace(s) and/or a comma. */
/*****************************************************************************/
{
int i=0, j, k;
WORDS *wbase=NULL, *wp;
static WORDS *wptr=NULL;
char wrd[LNLEN];
if (line == NULL)
{
wptr = NULL;
return(NULL);
}
while(Mciswspace(line[i])) i++;
if (line[i] == '\0') return(NULL);
wbase = GetWord(" ");
if (wptr)
{
wp = wptr;
wptr->next = wbase;
}
else wp = wbase;
wptr = wbase;
while(line[i])
{
j = 0;
while(line[i] != ',') /* get one word */
{
if (line[i] == '@') /* check for sticky space */
{
if (line[i+1] == '^')
{
wrd[j++] = ' ';
i += 2;
continue;
}
}
if (Mciswspace(line[i]))
{
while(Mciswspace(line[i])) i++;
if (line[i] != Ab) break;
else continue;
}
if (line[i] == Ab) /* have an array declaration */
{
k = 0;
do
{
wrd[j++] = line[i];
if (line[i] == Ab) k++;
else if (line[i] == Ae) k--;
i++;
}
while(k);
if (!line[i]) break;
}
else wrd[j++] = line[i++];
}
wrd[j] = '\0';
wptr->next = GetWord(wrd);
wptr = wptr->next;
if (line[i] == ',')
{
i++;
while(Mciswspace(line[i])) i++;
}
}
if (wptr == wbase) wptr = wbase = KillWord(wp, wbase);
else wbase = KillWord(wp, wbase);
return(wbase);
}
WORDS *GetArgs(EXTENV *EE, char *line)
/*****************************************************************************/
/* PURPOSE: splits a line into its seperate words by splitting on whitespcs, */
/* accepts args in double quotes as one arg. */
/*****************************************************************************/
{
int i=0, j;
WORDS *wptr, *wbase=NULL;
char word[LNLEN];
while(Mciswspace(line[i])) i++;
wptr = wbase = GetWord(" ");
while(line[i])
{
if (line[i] == '\"')
{
i++;
for (j=0; (line[i] != '\"') && line[i]; word[j++] = line[i++]);
if (!line[i]) ExtErr(EE, "Unmatched double quote.");
i++;