VirtualBox

source: kBuild/trunk/src/sed/sed/sed.c@ 1332

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

Added some append options too.

File size: 10.2 KB
Line 
1#define COPYRIGHT_NOTICE "Copyright (C) 2003 Free Software Foundation, Inc."
2#define BUG_ADDRESS "bonzini@gnu.org"
3
4/* GNU SED, a batch stream editor.
5 Copyright (C) 1989,90,91,92,93,94,95,98,99,2002,2003
6 Free Software Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21
22
23#include "sed.h"
24
25
26#include <stdio.h>
27#ifdef HAVE_STRINGS_H
28# include <strings.h>
29#else
30# include <string.h>
31#endif /*HAVE_STRINGS_H*/
32#ifdef HAVE_MEMORY_H
33# include <memory.h>
34#endif
35
36#ifndef HAVE_STRCHR
37# define strchr index
38# define strrchr rindex
39#endif
40
41#ifdef HAVE_STDLIB_H
42# include <stdlib.h>
43#endif
44
45#ifdef HAVE_SYS_TYPES_H
46# include <sys/types.h>
47#endif
48#include "getopt.h"
49
50#ifndef BOOTSTRAP
51#ifndef HAVE_STDLIB_H
52 extern char *getenv P_((const char *));
53#endif
54#endif
55
56#ifndef HAVE_STRTOUL
57# define ATOI(x) atoi(x)
58#else
59# define ATOI(x) strtoul(x, NULL, 0)
60#endif
61
62int extended_regexp_flags = 0;
63
64#ifndef CONFIG_WITHOUT_O_OPT
65/* The output file, defaults to stdout but can be overridden
66 by the -o or --output option. main sets this to avoid problems. */
67FILE *sed_stdout = NULL;
68#endif
69
70/* If set, fflush(stdout) on every line output. */
71bool unbuffered_output = false;
72
73/* If set, don't write out the line unless explicitly told to */
74bool no_default_output = false;
75
76/* If set, reset line counts on every new file. */
77bool separate_files = false;
78
79/* How do we edit files in-place? (we don't if NULL) */
80char *in_place_extension = NULL;
81
82/* Do we need to be pedantically POSIX compliant? */
83enum posixicity_types posixicity;
84
85/* How long should the `l' command's output line be? */
86countT lcmd_out_line_len = 70;
87
88/* The complete compiled SED program that we are going to run: */
89static struct vector *the_program = NULL;
90
91static void usage P_((int));
92static void
93usage(status)
94 int status;
95{
96 FILE *out = status ? stderr : stdout;
97
98#ifdef REG_PERL
99#define PERL_HELP _(" -R, --regexp-perl\n use Perl 5's regular expressions syntax in the script.\n")
100#else
101#define PERL_HELP ""
102#endif
103
104 fprintf(out, _("\
105Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\
106\n"), myname);
107
108 fprintf(out, _(" -n, --quiet, --silent\n\
109 suppress automatic printing of pattern space\n"));
110 fprintf(out, _(" -e script, --expression=script\n\
111 add the script to the commands to be executed\n"));
112 fprintf(out, _(" -f script-file, --file=script-file\n\
113 add the contents of script-file to the commands to be executed\n"));
114 fprintf(out, _(" -i[SUFFIX], --in-place[=SUFFIX]\n\
115 edit files in place (makes backup if extension supplied)\n"));
116 fprintf(out, _(" -l N, --line-length=N\n\
117 specify the desired line-wrap length for the `l' command\n"));
118 fprintf(out, _(" --posix\n\
119 disable all GNU extensions.\n"));
120#ifndef CONFIG_WITHOUT_O_OPT
121 fprintf(out, _(" -o, --output=file, --append=file, --output-text=file,\n"));
122 fprintf(out, _(" --output-binary=file, --append-text=file, append-binary=file\n\
123 use the specified file instead of stdout; the first\n\
124 three uses the default text/binary mode.\n"));
125#endif
126 fprintf(out, _(" -r, --regexp-extended\n\
127 use extended regular expressions in the script.\n"));
128 fprintf(out, PERL_HELP);
129 fprintf(out, _(" -s, --separate\n\
130 consider files as separate rather than as a single continuous\n\
131 long stream.\n"));
132 fprintf(out, _(" -u, --unbuffered\n\
133 load minimal amounts of data from the input files and flush\n\
134 the output buffers more often\n"));
135 fprintf(out, _(" --help display this help and exit\n"));
136 fprintf(out, _(" --version output version information and exit\n"));
137 fprintf(out, _("\n\
138If no -e, --expression, -f, or --file option is given, then the first\n\
139non-option argument is taken as the sed script to interpret. All\n\
140remaining arguments are names of input files; if no input files are\n\
141specified, then the standard input is read.\n\
142\n"));
143 fprintf(out, _("E-mail bug reports to: %s .\n\
144Be sure to include the word ``%s'' somewhere in the ``Subject:'' field.\n"),
145 BUG_ADDRESS, PACKAGE);
146
147 ck_fclose (NULL);
148 exit (status);
149}
150
151int
152main(argc, argv)
153 int argc;
154 char **argv;
155{
156#ifdef REG_PERL
157# ifndef CONFIG_WITHOUT_O_OPT
158#define SHORTOPTS "snrRue:f:l:i::o:V:"
159# else
160#define SHORTOPTS "snrRue:f:l:i::V:"
161# endif
162#else
163# ifndef CONFIG_WITHOUT_O_OPT
164#define SHORTOPTS "snrue:f:l:i::o:V:"
165# else
166#define SHORTOPTS "snrue:f:l:i::V:"
167# endif
168#endif
169
170 static struct option longopts[] = {
171 {"regexp-extended", 0, NULL, 'r'},
172#ifdef REG_PERL
173 {"regexp-perl", 0, NULL, 'R'},
174#endif
175 {"expression", 1, NULL, 'e'},
176 {"file", 1, NULL, 'f'},
177 {"in-place", 2, NULL, 'i'},
178 {"line-length", 1, NULL, 'l'},
179 {"quiet", 0, NULL, 'n'},
180 {"posix", 0, NULL, 'p'},
181 {"silent", 0, NULL, 'n'},
182 {"separate", 0, NULL, 's'},
183 {"unbuffered", 0, NULL, 'u'},
184#ifndef CONFIG_WITHOUT_O_OPT
185 {"output", 1, NULL, 'o'},
186 {"output-binary", 1, NULL, 300},
187 {"output-text", 1, NULL, 301},
188 {"append", 1, NULL, 302},
189 {"append-binary", 1, NULL, 303},
190 {"append-text", 1, NULL, 304},
191#endif
192 {"version", 0, NULL, 'v'},
193 {"help", 0, NULL, 'h'},
194 {NULL, 0, NULL, 0}
195 };
196
197 int opt;
198 int return_code;
199 const char *cols = getenv("COLS");
200
201 initialize_main (&argc, &argv);
202#ifndef CONFIG_WITHOUT_O_OPT
203 sed_stdout = stdout;
204#endif
205#if HAVE_SETLOCALE
206 /* Set locale according to user's wishes. */
207#ifdef _MSC_VER
208 {
209 /* The redmond guys doesn't listen to the user in the same way... */
210 const char *lc_all = getenv ("LC_ALL");
211 if (lc_all)
212 setlocale (LC_ALL, lc_all);
213 else
214 {
215 const char *lc_lang = getenv ("LANG");
216 const char *lc_ctype = getenv ("LC_TYPE");
217 const char *lc_collate = getenv ("LC_COLLATE");
218
219 setlocale (LC_ALL, lc_lang ? lc_lang : "");
220 if (lc_ctype)
221 setlocale (LC_CTYPE, lc_ctype ? lc_ctype : "");
222 if (lc_collate)
223 setlocale (LC_COLLATE, lc_collate ? lc_collate : "");
224 }
225 }
226#else
227 setlocale (LC_ALL, "");
228#endif
229#endif
230 initialize_mbcs ();
231
232#if ENABLE_NLS
233
234 /* Tell program which translations to use and where to find. */
235 bindtextdomain (PACKAGE, LOCALEDIR);
236 textdomain (PACKAGE);
237#endif
238
239 if (getenv("POSIXLY_CORRECT") != NULL)
240 posixicity = POSIXLY_CORRECT;
241 else
242 posixicity = POSIXLY_EXTENDED;
243
244 /* If environment variable `COLS' is set, use its value for
245 the baseline setting of `lcmd_out_line_len'. The "-1"
246 is to avoid gratuitous auto-line-wrap on ttys.
247 */
248 if (cols)
249 {
250 countT t = ATOI(cols);
251 if (t > 1)
252 lcmd_out_line_len = t-1;
253 }
254
255 myname = *argv;
256 while ((opt = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != EOF)
257 {
258 switch (opt)
259 {
260 case 'n':
261 no_default_output = true;
262 break;
263 case 'e':
264 the_program = compile_string(the_program, optarg, strlen(optarg));
265 break;
266 case 'f':
267 the_program = compile_file(the_program, optarg);
268 break;
269
270 case 'i':
271 separate_files = true;
272 if (optarg == NULL)
273 /* use no backups */
274 in_place_extension = ck_strdup ("*");
275
276 else if (strchr(optarg, '*') != NULL)
277 in_place_extension = ck_strdup(optarg);
278
279 else
280 {
281 in_place_extension = MALLOC (strlen(optarg) + 2, char);
282 in_place_extension[0] = '*';
283 strcpy (in_place_extension + 1, optarg);
284 }
285
286 break;
287
288 case 'l':
289 lcmd_out_line_len = ATOI(optarg);
290 break;
291
292#ifndef CONFIG_WITHOUT_O_OPT
293 case 'o':
294 sed_stdout = ck_fopen (optarg, "w", true /* fail on error */);
295 break;
296
297 case 300:
298 sed_stdout = ck_fopen (optarg, "wb", true /* fail on error */);
299 break;
300
301 case 301:
302 sed_stdout = ck_fopen (optarg, "wt", true /* fail on error */);
303 break;
304
305 case 302:
306 sed_stdout = ck_fopen (optarg, "a", true /* fail on error */);
307 break;
308
309 case 303:
310 sed_stdout = ck_fopen (optarg, "ab", true /* fail on error */);
311 break;
312
313 case 304:
314 sed_stdout = ck_fopen (optarg, "at", true /* fail on error */);
315 break;
316#endif
317
318 case 'p':
319 posixicity = POSIXLY_BASIC;
320 break;
321
322 case 'r':
323 if (extended_regexp_flags)
324 usage(4);
325 extended_regexp_flags = REG_EXTENDED;
326 break;
327
328#ifdef REG_PERL
329 case 'R':
330 if (extended_regexp_flags)
331 usage(4);
332 extended_regexp_flags = REG_PERL;
333 break;
334#endif
335
336 case 's':
337 separate_files = true;
338 break;
339
340 case 'u':
341 unbuffered_output = true;
342 break;
343
344 case 'v':
345#ifdef KBUILD_VERSION_MAJOR
346 fprintf(stdout, _("kmk_sed - kBuild version %d.%d.%d\n"
347 "\n"
348 "Based on "),
349 KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
350#endif
351#ifdef REG_PERL
352 fprintf(stdout, _("super-sed version %s\n"), VERSION);
353 fprintf(stdout, _("based on GNU sed version %s\n\n"), SED_FEATURE_VERSION);
354#else
355 fprintf(stdout, _("GNU sed version %s\n"), VERSION);
356#endif
357 fprintf(stdout, _("%s\n\
358This is free software; see the source for copying conditions. There is NO\n\
359warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
360to the extent permitted by law.\n\
361"), COPYRIGHT_NOTICE);
362
363 ck_fclose (NULL);
364 exit (0);
365 case 'h':
366 usage(0);
367 default:
368 usage(4);
369 }
370 }
371
372 if (!the_program)
373 {
374 if (optind < argc)
375 {
376 char *arg = argv[optind++];
377 the_program = compile_string(the_program, arg, strlen(arg));
378 }
379 else
380 usage(4);
381 }
382 check_final_program(the_program);
383
384 return_code = process_files(the_program, argv+optind);
385
386 finish_program(the_program);
387 ck_fclose(NULL);
388
389 return return_code;
390}
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