2008-07-02 09:17:14 +02:00
|
|
|
/*
|
2009-12-15 08:13:14 +01:00
|
|
|
* Copyright (c) 2003-2009, Takahide Matsutsuka.
|
|
|
|
* All rights reserved.
|
2008-07-02 09:17:14 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2009-12-15 08:13:14 +01:00
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
2008-07-02 09:17:14 +02:00
|
|
|
* products derived from this software without specific prior
|
2009-12-15 08:13:14 +01:00
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2008-07-02 09:17:14 +02:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2009-12-15 08:13:14 +01:00
|
|
|
* A main file for hexameter.
|
2008-07-02 09:17:14 +02:00
|
|
|
*/
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
#define MAX_PATH 1024
|
2008-07-02 09:17:14 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-07-20 09:44:39 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-07-02 09:17:14 +02:00
|
|
|
#include <unistd.h>
|
2008-07-20 09:44:39 +02:00
|
|
|
#include <ctype.h>
|
2008-07-02 09:17:14 +02:00
|
|
|
|
|
|
|
#include "ihx2bin.h"
|
|
|
|
|
|
|
|
#define MAXFILES 256
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
typedef struct {
|
2008-07-02 09:17:14 +02:00
|
|
|
char* output;
|
|
|
|
char* dir;
|
|
|
|
char *files[MAXFILES];
|
2008-07-20 09:44:39 +02:00
|
|
|
unsigned int defsize;
|
|
|
|
struct ConvertDefinition defs[DEF_MAX];
|
2008-07-02 09:17:14 +02:00
|
|
|
unsigned char verbose;
|
|
|
|
// number of the ihx files
|
|
|
|
int length;
|
|
|
|
// output file size
|
|
|
|
int size;
|
|
|
|
// output file wriiten size
|
|
|
|
int written;
|
2008-07-20 09:44:39 +02:00
|
|
|
} Configuration;
|
|
|
|
|
|
|
|
static
|
|
|
|
const char* IHXEXT = ".ihx";
|
|
|
|
|
|
|
|
static
|
|
|
|
const char *strcasestr(const char *haystack, const char *needle) {
|
|
|
|
int haypos;
|
|
|
|
int needlepos;
|
|
|
|
|
|
|
|
haypos = 0;
|
|
|
|
while (haystack[haypos]) {
|
|
|
|
if (tolower (haystack[haypos]) == tolower(needle[0])) {
|
|
|
|
needlepos = 1;
|
|
|
|
while ( (needle[needlepos]) &&
|
|
|
|
(tolower(haystack[haypos + needlepos])
|
|
|
|
== tolower(needle[needlepos])) )
|
|
|
|
++needlepos;
|
|
|
|
if (! needle[needlepos]) return (haystack + haypos);
|
|
|
|
}
|
|
|
|
++haypos;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-07-02 09:17:14 +02:00
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
static
|
|
|
|
char *changeExt(const char *path, const char* ext) {
|
2008-07-02 09:17:14 +02:00
|
|
|
char *p;
|
|
|
|
char *tail;
|
|
|
|
char *changed;
|
|
|
|
int len;
|
|
|
|
int extlen = strlen(ext);
|
|
|
|
|
|
|
|
for (tail = (char*) path; *tail != 0; tail++);
|
|
|
|
for (p = tail; p > path; p--) {
|
|
|
|
if (*p == '.') {
|
|
|
|
len = p - path;
|
|
|
|
changed = (char*) malloc(len + extlen + 2);
|
|
|
|
|
|
|
|
strncpy(changed, path, len + 1);
|
|
|
|
strcpy(changed + len + 1, ext);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(path);
|
|
|
|
changed = (char*) malloc(len + extlen + 2);
|
|
|
|
strncpy(changed, path, len);
|
|
|
|
*(changed + len) = '.';
|
|
|
|
strcpy(changed + len + 1, ext);
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
static
|
|
|
|
unsigned char analyzeOption(int argc, char **argv, Configuration *config) {
|
2008-07-02 09:17:14 +02:00
|
|
|
int c;
|
2008-07-20 09:44:39 +02:00
|
|
|
char *defval;
|
|
|
|
|
2008-07-02 09:17:14 +02:00
|
|
|
opterr = 0;
|
2008-07-20 09:44:39 +02:00
|
|
|
while ((c = getopt(argc, argv, "hvo:d:b:")) != EOF) {
|
2008-07-02 09:17:14 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
2008-07-20 09:44:39 +02:00
|
|
|
config->verbose = 1;
|
2008-07-02 09:17:14 +02:00
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
config->output = optarg;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
sscanf(optarg, "%x", &config->size);
|
|
|
|
break;
|
2008-07-20 09:44:39 +02:00
|
|
|
case 'd':
|
|
|
|
if (config->defsize >= DEF_MAX) {
|
|
|
|
printf("excess number of definitions\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
defval = strchr(optarg, '=');
|
|
|
|
if (defval) {
|
|
|
|
*defval = 0;
|
|
|
|
config->defs[config->defsize].name = optarg;
|
|
|
|
config->defs[config->defsize].value = defval + 1;
|
|
|
|
config->defsize++;
|
|
|
|
} else {
|
|
|
|
printf("definition value required:%s\n", optarg);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2008-07-02 09:17:14 +02:00
|
|
|
case 'h':
|
2009-12-15 08:13:14 +01:00
|
|
|
printf("Hexameter: Convert Intel HEX file (ihx) to binary file, ver. 2.1.3\n");
|
|
|
|
printf("Copyright (c) 2003-2009 Takahide Matsutsuka <markn@markn.org>\n");
|
2008-07-20 09:44:39 +02:00
|
|
|
printf("Usage: hexameter [options] <ihx|bin> [<ihx|bin>...]\n");
|
2008-07-02 09:17:14 +02:00
|
|
|
printf("Options:\n");
|
|
|
|
printf(" -v verbose output\n");
|
|
|
|
printf(" -o <output file name>\n");
|
2008-07-20 09:44:39 +02:00
|
|
|
printf(" -d <name>=<value> define property\n");
|
2008-07-02 09:17:14 +02:00
|
|
|
printf(" -b <output file size in hexadecimal bytes>\n");
|
2008-07-20 09:44:39 +02:00
|
|
|
printf(" -h show this help\n");
|
2008-07-02 09:17:14 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
default:
|
|
|
|
printf("unknown option:%c\n", optopt);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
#if 0
|
|
|
|
static
|
|
|
|
int isFileExists(const char *dir, const char *filename) {
|
2008-07-02 09:17:14 +02:00
|
|
|
char path[MAX_PATH];
|
|
|
|
FILE *f;
|
|
|
|
|
|
|
|
if (!filename) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (dir) {
|
|
|
|
strcpy(path, dir);
|
|
|
|
strcpy(path + strlen(path), filename);
|
|
|
|
} else {
|
|
|
|
strcpy(path, filename);
|
|
|
|
}
|
|
|
|
if (!(f = fopen(path, "rb"))) {
|
|
|
|
printf("cannot open: %s\n", filename);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-20 09:44:39 +02:00
|
|
|
#endif
|
2008-07-02 09:17:14 +02:00
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
/**
|
|
|
|
* @return 1 if given filename has an extension of ".ihx"
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int isIhx(const char *filename) {
|
|
|
|
const char* pos = strcasestr(filename, IHXEXT);
|
|
|
|
if (pos && pos[strlen(IHXEXT)] == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2008-07-02 09:17:14 +02:00
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
static
|
|
|
|
int checkExistence(Configuration *config) {
|
2008-07-02 09:17:14 +02:00
|
|
|
int i;
|
|
|
|
FILE *f;
|
2008-07-20 09:44:39 +02:00
|
|
|
|
2008-07-02 09:17:14 +02:00
|
|
|
for (i = 0; i < config->length; i++) {
|
|
|
|
f = fopen(config->files[i], "r");
|
|
|
|
if (!f) {
|
|
|
|
printf("cannot open: %s\n", config->files[i]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return writtn size in bytes
|
|
|
|
*/
|
|
|
|
static
|
|
|
|
int copy(FILE *out, const char* dir, const char* filename, unsigned char verbose) {
|
|
|
|
FILE *in;
|
2008-07-20 09:44:39 +02:00
|
|
|
int ch;
|
2008-07-02 09:17:14 +02:00
|
|
|
char path[MAX_PATH];
|
|
|
|
int bytes = 0;
|
|
|
|
if (!filename) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (dir) {
|
|
|
|
strcpy(path, dir);
|
|
|
|
strcpy(path + strlen(path), filename);
|
|
|
|
} else {
|
|
|
|
strcpy(path, filename);
|
|
|
|
}
|
|
|
|
in = fopen(path, "rb");
|
|
|
|
while ((ch = getc(in)) != EOF) {
|
|
|
|
putc(ch, out);
|
|
|
|
bytes++;
|
|
|
|
}
|
2008-07-20 09:44:39 +02:00
|
|
|
fclose(in);
|
|
|
|
if (verbose) {
|
|
|
|
printf("imported file: %s, size=%d\n", path, bytes);
|
|
|
|
}
|
2008-07-02 09:17:14 +02:00
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
static
|
|
|
|
int output(Configuration *config) {
|
2008-07-02 09:17:14 +02:00
|
|
|
FILE *out;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(out = fopen(config->output, "wb"))) {
|
|
|
|
printf("cannot open output file:%s\n", config->output);
|
|
|
|
return 1;
|
|
|
|
}
|
2008-07-20 09:44:39 +02:00
|
|
|
|
2008-07-02 09:17:14 +02:00
|
|
|
for (i = 0; i < config->length; i++) {
|
2008-07-20 09:44:39 +02:00
|
|
|
struct ConvertInfo info;
|
|
|
|
info.out = out;
|
|
|
|
info.filename = config->files[i];
|
|
|
|
info.verbose = config->verbose;
|
|
|
|
info.defsize = config->defsize;
|
|
|
|
info.defs = config->defs;
|
|
|
|
|
|
|
|
if (isIhx(config->files[i])) {
|
|
|
|
config->written += ihx2bin(&info);
|
|
|
|
} else {
|
|
|
|
config->written += copy(out, NULL, config->files[i], config->verbose);
|
|
|
|
}
|
2008-07-02 09:17:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->size) {
|
|
|
|
if (config->verbose) {
|
|
|
|
printf("Writing trailing bytes\n");
|
|
|
|
}
|
|
|
|
for (; config->size > config->written; config->written++) {
|
|
|
|
putc(0, out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(out);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2008-07-20 09:44:39 +02:00
|
|
|
Configuration config;
|
2008-07-02 09:17:14 +02:00
|
|
|
unsigned char r;
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
memset(&config, 0, sizeof(Configuration));
|
2008-07-02 09:17:14 +02:00
|
|
|
|
2009-12-15 08:13:14 +01:00
|
|
|
#ifndef __CYGWIN__
|
|
|
|
r = analyzeOption(argc, argv, &config);
|
|
|
|
if (r) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
#endif
|
2008-07-02 09:17:14 +02:00
|
|
|
while (optind < argc) {
|
2009-12-15 08:13:14 +01:00
|
|
|
#ifdef __CYGWIN__
|
2008-07-20 09:44:39 +02:00
|
|
|
r = analyzeOption(argc, argv, &config);
|
|
|
|
if (r) {
|
2008-07-02 09:17:14 +02:00
|
|
|
return r;
|
|
|
|
}
|
2009-12-15 08:13:14 +01:00
|
|
|
#endif
|
|
|
|
if (config.length >= MAXFILES) {
|
2008-07-02 09:17:14 +02:00
|
|
|
printf("too much files specified\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
config.files[config.length] = argv[optind];
|
|
|
|
config.length++;
|
|
|
|
optind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.length == 0) {
|
|
|
|
printf("no input specified\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.output == NULL) {
|
|
|
|
config.output = changeExt(config.files[0], "bin");
|
|
|
|
}
|
|
|
|
|
2008-07-20 09:44:39 +02:00
|
|
|
r = checkExistence(&config);
|
|
|
|
if (r) {
|
2008-07-02 09:17:14 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (config.verbose) {
|
|
|
|
printf("Generating file: %s\n", config.output);
|
|
|
|
}
|
2008-07-20 09:44:39 +02:00
|
|
|
r = output(&config);
|
|
|
|
if (r) {
|
2008-07-02 09:17:14 +02:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (config.verbose) {
|
|
|
|
printf("Done.\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|