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

« back to all changes in this revision

Viewing changes to libopts/environment.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
 *  $Id: environment.c,v 4.9 2006/09/24 02:11:16 bkorb Exp $
 
4
 * Time-stamp:      "2005-10-29 13:23:59 bkorb"
 
5
 *
 
6
 *  This file contains all of the routines that must be linked into
 
7
 *  an executable to use the generated option processing.  The optional
 
8
 *  routines are in separately compiled modules so that they will not
 
9
 *  necessarily be linked in.
 
10
 */
 
11
 
 
12
/*
 
13
 *  Automated Options copyright 1992-2006 Bruce Korb
 
14
 *
 
15
 *  Automated Options is free software.
 
16
 *  You may redistribute it and/or modify it under the terms of the
 
17
 *  GNU General Public License, as published by the Free Software
 
18
 *  Foundation; either version 2, or (at your option) any later version.
 
19
 *
 
20
 *  Automated Options is distributed in the hope that it will be useful,
 
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 *  GNU General Public License for more details.
 
24
 *
 
25
 *  You should have received a copy of the GNU General Public License
 
26
 *  along with Automated Options.  See the file "COPYING".  If not,
 
27
 *  write to:  The Free Software Foundation, Inc.,
 
28
 *             51 Franklin Street, Fifth Floor,
 
29
 *             Boston, MA  02110-1301, USA.
 
30
 *
 
31
 * As a special exception, Bruce Korb gives permission for additional
 
32
 * uses of the text contained in his release of AutoOpts.
 
33
 *
 
34
 * The exception is that, if you link the AutoOpts library with other
 
35
 * files to produce an executable, this does not by itself cause the
 
36
 * resulting executable to be covered by the GNU General Public License.
 
37
 * Your use of that executable is in no way restricted on account of
 
38
 * linking the AutoOpts library code into it.
 
39
 *
 
40
 * This exception does not however invalidate any other reasons why
 
41
 * the executable file might be covered by the GNU General Public License.
 
42
 *
 
43
 * This exception applies only to the code released by Bruce Korb under
 
44
 * the name AutoOpts.  If you copy code from other sources under the
 
45
 * General Public License into a copy of AutoOpts, as the General Public
 
46
 * License permits, the exception does not apply to the code that you add
 
47
 * in this way.  To avoid misleading anyone as to the status of such
 
48
 * modified files, you must delete this exception notice from them.
 
49
 *
 
50
 * If you write modifications of your own for AutoOpts, it is your choice
 
51
 * whether to permit this exception to apply to your modifications.
 
52
 * If you do not wish that, delete this exception notice.
 
53
 */
 
54
 
 
55
/* = = = START-STATIC-FORWARD = = = */
 
56
/* static forward declarations maintained by :mkfwd */
 
57
/* = = = END-STATIC-FORWARD = = = */
 
58
 
 
59
/*
 
60
 *  doPrognameEnv - check for preset values from the ${PROGNAME}
 
61
 *  environment variable.  This is accomplished by parsing the text into
 
62
 *  tokens, temporarily replacing the arg vector and calling
 
63
 *  doImmediateOpts and/or doRegularOpts.
 
64
 */
 
65
LOCAL void
 
66
doPrognameEnv( tOptions* pOpts, teEnvPresetType type )
 
67
{
 
68
    char const*   pczOptStr = getenv( pOpts->pzPROGNAME );
 
69
    token_list_t* pTL;
 
70
    int           sv_argc;
 
71
    tAoUI         sv_flag;
 
72
    char**        sv_argv;
 
73
 
 
74
    /*
 
75
     *  IF there is no such environment variable
 
76
     *   *or* there is, but we are doing immediate opts and there are
 
77
     *        no immediate opts to do (--help inside $PROGNAME is silly,
 
78
     *        but --no-load-defs is not, so that is marked)
 
79
     *  THEN bail out now.  (
 
80
     */
 
81
    if (  (pczOptStr == NULL)
 
82
       || (  (type == ENV_IMM)
 
83
          && ((pOpts->fOptSet & OPTPROC_HAS_IMMED) == 0)  )  )
 
84
        return;
 
85
 
 
86
    /*
 
87
     *  Tokenize the string.  If there's nothing of interest, we'll bail
 
88
     *  here immediately.
 
89
     */
 
90
    pTL = ao_string_tokenize( pczOptStr );
 
91
    if (pTL == NULL)
 
92
        return;
 
93
 
 
94
    /*
 
95
     *  Substitute our $PROGNAME argument list for the real one
 
96
     */
 
97
    sv_argc = pOpts->origArgCt;
 
98
    sv_argv = pOpts->origArgVect;
 
99
    sv_flag = pOpts->fOptSet;
 
100
 
 
101
    /*
 
102
     *  We add a bogus pointer to the start of the list.  The program name
 
103
     *  has already been pulled from "argv", so it won't get dereferenced.
 
104
     *  The option scanning code will skip the "program name" at the start
 
105
     *  of this list of tokens, so we accommodate this way ....
 
106
     */
 
107
    pOpts->origArgVect = (char**)(pTL->tkn_list - 1);
 
108
    pOpts->origArgCt   = pTL->tkn_ct   + 1;
 
109
    pOpts->fOptSet    &= ~OPTPROC_ERRSTOP;
 
110
 
 
111
    pOpts->curOptIdx   = 1;
 
112
    pOpts->pzCurOpt    = NULL;
 
113
 
 
114
    switch (type) {
 
115
    case ENV_IMM:
 
116
        /*
 
117
         *  We know the OPTPROC_HAS_IMMED bit is set.
 
118
         */
 
119
        (void)doImmediateOpts( pOpts );
 
120
        break;
 
121
 
 
122
    case ENV_NON_IMM:
 
123
        (void)doRegularOpts( pOpts );
 
124
        break;
 
125
 
 
126
    default:
 
127
        /*
 
128
         *  Only to immediate opts if the OPTPROC_HAS_IMMED bit is set.
 
129
         */
 
130
        if (pOpts->fOptSet & OPTPROC_HAS_IMMED) {
 
131
            (void)doImmediateOpts( pOpts );
 
132
            pOpts->curOptIdx = 1;
 
133
            pOpts->pzCurOpt  = NULL;
 
134
        }
 
135
        (void)doRegularOpts( pOpts );
 
136
        break;
 
137
    }
 
138
 
 
139
    /*
 
140
     *  Free up the temporary arg vector and restore the original program args.
 
141
     */
 
142
    free( pTL );
 
143
    pOpts->origArgVect = sv_argv;
 
144
    pOpts->origArgCt   = sv_argc;
 
145
    pOpts->fOptSet     = sv_flag;
 
146
}
 
