~ubuntu-branches/ubuntu/gutsy/ntp/gutsy

« back to all changes in this revision

Viewing changes to libopts/stack.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-05-18 22:41:56 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070518224156-563ruqxsxvqvoy8h
Tags: 1:4.2.4p0+dfsg-1ubuntu1
* Merge from Debian unstable.
* Remaining Ubuntu changes:
  - Update version in conflicts/replaces to that which was shipped in edgy,
    which was later than that in Debian (due to the ubuntuX).
  - Change default server to ntp.ubuntu.com.
  - Remove stop links from rc0 and rc6
  - Call dh_installinit with --error-handler
  - Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
3
 *  stack.c
 
4
 *  $Id: stack.c,v 4.10 2006/10/05 03:48:57 bkorb Exp $
 
5
 *  Time-stamp:      "2006-09-22 18:13:19 bkorb"
 
6
 *
 
7
 *  This is a special option processing routine that will save the
 
8
 *  argument to an option in a FIFO queue.
 
9
 */
 
10
 
 
11
/*
 
12
 *  Automated Options copyright 1992-2006 Bruce Korb
 
13
 *
 
14
 *  Automated Options is free software.
 
15
 *  You may redistribute it and/or modify it under the terms of the
 
16
 *  GNU General Public License, as published by the Free Software
 
17
 *  Foundation; either version 2, or (at your option) any later version.
 
18
 *
 
19
 *  Automated Options is distributed in the hope that it will be useful,
 
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
22
 *  GNU General Public License for more details.
 
23
 *
 
24
 *  You should have received a copy of the GNU General Public License
 
25
 *  along with Automated Options.  See the file "COPYING".  If not,
 
26
 *  write to:  The Free Software Foundation, Inc.,
 
27
 *             51 Franklin Street, Fifth Floor,
 
28
 *             Boston, MA  02110-1301, USA.
 
29
 *
 
30
 * As a special exception, Bruce Korb gives permission for additional
 
31
 * uses of the text contained in his release of AutoOpts.
 
32
 *
 
33
 * The exception is that, if you link the AutoOpts library with other
 
34
 * files to produce an executable, this does not by itself cause the
 
35
 * resulting executable to be covered by the GNU General Public License.
 
36
 * Your use of that executable is in no way restricted on account of
 
37
 * linking the AutoOpts library code into it.
 
38
 *
 
39
 * This exception does not however invalidate any other reasons why
 
40
 * the executable file might be covered by the GNU General Public License.
 
41
 *
 
42
 * This exception applies only to the code released by Bruce Korb under
 
43
 * the name AutoOpts.  If you copy code from other sources under the
 
44
 * General Public License into a copy of AutoOpts, as the General Public
 
45
 * License permits, the exception does not apply to the code that you add
 
46
 * in this way.  To avoid misleading anyone as to the status of such
 
47
 * modified files, you must delete this exception notice from them.
 
48
 *
 
49
 * If you write modifications of your own for AutoOpts, it is your choice
 
50
 * whether to permit this exception to apply to your modifications.
 
51
 * If you do not wish that, delete this exception notice.
 
52
 */
 
53
 
 
54
#ifdef WITH_LIBREGEX
 
55
#  include REGEX_HEADER
 
56
#endif
 
57
 
 
58
/*=export_func  optionUnstackArg
 
59
 * private:
 
60
 *
 
61
 * what:  Remove option args from a stack
 
62
 * arg:   + tOptions* + pOpts    + program options descriptor +
 
63
 * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
 
64
 *
 
65
 * doc:
 
66
 *  Invoked for options that are equivalenced to stacked options.
 
67
=*/
 
68
void
 
69
optionUnstackArg(
 
70
    tOptions*  pOpts,
 
71
    tOptDesc*  pOptDesc )
 
