diff lwar/main.c @ 172:47427342e41d

lwar now creates, lists, and adds to archive files
author lost
date Sun, 01 Mar 2009 22:59:52 +0000
parents d610b8aef91b
children cc41ccee8f64
line wrap: on
line diff
--- a/lwar/main.c	Sun Mar 01 19:37:03 2009 +0000
+++ b/lwar/main.c	Sun Mar 01 22:59:52 2009 +0000
@@ -30,6 +30,9 @@
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "lwar.h"
 
@@ -46,9 +49,35 @@
 		debug_level++;
 		break;
 	
-//	case ARGP_KEY_ARG:
-//
+	case 'a':
+		// add members
+		operation = LWAR_OP_ADD;
+		break;
+	
+	case 'c':
+		// create archive
+		operation = LWAR_OP_CREATE;
+		break;
+		
+//	case 'r':
+//		// remove members
+//		operation = LWAR_OP_REMOVE;
 //		break;
+	
+	case 'l':
+		// list members
+		operation = LWAR_OP_LIST;
+		break;
+	
+	case ARGP_KEY_ARG:
+		if (archive_file)
+		{
+			// add archive member to list
+			add_file_name(arg);
+		}
+		else
+			archive_file = arg;
+		break;
 		
 	default:
 		return ARGP_ERR_UNKNOWN;
@@ -58,6 +87,14 @@
 
 static struct argp_option options[] =
 {
+//	{ "remove",		'r',	0,		0,
+//				"Remove members from the archive" },
+	{ "add",		'a',	0,		0,
+				"Add members to the archive" },
+	{ "list",		'l',	0,		0,
+				"List the contents of the archive" },
+	{ "create",		'c',	0,		0,
+				"Create new archive (or truncate existing one)" },
 	{ "debug",		'd',	0,		0,
 				"Set debug mode"},
 	{ 0 }
@@ -67,15 +104,83 @@
 {
 	options,
 	parse_opts,
-	"<input file> ...",
-	"LWLINK, a HD6309 and MC6809 cross-linker"
+	"<archive> [<file> ...]",
+	"LWAR, a library file manager for LWLINK"
 };
 
+extern void do_list(void);
+extern void do_add(void);
+extern void do_remove(void);
+
 // main function; parse command line, set up assembler state, and run the
 // assembler on the first file
 int main(int argc, char **argv)
 {
 	argp_parse(&argp, argc, argv, 0, 0, NULL);
+	if (archive_file == NULL)
+	{
+		fprintf(stderr, "You must specify an archive file.\n");
+		exit(1);
+	}
+
+	if (operation == 0)
+	{
+		fprintf(stderr, "You must specify an operation.\n");
+		exit(1);
+	}
+
+	if (operation == LWAR_OP_LIST || operation == LWAR_OP_REMOVE)
+	{
+		struct stat stbuf;
+		// make sure the archive exists
+		if (stat(archive_file, &stbuf) < 0)
+		{
+			fprintf(stderr, "Cannot open archive file %s:\n", archive_file);
+			perror("");
+			exit(2);
+		}
+	}
+	if (operation == LWAR_OP_CREATE)
+	{
+		struct stat stbuf;
+		// check if the archive exists
+		if (stat(archive_file, &stbuf) < 0)
+		{
+			if (errno != ENOENT)
+			{
+				fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
+				perror("");
+				exit(2);
+			}
+		}
+		else
+		{
+			if (unlink(archive_file) < 0)
+			{
+				fprintf(stderr, "Cannot create archive file %s:\n", archive_file);
+				perror("");
+				exit(2);
+			}
+				
+		}
+	}
 	
+	switch (operation)
+	{
+	case LWAR_OP_LIST:
+		do_list();
+		break;
+	
+	case LWAR_OP_ADD:
+	case LWAR_OP_CREATE:
+		do_add();
+		break;
+	
+	case LWAR_OP_REMOVE:
+		do_remove();
+		break;
+	
+	}
+
 	exit(0);
 }