summaryrefslogtreecommitdiffstats
path: root/src/kmk/kmkbuiltin/append.c
blob: c34847ebfc4fe7711794daab842fa3fea5fb1e3a (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
/* $Id: append.c 3246 2018-12-25 21:02:04Z bird $ */
/** @file
 * kMk Builtin command - append text to file.
 */

/*
 * Copyright (c) 2005-2010 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                                                                                                                 *
*********************************************************************************************************************************/
#ifndef KMK_BUILTIN_STANDALONE
# include "makeint.h"
# include "filedef.h"
# include "variable.h"
#else
# include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef _MSC_VER
# include <io.h>
#endif
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif
#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
# include "../w32/winchildren.h"
#endif
#include "err.h"
#include "kmkbuiltin.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define STR_TUPLE(a_sz)     a_sz, (sizeof(a_sz) - 1)

/** No-inherit open flag.   */
#ifdef _O_NOINHERIT
# define MY_O_NOINHERIT     _O_NOINHERIT
#elif defined(O_NOINHERIT)
# define MY_O_NOINHERIT     O_NOINHERIT
#elif defined(O_CLOEXEC)
# define MY_O_NOINHERIT     O_CLOEXEC
#else
# define MY_O_NOINHERIT     0
#endif

/** Binary mode open flag. */
#ifdef _O_BINARY
# define MY_O_BINARY        _O_BINARY
#elif defined(O_BINARY)
# define MY_O_BINARY        O_BINARY
#else
# define MY_O_BINARY        0
#endif


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Append output buffer.
 */
typedef struct KMKBUILTINAPPENDBUF
{
    /** Buffer pointer. */
    char   *pszBuf;
    /** The buffer allocation size. */
    size_t  cbBuf;
    /** The current buffer offset. */
    size_t  offBuf;
    /** Set if we ran out of memory. */
    int     fOutOfMemory;
} KMKBUILTINAPPENDBUF;


/**
 * Appends a substring to the output buffer.
 *
 * @param   pBuf        The output buffer.
 * @param   pch         The substring pointer.
 * @param   cch         The substring length.
 */
static void write_to_buf(KMKBUILTINAPPENDBUF *pBuf, const char *pch, size_t cch)
{
    size_t const offCur = pBuf->offBuf;
    size_t       offNew = offCur + cch;

    if (offNew >= pBuf->cbBuf)
    {
        size_t cbNew = offNew + 1 + 256;
        void  *pvNew;
        cbNew = (cbNew + 511) & ~(size_t)511;
        pvNew = realloc(pBuf->pszBuf, cbNew);
        if (pvNew)
            pBuf->pszBuf = (char *)pvNew;
        else
        {
            free(pBuf->pszBuf);
            pBuf->pszBuf = NULL;
            pBuf->cbBuf  = 0;
            pBuf->offBuf = offNew;
            pBuf->fOutOfMemory = 1;
            return;
        }
    }

    memcpy(&pBuf->pszBuf[offCur], pch, cch);
    pBuf->pszBuf[offNew] = '\0';
    pBuf->offBuf = offNew;
}

/**
 * Adds a string to the output buffer.
 *
 * @param   pBuf        The output buffer.
 * @param   psz         The string.
 */
static void string_to_buf(KMKBUILTINAPPENDBUF *pBuf, const char *psz)
{
    write_to_buf(pBuf, psz, strlen(psz));
}


/**
 * Prints the usage and return 1.
 */
static int kmk_builtin_append_usage(const char *arg0, FILE *pf)
{
    fprintf(pf,
            "usage: %s [-dcnNtv] file [string ...]\n"
            "   or: %s --version\n"
            "   or: %s --help\n"
            "\n"
            "Options:\n"
            "  -d  Enclose the output in define ... endef, taking the name from\n"
            "      the first argument following the file name.\n"
            "  -c  Output the command for specified target(s). [builtin only]\n"
            "  -i  look for --insert-command=trg and --insert-variable=var. [builtin only]\n"
            "  -n  Insert a newline between the strings.\n"
            "  -N  Suppress the trailing newline.\n"
            "  -t  Truncate the file instead of appending\n"
            "  -v  Output the value(s) for specified variable(s). [builtin only]\n"
            ,
            arg0, arg0, arg0);
    return 1;
}

/**
 * Appends text to a textfile, creating the textfile if necessary.
 */
int kmk_builtin_append(int argc, char **argv, char **envp, PKMKBUILTINCTX pCtx, struct child *pChild, pid_t *pPidSpawned)
{
#if defined(KBUILD_OS_WINDOWS) || defined(KBUILD_OS_OS2)
    static const char s_szNewLine[] = "\r\n";
#else
    static const char s_szNewLine[] = "\n";
#endif
    KMKBUILTINAPPENDBUF OutBuf = { NULL, 0, 0, 0 };
    const char *pszFilename;
    int rc = 88;
    int i;
    int fFirst;
    int fNewline = 0;
    int fNoTrailingNewline = 0;
    int fTruncate = 0;
    int fDefine = 0;
    int fVariables = 0;
    int fCommands = 0;
#ifndef KMK_BUILTIN_STANDALONE
    int fLookForInserts = 0;
#else
    (void)pChild; (void)pPidSpawned;
#endif

    /*
     * Parse options.
     */
    i = 1;
    while (i < argc
       &&  argv[i][0] == '-'
       &&  argv[i][1] != '\0' /* '-' is a file */
       &&  strchr("-cdinNtv", argv[i][1]) /* valid option char */
       )
    {
        char *psz = &argv[i][1];
        if (*psz != '-')
        {
            do
            {
                switch (*psz)
                {
                    case 'c':
                        if (fVariables)
                        {
                            errx(pCtx, 1, "Option '-c' clashes with '-v'.");
                            return kmk_builtin_append_usage(argv[0], stderr);
                        }
#ifndef KMK_BUILTIN_STANDALONE
                        fCommands = 1;
                        break;
#else
                        errx(pCtx, 1, "Option '-c' isn't supported in external mode.");
                        return kmk_builtin_append_usage(argv[0], stderr);
#endif
                    case 'd':
                        if (fVariables)
                        {
                            errx(pCtx, 1, "Option '-d' must come before '-v'!");
                            return kmk_builtin_append_usage(argv[0], stderr);
                        }
                        fDefine = 1;
                        break;
                    case 'i':
                        if (fVariables || fCommands)
                        {
                            errx(pCtx, 1, fVariables ? "Option '-i' clashes with '-v'." : "Option '-i' clashes with '-c'.");
                            return kmk_builtin_append_usage(argv[0], stderr);
                        }
#ifndef KMK_BUILTIN_STANDALONE
                        fLookForInserts = 1;
                        break;
#else
                        errx(pCtx, 1, "Option '-C' isn't supported in external mode.");
                        return kmk_builtin_append_usage(argv[0], stderr);
#endif
                    case 'n':
                        fNewline = 1;
                        break;
                    case 'N':
                        fNoTrailingNewline = 1;
                        break;
                    case 't':
                        fTruncate = 1;
                        break;
                    case 'v':
                        if (fCommands)
                        {
                            errx(pCtx, 1, "Option '-v' clashes with '-c'.");
                            return kmk_builtin_append_usage(argv[0], stderr);
                        }
#ifndef KMK_BUILTIN_STANDALONE
                        fVariables = 1;
                        break;
#else
                        errx(pCtx, 1, "Option '-v' isn't supported in external mode.");
                        return kmk_builtin_append_usage(argv[0], stderr);
#endif
                    default:
                        errx(pCtx, 1, "Invalid option '%c'! (%s)", *psz, argv[i]);
                        return kmk_builtin_append_usage(argv[0], stderr);
                }
            } while (*++psz);
        }
        else if (!strcmp(psz, "-help"))
        {
            kmk_builtin_append_usage(argv[0], stdout);
            return 0;
        }
        else if (!strcmp(psz, "-version"))
            return kbuild_version(argv[0]);
        else
            break;
        i++;
    }

    /*
     * Take down the filename.
     */
    if (i + fDefine < argc)
        pszFilename = argv[i++];
    else
    {
        if (i <= argc)
            errx(pCtx, 1, "missing filename!");
        else
            errx(pCtx, 1, "missing define name!");
        return kmk_builtin_append_usage(argv[0], stderr);
    }

    /* Start of no-return zone! */

    /*
     * Start define?
     */
    if (fDefine)
    {
        write_to_buf(&OutBuf, STR_TUPLE("define "));
        string_to_buf(&OutBuf, argv[i]);
        write_to_buf(&OutBuf, STR_TUPLE(s_szNewLine));
        i++;
    }

    /*
     * Append the argument strings to the file
     */
    fFirst = 1;
    for (; i < argc; i++)
    {
        const char *psz = argv[i];
        size_t cch = strlen(psz);
        if (!fFirst)
        {
            if (fNewline)
                write_to_buf(&OutBuf, STR_TUPLE(s_szNewLine));
            else
                write_to_buf(&OutBuf, STR_TUPLE(" "));
        }
#ifndef KMK_BUILTIN_STANDALONE
        if (fCommands)
        {
            char *pszOldBuf;
            unsigned cchOldBuf;
            char *pchEnd;

            install_variable_buffer(&pszOldBuf, &cchOldBuf);

            pchEnd = func_commands(variable_buffer, &argv[i], "commands");
            write_to_buf(&OutBuf, variable_buffer, pchEnd - variable_buffer);

            restore_variable_buffer(pszOldBuf, cchOldBuf);
        }
        else if (fVariables)
        {
            struct variable *pVar = lookup_variable(psz, cch);
            if (!pVar)
                continue;
            if (   !pVar->recursive
                || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar))
                write_to_buf(&OutBuf, pVar->value, pVar->value_length);
            else
            {
                char *pszExpanded = allocated_variable_expand(pVar->value);
                string_to_buf(&OutBuf, pszExpanded);
                free(pszExpanded);
            }
        }
        else if (fLookForInserts && strncmp(psz, "--insert-command=", 17) == 0)
        {
            char *pszOldBuf;
            unsigned cchOldBuf;
            char *pchEnd;

            install_variable_buffer(&pszOldBuf, &cchOldBuf);

            psz += 17;
            pchEnd = func_commands(variable_buffer, (char **)&psz, "commands");
            write_to_buf(&OutBuf, variable_buffer, pchEnd - variable_buffer);

            restore_variable_buffer(pszOldBuf, cchOldBuf);
        }
        else if (fLookForInserts && strncmp(psz, "--insert-variable=", 18) == 0)
        {
            struct variable *pVar = lookup_variable(psz + 18, cch);
            if (!pVar)
                continue;
            if (   !pVar->recursive
                || IS_VARIABLE_RECURSIVE_WITHOUT_DOLLAR(pVar))
                write_to_buf(&OutBuf, pVar->value, pVar->value_length);
            else
            {
                char *pszExpanded = allocated_variable_expand(pVar->value);
                string_to_buf(&OutBuf, pszExpanded);
                free(pszExpanded);
            }
        }
        else
#endif
            write_to_buf(&OutBuf, psz, cch);
        fFirst = 0;
    }

    /*
     * End the define?
     */
    if (fDefine)
    {
        if (fFirst)
            write_to_buf(&OutBuf, STR_TUPLE(s_szNewLine));
        write_to_buf(&OutBuf, STR_TUPLE("endef"));
    }

    /*
     * Add final newline (unless supressed) and check for errors.
     */
    if (!fNoTrailingNewline)
        write_to_buf(&OutBuf, STR_TUPLE(s_szNewLine));

    /*
     * Write the buffer (unless we ran out of heap already).
     */
#if !defined(KMK_BUILTIN_STANDALONE) && defined(KBUILD_OS_WINDOWS) && defined(CONFIG_NEW_WIN_CHILDREN)
    if (!OutBuf.fOutOfMemory)
    {
        rc = MkWinChildCreateAppend(pszFilename, &OutBuf.pszBuf, OutBuf.offBuf, fTruncate, pChild, pPidSpawned);
        if (rc != 0)
            rc = errx(pCtx, rc, "MkWinChildCreateAppend failed: %u", rc);
        if (OutBuf.pszBuf)
            free(OutBuf.pszBuf);
    }
    else
#endif
    if (!OutBuf.fOutOfMemory)
    {
        int fd = open(pszFilename,
                      fTruncate ? O_WRONLY | O_TRUNC  | O_CREAT | MY_O_NOINHERIT | MY_O_BINARY
                                : O_WRONLY | O_APPEND | O_CREAT | MY_O_NOINHERIT | MY_O_BINARY,
                      0666);
        if (fd >= 0)
        {
            ssize_t cbWritten = write(fd, OutBuf.pszBuf, OutBuf.offBuf);
            if (cbWritten == (ssize_t)OutBuf.offBuf)
                rc = 0;
            else
                rc = err(pCtx, 1, "error writing %lu bytes to '%s'", (unsigned long)OutBuf.offBuf, pszFilename);
            if (close(fd) < 0)
                rc = err(pCtx, 1, "error closing '%s'", pszFilename);
        }
        else
            rc = err(pCtx, 1, "failed to open '%s'", pszFilename);
        free(OutBuf.pszBuf);
    }
    else
        rc = errx(pCtx, 1, "out of memory for output buffer! (%u needed)", OutBuf.offBuf + 1);
    return rc;
}

#ifdef KMK_BUILTIN_STANDALONE
int main(int argc, char **argv, char **envp)
{
    KMKBUILTINCTX Ctx = { "kmk_append", NULL };
    return kmk_builtin_append(argc, argv, envp, &Ctx, NULL, NULL);
}
#endif