72
{
 
73
    int       res;
 
74
 
 
75
    tArgList* pAL = (tArgList*)pOptDesc->optCookie;
 
76
    /*
 
77
     *  IF we don't have any stacked options,
 
78
     *  THEN indicate that we don't have any of these options
 
79
     */
 
80
    if (pAL == NULL) {
 
81
        pOptDesc->fOptState &= OPTST_PERSISTENT_MASK;
 
82
        if ( (pOptDesc->fOptState & OPTST_INITENABLED) == 0)
 
83
            pOptDesc->fOptState |= OPTST_DISABLED;
 
84
        return;
 
85
    }
 
86
 
 
87
#ifdef WITH_LIBREGEX
 
88
    {
 
89
        regex_t   re;
 
90
        int       i, ct, dIdx;
 
91
 
 
92
        if (regcomp( &re, pOptDesc->optArg.argString, REG_NOSUB ) != 0)
 
93
            return;
 
94
 
 
95
        /*
 
96
         *  search the list for the entry(s) to remove.  Entries that
 
97
         *  are removed are *not* copied into the result.  The source
 
98
         *  index is incremented every time.  The destination only when
 
99
         *  we are keeping a define.
 
100
         */
 
101
        for (i = 0, dIdx = 0, ct = pAL->useCt; --ct >= 0; i++) {
 
102
            tCC*      pzSrc = pAL->apzArgs[ i ];
 
103
            char*     pzEq  = strchr( pzSrc, '=' );
 
104
 
 
105
            if (pzEq != NULL)
 
106
                *pzEq = NUL;
 
107
 
 
108
            res = regexec( &re, pzSrc, (size_t)0, NULL, 0 );
 
109
            switch (res) {
 
110
            case 0:
 
111
                /*
 
112
                 *  Remove this entry by reducing the in-use count
 
113
                 *  and *not* putting the string pointer back into
 
114
                 *  the list.
 
115
                 */
 
116
                pAL->useCt--;
 
117
                break;
 
118
 
 
119
            default:
 
120
            case REG_NOMATCH:
 
121
                if (pzEq != NULL)
 
122
                    *pzEq = '=';
 
123
 
 
124
                /*
 
125
                 *  IF we have dropped an entry
 
126
                 *  THEN we have to move the current one.
 
127
                 */
 
128
                if (dIdx != i)
 
129
                    pAL->apzArgs[ dIdx ] = pzSrc;
 
130
                dIdx++;
 
131
            }
 
132
        }
 
133
 
 
134
        regfree( &re );
 
135
    }
 
136
#else  /* not WITH_LIBREGEX */
 
137
    {
 
138
        int i, ct, dIdx;
 
139
 
 
140
        /*
 
141
         *  search the list for the entry(s) to remove.  Entries that
 
142
         *  are removed are *not* copied into the result.  The source
 
143
         *  index is incremented every time.  The destination only when
 
144
         *  we are keeping a define.
 
145
         */
 
146
        for (i = 0, dIdx = 0, ct = pAL->useCt; --ct >= 0; i++) {
 
147
            tCC*      pzSrc = pAL->apzArgs[ i ];
 
148
            char*     pzEq  = strchr( pzSrc, '=' );
 
149
 
 
150
            if (pzEq != NULL)
 
151
                *pzEq = NUL;
 
152
 
 
153
            if (strcmp( pzSrc, pOptDesc->optArg.argString ) == 0) {
 
154
                /*
 
155
                 *  Remove this entry by reducing the in-use count
 
156
                 *  and *not* putting the string pointer back into
 
157
                 *  the list.
 
158
                 */
 
159
                pAL->useCt--;
 
160
            } else {
 
161
                if (pzEq != NULL)
 
162
                    *pzEq = '=';
 
163
 
 
164
                /*
 
165
                 *  IF we have dropped an entry
 
166
                 *  THEN we have to move the current one.
 
167
                 */
 
168
                if (dIdx != i)
 
169
                    pAL->apzArgs[ dIdx ] = pzSrc;
 
170
                dIdx++;
 
171
            }
 
172
        }
 
173
    }
 
174
#endif /* WITH_LIBREGEX */
 
175
    /*
 
176
     *  IF we have unstacked everything,
 
177
     *  THEN indicate that we don't have any of these options
 
178
     */
 
179
    if (pAL->useCt == 0) {
 
180
        pOptDesc->fOptState &= OPTST_PERSISTENT_MASK;
 
181
        if ( (pOptDesc->fOptState & OPTST_INITENABLED) == 0)
 
182
            pOptDesc->fOptState |= OPTST_DISABLED;
 
183
        free( (void*)pAL );
 
184
        pOptDesc->optCookie = NULL;
 
185
    }
 
186
}
 
