rcparserfn.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. {%MainUnit rcparser.pas}
  2. interface
  3. {$mode objfpc}{$H+}
  4. {$COPERATORS ON}
  5. {$GOTO ON}
  6. uses
  7. SysUtils, Classes, StrUtils, fgl, lexlib, yacclib, resource,
  8. acceleratorsresource, groupiconresource, stringtableresource,
  9. bitmapresource, versionresource, versiontypes, groupcursorresource;
  10. type
  11. TStringHashTable = specialize TFPGMap<String, String>;
  12. function yyparse : Integer;
  13. var
  14. aktresources: TResources;
  15. opt_code_page: TSystemCodePage;
  16. yyfilename: AnsiString;
  17. yyparseresult: YYSType;
  18. procedure DisposePools;
  19. procedure SetDefaults;
  20. procedure PragmaCodePage(cp: string);
  21. {$DEFINE INC_HEADER}
  22. {$I yyinclude.pp}
  23. {$I yypreproc.pp}
  24. {$UNDEF INC_HEADER}
  25. implementation
  26. procedure yyerror ( msg : String );
  27. begin
  28. writeln(ErrOutput, yyfilename, '(',yylineno,':',yycolno,'): at "',yytext,'": ', msg);
  29. WriteLn(ErrOutput, yyline);
  30. WriteLn(ErrOutput, '^':yycolno);
  31. end(*yyerrmsg*);
  32. {$I yyinclude.pp}
  33. {$I yypreproc.pp}
  34. (* I/O routines: *)
  35. const nl = #10; (* newline character *)
  36. const max_chars = 2048;
  37. var
  38. bufptr : Integer;
  39. buf : array [1..max_chars] of Char;
  40. function rc_get_char : Char;
  41. var i : Integer;
  42. ok : boolean;
  43. begin
  44. if (bufptr=0) and not eof(yyinput) then
  45. begin
  46. repeat
  47. readln(yyinput, yyline);
  48. inc(yylineno); yycolno := 1;
  49. ok:= ypreproc.useline(yyline);
  50. until (ok or eof(yyinput));
  51. if ok then begin
  52. buf[1] := nl;
  53. for i := 1 to length(yyline) do
  54. buf[i+1] := yyline[length(yyline)-i+1];
  55. inc(bufptr, length(yyline)+1);
  56. end;
  57. end;
  58. if bufptr>0 then
  59. begin
  60. rc_get_char := buf[bufptr];
  61. dec(bufptr);
  62. inc(yycolno);
  63. end
  64. else
  65. rc_get_char := #0;
  66. end(*get_char*);
  67. procedure rc_unget_char ( c : Char );
  68. begin
  69. if bufptr=max_chars then yyerror('input buffer overflow');
  70. inc(bufptr);
  71. dec(yycolno);
  72. buf[bufptr] := c;
  73. end(*unget_char*);
  74. procedure unget_string(s: string);
  75. var
  76. i: integer;
  77. begin
  78. for i:= Length(s) downto 1 do
  79. rc_unget_char(s[i]);
  80. end;
  81. procedure PragmaCodePage(cp: string);
  82. var cpi: integer;
  83. begin
  84. if Uppercase(cp) = 'DEFAULT' then
  85. opt_code_page:= DefaultFileSystemCodePage
  86. else begin
  87. if TryStrToInt(cp, cpi) and (cpi>=0) and (cpi<=high(TSystemCodePage)) then
  88. opt_code_page:= cpi
  89. else
  90. yyerror('Invalid code_page pragma: "' + cp + '"');
  91. end;
  92. end;
  93. type
  94. rcnumtype = record
  95. v: LongWord;
  96. long: boolean;
  97. end;
  98. rcstrtype = record
  99. v: PUnicodeString;
  100. cp: TSystemCodePage;
  101. end;
  102. function str_to_cbase(s: string): LongWord;
  103. begin
  104. if s = '0' then
  105. Exit(0);
  106. if Copy(s, 1, 2) = '0x' then
  107. Exit(StrToInt('$' + Copy(s, 3, Maxint)));
  108. if Copy(s, 1, 2) = '0o' then
  109. Exit(StrToInt('&' + Copy(s, 3, Maxint)));
  110. if Copy(s, 1, 1) = '0' then
  111. Exit(StrToInt('&' + Copy(s, 2, Maxint)));
  112. Result:= StrToInt(s);
  113. end;
  114. function str_to_num(s:string): rcnumtype;
  115. begin
  116. // this does not handle empty strings - should never get them from the lexer
  117. Result.long:= s[Length(s)] = 'L';
  118. if Result.long then
  119. setlength(s, Length(s) - 1);
  120. Result.v:= str_to_cbase(s);
  121. end;
  122. type
  123. PStrPoolItem = ^TStrPoolItem;
  124. TStrPoolItem = record
  125. str: PUnicodeString;
  126. next: PStrPoolItem;
  127. end;
  128. const
  129. MAX_RCSTR_LEN = 4096;
  130. var
  131. strbuf: array[0..MAX_RCSTR_LEN + 1] of char;
  132. strbuflen: Integer;
  133. stringpool: PStrPoolItem = nil;
  134. procedure strbuf_begin();
  135. begin
  136. FillChar(strbuf[0], sizeof(strbuf), 0);
  137. strbuflen:= 0;
  138. end;
  139. procedure strbuf_append(s: string);
  140. var
  141. rem: integer;
  142. begin
  143. rem:= MAX_RCSTR_LEN - strbuflen;
  144. if Length(s) < rem then
  145. rem:= Length(s);
  146. Move(s[1], strbuf[strbuflen], rem);
  147. inc(strbuflen, rem);
  148. end;
  149. procedure string_new(var str: rcstrtype; val: UnicodeString; cp: TSystemCodePage);
  150. var
  151. s: PStrPoolItem;
  152. begin
  153. New(str.v);
  154. str.v^:= val;
  155. str.cp:= cp;
  156. New(s);
  157. s^.next:= stringpool;
  158. s^.str:= str.v;
  159. stringpool:= s;
  160. end;
  161. procedure string_new_uni(var str: rcstrtype; val: PAnsiChar; len: integer; cp: TSystemCodePage; escapes: boolean);
  162. function translateChar(c: AnsiChar): UnicodeChar;
  163. var
  164. u: UnicodeString = '';
  165. begin
  166. if cp = CP_UTF16 then
  167. Result:= c
  168. else begin
  169. // TODO: there has to be a better way to translate a single codepoint
  170. widestringmanager.Ansi2UnicodeMoveProc(@c, cp, u, 1);
  171. Result:= u[1];
  172. end;
  173. end;
  174. var
  175. uni: UnicodeString;
  176. wc: PUnicodeChar;
  177. rc, endin: PAnsiChar;
  178. h: string;
  179. hexlen, i: integer;
  180. begin
  181. uni:= '';
  182. if not escapes then
  183. widestringmanager.Ansi2UnicodeMoveProc(val, cp, uni, len)
  184. else begin
  185. if cp = CP_UTF16 then
  186. hexlen:= 4
  187. else
  188. hexlen:= 2;
  189. setlength(uni, len);
  190. wc:= @uni[1];
  191. rc:= val;
  192. endin:= @val[len];
  193. while rc < endin do begin
  194. if (rc^ = '\') then begin
  195. inc(rc);
  196. case rc^ of
  197. #0: exit {Error: End too soon};
  198. '\': wc^:= '\';
  199. 'f': wc^:= #&14;
  200. 'n': wc^:= #&12;
  201. 'r': wc^:= #&15;
  202. 't': wc^:= #&11;
  203. 'x',
  204. 'X': begin
  205. h:= '$';
  206. for i:= 1 to hexlen do begin
  207. inc(rc);
  208. if rc >= endin then
  209. exit {Error: End too soon};
  210. h += rc^;
  211. end;
  212. if cp = CP_UTF16 then
  213. wc^:= WideChar(StrToInt(h))
  214. else
  215. wc^:= translateChar(Char(StrToInt(h)));
  216. end;
  217. '0'..'7': begin
  218. h:= '&' + rc^;
  219. for i:= 2 to 3 do begin
  220. inc(rc);
  221. if (rc >= endin) or not (rc^ in ['0'..'7']) then begin
  222. dec(rc);
  223. break;
  224. end;
  225. h += rc^;
  226. end;
  227. if cp = CP_UTF16 then
  228. wc^:= WideChar(StrToInt(h))
  229. else
  230. wc^:= translateChar(Char(StrToInt(h)));
  231. end;
  232. else
  233. wc^:= translateChar(rc^);
  234. end;
  235. end else
  236. wc^:= translateChar(rc^);
  237. inc(wc);
  238. inc(rc);
  239. end;
  240. i:= (PtrUInt(wc) - PtrUInt(@uni[1])) div SizeOf(WideChar);
  241. SetLength(uni, i);
  242. end;
  243. string_new(str, uni, cp);
  244. end;
  245. function Max(a, b: LongWord): LongWord; inline;
  246. begin
  247. if a > b then
  248. Result:= a
  249. else
  250. Result:= b;
  251. end;
  252. var
  253. aktresource: TAbstractResource;
  254. language: TLangID;
  255. procedure create_resource(aId, aType: TResourceDesc; aClass: TResourceClass);
  256. var
  257. r: TAbstractResource;
  258. begin
  259. r:= aClass.Create(aType, aId);
  260. r.LangID:= language;
  261. aktresources.Add(r);
  262. aktresource:= r;
  263. aId.Free;
  264. aType.Free;
  265. end;
  266. procedure create_resource(aId, aType: TResourceDesc); overload;
  267. begin
  268. create_resource(aId, aType, TGenericResource);
  269. end;
  270. procedure create_resource(aId: TResourceDesc; aType: Word); overload;
  271. var
  272. cls: TResourceClass;
  273. begin
  274. case aType of
  275. RT_BITMAP: cls:= TBitmapResource;
  276. RT_ICON: cls:= TGroupIconResource;
  277. RT_CURSOR: cls:= TGroupCursorResource;
  278. RT_VERSION: cls:= TVersionResource;
  279. else
  280. raise EResourceDescTypeException.CreateFmt('Resource type not supported: %d', [aType]);
  281. end;
  282. create_resource(aId, nil, cls);
  283. end;
  284. procedure change_lang_id(newlang: TLangID);
  285. begin
  286. // cannot change a language id while it is contained in a list, so remove and re-add
  287. aktresources.Remove(aktresource);
  288. aktresource.LangID:= newlang;
  289. aktresources.Add(aktresource);
  290. end;
  291. procedure raw_write_string(Stream: TMemoryStream; str: rcstrtype);
  292. var
  293. i: integer;
  294. u: UnicodeString;
  295. r: RawByteString = '';
  296. begin
  297. u:= str.v^;
  298. if str.cp = CP_UTF16 then begin
  299. for i:=1 to length(u) do
  300. Stream.WriteWord(NtoLE(Word(u[i])));
  301. end else begin
  302. widestringmanager.Unicode2AnsiMoveProc(@u[1], r, str.cp, Length(u));
  303. Stream.WriteBuffer(r[1], Length(r));
  304. end;
  305. end;
  306. procedure raw_write_int(Stream: TMemoryStream; num: rcnumtype);
  307. begin
  308. if num.long then
  309. Stream.WriteDWord(NtoLE(num.v))
  310. else
  311. Stream.WriteWord(NtoLE(Word(num.v)));
  312. end;
  313. procedure stringtable_begin();
  314. begin
  315. // create dummy resource that we will use to capture suboptions
  316. create_resource(TResourceDesc.create(1), TResourceDesc.create(1));
  317. aktresources.Remove(aktresource);
  318. end;
  319. procedure stringtable_add(ident: Word; str: AnsiString);
  320. var
  321. table: word;
  322. r: TStringTableResource;
  323. begin
  324. table:= (ident div 16) + 1;
  325. try
  326. { TODO : This is stupid }
  327. r:= aktresources.Find(RT_STRING, table, aktresource.LangID) as TStringTableResource;
  328. except
  329. on e: EResourceNotFoundException do begin
  330. r:= TStringTableResource.Create;
  331. r.LangID:= aktresource.LangID;
  332. r.MemoryFlags:= aktresource.MemoryFlags;
  333. r.Characteristics:= aktresource.Characteristics;
  334. r.Version:= aktresource.Version;
  335. r.FirstID:= ident;
  336. aktresources.Add(r);
  337. end;
  338. end;
  339. r.Strings[ident]:= str;
  340. end;
  341. procedure stringtable_end();
  342. begin
  343. FreeAndNil(aktresource);
  344. end;
  345. function make_version(a, b, c, d: Word): TFileProductVersion;
  346. begin
  347. Result[0]:= a;
  348. Result[1]:= b;
  349. Result[2]:= c;
  350. Result[3]:= d;
  351. end;
  352. procedure version_string_tab_begin(lcs: AnsiString);
  353. var
  354. vst: TVersionStringTable;
  355. begin
  356. vst:= TVersionStringTable.Create(lcs);
  357. TVersionResource(aktresource).StringFileInfo.Add(vst);
  358. end;
  359. procedure version_string_tab_add(key, value: AnsiString);
  360. begin
  361. TVersionResource(aktresource).StringFileInfo.Items[TVersionResource(aktresource).StringFileInfo.Count-1].Add(key, value);
  362. end;
  363. procedure version_var_translation_add(langid, cpid: word);
  364. var
  365. ti: TVerTranslationInfo;
  366. begin
  367. ti.language:= langid;
  368. ti.codepage:= cpid;
  369. TVersionResource(aktresource).VarFileInfo.Add(ti);
  370. end;
  371. procedure SetDefaults;
  372. begin
  373. language:= $0409; // MS RC starts up as en-US
  374. PragmaCodePage('DEFAULT');
  375. end;
  376. procedure DisposePools;
  377. var
  378. s: PStrPoolItem;
  379. begin
  380. while stringpool <> nil do begin
  381. s:= stringpool;
  382. stringpool:= s^.next;
  383. dispose(s^.str);
  384. dispose(s);
  385. end;
  386. end;