summaryrefslogtreecommitdiffstats
path: root/src/kmk/kmkbuiltin/common-env-and-cwd-opt.c
blob: a7f6d584421ab42f4c94b26d24732775fa901ea3 (plain)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/* $Id: common-env-and-cwd-opt.c 3332 2020-04-19 23:08:16Z bird $ */
/** @file
 * kMk Builtin command - Commmon environment and CWD option handling code.
 */

/*
 * Copyright (c) 2007-2016 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
 *
 * This file is part of kBuild.
 *
 * kBuild is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * kBuild is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with kBuild.  If not, see <http://www.gnu.org/licenses/>
 *
 */

/*******************************************************************************
*   Header Files                                                               *
*******************************************************************************/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#include "kmkbuiltin.h"
#include "err.h"


/** The environment variable compare function.
 * We must use case insensitive compare on windows (Path vs PATH).  */
#ifdef KBUILD_OS_WINDOWS
# define KSUBMIT_ENV_NCMP   _strnicmp
#else
# define KSUBMIT_ENV_NCMP   strncmp
#endif


/**
 * Duplicates a read-only enviornment vector.
 *
 * @returns The duplicate enviornment.
 * @param   pCtx                The built-in command context.
 * @param   papszEnv            The read-only vector.
 * @param   cEnvVars            The number of variables.
 * @param   pcAllocatedEnvVars  The allocated papszEnv size.  This is zero on
 *                              input and non-zero on successful return.
 * @param   cVerbosity          The verbosity level.
 */
static char **kBuiltinOptEnvDuplicate(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars,
                                      int cVerbosity)
{
    unsigned cAllocatedEnvVars = (cEnvVars + 2 + 0xf) & ~(unsigned)0xf;
    char    **papszEnvNew      = malloc(cAllocatedEnvVars * sizeof(papszEnvNew[0]));
    assert(*pcAllocatedEnvVars == 0);
    if (papszEnvNew)
    {
        unsigned i;
        for (i = 0; i < cEnvVars; i++)
        {
            papszEnvNew[i] = strdup(papszEnv[i]);
            if (!papszEnvNew)
            {
                while (i-- > 0)
                    free(papszEnvNew[i]);
                free(papszEnvNew);
                errx(pCtx, 1, "out of memory for duplicating environment variables!", i);
                return NULL;
            }
        }
        papszEnvNew[i] = NULL;
        *pcAllocatedEnvVars = cAllocatedEnvVars;
    }
    else
        errx(pCtx, 1, "out of memory for duplicating environment vector!");
    return papszEnvNew;
}


/**
 * Common worker for kBuiltinOptEnvSet and kBuiltinOptEnvAppendPrepend that adds
 * a new variable to the environment.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   papszEnv            The environment vector.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the variable holding max size of the
 *                              environment vector.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 */
static int kBuiltinOptEnvAddVar(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                                int cVerbosity, const char *pszValue)
{
    /* Append new variable. We probably need to resize the vector. */
    char   **papszEnv = *ppapszEnv;
    unsigned cEnvVars = *pcEnvVars;
    if ((cEnvVars + 2) > *pcAllocatedEnvVars)
    {
        *pcAllocatedEnvVars = (cEnvVars + 2 + 0xf) & ~(unsigned)0xf;
        papszEnv = (char **)realloc(papszEnv, *pcAllocatedEnvVars * sizeof(papszEnv[0]));
        if (!papszEnv)
            return errx(pCtx, 1, "out of memory growing environment vector!");
        *ppapszEnv = papszEnv;
    }
    papszEnv[cEnvVars] = strdup(pszValue);
    if (!papszEnv[cEnvVars])
        return errx(pCtx, 1, "out of memory adding environment variable!");
    papszEnv[++cEnvVars]   = NULL;
    *pcEnvVars = cEnvVars;
    if (cVerbosity > 0)
        warnx(pCtx, "added '%s'", papszEnv[cEnvVars - 1]);
    return 0;
}


/**
 * Common worker for kBuiltinOptEnvSet and kBuiltinOptEnvAppendPrepend that
 * remove duplicates.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   papszEnv            The environment vector.
 * @param   cEnvVars            Number of environment variables.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 * @param   cchVar              The length of the variable part of @a pszValue.
 * @param   iEnvVar             Where to start searching after.
 */
