FreeCalypso > hg > tcs211-patches
annotate tool/readspec.c @ 0:12e230d431f0
started writing libpatch tool
author | Space Falcon <falcon@ivan.Harhan.ORG> |
---|---|
date | Wed, 03 Jun 2015 07:40:00 +0000 |
parents | |
children |
rev | line source |
---|---|
0
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
1 /* |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
2 * This module contains the code that reads and parses the patch |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
3 * description or specification ASCII text file. |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
4 */ |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
5 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
6 #include <sys/types.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
7 #include <ctype.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
8 #include <stdio.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
9 #include <stdlib.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
10 #include <string.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
11 #include <strings.h> |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
12 #include "patchinfo.h" |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
13 #include "globals.h" |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
14 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
15 static int lineno; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
16 static struct patch_module_desc **module_add_ptr = &patch_module_list; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
17 static struct patch_desc **patch_add_ptr; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
18 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
19 static void |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
20 set_module(name) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
21 char *name; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
22 { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
23 int len; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
24 char *buf; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
25 struct patch_module_desc *newmod; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
26 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
27 len = sizeof(struct patch_module_desc) + strlen(name) + 1; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
28 buf = malloc(len); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
29 if (!buf) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
30 perror("malloc"); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
31 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
32 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
33 newmod = (struct patch_module_desc *) buf; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
34 newmod->member_name = buf + sizeof(struct patch_module_desc); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
35 strcpy(newmod->member_name, name); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
36 newmod->patches = 0; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
37 newmod->next = 0; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
38 *module_add_ptr = newmod; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
39 module_add_ptr = &newmod->next; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
40 patch_add_ptr = &newmod->patches; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
41 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
42 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
43 static void |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
44 add_patch(argline) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
45 char *argline; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
46 { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
47 char *cp, *args[3]; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
48 int i, len; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
49 char *buf; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
50 struct patch_desc *rec; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
51 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
52 for (cp = argline, i = 0; i < 3; i++) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
53 while (isspace(*cp)) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
54 cp++; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
55 if (!*cp) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
56 fprintf(stderr, "%s line %d: too few fields\n", |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
57 patch_desc_filename, lineno); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
58 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
59 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
60 args[i] = cp; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
61 while (*cp && !isspace(*cp)) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
62 cp++; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
63 if (*cp) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
64 *cp++ = '\0'; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
65 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
66 while (isspace(*cp)) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
67 cp++; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
68 if (*cp) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
69 fprintf(stderr, "%s line %d: too many fields\n", |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
70 patch_desc_filename, lineno); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
71 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
72 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
73 if (!patch_add_ptr) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
74 fprintf(stderr, |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
75 "error: patch given before module (%s line %d)\n", |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
76 patch_desc_filename, lineno); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
77 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
78 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
79 len = sizeof(struct patch_desc) + strlen(args[0]) + 1; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
80 buf = malloc(len); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
81 if (!buf) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
82 perror("malloc"); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
83 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
84 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
85 rec = (struct patch_desc *) buf; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
86 rec->section = buf + sizeof(struct patch_desc); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
87 strcpy(rec->section, args[0]); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
88 rec->offset = strtoul(args[1], 0, 16); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
89 rec->new_byte = strtoul(args[2], 0, 16); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
90 rec->next = 0; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
91 *patch_add_ptr = rec; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
92 patch_add_ptr = &rec->next; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
93 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
94 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
95 read_spec_file() |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
96 { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
97 FILE *f; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
98 char linebuf[128], *cp, *np; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
99 |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
100 f = fopen(patch_desc_filename, "r"); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
101 if (!f) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
102 perror(patch_desc_filename); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
103 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
104 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
105 for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
106 for (cp = linebuf; isspace(*cp); cp++) |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
107 ; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
108 if (!*cp || *cp == '#') |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
109 continue; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
110 if (*cp == '[') { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
111 np = ++cp; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
112 cp = index(cp, ']'); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
113 if (!cp) { |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
114 fprintf(stderr, |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
115 "%s line %d: unmatched bracket\n", |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
116 patch_desc_filename, lineno); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
117 exit(1); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
118 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
119 *cp = '\0'; |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
120 set_module(np); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
121 } else |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
122 add_patch(cp); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
123 } |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
124 fclose(f); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
125 return(0); |
12e230d431f0
started writing libpatch tool
Space Falcon <falcon@ivan.Harhan.ORG>
parents:
diff
changeset
|
126 } |