Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/qsubst Initial import of "qsubst", a command line qu...



details:   https://anonhg.NetBSD.org/src/rev/f50b211f7f7a
branches:  trunk
changeset: 476037:f50b211f7f7a
user:      perry <perry%NetBSD.org@localhost>
date:      Sat Sep 04 17:04:05 1999 +0000

description:
Initial import of "qsubst", a command line query search and replace
facility. Note that this is the raw distribution I got from der Mouse,
for reference on the vendor branch.

diffstat:

 usr.bin/qsubst/qsubst.1 |  127 +++++++++
 usr.bin/qsubst/qsubst.c |  622 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 749 insertions(+), 0 deletions(-)

diffs (truncated from 757 to 300 lines):

diff -r 84accea6848d -r f50b211f7f7a usr.bin/qsubst/qsubst.1
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/qsubst/qsubst.1   Sat Sep 04 17:04:05 1999 +0000
@@ -0,0 +1,127 @@
+.\" This file is in the public domain.
+.Dd May 29, 1997
+.Dt QSUBST 1
+.Os NetBSD 1.2BETA
+.Sh NAME
+.Nm qsubst
+.Nd query-replace strings in files
+.Sh SYNOPSIS
+.Nm
+.Ar str1
+.Ar str2
+.Op Ar flags
+.Ar file
+.Op Ar file Op Ar ...
+.Sh DESCRIPTION
+.Nm
+reads its options (see below) to get a list of files.  For each file on
+this list, it then replaces
+.Ar str1
+with
+.Ar str2
+wherever possible in that file, depending on user input (see below).
+The result is written back onto the original file.
+.Pp
+For each potential substitution found, the user is prompted with a few
+lines before and after the line containing the string to be
+substituted.  The string itself is displayed using the terminal's
+standout mode, if any.  Then one character is read from the terminal.
+This is then interpreted as follows (this is designed to be like Emacs'
+query-replace-string):
+.Bl -tag -compact -offset indent
+.It space
+Replace this occurrence and go on to the next one.
+.It \&.
+Replace this occurrence and don't change any more in this file (ie, go
+on to the next file).
+.It \&,
+Tentatively replace this occurrence.  The lines as they would look if
+the substitution were made are printed out.  Then another character is
+read and it is used to decide the result as if the tentative
+replacement had not happened.
+.It n
+Don't change this one; just go on to the next one.
+.It \&^G
+Don't change this one or any others in this file, but instead simply go
+on to the next file.
+.It \&!
+Change the rest in this file without asking, then go on to the next
+file (at which point qsubst will start asking again).
+.It \&?
+Print out the current filename and ask again.
+.El
+.Pp
+The first two arguments to qsubst are always the string to replace and
+the string to replace it with.  The options are as follows:
+.Bl -tag -compact -offset indent
+.It Fl w
+The search string is considered as a C symbol; it must be bounded by
+non-symbol characters.  This option toggles.
+.Pf ( Sq w
+for
+.Sq word . )
+.It Fl \&!
+.It Fl go
+.It Fl noask
+Enter \&! mode automatically at the beginning of each file.
+.It Fl nogo
+.It Fl ask
+Negate
+.Fl -go ,
+that is, ask as usual.
+.It Fl c Ns Ar N
+(Where N is a number.)  Give N lines of context above and below the
+line with the match when prompting the user.
+.It Fl CA Ns Ar N
+(Where N is a number.)  Give N lines of context above the line with the
+match when prompting the user.
+.It Fl CB Ns Ar N
+(Where N is a number.)  Give N lines of context below the line with the
+match when prompting the user.
+.It Fl f Ar filename
+The
+.Ar filename
+argument is one of the files qsubst should perform substitutions in.
+.It Fl F Ar filename
+.Nm
+reads
+.Ar filename
+to get the names of files to perform substitutions in.  The names
+should appear one to a line.
+.El
+.Pp
+The default amount of context is
+.Fl c2 ,
+that is, two lines above and two lines below the line with the match.
+.Pp
+Arguments not beginning with a
+.Fl \&
+sign in the options field are implicitly preceded by
+.Fl f .
+Thus,
+.Fl f
+is really needed only when the file name begins with a
+.Fl \&
+sign.
+.Pp
+.Nm
+reads its options in order and processes files as it gets them.  This
+means, for example, that a
+.Fl go
+will affect only files named after the
+.Fl go .
+.Pp
+The most context you can get is ten lines each, above and below.
+.Pp
+.Ar str1
+is limited to 512 characters; there is no limit on the size of
+.Ar str2 .
+Neither one may contain a NUL.
+.Pp
+NULs in the file may cause qsubst to make various mistakes.
+.Pp
+If any other program modifies the file while qsubst is running, all
+bets are off.
+.Sh AUTHOR
+der Mouse,
+.Aq mouse%rodents.montreal.qc.ca@localhost .
diff -r 84accea6848d -r f50b211f7f7a usr.bin/qsubst/qsubst.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.bin/qsubst/qsubst.c   Sat Sep 04 17:04:05 1999 +0000
@@ -0,0 +1,622 @@
+/*
+ * qsubst -- designed for renaming routines existing in a whole bunch
+ *  of files.  Needs -ltermcap.
+ *
+ * Usage:
+ *
+ * qsubst str1 str2 [ options ]
+ *
+ * qsubst reads its options (see below) to get a list of files.  For
+ *  each file on this list, it then replaces str1 with str2 wherever
+ *  possible in that file, depending on user input (see below).  The
+ *  result is written back onto the original file.
+ *
+ * For each possible substitution, the user is prompted with a few
+ *  lines before and after the line containing the string to be
+ *  substituted.  The string itself is displayed using the terminal's
+ *  standout mode, if any.  Then one character is read from the
+ *  terminal.  This is then interpreted as follows (this is designed to
+ *  be like Emacs' query-replace-string):
+ *
+ *     space   replace this occurrence and go on to the next one
+ *     .       replace this occurrence and don't change any more in
+ *             this file (ie, go on to the next file).
+ *     ,       tentatively replace this occurrence.  The lines as they
+ *             would look if the substitution were made are printed
+ *             out.  Then another character is read and it is used to
+ *             decide the result (possibly undoing the tentative
+ *             replacement).
+ *     n       don't change this one, but go on to the next one
+ *     ^G      don't change this one or any others in this file, but
+ *             instead go on to the next file.
+ *     !       change the rest in this file without asking, then go on
+ *             to the next file (at which point qsubst will start
+ *             asking again).
+ *     ?       print out the current filename and ask again.
+ *
+ * The first two arguments to qsubst are always the string to replace
+ *  and the string to replace it with.  The options are as follows:
+ *
+ *     -w      The search string is considered as a C symbol; it must
+ *             be bounded by non-symbol characters.  This option
+ *             toggles.  (`w' for `word'.)
+ *     -!      Enter ! mode automatically at the beginning of each
+ *             file.
+ *     -go     Same as -!
+ *     -noask  Same as -!
+ *     -nogo   Negate -go
+ *     -ask    Negate -noask (same as -nogo)
+ *     -cN     (N is a number) Give N lines of context above and below
+ *             the line with the match when prompting the user.
+ *     -CAN    (N is a number) Give N lines of context above the line
+ *             with the match when prompting the user.
+ *     -CBN    (N is a number) Give N lines of context below the line
+ *             with the match when prompting the user.
+ *     -f filename
+ *             The filename following the -f argument is one of the
+ *             files qsubst should perform substitutions in.
+ *     -F filename
+ *             qsubst should read the named file to get the names of
+ *             files to perform substitutions in.  The names should
+ *             appear one to a line.
+ *
+ * The default amount of context is -c2, that is, two lines above and
+ *  two lines below the line with the match.
+ *
+ * Arguments not beginning with a - sign in the options field are
+ *  implicitly preceded by -f.  Thus, -f is really needed only when the
+ *  file name begins with a - sign.
+ *
+ * qsubst reads its options in order and processes files as it gets
+ *  them.  This means, for example, that a -go will affect only files
+ *  from -f or -F options appearing after the -go option.
+ *
+ * The most context you can get is ten lines each, above and below
+ *  (corresponding to -c10).
+ *
+ * Str1 is limited to 512 characters; there is no limit on the size of
+ *  str2.  Neither one may contain a NUL.
+ *
+ * NULs in the file may cause qsubst to make various mistakes.
+ *
+ * If any other program modifies the file while qsubst is running, all
+ *  bets are off.
+ *
+ * This program is in the public domain.  Anyone may use it in any way
+ *  for any purpose.  Of course, it's also up to you to determine
+ *  whether what it does is suitable for you; the above comments may
+ *  help, but I can't promise they're accurate.  It's free, and you get
+ *  what you pay for.
+ *
+ * If you find any bugs I would appreciate hearing about them,
+ *  especially if you also fix them.
+ *
+ *                                     der Mouse
+ *
+ *                            mouse%rodents.montreal.qc.ca@localhost
+ */
+
+#include <stdio.h>
+#include <ctype.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
+#include <strings.h>
+#include <termios.h>
+#include <sys/file.h>
+#include <unused-arg.h>
+
+/* These belong in an include file, but which one? */
+extern int tgetent(char *, const char *);
+extern int tgetflag(const char *);
+extern const char *tgetstr(const char *, char **);
+extern void tputs(const char *, int, int (*)(char));
+
+extern const char *__progname;
+
+#define MAX_C_A 10
+#define MAX_C_B 10
+#define BUF_SIZ 1024
+
+static int debugging;
+static FILE *tempf;
+static long int tbeg;
+static FILE *workf;
+static char *str1;
+static char *str2;
+static int s1l;
+static int s2l;
+static long int nls[MAX_C_A+1];
+static char buf[(BUF_SIZ*2)+2];
+static char *bufp;
+static char *bufp0;
+static char *bufpmax;
+static int rahead;
+static int cabove;
+static int cbelow;
+static int wordmode;
+static int flying;
+static int flystate;
+static int allfly;
+static const char *nullstr = "";
+static int ul_;
+static char *current_file;
+static const char *beginul;
+static const char *endul;
+static char tcp_buf[1024];
+static char cap_buf[1024];
+static struct termios orig_tio;
+
+static void tstp_self(void)
+{
+ void (*old_tstp)(int);
+ int mask;
+
+ mask = sigblock(0);
+ kill(getpid(),SIGTSTP);
+ old_tstp = signal(SIGTSTP,SIG_DFL);
+ sigsetmask(mask&~sigmask(SIGTSTP));
+ signal(SIGTSTP,old_tstp);
+}
+
+static void sigtstp(UNUSED_ARG(int sig))
+{
+ struct termios tio;



Home | Main Index | Thread Index | Old Index