VirtualBox

source: kBuild/trunk/src/kmk/kmkbuiltin/append.c@ 2012

Last change on this file since 2012 was 1695, checked in by bird, 16 years ago

append: Added the -d and -t options for writing define...endef and truncating respectively.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.8 KB
Line 
1/* $Id: append.c 1695 2008-08-31 20:24:55Z 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 */
41static int usage(FILE *pf)
42{
43 fprintf(pf,
44 "usage: %s [-dcntv] file [string ...]\n"
45 " or: %s --version\n"
46 " or: %s --help\n"
47 "\n"
48 "Options:\n"
49 " -d Enclose the output in define ... endef, taking the name from\n"
50 " the first argument following the file name.\n"
51 " -c Output the command for specified target(s). [builtin only]\n"
52 " -n Insert a new line between the strings.\n"
53 " -t Truncate the file instead of appending\n"
54 " -v Output the value(s) for specified variable(s). [builtin only]\n"
55 ,
56 g_progname, g_progname, g_progname);
57 return 1;
58}
59
60
61/**
62 * Appends text to a textfile, creating the textfile if necessary.
63 */
64int kmk_builtin_append(int argc, char **argv, char **envp)
65{
66 int i;
67 int fFirst;
68 int iFile;
69 FILE *pFile;
70 int fNewLine = 0;
71 int fTruncate = 0;
72 int fDefine = 0;
73 int fVariables = 0;
74 int fCommands = 0;
75
76 g_progname = argv[0];
77
78 /*
79 * Parse options.
80 */
81 i = 1;
82 while (i < argc
83 && argv[i][0] == '-'
84 && argv[i][1] != '\0' /* '-' is a file */
85 && strchr("-cdntv", argv[i][1]) /* valid option char */
86 )
87 {
88 char *psz = &argv[i][1];
89 if (*psz != '-')
90 {
91 do
92 {
93 switch (*psz)
94 {
95 case 'c':
96 if (fVariables)
97 {
98 errx(1, "Option '-c' clashes with '-v'.");
99 return usage(stderr);
100 }
101#ifndef kmk_builtin_append
102 fCommands = 1;
103 break;
104#else
105 errx(1, "Option '-c' isn't supported in external mode.");
106 return usage(stderr);
107#endif
108 case 'd':
109 if (fVariables)
110 {
111 errx(1, "Option '-d' must come before '-v'!");
112 return usage(stderr);
113 }
114 fDefine = 1;
115 break;
116 case 'n':
117 fNewLine = 1;
118 break;
119 case 't':
120 fTruncate = 1;
121 break;
122 case 'v':
123 if (fCommands)
124 {
125 errx(1, "Option '-v' clashes with '-c'.");
126 return usage(stderr);
127 }
128#ifndef kmk_builtin_append
129 fVariables = 1;
130 break;
131#else
132 errx(1, "Option '-v' isn't supported in external mode.");
133 return usage(stderr);
134#endif
135 default:
136 errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
137 return usage(stderr);
138 }
139 } while (*++psz);
140 }
141 else if (!strcmp(psz, "-help"))
142 {
143 usage(stdout);
144 return 0;
145 }
146 else if (!strcmp(psz, "-version"))
147 return kbuild_version(argv[0]);
148 else
149 break;
150 i++;
151 }
152
153 if (i + fDefine >= argc)
154 {
155 if (i <= argc)
156 errx(1, "missing filename!");
157 else
158 errx(1, "missing define name!");
159 return usage(stderr);
160 }
161
162 /*
163 * Open the output file.
164 */
165 iFile = i;
166 pFile = fopen(argv[i], fTruncate ? "w" : "a");
167 if (!pFile)
168 return err(1, "failed to open '%s'.", argv[i]);
169
170 /*
171 * Start define?
172 */
173 if (fDefine)
174 {
175 i++;
176 fprintf(pFile, "define %s\n", argv[i]);
177 }
178
179 /*
180 * Append the argument strings to the file
181 */
182 fFirst = 1;
183 for (i++; i < argc; i++)
184 {
185 const char *psz = argv[i];
186 size_t cch = strlen(psz);
187 if (!fFirst)
188 fputc(fNewLine ? '\n' : ' ', pFile);
189#ifndef kmk_builtin_append
190 if (fCommands)
191 {
192 char *pszOldBuf;
193 unsigned cchOldBuf;
194 char *pchEnd;
195
196 install_variable_buffer(&pszOldBuf, &cchOldBuf);
197
198 pchEnd = func_commands(variable_buffer, &argv[i], "commands");
199 fwrite(variable_buffer, 1, pchEnd - variable_buffer, pFile);
200
201 restore_variable_buffer(pszOldBuf, cchOldBuf);
202 }
203 else if (fVariables)
204 {
205 struct variable *pVar = lookup_variable(psz, cch);
206 if (!pVar)
207 continue;
208 if ( pVar->recursive
209 && memchr(pVar->value, '$', pVar->value_length))
210 {
211 char *pszExpanded = allocated_variable_expand(pVar->value);
212 fwrite(pszExpanded, 1, strlen(pszExpanded), pFile);
213 free(pszExpanded);
214 }
215 else
216 fwrite(pVar->value, 1, pVar->value_length, pFile);
217 }
218 else
219#endif
220 fwrite(psz, 1, cch, pFile);
221 fFirst = 0;
222 }
223
224 /*
225 * Add the newline, closing the define if needed, and close the file.
226 */
227 if ( ( fDefine
228 ? fwrite(fFirst ? "endef\n" : "\nendef\n",
229 1,
230 fFirst ? sizeof("endef\n") - 1 : sizeof("\nendef\n") - 1,
231 pFile) < sizeof("endef\n")
232 : fputc('\n', pFile) == EOF)
233 || ferror(pFile))
234 {
235 fclose(pFile);
236 return errx(1, "error writing to '%s'!", argv[iFile]);
237 }
238 if (fclose(pFile))
239 return err(1, "failed to fclose '%s'!", argv[iFile]);
240 return 0;
241}
242
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette