source: trunk/src/oldsed/sed/sed.c@ 3610

Last change on this file since 3610 was 3560, checked in by bird, 3 years ago

sed: build fix for non-windows hosts.

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