3 * kMk Builtin command - append text to file.
7 * Copyright (c) 2005-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
9 * This file is part of kBuild.
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 3 of the License, or
14 * (at your option) any later version.
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.
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
26 /*******************************************************************************
28 *******************************************************************************/
29 #ifndef kmk_builtin_append
32 # include "variable.h"
42 #include "kmkbuiltin.h"
46 * Prints the usage and return 1.
48 static int usage(FILE *pf
)
51 "usage: %s [-dcnNtv] file [string ...]\n"
56 " -d Enclose the output in define ... endef, taking the name from\n"
57 " the first argument following the file name.\n"
58 " -c Output the command for specified target(s). [builtin only]\n"
59 " -n Insert a newline between the strings.\n"
60 " -N Suppress the trailing newline.\n"
61 " -t Truncate the file instead of appending\n"
62 " -v Output the value(s) for specified variable(s). [builtin only]\n"
64 g_progname
, g_progname
, g_progname
);
70 * Appends text to a textfile, creating the textfile if necessary.
72 int kmk_builtin_append(int argc
, char **argv
, char **envp
)
79 int fNoTrailingNewline
= 0;
93 && argv
[i
][1] != '\0' /* '-' is a file */
94 && strchr("-cdnNtv", argv
[i
][1]) /* valid option char */
97 char *psz
= &argv
[i
][1];
107 errx(1, "Option '-c' clashes with '-v'.");
108 return usage(stderr
);
110 #ifndef kmk_builtin_append
114 errx(1, "Option '-c' isn't supported in external mode.");
115 return usage(stderr
);
120 errx(1, "Option '-d' must come before '-v'!");
121 return usage(stderr
);
129 fNoTrailingNewline
= 1;
137 errx(1, "Option '-v' clashes with '-c'.");
138 return usage(stderr
);
140 #ifndef kmk_builtin_append
144 errx(1, "Option '-v' isn't supported in external mode.");
145 return usage(stderr
);
148 errx(1, "Invalid option '%c'! (%s)", *psz
, argv
[i
]);
149 return usage(stderr
);
153 else if (!strcmp(psz
, "-help"))
158 else if (!strcmp(psz
, "-version"))
159 return kbuild_version(argv
[0]);
165 if (i
+ fDefine
>= argc
)
168 errx(1, "missing filename!");
170 errx(1, "missing define name!");
171 return usage(stderr
);
175 * Open the output file.
178 pFile
= fopen(argv
[i
], fTruncate
? "w" : "a");
180 return err(1, "failed to open '%s'.", argv
[i
]);
188 fprintf(pFile
, "define %s\n", argv
[i
]);
192 * Append the argument strings to the file
195 for (i
++; i
< argc
; i
++)
197 const char *psz
= argv
[i
];
198 size_t cch
= strlen(psz
);
200 fputc(fNewline
? '\n' : ' ', pFile
);
201 #ifndef kmk_builtin_append
208 install_variable_buffer(&pszOldBuf
, &cchOldBuf
);
210 pchEnd
= func_commands(variable_buffer
, &argv
[i
], "commands");
211 fwrite(variable_buffer
, 1, pchEnd
- variable_buffer
, pFile
);
213 restore_variable_buffer(pszOldBuf
, cchOldBuf
);
217 struct variable
*pVar
= lookup_variable(psz
, cch
);
221 && memchr(pVar
->value
, '$', pVar
->value_length
))
223 char *pszExpanded
= allocated_variable_expand(pVar
->value
);
224 fwrite(pszExpanded
, 1, strlen(pszExpanded
), pFile
);
228 fwrite(pVar
->value
, 1, pVar
->value_length
, pFile
);
232 fwrite(psz
, 1, cch
, pFile
);
242 fwrite("\nendef", 1, sizeof("\nendef") - 1, pFile
);
244 fwrite("endef", 1, sizeof("endef") - 1, pFile
);
248 * Add the final newline (unless supressed) and close the file.
250 if ( ( !fNoTrailingNewline
251 && fputc('\n', pFile
) == EOF
)
255 return errx(1, "error writing to '%s'!", argv
[iFile
]);
258 return err(1, "failed to fclose '%s'!", argv
[iFile
]);