diff th_args.c @ 81:69aed051f84d

Synced th-libs.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 20 Apr 2009 22:02:37 +0300
parents 323c98360d8b
children 9ad157feb99a
line wrap: on
line diff
--- a/th_args.c	Fri Jan 30 01:54:51 2009 +0200
+++ b/th_args.c	Mon Apr 20 22:02:37 2009 +0300
@@ -128,19 +128,19 @@
  */
 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) {
-			THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n",
-				o->optShort, o->optLong);
-		} else if (o->optShort != 0) {
-			THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
-		} else if (o->optLong != NULL) {
-			THERR("Option --%s=ARG requires an argument!\n", o->optLong);
-		}
-		
-		return FALSE;
-	} else
-		return TRUE;
+    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) {
+            THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
+        } else if (o->optLong != NULL) {
+            THERR("Option --%s=ARG requires an argument!\n", o->optLong);
+        }
+        
+        return FALSE;
+    } else
+        return TRUE;
 
 }
 
@@ -148,101 +148,101 @@
 /* Handle short options
  */
 static BOOL th_args_process_short(char *currArg, int *newArgIndex,
-	BOOL *wasGiven, int argc, char *argv[],
-	optarg_t optList[], int optListN,
-	BOOL (*handleOpt)(int, char *, char *))
+    BOOL *wasGiven, int argc, char *argv[],
+    optarg_t optList[], int optListN,
+    BOOL (*handleOpt)(int, char *, char *))
 {
-	char *tmpArg = currArg, *optArg;
-	int optN;
-	BOOL isFound;
-	
-	/* Short options can be combined: -a -b -c == -abc */
-	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 };
+    char *tmpArg = currArg, *optArg;
+    int optN;
+    BOOL isFound;
+    
+    /* Short options can be combined: -a -b -c == -abc */
+    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 };
 
-				/* 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;
-		}
-		
-		if (!isFound) {
-			THERR("Unknown short option '%c' in argument '-%s'\n",
-				*tmpArg, currArg);
-			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;
+        }
+        
+        if (!isFound) {
+            THERR("Unknown short option '%c' in argument '-%s'\n",
+                *tmpArg, currArg);
+            return FALSE;
+        }
 
-		tmpArg++;
-	}
+        tmpArg++;
+    }
 
-	return TRUE;
+    return TRUE;
 }
 
 
 /* Handle long options
  */
 static BOOL th_args_process_long(char *currArg, int *newArgIndex,
-	BOOL *wasGiven, int argc, char *argv[],
-	optarg_t optList[], int optListN,
-	BOOL (*handleOpt)(int, char *, char *))
+    BOOL *wasGiven, int argc, char *argv[],
+    optarg_t optList[], int optListN,
+    BOOL (*handleOpt)(int, char *, char *))
 {
-	int optN, optLen, i;
-	char *optArg;
-	
-	(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;
-	}
-	
-	/* Get possible option argument, if needed */
-	if (optN >= 0) {
-		if ((optList[optN].optFlags & OPT_ARGMASK) != 0) {
-			if (currArg[optLen] == '=')
-				optArg = &currArg[optLen + 1];
-			else
-				optArg = NULL;
-		} else
-			optArg = NULL;
+    int optN, optLen, i;
+    char *optArg;
+    
+    (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;
+    }
+    
+    /* Get possible option argument, if needed */
+    if (optN >= 0) {
+        if ((optList[optN].optFlags & OPT_ARGMASK) != 0) {
+            if (currArg[optLen] == '=')
+                optArg = &currArg[optLen + 1];
+            else
+                optArg = NULL;
+        } else
+            optArg = NULL;
 
-		/* Check if option argument is required */
-		if (!th_args_check_arg(&optList[optN], optArg))
-			return FALSE;			
-		else {
-			/* Option was given succesfully, try to handle it */
-			wasGiven[optN] = TRUE;
-			if (!handleOpt(optList[optN].optID, optArg, currArg))
-				return FALSE;
-		}
-	} else {
-		THERR("Unknown long option '--%s'\n", currArg);
-		return FALSE;
-	}
+        /* Check if option argument is required */
+        if (!th_args_check_arg(&optList[optN], optArg))
+            return FALSE;            
+        else {
+            /* Option was given succesfully, try to handle it */
+            wasGiven[optN] = TRUE;
+            if (!handleOpt(optList[optN].optID, optArg, currArg))
+                return FALSE;
+        }
+    } else {
+        THERR("Unknown long option '--%s'\n", currArg);
+        return FALSE;
+    }
 
-	return TRUE;
+    return TRUE;
 }
 
 
@@ -250,138 +250,138 @@
  * 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;
