shell-test.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // This is an open source non-commercial project. Dear PVS-Studio, please check
  2. // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdint.h>
  6. #ifdef _MSC_VER
  7. #include <Windows.h>
  8. #define usleep(usecs) Sleep(usecs/1000)
  9. #else
  10. #include <unistd.h>
  11. #endif
  12. static void wait(void)
  13. {
  14. fflush(stdout);
  15. usleep(10*1000);
  16. }
  17. static void help(void)
  18. {
  19. puts("A simple implementation of a shell for testing termopen().");
  20. puts("");
  21. puts("Usage:");
  22. puts(" shell-test --help");
  23. puts(" Prints this help to stdout.");
  24. puts(" shell-test");
  25. puts(" shell-test EXE");
  26. puts(" Prints \"ready $ \" to stderr.");
  27. puts(" shell-test -t {prompt text}");
  28. puts(" Prints \"{prompt text} $ \" to stderr.");
  29. puts(" shell-test EXE \"prog args...\"");
  30. puts(" Prints \"ready $ prog args...\\n\" to stderr.");
  31. puts(" shell-test -t {prompt text} EXE \"prog args...\"");
  32. puts(" Prints \"{prompt text} $ progs args...\" to stderr.");
  33. puts(" shell-test REP {byte} \"line line line\"");
  34. puts(" Prints \"{lnr}: line line line\\n\" to stdout {byte} times.");
  35. puts(" I.e. for `shell-test REP ab \"test\"'");
  36. puts(" 0: test");
  37. puts(" ...");
  38. puts(" 96: test");
  39. puts(" will be printed because byte `a' is equal to 97.");
  40. }
  41. int main(int argc, char **argv)
  42. {
  43. if (argc == 2 && strcmp(argv[1], "--help") == 0) {
  44. help();
  45. }
  46. if (argc >= 2) {
  47. if (strcmp(argv[1], "-t") == 0) {
  48. if (argc < 3) {
  49. fprintf(stderr,"Missing prompt text for -t option\n");
  50. return 5;
  51. } else {
  52. fprintf(stderr, "%s $ ", argv[2]);
  53. if (argc >= 5 && (strcmp(argv[3], "EXE") == 0)) {
  54. fprintf(stderr, "%s\n", argv[4]);
  55. }
  56. }
  57. } else if (strcmp(argv[1], "EXE") == 0) {
  58. fprintf(stderr, "ready $ ");
  59. if (argc >= 3) {
  60. fprintf(stderr, "%s\n", argv[2]);
  61. }
  62. } else if (strcmp(argv[1], "REP") == 0) {
  63. if (argc < 4) {
  64. fprintf(stderr, "Not enough REP arguments\n");
  65. return 4;
  66. }
  67. uint8_t number = (uint8_t) *argv[2];
  68. for (uint8_t i = 0; i < number; i++) {
  69. printf("%d: %s\n", (int) i, argv[3]);
  70. }
  71. } else if (strcmp(argv[1], "UTF-8") == 0) {
  72. // test split-up UTF-8 sequence
  73. printf("\xc3"); wait();
  74. printf("\xa5\n"); wait();
  75. // split up a 2+2 grapheme clusters all possible ways
  76. printf("ref: \xc3\xa5\xcc\xb2\n"); wait();
  77. printf("1: \xc3"); wait();
  78. printf("\xa5\xcc\xb2\n"); wait();
  79. printf("2: \xc3\xa5"); wait();
  80. printf("\xcc\xb2\n"); wait();
  81. printf("3: \xc3\xa5\xcc"); wait();
  82. printf("\xb2\n"); wait();
  83. } else {
  84. fprintf(stderr, "Unknown first argument\n");
  85. return 3;
  86. }
  87. return 0;
  88. } else if (argc == 1) {
  89. fprintf(stderr, "ready $ ");
  90. return 0;
  91. } else {
  92. fprintf(stderr, "Missing first argument\n");
  93. return 2;
  94. }
  95. }