src/AwkHandler.h
branchv_0
changeset 4 050ec7c1f2e7
parent 3 e086ae6a19c3
child 5 86de8e6ab231
equal deleted inserted replaced
3:e086ae6a19c3 4:050ec7c1f2e7
    76 	}
    76 	}
    77 
    77 
    78 	void cleanUp() {
    78 	void cleanUp() {
    79 		if (awkInputWriterFD >= 0) {
    79 		if (awkInputWriterFD >= 0) {
    80 			closeOrThrow(awkInputWriterFD);
    80 			closeOrThrow(awkInputWriterFD);
    81 			fwprintf(stderr, L"writing done and closed\n");
       
    82 			__pid_t waitResult1 = wait(NULL);
    81 			__pid_t waitResult1 = wait(NULL);
    83 			fwprintf(stderr, L"wait 1 done: %d\n", waitResult1);
       
    84 			__pid_t waitResult2 = wait(NULL);
    82 			__pid_t waitResult2 = wait(NULL);
    85 			fwprintf(stderr, L"wait 2 done: %d\n", waitResult2);
       
    86 			awkInputWriterFD = -1;
    83 			awkInputWriterFD = -1;
    87 		}
    84 		}
    88 	}
    85 	}
    89 
    86 
    90 public:
    87 public:
   114 
   111 
   115 			redirectFD(awkInputReaderFD, STDIN_FILENO);
   112 			redirectFD(awkInputReaderFD, STDIN_FILENO);
   116 			redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   113 			redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   117 
   114 
   118 
   115 
   119 			fwprintf(stderr, L"I am child AWK with PID: %d\n", getpid());
       
   120 			execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
   116 			execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
   121 
   117 
   122 		} else {
   118 		} else {
   123 			// Parent process
   119 			// Parent process
   124 			closeOrThrow(awkInputReaderFD);
   120 			closeOrThrow(awkInputReaderFD);
   125 			closeOrThrow(awkOutputWriterFD);
   121 			closeOrThrow(awkOutputWriterFD);
   126 			fwprintf(stderr, L"Forked AWK has PID: %d\n", awkPid);
       
   127 
   122 
   128 			__pid_t writerPid = fork();
   123 			__pid_t writerPid = fork();
   129 
   124 
   130 			if (writerPid < 0) {
   125 			if (writerPid < 0) {
   131 				throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
   126 				throw cli::RelpipeCLIException(L"Unable to fork Writer process.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions?
   132 			} else if (writerPid == 0) {
   127 			} else if (writerPid == 0) {
   133 				// Writer child process
   128 				// Writer child process
   134 				closeOrThrow(awkInputWriterFD);
   129 				closeOrThrow(awkInputWriterFD);
   135 				fwprintf(stderr, L"I am child Writer with PID: %d Writer: %p\n", getpid(), relationalWriter);
       
   136 
   130 
   137 				__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in);
   131 				__gnu_cxx::stdio_filebuf<wchar_t> awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in);
   138 				std::wistream awkOutputReader(&awkOutputReaderBuffer);
   132 				std::wistream awkOutputReader(&awkOutputReaderBuffer);
   139 
   133 
   140 				relationalWriter->startRelation(L"writer_debug",{
   134 				relationalWriter->startRelation(L"writer_debug",{
   148 				closeOrThrow(awkOutputReaderFD);
   142 				closeOrThrow(awkOutputReaderFD);
   149 				exit(0);
   143 				exit(0);
   150 			} else {
   144 			} else {
   151 				// Parent process
   145 				// Parent process
   152 				closeOrThrow(awkOutputReaderFD);
   146 				closeOrThrow(awkOutputReaderFD);
   153 				fwprintf(stderr, L"Forked Writer has PID: %d\n", writerPid);
       
   154 			}
   147 			}
   155 		}
   148 		}
   156 
   149 
   157 	}
   150 	}
   158 
   151