aboutsummaryrefslogtreecommitdiff
path: root/test/functional/fixtures/shell-test.c
blob: d9ec254aff6213e84a03b65f2f3e4fbed72dfbad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <string.h>
#include <stdint.h>

static void help(void)
{
  puts("A simple implementation of a shell for testing termopen().");
  puts("");
  puts("Usage:");
  puts("  shell-test --help");
  puts("    Prints this help to stdout.");
  puts("  shell-test");
  puts("  shell-test EXE");
  puts("    Prints \"ready $ \" to stderr.");
  puts("  shell-test EXE \"prog args...\"");
  puts("    Prints \"ready $ prog args...\\n\" to stderr.");
  puts("  shell-test REP {byte} \"line line line\"");
  puts("    Prints \"{lnr}: line line line\\n\" to stdout {byte} times.");
  puts("    I.e. for `shell-test REP ab \"test\"'");
  puts("      0: test");
  puts("      ...");
  puts("      96: test");
  puts("    will be printed because byte `a' is equal to 97.");
}

int main(int argc, char **argv)
{
  if (argc == 2 && strcmp(argv[1], "--help") == 0) {
    help();
  }

  if (argc >= 2) {
    if (strcmp(argv[1], "EXE") == 0) {
      fprintf(stderr, "ready $ ");
      if (argc >= 3) {
        fprintf(stderr, "%s\n", argv[2]);
      }
    } else if (strcmp(argv[1], "REP") == 0) {
      if (argc < 4) {
        fprintf(stderr, "Not enough REP arguments\n");
        return 4;
      }
      uint8_t number = (uint8_t) *argv[2];
      for (uint8_t i = 0; i < number; i++) {
        printf("%d: %s\n", (int) i, argv[3]);
      }
    } else {
      fprintf(stderr, "Unknown first argument\n");
      return 3;
    }
    return 0;
  } else if (argc == 1) {
    fprintf(stderr, "ready $ ");
    return 0;
  } else {
    fprintf(stderr, "Missing first argument\n");
    return 2;
  }
}