summaryrefslogtreecommitdiff
path: root/utils/h2pas/h2paspp.pas
blob: 3478d66797a6febddbf295f89adf76a7e3904262 (plain)
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
{
    Copyright (c) 2000 by Peter Vreman

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

 ****************************************************************************}
program h2paspp;

type
  PSymbol=^TSymbol;
  TSymbol=record
    name : string[32];
    next : PSymbol;
  end;
var
  Symbols : PSymbol;
  OutFile : string;


procedure def_symbol(const s:string);
var
  p : PSymbol;
begin
  new(p);
  p^.name:=s;
  p^.next:=Symbols;
  Symbols:=p;
end;

procedure undef_symbol(const s:string);
var
  p,plast : PSymbol;
begin
  p:=Symbols;
  plast:=nil;
  while assigned(p) do
   begin
     if p^.name=s then
      begin
        if assigned(plast) then
         plast^.next:=p^.next
        else
         Symbols:=p^.next;
        dispose(p);
        exit;
      end;
     p:=p^.next;
   end;
end;

function check_symbol(const s:string):boolean;
var
  p : PSymbol;
begin
  check_symbol:=false;
  p:=Symbols;
  while assigned(p) do
   begin
     if p^.name=s then
      begin
        check_symbol:=true;
        exit;
      end;
     p:=p^.next;
   end;
end;

procedure clear_symbols;
var
  hp : PSymbol;
begin
  while assigned(Symbols) do
   begin
     hp:=Symbols;
     Symbols:=Symbols^.next;
     dispose(hp);
   end;
end;

function dofile(const filename : string):boolean;

  procedure RemoveSpace(var fn:string);
  var
    i : longint;
  begin
    i:=0;
    while (i<length(fn)) and (fn[i+1] in [' ',#9]) do
      inc(i);
    Delete(fn,1,i);
    i:=length(fn);
    while (i>0) and (fn[i] in [' ',#9]) do
      dec(i);
    fn:=copy(fn,1,i);
  end;

  function GetName(var fn:string):string;
  var
    i : longint;
  begin
    i:=0;
    while (i<length(fn)) and (fn[i+1] in ['a'..'z','A'..'Z','0'..'9','_','-']) do
     inc(i);
    GetName:=Copy(fn,1,i);
    Delete(fn,1,i);
  end;

const
  maxlevel=16;
var
  f,g   : text;
  s,orgs,
  opts  : string;
  skip  : array[0..maxlevel-1] of boolean;
  level : longint;
begin
  dofile:=false;
{ open file }
  assign(f,filename);
  {$I-}
  reset(f);
  {$I+}
  if ioresult<>0 then
   begin
     Writeln('Unable to open file ',filename);
     exit;
   end;
  if outfile='' then
   assign(g,'h2paspp.tmp')
  else
   assign(g,outfile);
  {$I-}
  rewrite(g);
  {$I+}
  if ioresult<>0 then
   begin
     Writeln('Unable to create file tmp');
     Close(f);
     exit;
   end;
  fillchar(skip,sizeof(skip),0);
  level:=0;
  while not eof(f) do
   begin
     readln(f,orgs);
     opts:=orgs;
     if (opts<>'') and (opts[1]='#') then
      begin
        Delete(opts,1,1);
        RemoveSpace(opts);
        s:=GetName(opts);
        if (s='ifdef') then
         begin
           RemoveSpace(opts);
           if Level>=maxlevel then
            begin
              Writeln('Too many ifdef levels');
              exit;
            end;
           inc(Level);
           skip[level]:=(skip[level-1] or (not check_symbol(GetName(opts))));
         end
        else
         if (s='if') then
          begin
            RemoveSpace(opts);
            if Level>=maxlevel then
             begin
               Writeln('Too many ifdef levels');
               exit;
             end;
            inc(Level);
            skip[level]:=(skip[level-1] or (not check_symbol(GetName(opts))));
          end
        else
         if (s='ifndef') then
          begin
            RemoveSpace(opts);
            if Level>=maxlevel then
             begin
               Writeln('Too many ifdef levels');
               exit;
             end;
            inc(Level);
            skip[level]:=(skip[level-1] or (check_symbol(GetName(opts))));
          end
        else
         if (s='else') then
          skip[level]:=skip[level-1] or (not skip[level])
        else
         if (s='endif') then
          begin
            skip[level]:=false;
            if Level=0 then
             begin
               Writeln('Too many endif found');
               exit;
             end;
            dec(level);
          end
        else
         if (not skip[level]) then
          begin
            if (s='define') then
             begin
               RemoveSpace(opts);
               def_symbol(GetName(opts));
             end
            else
             if (s='undef') then
              begin
                RemoveSpace(opts);
                undef_symbol(GetName(opts));
              end
            else
             if (s='include') then
              begin
                RemoveSpace(opts);
                Writeln('Uses include: ',opts);
                opts:='';
              end;
            { Add defines also to the output }
            if opts<>'' then
             writeln(g,orgs);
          end;
       end
      else
       begin
         if (not skip[level]) then
          writeln(g,orgs);
       end;
   end;
  if Level>0 then
   Writeln('Error: too less endif found');
  Close(f);
  Close(g);
  if outfile='' then
   begin
     Erase(f);
     Rename(g,filename);
   end;
  DoFile:=true;
end;


procedure Usage;
begin
  writeln('h2paspp [options] <file(s)>');
  writeln('options:');
  writeln('  -d<symbol>   define symbol');
  writeln('  -o<outfile>  output file');
  writeln('  -i           include also includes (default is to remove)');
  writeln('  -h or -?     this helpscreen');
  halt(0);
end;


var
  i,j : longint;
  s : string;
begin
{ process options }
  j:=0;
  for i:=1to paramcount do
   begin
     s:=paramstr(i);
     if s[1]='-' then
      begin
        case s[2] of
         'd' :
           def_symbol(Copy(s,3,255));
         'o' :
           outfile:=Copy(s,3,255);
         'h','?' :
           Usage;
        end;
      end
     else
      inc(j);
   end;
  { no files? }
  if j=0 then
   Usage;
{ process files }
  for i:=1to paramcount do
   begin
     s:=paramstr(i);
     if s[1]<>'-' then
      dofile(s);
   end;
end.