VirtualBox

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

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

Added options for sending the output to a file without having to make use of redirection (-o, --output, --output-text, --output-binary).

File size: 9.7 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, --output-text=file, --output-binary=file\n\
122 use the specified file instead of stdout; the first two uses\n\
123 uses the default text/binary mode.\n"));
124#endif
125 fprintf(out, _(" -r, --regexp-extended\n\
126 use extended regular expressions in the script.\n"));
127 fprintf(out, PERL_HELP);
128 fprintf(out, _(" -s, --separate\n\
129 consider files as separate rather than as a single continuous\n\
130 long stream.\n"));
131 fprintf(out, _(" -u, --unbuffered\n\
132 load minimal amounts of data from the input files and flush\n\
133 the output buffers more often\n"));
134 fprintf(out, _(" --help display this help and exit\n"));
135 fprintf(out, _(" --version output version information and exit\n"));
136 fprintf(out, _("\n\
137If no -e, --expression, -f, or --file option is given, then the first\n\
138non-option argument is taken as the sed script to interpret. All\n\
139remaining arguments are names of input files; if no input files are\n\
140specified, then the standard input is read.\n\
141\n"));
142 fprintf(out, _("E-mail bug reports to: %s .\n\
143Be sure to include the word ``%s'' somewhere in the ``Subject:'' field.\n"),
144 BUG_ADDRESS, PACKAGE);
145
146 ck_fclose (NULL);
147 exit (status);
148}
149
150int
151main(argc, argv)
152 int argc;
153 char **argv;
154{
155#ifdef REG_PERL
156# ifndef CONFIG_WITHOUT_O_OPT
157#define SHORTOPTS "snrRue:f:l:i::o:V:"
158# else
159#define SHORTOPTS "snrRue:f:l:i::V:"
160# endif
161#else
162# ifndef CONFIG_WITHOUT_O_OPT
163#define SHORTOPTS "snrue:f:l:i::o:V:"
164# else
165#define SHORTOPTS "snrue:f:l:i::V:"
166# endif
167#endif
168
169 static struct option longopts[] = {
170 {"regexp-extended", 0, NULL, 'r'},
171#ifdef REG_PERL
172 {"regexp-perl", 0, NULL, 'R'},
173#endif
174 {"expression", 1, NULL, 'e'},
175 {"file", 1, NULL, 'f'},
176 {"in-place", 2, NULL, 'i'},
177 {"line-length", 1, NULL, 'l'},
178 {"quiet", 0, NULL, 'n'},
179 {"posix", 0, NULL, 'p'},
180 {"silent", 0, NULL, 'n'},
181 {"separate", 0, NULL, 's'},
182 {"unbuffered", 0, NULL, 'u'},
183#ifndef CONFIG_WITHOUT_O_OPT
184 {"output", 1, NULL, 'o'},
185 {"output-binary", 1, NULL, 300},
186 {"output-text", 1, NULL, 301},
187#endif
188 {"version", 0, NULL, 'v'},
189 {"help", 0, NULL, 'h'},
190 {NULL, 0, NULL, 0}
191 };
192
193 int opt;
194 int return_code;
195 const char *cols = getenv("COLS");
196
197 initialize_main (&argc, &argv);
198#ifndef CONFIG_WITHOUT_O_OPT
199 sed_stdout = stdout;
200#endif
201#if HAVE_SETLOCALE
202 /* Set locale according to user's wishes. */
203#ifdef _MSC_VER
204 {
205 /* The redmond guys doesn't listen to the user in the same way... */
206 const char *lc_all = getenv ("LC_ALL");
207 if (lc_all)
208 setlocale (LC_ALL, lc_all);
209 else
210 {
211 const char *lc_lang = getenv ("LANG");
212 const char *lc_ctype = getenv ("LC_TYPE");
213 const char *lc_collate = getenv ("LC_COLLATE");
214
215 setlocale (LC_ALL, lc_lang ? lc_lang : "");
216 if (lc_ctype)
217 setlocale (LC_CTYPE, lc_ctype ? lc_ctype : "");
218 if (lc_collate)
219 setlocale (LC_COLLATE, lc_collate ? lc_collate : "");
220 }
221 }
222#else
223 setlocale (LC_ALL, "");
224#endif
225#endif
226 initialize_mbcs ();
227
228#if ENABLE_NLS
229
230 /* Tell program which translations to use and where to find. */
231 bindtextdomain (PACKAGE, LOCALEDIR);
232 textdomain (PACKAGE);
233#endif
234
235 if (getenv("POSIXLY_CORRECT") != NULL)
236 posixicity = POSIXLY_CORRECT;
237 else
238 posixicity = POSIXLY_EXTENDED;
239
240 /* If environment variable `COLS' is set, use its value for
241 the baseline setting of `lcmd_out_line_len'. The "-1"
242 is to avoid gratuitous auto-line-wrap on ttys.
243 */
244 if (cols)
245 {
246 countT t = ATOI(cols);
247 if (t > 1)
248 lcmd_out_line_len = t-1;
249 }
250
251 myname = *argv;
252 while ((opt = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != EOF)
253 {
254 switch (opt)
255 {
256 case 'n':
257 no_default_output = true;
258 break;
259 case 'e':
260 the_program = compile_string(the_program, optarg, strlen(optarg));
261 break;
262 case 'f':
263 the_program = compile_file(the_program, optarg);
264 break;
265
266 case 'i':
267 separate_files = true;
268 if (optarg == NULL)
269 /* use no backups */
270 in_place_extension = ck_strdup ("*");
271
272 else if (strchr(optarg, '*') != NULL)
273 in_place_extension = ck_strdup(optarg);
274
275 else
276 {
277 in_place_extension = MALLOC (strlen(optarg) + 2, char);
278 in_place_extension[0] = '*';
279 strcpy (in_place_extension + 1, optarg);
280 }
281
282 break;
283
284 case 'l':
285 lcmd_out_line_len = ATOI(optarg);
286 break;
287
288#ifndef CONFIG_WITHOUT_O_OPT
289 case 'o':
290 sed_stdout = ck_fopen (optarg, "w", true /* fail on error */);
291 break;
292
293 case 300:
294 sed_stdout = ck_fopen (optarg, "wb", true /* fail on error */);
295 break;
296
297 case 301:
298 sed_stdout = ck_fopen (optarg, "wt", true /* fail on error */);
299 break;
300#endif
301
302 case 'p':
303 posixicity = POSIXLY_BASIC;
304 break;
305
306 case 'r':
307 if (extended_regexp_flags)
308 usage(4);
309 extended_regexp_flags = REG_EXTENDED;
310 break;
311
312#ifdef REG_PERL
313 case 'R':
314 if (extended_regexp_flags)
315 usage(4);
316 extended_regexp_flags = REG_PERL;
317 break;
318#endif
319
320 case 's':
321 separate_files = true;
322 break;
323
324 case 'u':
325 unbuffered_output = true;
326 break;
327
328 case 'v':
329#ifdef KBUILD_VERSION_MAJOR
330 fprintf(stdout, _("kmk_sed - kBuild version %d.%d.%d\n"
331 "\n"
332 "Based on "),
333 KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
334#endif
335#ifdef REG_PERL
336 fprintf(stdout, _("super-sed version %s\n"), VERSION);
337 fprintf(stdout, _("based on GNU sed version %s\n\n"), SED_FEATURE_VERSION);
338#else
339 fprintf(stdout, _("GNU sed version %s\n"), VERSION);
340#endif
341 fprintf(stdout, _("%s\n\
342This is free software; see the source for copying conditions. There is NO\n\
343warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
344to the extent permitted by law.\n\
345"), COPYRIGHT_NOTICE);
346
347 ck_fclose (NULL);
348 exit (0);
349 case 'h':
350 usage(0);
351 default:
352 usage(4);
353 }
354 }
355
356 if (!the_program)
357 {
358 if (optind < argc)
359 {
360 char *arg = argv[optind++];
361 the_program = compile_string(the_program, arg, strlen(arg));
362 }
363 else
364 usage(4);
365 }
366 check_final_program(the_program);
367
368 return_code = process_files(the_program, argv+optind);
369
370 finish_program(the_program);
371 ck_fclose(NULL);
372
373 return return_code;
374}
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