static int kBuiltinOptEnvRemoveDuplicates(PKMKBUILTINCTX pCtx, char **papszEnv, unsigned cEnvVars, int cVerbosity,
                                          const char *pszValue, size_t cchVar, unsigned iEnvVar)
{
    for (iEnvVar++; iEnvVar < cEnvVars; iEnvVar++)
        if (   KSUBMIT_ENV_NCMP(papszEnv[iEnvVar], pszValue, cchVar) == 0
            && papszEnv[iEnvVar][cchVar] == '=')
        {
            if (cVerbosity > 0)
                warnx(pCtx, "removing duplicate '%s'", papszEnv[iEnvVar]);
            free(papszEnv[iEnvVar]);
            cEnvVars--;
            if (iEnvVar != cEnvVars)
                papszEnv[iEnvVar] = papszEnv[cEnvVars];
            papszEnv[cEnvVars] = NULL;
            iEnvVar--;
        }
    return 0;
}


/**
 * Handles the --set var=value option.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the variable holding max size of the
 *                              environment vector.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 */
int kBuiltinOptEnvSet(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                      int cVerbosity, const char *pszValue)
{
    const char *pszEqual = strchr(pszValue, '=');
    if (pszEqual)
    {
        char   **papszEnv = *ppapszEnv;
        unsigned iEnvVar;
        unsigned cEnvVars = *pcEnvVars;
        size_t const cchVar = pszEqual - pszValue;

        if (!*pcAllocatedEnvVars)
        {
            papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
            if (!papszEnv)
                return errx(pCtx, 1, "out of memory duplicating enviornment (setenv)!");
            *ppapszEnv = papszEnv;
        }

        for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
        {
            char *pszCur = papszEnv[iEnvVar];
            if (   KSUBMIT_ENV_NCMP(pszCur, pszValue, cchVar) == 0
                && pszCur[cchVar] == '=')
            {
                if (cVerbosity > 0)
                    warnx(pCtx, "replacing '%s' with '%s'", papszEnv[iEnvVar], pszValue);
                free(papszEnv[iEnvVar]);
                papszEnv[iEnvVar] = strdup(pszValue);
                if (!papszEnv[iEnvVar])
                    return errx(pCtx, 1, "out of memory for modified environment variable!");

                return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);
            }
        }
        return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);
    }
    return errx(pCtx, 1, "Missing '=': -E %s", pszValue);
}


/**
 * Common worker for kBuiltinOptEnvAppend and kBuiltinOptEnvPrepend.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the variable holding max size of the
 *                              environment vector.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 */
static int kBuiltinOptEnvAppendPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                                       int cVerbosity, const char *pszValue, int fAppend)
{
    const char *pszEqual = strchr(pszValue, '=');
    if (pszEqual)
    {
        char   **papszEnv = *ppapszEnv;
        unsigned iEnvVar;
        unsigned cEnvVars = *pcEnvVars;
        size_t const cchVar = pszEqual - pszValue;

        if (!*pcAllocatedEnvVars)
        {
            papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
            if (!papszEnv)
                return errx(pCtx, 1, "out of memory duplicating environment (append)!");
            *ppapszEnv = papszEnv;
        }

        for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
        {
            char *pszCur = papszEnv[iEnvVar];
            if (   KSUBMIT_ENV_NCMP(pszCur, pszValue, cchVar) == 0
                && pszCur[cchVar] == '=')
            {
                size_t cchOldValue = strlen(pszCur)   - cchVar - 1;
                size_t cchNewValue = strlen(pszValue) - cchVar - 1;
                char  *pszNew      = malloc(cchVar + 1 + cchOldValue + cchNewValue + 1);
                if (!pszNew)
                    return errx(pCtx, 1, "out of memory appending to environment variable!");
                if (fAppend)
                {
                    memcpy(pszNew, pszCur, cchVar + 1 + cchOldValue);
                    memcpy(&pszNew[cchVar + 1 + cchOldValue], &pszValue[cchVar + 1], cchNewValue + 1);
                }
                else
                {
                    memcpy(pszNew, pszCur, cchVar + 1); /* preserve variable name case  */
                    memcpy(&pszNew[cchVar + 1], &pszValue[cchVar + 1], cchNewValue);
                    memcpy(&pszNew[cchVar + 1 + cchNewValue], &pszCur[cchVar + 1], cchOldValue + 1);
                }

                if (cVerbosity > 0)
                    warnx(pCtx, "replacing '%s' with '%s'", pszCur, pszNew);
                free(pszCur);
                papszEnv[iEnvVar] = pszNew;

                return kBuiltinOptEnvRemoveDuplicates(pCtx, papszEnv, cEnvVars, cVerbosity, pszValue, cchVar, iEnvVar);
            }
        }
        return kBuiltinOptEnvAddVar(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue);
    }
    return errx(pCtx, 1, "Missing '=': -%c %s", fAppend ? 'A' : 'D', pszValue);
}


