|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- #include <iostream>
- #ifdef DEBUG_MESSAGES
- #include <fstream> // debug
- #endif
- #include <string>
- #include <vector>
- #include <thread>
- #include <chrono>
-
- int
- main(int argc, char *argv[]) {
-
- #ifdef DEBUG_MESSAGES
- std::ofstream log("childProgram.log");
- #endif
-
- int returnStatus = 25; // Successful return.
-
- // Output for read test.
- if (argc == 2) {
-
- // Write a single line and exit.
- if (std::string(argv[1]) == "write") {
- #ifdef DEBUG_MESSAGES
- log << "Command is \"write\". Returning \"This is a test\""
- << " to stdout and stderr." << std::endl;
- #endif
- std::cout << "This ";
- std::cout.flush();
-
- std::cerr << "is a";
-
- std::cout << " test";
- std::cout.flush();
-
- if (!std::cout) {
- returnStatus = 10; // Unsuccessful return.
- }
-
- goto exit;
-
- }
-
- // Exit without writing anything.
- if (std::string(argv[1]) == "quit") {
- #ifdef DEBUG_MESSAGES
- log << "Command is \"quit\". Exiting" << std::endl;
- #endif
- goto exit;
-
- }
-
- // Wait for standard input to close, and exit.
- if (std::string(argv[1]) == "checkStdinEOF") {
-
- #ifdef DEBUG_MESSAGES
- log << "Command is \"checkStdinEOF\". Checking that stdin is closed."
- << std::endl;
- #endif
-
- int const BufferSize = 512;
-
- std::vector<char> readBuffer;
- readBuffer.resize(BufferSize, 0);
-
- std::cin.read(&(readBuffer[0]), BufferSize);
-
- if (!std::cin.eof()) {
-
- #ifdef DEBUG_MESSAGES
- log << "stdin was not closed." << std::endl;
- #endif
- returnStatus = 10; // Unsuccessful return.
-
- } else {
- #ifdef DEBUG_MESSAGES
- log << "stdin was closed." << std::endl;
- #endif
- returnStatus = 15; // Successful return.
- }
-
- goto exit;
-
- }
-
- }
-
- char ch;
- #ifdef DEBUG_MESSAGES
- log << "Received \"";
- #endif
- while (std::cin >> ch) {
-
- // Exit?
- if ('q' == ch) {
- #ifdef DEBUG_MESSAGES
- log << (char) ch << "\"" << std::endl;
- #endif
- break;
-
- }
-
- std::cout << (char) std::toupper(ch);
- std::cout.flush();
- #ifdef DEBUG_MESSAGES
- log << (char) ch;
- log.flush();
- #endif
-
- }
-
- exit:
- #ifdef DEBUG_MESSAGES
- log.close();
- #endif
- std::this_thread::sleep_for(std::chrono::milliseconds(50));
- return returnStatus;
-
- }
|