File: muxcli.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (225 lines) | stat: -rw-r--r-- 6,421 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*! \file muxcli.cpp
 * \brief MUX command-line option parser.
 *
 * $Id: muxcli.cpp 2664 2007-10-06 16:36:54Z brazilofmux $
 *
 */

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

// 0 -- A non-option argument.
// 1 -- A short-option argument.
// 2 -- A long-option argument.
// 3 -- An 'end of options' indicator.
//
static int iArgType(char *pArg)
{
    // How many characters from "--" does the argument match?
    //
    static char aHHN[3] = "--";
    int iType = 0;
    for (; iType < 3 && aHHN[iType] == pArg[iType]; iType++)
    {
        // Nothing
    }
    if (iType > 3)
    {
        iType = 3;
    }

    // "-" is a special case. It is a non-option argument.
    //
    if (iType == 1 && pArg[1] == '\0')
    {
        iType = 0;
    }
    return iType;
}

// Examples:
//
// 1. prog -c123   --> (c,123)
// 2. prog -c 123  --> (c,123)
// 3. prog -c=123  --> (c,123)
// 4. prog -cs 123 --> (c,123) (s)
// 5. prog -sc=123 --> (s) (c,123)
// 6. prog -cs123 --> (c,s123)
//
void CLI_Process
(
    int argc,
    char *argv[],
    CLI_OptionEntry *aOptionTable,
    int nOptionTable,
    CLI_CALLBACKFUNC *pFunc
)
{
    int minNonOption = 0;
    int bEndOfOptions = 0;
    for (int i = 1; i < argc; i++)
    {
        char *pArgv = argv[i];
        int iType = 0;
        if (!bEndOfOptions)
        {
            iType = iArgType(pArgv);
        }

        if (iType == 0)
        {
            // Non-option argument.
            //
            if (minNonOption <= i)
            {
                // We haven't associated it with an option, yet, so
                // pass it in by itself.
                //
                pFunc(0, pArgv);
            }
            continue;
        }

        if (minNonOption < i+1)
        {
            minNonOption = i+1;
        }

        if (iType == 3)
        {
            // A "--" causes the remaining unpaired arguments to be
            // treated as non-option arguments.
            //
            bEndOfOptions = 1;
            continue;
        }

        const char *p = pArgv+iType;

        if (iType == 2)
        {
            // We have a long option.
            //
            const char *pEqual = strchr(p, '=');
            size_t nLen;
            if (pEqual)
            {
                nLen = pEqual - p;
            }
            else
            {
                nLen = strlen(p);
            }
            for (int j = 0; j < nOptionTable; j++)
            {
                if (  !strncmp(aOptionTable[j].m_Flag, p, nLen)
                   && aOptionTable[j].m_Flag[nLen] == '\0')
                {
                    switch (aOptionTable[j].m_ArgControl)
                    {
                    case CLI_NONE:
                        pFunc(aOptionTable + j, 0);
                        break;

                    case CLI_OPTIONAL:
                    case CLI_REQUIRED:
                        if (pEqual)
                        {
                            pFunc(aOptionTable + j, pEqual+1);
                            break;
                        }
                        int bFound = 0;
                        for (; minNonOption < argc; minNonOption++)
                        {
                            int iType2 = iArgType(argv[minNonOption]);
                            if (iType2 == 0)
                            {
                                pFunc(aOptionTable + j, argv[minNonOption]);
                                minNonOption++;
                                bFound = 1;
                                break;
                            }
                            else if (iType2 == 3)
                            {
                                // End of options. Stop.
                                //
                                break;
                            }
                        }
                        if (  !bFound
                           && aOptionTable[j].m_ArgControl == CLI_OPTIONAL)
                        {
                            pFunc(aOptionTable + j, 0);
                        }
                        break;
                    }
                    break;
                }
            }
            continue;
        }

        // At this point, the only possibilities left are a short
        // option.
        //
        while (*p)
        {
            int ch = *p++;
            for (int j = 0; j < nOptionTable; j++)
            {
                if (  aOptionTable[j].m_Flag[0] == ch
                   && aOptionTable[j].m_Flag[1] == '\0')
                {
                    switch (aOptionTable[j].m_ArgControl)
                    {
                    case CLI_NONE:
                        pFunc(aOptionTable + j, 0);
                        break;

                    case CLI_OPTIONAL:
                    case CLI_REQUIRED:
                        if (*p)
                        {
                            // Value follows option letter
                            //
                            if (*p == '=')
                            {
                                p++;
                            }

                            pFunc(aOptionTable + j, p);
                            p = "";
                            break;
                        }
                        int bFound = 0;
                        for (; minNonOption < argc; minNonOption++)
                        {
                            int iType2 = iArgType(argv[minNonOption]);
                            if (iType2 == 0)
                            {
                                pFunc(aOptionTable + j, argv[minNonOption]);
                                minNonOption++;
                                bFound = 1;
                                break;
                            }
                            else if (iType2 == 3)
                            {
                                // End of options. Stop.
                                //
                                break;
                            }
                        }
                        if (  !bFound
                           && aOptionTable[j].m_ArgControl == CLI_OPTIONAL)
                        {
                            pFunc(aOptionTable + j, 0);
                        }
                        break;
                    }
                    break;
                }
            }
        }
    }
}