/**
 * Handles the --append var=value option.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the variable holding max size of the
 *                              environment vector.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 */
int kBuiltinOptEnvAppend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                         int cVerbosity, const char *pszValue)
{
    return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 1 /*fAppend*/);
}


/**
 * Handles the --prepend var=value option.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the variable holding max size of the
 *                              environment vector.
 * @param   cVerbosity          The verbosity level.
 * @param   pszValue            The var=value string to apply.
 */
int kBuiltinOptEnvPrepend(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                          int cVerbosity, const char *pszValue)
{
    return kBuiltinOptEnvAppendPrepend(pCtx, ppapszEnv, pcEnvVars, pcAllocatedEnvVars, cVerbosity, pszValue, 0 /*fAppend*/);
}


/**
 * Handles the --unset var option.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the size of the vector allocation.
 *                              The size is zero when read-only (CRT, GNU make)
 *                              environment.
 * @param   cVerbosity          The verbosity level.
 * @param   pszVarToRemove      The name of the variable to remove.
 */
int kBuiltinOptEnvUnset(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars,
                        int cVerbosity, const char *pszVarToRemove)
{
    if (strchr(pszVarToRemove, '=') == NULL)
    {
        char       **papszEnv = *ppapszEnv;
        unsigned     cRemoved = 0;
        size_t const cchVar   = strlen(pszVarToRemove);
        unsigned     cEnvVars = *pcEnvVars;
        unsigned     iEnvVar;

        for (iEnvVar = 0; iEnvVar < cEnvVars; iEnvVar++)
            if (   KSUBMIT_ENV_NCMP(papszEnv[iEnvVar], pszVarToRemove, cchVar) == 0
                && papszEnv[iEnvVar][cchVar] == '=')
            {
                if (cVerbosity > 0)
                    warnx(pCtx, !cRemoved ? "removing '%s'" : "removing duplicate '%s'", papszEnv[iEnvVar]);

                if (!*pcAllocatedEnvVars)
                {
                    papszEnv = kBuiltinOptEnvDuplicate(pCtx, papszEnv, cEnvVars, pcAllocatedEnvVars, cVerbosity);
                    if (!papszEnv)
                        return errx(pCtx, 1, "out of memory duplicating environment (unset)!");
                    *ppapszEnv = papszEnv;
                }

                free(papszEnv[iEnvVar]);
                cEnvVars--;
                if (iEnvVar != cEnvVars)
                    papszEnv[iEnvVar] = papszEnv[cEnvVars];
                papszEnv[cEnvVars] = NULL;
                cRemoved++;
                iEnvVar--;
            }
        *pcEnvVars = cEnvVars;

        if (cVerbosity > 0 && !cRemoved)
            warnx(pCtx, "not found '%s'", pszVarToRemove);
    }
    else
        return errx(pCtx, 1, "Found invalid variable name character '=' in: -U %s", pszVarToRemove);
    return 0;
}


/**
 * Handles the --zap-env & --ignore-environment options.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   ppapszEnv           The environment vector pointer.
 * @param   pcEnvVars           Pointer to the variable holding the number of
 *                              environment variables held by @a papszEnv.
 * @param   pcAllocatedEnvVars  Pointer to the size of the vector allocation.
 *                              The size is zero when read-only (CRT, GNU make)
 *                              environment.
 * @param   cVerbosity          The verbosity level.
 */