-	char *currArg;
-	BOOL *wasGiven;
+    BOOL endOptions, optionsOK;
+    int argIndex, newArgIndex, i;
+    char *currArg;
+    BOOL *wasGiven;
 
-	/* Allocate wasGiven */
-	wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
-	if (!wasGiven) {
-		THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
-		exit(128);
-	}
+    /* Allocate wasGiven */
+    wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
+    if (!wasGiven) {
+        THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
+        exit(128);
+    }
 
-	/* Parse arguments */
-	argIndex = 1;
-	optionsOK = TRUE;
-	endOptions = FALSE;
-	while (argIndex < argc) {
-		currArg = argv[argIndex];
-		if ((currArg[0] == '-') && !endOptions) {
-			newArgIndex = argIndex;
-			currArg++;
-			if (*currArg == '-') {
-				/* Check for "--", which ends the options-list */
-				currArg++;
-				if (*currArg == 0) {
-					endOptions = TRUE;
-					continue;
-				}
-				
-				/* Long options */
-				if (!th_args_process_long(currArg, &newArgIndex,
-					wasGiven, argc, argv, optList, optListN,
-					handleOpt))
-					optionsOK = FALSE;
-			} else {
-				/* Short options */
-				if (!th_args_process_short(currArg, &newArgIndex,
-					wasGiven, argc, argv, optList, optListN,
-					handleOpt))
-					optionsOK = FALSE;
-			}
+    /* Parse arguments */
+    argIndex = 1;
+    optionsOK = TRUE;
+    endOptions = FALSE;
+    while (argIndex < argc) {
+        currArg = argv[argIndex];
+        if ((currArg[0] == '-') && !endOptions) {
+            newArgIndex = argIndex;
+            currArg++;
+            if (*currArg == '-') {
+                /* Check for "--", which ends the options-list */
+                currArg++;
+                if (*currArg == 0) {
+                    endOptions = TRUE;
+                    continue;
+                }
+                
+                /* Long options */
+                if (!th_args_process_long(currArg, &newArgIndex,
+                    wasGiven, argc, argv, optList, optListN,
+                    handleOpt))
+                    optionsOK = FALSE;
+            } else {
+                /* Short options */
+                if (!th_args_process_short(currArg, &newArgIndex,
+                    wasGiven, argc, argv, optList, optListN,
+                    handleOpt))
+                    optionsOK = FALSE;
+            }
 
-			argIndex = newArgIndex;
-		} else {
-			/* Was not option argument */
-			if (!handleNonOption || (handleNonOption && !handleNonOption(currArg))) {
-				THERR("Invalid argument '%s'\n", currArg);
-				optionsOK = FALSE;
-			}
-		}
-		
-		/* Check if we bail out on invalid argument */
-		if (!optionsOK && bailOut) {
-			th_free(wasGiven);
-			return FALSE;
-		}
-		
-		argIndex++;
-	}
+            argIndex = newArgIndex;
+        } else {
+            /* Was not option argument */
+            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) {
+            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);
+    /* 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);
 
-		optionsOK = FALSE;
-		if (bailOut) break;
-	}
-	
-	th_free(wasGiven);
-	return optionsOK;
+        optionsOK = FALSE;
+        if (bailOut) break;
+    }
+    
+    th_free(wasGiven);
+    return optionsOK;
 }
 
 
 /* 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;
+    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);
+    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);
 
 
-	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) {
-			char tmpStr[64], *p;
-			
-			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);
-				p = tmpStr;
-			} else
-				p = o->optLong;
-			
-			fprintf(outFile, "--%-15s", p);
-		} else
-			fprintf(outFile, "                 ");
+    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) {
+            char tmpStr[64], *p;
+            
+            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);
+                p = tmpStr;
+            } else
+                p = o->optLong;
+            
+            fprintf(outFile, "--%-15s", p);
+        } else
+            fprintf(outFile, "                 ");
 
-		fprintf(outFile, "  %s.", optList[i].optDesc);
-		
-		if (o->optFlags & OPT_REQUIRED) {
-			fprintf(outFile, " [*]\n");
-			nrequired++;
-		} else
-			fprintf(outFile, "\n");
-	}
-	
-	if (nrequired > 0)
-		fprintf(outFile, "(Options marked with [*] are required)\n");
+        fprintf(outFile, "  %s.", optList[i].optDesc);
+        
+        if (o->optFlags & OPT_REQUIRED) {
+            fprintf(outFile, " [*]\n");
+            nrequired++;
+        } else
+            fprintf(outFile, "\n");
+    }
+    
+    if (nrequired > 0)
+        fprintf(outFile, "(Options marked with [*] are required)\n");
 }