1 /* This utility program is not part of the sources to Omega.
2 It was written by Nathan Glasser nathan@brokaw.lcs.mit.edu (internet)
3 nathan@mit-eddie.uucp (usenet).
4 As such, it is copyright by Nathan Glasser, 1987,1988,1989.
5 Please don't attempt to sell this program or take credit for it
6 yourself, and please don't remove this notice.
7 */
8
9
10 /* This program reads in a C source file and replaces all static strings
11 with variable names defined above in the file, which are all declared
12 to be far arrays. You don't need to run this for the unix version. */
13
14 /* This program does not know about comments, or \'s in front of "'s.
15 Thus it can be broken. */
16
17 #include <stdio.h>
18 #ifndef __FreeBSD__
19 #include <malloc.h>
20 #endif
21
22 #define TMPFILE "fixtmp.c"
23
24 int num_strings;
25 char **string_list;
26
27 #define REALLOC_INCR 500
28
29
main(argc,argv)30 main(argc,argv)
31 int argc;
32 char **argv;
33 {
34 FILE *sourcefp,*destfp;
35
36 if (argc != 2)
37 {
38 printf("Usage: %s <cfile>\n",argv[0]);
39 exit(1);
40 }
41
42 if ((sourcefp = fopen(argv[1],"r")) == NULL ||
43 (destfp = fopen(TMPFILE,"w")) == NULL)
44 {
45 perror("Can't open a file (pass1)");
46 exit(1);
47 }
48
49 printf("Scanning %s...",argv[1]);
50 fflush(stdout);
51 do_scan(sourcefp,destfp);
52 printf("Done\n");
53
54 fclose(sourcefp);
55 fclose(destfp);
56
57 if ((sourcefp = fopen(TMPFILE,"r")) == NULL ||
58 (destfp = fopen(argv[1],"w")) == NULL)
59 {
60 perror("Can't open a file (pass2)");
61 exit(1);
62 }
63
64 printf("Writing new %s...",argv[1]);
65 fflush(stdout);
66 do_output(sourcefp,destfp);
67 printf("Done\n");
68 remove(TMPFILE);
69 exit(0);
70 }
71
72 char include[] = "#include";
73 #define include_size (sizeof(include) - 1)
74
do_scan(sourcefp,destfp)75 do_scan(sourcefp,destfp)
76 FILE *sourcefp,*destfp;
77 {
78 int max_strings;
79 int ch,last_ch = EOF;
80 char temp_string[128],*temp;
81
82 /* Vars for figuring out about #include's */
83 int pos = 0,include_flag = 0,include_tmp = 1;
84
85 string_list = (char **)malloc((max_strings = REALLOC_INCR) *
86 sizeof(char *));
87
88 while ((ch = getc(sourcefp)) != EOF)
89 {
90 switch (ch)
91 {
92 case '\n':
93 putc(ch,destfp);
94 pos = include_flag = 0;
95 include_tmp = 1;
96 break;
97 case '"':
98 if (!include_flag && last_ch != '\'')
99 {
100 /* Start of a string */
101 for (temp = temp_string; (*temp = getc(sourcefp)) != '"';
102 temp++);
103 *temp = '\0';
104 strcpy((string_list[num_strings] =
105 (char *)malloc(temp - temp_string + 1)),temp_string);
106 fprintf(destfp,"_str_%d",num_strings);
107 if (++num_strings == max_strings)
108 string_list = (char **)realloc(string_list,
109 (max_strings += REALLOC_INCR) * sizeof(char *));
110 include_tmp = 0;
111 break;
112 }
113 default:
114 if (include_tmp)
115 {
116 if ((include_tmp = ch == include[pos++]) &&
117 pos == include_size)
118 {
119 include_flag = 1;
120 include_tmp = 0;
121 }
122 }
123 putc(ch,destfp);
124 break;
125 }
126 last_ch = ch;
127 }
128 }
129
do_output(sourcefp,destfp)130 do_output(sourcefp,destfp)
131 FILE *sourcefp,*destfp;
132 {
133 char buf[1024];
134 int i;
135
136 fprintf(destfp,"/* These static strings have been moved here to */\n");
137 fprintf(destfp,"/* declare them as far and avoid having too much */\n");
138 fprintf(destfp,"/* initialized memory in the CONST segment. */\n\n");
139
140 for (i = 0; i < num_strings; i++)
141 fprintf(destfp,"static char far _str_%d[] = \"%s\";\n",
142 i,string_list[i]);
143
144 putc('\n',destfp);
145
146 while (i = fread(buf,1,sizeof(buf),sourcefp))
147 fwrite(buf,i,1,destfp);
148 }
149