~ok2/retro-language/arduino

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define STDIN 0

int setTerm(void)
{
  struct termios t;
  int fd;
  char fname[1024];
  if (tcgetattr(STDIN, &t))
    return 1;
  fname[0] = 0;
  strncat(fname, getenv("HOME"), 1000);
  strcat(fname, "/.term_settings");
  fd = open(fname, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  if (fd == -1)
    return 2;
  if (write(fd, &t, sizeof(struct termios)) != sizeof(struct termios))
    return 3;
  if (close(fd))
    return 4;
  t.c_lflag &= ~(ICANON | ECHO | ISIG);
  t.c_iflag = 0;
  if (tcsetattr(STDIN, TCSANOW, &t))
    return 5;
  return 0;
}

int restoreTerm(void)
{
  struct termios t;
  int fd;
  char fname[1024];
  fname[0] = 0;
  strncat(fname, getenv("HOME"), 1000);
  strcat(fname, "/.term_settings");
  fd = open(fname, O_RDWR);
  if (fd == -1)
    return 2;
  if (read(fd, &t, sizeof(struct termios)) != sizeof(struct termios))
    return 3;
  if (close(fd))
    return 4;
  if (tcsetattr(STDIN, TCSANOW, &t))
    return 5;
  if (unlink(fname))
    return 6;
  return 0;
}


int main(int argc, char **argv)
{
  if (argc < 2)
    return -1;
  if (strcmp(argv[1], "--set") == 0)
    setTerm();
  if (strcmp(argv[1], "--restore") == 0)
    restoreTerm();
  if (strcmp(argv[1], "--help") == 0)
  {
    printf("terminal tweaks\n");
    printf("--set       set non-buffered keyboard input\n");
    printf("--restore   revert to default settings\n");
  }

  return 0;
}