mux/src/muxcli.cpp File Reference

#include "copyright.h"
#include <string.h>
#include "muxcli.h"

Include dependency graph for muxcli.cpp:

Go to the source code of this file.

Functions

static int iArgType (char *pArg)
void CLI_Process (int argc, char *argv[], CLI_OptionEntry *aOptionTable, int nOptionTable, CLI_CALLBACKFUNC *pFunc)


Function Documentation

void CLI_Process ( int  argc,
char *  argv[],
CLI_OptionEntry aOptionTable,
int  nOptionTable,
CLI_CALLBACKFUNC pFunc 
)

Definition at line 52 of file muxcli.cpp.

References CLI_NONE, CLI_OPTIONAL, CLI_REQUIRED, and iArgType().

Referenced by iArgType(), and main().

00059 {
00060     int minNonOption = 0;
00061     int bEndOfOptions = 0;
00062     for (int i = 1; i < argc; i++)
00063     {
00064         char *pArgv = argv[i];
00065         int iType = 0;
00066         if (!bEndOfOptions)
00067         {
00068             iType = iArgType(pArgv);
00069         }
00070 
00071         if (iType == 0)
00072         {
00073             // Non-option argument.
00074             //
00075             if (minNonOption <= i)
00076             {
00077                 // We haven't associated it with an option, yet, so
00078                 // pass it in by itself.
00079                 //
00080                 pFunc(0, pArgv);
00081             }
00082             continue;
00083         }
00084 
00085         if (minNonOption < i+1)
00086         {
00087             minNonOption = i+1;
00088         }
00089 
00090         if (iType == 3)
00091         {
00092             // A "--" causes the remaining unpaired arguments to be
00093             // treated as non-option arguments.
00094             //
00095             bEndOfOptions = 1;
00096             continue;
00097         }
00098 
00099         char *p = pArgv+iType;
00100 
00101         if (iType == 2)
00102         {
00103             // We have a long option.
00104             //
00105             char *pEqual = strchr(p, '=');
00106             int nLen;
00107             if (pEqual)
00108             {
00109                 nLen = pEqual - p;
00110             }
00111             else
00112             {
00113                 nLen = strlen(p);
00114             }
00115             for (int j = 0; j < nOptionTable; j++)
00116             {
00117                 if (  !strncmp(aOptionTable[j].m_Flag, p, nLen)
00118                    && aOptionTable[j].m_Flag[nLen] == '\0')
00119                 {
00120                     switch (aOptionTable[j].m_ArgControl)
00121                     {
00122                     case CLI_NONE:
00123                         pFunc(aOptionTable + j, 0);
00124                         break;
00125 
00126                     case CLI_OPTIONAL:
00127                     case CLI_REQUIRED:
00128                         if (pEqual)
00129                         {
00130                             pFunc(aOptionTable + j, pEqual+1);
00131                             break;
00132                         }
00133                         int bFound = 0;
00134                         for (; minNonOption < argc; minNonOption++)
00135                         {
00136                             int iType2 = iArgType(argv[minNonOption]);
00137                             if (iType2 == 0)
00138                             {
00139                                 pFunc(aOptionTable + j, argv[minNonOption]);
00140                                 minNonOption++;
00141                                 bFound = 1;
00142                                 break;
00143                             }
00144                             else if (iType2 == 3)
00145                             {
00146                                 // End of options. Stop.
00147                                 //
00148                                 break;
00149                             }
00150                         }
00151                         if (  !bFound
00152                            && aOptionTable[j].m_ArgControl == CLI_OPTIONAL)
00153                         {
00154                             pFunc(aOptionTable + j, 0);
00155                         }
00156                         break;
00157                     }
00158                     break;
00159                 }
00160             }
00161             continue;
00162         }
00163 
00164         // At this point, the only possibilities left are a short
00165         // option.
00166         //
00167         while (*p)
00168         {
00169             int ch = *p++;
00170             for (int j = 0; j < nOptionTable; j++)
00171             {
00172                 if (  aOptionTable[j].m_Flag[0] == ch
00173                    && aOptionTable[j].m_Flag[1] == '\0')
00174                 {
00175                     switch (aOptionTable[j].m_ArgControl)
00176                     {
00177                     case CLI_NONE:
00178                         pFunc(aOptionTable + j, 0);
00179                         break;
00180 
00181                     case CLI_OPTIONAL:
00182                     case CLI_REQUIRED:
00183                         if (*p)
00184                         {
00185                             // Value follows option letter
00186                             //
00187                             if (*p == '=')
00188                             {
00189                                 p++;
00190                             }
00191 
00192                             pFunc(aOptionTable + j, p);
00193                             p = "";
00194                             break;
00195                         }
00196                         int bFound = 0;
00197                         for (; minNonOption < argc; minNonOption++)
00198                         {
00199                             int iType2 = iArgType(argv[minNonOption]);
00200                             if (iType2 == 0)
00201                             {
00202                                 pFunc(aOptionTable + j, argv[minNonOption]);
00203                                 minNonOption++;
00204                                 bFound = 1;
00205                                 break;
00206                             }
00207                             else if (iType2 == 3)
00208                             {
00209                                 // End of options. Stop.
00210                                 //
00211                                 break;
00212                             }
00213                         }
00214                         if (  !bFound
00215                            && aOptionTable[j].m_ArgControl == CLI_OPTIONAL)
00216                         {
00217                             pFunc(aOptionTable + j, 0);
00218                         }
00219                         break;
00220                     }
00221                     break;
00222                 }
00223             }
00224         }
00225     }
00226 }

static int iArgType ( char *  pArg  )  [static]

Definition at line 18 of file muxcli.cpp.

References CLI_Process().

Referenced by CLI_Process().

00019 {
00020     // How many characters from "--" does the argument match?
00021     //
00022     static char aHHN[3] = "--";
00023     int iType = 0;
00024     for (; iType < 3 && aHHN[iType] == pArg[iType]; iType++)
00025     {
00026         // Nothing
00027     }
00028     if (iType > 3)
00029     {
00030         iType = 3;
00031     }
00032 
00033     // "-" is a special case. It is a non-option argument.
00034     //
00035     if (iType == 1 && pArg[1] == '\0')
00036     {
00037         iType = 0;
00038     }
00039     return iType;
00040 }


Generated on Mon May 28 04:40:20 2007 for MUX by  doxygen 1.4.7