main.hpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #pragma once
  2. #include <nall/platform.hpp>
  3. #include <nall/arguments.hpp>
  4. #include <nall/string.hpp>
  5. namespace nall {
  6. auto main(Arguments arguments) -> void;
  7. auto main(int argc, char** argv) -> int {
  8. #if defined(PLATFORM_WINDOWS)
  9. CoInitialize(0);
  10. WSAData wsaData{0};
  11. WSAStartup(MAKEWORD(2, 2), &wsaData);
  12. _setmode(_fileno(stdin ), O_BINARY);
  13. _setmode(_fileno(stdout), O_BINARY);
  14. _setmode(_fileno(stderr), O_BINARY);
  15. #endif
  16. main(move(Arguments{argc, argv}));
  17. //when a program is running, input on the terminal queues in stdin
  18. //when terminating the program, the shell proceeds to try and execute all stdin data
  19. //this is annoying behavior: this code tries to minimize the impact as much as it can
  20. //we can flush all of stdin up to the last line feed, preventing spurious commands from executing
  21. //however, even with setvbuf(_IONBF), we can't stop the last line from echoing to the terminal
  22. #if !defined(PLATFORM_WINDOWS)
  23. auto flags = fcntl(fileno(stdin), F_GETFL, 0);
  24. fcntl(fileno(stdin), F_SETFL, flags | O_NONBLOCK); //don't allow read() to block when empty
  25. char buffer[4096], data = false;
  26. while(read(fileno(stdin), buffer, sizeof(buffer)) > 0) data = true;
  27. fcntl(fileno(stdin), F_SETFL, flags); //restore original flags for the terminal
  28. if(data) putchar('\r'); //ensures PS1 is printed at the start of the line
  29. #endif
  30. return EXIT_SUCCESS;
  31. }
  32. }
  33. auto main(int argc, char** argv) -> int {
  34. return nall::main(argc, argv);
  35. }