mdebug

cortex m series debugger
git clone http://frotz.net/git/mdebug.git
Log | Files | Refs | README | LICENSE

mkbuiltins.c (2907B)


      1 /* mkbuiltin.c
      2  *
      3  * Copyright 2011 Brian Swetland <swetland@frotz.net>
      4  * 
      5  * Licensed under the Apache License, Version 2.0 (the "License");
      6  * you may not use this file except in compliance with the License.
      7  * You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  * Unless required by applicable law or agreed to in writing, software
     12  * distributed under the License is distributed on an "AS IS" BASIS,
     13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  * See the License for the specific language governing permissions and
     15  * limitations under the License.
     16  */
     17 
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <unistd.h>
     21 #include <string.h>
     22 #include <fcntl.h>
     23 
     24 void *load_file(const char *fn, size_t *_sz) {
     25 	int fd;
     26 	off_t sz;
     27 	void *data = NULL;
     28 	fd = open(fn, O_RDONLY);
     29 	if (fd < 0) goto fail;
     30 	sz = lseek(fd, 0, SEEK_END);
     31 	if (sz < 0) goto fail;
     32 	if (lseek(fd, 0, SEEK_SET)) goto fail;
     33 	if ((data = malloc(sz + 4)) == NULL) goto fail;
     34 	if (read(fd, data, sz) != sz) goto fail;
     35 	*_sz = sz;
     36 	return data;
     37 fail:
     38 	if (data) free(data);
     39 	if (fd >= 0) close(fd);
     40 	return NULL;
     41 }
     42 
     43 typedef struct entry {
     44 	struct entry *next;
     45 	const char *fn;
     46 	void *data;
     47 	size_t sz;
     48 } entry;
     49 
     50 entry *first = NULL, *last = NULL;
     51 
     52 int import(const char *fn) {
     53 	char *x;
     54 	entry *e = malloc(sizeof(entry));
     55 	if (e == NULL) return -1;
     56 	e->data = load_file(fn, &e->sz);
     57 	if (e->data == NULL) return -1;
     58 	if ((x = strrchr(fn, '/'))) {
     59 		e->fn = x + 1;
     60 	} else {
     61 		e->fn = fn;
     62 	}
     63 	e->next = NULL;
     64 	if (last) {
     65 		last->next = e;
     66 	} else {
     67 		first = e;
     68 	}
     69 	last = e;
     70 	return 0;
     71 }
     72 
     73 void exportline(unsigned char *x, int len) {
     74 	printf("\t\"");
     75 	while (len > 0) {
     76 		printf("\\x%02X", *x++);
     77 		len--;
     78 	}
     79 	printf("\"\n");
     80 }
     81 
     82 void export(entry *e) {
     83 	unsigned char *x = e->data;
     84 	size_t len = e->sz;
     85 	printf("\t{ \"%s\", %zu,\n", e->fn, e->sz);
     86 	while (len > 0) {
     87 		int xfer = (len > 16) ? 16 : len;
     88 		exportline(x, xfer);
     89 		x += xfer;
     90 		len -= xfer;
     91 	}
     92 	printf("\t},\n");
     93 }
     94 
     95 int main(int argc, char **argv) {
     96 	entry *e;
     97 
     98 	while (argc > 1) {
     99 		if (import(argv[1])) return -1;
    100 		argc--;
    101 		argv++;
    102 	}
    103 
    104 	printf(
    105 "/* this file is machine-generated by mkbuiltins -- do not modify */\n\n"
    106 "#include <string.h>\n"
    107 "#include <stdint.h>\n\n"
    108 "static struct {\n"
    109 "	const char *name;\n"
    110 "	size_t size;\n"
    111 "	void *data;\n"
    112 "} files[] = {\n"
    113 	);
    114 	for (e = first; e != NULL; e = e->next) {
    115 		export(e);
    116 	}
    117 	printf(
    118 "};\n\n"
    119 "void *get_builtin_file(const char *name, size_t *sz) {\n"
    120 "	int n;\n"
    121 "	for (n = 0; n < (sizeof(files)/sizeof(files[0])); n++) {\n"
    122 "		if (!strcmp(name, files[n].name)) {\n"
    123 "			*sz = files[n].size;\n"
    124 "			return files[n].data;\n"
    125 "		}\n"
    126 "	}\n"
    127 "	return NULL;\n"
    128 "}\n"
    129 "\n"
    130 "const char *get_builtin_filename(unsigned n) {\n"
    131 "	if (n >= (sizeof(files)/sizeof(files[0]))) {\n"
    132 "		return NULL;\n"
    133 "	}\n"
    134 "	return files[n].name;\n"
    135 "}\n"
    136 	);
    137 
    138 	return 0;
    139 }
    140