diff th_args.c @ 377:9ad157feb99a

Sync th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Oct 2011 00:04:33 +0300
parents 69aed051f84d
children afbc3bfd3e03
line wrap: on
line diff
--- a/th_args.c	Tue Jun 28 21:46:18 2011 +0300
+++ b/th_args.c	Mon Oct 03 00:04:33 2011 +0300
@@ -128,20 +128,26 @@
  */
 static BOOL th_args_check_arg(optarg_t *o, char *optArg)
 {
-    if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) {
-        if (o->optShort != 0 && o->optLong != NULL) {
+    if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL)
+    {
+        if (o->optShort != 0 && o->optLong != NULL)
+        {
             THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n",
-                o->optShort, o->optLong);
-        } else if (o->optShort != 0) {
+                  o->optShort, o->optLong);
+        }
+        else if (o->optShort != 0)
+        {
             THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
-        } else if (o->optLong != NULL) {
+        }
+        else if (o->optLong != NULL)
+        {
             THERR("Option --%s=ARG requires an argument!\n", o->optLong);
         }
-        
+
         return FALSE;
-    } else
+    }
+    else
         return TRUE;
-
 }
 
 
@@ -155,39 +161,43 @@
     char *tmpArg = currArg, *optArg;
     int optN;
     BOOL isFound;
-    
+
     /* Short options can be combined: -a -b -c == -abc */
