נשאלה שאלה ברשימת הדיוור של FPC איך אפשר לקחת קוד שכתוב איתו, ולגרום לו להיות כמו bash, perl, ruby וכו', כלומר ש bash ידע להריץ אותו למרות שמדובר בקוד המקור בלבד.
כלומר עבור קוד שנראה כזה:
#!/usr/bin/fpcscript program testscript; Var I : Integer; begin Write('Testscript called as '); For I:=0 to ParamCount do begin if I>0 then Write(' '); Write(paramstr(i)); end; writeln; end.
bash ידע שצריך להריץ אותו בצורה רגילה, כלומר:
$ ./testscript
אז ישבו והציעו המון רעיונות מעניינים, ובסוף מיכאל כתב את הקוד הבא (יש לדעתי צורך בכמה שיפצורים קטנים):
{$mode objfpc}{$h+} program fpcscript; uses sysutils,classes,baseunix, unix; Const BufSize = 10 * 1024; Comment = '//'; Type TStringArray = Array of string; Var F1,F2 : TFileStream; FN : String; Args : TStringArray; E,I : integer; begin E:=127; F1:=TFileStream.Create(ParamStr(1),fmOpenRead); try FN:=GetTempFileName(GetTempDir,'fpc')+'.pp'; F2:=TFileStream.Create(FN,fmCreate); try F2.WriteBuffer(Comment[1],Length(Comment)); F2.CopyFrom(F1,F1.Size); finally F2.Free; end; if (ExecuteProcess('/usr/bin/fpc',[FN])=0) then begin If FileExists(ChangeFileExt(FN,'.o')) then DeleteFIle(ChangeFileExt(FN,'.o')); SetLength(Args,ParamCount-1); For I:=2 to ParamCount do Args[I-1]:=Paramstr(i); E:=ExecuteProcess(ChangeFileExt(FN,''),Args); If FileExists(ChangeFileExt(FN,'')) then DeleteFile(ChangeFileExt(FN,'')); If FileExists(FN) then DeleteFile(Fn); end; Finally F1.Free; end; Halt(E); end.