Comandos.pas 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {Unidad que implementa la ejecución de comandos de tipo: $EDIT, $EXPLORER, ...}
  2. unit Comandos;
  3. {$mode objfpc}{$H+}
  4. interface
  5. uses
  6. Classes, SysUtils, MisUtils, FrameTabSession, Globales, FrameTabSessions,
  7. FormRemoteEditor, process;
  8. function GetCommand(lin: string; out comm, pars: string): boolean;
  9. function ExecSFTP(usr, pwd, ip, cmds: string): boolean;
  10. function ProcessCommand(lin: string; ses: TfraTabSession; tabSessions: TfraTabSessions): boolean;
  11. implementation
  12. function GetCommand(lin: string; out comm, pars: string): boolean;
  13. {Analiza una linea para ver si contiene un comando como $EDITOR o $EXPLORER.
  14. Si encuentra un comando, devuelve TRUE, el texto del comando en "comm" y el
  15. parámetro en "pars".}
  16. var
  17. p: SizeInt;
  18. begin
  19. lin := trim(lin);
  20. if lin='' then exit(false);
  21. if lin[1] = '$' then begin
  22. p := pos(' ', lin);
  23. if p=0 then begin
  24. //Sin separacion
  25. comm := lin;
  26. pars := '';
  27. exit(true);
  28. end else begin
  29. //Hay separación
  30. comm := copy(lin, 1, p-1);
  31. pars := copy(lin, p+1, length(lin));
  32. exit(true);
  33. end;
  34. end else exit(false);
  35. end;
  36. function ExecSFTP(usr, pwd, ip, cmds: string): boolean;
  37. //Ejecuta el cliente SFTP. Devuelve FALSE si hubo error
  38. var
  39. p : TProcess; //el proceso a manejar
  40. begin
  41. Result := true;
  42. StringToFile(cmds, 'killme.tmp'); //Ceea archivo en blanco.
  43. p := TProcess.Create(nil); //Crea proceso
  44. p.Options:= p.Options + [poNoConsole, poWaitOnExit];
  45. p.Executable:='psftp.exe';
  46. p.Parameters.Clear;
  47. p.Parameters.Add(usr+ '@' + ip);
  48. p.Parameters.Add('-pw');
  49. p.Parameters.Add(pwd);
  50. p.Parameters.Add('-b');
  51. p.Parameters.Add('killme.tmp');
  52. try
  53. p.Execute;
  54. except
  55. Result := false;
  56. MsgBox('Fallo al iniciar aplicativo: '+ p.Executable);;
  57. end;
  58. p.Free;
  59. DeleteFile('killme.tmp'); //Limpia la casa
  60. end;
  61. function ProcessCommand(lin: string; ses: TfraTabSession; tabSessions: TfraTabSessions): boolean;
  62. {Procesa una línea que debe contener un comando, si procesa el comando. Si no encuentra
  63. un comando, devuelve FALSE.}
  64. var
  65. comm, pars, res: string;
  66. edit: TfrmRemoteEditor;
  67. begin
  68. if GetCommand(lin, comm, pars) then begin
  69. //Es un comando
  70. if comm = ses.commandEx then begin //Comando $EXPLORER
  71. if ses.explorMode = expBashComm then begin
  72. //Explorador Bash
  73. tabSessions.PageEvent('exec_explor', ses, res); //Lanza explorador
  74. end else begin
  75. //Explorador de comando
  76. Exec(ses.exterEditor, '');
  77. end;
  78. end else if comm = ses.commandEd then begin
  79. if ses.editMode = edtLocal then begin
  80. //Editor local por comando
  81. //Exec(ses.exterEditor, '');
  82. frmRemoteEditor.Init(ses);
  83. frmRemoteEditor.Open(pars);
  84. end else if ses.editMode = edtBashComm then begin
  85. //Editor remoto por comandos bash
  86. tabSessions.PageEvent('exec_edit', ses, res); //Lanza explorador
  87. end else if ses.editMode = edtRemotSFTP then begin
  88. //Editor remoto usando SFTP
  89. if pars<>'' then begin
  90. //Se espera que se haya indicado el archivo a editar
  91. frmRemoteEditor.Init(ses);
  92. frmRemoteEditor.Open(pars);
  93. //edit := TfrmRemoteEditor.Create(nil);
  94. //edit.Init(ses);
  95. //edit.Open(pars);
  96. end else begin
  97. Exec('notepad', '');
  98. end;
  99. end else begin
  100. MsgExc('Invalid option');
  101. end;
  102. end else begin
  103. //No se reconoce el comando.
  104. exit(false);
  105. end;
  106. exit(true);
  107. end else begin
  108. //No es comando
  109. exit(false)
  110. end;
  111. end;
  112. end.