You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

childProgram.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <iostream>
  2. #ifdef DEBUG_MESSAGES
  3. #include <fstream> // debug
  4. #endif
  5. #include <string>
  6. #include <thread>
  7. #include <chrono>
  8. int
  9. main(int argc, char *argv[]) {
  10. #ifdef DEBUG_MESSAGES
  11. std::ofstream log("childProgram.log");
  12. #endif
  13. int returnStatus = 25; // Successful return.
  14. // Output for read test.
  15. if (argc == 2) {
  16. // Write a single line and exit.
  17. if (std::string(argv[1]) == "write") {
  18. #ifdef DEBUG_MESSAGES
  19. log << "Command is \"write\". Returning \"This is a test\""
  20. << " to stdout and stderr." << std::endl;
  21. #endif
  22. std::cout << "This ";
  23. std::cout.flush();
  24. std::cerr << "is a";
  25. std::cout << " test";
  26. std::cout.flush();
  27. if (!std::cout) {
  28. returnStatus = 10; // Unsuccessful return.
  29. }
  30. goto exit;
  31. }
  32. // Exit without writing anything.
  33. if (std::string(argv[1]) == "quit") {
  34. #ifdef DEBUG_MESSAGES
  35. log << "Command is \"quit\". Exiting" << std::endl;
  36. #endif
  37. goto exit;
  38. }
  39. // Wait for standard input to close, and exit.
  40. if (std::string(argv[1]) == "checkStdinEOF") {
  41. #ifdef DEBUG_MESSAGES
  42. log << "Command is \"checkStdinEOF\". Checking that stdin is closed."
  43. << std::endl;
  44. #endif
  45. std::string temp;
  46. std::cin >> temp;
  47. if (std::cin.eof()) {
  48. #ifdef DEBUG_MESSAGES
  49. log << "stdin was closed." << std::endl;
  50. #endif
  51. returnStatus = 15; // Successful return.
  52. } else {
  53. #ifdef DEBUG_MESSAGES
  54. log << "stdin was not closed." << std::endl;
  55. #endif
  56. returnStatus = 10; // Unsuccessful return.
  57. }
  58. goto exit;
  59. }
  60. }
  61. char ch;
  62. #ifdef DEBUG_MESSAGES
  63. log << "Received \"";
  64. #endif
  65. while (std::cin >> ch) {
  66. // Exit?
  67. if ('q' == ch) {
  68. #ifdef DEBUG_MESSAGES
  69. log << (char) ch << "\"" << std::endl;
  70. #endif
  71. break;
  72. }
  73. std::cout << (char) std::toupper(ch);
  74. std::cout.flush();
  75. #ifdef DEBUG_MESSAGES
  76. log << (char) ch;
  77. log.flush();
  78. #endif
  79. }
  80. exit:
  81. #ifdef DEBUG_MESSAGES
  82. log.close();
  83. #endif
  84. std::this_thread::sleep_for(std::chrono::milliseconds(50));
  85. return returnStatus;
  86. }