procedure TMainForm.MenuConnectionConnectProgramClick(Sender: TObject);
var process: TProcess= nil;
i: integer;
procedure shutdown(const msg: string= '');
begin
if msg <> '' then begin
ConnectProgressForm.Label1.Caption := msg;
if StatusBar1.SimplePanel = true then (* Won't mess up status bar *)
FullWidth(msg)
end;
if process <> nil then
try
process.Terminate(0)
except
end;
FreeAndNil(process)
end { shutdown } ;
procedure unableToConnect(const server: string);
begin
{$ifdef SMARTPOS }
MessageDlgPos('Unable to connect ' + server, mtError, [mbOK], 0,
(Left + Width div 2 + Screen.Width div 2) div 2,
(Top + Height div 2 + Screen.Height div 2) div 2)
{$else }
MessageDlg('Unable to connect ' + server, mtError, [mbOK], 0)
{$endif SMARTPOS }
end { unableToConnect } ;
(* If the second parameter is false trim and return the first parameter.
Otherwise locate substrings that look like shell environment names and
expand them.
*)
function expand(const str: string; doExpand: boolean): string;
var unprocessed, name: string;
begin
result := Trim(str);
if doExpand then begin
unprocessed := result;
result := '';
while unprocessed <> '' do begin
case unprocessed[1] of
'~': begin
if result = '' then (* At start only *)
result := UserHomeDirectory
else
result += '~';
Delete(unprocessed, 1, 1)
end;
'\': begin
Delete(unprocessed, 1, 1); (* Backslash *)
if unprocessed <> '' then begin
result += unprocessed[1]; (* Might be special, don't care *)
Delete(unprocessed, 1, 1)
end
end;
'$': begin (* No provision here for \{ or \} *)
Delete(unprocessed, 1, 1); (* Dollar *)
name := '';
if unprocessed <> '' then begin
if unprocessed[1] <> '{' then
while (unprocessed <> '') and
(unprocessed[1] in ['0'..'9', 'A'..'Z', 'a'..'z', '_']) do begin
name += unprocessed[1];
Delete(unprocessed, 1, 1)
end
else begin
Delete(unprocessed, 1, 1); (* Left brace *)
while (unprocessed <> '') and (unprocessed[1] <> '}') do begin
name += unprocessed[1];
Delete(unprocessed, 1, 1)
end;
Delete(unprocessed, 1, 1) (* Right brace *)
end;
if name <> '' then
result += GetEnvironmentVariable(name)
end
end
otherwise
result += unprocessed[1];
Delete(unprocessed, 1, 1)
end
end
end
end { expand } ;
begin
if CurrentConnectionType() <> ConnectionNone then begin
unableToConnect('since already online');
exit
end;
process := TProcess.Create(nil);
(* If we're not here via a reconnect then display a form for extra program *)
(* parameters. Even if we know there's no extra parameters we have to set this *)
(* form up since its content is used as the command line (ouch)-: *)
if not (Reconnecting in ConnectionFlags) then
if ProgramSettingsForm.CheckBoxParameters.Checked then
try
case ProgramConnectForm.ShowModal of
mrOk: begin
end
otherwise
ProgramConnectForm.FormShow(nil); (* Check program path *)
FreeAndNil(process);
exit
end
finally
end (* End of fresh or reconnect setup *)
else
ProgramConnectForm.FormShow(nil);
(* We should have a good program name etc. *)
if FileExists(ProgramConnectForm.Executable) then begin
process.Executable := ProgramConnectForm.Executable;
for i := 0 to ProgramSettingsForm.MemoOptions.Lines.Count - 1 do
if Trim(ProgramSettingsForm.MemoOptions.Lines[i]) <> '' then
process.Parameters.Add(Trim(ProgramSettingsForm.MemoOptions.Lines[i]));
for i := 0 to ProgramConnectForm.MemoParameters.Lines.Count - 1 do
if Trim(ProgramConnectForm.MemoParameters.Lines[i]) <> '' then
process.Parameters.Add(Trim(ProgramConnectForm.MemoParameters.Lines[i]));
if ProgramSettingsForm.CheckBoxEnvironmentInherit.Checked then
for i := 1 to GetEnvironmentVariableCount do
process.Environment.Add(GetEnvironmentString(i));
for i := 0 to ProgramSettingsForm.MemoEnvironment.Lines.Count - 1 do
if expand(ProgramSettingsForm.MemoEnvironment.Lines[i],
ProgramSettingsForm.CheckBoxEnvironmentExpand.Checked) <> '' then
process.Environment.Add(expand(ProgramSettingsForm.MemoEnvironment.Lines[i],
ProgramSettingsForm.CheckBoxEnvironmentExpand.Checked));
if Trim(ProgramSettingsForm.LabeledEditWorkingDirectory.Text) <> '' then
process.CurrentDirectory := ProgramSettingsForm.LabeledEditWorkingDirectory.Text;
process.Options := [poUsePipes];
process.Execute;
Sleep(100);
Application.ProcessMessages;
if process.Running then
StatusBar1.Panels[DbgOnline].Text := 'Online'
else
shutdown;
LastConnectionType := ConnectionProgram
end else
shutdown;
if process = nil then
exit;
(* Assume that we've exited before getting here if things aren't OK. *)
ProcessObject := process; (* Global record that the port is open *)
MenuConnectionConnectUsingProfile.Enabled := false;
MenuConnectionConnectToLine.Enabled := false;
MenuConnectionReconnect.Enabled := false;
MenuConfigurationRJEAttachListingDisplay.Enabled := false;
MenuConfigurationRJEAttachPunchEditor.Enabled := false;
MenuConnectionAttention.Enabled := true;
MenuConnectionBreak.Enabled := SerialClientHandle <> -1;
MenuConnectionDisconnect.Enabled := true;
MenuConnectionRestartRJE.Enabled := false
end { TMainForm.MenuConnectionConnectProgramClick } ;