Pipes and CreateNewProcTags

3 posts / 0 new
Last post
alfkil
alfkil's picture
Offline
Last seen: 3 months 5 days ago
Joined: 2011-05-10 22:02
Pipes and CreateNewProcTags

I have created a Pipe class in c++. The following test works with SystemTags, but fails with CreateNewProcTags (no output).

  1. #include "../Pipe.hpp"
  2. #include <iostream>
  3.  
  4. #include <proto/exec.h>
  5. #include <proto/dos.h>
  6.  
  7.  
  8. int main() {
  9. Pipe pipe;
  10.  
  11. #if 0
  12. BPTR seglist = IDOS->LoadSeg ("C:dir");
  13. if(!seglist) {
  14. cout << "Failed to load\n";
  15. return -1;
  16. }
  17.  
  18. Process *process = IDOS->CreateNewProcTags(
  19. NP_Seglist, seglist,
  20. NP_FreeSeglist, TRUE,
  21. NP_Cli, true,
  22. NP_Child, true,
  23. NP_Output, pipe.getWrite(),
  24. NP_CloseOutput, false,
  25. NP_NotifyOnDeathSigTask, IExec->FindTask(0),
  26. TAG_DONE
  27. );
  28.  
  29. IExec->Wait(SIGF_CHILD);
  30. #else
  31. int result = IDOS->SystemTags("dir",
  32. SYS_Output, pipe.getWrite(),
  33. TAG_DONE);
  34. #endif
  35.  
  36. vector<string> output = pipe.emptyPipe();
  37. for(int i = 0; i < output.size(); i++)
  38. cout << "--] " << output\[i\] << "\n";
  39.  
  40. cout << "Done.\n";
  41. return 0;
  42. }

The Pipe class.

  1. #include <proto/exec.h>
  2. #include <proto/dos.h>
  3.  
  4. #include <string.h>
  5.  
  6. #include "Pipe.hpp"
  7. #include "Strings.hpp"
  8. #include "async.h"
  9.  
  10. Pipe::Pipe() {
  11. init();
  12. }
  13.  
  14. Pipe::~Pipe() {
  15. cleanup();
  16. }
  17.  
  18. void Pipe::init() {
  19. isReady = false;
  20. bool noblock = true;
  21. ExamineData *data;
  22.  
  23. fd[0] = 0;
  24. fd[1] = 0;
  25.  
  26. fd[1] = IDOS->Open ("PIPE:/UNIQUE/NOBLOCK", MODE_NEWFILE);
  27. if (fd[1] == 0) return;
  28.  
  29. data = IDOS->ExamineObjectTags(EX_FileHandleInput, fd[1], TAG_END);
  30. if (data == 0) {
  31. IDOS->PrintFault(IDOS->IoErr(), 0); /* failure - why ? */
  32. IDOS->Close(fd[1]);
  33. return;
  34. }
  35.  
  36. string file = string("PIPE:") + (const char *)data->Name;
  37. if (noblock) file += "/NOBLOCK";
  38. IDOS->FreeDosObject(DOS_EXAMINEDATA, data);
  39.  
  40.  
  41. fd[0] = IDOS->Open (file.c_str(), MODE_OLDFILE);
  42. if (fd[0] == 0) {
  43. IDOS->Close(fd[1]);
  44. return;
  45. }
  46.  
  47. size = CHUNK_SIZE;
  48. buffer = new char\[size\];
  49. buffer[0] = '\0';
  50. bytes = 0;
  51. isReady = true;
  52. }
  53.  
  54. void Pipe::cleanup()
  55. {
  56. if(fd[0]) IDOS->Close(fd[0]);
  57. if(fd[1]) IDOS->Close(fd[1]);
  58. }
  59.  
  60. int Pipe::write(string text) {
  61. if(!isReady) return 0;
  62. int bytes = IDOS->Write(fd[1], text.c_str(), text.size());
  63. if(bytes == -1) {
  64. IDOS->PrintFault(IDOS->IoErr(), "IO error: "); /* failure - why ? */
  65. bytes = 0;
  66. }
  67. return bytes;
  68. }
  69.  
  70. int Pipe::read()
  71. {
  72. if(!isReady) return 0;
  73. bytes = IDOS->Read(fd[0], buffer, size-1);
  74. if(bytes == -1) {
  75. IDOS->PrintFault(IDOS->IoErr(), "IO error: "); /* failure - why ? */
  76. bytes = 0;
  77. }
  78. buffer[bytes] = '\0';
  79. return bytes;
  80. }
  81.  
  82. bool Pipe::bytesToRead() {
  83. return bytes;
  84. }
  85.  
  86. vector<string> Pipe::emptyPipe() {
  87. read();
  88. return astream(buffer).split('\n');
  89. }
salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Re: Pipes and CreateNewProcTags

You should set NP_Arguments when running a CLI command as most code does not handle the combination of NULL pr_Arguments and non-NULL pr_CLI very well. IIRC for legacy reasons the argument string needs to contain a newline at the end so it would be "dir\n" in your case.

Also I don't see the advantage of using CreateNewProcTags() instead of SystemTags() in your case. SystemTags() will use a resident "dir" command if one is available but the version using CreateNewProcTags() won't.

alfkil
alfkil's picture
Offline
Last seen: 3 months 5 days ago
Joined: 2011-05-10 22:02
Re: Pipes and CreateNewProcTags

Thanks for the advice about NP_Arguments. I will take that into account. For the present purpose, though, I think it is irrelevant, because the call works regardless. This is not the issue at hand.

The advantage of using CreateProcTags is of course not obvious from the presented case. The real case is of course a much more elaborate one, where SystemTags does not apply.

Log in or register to post comments