-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixstr.c
146 lines (121 loc) · 3.38 KB
/
fixstr.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
/* This utility program is not part of the sources to Omega.
It was written by Nathan Glasser [email protected] (internet)
[email protected] (usenet).
As such, it is copyright by Nathan Glasser, 1987,1988,1989.
Please don't attempt to sell this program or take credit for it
yourself, and please don't remove this notice.
*/
/* This program reads in a C source file and replaces all static strings
with variable names defined above in the file, which are all declared
to be far arrays. You don't need to run this for the unix version. */
/* This program does not know about comments, or \'s in front of "'s.
Thus it can be broken. */
#include <stdio.h>
#include <malloc.h>
#define TMPFILE "fixtmp.c"
int num_strings;
char **string_list;
#define REALLOC_INCR 500
main(argc,argv)
int argc;
char **argv;
{
FILE *sourcefp,*destfp;
if (argc != 2)
{
printf("Usage: %s <cfile>\n",argv[0]);
exit(1);
}
if ((sourcefp = fopen(argv[1],"r")) == NULL ||
(destfp = fopen(TMPFILE,"w")) == NULL)
{
perror("Can't open a file (pass1)");
exit(1);
}
printf("Scanning %s...",argv[1]);
fflush(stdout);
do_scan(sourcefp,destfp);
printf("Done\n");
fclose(sourcefp);
fclose(destfp);
if ((sourcefp = fopen(TMPFILE,"r")) == NULL ||
(destfp = fopen(argv[1],"w")) == NULL)
{
perror("Can't open a file (pass2)");
exit(1);
}
printf("Writing new %s...",argv[1]);
fflush(stdout);
do_output(sourcefp,destfp);
printf("Done\n");
remove(TMPFILE);
exit(0);
}
char include[] = "#include";
#define include_size (sizeof(include) - 1)
do_scan(sourcefp,destfp)
FILE *sourcefp,*destfp;
{
int max_strings;
int ch,last_ch = EOF;
char temp_string[128],*temp;
/* Vars for figuring out about #include's */
int pos = 0,include_flag = 0,include_tmp = 1;
string_list = (char **)malloc((max_strings = REALLOC_INCR) *
sizeof(char *));
while ((ch = getc(sourcefp)) != EOF)
{
switch (ch)
{
case '\n':
putc(ch,destfp);
pos = include_flag = 0;
include_tmp = 1;
break;
case '"':
if (!include_flag && last_ch != '\'')
{
/* Start of a string */
for (temp = temp_string; (*temp = getc(sourcefp)) != '"';
temp++);
*temp = '\0';
strcpy((string_list[num_strings] =
(char *)malloc(temp - temp_string + 1)),temp_string);
fprintf(destfp,"_str_%d",num_strings);
if (++num_strings == max_strings)
string_list = (char **)realloc(string_list,
(max_strings += REALLOC_INCR) * sizeof(char *));
include_tmp = 0;
break;
}
default:
if (include_tmp)
{
if ((include_tmp = ch == include[pos++]) &&
pos == include_size)
{
include_flag = 1;
include_tmp = 0;
}
}
putc(ch,destfp);
break;
}
last_ch = ch;
}
}
do_output(sourcefp,destfp)
FILE *sourcefp,*destfp;
{
char buf[1024];
int i;
fprintf(destfp,"/* These static strings have been moved here to */\n");
fprintf(destfp,"/* declare them as far and avoid having too much */\n");
fprintf(destfp,"/* initialized memory in the CONST segment. */\n\n");
for (i = 0; i < num_strings; i++)
fprintf(destfp,"static char far _str_%d[] = \"%s\";\n",
i,string_list[i]);
putc('\n',destfp);
while (i = fread(buf,1,sizeof(buf),sourcefp))
fwrite(buf,i,1,destfp);
}