-    while (*tmpArg) {
-        
+    while (*tmpArg)
+    {
         for (optN = 0, isFound = FALSE; (optN < optListN) && !isFound; optN++)
-        if (*tmpArg == optList[optN].optShort) {
-            /* Get possible option argument, if needed */
-            if ((optList[optN].optFlags & OPT_ARGMASK) != 0 && (++(*newArgIndex) < argc))
-                optArg = argv[*newArgIndex];
-            else
-                optArg = NULL;
-            
-            /* Check if option argument is required */
-            if (!th_args_check_arg(&optList[optN], optArg))
-                return FALSE;            
-            else {
-                char tmpStr[2] = { 0, 0 };
+            if (*tmpArg == optList[optN].optShort)
+            {
+                /* Get possible option argument, if needed */
+                if ((optList[optN].optFlags & OPT_ARGMASK) != 0
+                    && (++(*newArgIndex) < argc))
+                    optArg = argv[*newArgIndex];
+                else
+                    optArg = NULL;
+
+                /* Check if option argument is required */
+                if (!th_args_check_arg(&optList[optN], optArg))
+                    return FALSE;
+                else
+                {
+                    char tmpStr[2] = { 0, 0 };
 
-                /* Option was given succesfully, try to handle it */
-                wasGiven[optN] = TRUE;
-                
-                tmpStr[0] = *tmpArg;
-                
-                if (!handleOpt(optList[optN].optID, optArg, tmpStr))
-                    return FALSE;
+                    /* Option was given succesfully, try to handle it */
+                    wasGiven[optN] = TRUE;
+
+                    tmpStr[0] = *tmpArg;
+
+                    if (!handleOpt(optList[optN].optID, optArg, tmpStr))
+                        return FALSE;
+                }
+
+                isFound = TRUE;
             }
-                            
-            isFound = TRUE;
-        }
-        
-        if (!isFound) {
+
+        if (!isFound)
+        {
             THERR("Unknown short option '%c' in argument '-%s'\n",
-                *tmpArg, currArg);
+                  *tmpArg, currArg);
             return FALSE;
         }
 
@@ -207,37 +217,46 @@
 {
     int optN, optLen, i;
     char *optArg;
-    
-    (void) argc; (void) argv; (void) newArgIndex;
-    
+
+    (void) argc;
+    (void) argv;
+    (void) newArgIndex;
+
     /* Long option */
     for (optN = -1, optLen = i = 0; (i < optListN) && (optN < 0); i++)
-    if (optList[i].optLong) {
-        optLen = strlen(optList[i].optLong);
-        if (strncmp(currArg, optList[i].optLong, optLen) == 0)
-            optN = i;
-    }
-    
+        if (optList[i].optLong)
+        {
+            optLen = strlen(optList[i].optLong);
+            if (strncmp(currArg, optList[i].optLong, optLen) == 0)
+                optN = i;
+        }
+
     /* Get possible option argument, if needed */
-    if (optN >= 0) {
-        if ((optList[optN].optFlags & OPT_ARGMASK) != 0) {
+    if (optN >= 0)
+    {
+        if ((optList[optN].optFlags & OPT_ARGMASK) != 0)
+        {
             if (currArg[optLen] == '=')
                 optArg = &currArg[optLen + 1];
             else
                 optArg = NULL;
-        } else
+        }
+        else
             optArg = NULL;
 
         /* Check if option argument is required */
         if (!th_args_check_arg(&optList[optN], optArg))
-            return FALSE;            
-        else {
+            return FALSE;
+        else
+        {
             /* Option was given succesfully, try to handle it */
             wasGiven[optN] = TRUE;
             if (!handleOpt(optList[optN].optID, optArg, currArg))
                 return FALSE;
         }
-    } else {
+    }
+    else
+    {
         THERR("Unknown long option '--%s'\n", currArg);
         return FALSE;
     }
@@ -250,9 +269,9 @@
  * calling the given callback functions.
  */
 BOOL th_args_process(int argc, char *argv[],
-             optarg_t optList[], int optListN,
-             BOOL (*handleOpt) (int, char *, char *),
-             BOOL (*handleNonOption) (char *), BOOL bailOut)
+                     optarg_t optList[], int optListN,
+                     BOOL(*handleOpt) (int, char *, char *),
+                     BOOL(*handleNonOption) (char *), BOOL bailOut)
 {
     BOOL endOptions, optionsOK;
     int argIndex, newArgIndex, i;
@@ -261,8 +280,10 @@
 
     /* Allocate wasGiven */
     wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
-    if (!wasGiven) {
-        THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
+    if (!wasGiven)
+    {
+        THERR
+            ("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
         exit(128);
     }
 
@@ -270,60 +291,73 @@
     argIndex = 1;
     optionsOK = TRUE;
     endOptions = FALSE;
-    while (argIndex < argc) {
+    while (argIndex < argc)
+    {
         currArg = argv[argIndex];
-        if ((currArg[0] == '-') && !endOptions) {
+        if ((currArg[0] == '-') && !endOptions)
+        {
             newArgIndex = argIndex;
             currArg++;
-            if (*currArg == '-') {
+            if (*currArg == '-')
+            {
                 /* Check for "--", which ends the options-list */
                 currArg++;
-                if (*currArg == 0) {
+                if (*currArg == 0)
+                {
                     endOptions = TRUE;
                     continue;
                 }
-                
+
                 /* Long options */
                 if (!th_args_process_long(currArg, &newArgIndex,
-                    wasGiven, argc, argv, optList, optListN,
-                    handleOpt))
+                                          wasGiven, argc, argv, optList,
+                                          optListN, handleOpt))
                     optionsOK = FALSE;
-            } else {
+            }
+            else
+            {
                 /* Short options */
                 if (!th_args_process_short(currArg, &newArgIndex,
-                    wasGiven, argc, argv, optList, optListN,
-                    handleOpt))
+                                           wasGiven, argc, argv, optList,
+                                           optListN, handleOpt))
                     optionsOK = FALSE;
             }
 
             argIndex = newArgIndex;
-        } else {
+        }
+        else
+        {
             /* Was not option argument */
-            if (handleNonOption == NULL || (handleNonOption != NULL && !handleNonOption(currArg))) {
+            if (handleNonOption == NULL
+                || (handleNonOption != NULL && !handleNonOption(currArg)))
+            {
                 THERR("Invalid argument '%s'\n", currArg);
                 optionsOK = FALSE;
             }
         }
-        
+
         /* Check if we bail out on invalid argument */
-        if (!optionsOK && bailOut) {
+        if (!optionsOK && bailOut)
+        {
             th_free(wasGiven);
             return FALSE;
         }
-        
+
         argIndex++;
     }
 
     /* Check wasGiven by isRequired */
     for (i = 0; i < optListN; i++)
-    if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i]) {
-        THERR("Option -%s (--%s) is required.\n",
-            optList[i].optShort, optList[i].optLong);
+        if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i])
+        {
+            THERR("Option -%s (--%s) is required.\n",
+                  optList[i].optShort, optList[i].optLong);
 
-        optionsOK = FALSE;
-        if (bailOut) break;
-    }
-    
+            optionsOK = FALSE;
+            if (bailOut)
+                break;
+        }
+
     th_free(wasGiven);
     return optionsOK;
 }
@@ -332,56 +366,66 @@
 /* Print help for commandline arguments/options
  */
 void th_args_help(FILE * outFile,
-    optarg_t optList[], int optListN,
-    char * progName, char * progUsage)
+                  optarg_t optList[], int optListN,
+                  char *progName, char *progUsage)
 {
     int i, nrequired;
 
     fprintf(outFile,
-        "\n%s v%s (%s)\n"
-        "%s\n"
-        "%s\n"
-        "Usage: %s %s\n",
-        th_prog_name, th_prog_version, th_prog_fullname,
-        th_prog_author, th_prog_license, progName, progUsage);
+            "\n%s v%s (%s)\n"
+            "%s\n"
+            "%s\n"
+            "Usage: %s %s\n",
+            th_prog_name, th_prog_version, th_prog_fullname,
+            th_prog_author, th_prog_license, progName, progUsage);
 
 
-    for (i = nrequired = 0; i < optListN; i++) {
+    for (i = nrequired = 0; i < optListN; i++)
+    {
         optarg_t *o = &optList[i];
-        
+
         /* Print short option */
         if (o->optShort != 0)
             fprintf(outFile, "  -%c,  ", o->optShort);
         else
             fprintf(outFile, "       ");
-        
+
         /* Print long option */
-        if (o->optLong) {
+        if (o->optLong)
+        {
             char tmpStr[64], *p;
-            
-            if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT) {
-                snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", optList[i].optLong);
+
+            if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT)
+            {
+                snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]",
+                         optList[i].optLong);
                 p = tmpStr;
-            } else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ) {
-                snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", optList[i].optLong);
+            }
+            else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ)
+            {
+                snprintf(tmpStr, sizeof(tmpStr), "%s=ARG",
+                         optList[i].optLong);
                 p = tmpStr;
-            } else
+            }
+            else
                 p = o->optLong;
-            
+
             fprintf(outFile, "--%-15s", p);
-        } else
+        }
+        else
             fprintf(outFile, "                 ");
 
         fprintf(outFile, "  %s.", optList[i].optDesc);
-        
-        if (o->optFlags & OPT_REQUIRED) {
+
+        if (o->optFlags & OPT_REQUIRED)
+        {
             fprintf(outFile, " [*]\n");
             nrequired++;
-        } else
+        }
+        else
             fprintf(outFile, "\n");
     }
-    
+
     if (nrequired > 0)
         fprintf(outFile, "(Options marked with [*] are required)\n");
 }
-