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