diff options
| -rw-r--r-- | hktool.c | 31 | ||||
| -rw-r--r-- | params.c | 86 | ||||
| -rw-r--r-- | params.h | 5 | 
3 files changed, 57 insertions, 65 deletions
@@ -19,10 +19,12 @@   */  #include <errno.h> +#include <stdbool.h>  #include <stdio.h>  #include <stdlib.h> -#include <stdbool.h> +#include <string.h>  #include <unistd.h> +#include <assert.h>  #include "halfkay.h"  #include "eprintf.h" @@ -31,6 +33,9 @@  #ifndef VERSION  #define VERSION "unknown version"  #endif +#ifndef DATADIR +#define DATADIR "hktool" +#endif  static const char *usage = "[-hlvr] [-f file] [--] mcu"; @@ -63,12 +68,16 @@ static void version(void)  int main(int argc, char **argv)  {  	bool doflash = false, doreboot = false; -	char *flashfile = NULL; +	char *flashfile = NULL, *mcuname;  	int opt;  	struct flashparams fp;  	setprogname(argv[0] != NULL ? argv[0] : "hktool"); +	datadir = getenv("HKTOOL_DATADIR"); +	if (datadir == NULL || strlen(datadir) == 0) +		datadir = DATADIR; +  	while (opt = getopt(argc, argv, "f:h-lvr"), opt != -1) {  		switch (opt) {  		case 'f': @@ -81,7 +90,7 @@ int main(int argc, char **argv)  			return EXIT_SUCCESS;  			break;  		case 'l': -			getparams(NULL, NULL); +			listparams();  			return EXIT_SUCCESS;  			break;  		case 'v': @@ -102,6 +111,7 @@ int main(int argc, char **argv)  	if (optind >= argc)  		eprintf("No MCU specified\nUsage: %s %s", argv[0], usage); +	mcuname = argv[optind];  	if (!doflash && !doreboot)  		eprintf("Nothing to do"); @@ -109,8 +119,19 @@ int main(int argc, char **argv)  	if (argc - optind > 1)  		eprintf("Invalid number of arguments\nUsage: %s %s", argv[0], usage); -	if (getparams(&fp, argv[optind]) == false) -		eprintf("Could not find flash parameters for '%s'", argv[optind]); +	if (strchr(mcuname, '/') == NULL) { +		int size; +		char *path; + +		size = snprintf(NULL, 0, "%s/%s", datadir, mcuname); +		assert(size > 0); +		path = emalloc((size_t)size + 1); +		snprintf(path, size, "%s/%s", datadir, mcuname); +		mcuname = path; +	} +	readparams(&fp, mcuname); +	if (mcuname != argv[optind]) +		free(mcuname);  	if (doflash)  		if (flash(&fp, flashfile) != 0) @@ -18,9 +18,11 @@   */  #include <assert.h> +#include <ctype.h>  #include <dirent.h>  #include <errno.h>  #include <fcntl.h> +#include <glob.h>  #include <limits.h>  #include <stdbool.h>  #include <stdint.h> @@ -28,7 +30,6 @@  #include <stdlib.h>  #include <string.h>  #include <sys/stat.h> -#include <ctype.h>  #include "eprintf.h"  #include "params.h" @@ -37,12 +38,10 @@  /* TODO: make this dynamic */  #define SZMAX_PFILE 1024 -#ifndef DATADIR -#define DATADIR "hktool" -#endif +char *datadir;  /* readparams: read a file's param definition */ -static void readparams(struct flashparams *fp, const char *file) +void readparams(struct flashparams *fp, const char *file)  {  	char cont[SZMAX_PFILE], *pos;  	size_t contsz; @@ -124,81 +123,50 @@ static void printcname(const char *file)  			putchar(c);  } -/* getparams: Locate a parameter file and load it or show names of all files */ -bool getparams(struct flashparams *fp, const char *name) +/* listparams: Print a list of the parameter files which could be found */ +void listparams(void)  { -	char pathbuf[PATH_MAX], *dirname, *fullpath; +	char pathbuf[PATH_MAX], *fullpath;  	struct dirent *de;  	size_t pathsize; -	bool found;  	DIR *dir; -	assert((fp == NULL && name == NULL) || (fp != NULL && name != NULL));  	fullpath = pathbuf;  	pathsize = sizeof pathbuf; -	dirname = getenv("HKTOOL_DATADIR"); -	if (dirname == NULL || strlen(dirname) == 0) -		dirname = DATADIR; - -	dir = opendir(dirname); +	dir = opendir(datadir);  	if (dir == NULL) -		eprintf("Could not open directory '%s':", dirname); - -	if (name == NULL) -		printf("Searching '%s' for Device Parameter Files:\n", dirname); +		eprintf("Could not open directory '%s':", datadir); -	found = false;  	while (errno = 0, de = readdir(dir), de != NULL) { -		bool ismatch, doprint; +		int size; -		doprint = name == NULL; -		if (doprint) -			ismatch = strcmp(de->d_name, name) == 0; -		else -			ismatch = false; - -		if (doprint || ismatch) { -			int size; - -			size = snprintf(NULL, 0, "%s/%s", dirname, de->d_name); -			assert(size > 0); -			if ((size_t)size > pathsize) { -				if (fullpath == pathbuf) -					fullpath = NULL; -				fullpath = erealloc(fullpath, size); -				pathsize = size; -			} -			size = snprintf(fullpath, pathsize, "%s/%s", dirname, de->d_name); -			assert(size > 0); -			assert((size_t)size < pathsize); -		} - -		if (doprint) { -			printf("%s", de->d_name); -			printcname(fullpath); -			putchar('\n'); -			continue; -		}; - -		if (!ismatch) +		if (de->d_name[0] == '.')  			continue; +		size = snprintf(NULL, 0, "%s/%s", datadir, de->d_name); +		assert(size > 0); +		if ((size_t)size > pathsize) { +			if (fullpath == pathbuf) +				fullpath = NULL; +			fullpath = erealloc(fullpath, size); +			pathsize = size; +		} +		size = snprintf(fullpath, pathsize, "%s/%s", datadir, de->d_name); +		assert(size > 0); +		assert((size_t)size < pathsize); -		readparams(fp, fullpath); -		found = true; -		errno = 0; -		break; +		printf("%s", de->d_name); +		printcname(fullpath); +		putchar('\n');  	}  	if (errno) -		weprintf("Could not read directory '%s':", dirname); +		weprintf("Could not read directory '%s':", datadir);  	if (fullpath != pathbuf)  		free(fullpath);  	if (closedir(dir) == -1) -		eprintf("Could not close directory '%s':", dirname); - -	return found; +		eprintf("Could not close directory '%s':", datadir);  } @@ -20,6 +20,8 @@  #ifndef PARAMS_H  #define PARAMS_H +extern char *datadir; +  struct flashparams {  	size_t memsz;  	size_t blksz; @@ -27,6 +29,7 @@ struct flashparams {  	size_t addrshft;  }; -bool getparams(struct flashparams *fp, const char *name); +void readparams(struct flashparams *fp, const char *file); +void listparams(void);  #endif /* PARAMS_H */  | 
