| 1 | /* $Id: append.c 785 2007-01-24 22:21:56Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | *
|
|---|
| 4 | * kMk Builtin command - append text to file.
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright (c) 2005-2007 knut st. osmundsen <bird-kBuild-spam@anduin.net>
|
|---|
| 7 | *
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 11 | * kBuild is free software; you can redistribute it and/or modify
|
|---|
| 12 | * it under the terms of the GNU General Public License as published by
|
|---|
| 13 | * the Free Software Foundation; either version 2 of the License, or
|
|---|
| 14 | * (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kBuild is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 19 | * GNU General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU General Public License
|
|---|
| 22 | * along with kBuild; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include <string.h>
|
|---|
| 28 | #include <stdio.h>
|
|---|
| 29 | #include "err.h"
|
|---|
| 30 | #include "kmkbuiltin.h"
|
|---|
| 31 | #ifndef kmk_builtin_append
|
|---|
| 32 | # include "make.h"
|
|---|
| 33 | # include "variable.h"
|
|---|
| 34 | #endif
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | /**
|
|---|
| 38 | * Prints the usage and return 1.
|
|---|
| 39 | */
|
|---|
| 40 | static int usage(void)
|
|---|
| 41 | {
|
|---|
| 42 | fprintf(stderr, "usage: append [-nv] file [string ...]\n");
|
|---|
| 43 | return 1;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | /**
|
|---|
| 48 | * Appends text to a textfile, creating the textfile if necessary.
|
|---|
| 49 | */
|
|---|
| 50 | int kmk_builtin_append(int argc, char **argv, char **envp)
|
|---|
| 51 | {
|
|---|
| 52 | int i;
|
|---|
| 53 | int fFirst;
|
|---|
| 54 | FILE *pFile;
|
|---|
| 55 | int fNewLine = 0;
|
|---|
| 56 | #ifndef kmk_builtin_append
|
|---|
| 57 | int fVariables = 0;
|
|---|
| 58 | #endif
|
|---|
| 59 |
|
|---|
| 60 | g_progname = argv[0];
|
|---|
| 61 |
|
|---|
| 62 | /*
|
|---|
| 63 | * Parse options.
|
|---|
| 64 | */
|
|---|
| 65 | i = 1;
|
|---|
| 66 | while (i < argc
|
|---|
| 67 | && argv[i][0] == '-'
|
|---|
| 68 | && argv[i][1] != '\0' /* '-' is a file */
|
|---|
| 69 | && strchr("nv", argv[i][1]) /* valid option char */
|
|---|
| 70 | )
|
|---|
| 71 | {
|
|---|
| 72 | char *psz = &argv[i][1];
|
|---|
| 73 | do
|
|---|
| 74 | {
|
|---|
| 75 | switch (*psz)
|
|---|
| 76 | {
|
|---|
| 77 | case 'n':
|
|---|
| 78 | fNewLine = 1;
|
|---|
| 79 | break;
|
|---|
| 80 | case 'v':
|
|---|
| 81 | #ifndef kmk_builtin_append
|
|---|
| 82 | fVariables = 1;
|
|---|
| 83 | break;
|
|---|
| 84 | #else
|
|---|
| 85 | errx(1, "Option '-v' isn't supported in external mode.");
|
|---|
| 86 | return usage();
|
|---|
| 87 | #endif
|
|---|
| 88 | default:
|
|---|
| 89 | errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
|
|---|
| 90 | return usage();
|
|---|
| 91 | }
|
|---|
| 92 | } while (*++psz);
|
|---|
| 93 | i++;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /*
|
|---|
| 97 | * Open the output file.
|
|---|
| 98 | */
|
|---|
| 99 | if (i >= argc)
|
|---|
| 100 | {
|
|---|
| 101 | errx(1, "missing filename!");
|
|---|
| 102 | return usage();
|
|---|
| 103 | }
|
|---|
| 104 | pFile = fopen(argv[i], "a");
|
|---|
| 105 | if (!pFile)
|
|---|
| 106 | return err(1, "failed to open '%s'.", argv[i]);
|
|---|
| 107 |
|
|---|
| 108 | /*
|
|---|
| 109 | * Append the argument strings to the file
|
|---|
| 110 | */
|
|---|
| 111 | fFirst = 1;
|
|---|
| 112 | for (i++; i < argc; i++)
|
|---|
| 113 | {
|
|---|
| 114 | const char *psz = argv[i];
|
|---|
| 115 | size_t cch = strlen(psz);
|
|---|
| 116 | if (!fFirst)
|
|---|
| 117 | fputc(fNewLine ? '\n' : ' ', pFile);
|
|---|
| 118 | #ifndef kmk_builtin_append
|
|---|
| 119 | if (fVariables)
|
|---|
| 120 | {
|
|---|
| 121 | struct variable *pVar = lookup_variable(psz, cch);
|
|---|
| 122 | if (!pVar)
|
|---|
| 123 | continue;
|
|---|
| 124 | if ( pVar->recursive
|
|---|
| 125 | && memchr(pVar->value, '$', pVar->value_length))
|
|---|
| 126 | {
|
|---|
| 127 | char *pszExpanded = allocated_variable_expand(pVar->value);
|
|---|
| 128 | fwrite(pszExpanded, 1, strlen(pszExpanded), pFile);
|
|---|
| 129 | free(pszExpanded);
|
|---|
| 130 | }
|
|---|
| 131 | else
|
|---|
| 132 | fwrite(pVar->value, 1, pVar->value_length, pFile);
|
|---|
| 133 | }
|
|---|
| 134 | else
|
|---|
| 135 | #endif
|
|---|
| 136 | fwrite(psz, 1, cch, pFile);
|
|---|
| 137 | fFirst = 0;
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | /*
|
|---|
| 141 | * Add the newline and close the file.
|
|---|
| 142 | */
|
|---|
| 143 | if ( fputc('\n', pFile) == EOF
|
|---|
| 144 | || ferror(pFile))
|
|---|
| 145 | {
|
|---|
| 146 | fclose(pFile);
|
|---|
| 147 | return errx(1, "error writing to '%s'!", argv[1]);
|
|---|
| 148 | }
|
|---|
| 149 | if (fclose(pFile))
|
|---|
| 150 | return err(1, "failed to fclose '%s'!", argv[1]);
|
|---|
| 151 | return 0;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|