| 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 |
|
|---|
| 62 | int extended_regexp_flags = 0;
|
|---|
| 63 |
|
|---|
| 64 | /* If set, fflush(stdout) on every line output. */
|
|---|
| 65 | bool unbuffered_output = false;
|
|---|
| 66 |
|
|---|
| 67 | /* If set, don't write out the line unless explicitly told to */
|
|---|
| 68 | bool no_default_output = false;
|
|---|
| 69 |
|
|---|
| 70 | /* If set, reset line counts on every new file. */
|
|---|
| 71 | bool separate_files = false;
|
|---|
| 72 |
|
|---|
| 73 | /* How do we edit files in-place? (we don't if NULL) */
|
|---|
| 74 | char *in_place_extension = NULL;
|
|---|
| 75 |
|
|---|
| 76 | /* Do we need to be pedantically POSIX compliant? */
|
|---|
| 77 | enum posixicity_types posixicity;
|
|---|
| 78 |
|
|---|
| 79 | /* How long should the `l' command's output line be? */
|
|---|
| 80 | countT lcmd_out_line_len = 70;
|
|---|
| 81 |
|
|---|
| 82 | /* The complete compiled SED program that we are going to run: */
|
|---|
| 83 | static struct vector *the_program = NULL;
|
|---|
| 84 |
|
|---|
| 85 | static void usage P_((int));
|
|---|
| 86 | static void
|
|---|
| 87 | usage(status)
|
|---|
| 88 | int status;
|
|---|
| 89 | {
|
|---|
| 90 | FILE *out = status ? stderr : stdout;
|
|---|
| 91 |
|
|---|
| 92 | #ifdef REG_PERL
|
|---|
| 93 | #define PERL_HELP _(" -R, --regexp-perl\n use Perl 5's regular expressions syntax in the script.\n")
|
|---|
| 94 | #else
|
|---|
| 95 | #define PERL_HELP ""
|
|---|
| 96 | #endif
|
|---|
| 97 |
|
|---|
| 98 | fprintf(out, _("\
|
|---|
| 99 | Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\
|
|---|
| 100 | \n"), myname);
|
|---|
| 101 |
|
|---|
| 102 | fprintf(out, _(" -n, --quiet, --silent\n\
|
|---|
| 103 | suppress automatic printing of pattern space\n"));
|
|---|
| 104 | fprintf(out, _(" -e script, --expression=script\n\
|
|---|
| 105 | add the script to the commands to be executed\n"));
|
|---|
| 106 | fprintf(out, _(" -f script-file, --file=script-file\n\
|
|---|
| 107 | add the contents of script-file to the commands to be executed\n"));
|
|---|
| 108 | fprintf(out, _(" -i[SUFFIX], --in-place[=SUFFIX]\n\
|
|---|
| 109 | edit files in place (makes backup if extension supplied)\n"));
|
|---|
| 110 | fprintf(out, _(" -l N, --line-length=N\n\
|
|---|
| 111 | specify the desired line-wrap length for the `l' command\n"));
|
|---|
| 112 | fprintf(out, _(" --posix\n\
|
|---|
| 113 | disable all GNU extensions.\n"));
|
|---|
| 114 | fprintf(out, _(" -r, --regexp-extended\n\
|
|---|
| 115 | use extended regular expressions in the script.\n"));
|
|---|
| 116 | fprintf(out, PERL_HELP);
|
|---|
| 117 | fprintf(out, _(" -s, --separate\n\
|
|---|
| 118 | consider files as separate rather than as a single continuous\n\
|
|---|
| 119 | long stream.\n"));
|
|---|
| 120 | fprintf(out, _(" -u, --unbuffered\n\
|
|---|
| 121 | load minimal amounts of data from the input files and flush\n\
|
|---|
| 122 | the output buffers more often\n"));
|
|---|
| 123 | fprintf(out, _(" --help display this help and exit\n"));
|
|---|
| 124 | fprintf(out, _(" --version output version information and exit\n"));
|
|---|
| 125 | fprintf(out, _("\n\
|
|---|
| 126 | If no -e, --expression, -f, or --file option is given, then the first\n\
|
|---|
| 127 | non-option argument is taken as the sed script to interpret. All\n\
|
|---|
| 128 | remaining arguments are names of input files; if no input files are\n\
|
|---|
| 129 | specified, then the standard input is read.\n\
|
|---|
| 130 | \n"));
|
|---|
| 131 | fprintf(out, _("E-mail bug reports to: %s .\n\
|
|---|
| 132 | Be sure to include the word ``%s'' somewhere in the ``Subject:'' field.\n"),
|
|---|
| 133 | BUG_ADDRESS, PACKAGE);
|
|---|
| 134 |
|
|---|
| 135 | ck_fclose (NULL);
|
|---|
| 136 | exit (status);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | int
|
|---|
| 140 | main(argc, argv)
|
|---|
| 141 | int argc;
|
|---|
| 142 | char **argv;
|
|---|
| 143 | {
|
|---|
| 144 | #ifdef REG_PERL
|
|---|
| 145 | #define SHORTOPTS "snrRue:f:l:i::V:"
|
|---|
| 146 | #else
|
|---|
| 147 | #define SHORTOPTS "snrue:f:l:i::V:"
|
|---|
| 148 | #endif
|
|---|
| 149 |
|
|---|
| 150 | static struct option longopts[] = {
|
|---|
| 151 | {"regexp-extended", 0, NULL, 'r'},
|
|---|
| 152 | #ifdef REG_PERL
|
|---|
| 153 | {"regexp-perl", 0, NULL, 'R'},
|
|---|
| 154 | #endif
|
|---|
| 155 | {"expression", 1, NULL, 'e'},
|
|---|
| 156 | {"file", 1, NULL, 'f'},
|
|---|
| 157 | {"in-place", 2, NULL, 'i'},
|
|---|
| 158 | {"line-length", 1, NULL, 'l'},
|
|---|
| 159 | {"quiet", 0, NULL, 'n'},
|
|---|
| 160 | {"posix", 0, NULL, 'p'},
|
|---|
| 161 | {"silent", 0, NULL, 'n'},
|
|---|
| 162 | {"separate", 0, NULL, 's'},
|
|---|
| 163 | {"unbuffered", 0, NULL, 'u'},
|
|---|
| 164 | {"version", 0, NULL, 'v'},
|
|---|
| 165 | {"help", 0, NULL, 'h'},
|
|---|
| 166 | {NULL, 0, NULL, 0}
|
|---|
| 167 | };
|
|---|
| 168 |
|
|---|
| 169 | int opt;
|
|---|
| 170 | int return_code;
|
|---|
| 171 | const char *cols = getenv("COLS");
|
|---|
| 172 |
|
|---|
| 173 | initialize_main (&argc, &argv);
|
|---|
| 174 | #if HAVE_SETLOCALE
|
|---|
| 175 | /* Set locale according to user's wishes. */
|
|---|
| 176 | #ifdef _MSC_VER
|
|---|
| 177 | {
|
|---|
| 178 | /* The redmond guys doesn't listen to the user in the same way... */
|
|---|
| 179 | const char *lc_all = getenv ("LC_ALL");
|
|---|
| 180 | if (lc_all)
|
|---|
| 181 | setlocale (LC_ALL, lc_all);
|
|---|
| 182 | else
|
|---|
| 183 | {
|
|---|
| 184 | const char *lc_lang = getenv ("LANG");
|
|---|
| 185 | const char *lc_ctype = getenv ("LC_TYPE");
|
|---|
| 186 | const char *lc_collate = getenv ("LC_COLLATE");
|
|---|
| 187 |
|
|---|
| 188 | setlocale (LC_ALL, lc_lang ? lc_lang : "");
|
|---|
| 189 | if (lc_ctype)
|
|---|
| 190 | setlocale (LC_CTYPE, lc_ctype ? lc_ctype : "");
|
|---|
| 191 | if (lc_collate)
|
|---|
| 192 | setlocale (LC_COLLATE, lc_collate ? lc_collate : "");
|
|---|
| 193 | }
|
|---|
| 194 | }
|
|---|
| 195 | #else
|
|---|
| 196 | setlocale (LC_ALL, "");
|
|---|
| 197 | #endif
|
|---|
| 198 | #endif
|
|---|
| 199 | initialize_mbcs ();
|
|---|
| 200 |
|
|---|
| 201 | #if ENABLE_NLS
|
|---|
| 202 |
|
|---|
| 203 | /* Tell program which translations to use and where to find. */
|
|---|
| 204 | bindtextdomain (PACKAGE, LOCALEDIR);
|
|---|
| 205 | textdomain (PACKAGE);
|
|---|
| 206 | #endif
|
|---|
| 207 |
|
|---|
| 208 | if (getenv("POSIXLY_CORRECT") != NULL)
|
|---|
| 209 | posixicity = POSIXLY_CORRECT;
|
|---|
| 210 | else
|
|---|
| 211 | posixicity = POSIXLY_EXTENDED;
|
|---|
| 212 |
|
|---|
| 213 | /* If environment variable `COLS' is set, use its value for
|
|---|
| 214 | the baseline setting of `lcmd_out_line_len'. The "-1"
|
|---|
| 215 | is to avoid gratuitous auto-line-wrap on ttys.
|
|---|
| 216 | */
|
|---|
| 217 | if (cols)
|
|---|
| 218 | {
|
|---|
| 219 | countT t = ATOI(cols);
|
|---|
| 220 | if (t > 1)
|
|---|
| 221 | lcmd_out_line_len = t-1;
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | myname = *argv;
|
|---|
| 225 | while ((opt = getopt_long(argc, argv, SHORTOPTS, longopts, NULL)) != EOF)
|
|---|
| 226 | {
|
|---|
| 227 | switch (opt)
|
|---|
| 228 | {
|
|---|
| 229 | case 'n':
|
|---|
| 230 | no_default_output = true;
|
|---|
| 231 | break;
|
|---|
| 232 | case 'e':
|
|---|
| 233 | the_program = compile_string(the_program, optarg, strlen(optarg));
|
|---|
| 234 | break;
|
|---|
| 235 | case 'f':
|
|---|
| 236 | the_program = compile_file(the_program, optarg);
|
|---|
| 237 | break;
|
|---|
| 238 |
|
|---|
| 239 | case 'i':
|
|---|
| 240 | separate_files = true;
|
|---|
| 241 | if (optarg == NULL)
|
|---|
| 242 | /* use no backups */
|
|---|
| 243 | in_place_extension = ck_strdup ("*");
|
|---|
| 244 |
|
|---|
| 245 | else if (strchr(optarg, '*') != NULL)
|
|---|
| 246 | in_place_extension = ck_strdup(optarg);
|
|---|
| 247 |
|
|---|
| 248 | else
|
|---|
| 249 | {
|
|---|
| 250 | in_place_extension = MALLOC (strlen(optarg) + 2, char);
|
|---|
| 251 | in_place_extension[0] = '*';
|
|---|
| 252 | strcpy (in_place_extension + 1, optarg);
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | break;
|
|---|
| 256 |
|
|---|
| 257 | case 'l':
|
|---|
| 258 | lcmd_out_line_len = ATOI(optarg);
|
|---|
| 259 | break;
|
|---|
| 260 |
|
|---|
| 261 | case 'p':
|
|---|
| 262 | posixicity = POSIXLY_BASIC;
|
|---|
| 263 | break;
|
|---|
| 264 |
|
|---|
| 265 | case 'r':
|
|---|
| 266 | if (extended_regexp_flags)
|
|---|
| 267 | usage(4);
|
|---|
| 268 | extended_regexp_flags = REG_EXTENDED;
|
|---|
| 269 | break;
|
|---|
| 270 |
|
|---|
| 271 | #ifdef REG_PERL
|
|---|
| 272 | case 'R':
|
|---|
| 273 | if (extended_regexp_flags)
|
|---|
| 274 | usage(4);
|
|---|
| 275 | extended_regexp_flags = REG_PERL;
|
|---|
| 276 | break;
|
|---|
| 277 | #endif
|
|---|
| 278 |
|
|---|
| 279 | case 's':
|
|---|
| 280 | separate_files = true;
|
|---|
| 281 | break;
|
|---|
| 282 |
|
|---|
| 283 | case 'u':
|
|---|
| 284 | unbuffered_output = true;
|
|---|
| 285 | break;
|
|---|
| 286 |
|
|---|
| 287 | case 'v':
|
|---|
| 288 | #ifdef REG_PERL
|
|---|
| 289 | fprintf(stdout, _("super-sed version %s\n"), VERSION);
|
|---|
| 290 | fprintf(stdout, _("based on GNU sed version %s\n\n"), SED_FEATURE_VERSION);
|
|---|
| 291 | #else
|
|---|
| 292 | fprintf(stdout, _("GNU sed version %s\n"), VERSION);
|
|---|
| 293 | #endif
|
|---|
| 294 | fprintf(stdout, _("%s\n\
|
|---|
| 295 | This is free software; see the source for copying conditions. There is NO\n\
|
|---|
| 296 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,\n\
|
|---|
| 297 | to the extent permitted by law.\n\
|
|---|
| 298 | "), COPYRIGHT_NOTICE);
|
|---|
| 299 | #ifdef KBUILD_VERSION_MAJOR
|
|---|
| 300 | fprintf(stdout, _("\n\
|
|---|
| 301 | kBuild %d.%d.%d [" __DATE__ " " __TIME__ "]\n\
|
|---|
| 302 | "), KBUILD_VERSION_MAJOR, KBUILD_VERSION_MINOR, KBUILD_VERSION_PATCH);
|
|---|
| 303 | #endif
|
|---|
| 304 |
|
|---|
| 305 | ck_fclose (NULL);
|
|---|
| 306 | exit (0);
|
|---|
| 307 | case 'h':
|
|---|
| 308 | usage(0);
|
|---|
| 309 | default:
|
|---|
| 310 | usage(4);
|
|---|
| 311 | }
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | if (!the_program)
|
|---|
| 315 | {
|
|---|
| 316 | if (optind < argc)
|
|---|
| 317 | {
|
|---|
| 318 | char *arg = argv[optind++];
|
|---|
| 319 | the_program = compile_string(the_program, arg, strlen(arg));
|
|---|
| 320 | }
|
|---|
| 321 | else
|
|---|
| 322 | usage(4);
|
|---|
| 323 | }
|
|---|
| 324 | check_final_program(the_program);
|
|---|
| 325 |
|
|---|
| 326 | return_code = process_files(the_program, argv+optind);
|
|---|
| 327 |
|
|---|
| 328 | finish_program(the_program);
|
|---|
| 329 | ck_fclose(NULL);
|
|---|
| 330 |
|
|---|
| 331 | return return_code;
|
|---|
| 332 | }
|
|---|