VirtualBox

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

Last change on this file since 1183 was 1183, checked in by bird, 17 years ago

Added --version and --help to all builtins.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
1/* $Id: append.c 1183 2007-10-05 22:16:46Z 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 [-nv] 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 */
55int 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#endif
64
65 g_progname = argv[0];
66
67 /*
68 * Parse options.
69 */
70 i = 1;
71 while (i < argc
72 && argv[i][0] == '-'
73 && argv[i][1] != '\0' /* '-' is a file */
74 && strchr("-nv", argv[i][1]) /* valid option char */
75 )
76 {
77 char *psz = &argv[i][1];
78 if (*psz != '-')
79 {
80 do
81 {
82 switch (*psz)
83 {
84 case 'n':
85 fNewLine = 1;
86 break;
87 case 'v':
88#ifndef kmk_builtin_append
89 fVariables = 1;
90 break;
91#else
92 errx(1, "Option '-v' isn't supported in external mode.");
93 return usage(stderr);
94#endif
95 default:
96 errx(1, "Invalid option '%c'! (%s)", *psz, argv[i]);
97 return usage(stderr);
98 }
99 } while (*++psz);
100 }
101 else if (!strcmp(psz, "-help"))
102 {
103 usage(stdout);
104 return 0;
105 }
106 else if (!strcmp(psz, "-version"))
107 return kbuild_version(argv[0]);
108 else
109 break;
110 i++;
111 }
112
113 /*
114 * Open the output file.
115 */
116 if (i >= argc)
117 {
118 errx(1, "missing filename!");
119 return usage(stderr);
120 }
121 pFile = fopen(argv[i], "a");
122 if (!pFile)
123 return err(1, "failed to open '%s'.", argv[i]);
124
125 /*
126 * Append the argument strings to the file
127 */
128 fFirst = 1;
129 for (i++; i < argc; i++)
130 {
131 const char *psz = argv[i];
132 size_t cch = strlen(psz);
133 if (!fFirst)
134 fputc(fNewLine ? '\n' : ' ', pFile);
135#ifndef kmk_builtin_append
136 if (fVariables)
137 {
138 struct variable *pVar = lookup_variable(psz, cch);
139 if (!pVar)
140 continue;
141 if ( pVar->recursive
142 && memchr(pVar->value, '$', pVar->value_length))
143 {
144 char *pszExpanded = allocated_variable_expand(pVar->value);
145 fwrite(pszExpanded, 1, strlen(pszExpanded), pFile);
146 free(pszExpanded);
147 }
148 else
149 fwrite(pVar->value, 1, pVar->value_length, pFile);
150 }
151 else
152#endif
153 fwrite(psz, 1, cch, pFile);
154 fFirst = 0;
155 }
156
157 /*
158 * Add the newline and close the file.
159 */
160 if ( fputc('\n', pFile) == EOF
161 || ferror(pFile))
162 {
163 fclose(pFile);
164 return errx(1, "error writing to '%s'!", argv[1]);
165 }
166 if (fclose(pFile))
167 return err(1, "failed to fclose '%s'!", argv[1]);
168 return 0;
169}
170
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