src/AwkHandler.h
branchv_0
changeset 5 86de8e6ab231
parent 4 050ec7c1f2e7
child 6 efa96f51b308
equal deleted inserted replaced
4:050ec7c1f2e7 5:86de8e6ab231
    48 using namespace std;
    48 using namespace std;
    49 using namespace relpipe;
    49 using namespace relpipe;
    50 using namespace relpipe::reader;
    50 using namespace relpipe::reader;
    51 using namespace relpipe::reader::handlers;
    51 using namespace relpipe::reader::handlers;
    52 
    52 
       
    53 /**
       
    54  * This transformation consists of three processes connected together using pipes.
       
    55  * 
       
    56  * Processes:
       
    57  *	- Parent: process RelationalReaderStringHandler events (read relational data, usually from STDIN) and pass them in the special text format to the pipe1
       
    58  *  - AWK: external program (/usr/bin/awk), read from the pipe1, writes to the pipe2
       
    59  *  - Writer: reads from the pipe2 and writes to relationalWriter (relational data, usually to STDOUT)
       
    60  */
    53 class AwkHandler : public RelationalReaderStringHandler {
    61 class AwkHandler : public RelationalReaderStringHandler {
    54 private:
    62 private:
    55 	Configuration configuration;
    63 	Configuration configuration;
    56 	writer::RelationalWriter* relationalWriter;
    64 	writer::RelationalWriter* relationalWriter;
    57 
    65 
    76 	}
    84 	}
    77 
    85 
    78 	void cleanUp() {
    86 	void cleanUp() {
    79 		if (awkInputWriterFD >= 0) {
    87 		if (awkInputWriterFD >= 0) {
    80 			closeOrThrow(awkInputWriterFD);
    88 			closeOrThrow(awkInputWriterFD);
       
    89 			// TODO: check exit codes
    81 			__pid_t waitResult1 = wait(NULL);
    90 			__pid_t waitResult1 = wait(NULL);
    82 			__pid_t waitResult2 = wait(NULL);
    91 			__pid_t waitResult2 = wait(NULL);
    83 			awkInputWriterFD = -1;
    92 			awkInputWriterFD = -1;
    84 		}
    93 		}
    85 	}
    94 	}
   110 			closeOrThrow(awkOutputReaderFD);
   119 			closeOrThrow(awkOutputReaderFD);
   111 
   120 
   112 			redirectFD(awkInputReaderFD, STDIN_FILENO);
   121 			redirectFD(awkInputReaderFD, STDIN_FILENO);
   113 			redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   122 			redirectFD(awkOutputWriterFD, STDOUT_FILENO);
   114 
   123 
   115 
   124 			// Runs AWK program found on $PATH → user can plug-in a custom implementation or a wrapper, but this can be also bit dangerous (however AWK itself is dangerous).
   116 			execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
   125 			execlp("awk", "awk", "{print \"AWK says: line \" NR \" = \" $0;}", nullptr);
   117 
       
   118 		} else {
   126 		} else {
   119 			// Parent process
   127 			// Parent process
   120 			closeOrThrow(awkInputReaderFD);
   128 			closeOrThrow(awkInputReaderFD);
   121 			closeOrThrow(awkOutputWriterFD);
   129 			closeOrThrow(awkOutputWriterFD);
   122 
   130