comparison ffstools/newcomp/compile-fc-batt.c @ 318:182c3ae209f6

compile-fc-batt tool written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 17 Dec 2017 00:57:42 +0000
parents
children c79aaed75bd8
comparison
equal deleted inserted replaced
317:b8479251ad0a 318:182c3ae209f6
1 /*
2 * This utility compiles a table of battery thresholds for the new FreeCalypso
3 * battery management code from ASCII source into the binary form suitable
4 * for uploading into /etc/batterytab 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, *outf;
16 char linebuf[256];
17 int lineno, record_count;
18 unsigned last_mv, last_percent;
19
20 process_line()
21 {
22 char *cp;
23 unsigned mv, percent;
24
25 for (cp = linebuf; isspace(*cp); cp++)
26 ;
27 if (*cp == '\0' || *cp == '#')
28 return(0);
29 if (!isdigit(*cp)) {
30 inv: fprintf(stderr, "%s line %d: invalid syntax\n", infname,
31 lineno);
32 exit(ERROR_USAGE);
33 }
34 mv = strtoul(cp, 0, 10);
35 while (isdigit(*cp))
36 cp++;
37 if (!isspace(*cp))
38 goto inv;
39 while (isspace(*cp))
40 cp++;
41 if (!isdigit(*cp))
42 goto inv;
43 percent = strtoul(cp, 0, 10);
44 while (isdigit(*cp))
45 cp++;
46 while (isspace(*cp))
47 cp++;
48 if (*cp != '\0' && *cp != '#')
49 goto inv;
50 if (mv > 0xFFFF) {
51 fprintf(stderr, "%s line %d: the millivolt value is invalid\n",
52 infname, lineno);
53 exit(ERROR_USAGE);
54 }
55 if (percent > 100) {
56 fprintf(stderr, "%s line %d: the percent value is invalid\n",
57 infname, lineno);
58 exit(ERROR_USAGE);
59 }
60 if (record_count) {
61 if (mv >= last_mv) {
62 fprintf(stderr,
63 "%s line %d: millivolt numbers must be decreasing\n",
64 infname, lineno);
65 exit(ERROR_USAGE);
66 }
67 if (percent >= last_percent) {
68 fprintf(stderr,
69 "%s line %d: percent numbers must be decreasing\n",
70 infname, lineno);
71 exit(ERROR_USAGE);
72 }
73 }
74 putc(mv, outf);
75 putc(mv >> 8, outf);
76 putc(percent, outf);
77 putc(0, outf);
78 last_mv = mv;
79 last_percent = percent;
80 record_count++;
81 return(1);
82 }
83
84 main(argc, argv)
85 char **argv;
86 {
87 if (argc != 3) {
88 fprintf(stderr, "usage: %s srcfile output-binfile\n", argv[0]);
89 exit(ERROR_USAGE);
90 }
91 infname = argv[1];
92 inf = fopen(infname, "r");
93 if (!inf) {
94 perror(infname);
95 exit(ERROR_UNIX);
96 }
97 outf = fopen(argv[2], "w");
98 if (!outf) {
99 perror(argv[2]);
100 exit(ERROR_UNIX);
101 }
102 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++)
103 process_line();
104 fclose(inf);
105 fclose(outf);
106 if (record_count)
107 exit(0);
108 fprintf(stderr, "error: %s is empty\n", infname);
109 exit(ERROR_USAGE);
110 }