147
 
 
148
 
 
149
/*
 
150
 *  doEnvPresets - check for preset values from the envrionment
 
151
 *  This routine should process in all, immediate or normal modes....
 
152
 */
 
153
LOCAL void
 
154
doEnvPresets( tOptions* pOpts, teEnvPresetType type )
 
155
{
 
156
    int        ct;
 
157
    tOptState  st;
 
158
    char*      pzFlagName;
 
159
    size_t     spaceLeft;
 
160
    char       zEnvName[ AO_NAME_SIZE ];
 
161
 
 
162
    /*
 
163
     *  Finally, see if we are to look at the environment
 
164
     *  variables for initial values.
 
165
     */
 
166
    if ((pOpts->fOptSet & OPTPROC_ENVIRON) == 0)
 
167
        return;
 
168
 
 
169
    doPrognameEnv( pOpts, type );
 
170
 
 
171
    ct  = pOpts->presetOptCt;
 
172
    st.pOD = pOpts->pOptDesc;
 
173
 
 
174
    pzFlagName = zEnvName
 
175
        + snprintf( zEnvName, sizeof( zEnvName ), "%s_", pOpts->pzPROGNAME );
 
176
    spaceLeft = AO_NAME_SIZE - (pzFlagName - zEnvName) - 1;
 
177
 
 
178
    for (;ct-- > 0; st.pOD++) {
 
179
        /*
 
180
         *  If presetting is disallowed, then skip this entry
 
181
         */
 
182
        if (  ((st.pOD->fOptState & OPTST_NO_INIT) != 0)
 
183
           || (st.pOD->optEquivIndex != NO_EQUIVALENT)  )
 
184
            continue;
 
185
 
 
186
        /*
 
187
         *  IF there is no such environment variable,
 
188
         *  THEN skip this entry, too.
 
189
         */
 
190
        if (strlen( st.pOD->pz_NAME ) >= spaceLeft)
 
191
            continue;
 
192
 
 
193
        /*
 
194
         *  Set up the option state
 
195
         */
 
196
        strcpy( pzFlagName, st.pOD->pz_NAME );
 
197
        st.pzOptArg = getenv( zEnvName );
 
198
        if (st.pzOptArg == NULL)
 
199
            continue;
 
200
        st.flags    = OPTST_PRESET | st.pOD->fOptState;
 
201
        st.optType  = TOPT_UNDEFINED;
 
202
 
 
203
        if (  (st.pOD->pz_DisablePfx != NULL)
 
204
           && (streqvcmp( st.pzOptArg, st.pOD->pz_DisablePfx ) == 0)) {
 
205
            st.flags |= OPTST_DISABLED;
 
206
            st.pzOptArg = NULL;
 
207
        }
 
208
 
 
209
        switch (type) {
 
210
        case ENV_IMM:
 
211
            /*
 
212
             *  Process only immediate actions
 
213
             */
 
214
            if (DO_IMMEDIATELY(st.flags))
 
215
                break;
 
216
            continue;
 
217
 
 
218
        case ENV_NON_IMM:
 
219
            /*
 
220
             *  Process only NON immediate actions
 
221
             */
 
222
            if (DO_NORMALLY(st.flags) || DO_SECOND_TIME(st.flags))
 
223
                break;
 
224
            continue;
 
225
 
 
226
        default: /* process everything */
 
227
            break;
 
228
        }
 
229
 
 
230
        /*
 
231
         *  Make sure the option value string is persistent and consistent.
 
232
         *  This may be a memory leak, but we cannot do anything about it.
 
233
         *
 
234
         *  The interpretation of the option value depends
 
235
         *  on the type of value argument the option takes
 
236
         */
 
237
        if (st.pzOptArg != NULL) {
 
238
            if (OPTST_GET_ARGTYPE(st.pOD->fOptState) == OPARG_TYPE_NONE) {
 
239
                st.pzOptArg = NULL;
 
240
            } else if (  (st.pOD->fOptState & OPTST_ARG_OPTIONAL)
 
241
                      && (*st.pzOptArg == NUL)) {
 
242
                    st.pzOptArg = NULL;
 
243
            } else if (*st.pzOptArg == NUL) {
 
244
                st.pzOptArg = zNil;
 
245
            } else {
 
246
                AGDUPSTR( st.pzOptArg, st.pzOptArg, "option argument" );
 
247
            }
 
248
        }
 
249
 
 
250
        handleOption( pOpts, &st );
 
251
    }
 
252
}
 
253
 
 
254
/*
 
255
 * Local Variables:
 
256
 * mode: C
 
257
 * c-file-style: "stroustrup"
 
258
 * indent-tabs-mode: nil
 
259
 * End:
 
260
 * end of autoopts/environment.c */