VirtualBox

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

Last change on this file since 2506 was 2453, checked in by bird, 13 years ago

sed: Added --lang_c, fixing #87.

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