]> git.sur5r.net Git - cc65/blobdiff - src/common/cmdline.c
Fix macro output in the listing
[cc65] / src / common / cmdline.c
index 057afaa6760c1d32c16602ad07466fc24bd07890..3403cdd7cb2980edebe961b40e187913891fce7f 100644 (file)
@@ -33,9 +33,9 @@
 
 
 
-#include <stdio.h>
-#include <stdlib.h>
+#include <string.h>
 
+#include "abend.h"
 #include "cmdline.h"
 
 
 
 
 
+/* Program name - is set after call to InitCmdLine */
+const char* ProgName;
+
+/* The program argument vector */
 static char** ArgVec     = 0;
 static unsigned ArgCount = 0;
 
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                          Code                                    */
 /*****************************************************************************/
 
 
 
-void InitCmdLine (char* aArgVec[], unsigned aArgCount)
+void InitCmdLine (unsigned aArgCount, char* aArgVec[], const char* aProgName)
 /* Initialize command line parsing. aArgVec is the argument array terminated by
  * a NULL pointer (as usual), ArgCount is the number of valid arguments in the
  * array. Both arguments are remembered in static storage.
  */
 {
-    ArgVec   = aArgVec;
+    /* Remember the argument vector */
     ArgCount = aArgCount;
+    ArgVec   = aArgVec;
+
+    /* Get the program name from argv[0] but strip a path */
+    if (ArgVec[0] == 0) {
+       /* Use the default name given */
+       ProgName = aProgName;
+    } else {
+       /* Strip a path */
+       ProgName = strchr (ArgVec[0], '\0');
+       while (ProgName > ArgVec[0]) {
+           --ProgName;
+                   if (*ProgName == '/' || *ProgName == '\\') {
+               ++ProgName;
+               break;
+           }
+       }
+       if (ProgName[0] == '\0') {
+           /* Use the default */
+           ProgName = aProgName;
+       }
+    }
 }
 
 
@@ -72,8 +97,7 @@ void InitCmdLine (char* aArgVec[], unsigned aArgCount)
 void UnknownOption (const char* Opt)
 /* Print an error about an unknown option. */
 {
-    fprintf (stderr, "Unknown option: %s\n", Opt);
-    exit (EXIT_FAILURE);
+    AbEnd ("Unknown option: %s\n", Opt);
 }
 
 
@@ -81,17 +105,15 @@ void UnknownOption (const char* Opt)
 void NeedArg (const char* Opt)
 /* Print an error about a missing option argument and exit. */
 {
-    fprintf (stderr, "Option requires an argument: %s\n", Opt);
-    exit (EXIT_FAILURE);
+    AbEnd ("Option requires an argument: %s\n", Opt);
 }
 
 
 
-void InvSym (const char* Def)
-/* Print an error about an invalid symbol definition and die */
+void InvDef (const char* Def)
+/* Print an error about an invalid definition and die */
 {
-    fprintf (stderr, "Invalid symbol definition: `%s'\n", Def);
-    exit (EXIT_FAILURE);
+    AbEnd ("Invalid definition: `%s'\n", Def);
 }
 
 
@@ -130,10 +152,12 @@ void LongOption (int* ArgNum, const LongOpt* OptTab, unsigned OptCount)
        if (strcmp (Opt, OptTab->Option) == 0) {
            /* Found, call the function */
            if (OptTab->ArgCount > 0) {
-               OptTab->Func (Opt, ArgVec[++(*ArgNum)]);
+               OptTab->Func (Opt, ArgVec[++(*ArgNum)]);
            } else {
-               OptTab->Func (Opt, 0);
+               OptTab->Func (Opt, 0);
            }
+           /* Done */
+           return;
        }
 
        /* Next table entry */