-
Notifications
You must be signed in to change notification settings - Fork 62
/
macros.c
282 lines (241 loc) · 5.72 KB
/
macros.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "sdr_ui.h"
int macro_exec(int key, char *dest);
void macro_get_var(char *var, char *s);
#define MACRO_MAX_LABEL (30)
#define MACRO_MAX_TEXT (200)
#define MACRO_MAX (25)
struct macro {
int fn_key;
char label[MACRO_MAX_LABEL];
char text[MACRO_MAX_TEXT];
};
static struct macro macro_table[MACRO_MAX];
static int serial = 1;
static char macro_v_str[10];
static char is_running = 0;
void macro_get_keys(char *output){
output[0] = 0;
for (int i = 0; i < MACRO_MAX; i++){
if (macro_table[i].label[0] == 0)
break;
char key_name[10];
sprintf(key_name, "|%d ", macro_table[i].fn_key);
strcat(output, key_name);
strcat(output, macro_table[i].label);
}
}
void macro_list(char *output){
char full_path[200]; //dangerous, find the MAX_PATH and replace 200 with it
char *home_path = getenv("HOME");
strcpy(full_path, home_path);
strcat(full_path, "/sbitx/web/");
DIR *d = opendir(full_path);
struct dirent *dir;
if (!d){
write_console(FONT_LOG, "\Error:web subdirectory is missing\n");
return;
}
if(output)
output[0] = 0;
while ((dir = readdir(d)) != NULL) {
char *p = dir->d_name;
int len = strlen(p);
if (p[len-3] == '.' && p[len-2] == 'm' && p[len-1] == 'c'){
p[len-3] = 0;
if (output){
strcat(output, dir->d_name);
strcat(output, "|");
}
}
}
closedir(d);
}
void macro_label(int fn_key, char *label){
*label = 0;
for (int i = 0; i < MACRO_MAX; i++){
if (macro_table[i].fn_key == fn_key){
strcpy(label, macro_table[i].label);
if (is_running)
break;
}
}
}
int macro_load(char *filename, char *output){
char macro_line[255];
char full_path[200]; //dangerous, find the MAX_PATH and replace 200 with it
char *home_path = getenv("HOME");
strcpy(full_path, home_path);
strcat(full_path, "/sbitx/web/");
strcat(full_path, filename);
strcat(full_path, ".mc");
FILE *pf = fopen(full_path, "r");
if(!pf)
return -1;
memset(macro_table, 0, sizeof(macro_table));
int i = 0;
while (i < MACRO_MAX){
if (fgets(macro_line, sizeof(macro_line) - 1, pf) == NULL)
break;
char *p = macro_line;
if (*p++ != 'F')
continue;
macro_table[i].fn_key = atoi(p);
//now, skip to beyond the key identifier
while(*p != ' ' && *p)
p++;
//skip the following spaces to the start of button label
while(*p == ' ' && *p)
p++;
int j = 0;
for (j = 0; j < MACRO_MAX_LABEL; j++){
if (*p && *p != ',')
macro_table[i].label[j] = *p++;
else
break;
}
macro_table[i].label[j] = 0;
//expect a comma before the macro text
if (*p != ','){
//printf("Macro loading %s, Expected a comma before [%s]\n", full_path, p);
continue;
}
strcpy(macro_table[i].text, ++p);
i++;
}
fclose(pf);
for (int i = 1; i <= 12; i++){
char button[32], label[32];
macro_label(i, label);
sprintf(button, "F%d", i);
field_set(button, label);
}
return 0;
}
void macro_get_var(char *var, char *s){
*s = 0;
if(!strcmp(var, "MYCALL"))
strcpy(s, field_str("MYCALLSIGN"));
else if (!strcmp(var, "CALL"))
strcpy(s, field_str("CALL"));
else if (!strcmp(var, "SENTRST"))
strcpy(s, field_str("SENT"));
else if (!strcmp(var, "SENTRSTCUT")){
strcpy(s, field_str("SENT"));
if (s[1] == '9')
s[1] = 'N';
if (s[2] == '9')
s[2] = 'N';
}
else if (!strcmp(var, "GRID"))
strcpy(s, field_str("MYGRID"));
else if (!strcmp(var, "GRIDSQUARE")){
strcpy(s, field_str("MYGRID"));
s[4] = 0;
}
else if (!strcmp(var, "EXCH")){
strcpy(s, field_str("NR"));
}
else if (!strcmp(var, "WIPE"))
call_wipe();
else if (!strcmp(var, "SAVE")){
enter_qso();
}
else
*s = 0;
}
static char *macro_expand_var(char *var, char *s){
*s = 0;
if (!strcmp(var, "RUN"))
is_running = 1;
else if (!strcmp(var, "S&P"))
is_running = 0;
else if(!strcmp(var, "MYCALL"))
macro_get_var(var, s);
else if (!strcmp(var, "CALL"))
macro_get_var(var, s);
else if (!strcmp(var, "SENTRSTCUT"))
strcpy(s, "5NN");
else if (!strcmp(var, "SENTRST"))
macro_get_var(var, s);
else if (!strcmp(var, "EXCH") || !strcmp(var, "EXCHANGE"))
macro_get_var("EXCH", s);
else if (!strcmp(var, "GRID"))
macro_get_var(var, s);
else if (!strcmp(var, "GRIDSQUARE"))
macro_get_var(var, s);
return s + strlen(s);
}
static char *macro_expand_short_var(char v, char *dest){
if(v == '*')
return macro_expand_var("MYCALL", dest);
else if (v == '!')
return macro_expand_var("CALL", dest);
else if (v == '#')
return macro_expand_var("EXCH", dest);
else if (v == '@')
return macro_expand_var("FREQ", dest);
return dest;
}
int macro_exec(int key, char *dest){
struct macro *m = NULL;
//if you are running, take the first definition of the fn key,
//else (you are s&p-ing, take the last definition
int i;
for(i = 0; i < MACRO_MAX; i++)
if (macro_table[i].fn_key == key){
m = macro_table + i;
//if(is_running)
break;
}
if(!m)
return -1;
//now we have a candidate macro
char macro_name[10];
char *q = dest;
char *p = m->text;
char var[20];
char in_var = 0;
var[0] = 0;
//we end at the string end, newline, etc.
while (*p >= ' '){
//the sequency of if-else-if is imp.
if (*p == '!' || *p == '*' || *p == '@' || *p == '#'){
q = macro_expand_short_var(*p++, q);
}
else if (*p == '}'){
q = macro_expand_var(var, q);
var[0] = 0;
p++;
in_var = 0;
}
else if (in_var == 1){
int l = strlen(var);
var[l++] = *p++;
var[l++] = 0;
}
else if (*p == '{'){
in_var = 1;
p++;
}
else{
*q++ = *p++;
*q = 0;
}
}
return 0;
}
/*
int main(int argc, char *argv[]){
macro_load("CQWWCW");
strcpy(mycallsign, "vu2ese");
strcpy(yourcallsign, "ka7exm");
strcpy(mygrid, "Mk97fg");
char buff[100];
macro_exec(atoi(argv[1]), atoi(argv[2]), buff);
puts(buff);
}
*/