FreeCalypso > hg > freecalypso-tools
comparison ffstools/newcomp/compile-fc-chg.c @ 320:7572c35a768a
compile-fc-chg tool written, compiles
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 17 Dec 2017 02:25:05 +0000 |
parents | |
children | 0937521ec2f4 |
comparison
equal
deleted
inserted
replaced
319:cbb6ebf665fa | 320:7572c35a768a |
---|---|
1 /* | |
2 * This utility compiles a FreeCalypso battery charging configuration file | |
3 * from ASCII source into the binary form suitable for uploading into | |
4 * /etc/charging on a FreeCalypso device. | |
5 */ | |
6 | |
7 #include <ctype.h> | |
8 #include <string.h> | |
9 #include <strings.h> | |
10 #include <stdio.h> | |
11 #include <stdlib.h> | |
12 #include "../../rvinterf/include/exitcodes.h" | |
13 | |
14 char *infname; | |
15 FILE *inf; | |
16 char linebuf[256]; | |
17 int lineno; | |
18 | |
19 struct setting { | |
20 char *kw; | |
21 int allow_always; | |
22 int allow_never; | |
23 int mandatory; | |
24 unsigned default_value; | |
25 unsigned set_value; | |
26 int is_set; | |
27 } settings[] = { | |
28 {"start-delay", 0, 0, 0, 0, 0, 0}, | |
29 {"start-threshold", 1, 1, 1, 0, 0, 0}, | |
30 {"restart-threshold", 0, 1, 1, 0, 0, 0}, | |
31 {"charge-to-voltage", 0, 0, 1, 0, 0, 0}, | |
32 {"overvoltage", 0, 0, 0, 0xFFFF, 0, 0}, | |
33 {"ci-current", 0, 0, 1, 0, 0, 0}, | |
34 {"end-current", 0, 0, 1, 0, 0, 0}, | |
35 {"i2v-offset", 0, 0, 0, 0, 0, 0}, | |
36 {0, 0, 0, 0, 0, 0, 0} | |
37 }; | |
38 | |
39 static void | |
40 do_setting(tp, arg) | |
41 struct setting *tp; | |
42 char *arg; | |
43 { | |
44 char *endp; | |
45 | |
46 if (tp->allow_always && !strcmp(arg, "always")) { | |
47 tp->set_value = 0xFFFF; | |
48 return; | |
49 } | |
50 if (tp->allow_never && !strcmp(arg, "never")) { | |
51 tp->set_value = 0; | |
52 return; | |
53 } | |
54 tp->set_value = strtoul(arg, &endp, 0); | |
55 if (*endp || tp->set_value > 0xFFFF) { | |
56 fprintf(stderr, "%s line %d: invalid argument to %s\n", | |
57 infname, lineno, tp->kw); | |
58 exit(ERROR_USAGE); | |
59 } | |
60 } | |
61 | |
62 process_line() | |
63 { | |
64 char *cp, *kw, *arg; | |
65 struct setting *tp; | |
66 | |
67 for (cp = linebuf; isspace(*cp); cp++) | |
68 ; | |
69 if (*cp == '\0' || *cp == '#') | |
70 return(0); | |
71 kw = cp; | |
72 while (*cp && !isspace(*cp)) | |
73 cp++; | |
74 if (!*cp) { | |
75 inv: fprintf(stderr, "%s line %d: invalid syntax\n", infname, | |
76 lineno); | |
77 exit(ERROR_USAGE); | |
78 } | |
79 *cp++ = '\0'; | |
80 while (isspace(*cp)) | |
81 cp++; | |
82 if (*cp == '\0' || *cp == '#') | |
83 goto inv; | |
84 arg = cp; | |
85 while (*cp && !isspace(*cp)) | |
86 cp++; | |
87 if (*cp) | |
88 *cp++ = '\0'; | |
89 while (isspace(*cp)) | |
90 cp++; | |
91 if (*cp != '\0' && *cp != '#') | |
92 goto inv; | |
93 for (tp = settings; tp->kw; tp++) | |
94 if (!strcmp(tp->kw, kw)) | |
95 break; | |
96 if (!tp->kw) { | |
97 fprintf(stderr, "%s line %d: setting \"%s\" not known\n", | |
98 infname, lineno, kw); | |
99 exit(ERROR_USAGE); | |
100 } | |
101 if (tp->is_set) { | |
102 fprintf(stderr, "%s line %d: %s set more than once\n", infname, | |
103 lineno, kw); | |
104 exit(ERROR_USAGE); | |
105 } | |
106 do_setting(tp, arg); | |
107 tp->is_set = 1; | |
108 return(1); | |
109 } | |
110 | |
111 set_defaults() | |
112 { | |
113 struct setting *tp; | |
114 | |
115 for (tp = settings; tp->kw; tp++) { | |
116 if (tp->is_set) | |
117 continue; | |
118 if (tp->mandatory) { | |
119 fprintf(stderr, | |
120 "error: required setting %s is not set\n", | |
121 tp->kw); | |
122 exit(ERROR_USAGE); | |
123 } | |
124 tp->set_value = tp->default_value; | |
125 } | |
126 } | |
127 | |
128 write_output(filename) | |
129 char *filename; | |
130 { | |
131 FILE *of; | |
132 struct setting *tp; | |
133 | |
134 of = fopen(filename, "w"); | |
135 if (!of) { | |
136 perror(filename); | |
137 exit(ERROR_UNIX); | |
138 } | |
139 for (tp = settings; tp->kw; tp++) { | |
140 putc(tp->set_value, of); | |
141 putc(tp->set_value >> 8, of); | |
142 } | |
143 fclose(of); | |
144 } | |
145 | |
146 main(argc, argv) | |
147 char **argv; | |
148 { | |
149 if (argc != 3) { | |
150 fprintf(stderr, "usage: %s srcfile output-binfile\n", argv[0]); | |
151 exit(ERROR_USAGE); | |
152 } | |
153 infname = argv[1]; | |
154 inf = fopen(infname, "r"); | |
155 if (!inf) { | |
156 perror(infname); | |
157 exit(ERROR_UNIX); | |
158 } | |
159 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) | |
160 process_line(); | |
161 fclose(inf); | |
162 set_defaults(); | |
163 write_output(argv[2]); | |
164 exit(0); | |
165 } |