{========

　RDE でも使っている DosCommand コンポーネントを Apollo から使うためのライブラリ。

　添付してあるパッチ版をご使用ください。

　DosCommand の元のソース（maxime_collomb 氏のバージョン）は

http://www.torry.net.  の「Quick Search」で「DosCommand」を検索。
http://homepages.borland.com/torry/vcl/system/tasks/doscommand.zip

ですがこれではコンパイルできません。

　rde のソースに含まれているものも不可です（突き合わせ中：2002-11-06）。

http://homepage2.nifty.com/sakazuki/rde_e.html
http://prdownloads.sourceforge.net/rubyde/rde0980src.zip?download

ちなみに、RDE版の中にある「2002-02-23 : tk」は私のことです。

TODO: [x] で終了するとエラーになる。

2002-12-14: ver. 0.1.5 on_terminate 追加、on_terminated と on_new_line は廃止。
2002-11-06: ver. 0.1.4 .. set default CreationFlag.
2002-11-06: ver. 0.1.3 [ap-ext:0049] patch for event handlers by tos, thanks
2002-11-01: ver. 0.1.2 [ap-list:2967]
2002-02-17: ver. 0.1.1 [ap-ext:0005] 
========}

library DosCmd;

uses
  Classes,
  Rubies,
  uDefUtils,
  Pythia,
  DosCommandHandle,
  DosCommand;

{$E so}

type
TPhi_DosCommand = class(TDosCommand)
  published
    property Active; // read getActive;
    property Prompting;
    property SinceBeginning;
    property SinceLastOutput;

    property ThreadStatus;
    property OutputLines;
    property Priority;
    property LineBeginned;
    property ExitCode;
end;

var
  cDosCommand: Tvalue;


function ap_cDosCommand: Tvalue;
begin
  result := cDosCommand;
end;

procedure DosCommand_setup(obj: Tvalue; real: TPhi_DosCommand);
begin
//  ap_set_child_attr_module(obj);
//  if @real.OnTerminated = nil then real.OnTerminated := Handle.doTerminated;
//  if @real.OnNewLine    = nil then real.OnNewLine    := Handle.doNewLine;
  if @real.OnTerminate = nil then real.OnTerminate := Handle.doTerminate;
end;

function DosCommand_event_handle(This, name: Tvalue): Tvalue; cdecl;
begin
  EventHandle(This, name, [Handle, PhiGetHandle]);
  result := Qnil;
end;

function DosCommand_alloc(This: Tvalue; real: TPhi_DosCommand): Tvalue;
begin
  result := TmpAlloc(This, real);
  DosCommand_setup(result,real);
end;

function DosCommand_alloc_v(var AControl): Tvalue;
begin
  result := DosCommand_alloc(cDosCommand, TPhi_DosCommand(AControl));
end;

function DosCommand_new(argc: integer; argv: Pointer; This: Tvalue): Tvalue; cdecl;
var
  real: TPhi_DosCommand;
begin
  real := TPhi_DosCommand.Create(nil);
  result := CompoAlloc(This, real);
  CompoSetup(argc, argv, real);
  DosCommand_setup(result, real);
  // set default for Phi.console
  real.CreationFlag := FDETACHED_PROCESS;
  real.ShowWindow := SWSHOW;
  ap_obj_call_init(result, argc, argv);
end;

function dl_DosCommand(This: Tvalue): TPhi_DosCommand;
begin
  result := ap_data_get_struct(This);
end;

function DosCommand_execute(This: Tvalue): Tvalue; cdecl;
begin
  result := This;
  dl_DosCommand(This).Execute;
end;

function DosCommand_execute2(This: Tvalue): Tvalue; cdecl;
begin
  result := ap_Integer(dl_DosCommand(This).Execute2);
end;

function DosCommand_stop(This: Tvalue): Tvalue; cdecl;
begin
  result := This;
  dl_DosCommand(This).Stop;
end;

function DosCommand_send_line(This , v, eol:Tvalue): Tvalue; cdecl;
begin
  result := This;
  dl_DosCommand(This).SendLine(dl_String(v) , dl_Boolean(eol));
end;

function ap_iDosCommand(real: TPhi_DosCommand; owner: Tvalue): Tvalue;
begin
  result := DosCommand_alloc(cDosCommand, real);
  ap_owner(result, owner);
end;
 
function ap_iDosCommand_v(var AControl; owner: Tvalue): Tvalue;
begin
  result := ap_iDosCommand(TPhi_DosCommand(AControl), owner);
end;

procedure Init_DosCmd;
begin
  PhiStart;

  cDosCommand := DefinePersistentClass(ap_mPhi, TPhi_DosCommand, ap_cComponent, ap_iDosCommand_v);
  rb_define_method(cDosCommand, 'event_handle', @DosCommand_event_handle, 1);

  DefineSingletonMethod(cDosCommand, 'new', @DosCommand_new);

  rb_define_method(cDosCommand, 'execute', @DosCommand_execute, 0);
  rb_define_method(cDosCommand, 'stop', @DosCommand_stop, 0);
  rb_define_method(cDosCommand, 'send_line', @DosCommand_send_line, 2);
  rb_define_method(cDosCommand, 'execute2', @DosCommand_execute2, 0);
end;

exports
  ap_cDosCommand,
  ap_iDosCommand,
  DosCommand_alloc,
  Init_DosCmd;

end.

