aboutsummaryrefslogtreecommitdiff
path: root/src/tsarc.c
blob: 27ab941218721a7e3953315731ceee2cf152631f (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

#define PIPE_PATH "/tmp/tsarc.pipe"
#define STACK_LEN(arr) (sizeof(arr)/sizeof(arr[0]))

const char help_text[] =
  "usage: tsarc [command] ...\n"
  "commands:\n"
  "\tconfig - configure tsarbar settings\n"
  "\tset - set component data and options\n"
  "\tlayout - define the layout of the bar\n"
  "\n"
  "config:\n"
  "usage: tsarc config (-[property] [value] ...)\n"
  "\t-height - height of the bar, in pixels\n"
  "\t-font - font to use\n"
  "\t-gap - gap between components, in pixels\n"
  "\t-foreground, -fg - foreground color, as a hex code (e.g. #FFAA44)\n"
  "\t-background, -bg - background color, as a hex code (e.g. #FFAA44)\n"
  "\n"
  "set:\n"
  "usage: tsarc set [variable] (-[property] [value] ...)\n"
  "\t-text - text of the component\n"
  "\t-fg - foreground color, as a hex code (e.g. #FFAA44)\n"
  "\t-bg - background color, as a hex code (e.g. #FFAA44)\n"
  "\t-margin-left - margin to the left, in pixels\n"
  "\t-margin-right - margin to the right, in pixels\n"
  "\t-margin - sets both -margin-left and -margin-right\n"
  "\n"
  "layout:\n"
  "usage: tsarc layout (-[side] [component list] ...)\n"
  "\t-left, -center, -right - valid sides\n"
;

const char *valid_config_properties[] = {
  "-height",
  "-font",
  "-gap",
  "-fg",
  "-bg",
  "-foreground",
  "-background",
};

const char *valid_set_properties[] = {
  "-text",
  "-fg",
  "-bg",
  "-margin-left",
  "-margin-right",
  "-margin",
};

void show_help(FILE* file) {
  fputs(help_text, file);
}

bool verify_argument_spelling(const char* arg, const char** valid, int validc) {
  for (int i = 0; i < validc; i++) {
    if (strcmp(arg, valid[i]) == 0) {
      return true;
    }
  }
  return false;
}

void die(const char* fmt, ...) {
  va_list args;
  va_start(args, fmt);
  vfprintf(stderr, fmt, args);
  va_end(args);

  putc('\n', stderr);

  show_help(stderr);

  exit(1);
}

int main(int argc, const char* argv[]) {
  if (argc < 2) {
    die("error: missing a command");
  }

  const char* cmd = argv[1];

  if (strcmp(cmd, "-h") == 0 || strcmp(cmd, "--help") == 0) {
    show_help(stdout);
    return 0;
  }

  if (strcmp(cmd, "config") == 0) {
    if (argc < 3) {
      die("error: missing a config property");
    }

    verify_argument_spelling(
      argv[2],
      valid_config_properties,
      STACK_LEN(valid_config_properties)
    );
  }
  if (strcmp(cmd, "set") == 0) {
    if (argc < 3) {
      die("error: missing a component name");
    }
    if (argc < 4) {
      die("error: missing a component property");
    }

    verify_argument_spelling(
      argv[3],
      valid_set_properties,
      STACK_LEN(valid_set_properties)
    );
  }

  int cmd_len = 0;
  for (int i = 1; i < argc; i++) {
    cmd_len += strlen(argv[i]);
  }
  
  cmd_len += argc;
  char* cmd_str = (char*)malloc(sizeof(char) * (cmd_len + 1));

  int start = 0;
  for (int i = 1; i < argc; i++) {
    size_t arg_len = strlen(argv[i]);
    memcpy(cmd_str + start, argv[i], arg_len);
    cmd_str[start + arg_len] = '\201';
    start += arg_len + 1;
  }

  cmd_str[cmd_len-1] = '\n';
  cmd_str[cmd_len] = '\0';
  
  mkfifo(PIPE_PATH, 0666);
  FILE* named_pipe = fopen(PIPE_PATH, "w");

  fwrite(cmd_str, sizeof(char), cmd_len, named_pipe);

  fclose(named_pipe);

  return 0;
}