comparison simtool/pbrestore.c @ 10:ddd767f6e15b

fc-simtool ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 07:11:25 +0000
parents
children
comparison
equal deleted inserted replaced
9:c9ef9e91dd8e 10:ddd767f6e15b
1 /*
2 * This module implements pb-restore and pb-update commands.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <string.h>
8 #include <strings.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include "curfile.h"
12
13 extern FILE *open_script_input_file();
14
15 extern char *alpha_from_file_qstring();
16 extern char *alpha_from_file_hex();
17
18 static
19 process_record(line, bin_file_buf, filename_for_errs, lineno_for_errs)
20 char *line, *filename_for_errs;
21 u_char *bin_file_buf;
22 {
23 unsigned recno;
24 u_char record[255], *fixp;
25 u_char digits[20];
26 unsigned ndigits, num_digit_bytes;
27 char *cp;
28 int c;
29
30 recno = strtoul(line+1, 0, 10);
31 if (recno < 1 || recno > curfile_record_count) {
32 fprintf(stderr, "%s line %d: record number is out of range\n",
33 filename_for_errs, lineno_for_errs);
34 return(-1);
35 }
36 cp = line + 1;
37 while (isdigit(*cp))
38 cp++;
39 if (*cp++ != ':') {
40 inv_syntax: fprintf(stderr, "%s line %d: invalid syntax\n",
41 filename_for_errs, lineno_for_errs);
42 return(-1);
43 }
44 while (isspace(*cp))
45 cp++;
46 memset(record, 0xFF, curfile_record_len);
47 fixp = record + curfile_record_len - 14;
48 if (digit_char_to_gsm(*cp) < 0)
49 goto inv_syntax;
50 for (ndigits = 0; ; ndigits++) {
51 c = digit_char_to_gsm(*cp);
52 if (c < 0)
53 break;
54 cp++;
55 if (ndigits >= 20) {
56 fprintf(stderr, "%s line %d: too many number digits\n",
57 filename_for_errs, lineno_for_errs);
58 return(-1);
59 }
60 digits[ndigits] = c;
61 }
62 if (ndigits & 1)
63 digits[ndigits++] = 0xF;
64 num_digit_bytes = ndigits >> 1;
65 fixp[0] = num_digit_bytes + 1;
66 pack_digit_bytes(digits, fixp + 2, num_digit_bytes);
67 if (*cp++ != ',')
68 goto inv_syntax;
69 if (cp[0] != '0' || cp[1] != 'x' && cp[1] != 'X' || !isxdigit(cp[2]) ||
70 !isxdigit(cp[3]) || !isspace(cp[4]))
71 goto inv_syntax;
72 fixp[1] = strtoul(cp, 0, 16);
73 cp += 5;
74 while (isspace(*cp))
75 cp++;
76 if (!strncasecmp(cp, "CCP=", 4)) {
77 cp += 4;
78 fixp[12] = strtoul(cp, 0, 0);
79 while (*cp && !isspace(*cp))
80 cp++;
81 while (isspace(*cp))
82 cp++;
83 }
84 if (!strncasecmp(cp, "EXT=", 4)) {
85 cp += 4;
86 fixp[13] = strtoul(cp, 0, 0);
87 while (*cp && !isspace(*cp))
88 cp++;
89 while (isspace(*cp))
90 cp++;
91 }
92 if (*cp == '"') {
93 cp++;
94 cp = alpha_from_file_qstring(cp, record,
95 curfile_record_len - 14,
96 filename_for_errs,
97 lineno_for_errs);
98 if (!cp)
99 return(-1);
100 } else if (!strncasecmp(cp, "HEX", 3)) {
101 cp += 3;
102 while (isspace(*cp))
103 cp++;
104 cp = alpha_from_file_hex(cp, record, curfile_record_len - 14,
105 filename_for_errs, lineno_for_errs);
106 if (!cp)
107 return(-1);
108 } else
109 goto inv_syntax;
110 while (isspace(*cp))
111 cp++;
112 if (*cp)
113 goto inv_syntax;
114 if (bin_file_buf) {
115 bcopy(record, bin_file_buf + (recno - 1) * curfile_record_len,
116 curfile_record_len);
117 return(0);
118 } else
119 return update_rec_op(recno, 0x04, record, curfile_record_len);
120 }
121
122 cmd_pb_restore(argc, argv)
123 char **argv;
124 {
125 int rc;
126 FILE *inf;
127 int lineno;
128 char linebuf[1024];
129 u_char *databuf;
130
131 rc = phonebook_op_common(argv[1]);
132 if (rc < 0)
133 return(rc);
134 databuf = malloc(curfile_total_size);
135 if (!databuf) {
136 perror("malloc for full phonebook EF");
137 return(-1);
138 }
139 inf = open_script_input_file(argv[2]);
140 if (!inf) {
141 perror(argv[2]);
142 free(databuf);
143 return(-1);
144 }
145 memset(databuf, 0xFF, curfile_total_size);
146 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
147 if (!index(linebuf, '\n')) {
148 fprintf(stderr,
149 "%s line %d: too long or missing newline\n",
150 argv[2], lineno);
151 fclose(inf);
152 free(databuf);
153 return(-1);
154 }
155 if (linebuf[0] != '#' || !isdigit(linebuf[1]))
156 continue;
157 rc = process_record(linebuf, databuf, argv[2], lineno);
158 if (rc < 0) {
159 fclose(inf);
160 free(databuf);
161 return(rc);
162 }
163 }
164 fclose(inf);
165 rc = restore_bin_records(databuf);
166 free(databuf);
167 return(rc);
168 }
169
170 cmd_pb_update(argc, argv)
171 char **argv;
172 {
173 int rc;
174 FILE *inf;
175 int lineno;
176 char linebuf[1024];
177
178 rc = phonebook_op_common(argv[1]);
179 if (rc < 0)
180 return(rc);
181 inf = open_script_input_file(argv[2]);
182 if (!inf) {
183 perror(argv[2]);
184 return(-1);
185 }
186 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
187 if (!index(linebuf, '\n')) {
188 fprintf(stderr,
189 "%s line %d: too long or missing newline\n",
190 argv[2], lineno);
191 fclose(inf);
192 return(-1);
193 }
194 if (linebuf[0] != '#' || !isdigit(linebuf[1]))
195 continue;
196 rc = process_record(linebuf, 0, argv[2], lineno);
197 if (rc < 0) {
198 fclose(inf);
199 return(rc);
200 }
201 }
202 fclose(inf);
203 return(0);
204 }
205
206 cmd_lnd_restore(argc, argv)
207 char **argv;
208 {
209 int rc;
210 FILE *inf;
211 int lineno;
212 char linebuf[1024];
213 u_char *databuf;
214
215 rc = select_ef_lnd();
216 if (rc < 0)
217 return(rc);
218 databuf = malloc(curfile_total_size);
219 if (!databuf) {
220 perror("malloc for full EF_LND");
221 return(-1);
222 }
223 inf = open_script_input_file(argv[1]);
224 if (!inf) {
225 perror(argv[1]);
226 free(databuf);
227 return(-1);
228 }
229 memset(databuf, 0xFF, curfile_total_size);
230 for (lineno = 1; fgets(linebuf, sizeof linebuf, inf); lineno++) {
231 if (!index(linebuf, '\n')) {
232 fprintf(stderr,
233 "%s line %d: too long or missing newline\n",
234 argv[1], lineno);
235 fclose(inf);
236 free(databuf);
237 return(-1);
238 }
239 if (linebuf[0] != '#' || !isdigit(linebuf[1]))
240 continue;
241 rc = process_record(linebuf, databuf, argv[1], lineno);
242 if (rc < 0) {
243 fclose(inf);
244 free(databuf);
245 return(rc);
246 }
247 }
248 fclose(inf);
249 rc = restore_bin_cyclic(databuf);
250 free(databuf);
251 return(rc);
252 }