forked from cminyard/ser2net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyamlconf.c
1215 lines (1072 loc) · 26.9 KB
/
yamlconf.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ser2net - A program for allowing telnet connection to serial ports
* Copyright (C) 2019 Corey Minyard <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdbool.h>
#include <yaml.h>
#include <syslog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <gensio/gensio.h>
#include <gensio/argvutils.h>
#include "ser2net.h"
#include "dataxfer.h"
#include "readconfig.h"
#include "led.h"
//#define DEBUG 1
/*
* States for the state machine reading the configuration file.
*/
enum ystate {
PARSE_ERR,
BEGIN_DOC,
/*
* The main level consists of mappings.
*/
MAIN_LEVEL,
IN_DEFINE,
/*
* We have read the type of the main mapping (connection, led, etc)
* now waiting for a mapping start.
*/
IN_MAIN_NAME,
/*
* We are in the mapping of a "normal" main mapping, it will contain
* things like name, accepter, etc. in key-value pairs.
*/
IN_MAIN_MAP,
/*
* Got the key-value name, waiting for the value.
*/
IN_MAIN_MAP_KEYVAL,
/*
* Got an "options" key, these consists of another mapping holding
* the options.
*/
IN_OPTIONS,
IN_OPTIONS_MAP,
IN_OPTIONS_NAME,
/*
* Special handling for an integer timeout.
*/
IN_CONNSPEC_TIMEOUT,
/*
* Special handling for an enable bool.
*/
IN_CONNSPEC_ENABLE,
/*
* Rotators have a sequence of connections.
*/
IN_ROTATOR_CONNECTIONS,
IN_ROTATOR_CONNECTIONS_SEQ,
/*
* Shouldn't see anything after this.
*/
END_DOC
};
struct alias {
char *name;
char *value;
unsigned int namelen;
struct alias *next;
};
struct yfile {
char *name;
char *value;
unsigned int namelen;
struct yfile *next;
};
struct scalar_next_state;
struct option_info {
char *name;
enum ystate option_next_state;
};
struct keyval_info {
char *name;
unsigned int keyval_offset;
int keyval_type;
};
struct map_info {
char *name;
struct scalar_next_state *states;
enum ystate next_state;
int map_type;
bool needs_anchor;
};
enum which_info {
WHICH_INFO_NONE = 0,
WHICH_INFO_OPTION,
WHICH_INFO_KEYVAL,
WHICH_INFO_MAP
};
struct scalar_next_state {
char *name;
enum ystate next_state;
enum which_info infotype;
union {
struct option_info *option_info;
struct keyval_info *keyval_info;
struct map_info *map_info;
};
};
struct yconf {
enum ystate state;
struct map_info *map_info;
struct keyval_info *keyval_info;
/* main keyvalue types. */
char *name;
char *accepter;
char *driver;
char *connector;
char *value;
char *class;
unsigned int timeout;
bool enable;
char **connections;
unsigned int curr_connection;
unsigned int connections_len;
struct option_info *option_info;
char **options;
char *optionname;
unsigned int curr_option;
unsigned int options_len;
struct alias *aliases;
struct yfile *files;
yaml_parser_t parser;
yaml_event_t e;
struct absout *eout;
};
static void
dofree(char **val)
{
if (*val) {
free(*val);
*val = NULL;
}
}
static void
yconf_cleanup_main(struct yconf *y)
{
dofree(&y->name);
dofree(&y->accepter);
dofree(&y->driver);
y->timeout = 0;
y->enable = true;
dofree(&y->connector);
dofree(&y->value);
dofree(&y->class);
dofree(&y->optionname);
if (y->options) {
unsigned int i;
for (i = 0; i < y->curr_option && y->options[i]; i++) {
free(y->options[i]);
y->options[i] = NULL;
}
}
y->curr_option = 0;
if (y->connections) {
unsigned int i;
for (i = 0; i < y->curr_connection && y->connections[i]; i++) {
free(y->connections[i]);
y->connections[i] = NULL;
}
}
y->curr_connection = 0;
}
static int
syslog_eprint(struct absout *e, const char *str, ...)
{
va_list ap;
char buf[1024];
struct yconf *y = e->data;
va_start(ap, str);
vsnprintf(buf, sizeof(buf), str, ap);
va_end(ap);
syslog(LOG_ERR, "%s on line %lu column %lu", buf,
(unsigned long) y->e.start_mark.line,
(unsigned long) y->e.start_mark.column);
return 0;
}
static struct alias *
lookup_alias_len(struct yconf *y, const char *name, unsigned int len)
{
struct alias *a;
a = y->aliases;
while (a && (a->namelen != len || strncmp(a->name, name, len) != 0))
a = a->next;
return a;
}
static struct alias *
lookup_alias(struct yconf *y, const char *name)
{
return lookup_alias_len(y, name, strlen(name));
}
static int
add_alias(struct yconf *y, const char *iname, const char *ivalue)
{
struct alias *a;
char *name, *value;
name = strdup(iname);
if (!name) {
y->eout->out(y->eout, "Out of memory allocating alias name");
return -1;
}
value = strdup(ivalue);
if (!value) {
free(name);
y->eout->out(y->eout, "Out of memory allocating alias value");
return -1;
}
a = lookup_alias(y, name);
if (a) {
free(a->name);
free(a->value);
} else {
a = malloc(sizeof(*a));
if (!a) {
free(name);
free(value);
y->eout->out(y->eout, "Out of memory allocating alias");
return -1;
}
a->next = y->aliases;
y->aliases = a;
}
a->name = name;
a->value = value;
a->namelen = strlen(name);
return 0;
}
static struct yfile *
lookup_filename_len(struct yconf *y, const char *filename, unsigned int len)
{
struct yfile *f = y->files;
int infd, rv;
char *name, *value = NULL;
struct stat stat;
while (f && f->namelen == len && strncmp(f->name, filename, len) != 0)
f = f->next;
if (f)
return f;
name = strndup(filename, len);
if (!name) {
y->eout->out(y->eout, "Out of memory allocating alias name");
return NULL;
}
infd = open(name, O_RDONLY);
if (infd == -1) {
y->eout->out(y->eout, "Error opening %s: %s", name, strerror(errno));
goto out_err;
}
rv = fstat(infd, &stat);
if (rv == -1) {
y->eout->out(y->eout, "Error stat-ing %s: %s", name, strerror(errno));
goto out_err;
}
value = malloc(stat.st_size + 1);
if (!value) {
y->eout->out(y->eout, "Error allocating memory for file %s", name);
goto out_err;
}
rv = read(infd, value, stat.st_size);
if (rv == -1) {
y->eout->out(y->eout, "Error reading %s: %s", name, strerror(errno));
goto out_err;
}
value[stat.st_size] = '\0';
f = malloc(sizeof(*f));
if (!f) {
y->eout->out(y->eout, "Error allocating memory for file struct %s",
name);
goto out_err;
}
close(infd);
f->name = name;
f->namelen = strlen(name);
f->value = value;
f->next = y->files;
y->files = f;
return f;
out_err:
if (value)
free(value);
free(name);
if (infd != -1)
close(infd);
return NULL;
}
static int
add_option(struct yconf *y, const char *name, const char *option,
const char *place)
{
if (y->curr_option >= y->options_len) {
unsigned int new_len = y->options_len + 10;
char **new_options = malloc(sizeof(char *) * new_len);
if (!new_options) {
y->eout->out(y->eout,
"Out of memory allocating option array for %s", place);
return -1;
}
memcpy(new_options, y->options, sizeof(char *) * y->options_len);
free(y->options);
y->options = new_options;
y->options_len = new_len;
}
if (name) {
char *s;
if (option && strlen(option) > 0) {
s = malloc(strlen(name) + strlen(option) + 2);
if (s)
sprintf(s, "%s=%s", name, option);
} else {
s = strdup(name);
}
if (!s) {
y->eout->out(y->eout, "Out of memory allocating option %s for %s",
option, place);
return -1;
}
y->options[y->curr_option] = s;
} else {
y->options[y->curr_option] = NULL;
}
y->curr_option++;
return 0;
}
static int
add_connection(struct yconf *y, const char *connection, const char *place)
{
if (y->curr_connection >= y->connections_len) {
unsigned int new_len = y->connections_len + 10;
char **new_connections = malloc(sizeof(char *) * new_len);
if (!new_connections) {
y->eout->out(y->eout,
"Out of memory allocating connection array for %s",
place);
return -1;
}
memcpy(new_connections, y->connections,
sizeof(char *) * y->connections_len);
free(y->connections);
y->connections = new_connections;
y->connections_len = new_len;
}
if (connection) {
y->connections[y->curr_connection] = strdup(connection);
if (!y->connections[y->curr_connection]) {
y->eout->out(y->eout,
"Out of memory allocating connection %s for %s",
connection, place);
return -1;
}
} else {
y->connections[y->curr_connection] = NULL;
}
y->curr_connection++;
return 0;
}
enum main_keytypes {
MAIN_KEYTYPE_name,
MAIN_KEYTYPE_accepter,
MAIN_KEYTYPE_driver,
MAIN_KEYTYPE_connector,
MAIN_KEYTYPE_value,
MAIN_KEYTYPE_class
};
#define DECL_KEYVAL(type) \
static struct keyval_info keyval_##type = { #type, \
offsetof(struct yconf, type), \
MAIN_KEYTYPE_##type }
DECL_KEYVAL(name);
DECL_KEYVAL(accepter);
DECL_KEYVAL(driver);
DECL_KEYVAL(connector);
DECL_KEYVAL(value);
DECL_KEYVAL(class);
#define KEYVAL_OFFSET(y) \
((char **) (((char *) (y)) + (y)->keyval_info->keyval_offset))
static struct scalar_next_state sc_default[] = {
{ "name", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_name },
{ "value", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_value },
{ "class", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_class },
{}
};
static struct scalar_next_state sc_deldefault[] = {
{ "name", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_name },
{ "class", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_class },
{}
};
static struct option_info connspec_option_info = {
"connection",
IN_MAIN_MAP,
};
static struct scalar_next_state sc_connection[] = {
{ "accepter", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_accepter },
{ "timeout", IN_CONNSPEC_TIMEOUT },
{ "enable", IN_CONNSPEC_ENABLE },
{ "connector", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_connector },
{ "options", IN_OPTIONS, WHICH_INFO_OPTION,
.option_info = &connspec_option_info },
{}
};
static struct option_info rotator_option_info = {
"rotator",
IN_MAIN_MAP,
};
static struct scalar_next_state sc_rotator[] = {
{ "accepter", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_accepter },
{ "connections", IN_ROTATOR_CONNECTIONS },
{ "options", IN_OPTIONS, WHICH_INFO_OPTION,
.option_info = &rotator_option_info },
{}
};
static struct option_info led_option_info = {
"led",
IN_MAIN_MAP,
};
static struct scalar_next_state sc_led[] = {
{ "driver", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_driver },
{ "options", IN_OPTIONS, WHICH_INFO_OPTION,
.option_info = &led_option_info },
{}
};
static struct scalar_next_state sc_admin[] = {
{ "accepter", IN_MAIN_MAP_KEYVAL, WHICH_INFO_KEYVAL,
.keyval_info = &keyval_accepter },
{ "options", IN_OPTIONS, WHICH_INFO_OPTION,
.option_info = &led_option_info },
{}
};
enum main_map_types {
MAIN_MAP_DEFAULT,
MAIN_MAP_DELDEFAULT,
MAIN_MAP_CONNECTION,
MAIN_MAP_ROTATOR,
MAIN_MAP_LED,
MAIN_MAP_ADMIN
};
static struct map_info sc_default_map = {
"default", sc_default, MAIN_LEVEL, MAIN_MAP_DEFAULT, false
};
static struct map_info sc_deldefault_map = {
"deldefault", sc_deldefault, MAIN_LEVEL, MAIN_MAP_DELDEFAULT, false
};
static struct map_info sc_connection_map = {
"connection", sc_connection, MAIN_LEVEL, MAIN_MAP_CONNECTION, true
};
static struct map_info sc_rotator_map = {
"rotator", sc_rotator, MAIN_LEVEL, MAIN_MAP_ROTATOR, true
};
static struct map_info sc_led_map = {
"led", sc_led, MAIN_LEVEL, MAIN_MAP_LED, true
};
static struct map_info sc_admin_map = {
"admin", sc_admin, MAIN_LEVEL, MAIN_MAP_ADMIN, false
};
static struct scalar_next_state sc_main[] = {
{ "define", IN_DEFINE },
{ "default", IN_MAIN_NAME, WHICH_INFO_MAP, .map_info = &sc_default_map },
{ "delete_default", IN_MAIN_NAME, WHICH_INFO_MAP,
.map_info = &sc_deldefault_map },
{ "connection", IN_MAIN_NAME, WHICH_INFO_MAP,
.map_info = &sc_connection_map },
{ "rotator", IN_MAIN_NAME, WHICH_INFO_MAP, .map_info = &sc_rotator_map },
{ "led", IN_MAIN_NAME, WHICH_INFO_MAP, .map_info = &sc_led_map },
{ "admin", IN_MAIN_NAME, WHICH_INFO_MAP, .map_info = &sc_admin_map },
{}
};
static int
setstr(char **oval, const char *ival, const char *mapname, const char *keyname,
struct absout *eout)
{
if (!ival || strlen(ival) == 0) {
eout->out(eout, "Empty %s %s not permitted", mapname, keyname);
return -1;
}
if (*oval) {
eout->out(eout, "%s %s already set in connection", mapname, keyname);
return -1;
}
*oval = strdup(ival);
if (!*oval) {
eout->out(eout, "Unable to allocate %s %s", mapname, keyname);
return -1;
}
return 0;
}
static void
scalar_next_state(struct yconf *y,
struct scalar_next_state *s, const char *scalar)
{
while (s->name) {
if (strcasecmp(s->name, scalar) == 0) {
switch (s->infotype) {
case WHICH_INFO_NONE: break;
case WHICH_INFO_OPTION: y->option_info = s->option_info; break;
case WHICH_INFO_KEYVAL: y->keyval_info = s->keyval_info; break;
case WHICH_INFO_MAP: y->map_info = s->map_info; break;
}
y->state = s->next_state;
return;
}
s++;
}
y->state = PARSE_ERR;
}
/*
* Convert *(xxx) into the alias text and *{filename} into the
* file's contents.
*/
static char *
process_scalar(struct yconf *y, const char *iscalar)
{
struct absout *eout = y->eout;
const char *s, *start = NULL;
char *rv = NULL, *out = NULL;
unsigned int len = 0, alen;
int state = 0;
restart:
for (s = iscalar; *s; s++) {
if (state == 0) {
if (*s == '*')
state = 1;
else {
if (out)
*out++ = *s;
len++;
}
} else if (state == 1) {
/* Last character was a '*' */
if (*s == '(') {
start = s + 1;
state = 2;
} else if (*s == '{') {
start = s + 1;
state = 3;
} else if (*s == '*') {
if (out)
*out++ = *s;
len++; /* Stay in state 1 */
} else {
if (out) {
*out++ = '*';
*out++ = *s;
}
len += 2;
state = 0;
}
} else if (state == 2) {
/* We are in a '*(' */
if (start == s && *s == '*') {
/* '*(*' outputs a '*(' */
if (out) {
*out++ = '*';
*out++ = '(';
}
len += 2;
state = 0;
} else if (!*s) {
eout->out(eout, "Missing ')' for alias at '%s'", start - 2);
goto out_err;
} else if (*s == ')') {
struct alias *a = lookup_alias_len(y, start, s - start);
if (!a) {
eout->out(eout, "unknown alias at '%s'", start - 2);
goto out_err;
}
alen = strlen(a->value);
if (out) {
memcpy(out, a->value, alen);
out += alen;
}
len += alen;
state = 0;
}
} else if (state == 3) {
/* We are in a '*{' */
if (start == s && *s == '*') {
/* '*{*' outputs a '*{' */
if (out) {
*out++ = '*';
*out++ = '{';
}
len += 2;
state = 0;
} else if (!*s) {
eout->out(eout, "Missing '}' for filename at '%s'", start - 2);
goto out_err;
} else if (*s == '}') {
struct yfile *f = lookup_filename_len(y, start, s - start);
if (!f)
goto out_err;
alen = strlen(f->value);
if (out) {
memcpy(out, f->value, alen);
out += alen;
}
len += alen;
state = 0;
}
}
}
if (!out) {
out = malloc(len + 1);
if (!out) {
eout->out(eout, "Out of memory processing string '%s'", iscalar);
return NULL;
}
rv = out;
goto restart;
}
*out = '\0';
return rv;
out_err:
if (rv)
free(rv);
return NULL;
}
static int
yhandle_scalar(struct yconf *y, const char *anchor, const char *iscalar)
{
struct absout *eout = y->eout;
bool anchor_allowed = false;
char *end;
char *scalar;
scalar = process_scalar(y, iscalar);
if (!scalar)
return -1;
switch (y->state) {
case MAIN_LEVEL:
scalar_next_state(y, sc_main, scalar);
if (y->state == PARSE_ERR) {
eout->out(eout, "Invalid token at the main level: %s\n", scalar);
goto out_err;
}
break;
case IN_DEFINE:
anchor_allowed = true;
if (!anchor)
eout->out(eout, "No anchor for define, define ignored\n");
else {
if (add_alias(y, anchor, scalar))
goto out_err;
}
y->state = MAIN_LEVEL;
break;
case IN_MAIN_MAP:
scalar_next_state(y, y->map_info->states, scalar);
if (y->state == PARSE_ERR) {
eout->out(eout, "Invalid token in the %s: %s",
y->map_info->name, scalar);
goto out_err;
}
break;
case IN_MAIN_MAP_KEYVAL:
if (setstr(KEYVAL_OFFSET(y), scalar, y->map_info->name,
y->keyval_info->name, eout))
goto out_err;
y->state = IN_MAIN_MAP;
break;
case IN_CONNSPEC_TIMEOUT:
y->timeout = strtoul(scalar, &end, 0);
if (end == scalar || *end != '\0') {
eout->out(eout, "Invalid number in connection timeout");
goto out_err;
}
y->state = IN_MAIN_MAP;
break;
case IN_CONNSPEC_ENABLE:
if (strcasecmp(scalar, "on") == 0) {
y->enable = true;
} else if (strcasecmp(scalar, "off") == 0) {
y->enable = false;
} else {
eout->out(eout, "enable must be 'on' or 'off'");
goto out_err;
}
y->state = IN_MAIN_MAP;
break;
case IN_OPTIONS_MAP:
if (setstr(&y->optionname, scalar, y->option_info->name, "option name",
eout))
goto out_err;
y->state = IN_OPTIONS_NAME;
break;
case IN_OPTIONS_NAME:
if (add_option(y, y->optionname, scalar, y->option_info->name))
goto out_err;
dofree(&y->optionname);
y->state = IN_OPTIONS_MAP;
break;
case IN_ROTATOR_CONNECTIONS_SEQ:
if (add_connection(y, scalar, "rotator"))
goto out_err;
break;
default:
eout->out(eout, "Unexpected scalar value");
goto out_err;
}
if (anchor && !anchor_allowed)
eout->out(eout, "Anchor on non-scalar ignored\n");
free(scalar);
return 0;
out_err:
free(scalar);
return -1;
}
static int
yhandle_seq_start(struct yconf *y)
{
switch (y->state) {
case IN_ROTATOR_CONNECTIONS:
y->state = IN_ROTATOR_CONNECTIONS_SEQ;
break;
default:
y->eout->out(y->eout, "Unexpected sequence start: %d", y->state);
return -1;
}
return 0;
}
static int
yhandle_seq_end(struct yconf *y)
{
switch (y->state) {
case IN_ROTATOR_CONNECTIONS_SEQ:
y->state = IN_MAIN_MAP;
break;
default:
y->eout->out(y->eout, "Unexpected sequence end: %d", y->state);
return -1;
}
return 0;
}
static int
yhandle_mapping_start(struct yconf *y)
{
struct absout *eout = y->eout;
switch (y->state) {
case BEGIN_DOC:
y->state = MAIN_LEVEL;
break;
case IN_MAIN_NAME:
if (y->map_info->needs_anchor) {
char *anchor = (char *) y->e.data.mapping_start.anchor;
if (!anchor) {
eout->out(eout, "Main mapping requires an anchor for the name");
return -1;
}
y->name = strdup(anchor);
if (!y->name) {
eout->out(eout, "Out of memory allocating name");
return -1;
}
if (add_alias(y, anchor, anchor))
return -1;
}
y->state = IN_MAIN_MAP;
break;
case IN_OPTIONS:
y->state = IN_OPTIONS_MAP;
break;
default:
eout->out(eout, "Unexpected mapping start: %d", y->state);
return -1;
}
return 0;
}
static int
yhandle_mapping_end(struct yconf *y)
{
struct absout *eout = y->eout;
int err, argc;
const char **argv;
switch (y->state) {
case MAIN_LEVEL:
y->state = END_DOC;
break;
case IN_MAIN_MAP:
switch (y->map_info->map_type) {
case MAIN_MAP_DEFAULT:
if (!y->name) {
eout->out(eout, "No name given in default");
return -1;
}
err = gensio_set_default(so, y->class, y->name, y->value, 0);
if (err) {
eout->out(eout, "Unable to set default name %s:%s:%s: %s",
y->class ? y->class : "",
y->name, y->value, gensio_err_to_str(err));
return -1;
}
y->state = MAIN_LEVEL;
yconf_cleanup_main(y);
break;
case MAIN_MAP_DELDEFAULT:
if (!y->name) {
eout->out(eout, "No name given in delete_default");
return -1;
}
if (!y->class) {
eout->out(eout, "No class given in delete_default");
return -1;
}
err = gensio_del_default(so, y->class, y->name, false);
if (err) {
eout->out(eout, "Unable to set default name %s:%s:%s: %s",
y->class ? y->class : "",
y->name, y->value, gensio_err_to_str(err));
return -1;
}
y->state = MAIN_LEVEL;
yconf_cleanup_main(y);
break;
case MAIN_MAP_CONNECTION:
if (!y->name) {
eout->out(eout, "No name given in connection");
return -1;
}
if (!y->accepter) {
eout->out(eout, "No accepter given in connection");
return -1;
}
if (!y->connector) {
eout->out(eout, "No connector given in connection");
return -1;
}
/* NULL terminate the options. */
if (add_option(y, NULL, NULL, "connection"))
return -1;
portconfig(eout, y->name, y->accepter,
y->enable ? "raw" : "off", y->timeout,
y->connector, (const char **) y->options);
y->state = MAIN_LEVEL;
yconf_cleanup_main(y);
break;
case MAIN_MAP_ROTATOR:
if (!y->name) {
eout->out(eout, "No name given in rotator");
return -1;
}
if (!y->accepter) {