forked from unixwitch/tts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
157 lines (135 loc) · 2.87 KB
/
commands.c
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
153
154
155
156
/*
* TTS - track your time.
* Copyright (c) 2012-2014 Felicity Tarnell.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely. This software is provided 'as-is', without any express or implied
* warranty.
*/
#include <stdlib.h>
#include "commands.h"
#include "style.h"
#include "bindings.h"
#include "variable.h"
static command_t commands[] = {
{ L"bind", c_bind },
{ L"macro", c_macro },
{ L"style", c_style },
{ L"set", c_set },
{ }
};
command_t *
find_command(name)
const wchar_t *name;
{
command_t *c;
for (c = commands; c->cm_name; c++)
if (wcscmp(name, c->cm_name) == 0)
return c;
return NULL;
}
void
c_style(argc, argv)
size_t argc;
wchar_t **argv;
{
style_t *sy;
wchar_t *last, *tok;
if (argc < 3 || argc > 4) {
cmderr(L"Usage: style <item> <foreground> [background]");
return;
}
if (wcscmp(argv[1], L"header") == 0)
sy = &sy_header;
else if (wcscmp(argv[1], L"status") == 0)
sy = &sy_status;
else if (wcscmp(argv[1], L"entry") == 0)
sy = &sy_entry;
else if (wcscmp(argv[1], L"selected") == 0)
sy = &sy_selected;
else if (wcscmp(argv[1], L"running") == 0)
sy = &sy_running;
else if (wcscmp(argv[1], L"date") == 0)
sy = &sy_date;
else {
cmderr(L"Unknown style item.");
return;
}
style_clear(sy);
for (tok = wcstok(argv[2], L",", &last); tok != NULL;
tok = wcstok(NULL, L",", &last)) {
style_add(sy, tok, argv[3]);
}
apply_styles();
}
void
c_bind(argc, argv)
size_t argc;
wchar_t **argv;
{
if (argc != 3) {
cmderr(L"Usage: bind <key> <function>");
return;
}
bind_key(argv[1], argv[2], 0);
}
void
c_macro(argc, argv)
size_t argc;
wchar_t **argv;
{
if (argc != 3) {
cmderr(L"Usage: macro <key> <def>");
return;
}
bind_key(argv[1], argv[2], 1);
}
void
c_set(argc, argv)
size_t argc;
wchar_t **argv;
{
variable_t *var;
int val;
wchar_t *p = NULL;
if (argc != 3) {
cmderr(L"Usage: set <variable> <value>");
return;
}
if ((var = find_variable(argv[1])) == NULL) {
cmderr(L"Unknown variable \"%ls\".", argv[1]);
return;
}
switch (var->va_type) {
case VTYPE_BOOL:
if (wcscmp(argv[2], L"true") == 0 ||
wcscmp(argv[2], L"yes") == 0 ||
wcscmp(argv[2], L"on") == 0 ||
wcscmp(argv[2], L"1") == 0) {
val = 1;
} else if (wcscmp(argv[2], L"false") == 0 ||
wcscmp(argv[2], L"no") == 0 ||
wcscmp(argv[2], L"off") == 0 ||
wcscmp(argv[2], L"0") == 0) {
val = 0;
} else {
cmderr(L"Invalid value for boolean: \"%ls\".", argv[2]);
return;
}
*(int *)var->va_addr = val;
break;
case VTYPE_STRING:
free(*(wchar_t **)var->va_addr);
*(wchar_t **)var->va_addr = wcsdup(argv[2]);
break;
case VTYPE_INT:
val = wcstol(argv[2], &p, 0);
if (!*argv[2] || *p) {
cmderr(L"Invalid number \"%ls\"", argv[2]);
break;
}
*(int *)var->va_addr = val;
break;
}
}