shithub: rgbds

ref: aea1990de311307ed0a05a68b5ba37c5d07c0b8e
dir: /src/lib/main.c/

View raw version
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>

#include "asmotor.h"

#include "lib/types.h"
#include "lib/library.h"

/*
 * Print the usagescreen
 *
 */

static void 
usage(void)
{
	printf("RGBLib v" LIB_VERSION " (part of ASMotor " ASMOTOR_VERSION ")\n\n");
	printf("usage: rgblib file [add | delete | extract | list] [module ...]\n");
	exit(EX_USAGE);
}
/*
 * The main routine
 *
 */

int 
main(int argc, char *argv[])
{
	SLONG argn = 0;
	char *libname;

	argc -= 1;
	argn += 1;

	if (argc >= 2) {
		sLibrary *lib;

		lib = lib_Read(libname = argv[argn++]);
		argc -= 1;

		if (strcmp(argv[argn], "add") == 0) {
			argn += 1;
			argc -= 1;

			while (argc) {
				lib = lib_AddReplace(lib, argv[argn++]);
				argc -= 1;
			}
			lib_Write(lib, libname);
			lib_Free(lib);
		} else if (strcmp(argv[argn], "delete") == 0) {
			argn += 1;
			argc -= 1;

			while (argc) {
				lib =
				    lib_DeleteModule(lib, argv[argn++]);
				argc -= 1;
			}
			lib_Write(lib, libname);
			lib_Free(lib);
		} else if (strcmp(argv[argn], "extract") == 0) {
			argn += 1;
			argc -= 1;

			while (argc) {
				sLibrary *l;

				l = lib_Find(lib, argv[argn]);
				if (l) {
					FILE *f;

					if ((f = fopen(argv[argn], "wb"))) {
						fwrite(l->pData,
						    sizeof(UBYTE),
						    l->nByteLength,
						    f);
						fclose(f);
						printf
						    ("Extracted module '%s'\n",
						    argv[argn]);
					} else
						errx(EX_NOINPUT,
						    "Unable to write module '%s' : %s", argv[argn], strerror(errno));
				} else
					errx(EX_NOINPUT, "Module not found");

				argn += 1;
				argc -= 1;
			}
			lib_Free(lib);
		} else if (strcmp(argv[argn], "list") == 0) {
			argn += 1;
			argc -= 1;

			sLibrary *l;

			l = lib;

			while (l) {
				printf("%10ld %s\n",
				    l->nByteLength,
				    l->tName);
				l = l->pNext;
			}
		} else
			usage();
	} else
		usage();

	return (0);
}