| 1 | /* $Id: append.c 820 2007-02-01 04:53:30Z 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 "filedef.h"
|
|---|
| 34 | # include "variable.h"
|
|---|
| 35 | #endif
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Prints the usage and return 1.
|
|---|
| 40 | */
|
|---|
| 41 | static int usage(void)
|
|---|
| 42 | {
|
|---|
| 43 | fprintf(stderr, "usage: append [-nv] file [string ...]\n");
|
|---|
| 44 | return 1;
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 | /**
|
|---|
| 49 | * Appends text to a textfile, creating the textfile if necessary.
|
|---|
| 50 | */
|
|---|
| 51 | int kmk_builtin_append(int argc, char **argv, char **envp)
|
|---|
| 52 | {
|
|---|
| 53 | int i;
|
|---|
| 54 | int fFirst;
|
|---|
| 55 | FILE *pFile;
|
|---|
| 56 | int fNewLine = 0;
|
|---|
| 57 | #ifndef kmk_builtin_append
|
|---|
| 58 | int fVariables = 0;
|
|---|
| 59 | #endif
|
|---|
| 60 |
|
|---|
| 61 | g_progname = argv[0];
|
|---|
| 62 |
|
|---|
| 63 | /*
|
|---|
| 64 | * Parse options.
|
|---|
| 65 | */
|
|---|
| 66 | i = 1;
|
|---|
| 67 | while (i < argc
|
|---|
| 68 | && argv[i][0] == '-'
|
|---|
| 69 | && argv[i][1] != '\0' /* '-' is a file */
|
|---|
| 70 | && strchr("nv", argv[i][1]) /* valid option char */
|
|---|
| 71 | )
|
|---|
| 72 | {
|
|---|
| 73 | char *psz = &argv[i][1];
|
|---|
| 74 | do
|
|---|
| 75 | {
|
|---|
| 76 | switch (*psz)
|
|---|
| 77 | {
|
|---|
| 78 | case 'n':
|
|---|
| 79 | fNewLine = 1;
|
|---|
| 80 | break;
|
|---|
| 81 | case 'v':
|
|---|
| 82 | #ifndef kmk_builtin_append
|
|---|
| 83 | fVariables = 1;
|
|---|
| 84 | break;
|
|---|
| 85 | #else
|
|---|
| 86 | errx(1, "Option '-v' isn't supported in external mode.");
|
|---|
| 87 | return usage();
|
|---|
| 88 | #endif
|
|---|
| 89 | default:
|
|---|
| 90 | errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
|
|---|
| 91 | return usage();
|
|---|
| 92 | }
|
|---|
| 93 | } while (*++psz);
|
|---|
| 94 | i++;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | /*
|
|---|
| 98 | * Open the output file.
|
|---|
| 99 | */
|
|---|
| 100 | if (i >= argc)
|
|---|
| 101 | {
|
|---|
| 102 | errx(1, "missing filename!");
|
|---|
| 103 | return usage();
|
|---|
| 104 | }
|
|---|
| 105 | pFile = fopen(argv[i], "a");
|
|---|
| 106 | if (!pFile)
|
|---|
| 107 | return err(1, "failed to open '%s'.", argv[i]);
|
|---|
| 108 |
|
|---|
| 109 | /*
|
|---|
| 110 | * Append the argument strings to the file
|
|---|
| 111 | */
|
|---|
| 112 | fFirst = 1;
|
|---|
| 113 | for (i++; i < argc; i++)
|
|---|
| 114 | {
|
|---|
| 115 | const char *psz = argv[i];
|
|---|
| 116 | size_t cch = strlen(psz);
|
|---|
| 117 | if (!fFirst)
|
|---|
| 118 | fputc(fNewLine ? '\n' : ' ', pFile);
|
|---|
| 119 | #ifndef kmk_builtin_append
|
|---|
| 120 | if (fVariables)
|
|---|
| 121 | {
|
|---|
| 122 | struct variable *pVar = lookup_variable(psz, cch);
|
|---|
| 123 | if (!pVar)
|
|---|
| 124 | continue;
|
|---|
| 125 | if ( pVar->recursive
|
|---|
| 126 | && memchr(pVar->value, '$', pVar->value_length))
|
|---|
| 127 | {
|
|---|
| 128 | char *pszExpanded = allocated_variable_expand(pVar->value);
|
|---|
| 129 | fwrite(pszExpanded, 1, strlen(pszExpanded), pFile);
|
|---|
| 130 | free(pszExpanded);
|
|---|
| 131 | }
|
|---|
| 132 | else
|
|---|
| 133 | fwrite(pVar->value, 1, pVar->value_length, pFile);
|
|---|
| 134 | }
|
|---|
| 135 | else
|
|---|
| 136 | #endif
|
|---|
| 137 | fwrite(psz, 1, cch, pFile);
|
|---|
| 138 | fFirst = 0;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | /*
|
|---|
| 142 | * Add the newline and close the file.
|
|---|
| 143 | */
|
|---|
| 144 | if ( fputc('\n', pFile) == EOF
|
|---|
| 145 | || ferror(pFile))
|
|---|
| 146 | {
|
|---|
| 147 | fclose(pFile);
|
|---|
| 148 | return errx(1, "error writing to '%s'!", argv[1]);
|
|---|
| 149 | }
|
|---|
| 150 | if (fclose(pFile))
|
|---|
| 151 | return err(1, "failed to fclose '%s'!", argv[1]);
|
|---|
| 152 | return 0;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|