187
 
 
188
 
 
189
/*
 
190
 *  Put an entry into an argument list.  The first argument points to
 
191
 *  a pointer to the argument list structure.  It gets passed around
 
192
 *  as an opaque address.
 
193
 */
 
194
LOCAL void
 
195
addArgListEntry( void** ppAL, void* entry )
 
196
{
 
197
    tArgList* pAL = *(void**)ppAL;
 
198
 
 
199
    /*
 
200
     *  IF we have never allocated one of these,
 
201
     *  THEN allocate one now
 
202
     */
 
203
    if (pAL == NULL) {
 
204
        pAL = (tArgList*)AGALOC( sizeof( *pAL ), "new option arg stack" );
 
205
        if (pAL == NULL)
 
206
            return;
 
207
        pAL->useCt   = 0;
 
208
        pAL->allocCt = MIN_ARG_ALLOC_CT;
 
209
        *ppAL = (void*)pAL;
 
210
    }
 
211
 
 
212
    /*
 
213
     *  ELSE if we are out of room
 
214
     *  THEN make it bigger
 
215
     */
 
216
    else if (pAL->useCt >= pAL->allocCt) {
 
217
        size_t sz = sizeof( *pAL );
 
218
        pAL->allocCt += INCR_ARG_ALLOC_CT;
 
219
 
 
220
        /*
 
221
         *  The base structure contains space for MIN_ARG_ALLOC_CT
 
222
         *  pointers.  We subtract it off to find our augment size.
 
223
         */
 
224
        sz += sizeof(char*) * (pAL->allocCt - MIN_ARG_ALLOC_CT);
 
225
        pAL = (tArgList*)AGREALOC( (void*)pAL, sz, "expanded opt arg stack" );
 
226
        if (pAL == NULL)
 
227
            return;
 
228
        *ppAL = (void*)pAL;
 
229
    }
 
230
 
 
231
    /*
 
232
     *  Insert the new argument into the list
 
233
     */
 
234
    pAL->apzArgs[ (pAL->useCt)++ ] = entry;
 
235
}
 
236
 
 
237
 
 
238
/*=export_func  optionStackArg
 
239
 * private:
 
240
 *
 
241
 * what:  put option args on a stack
 
242
 * arg:   + tOptions* + pOpts    + program options descriptor +
 
243
 * arg:   + tOptDesc* + pOptDesc + the descriptor for this arg +
 
244
 *
 
245
 * doc:
 
246
 *  Keep an entry-ordered list of option arguments.
 
247
=*/
 
248
void
 
249
optionStackArg(
 
250
    tOptions*  pOpts,
 
251
    tOptDesc*  pOD )
 
252
{
 
253
    if (pOD->optArg.argString == NULL)
 
254
        return;
 
255
 
 
256
    addArgListEntry( &(pOD->optCookie), (void*)pOD->optArg.argString );
 
257
}
 
258
/*
 
259
 * Local Variables:
 
260
 * mode: C
 
261
 * c-file-style: "stroustrup"
 
262
 * indent-tabs-mode: nil
 
263
 * End:
 
264
 * end of autoopts/stack.c */