changeset 0:12e230d431f0

started writing libpatch tool
author Space Falcon <falcon@ivan.Harhan.ORG>
date Wed, 03 Jun 2015 07:40:00 +0000
parents
children b9fe017905f8
files .hgignore tool/Makefile tool/ar.h tool/globals.c tool/globals.h tool/main.c tool/patchinfo.h tool/readspec.c
diffstat 8 files changed, 223 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,4 @@
+syntax: regexp
+
+^tool/.*\.o$
+^tool/libpatch$
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/Makefile	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,18 @@
+CC=	gcc
+CFLAGS=	-O2
+PROG=	libpatch
+OBJS=	globals.o main.o readspec.o
+HDRS=	ar.h globals.h patchinfo.h
+
+all:	${PROG}
+
+${PROG}: ${OBJS}
+	${CC} -o $@ ${OBJS}
+
+${OBJS}: ${HDRS}
+
+install:
+	install -c -o bin -g bin -m 755 ${PROG} /usr/local/bin
+
+clean:
+	rm -f *.o ${PROG} *errs
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/ar.h	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,22 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ *
+ *	@(#)ar.h	5.1 (Berkeley) 5/30/85
+ */
+
+#define	ARMAG	"!<arch>\n"
+#define	SARMAG	8
+
+#define	ARFMAG	"`\n"
+
+struct ar_hdr {
+	char	ar_name[16];
+	char	ar_date[12];
+	char	ar_uid[6];
+	char	ar_gid[6];
+	char	ar_mode[8];
+	char	ar_size[10];
+	char	ar_fmag[2];
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/globals.c	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,7 @@
+/*
+ * This module contains global variable definitions for our libpatch tool.
+ */
+
+char *lib_in_filename, *lib_out_filename;
+char *patch_desc_filename;
+struct patch_module_desc *patch_module_list;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/globals.h	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,5 @@
+/* external declarations for the global variables defined in globals.c */
+
+extern char *lib_in_filename, *lib_out_filename;
+extern char *patch_desc_filename;
+extern struct patch_module_desc *patch_module_list;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/main.c	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,23 @@
+/*
+ * libpatch main module
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include "globals.h"
+
+main(argc, argv)
+	char **argv;
+{
+	if (argc != 4) {
+		fprintf(stderr, "usage: %s in.out patch-desc out.lib\n",
+			argv[0]);
+		exit(1);
+	}
+	lib_in_filename = argv[1];
+	patch_desc_filename = argv[2];
+	lib_out_filename = argv[3];
+	read_spec_file();
+	/* code to be filled here */
+	exit(0);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/patchinfo.h	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,18 @@
+/*
+ * Our libpatch tool's first step will be to read and parse an ASCII text file
+ * specifying the patches to be applied.  This information will be captured
+ * in data structures defined in this header file.
+ */
+
+struct patch_module_desc {
+	char *member_name;
+	struct patch_desc *patches;
+	struct patch_module_desc *next;
+};
+
+struct patch_desc {
+	char *section;
+	unsigned offset;
+	u_char new_byte;
+	struct patch_desc *next;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool/readspec.c	Wed Jun 03 07:40:00 2015 +0000
@@ -0,0 +1,126 @@
+/*
+ * This module contains the code that reads and parses the patch
+ * description or specification ASCII text file.
+ */
+
+#include <sys/types.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include "patchinfo.h"
+#include "globals.h"
+
+static int lineno;
+static struct patch_module_desc **module_add_ptr = &patch_module_list;
+static struct patch_desc **patch_add_ptr;
+
+static void
+set_module(name)
+	char *name;
+{
+	int len;
+	char *buf;
+	struct patch_module_desc *newmod;
+
+	len = sizeof(struct patch_module_desc) + strlen(name) + 1;
+	buf = malloc(len);
+	if (!buf) {
+		perror("malloc");
+		exit(1);
+	}
+	newmod = (struct patch_module_desc *) buf;
+	newmod->member_name = buf + sizeof(struct patch_module_desc);
+	strcpy(newmod->member_name, name);
+	newmod->patches = 0;
+	newmod->next = 0;
+	*module_add_ptr = newmod;
+	module_add_ptr = &newmod->next;
+	patch_add_ptr = &newmod->patches;
+}
+
+static void
+add_patch(argline)
+	char *argline;
+{
+	char *cp, *args[3];
+	int i, len;
+	char *buf;
+	struct patch_desc *rec;
+
+	for (cp = argline, i = 0; i < 3; i++) {
+		while (isspace(*cp))
+			cp++;
+		if (!*cp) {
+			fprintf(stderr, "%s line %d: too few fields\n",
+				patch_desc_filename, lineno);
+			exit(1);
+		}
+		args[i] = cp;
+		while (*cp && !isspace(*cp))
+			cp++;
+		if (*cp)
+			*cp++ = '\0';
+	}
+	while (isspace(*cp))
+		cp++;
+	if (*cp) {
+		fprintf(stderr, "%s line %d: too many fields\n",
+			patch_desc_filename, lineno);
+		exit(1);
+	}
+	if (!patch_add_ptr) {
+		fprintf(stderr,
+			"error: patch given before module (%s line %d)\n",
+			patch_desc_filename, lineno);
+		exit(1);
+	}
+	len = sizeof(struct patch_desc) + strlen(args[0]) + 1;
+	buf = malloc(len);
+	if (!buf) {
+		perror("malloc");
+		exit(1);
+	}
+	rec = (struct patch_desc *) buf;
+	rec->section = buf + sizeof(struct patch_desc);
+	strcpy(rec->section, args[0]);
+	rec->offset = strtoul(args[1], 0, 16);
+	rec->new_byte = strtoul(args[2], 0, 16);
+	rec->next = 0;
+	*patch_add_ptr = rec;
+	patch_add_ptr = &rec->next;
+}
+
+read_spec_file()
+{
+	FILE *f;
+	char linebuf[128], *cp, *np;
+
+	f = fopen(patch_desc_filename, "r");
+	if (!f) {
+		perror(patch_desc_filename);
+		exit(1);
+	}
+	for (lineno = 1; fgets(linebuf, sizeof linebuf, f); lineno++) {
+		for (cp = linebuf; isspace(*cp); cp++)
+			;
+		if (!*cp || *cp == '#')
+			continue;
+		if (*cp == '[') {
+			np = ++cp;
+			cp = index(cp, ']');
+			if (!cp) {
+				fprintf(stderr,
+					"%s line %d: unmatched bracket\n",
+					patch_desc_filename, lineno);
+				exit(1);
+			}
+			*cp = '\0';
+			set_module(np);
+		} else
+			add_patch(cp);
+	}
+	fclose(f);
+	return(0);
+}