h2paspp.pas 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. {
  2. $Id$
  3. Copyright (c) 2000 by Peter Vreman
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************}
  16. program h2paspp;
  17. type
  18. PSymbol=^TSymbol;
  19. TSymbol=record
  20. name : string[32];
  21. next : PSymbol;
  22. end;
  23. var
  24. Symbols : PSymbol;
  25. OutFile : string;
  26. procedure def_symbol(const s:string);
  27. var
  28. p : PSymbol;
  29. begin
  30. new(p);
  31. p^.name:=s;
  32. p^.next:=Symbols;
  33. Symbols:=p;
  34. end;
  35. procedure undef_symbol(const s:string);
  36. var
  37. p,plast : PSymbol;
  38. begin
  39. p:=Symbols;
  40. plast:=nil;
  41. while assigned(p) do
  42. begin
  43. if p^.name=s then
  44. begin
  45. if assigned(plast) then
  46. plast^.next:=p^.next
  47. else
  48. Symbols:=p^.next;
  49. dispose(p);
  50. exit;
  51. end;
  52. p:=p^.next;
  53. end;
  54. end;
  55. function check_symbol(const s:string):boolean;
  56. var
  57. p : PSymbol;
  58. begin
  59. check_symbol:=false;
  60. p:=Symbols;
  61. while assigned(p) do
  62. begin
  63. if p^.name=s then
  64. begin
  65. check_symbol:=true;
  66. exit;
  67. end;
  68. p:=p^.next;
  69. end;
  70. end;
  71. procedure clear_symbols;
  72. var
  73. hp : PSymbol;
  74. begin
  75. while assigned(Symbols) do
  76. begin
  77. hp:=Symbols;
  78. Symbols:=Symbols^.next;
  79. dispose(hp);
  80. end;
  81. end;
  82. function dofile(const filename : string):boolean;
  83. procedure RemoveSpace(var fn:string);
  84. var
  85. i : longint;
  86. begin
  87. i:=0;
  88. while (i<length(fn)) and (fn[i+1] in [' ',#9]) do
  89. inc(i);
  90. Delete(fn,1,i);
  91. i:=length(fn);
  92. while (i>0) and (fn[i] in [' ',#9]) do
  93. dec(i);
  94. fn:=copy(fn,1,i);
  95. end;
  96. function GetName(var fn:string):string;
  97. var
  98. i : longint;
  99. begin
  100. i:=0;
  101. while (i<length(fn)) and (fn[i+1] in ['a'..'z','A'..'Z','0'..'9','_','-']) do
  102. inc(i);
  103. GetName:=Copy(fn,1,i);
  104. Delete(fn,1,i);
  105. end;
  106. const
  107. maxlevel=16;
  108. var
  109. f,g : text;
  110. s,orgs,
  111. opts : string;
  112. skip : array[0..maxlevel-1] of boolean;
  113. level : longint;
  114. begin
  115. dofile:=false;
  116. { open file }
  117. assign(f,filename);
  118. {$I-}
  119. reset(f);
  120. {$I+}
  121. if ioresult<>0 then
  122. begin
  123. Writeln('Unable to open file ',filename);
  124. exit;
  125. end;
  126. if outfile='' then
  127. assign(g,'h2paspp.tmp')
  128. else
  129. assign(g,outfile);
  130. {$I-}
  131. rewrite(g);
  132. {$I+}
  133. if ioresult<>0 then
  134. begin
  135. Writeln('Unable to create file tmp');
  136. Close(f);
  137. exit;
  138. end;
  139. fillchar(skip,sizeof(skip),0);
  140. level:=0;
  141. while not eof(f) do
  142. begin
  143. readln(f,orgs);
  144. opts:=orgs;
  145. if (opts<>'') and (opts[1]='#') then
  146. begin
  147. Delete(opts,1,1);
  148. RemoveSpace(opts);
  149. s:=GetName(opts);
  150. if (s='ifdef') then
  151. begin
  152. RemoveSpace(opts);
  153. if Level>=maxlevel then
  154. begin
  155. Writeln('Too many ifdef levels');
  156. exit;
  157. end;
  158. inc(Level);
  159. skip[level]:=(skip[level-1] or (not check_symbol(GetName(opts))));
  160. end
  161. else
  162. if (s='if') then
  163. begin
  164. RemoveSpace(opts);
  165. if Level>=maxlevel then
  166. begin
  167. Writeln('Too many ifdef levels');
  168. exit;
  169. end;
  170. inc(Level);
  171. skip[level]:=(skip[level-1] or (not check_symbol(GetName(opts))));
  172. end
  173. else
  174. if (s='ifndef') then
  175. begin
  176. RemoveSpace(opts);
  177. if Level>=maxlevel then
  178. begin
  179. Writeln('Too many ifdef levels');
  180. exit;
  181. end;
  182. inc(Level);
  183. skip[level]:=(skip[level-1] or (check_symbol(GetName(opts))));
  184. end
  185. else
  186. if (s='else') then
  187. skip[level]:=skip[level-1] or (not skip[level])
  188. else
  189. if (s='endif') then
  190. begin
  191. skip[level]:=false;
  192. if Level=0 then
  193. begin
  194. Writeln('Too many endif found');
  195. exit;
  196. end;
  197. dec(level);
  198. end
  199. else
  200. if (not skip[level]) then
  201. begin
  202. if (s='define') then
  203. begin
  204. RemoveSpace(opts);
  205. def_symbol(GetName(opts));
  206. end
  207. else
  208. if (s='undef') then
  209. begin
  210. RemoveSpace(opts);
  211. undef_symbol(GetName(opts));
  212. end
  213. else
  214. if (s='include') then
  215. begin
  216. RemoveSpace(opts);
  217. Writeln('Uses include: ',opts);
  218. opts:='';
  219. end;
  220. { Add defines also to the output }
  221. if opts<>'' then
  222. writeln(g,orgs);
  223. end;
  224. end
  225. else
  226. begin
  227. if (not skip[level]) then
  228. writeln(g,orgs);
  229. end;
  230. end;
  231. if Level>0 then
  232. Writeln('Error: too less endif found');
  233. Close(f);
  234. Close(g);
  235. if outfile='' then
  236. begin
  237. Erase(f);
  238. Rename(g,filename);
  239. end;
  240. DoFile:=true;
  241. end;
  242. procedure Usage;
  243. begin
  244. writeln('h2paspp [options] <file(s)>');
  245. writeln('options:');
  246. writeln(' -d<symbol> define symbol');
  247. writeln(' -o<outfile> output file');
  248. writeln(' -i include also includes (default is to remove)');
  249. writeln(' -h or -? this helpscreen');
  250. halt(0);
  251. end;
  252. var
  253. i,j : longint;
  254. s : string;
  255. begin
  256. { process options }
  257. j:=0;
  258. for i:=1to paramcount do
  259. begin
  260. s:=paramstr(i);
  261. if s[1]='-' then
  262. begin
  263. case s[2] of
  264. 'd' :
  265. def_symbol(Copy(s,3,255));
  266. 'o' :
  267. outfile:=Copy(s,3,255);
  268. 'h','?' :
  269. Usage;
  270. end;
  271. end
  272. else
  273. inc(j);
  274. end;
  275. { no files? }
  276. if j=0 then
  277. Usage;
  278. { process files }
  279. for i:=1to paramcount do
  280. begin
  281. s:=paramstr(i);
  282. if s[1]<>'-' then
  283. dofile(s);
  284. end;
  285. end.