From b8d58d678d51b3becd9ee80f515e801f6eca3f55 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Sun, 17 Dec 2006 14:20:37 +0000 Subject: [PATCH] merge from CVS sed Patches applied: * bonzini@gnu.org--2004b/sed--stable--4.1--patch-83 don't download bad translations * bonzini@gnu.org--2004b/sed--stable--4.1--patch-84 --posix disables all extensions to regular expressions * bonzini@gnu.org--2004b/sed--stable--4.1--patch-85 bump version number to 4.1b * bonzini@gnu.org--2004b/sed--stable--4.1--patch-86 document --follow-symlinks * bonzini@gnu.org--2004b/sed--stable--4.1--patch-87 fixes from Ralf Wildenhues git-archimport-id: bonzini@gnu.org--2004b/sed--ssed--3.61--patch-35 --- ChangeLog | 12 +++++++ autoboot | 2 +- configure.ac | 7 +++++ doc/sed-in.texi | 44 ++++++++++++++++---------- doc/sed.1 | 4 +++ doc/sed.texi | 44 ++++++++++++++++---------- po/it.po | 31 +++++++++++------- sed/execute.c | 13 ++++++-- sed/regexp.c | 23 +++++++++----- sed/sed.c | 14 +++++++++ sed/sed.h | 3 ++ sed/utils.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ sed/utils.h | 1 + 13 files changed, 226 insertions(+), 56 deletions(-) diff --git a/ChangeLog b/ChangeLog index 646b8e5..a30d59d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2006-12-17 Paolo Bonzini + + * sed/utils.c: Include limits.h. + +2006-12-16 Paolo Bonzini + + * doc/sed-in.texi: Document --follow-symlinks. + +2006-12-15 Paolo Bonzini + + * sed/regexp.c: Disable all extensions on --posix. + 2006-09-24 Paolo Bonzini * sed/execute.c: Support ACLs. diff --git a/autoboot b/autoboot index 8d0fa87..c0552cd 100644 --- a/autoboot +++ b/autoboot @@ -88,7 +88,7 @@ case ${GNULIB_SRCDIR--} in esac gnulib_tool=$GNULIB_SRCDIR/gnulib-tool -<$gnulib_tool || exit +test -x $gnulib_tool || exit # $gnulib_tool --import --m4-base=build-aux --lib=libsed --no-changelog --makefile-name=gnulib.mk \ # acl alloca getline getopt gettext memchr memcmp memmove \ diff --git a/configure.ac b/configure.ac index 8d9169e..f44ddcf 100644 --- a/configure.ac +++ b/configure.ac @@ -67,6 +67,13 @@ enable_html=no) AM_CONDITIONAL(BUILD_HTML, test "x$enable_html" != xno) +# Check whether we are able to follow symlinks +AC_CHECK_FUNC(lstat, have_lstat=yes) +AC_CHECK_FUNC(readlink, have_readlink=yes) +if test "x$have_lstat" = xyes -a "x$have_readlink" = xyes; then + AC_DEFINE(ENABLE_FOLLOW_SYMLINKS, ,[Follow symlinks when processing in place]) +fi + : ${TEXI2HTML=texi2html -monolithic} AC_SUBST(TEXI2HTML) diff --git a/doc/sed-in.texi b/doc/sed-in.texi index 32caefd..330854b 100644 --- a/doc/sed-in.texi +++ b/doc/sed-in.texi @@ -215,6 +215,22 @@ These options disable this automatic printing, and @command{sed} only produces output when explicitly told to via the @code{p} command. +@item -e @var{script} +@itemx --expression=@var{script} +@opindex -e +@opindex --expression +@cindex Script, from command line +Add the commands in @var{script} to the set of commands to be +run while processing the input. + +@item -f @var{script-file} +@itemx --file=@var{script-file} +@opindex -f +@opindex --file +@cindex Script, from a file +Add the commands contained in the file @var{script-file} +to the set of commands to be run while processing the input. + @item -i[@var{SUFFIX}] @itemx --in-place[=@var{SUFFIX}] @opindex -i @@ -281,7 +297,7 @@ to a non-empty value. @itemx --binary @opindex -b @opindex --binary -This option is available on any platforms, but is only effective where the +This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made---as is the case for MS-DOS, Windows, Cygwin---text files are composed of lines separated by a carriage return @@ -290,6 +306,16 @@ ending CR. When this option is specified, @command{sed} will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed. +@item --follow-symlinks +@opindex --follow-symlinks +This option is available only on platforms that support +symbolic links and has an effect only if option @option{-i} +is specified. In this case, if the file that is specified +on the command line is a symbolic link, @command{sed} will +follow the link and edit the ultimate destination of the +link. The default behavior is to break the symbolic link, +so that the link destination will not be modified. + @item -r @itemx --regexp-extended @opindex -r @@ -339,22 +365,6 @@ Buffer both input and output as minimally as practical. the likes of @samp{tail -f}, and you wish to see the transformed output as soon as possible.) -@item -e @var{script} -@itemx --expression=@var{script} -@opindex -e -@opindex --expression -@cindex Script, from command line -Add the commands in @var{script} to the set of commands to be -run while processing the input. - -@item -f @var{script-file} -@itemx --file=@var{script-file} -@opindex -f -@opindex --file -@cindex Script, from a file -Add the commands contained in the file @var{script-file} -to the set of commands to be run while processing the input. - @end table If no @option{-e}, @option{-f}, @option{--expression}, or @option{--file} diff --git a/doc/sed.1 b/doc/sed.1 index 6ce741d..a108222 100644 --- a/doc/sed.1 +++ b/doc/sed.1 @@ -32,6 +32,10 @@ add the script to the commands to be executed .IP add the contents of script-file to the commands to be executed .HP +\fB\-\-follow\-symlinks\fR +.IP +follow symlinks when processing in place +.HP \fB\-i[SUFFIX]\fR, \fB\-\-in\-place\fR[=\fISUFFIX\fR] .IP edit files in place (makes backup if extension supplied) diff --git a/doc/sed.texi b/doc/sed.texi index e274b6f..7d1d06b 100644 --- a/doc/sed.texi +++ b/doc/sed.texi @@ -216,6 +216,22 @@ These options disable this automatic printing, and @command{sed} only produces output when explicitly told to via the @code{p} command. +@item -e @var{script} +@itemx --expression=@var{script} +@opindex -e +@opindex --expression +@cindex Script, from command line +Add the commands in @var{script} to the set of commands to be +run while processing the input. + +@item -f @var{script-file} +@itemx --file=@var{script-file} +@opindex -f +@opindex --file +@cindex Script, from a file +Add the commands contained in the file @var{script-file} +to the set of commands to be run while processing the input. + @item -i[@var{SUFFIX}] @itemx --in-place[=@var{SUFFIX}] @opindex -i @@ -282,7 +298,7 @@ to a non-empty value. @itemx --binary @opindex -b @opindex --binary -This option is available on any platforms, but is only effective where the +This option is available on every platform, but is only effective where the operating system makes a distinction between text files and binary files. When such a distinction is made---as is the case for MS-DOS, Windows, Cygwin---text files are composed of lines separated by a carriage return @@ -291,6 +307,16 @@ ending CR. When this option is specified, @command{sed} will open input files in binary mode, thus not requesting this special processing and considering lines to end at a line feed. +@item --follow-symlinks +@opindex --follow-symlinks +This option is available only on platforms that support +symbolic links and has an effect only if option @option{-i} +is specified. In this case, if the file that is specified +on the command line is a symbolic link, @command{sed} will +follow the link and edit the ultimate destination of the +link. The default behavior is to break the symbolic link, +so that the link destination will not be modified. + @item -r @itemx --regexp-extended @opindex -r @@ -340,22 +366,6 @@ Buffer both input and output as minimally as practical. the likes of @samp{tail -f}, and you wish to see the transformed output as soon as possible.) -@item -e @var{script} -@itemx --expression=@var{script} -@opindex -e -@opindex --expression -@cindex Script, from command line -Add the commands in @var{script} to the set of commands to be -run while processing the input. - -@item -f @var{script-file} -@itemx --file=@var{script-file} -@opindex -f -@opindex --file -@cindex Script, from a file -Add the commands contained in the file @var{script-file} -to the set of commands to be run while processing the input. - @end table If no @option{-e}, @option{-f}, @option{--expression}, or @option{--file} diff --git a/po/it.po b/po/it.po index 9b61ba7..8f42351 100644 --- a/po/it.po +++ b/po/it.po @@ -378,7 +378,7 @@ msgstr "" " -R, --regexp-perl\n" " usa la sintassi Perl 5 per le espressioni regolari\n" -#: sed/sed.c:103 +#: sed/sed.c:106 #, c-format msgid "" "Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n" @@ -388,7 +388,7 @@ msgstr "" "file]...\n" "\n" -#: sed/sed.c:109 +#: sed/sed.c:112 #, c-format msgid "" " -n, --quiet, --silent\n" @@ -397,7 +397,7 @@ msgstr "" " -n, --quiet, --silent\n" " sopprime la stampa automatica del pattern space\n" -#: sed/sed.c:111 +#: sed/sed.c:114 #, c-format msgid "" " -e script, --expression=script\n" @@ -417,7 +417,16 @@ msgstr "" " aggiunge il contenuto di file-script ai comandi da " "eseguire\n" -#: sed/sed.c:113 +#: sed/sed.c:117 +#, c-format +msgid "" +" --follow-symlinks\n" +" follow symlinks when processing in place\n" +msgstr "" +" --follow-symlinks\n" +" segue i link simbolici quando viene utilizzato -i\n" + +#: sed/sed.c:120 #, c-format msgid "" " -i[SUFFIX], --in-place[=SUFFIX]\n" @@ -427,7 +436,7 @@ msgstr "" " scrive il risultato sul file originale (facendo una copia\n" " se è fornita un'estensione)\n" -#: sed/sed.c:116 +#: sed/sed.c:123 #, c-format msgid "" " -b, --binary\n" @@ -448,7 +457,7 @@ msgstr "" " specifica la lunghezza delle linee generate dal comando " "`l'\n" -#: sed/sed.c:121 +#: sed/sed.c:128 #, c-format msgid "" " --posix\n" @@ -466,7 +475,7 @@ msgstr "" " -r, --regexp-extended\n" " usa la sintassi di `egrep' per le espressioni regolari\n" -#: sed/sed.c:126 +#: sed/sed.c:133 #, c-format msgid "" " -s, --separate\n" @@ -478,7 +487,7 @@ msgstr "" " considera i file di input come separati invece che come un\n" " unico file lungo.\n" -#: sed/sed.c:129 +#: sed/sed.c:136 #, c-format msgid "" " -u, --unbuffered\n" @@ -489,17 +498,17 @@ msgstr "" " -u, --unbuffered\n" " carica e visualizza i dati una a pezzetti piu' piccoli\n" -#: sed/sed.c:132 +#: sed/sed.c:139 #, c-format msgid " --help display this help and exit\n" msgstr " --help mostra questo aiuto ed esce\n" -#: sed/sed.c:133 +#: sed/sed.c:140 #, c-format msgid " --version output version information and exit\n" msgstr " --version stampa le informazioni sulla versione ed esce\n" -#: sed/sed.c:134 +#: sed/sed.c:141 #, c-format msgid "" "\n" diff --git a/sed/execute.c b/sed/execute.c index 0ca2ca5..6b6632c 100644 --- a/sed/execute.c +++ b/sed/execute.c @@ -762,15 +762,22 @@ closedown(input) if (in_place_extension && output_file.fp != NULL) { + const char *target_name; ck_fclose (output_file.fp); + + if (follow_symlinks) + target_name = follow_symlink (input->in_file_name); + else + target_name = input->in_file_name; + if (strcmp(in_place_extension, "*") != 0) { - char *backup_file_name = get_backup_file_name(input->in_file_name); - ck_rename (input->in_file_name, backup_file_name, input->out_file_name); + char *backup_file_name = get_backup_file_name(target_name); + ck_rename (target_name, backup_file_name, input->out_file_name); free (backup_file_name); } - ck_rename (input->out_file_name, input->in_file_name, input->out_file_name); + ck_rename (input->out_file_name, target_name, input->out_file_name); free (input->out_file_name); } diff --git a/sed/regexp.c b/sed/regexp.c index 1297c5e..4daefe2 100644 --- a/sed/regexp.c +++ b/sed/regexp.c @@ -64,15 +64,24 @@ compile_regex_1 (new_regex, needed_sub) const char *error; int syntax = ((extended_regexp_flags & REG_EXTENDED) ? RE_SYNTAX_POSIX_EXTENDED - : RE_SYNTAX_POSIX_BASIC) - & ~RE_DOT_NOT_NULL; - - if (posixicity == POSIXLY_EXTENDED) - syntax &= ~RE_UNMATCHED_RIGHT_PAREN_ORD; - else - syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD; + : RE_SYNTAX_POSIX_BASIC); + syntax &= ~RE_DOT_NOT_NULL; syntax |= RE_NO_POSIX_BACKTRACKING; + + switch (posixicity) + { + case POSIXLY_EXTENDED: + syntax &= ~RE_UNMATCHED_RIGHT_PAREN_ORD; + break; + case POSIXLY_CORRECT: + syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD; + break; + case POSIXLY_BASIC: + syntax |= RE_UNMATCHED_RIGHT_PAREN_ORD | RE_LIMITED_OPS | RE_NO_GNU_OPS; + break; + } + #ifdef RE_ICASE syntax |= (new_regex->flags & REG_ICASE) ? RE_ICASE : 0; #endif diff --git a/sed/sed.c b/sed/sed.c index 178d2cf..e78cf4e 100644 --- a/sed/sed.c +++ b/sed/sed.c @@ -72,6 +72,9 @@ bool no_default_output = false; /* If set, reset line counts on every new file. */ bool separate_files = false; +/* If set, follow symlinks when processing in place */ +bool follow_symlinks = false; + /* How do we edit files in-place? (we don't if NULL) */ char *in_place_extension = NULL; @@ -110,6 +113,10 @@ Usage: %s [OPTION]... {script-only-if-no-other-script} [input-file]...\n\ add the script to the commands to be executed\n")); fprintf(out, _(" -f script-file, --file=script-file\n\ add the contents of script-file to the commands to be executed\n")); +#ifdef ENABLE_FOLLOW_SYMLINKS + fprintf(out, _(" --follow-symlinks\n\ + follow symlinks when processing in place\n")); +#endif fprintf(out, _(" -i[SUFFIX], --in-place[=SUFFIX]\n\ edit files in place (makes backup if extension supplied)\n")); #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(MSDOS) || defined(__EMX__) @@ -173,6 +180,9 @@ main(argc, argv) {"unbuffered", 0, NULL, 'u'}, {"version", 0, NULL, 'v'}, {"help", 0, NULL, 'h'}, +#ifdef ENABLE_FOLLOW_SYMLINKS + {"follow-symlinks", 0, NULL, 'F'}, +#endif {NULL, 0, NULL, 0} }; @@ -226,6 +236,10 @@ main(argc, argv) the_program = compile_file(the_program, optarg); break; + case 'F': + follow_symlinks = true; + break; + case 'i': separate_files = true; if (optarg == NULL) diff --git a/sed/sed.h b/sed/sed.h index 8ff27bc..ce8f2cc 100644 --- a/sed/sed.h +++ b/sed/sed.h @@ -220,6 +220,9 @@ extern bool no_default_output; /* If set, reset line counts on every new file. */ extern bool separate_files; +/* If set, follow symlinks when invoked with -i option */ +extern bool follow_symlinks; + /* Do we need to be pedantically POSIX compliant? */ extern enum posixicity_types posixicity; diff --git a/sed/utils.c b/sed/utils.c index b4f0164..e0ebd20 100644 --- a/sed/utils.c +++ b/sed/utils.c @@ -35,6 +35,11 @@ # include #endif /* HAVE_STDLIB_H */ +#include +#include +#include +#include + #include "utils.h" const char *myname; @@ -342,6 +347,85 @@ do_ck_fclose(fp) } +/* Follow symlink and panic if something fails. Return the ultimate + symlink target, stored in a temporary buffer that the caller should + not free. */ +const char * +follow_symlink(const char *fname) +{ +#ifdef ENABLE_FOLLOW_SYMLINKS + static char *buf1, *buf2; + static int buf_size; + + struct stat statbuf; + const char *buf = fname, *c; + int rc; + + if (buf_size == 0) + { + buf1 = ck_malloc (PATH_MAX + 1); + buf2 = ck_malloc (PATH_MAX + 1); + buf_size = PATH_MAX + 1; + } + + while ((rc = lstat (buf, &statbuf)) == 0 + && (statbuf.st_mode & S_IFLNK) == S_IFLNK) + { + if (buf == buf2) + { + strcpy (buf1, buf2); + buf = buf1; + } + + while ((rc = readlink (buf, buf2, buf_size)) == buf_size) + { + buf_size *= 2; + buf1 = ck_realloc (buf1, buf_size); + buf2 = ck_realloc (buf2, buf_size); + } + if (rc < 0) + panic (_("couldn't follow symlink %s: %s"), buf, strerror(errno)); + else + buf2 [rc] = '\0'; + + if (buf2[0] != '/' && (c = strrchr (buf, '/')) != NULL) + { + /* Need to handle relative paths with care. Reallocate buf1 and + buf2 to be big enough. */ + int len = c - buf + 1; + if (len + rc + 1 > buf_size) + { + buf_size = len + rc + 1; + buf1 = ck_realloc (buf1, buf_size); + buf2 = ck_realloc (buf2, buf_size); + } + + /* Always store the new path in buf1. */ + if (buf != buf1) + memcpy (buf1, buf, len); + + /* Tack the relative symlink at the end of buf1. */ + memcpy (buf1 + len, buf2, rc + 1); + buf = buf1; + } + else + { + /* Use buf2 as the buffer, it saves a strcpy if it is not pointing to + another link. It works for absolute symlinks, and as long as + symlinks do not leave the current directory. */ + buf = buf2; + } + } + + if (rc < 0) + panic (_("cannot stat %s: %s"), buf, strerror(errno)); + + return buf; +#else + return fname; +#endif /* ENABLE_FOLLOW_SYMLINKS */ +} + /* Panic on failing rename */ void ck_rename (from, to, unlink_if_fail) diff --git a/sed/utils.h b/sed/utils.h index cef0f6d..a80202d 100644 --- a/sed/utils.h +++ b/sed/utils.h @@ -28,6 +28,7 @@ void ck_fwrite P_((const VOID *ptr, size_t size, size_t nmemb, FILE *stream)); size_t ck_fread P_((VOID *ptr, size_t size, size_t nmemb, FILE *stream)); void ck_fflush P_((FILE *stream)); void ck_fclose P_((FILE *stream)); +const char *follow_symlink P_((const char *path)); size_t ck_getline P_((char **text, size_t *buflen, FILE *stream)); FILE * ck_mkstemp P_((char **p_filename, char *tmpdir, char *base)); void ck_rename P_((const char *from, const char *to, const char *unlink_if_fail));