1 | /* $Id: append.c 1442 2008-03-30 06:45:10Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * kMk Builtin command - append text to file.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2005-2007 knut st. osmundsen <[email protected]>
|
---|
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(FILE *pf)
|
---|
42 | {
|
---|
43 | fprintf(pf,
|
---|
44 | "usage: %s [-cnv] file [string ...]\n"
|
---|
45 | " or: %s --version\n"
|
---|
46 | " or: %s --help\n",
|
---|
47 | g_progname, g_progname, g_progname);
|
---|
48 | return 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Appends text to a textfile, creating the textfile if necessary.
|
---|
54 | */
|
---|
55 | int kmk_builtin_append(int argc, char **argv, char **envp)
|
---|
56 | {
|
---|
57 | int i;
|
---|
58 | int fFirst;
|
---|
59 | FILE *pFile;
|
---|
60 | int fNewLine = 0;
|
---|
61 | #ifndef kmk_builtin_append
|
---|
62 | int fVariables = 0;
|
---|
63 | int fCommands = 0;
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | g_progname = argv[0];
|
---|
67 |
|
---|
68 | /*
|
---|
69 | * Parse options.
|
---|
70 | */
|
---|
71 | i = 1;
|
---|
72 | while (i < argc
|
---|
73 | && argv[i][0] == '-'
|
---|
74 | && argv[i][1] != '\0' /* '-' is a file */
|
---|
75 | && strchr("-cnv", argv[i][1]) /* valid option char */
|
---|
76 | )
|
---|
77 | {
|
---|
78 | char *psz = &argv[i][1];
|
---|
79 | if (*psz != '-')
|
---|
80 | {
|
---|
81 | do
|
---|
82 | {
|
---|
83 | switch (*psz)
|
---|
84 | {
|
---|
85 | case 'c':
|
---|
86 | #ifndef kmk_builtin_append
|
---|
87 | fCommands = 1;
|
---|
88 | break;
|
---|
89 | #else
|
---|
90 | errx(1, "Option '-c' isn't supported in external mode.");
|
---|
91 | return usage(stderr);
|
---|
92 | #endif
|
---|
93 | case 'n':
|
---|
94 | fNewLine = 1;
|
---|
95 | break;
|
---|
96 | case 'v':
|
---|
97 | #ifndef kmk_builtin_append
|
---|
98 | fVariables = 1;
|
---|
99 | break;
|
---|
100 | #else
|
---|
101 | errx(1, "Option '-v' isn't supported in external mode.");
|
---|
102 | return usage(stderr);
|
---|
103 | #endif
|
---|
104 | default:
|
---|
105 | errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
|
---|
106 | return usage(stderr);
|
---|
107 | }
|
---|
108 | } while (*++psz);
|
---|
109 | }
|
---|
110 | else if (!strcmp(psz, "-help"))
|
---|
111 | {
|
---|
112 | usage(stdout);
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 | else if (!strcmp(psz, "-version"))
|
---|
116 | return kbuild_version(argv[0]);
|
---|
117 | else
|
---|
118 | break;
|
---|
119 | i++;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Open the output file.
|
---|
124 | */
|
---|
125 | if (i >= argc)
|
---|
126 | {
|
---|
127 | errx(1, "missing filename!");
|
---|
128 | return usage(stderr);
|
---|
129 | }
|
---|
130 | pFile = fopen(argv[i], "a");
|
---|
131 | if (!pFile)
|
---|
132 | return err(1, "failed to open '%s'.", argv[i]);
|
---|
133 |
|
---|
134 | /*
|
---|
135 | * Append the argument strings to the file
|
---|
136 | */
|
---|
137 | fFirst = 1;
|
---|
138 | for (i++; i < argc; i++)
|
---|
139 | {
|
---|
140 | const char *psz = argv[i];
|
---|
141 | size_t cch = strlen(psz);
|
---|
142 | if (!fFirst)
|
---|
143 | fputc(fNewLine ? '\n' : ' ', pFile);
|
---|
144 | #ifndef kmk_builtin_append
|
---|
145 | if (fCommands)
|
---|
146 | {
|
---|
147 | char *pszOldBuf;
|
---|
148 | unsigned cchOldBuf;
|
---|
149 | char *pchEnd;
|
---|
150 |
|
---|
151 | install_variable_buffer(&pszOldBuf, &cchOldBuf);
|
---|
152 |
|
---|
153 | pchEnd = func_commands(variable_buffer, &argv[i], "commands");
|
---|
154 | fwrite(variable_buffer, 1, pchEnd - variable_buffer, pFile);
|
---|
155 |
|
---|
156 | restore_variable_buffer(pszOldBuf, cchOldBuf);
|
---|
157 | }
|
---|
158 | else if (fVariables)
|
---|
159 | {
|
---|
160 | struct variable *pVar = lookup_variable(psz, cch);
|
---|
161 | if (!pVar)
|
---|
162 | continue;
|
---|
163 | if ( pVar->recursive
|
---|
164 | && memchr(pVar->value, '$', pVar->value_length))
|
---|
165 | {
|
---|
166 | char *pszExpanded = allocated_variable_expand(pVar->value);
|
---|
167 | fwrite(pszExpanded, 1, strlen(pszExpanded), pFile);
|
---|
168 | free(pszExpanded);
|
---|
169 | }
|
---|
170 | else
|
---|
171 | fwrite(pVar->value, 1, pVar->value_length, pFile);
|
---|
172 | }
|
---|
173 | else
|
---|
174 | #endif
|
---|
175 | fwrite(psz, 1, cch, pFile);
|
---|
176 | fFirst = 0;
|
---|
177 | }
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Add the newline and close the file.
|
---|
181 | */
|
---|
182 | if ( fputc('\n', pFile) == EOF
|
---|
183 | || ferror(pFile))
|
---|
184 | {
|
---|
185 | fclose(pFile);
|
---|
186 | return errx(1, "error writing to '%s'!", argv[1]);
|
---|
187 | }
|
---|
188 | if (fclose(pFile))
|
---|
189 | return err(1, "failed to fclose '%s'!", argv[1]);
|
---|
190 | return 0;
|
---|
191 | }
|
---|
192 |
|
---|