int kBuiltinOptEnvZap(PKMKBUILTINCTX pCtx, char ***ppapszEnv, unsigned *pcEnvVars, unsigned *pcAllocatedEnvVars, int cVerbosity)
{
    if (*pcAllocatedEnvVars > 0)
    {
        char **papszEnv = *ppapszEnv;
        unsigned i = *pcEnvVars;
        while (i-- > 0)
        {
            free(papszEnv[i]);
            papszEnv[i] = NULL;
        }
    }
    else
    {
        char **papszEnv = calloc(4, sizeof(char *));
        if (!papszEnv)
            return err(pCtx, 1, "out of memory!");
        *ppapszEnv = papszEnv;
        *pcAllocatedEnvVars = 4;
    }
    *pcEnvVars = 0;
    return 0;
}


/**
 * Cleans up afterwards, if necessary.
 *
 * @param   ppapszEnv           The environment vector pointer.
 * @param   cEnvVars            The number of variables in the vector.
 * @param   pcAllocatedEnvVars  Pointer to the size of the vector allocation.
 *                              The size is zero when read-only (CRT, GNU make)
 *                              environment.
 */
void kBuiltinOptEnvCleanup(char ***ppapszEnv, unsigned cEnvVars, unsigned *pcAllocatedEnvVars)
{
    char **papszEnv = *ppapszEnv;
    *ppapszEnv = NULL;
    if (*pcAllocatedEnvVars > 0)
    {
        *pcAllocatedEnvVars = 0;
        while (cEnvVars-- > 0)
        {
            free(papszEnv[cEnvVars]);
            papszEnv[cEnvVars] = NULL;
        }
        free(papszEnv);
    }
}


/**
 * Handles the --chdir dir option.
 *
 * @returns 0 on success, non-zero exit code on error.
 * @param   pCtx                The built-in command context.
 * @param   pszCwd              The CWD buffer.  Contains current CWD on input,
 *                              modified by @a pszValue on output.
 * @param   cbCwdBuf            The size of the CWD buffer.
 * @param   pszValue            The --chdir value to apply.
 */
int kBuiltinOptChDir(PKMKBUILTINCTX pCtx, char *pszCwd, size_t cbCwdBuf, const char *pszValue)
{
    size_t cchNewCwd = strlen(pszValue);
    size_t offDst;
    if (cchNewCwd)
    {
#ifdef HAVE_DOS_PATHS
        if (*pszValue == '/' || *pszValue == '\\')
        {
            if (pszValue[1] == '/' || pszValue[1] == '\\')
                offDst = 0; /* UNC */
            else if (pszCwd[1] == ':' && isalpha(pszCwd[0]))
                offDst = 2; /* Take drive letter from CWD. */
            else
                return errx(pCtx, 1, "UNC relative CWD not implemented: cur='%s' new='%s'", pszCwd, pszValue);
        }
        else if (   pszValue[1] == ':'
                 && isalpha(pszValue[0]))
        {
            if (pszValue[2] == '/'|| pszValue[2] == '\\')
                offDst = 0; /* DOS style absolute path. */
            else if (   pszCwd[1] == ':'
                     && tolower(pszCwd[0]) == tolower(pszValue[0]) )
            {
                pszValue += 2; /* Same drive as CWD, append drive relative path from value. */
                cchNewCwd -= 2;
                offDst = strlen(pszCwd);
            }
            else
            {
                /* Get current CWD on the specified drive and append value. */
                int iDrive = tolower(pszValue[0]) - 'a' + 1;
                if (!_getdcwd(iDrive, pszCwd, cbCwdBuf))
                    return err(pCtx, 1, "_getdcwd(%d,,) failed", iDrive);
                pszValue += 2;
                cchNewCwd -= 2;
            }
        }
#else
        if (*pszValue == '/')
            offDst = 0;
#endif
        else
            offDst = strlen(pszCwd); /* Relative path, append to the existing CWD value. */

        /* Do the copying. */
#ifdef HAVE_DOS_PATHS
        if (offDst > 0 && pszCwd[offDst - 1] != '/' && pszCwd[offDst - 1] != '\\')
#else
        if (offDst > 0 && pszCwd[offDst - 1] != '/')
#endif
             pszCwd[offDst++] = '/';
        if (offDst + cchNewCwd >= cbCwdBuf)
            return errx(pCtx, 1, "Too long CWD: %*.*s%s", offDst, offDst, pszCwd, pszValue);
        memcpy(&pszCwd[offDst], pszValue, cchNewCwd + 1);
    }
    /* else: relative, no change - quitely ignore. */
    return 0;
}