summaryrefslogtreecommitdiff
path: root/release/src/router/busybox/editors
diff options
context:
space:
mode:
Diffstat (limited to 'release/src/router/busybox/editors')
-rw-r--r--[-rwxr-xr-x]release/src/router/busybox/editors/Config.in174
-rw-r--r--release/src/router/busybox/editors/Kbuild14
-rw-r--r--release/src/router/busybox/editors/Makefile30
-rwxr-xr-xrelease/src/router/busybox/editors/Makefile.in44
-rw-r--r--release/src/router/busybox/editors/awk.c2275
-rw-r--r--release/src/router/busybox/editors/cmp.c135
-rw-r--r--release/src/router/busybox/editors/diff.c1342
-rw-r--r--release/src/router/busybox/editors/ed.c1049
-rw-r--r--release/src/router/busybox/editors/patch.c330
-rw-r--r--release/src/router/busybox/editors/sed.c1878
-rw-r--r--release/src/router/busybox/editors/sed1line.txt425
-rw-r--r--release/src/router/busybox/editors/sed_summary.htm223
-rw-r--r--release/src/router/busybox/editors/vi.c3412
13 files changed, 7409 insertions, 3922 deletions
diff --git a/release/src/router/busybox/editors/Config.in b/release/src/router/busybox/editors/Config.in
index 14b698f5..7dbc9b6d 100755..100644
--- a/release/src/router/busybox/editors/Config.in
+++ b/release/src/router/busybox/editors/Config.in
@@ -5,128 +5,192 @@
menu "Editors"
-config CONFIG_AWK
+config AWK
bool "awk"
default n
help
- Awk is used as a pattern scanning and processing language. This is
+ Awk is used as a pattern scanning and processing language. This is
the BusyBox implementation of that programming language.
-config CONFIG_FEATURE_AWK_MATH
- bool " Enable math functions (requires libm)"
- default y
- depends on CONFIG_AWK
+config FEATURE_AWK_LIBM
+ bool "Enable math functions (requires libm)"
+ default n
+ depends on AWK
help
Enable math functions of the Awk programming language.
NOTE: This will require libm to be present for linking.
-config CONFIG_PATCH
+config CMP
+ bool "cmp"
+ default n
+ help
+ cmp is used to compare two files and returns the result
+ to standard output.
+
+config DIFF
+ bool "diff"
+ default n
+ help
+ diff compares two files or directories and outputs the
+ differences between them in a form that can be given to
+ the patch command.
+
+config FEATURE_DIFF_BINARY
+ bool "Enable checks for binary files"
+ default y
+ depends on DIFF
+ help
+ This option enables support for checking for binary files
+ before a comparison is carried out.
+
+config FEATURE_DIFF_DIR
+ bool "Enable directory support"
+ default y
+ depends on DIFF
+ help
+ This option enables support for directory and subdirectory
+ comparison.
+
+config FEATURE_DIFF_MINIMAL
+ bool "Enable -d option to find smaller sets of changes"
+ default n
+ depends on DIFF
+ help
+ Enabling this option allows the use of -d to make diff
+ try hard to find the smallest possible set of changes.
+
+config ED
+ bool "ed"
+ default n
+ help
+ The original 1970's Unix text editor, from the days of teletypes.
+ Small, simple, evil. Part of SUSv3. If you're not already using
+ this, you don't need it.
+
+config PATCH
bool "patch"
default n
help
- Apply a unified diff formated patch.
+ Apply a unified diff formatted patch.
-config CONFIG_SED
+config SED
bool "sed"
default n
help
sed is used to perform text transformations on a file
or input from a pipeline.
-config CONFIG_FEATURE_SED_EMBEDED_NEWLINE
- bool " Embeded newline (EXPERIMENTAL)"
- default n
- depends on CONFIG_SED
- help
- This is a hack to allow matching of '\n' in regular expressions.
- It works by translating '\n' to "\n" and back.
- It may introduce unexpected results if you use "\n" in your text.
-
-config CONFIG_VI
+config VI
bool "vi"
default n
help
- 'vi' is a text editor. More specifically, it is the One True
- text editor <grin>. It does, however, have a rather steep
- learning curve. If you are not already comfortable with 'vi'
+ 'vi' is a text editor. More specifically, it is the One True
+ text editor <grin>. It does, however, have a rather steep
+ learning curve. If you are not already comfortable with 'vi'
you may wish to use something else.
-config CONFIG_FEATURE_VI_COLON
- bool " Enable \":\" colon commands (no \"ex\" mode)"
+config FEATURE_VI_MAX_LEN
+ int "Maximum screen width in vi"
+ range 256 16384
+ default 4096
+ depends on VI
+ help
+ Contrary to what you may think, this is not eating much.
+ Make it smaller than 4k only if you are very limited on memory.
+
+config FEATURE_VI_8BIT
+ bool "Allow vi to display 8-bit chars (otherwise shows dots)"
+ default y
+ depends on VI
+ help
+ If your terminal can display characters with high bit set,
+ you may want to enable this. Note: vi is not Unicode-capable.
+ If your terminal combines several 8-bit bytes into one character
+ (as in Unicode mode), this will not work properly.
+
+config FEATURE_VI_COLON
+ bool "Enable \":\" colon commands (no \"ex\" mode)"
default y
- depends on CONFIG_VI
+ depends on VI
help
- Enable a limited set of colon commands for vi. This does not
+ Enable a limited set of colon commands for vi. This does not
provide an "ex" mode.
-config CONFIG_FEATURE_VI_YANKMARK
- bool " Enable yank/put commands and mark cmds"
+config FEATURE_VI_YANKMARK
+ bool "Enable yank/put commands and mark cmds"
default y
- depends on CONFIG_VI
+ depends on VI
help
This will enable you to use yank and put, as well as mark in
busybox vi.
-config CONFIG_FEATURE_VI_SEARCH
- bool " Enable search and replace cmds"
+config FEATURE_VI_SEARCH
+ bool "Enable search and replace cmds"
default y
- depends on CONFIG_VI
+ depends on VI
help
Select this if you wish to be able to do search and replace in
busybox vi.
-config CONFIG_FEATURE_VI_USE_SIGNALS
- bool " Catch signals"
+config FEATURE_VI_USE_SIGNALS
+ bool "Catch signals"
default y
- depends on CONFIG_VI
+ depends on VI
help
- Selecting this option will make busybox vi signal aware. This will
+ Selecting this option will make busybox vi signal aware. This will
make busybox vi support SIGWINCH to deal with Window Changes, catch
Ctrl-Z and Ctrl-C and alarms.
-config CONFIG_FEATURE_VI_DOT_CMD
- bool " Remember previous cmd and \".\" cmd"
+config FEATURE_VI_DOT_CMD
+ bool "Remember previous cmd and \".\" cmd"
default y
- depends on CONFIG_VI
+ depends on VI
help
Make busybox vi remember the last command and be able to repeat it.
-config CONFIG_FEATURE_VI_READONLY
- bool " Enable -R option and \"view\" mode"
+config FEATURE_VI_READONLY
+ bool "Enable -R option and \"view\" mode"
default y
- depends on CONFIG_VI
+ depends on VI
help
Enable the read-only command line option, which allows the user to
open a file in read-only mode.
-config CONFIG_FEATURE_VI_SETOPTS
- bool " Enable set-able options, ai ic showmatch"
+config FEATURE_VI_SETOPTS
+ bool "Enable set-able options, ai ic showmatch"
default y
- depends on CONFIG_VI
+ depends on VI
help
Enable the editor to set some (ai, ic, showmatch) options.
-config CONFIG_FEATURE_VI_SET
- bool " Support for :set"
+config FEATURE_VI_SET
+ bool "Support for :set"
default y
- depends on CONFIG_VI
+ depends on VI
help
Support for ":set".
-config CONFIG_FEATURE_VI_WIN_RESIZE
- bool " Handle window resize"
+config FEATURE_VI_WIN_RESIZE
+ bool "Handle window resize"
default y
- depends on CONFIG_VI
+ depends on VI
help
Make busybox vi behave nicely with terminals that get resized.
-config CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- bool " Optimize cursor movement"
+config FEATURE_VI_OPTIMIZE_CURSOR
+ bool "Optimize cursor movement"
default y
- depends on CONFIG_VI
+ depends on VI
help
This will make the cursor movement faster, but requires more memory
and it makes the applet a tiny bit larger.
-endmenu
+config FEATURE_ALLOW_EXEC
+ bool "Allow vi and awk to execute shell commands"
+ default y
+ depends on VI || AWK
+ help
+ Enables vi and awk features which allows user to execute
+ shell commands (using system() C call).
+endmenu
diff --git a/release/src/router/busybox/editors/Kbuild b/release/src/router/busybox/editors/Kbuild
new file mode 100644
index 00000000..76302aa7
--- /dev/null
+++ b/release/src/router/busybox/editors/Kbuild
@@ -0,0 +1,14 @@
+# Makefile for busybox
+#
+# Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org>
+#
+# Licensed under the GPL v2, see the file LICENSE in this tarball.
+
+lib-y:=
+lib-$(CONFIG_AWK) += awk.o
+lib-$(CONFIG_CMP) += cmp.o
+lib-$(CONFIG_DIFF) += diff.o
+lib-$(CONFIG_ED) += ed.o
+lib-$(CONFIG_PATCH) += patch.o
+lib-$(CONFIG_SED) += sed.o
+lib-$(CONFIG_VI) += vi.o
diff --git a/release/src/router/busybox/editors/Makefile b/release/src/router/busybox/editors/Makefile
deleted file mode 100644
index 00ef6d50..00000000
--- a/release/src/router/busybox/editors/Makefile
+++ /dev/null
@@ -1,30 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-#
-
-TOPDIR:= ../
-EDITOR_DIR:=./
-include $(TOPDIR).config
-include $(TOPDIR)Rules.mak
-include Makefile.in
-all: $(libraries-y)
--include $(TOPDIR).depend
-
-clean:
- rm -f *.o *.a $(AR_TARGET)
-
diff --git a/release/src/router/busybox/editors/Makefile.in b/release/src/router/busybox/editors/Makefile.in
deleted file mode 100755
index 97450479..00000000
--- a/release/src/router/busybox/editors/Makefile.in
+++ /dev/null
@@ -1,44 +0,0 @@
-# Makefile for busybox
-#
-# Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org>
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-#
-
-EDITOR_AR:=editors.a
-ifndef $(EDITOR_DIR)
-EDITOR_DIR:=$(TOPDIR)editors/
-endif
-
-EDITOR-y:=
-EDITOR-$(CONFIG_AWK) += awk.o
-EDITOR-$(CONFIG_PATCH) += patch.o
-EDITOR-$(CONFIG_SED) += sed.o
-EDITOR-$(CONFIG_VI) += vi.o
-EDITOR_SRC:= $(EDITOR-y)
-EDITOR_OBJ:= $(patsubst %.c,$(EDITOR_DIR)%.o, $(EDITOR_SRC))
-
-libraries-y+=$(EDITOR_DIR)$(EDITOR_AR)
-
-needlibm-y:=
-needlibm-$(CONFIG_FEATURE_AWK_MATH) := y
-
-ifeq ($(needlibm-y),y)
- LIBRARIES += -lm
-endif
-
-$(EDITOR_DIR)$(EDITOR_AR): $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y))
- $(AR) -ro $@ $(patsubst %,$(EDITOR_DIR)%, $(EDITOR-y))
-
diff --git a/release/src/router/busybox/editors/awk.c b/release/src/router/busybox/editors/awk.c
index 0f8cf94f..89ae503a 100644
--- a/release/src/router/busybox/editors/awk.c
+++ b/release/src/router/busybox/editors/awk.c
@@ -4,64 +4,44 @@
*
* Copyright (C) 2002 by Dmitry Zakharov <dmit@crp.bank.gov.ua>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <time.h>
+#include "libbb.h"
+#include "xregex.h"
#include <math.h>
-#include <ctype.h>
-#include <getopt.h>
-#include <regex.h>
-#include "busybox.h"
+/* This is a NOEXEC applet. Be very careful! */
-#define MAXVARFMT 240
-#define MINNVBLOCK 64
+#define MAXVARFMT 240
+#define MINNVBLOCK 64
/* variable flags */
-#define VF_NUMBER 0x0001 /* 1 = primary type is number */
-#define VF_ARRAY 0x0002 /* 1 = it's an array */
+#define VF_NUMBER 0x0001 /* 1 = primary type is number */
+#define VF_ARRAY 0x0002 /* 1 = it's an array */
-#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
-#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
-#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
-#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
-#define VF_FSTR 0x1000 /* 1 = string points to fstring buffer */
-#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
-#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
+#define VF_CACHED 0x0100 /* 1 = num/str value has cached str/num eq */
+#define VF_USER 0x0200 /* 1 = user input (may be numeric string) */
+#define VF_SPECIAL 0x0400 /* 1 = requires extra handling when changed */
+#define VF_WALK 0x0800 /* 1 = variable has alloc'd x.walker list */
+#define VF_FSTR 0x1000 /* 1 = var::string points to fstring buffer */
+#define VF_CHILD 0x2000 /* 1 = function arg; x.parent points to source */
+#define VF_DIRTY 0x4000 /* 1 = variable was set explicitly */
/* these flags are static, don't change them when value is changed */
-#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
+#define VF_DONTTOUCH (VF_ARRAY | VF_SPECIAL | VF_WALK | VF_CHILD | VF_DIRTY)
/* Variable */
typedef struct var_s {
- unsigned short type; /* flags */
+ unsigned type; /* flags */
double number;
char *string;
union {
- int aidx; /* func arg index (on compilation stage) */
- struct xhash_s *array; /* array ptr */
- struct var_s *parent; /* for func args, ptr to actual parameter */
- char **walker; /* list of array elements (for..in) */
+ int aidx; /* func arg idx (for compilation stage) */
+ struct xhash_s *array; /* array ptr */
+ struct var_s *parent; /* for func args, ptr to actual parameter */
+ char **walker; /* list of array elements (for..in) */
} x;
} var;
@@ -69,12 +49,12 @@ typedef struct var_s {
typedef struct chain_s {
struct node_s *first;
struct node_s *last;
- char *programname;
+ const char *programname;
} chain;
/* Function */
typedef struct func_s {
- unsigned short nargs;
+ unsigned nargs;
struct chain_s body;
} func;
@@ -82,33 +62,34 @@ typedef struct func_s {
typedef struct rstream_s {
FILE *F;
char *buffer;
+ int adv;
int size;
int pos;
- unsigned short is_pipe;
+ smallint is_pipe;
} rstream;
typedef struct hash_item_s {
union {
- struct var_s v; /* variable/array hash */
- struct rstream_s rs; /* redirect streams hash */
- struct func_s f; /* functions hash */
+ struct var_s v; /* variable/array hash */
+ struct rstream_s rs; /* redirect streams hash */
+ struct func_s f; /* functions hash */
} data;
- struct hash_item_s *next; /* next in chain */
- char name[1]; /* really it's longer */
+ struct hash_item_s *next; /* next in chain */
+ char name[1]; /* really it's longer */
} hash_item;
typedef struct xhash_s {
- unsigned int nel; /* num of elements */
- unsigned int csize; /* current hash size */
- unsigned int nprime; /* next hash size in PRIMES[] */
- unsigned int glen; /* summary length of item names */
+ unsigned nel; /* num of elements */
+ unsigned csize; /* current hash size */
+ unsigned nprime; /* next hash size in PRIMES[] */
+ unsigned glen; /* summary length of item names */
struct hash_item_s **items;
} xhash;
/* Tree node */
typedef struct node_s {
- unsigned long info;
- unsigned short lineno;
+ uint32_t info;
+ unsigned lineno;
union {
struct node_s *n;
var *v;
@@ -174,42 +155,42 @@ typedef struct tsplitter_s {
#define TC_STRING (1 << 28)
#define TC_NUMBER (1 << 29)
-#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
+#define TC_UOPPRE (TC_UOPPRE1 | TC_UOPPRE2)
/* combined token classes */
-#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
-#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
-#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION | \
- TC_BUILTIN | TC_GETLINE | TC_SEQSTART | TC_STRING | TC_NUMBER)
+#define TC_BINOP (TC_BINOPX | TC_COMMA | TC_PIPE | TC_IN)
+#define TC_UNARYOP (TC_UOPPRE | TC_UOPPOST)
+#define TC_OPERAND (TC_VARIABLE | TC_ARRAY | TC_FUNCTION \
+ | TC_BUILTIN | TC_GETLINE | TC_SEQSTART | TC_STRING | TC_NUMBER)
-#define TC_STATEMNT (TC_STATX | TC_WHILE)
-#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
+#define TC_STATEMNT (TC_STATX | TC_WHILE)
+#define TC_OPTERM (TC_SEMICOL | TC_NEWLINE)
/* word tokens, cannot mean something else if not expected */
-#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE | TC_BUILTIN | \
- TC_GETLINE | TC_FUNCDECL | TC_BEGIN | TC_END)
+#define TC_WORD (TC_IN | TC_STATEMNT | TC_ELSE | TC_BUILTIN \
+ | TC_GETLINE | TC_FUNCDECL | TC_BEGIN | TC_END)
/* discard newlines after these */
-#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM | \
- TC_BINOP | TC_OPTERM)
+#define TC_NOTERM (TC_COMMA | TC_GRPSTART | TC_GRPTERM \
+ | TC_BINOP | TC_OPTERM)
/* what can expression begin with */
-#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
+#define TC_OPSEQ (TC_OPERAND | TC_UOPPRE | TC_REGEXP)
/* what can group begin with */
-#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
+#define TC_GRPSEQ (TC_OPSEQ | TC_OPTERM | TC_STATEMNT | TC_GRPSTART)
/* if previous token class is CONCAT1 and next is CONCAT2, concatenation */
/* operator is inserted between them */
-#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM | \
- TC_STRING | TC_NUMBER | TC_UOPPOST)
-#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
+#define TC_CONCAT1 (TC_VARIABLE | TC_ARRTERM | TC_SEQTERM \
+ | TC_STRING | TC_NUMBER | TC_UOPPOST)
+#define TC_CONCAT2 (TC_OPERAND | TC_UOPPRE)
-#define OF_RES1 0x010000
-#define OF_RES2 0x020000
-#define OF_STR1 0x040000
-#define OF_STR2 0x080000
-#define OF_NUM1 0x100000
-#define OF_CHECKED 0x200000
+#define OF_RES1 0x010000
+#define OF_RES2 0x020000
+#define OF_STR1 0x040000
+#define OF_STR2 0x080000
+#define OF_NUM1 0x100000
+#define OF_CHECKED 0x200000
/* combined operator flags */
#define xx 0
@@ -223,16 +204,16 @@ typedef struct tsplitter_s {
#define SV (OF_RES1 | OF_STR1 | OF_RES2)
#define SS (OF_RES1 | OF_STR1 | OF_RES2 | OF_STR2)
-#define OPCLSMASK 0xFF00
-#define OPNMASK 0x007F
+#define OPCLSMASK 0xFF00
+#define OPNMASK 0x007F
/* operator priority is a highest byte (even: r->l, odd: l->r grouping)
* For builtins it has different meaning: n n s3 s2 s1 v3 v2 v1,
* n - min. number of args, vN - resolve Nth arg to var, sN - resolve to string
*/
-#define P(x) (x << 24)
-#define PRIMASK 0x7F000000
-#define PRIMASK2 0x7E000000
+#define P(x) (x << 24)
+#define PRIMASK 0x7F000000
+#define PRIMASK2 0x7E000000
/* Operation classes */
@@ -240,129 +221,133 @@ typedef struct tsplitter_s {
#define RECUR_FROM_THIS 0x1000
enum {
- OC_DELETE=0x0100, OC_EXEC=0x0200, OC_NEWSOURCE=0x0300,
- OC_PRINT=0x0400, OC_PRINTF=0x0500, OC_WALKINIT=0x0600,
-
- OC_BR=0x0700, OC_BREAK=0x0800, OC_CONTINUE=0x0900,
- OC_EXIT=0x0a00, OC_NEXT=0x0b00, OC_NEXTFILE=0x0c00,
- OC_TEST=0x0d00, OC_WALKNEXT=0x0e00,
-
- OC_BINARY=0x1000, OC_BUILTIN=0x1100, OC_COLON=0x1200,
- OC_COMMA=0x1300, OC_COMPARE=0x1400, OC_CONCAT=0x1500,
- OC_FBLTIN=0x1600, OC_FIELD=0x1700, OC_FNARG=0x1800,
- OC_FUNC=0x1900, OC_GETLINE=0x1a00, OC_IN=0x1b00,
- OC_LAND=0x1c00, OC_LOR=0x1d00, OC_MATCH=0x1e00,
- OC_MOVE=0x1f00, OC_PGETLINE=0x2000, OC_REGEXP=0x2100,
- OC_REPLACE=0x2200, OC_RETURN=0x2300, OC_SPRINTF=0x2400,
- OC_TERNARY=0x2500, OC_UNARY=0x2600, OC_VAR=0x2700,
- OC_DONE=0x2800,
-
- ST_IF=0x3000, ST_DO=0x3100, ST_FOR=0x3200,
- ST_WHILE=0x3300
+ OC_DELETE = 0x0100, OC_EXEC = 0x0200, OC_NEWSOURCE = 0x0300,
+ OC_PRINT = 0x0400, OC_PRINTF = 0x0500, OC_WALKINIT = 0x0600,
+
+ OC_BR = 0x0700, OC_BREAK = 0x0800, OC_CONTINUE = 0x0900,
+ OC_EXIT = 0x0a00, OC_NEXT = 0x0b00, OC_NEXTFILE = 0x0c00,
+ OC_TEST = 0x0d00, OC_WALKNEXT = 0x0e00,
+
+ OC_BINARY = 0x1000, OC_BUILTIN = 0x1100, OC_COLON = 0x1200,
+ OC_COMMA = 0x1300, OC_COMPARE = 0x1400, OC_CONCAT = 0x1500,
+ OC_FBLTIN = 0x1600, OC_FIELD = 0x1700, OC_FNARG = 0x1800,
+ OC_FUNC = 0x1900, OC_GETLINE = 0x1a00, OC_IN = 0x1b00,
+ OC_LAND = 0x1c00, OC_LOR = 0x1d00, OC_MATCH = 0x1e00,
+ OC_MOVE = 0x1f00, OC_PGETLINE = 0x2000, OC_REGEXP = 0x2100,
+ OC_REPLACE = 0x2200, OC_RETURN = 0x2300, OC_SPRINTF = 0x2400,
+ OC_TERNARY = 0x2500, OC_UNARY = 0x2600, OC_VAR = 0x2700,
+ OC_DONE = 0x2800,
+
+ ST_IF = 0x3000, ST_DO = 0x3100, ST_FOR = 0x3200,
+ ST_WHILE = 0x3300
};
/* simple builtins */
enum {
- F_in=0, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
+ F_in, F_rn, F_co, F_ex, F_lg, F_si, F_sq, F_sr,
F_ti, F_le, F_sy, F_ff, F_cl
};
/* builtins */
enum {
- B_a2=0, B_ix, B_ma, B_sp, B_ss, B_ti, B_lo, B_up,
- B_ge, B_gs, B_su
+ B_a2, B_ix, B_ma, B_sp, B_ss, B_ti, B_lo, B_up,
+ B_ge, B_gs, B_su,
+ B_an, B_co, B_ls, B_or, B_rs, B_xo,
};
/* tokens and their corresponding info values */
-#define NTC "\377" /* switch to next token class (tc<<1) */
-#define NTCC '\377'
+#define NTC "\377" /* switch to next token class (tc<<1) */
+#define NTCC '\377'
#define OC_B OC_BUILTIN
-static char * const tokenlist =
- "\1(" NTC
- "\1)" NTC
- "\1/" NTC /* REGEXP */
- "\2>>" "\1>" "\1|" NTC /* OUTRDR */
- "\2++" "\2--" NTC /* UOPPOST */
- "\2++" "\2--" "\1$" NTC /* UOPPRE1 */
- "\2==" "\1=" "\2+=" "\2-=" /* BINOPX */
- "\2*=" "\2/=" "\2%=" "\2^="
- "\1+" "\1-" "\3**=" "\2**"
- "\1/" "\1%" "\1^" "\1*"
- "\2!=" "\2>=" "\2<=" "\1>"
- "\1<" "\2!~" "\1~" "\2&&"
- "\2||" "\1?" "\1:" NTC
- "\2in" NTC
- "\1," NTC
- "\1|" NTC
- "\1+" "\1-" "\1!" NTC /* UOPPRE2 */
- "\1]" NTC
- "\1{" NTC
- "\1}" NTC
- "\1;" NTC
- "\1\n" NTC
- "\2if" "\2do" "\3for" "\5break" /* STATX */
- "\10continue" "\6delete" "\5print"
- "\6printf" "\4next" "\10nextfile"
- "\6return" "\4exit" NTC
- "\5while" NTC
- "\4else" NTC
-
- "\5close" "\6system" "\6fflush" "\5atan2" /* BUILTIN */
- "\3cos" "\3exp" "\3int" "\3log"
- "\4rand" "\3sin" "\4sqrt" "\5srand"
- "\6gensub" "\4gsub" "\5index" "\6length"
- "\5match" "\5split" "\7sprintf" "\3sub"
- "\6substr" "\7systime" "\10strftime"
- "\7tolower" "\7toupper" NTC
- "\7getline" NTC
- "\4func" "\10function" NTC
- "\5BEGIN" NTC
- "\3END" "\0"
+static const char tokenlist[] ALIGN1 =
+ "\1(" NTC
+ "\1)" NTC
+ "\1/" NTC /* REGEXP */
+ "\2>>" "\1>" "\1|" NTC /* OUTRDR */
+ "\2++" "\2--" NTC /* UOPPOST */
+ "\2++" "\2--" "\1$" NTC /* UOPPRE1 */
+ "\2==" "\1=" "\2+=" "\2-=" /* BINOPX */
+ "\2*=" "\2/=" "\2%=" "\2^="
+ "\1+" "\1-" "\3**=" "\2**"
+ "\1/" "\1%" "\1^" "\1*"
+ "\2!=" "\2>=" "\2<=" "\1>"
+ "\1<" "\2!~" "\1~" "\2&&"
+ "\2||" "\1?" "\1:" NTC
+ "\2in" NTC
+ "\1," NTC
+ "\1|" NTC
+ "\1+" "\1-" "\1!" NTC /* UOPPRE2 */
+ "\1]" NTC
+ "\1{" NTC
+ "\1}" NTC
+ "\1;" NTC
+ "\1\n" NTC
+ "\2if" "\2do" "\3for" "\5break" /* STATX */
+ "\10continue" "\6delete" "\5print"
+ "\6printf" "\4next" "\10nextfile"
+ "\6return" "\4exit" NTC
+ "\5while" NTC
+ "\4else" NTC
+
+ "\3and" "\5compl" "\6lshift" "\2or"
+ "\6rshift" "\3xor"
+ "\5close" "\6system" "\6fflush" "\5atan2" /* BUILTIN */
+ "\3cos" "\3exp" "\3int" "\3log"
+ "\4rand" "\3sin" "\4sqrt" "\5srand"
+ "\6gensub" "\4gsub" "\5index" "\6length"
+ "\5match" "\5split" "\7sprintf" "\3sub"
+ "\6substr" "\7systime" "\10strftime"
+ "\7tolower" "\7toupper" NTC
+ "\7getline" NTC
+ "\4func" "\10function" NTC
+ "\5BEGIN" NTC
+ "\3END" "\0"
;
-static unsigned long tokeninfo[] = {
-
+static const uint32_t tokeninfo[] = {
0,
0,
OC_REGEXP,
- xS|'a', xS|'w', xS|'|',
- OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
- OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M',
- OC_FIELD|xV|P(5),
- OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74),
- OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
- OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/',
- OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
- OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-',
- OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
- OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%',
- OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
- OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3,
- OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
- OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!',
- OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
- OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?',
- OC_COLON|xx|P(67)|':',
+ xS|'a', xS|'w', xS|'|',
+ OC_UNARY|xV|P(9)|'p', OC_UNARY|xV|P(9)|'m',
+ OC_UNARY|xV|P(9)|'P', OC_UNARY|xV|P(9)|'M',
+ OC_FIELD|xV|P(5),
+ OC_COMPARE|VV|P(39)|5, OC_MOVE|VV|P(74),
+ OC_REPLACE|NV|P(74)|'+', OC_REPLACE|NV|P(74)|'-',
+ OC_REPLACE|NV|P(74)|'*', OC_REPLACE|NV|P(74)|'/',
+ OC_REPLACE|NV|P(74)|'%', OC_REPLACE|NV|P(74)|'&',
+ OC_BINARY|NV|P(29)|'+', OC_BINARY|NV|P(29)|'-',
+ OC_REPLACE|NV|P(74)|'&', OC_BINARY|NV|P(15)|'&',
+ OC_BINARY|NV|P(25)|'/', OC_BINARY|NV|P(25)|'%',
+ OC_BINARY|NV|P(15)|'&', OC_BINARY|NV|P(25)|'*',
+ OC_COMPARE|VV|P(39)|4, OC_COMPARE|VV|P(39)|3,
+ OC_COMPARE|VV|P(39)|0, OC_COMPARE|VV|P(39)|1,
+ OC_COMPARE|VV|P(39)|2, OC_MATCH|Sx|P(45)|'!',
+ OC_MATCH|Sx|P(45)|'~', OC_LAND|Vx|P(55),
+ OC_LOR|Vx|P(59), OC_TERNARY|Vx|P(64)|'?',
+ OC_COLON|xx|P(67)|':',
OC_IN|SV|P(49),
OC_COMMA|SS|P(80),
OC_PGETLINE|SV|P(37),
- OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-',
- OC_UNARY|xV|P(19)|'!',
+ OC_UNARY|xV|P(19)|'+', OC_UNARY|xV|P(19)|'-',
+ OC_UNARY|xV|P(19)|'!',
0,
0,
0,
0,
0,
- ST_IF, ST_DO, ST_FOR, OC_BREAK,
- OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
- OC_PRINTF, OC_NEXT, OC_NEXTFILE,
- OC_RETURN|Vx, OC_EXIT|Nx,
+ ST_IF, ST_DO, ST_FOR, OC_BREAK,
+ OC_CONTINUE, OC_DELETE|Vx, OC_PRINT,
+ OC_PRINTF, OC_NEXT, OC_NEXTFILE,
+ OC_RETURN|Vx, OC_EXIT|Nx,
ST_WHILE,
0,
+ OC_B|B_an|P(0x83), OC_B|B_co|P(0x41), OC_B|B_ls|P(0x83), OC_B|B_or|P(0x83),
+ OC_B|B_rs|P(0x83), OC_B|B_xo|P(0x83),
OC_FBLTIN|Sx|F_cl, OC_FBLTIN|Sx|F_sy, OC_FBLTIN|Sx|F_ff, OC_B|B_a2|P(0x83),
OC_FBLTIN|Nx|F_co, OC_FBLTIN|Nx|F_ex, OC_FBLTIN|Nx|F_in, OC_FBLTIN|Nx|F_lg,
OC_FBLTIN|F_rn, OC_FBLTIN|Nx|F_si, OC_FBLTIN|Nx|F_sq, OC_FBLTIN|Nx|F_sr,
@@ -377,124 +362,196 @@ static unsigned long tokeninfo[] = {
};
/* internal variable names and their initial values */
-/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
+/* asterisk marks SPECIAL vars; $ is just no-named Field0 */
enum {
- CONVFMT=0, OFMT, FS, OFS,
- ORS, RS, RT, FILENAME,
- SUBSEP, ARGIND, ARGC, ARGV,
- ERRNO, FNR,
- NR, NF, IGNORECASE,
- ENVIRON, F0, _intvarcount_
+ CONVFMT, OFMT, FS, OFS,
+ ORS, RS, RT, FILENAME,
+ SUBSEP, F0, ARGIND, ARGC,
+ ARGV, ERRNO, FNR, NR,
+ NF, IGNORECASE, ENVIRON, NUM_INTERNAL_VARS
};
-static char * vNames =
- "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
- "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
- "SUBSEP\0" "ARGIND\0" "ARGC\0" "ARGV\0"
- "ERRNO\0" "FNR\0"
- "NR\0" "NF\0*" "IGNORECASE\0*"
- "ENVIRON\0" "$\0*" "\0";
+static const char vNames[] ALIGN1 =
+ "CONVFMT\0" "OFMT\0" "FS\0*" "OFS\0"
+ "ORS\0" "RS\0*" "RT\0" "FILENAME\0"
+ "SUBSEP\0" "$\0*" "ARGIND\0" "ARGC\0"
+ "ARGV\0" "ERRNO\0" "FNR\0" "NR\0"
+ "NF\0*" "IGNORECASE\0*" "ENVIRON\0" "\0";
-static char * vValues =
- "%.6g\0" "%.6g\0" " \0" " \0"
- "\n\0" "\n\0" "\0" "\0"
- "\034\0"
- "\377";
+static const char vValues[] ALIGN1 =
+ "%.6g\0" "%.6g\0" " \0" " \0"
+ "\n\0" "\n\0" "\0" "\0"
+ "\034\0" "\0" "\377";
/* hash size may grow to these values */
-#define FIRST_PRIME 61;
-static const unsigned int PRIMES[] = { 251, 1021, 4093, 16381, 65521 };
-static const unsigned int NPRIMES = sizeof(PRIMES) / sizeof(unsigned int);
-
-/* globals */
-
-extern char **environ;
-
-static var * V[_intvarcount_];
-static chain beginseq, mainseq, endseq, *seq;
-static int nextrec, nextfile;
-static node *break_ptr, *continue_ptr;
-static rstream *iF;
-static xhash *vhash, *ahash, *fdhash, *fnhash;
-static char *programname;
-static short lineno;
-static int is_f0_split;
-static int nfields = 0;
-static var *Fields = NULL;
-static tsplitter fsplitter, rsplitter;
-static nvblock *cb = NULL;
-static char *pos;
-static char *buf;
-static int icase = FALSE;
-
-static struct {
- unsigned long tclass;
- unsigned long info;
- char *string;
- double number;
- short lineno;
- int rollback;
-} t;
+#define FIRST_PRIME 61
+static const uint16_t PRIMES[] ALIGN2 = { 251, 1021, 4093, 16381, 65521 };
+
+
+/* Globals. Split in two parts so that first one is addressed
+ * with (mostly short) negative offsets.
+ * NB: it's unsafe to put members of type "double"
+ * into globals2 (gcc may fail to align them).
+ */
+struct globals {
+ double t_double;
+ chain beginseq, mainseq, endseq;
+ chain *seq;
+ node *break_ptr, *continue_ptr;
+ rstream *iF;
+ xhash *vhash, *ahash, *fdhash, *fnhash;
+ const char *g_progname;
+ int g_lineno;
+ int nfields;
+ int maxfields; /* used in fsrealloc() only */
+ var *Fields;
+ nvblock *g_cb;
+ char *g_pos;
+ char *g_buf;
+ smallint icase;
+ smallint exiting;
+ smallint nextrec;
+ smallint nextfile;
+ smallint is_f0_split;
+};
+struct globals2 {
+ uint32_t t_info; /* often used */
+ uint32_t t_tclass;
+ char *t_string;
+ int t_lineno;
+ int t_rollback;
+
+ var *intvar[NUM_INTERNAL_VARS]; /* often used */
+
+ /* former statics from various functions */
+ char *split_f0__fstrings;
+
+ uint32_t next_token__save_tclass;
+ uint32_t next_token__save_info;
+ uint32_t next_token__ltclass;
+ smallint next_token__concat_inserted;
+
+ smallint next_input_file__files_happen;
+ rstream next_input_file__rsm;
+
+ var *evaluate__fnargs;
+ unsigned evaluate__seed;
+ regex_t evaluate__sreg;
+
+ var ptest__v;
+
+ tsplitter exec_builtin__tspl;
+
+ /* biggest and least used members go last */
+ tsplitter fsplitter, rsplitter;
+};
+#define G1 (ptr_to_globals[-1])
+#define G (*(struct globals2 *)ptr_to_globals)
+/* For debug. nm --size-sort awk.o | grep -vi ' [tr] ' */
+/*char G1size[sizeof(G1)]; - 0x74 */
+/*char Gsize[sizeof(G)]; - 0x1c4 */
+/* Trying to keep most of members accessible with short offsets: */
+/*char Gofs_seed[offsetof(struct globals2, evaluate__seed)]; - 0x90 */
+#define t_double (G1.t_double )
+#define beginseq (G1.beginseq )
+#define mainseq (G1.mainseq )
+#define endseq (G1.endseq )
+#define seq (G1.seq )
+#define break_ptr (G1.break_ptr )
+#define continue_ptr (G1.continue_ptr)
+#define iF (G1.iF )
+#define vhash (G1.vhash )
+#define ahash (G1.ahash )
+#define fdhash (G1.fdhash )
+#define fnhash (G1.fnhash )
+#define g_progname (G1.g_progname )
+#define g_lineno (G1.g_lineno )
+#define nfields (G1.nfields )
+#define maxfields (G1.maxfields )
+#define Fields (G1.Fields )
+#define g_cb (G1.g_cb )
+#define g_pos (G1.g_pos )
+#define g_buf (G1.g_buf )
+#define icase (G1.icase )
+#define exiting (G1.exiting )
+#define nextrec (G1.nextrec )
+#define nextfile (G1.nextfile )
+#define is_f0_split (G1.is_f0_split )
+#define t_info (G.t_info )
+#define t_tclass (G.t_tclass )
+#define t_string (G.t_string )
+#define t_lineno (G.t_lineno )
+#define t_rollback (G.t_rollback )
+#define intvar (G.intvar )
+#define fsplitter (G.fsplitter )
+#define rsplitter (G.rsplitter )
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G1) + sizeof(G)) + sizeof(G1)); \
+ G.next_token__ltclass = TC_OPTERM; \
+ G.evaluate__seed = 1; \
+} while (0)
+
/* function prototypes */
-extern void xregcomp(regex_t *preg, const char *regex, int cflags);
static void handle_special(var *);
-static node *parse_expr(unsigned long);
+static node *parse_expr(uint32_t);
static void chain_group(void);
static var *evaluate(node *, var *);
static rstream *next_input_file(void);
-static int fmt_num(char *, int, char *, double, int);
-static int awk_exit(int);
+static int fmt_num(char *, int, const char *, double, int);
+static int awk_exit(int) NORETURN;
/* ---- error handling ---- */
-static const char EMSG_INTERNAL_ERROR[] = "Internal error";
-static const char EMSG_UNEXP_EOS[] = "Unexpected end of string";
-static const char EMSG_UNEXP_TOKEN[] = "Unexpected token";
-static const char EMSG_DIV_BY_ZERO[] = "Division by zero";
-static const char EMSG_INV_FMT[] = "Invalid format specifier";
-static const char EMSG_TOO_FEW_ARGS[] = "Too few arguments for builtin";
-static const char EMSG_NOT_ARRAY[] = "Not an array";
-static const char EMSG_POSSIBLE_ERROR[] = "Possible syntax error";
-static const char EMSG_UNDEF_FUNC[] = "Call to undefined function";
-#ifndef CONFIG_FEATURE_AWK_MATH
-static const char EMSG_NO_MATH[] = "Math support is not compiled in";
+static const char EMSG_INTERNAL_ERROR[] ALIGN1 = "Internal error";
+static const char EMSG_UNEXP_EOS[] ALIGN1 = "Unexpected end of string";
+static const char EMSG_UNEXP_TOKEN[] ALIGN1 = "Unexpected token";
+static const char EMSG_DIV_BY_ZERO[] ALIGN1 = "Division by zero";
+static const char EMSG_INV_FMT[] ALIGN1 = "Invalid format specifier";
+static const char EMSG_TOO_FEW_ARGS[] ALIGN1 = "Too few arguments for builtin";
+static const char EMSG_NOT_ARRAY[] ALIGN1 = "Not an array";
+static const char EMSG_POSSIBLE_ERROR[] ALIGN1 = "Possible syntax error";
+static const char EMSG_UNDEF_FUNC[] ALIGN1 = "Call to undefined function";
+#if !ENABLE_FEATURE_AWK_LIBM
+static const char EMSG_NO_MATH[] ALIGN1 = "Math support is not compiled in";
#endif
-static void syntax_error(const char * const message)
+static void zero_out_var(var * vp)
{
- bb_error_msg("%s:%i: %s", programname, lineno, message);
- awk_exit(1);
+ memset(vp, 0, sizeof(*vp));
}
-#define runtime_error(x) syntax_error(x)
-
+static void syntax_error(const char *const message) NORETURN;
+static void syntax_error(const char *const message)
+{
+ bb_error_msg_and_die("%s:%i: %s", g_progname, g_lineno, message);
+}
/* ---- hash stuff ---- */
-static unsigned int hashidx(char *name) {
-
- register unsigned int idx=0;
+static unsigned hashidx(const char *name)
+{
+ unsigned idx = 0;
- while (*name) idx = *name++ + (idx << 6) - idx;
+ while (*name) idx = *name++ + (idx << 6) - idx;
return idx;
}
/* create new hash */
-static xhash *hash_init(void) {
-
+static xhash *hash_init(void)
+{
xhash *newhash;
-
- newhash = (xhash *)xcalloc(1, sizeof(xhash));
+
+ newhash = xzalloc(sizeof(xhash));
newhash->csize = FIRST_PRIME;
- newhash->items = (hash_item **)xcalloc(newhash->csize, sizeof(hash_item *));
+ newhash->items = xzalloc(newhash->csize * sizeof(hash_item *));
return newhash;
}
/* find item in hash, return ptr to data, NULL if not found */
-static void *hash_search(xhash *hash, char *name) {
-
+static void *hash_search(xhash *hash, const char *name)
+{
hash_item *hi;
hi = hash->items [ hashidx(name) % hash->csize ];
@@ -507,18 +564,18 @@ static void *hash_search(xhash *hash, char *name) {
}
/* grow hash if it becomes too big */
-static void hash_rebuild(xhash *hash) {
-
- unsigned int newsize, i, idx;
+static void hash_rebuild(xhash *hash)
+{
+ unsigned newsize, i, idx;
hash_item **newitems, *hi, *thi;
- if (hash->nprime == NPRIMES)
+ if (hash->nprime == ARRAY_SIZE(PRIMES))
return;
newsize = PRIMES[hash->nprime++];
- newitems = (hash_item **)xcalloc(newsize, sizeof(hash_item *));
+ newitems = xzalloc(newsize * sizeof(hash_item *));
- for (i=0; i<hash->csize; i++) {
+ for (i = 0; i < hash->csize; i++) {
hi = hash->items[i];
while (hi) {
thi = hi;
@@ -535,20 +592,20 @@ static void hash_rebuild(xhash *hash) {
}
/* find item in hash, add it if necessary. Return ptr to data */
-static void *hash_find(xhash *hash, char *name) {
-
+static void *hash_find(xhash *hash, const char *name)
+{
hash_item *hi;
- unsigned int idx;
+ unsigned idx;
int l;
hi = hash_search(hash, name);
- if (! hi) {
+ if (!hi) {
if (++hash->nel / hash->csize > 10)
hash_rebuild(hash);
- l = bb_strlen(name) + 1;
- hi = xcalloc(sizeof(hash_item) + l, 1);
- memcpy(hi->name, name, l);
+ l = strlen(name) + 1;
+ hi = xzalloc(sizeof(*hi) + l);
+ strcpy(hi->name, name);
idx = hashidx(name) % hash->csize;
hi->next = hash->items[idx];
@@ -558,20 +615,20 @@ static void *hash_find(xhash *hash, char *name) {
return &(hi->data);
}
-#define findvar(hash, name) (var *) hash_find ( (hash) , (name) )
-#define newvar(name) (var *) hash_find ( vhash , (name) )
-#define newfile(name) (rstream *) hash_find ( fdhash , (name) )
-#define newfunc(name) (func *) hash_find ( fnhash , (name) )
-
-static void hash_remove(xhash *hash, char *name) {
+#define findvar(hash, name) ((var*) hash_find((hash), (name)))
+#define newvar(name) ((var*) hash_find(vhash, (name)))
+#define newfile(name) ((rstream*)hash_find(fdhash, (name)))
+#define newfunc(name) ((func*) hash_find(fnhash, (name)))
+static void hash_remove(xhash *hash, const char *name)
+{
hash_item *hi, **phi;
- phi = &(hash->items[ hashidx(name) % hash->csize ]);
+ phi = &(hash->items[hashidx(name) % hash->csize]);
while (*phi) {
hi = *phi;
if (strcmp(hi->name, name) == 0) {
- hash->glen -= (bb_strlen(name) + 1);
+ hash->glen -= (strlen(name) + 1);
hash->nel--;
*phi = hi->next;
free(hi);
@@ -583,29 +640,34 @@ static void hash_remove(xhash *hash, char *name) {
/* ------ some useful functions ------ */
-static void skip_spaces(char **s) {
-
- register char *p = *s;
+static void skip_spaces(char **s)
+{
+ char *p = *s;
- while(*p == ' ' || *p == '\t' ||
- (*p == '\\' && *(p+1) == '\n' && (++p, ++t.lineno))) {
- p++;
+ while (1) {
+ if (*p == '\\' && p[1] == '\n') {
+ p++;
+ t_lineno++;
+ } else if (*p != ' ' && *p != '\t') {
+ break;
+ }
+ p++;
}
*s = p;
}
-static char *nextword(char **s) {
-
- register char *p = *s;
+static char *nextword(char **s)
+{
+ char *p = *s;
- while (*(*s)++) ;
+ while (*(*s)++) /* */;
return p;
}
-static char nextchar(char **s) {
-
- register char c, *pps;
+static char nextchar(char **s)
+{
+ char c, *pps;
c = *((*s)++);
pps = *s;
@@ -614,38 +676,45 @@ static char nextchar(char **s) {
return c;
}
-static inline int isalnum_(int c) {
-
+static ALWAYS_INLINE int isalnum_(int c)
+{
return (isalnum(c) || c == '_');
}
-static FILE *afopen(const char *path, const char *mode) {
-
- return (*path == '-' && *(path+1) == '\0') ? stdin : bb_xfopen(path, mode);
+static double my_strtod(char **pp)
+{
+#if ENABLE_DESKTOP
+ if ((*pp)[0] == '0'
+ && ((((*pp)[1] | 0x20) == 'x') || isdigit((*pp)[1]))
+ ) {
+ return strtoull(*pp, pp, 0);
+ }
+#endif
+ return strtod(*pp, pp);
}
/* -------- working with variables (set/get/copy/etc) -------- */
-static xhash *iamarray(var *v) {
-
+static xhash *iamarray(var *v)
+{
var *a = v;
while (a->type & VF_CHILD)
a = a->x.parent;
- if (! (a->type & VF_ARRAY)) {
+ if (!(a->type & VF_ARRAY)) {
a->type |= VF_ARRAY;
a->x.array = hash_init();
}
return a->x.array;
}
-static void clear_array(xhash *array) {
-
- unsigned int i;
+static void clear_array(xhash *array)
+{
+ unsigned i;
hash_item *hi, *thi;
- for (i=0; i<array->csize; i++) {
+ for (i = 0; i < array->csize; i++) {
hi = array->items[i];
while (hi) {
thi = hi;
@@ -659,8 +728,8 @@ static void clear_array(xhash *array) {
}
/* clear a variable */
-static var *clrvar(var *v) {
-
+static var *clrvar(var *v)
+{
if (!(v->type & VF_FSTR))
free(v->string);
@@ -671,34 +740,33 @@ static var *clrvar(var *v) {
}
/* assign string value to variable */
-static var *setvar_p(var *v, char *value) {
-
+static var *setvar_p(var *v, char *value)
+{
clrvar(v);
v->string = value;
handle_special(v);
-
return v;
}
/* same as setvar_p but make a copy of string */
-static var *setvar_s(var *v, char *value) {
-
- return setvar_p(v, (value && *value) ? bb_xstrdup(value) : NULL);
+static var *setvar_s(var *v, const char *value)
+{
+ return setvar_p(v, (value && *value) ? xstrdup(value) : NULL);
}
/* same as setvar_s but set USER flag */
-static var *setvar_u(var *v, char *value) {
-
+static var *setvar_u(var *v, const char *value)
+{
setvar_s(v, value);
v->type |= VF_USER;
return v;
}
/* set array element to user string */
-static void setari_u(var *a, int idx, char *s) {
-
- register var *v;
- static char sidx[12];
+static void setari_u(var *a, int idx, const char *s)
+{
+ char sidx[sizeof(int)*3 + 1];
+ var *v;
sprintf(sidx, "%d", idx);
v = findvar(iamarray(a), sidx);
@@ -706,8 +774,8 @@ static void setari_u(var *a, int idx, char *s) {
}
/* assign numeric value to variable */
-static var *setvar_i(var *v, double value) {
-
+static var *setvar_i(var *v, double value)
+{
clrvar(v);
v->type |= VF_NUMBER;
v->number = value;
@@ -715,26 +783,26 @@ static var *setvar_i(var *v, double value) {
return v;
}
-static char *getvar_s(var *v) {
-
+static const char *getvar_s(var *v)
+{
/* if v is numeric and has no cached string, convert it to string */
if ((v->type & (VF_NUMBER | VF_CACHED)) == VF_NUMBER) {
- fmt_num(buf, MAXVARFMT, getvar_s(V[CONVFMT]), v->number, TRUE);
- v->string = bb_xstrdup(buf);
+ fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[CONVFMT]), v->number, TRUE);
+ v->string = xstrdup(g_buf);
v->type |= VF_CACHED;
}
return (v->string == NULL) ? "" : v->string;
}
-static double getvar_i(var *v) {
-
+static double getvar_i(var *v)
+{
char *s;
if ((v->type & (VF_NUMBER | VF_CACHED)) == 0) {
v->number = 0;
s = v->string;
if (s && *s) {
- v->number = strtod(s, &s);
+ v->number = my_strtod(&s);
if (v->type & VF_USER) {
skip_spaces(&s);
if (*s != '\0')
@@ -748,67 +816,79 @@ static double getvar_i(var *v) {
return v->number;
}
-static var *copyvar(var *dest, var *src) {
+/* Used for operands of bitwise ops */
+static unsigned long getvar_i_int(var *v)
+{
+ double d = getvar_i(v);
+
+ /* Casting doubles to longs is undefined for values outside
+ * of target type range. Try to widen it as much as possible */
+ if (d >= 0)
+ return (unsigned long)d;
+ /* Why? Think about d == -4294967295.0 (assuming 32bit longs) */
+ return - (long) (unsigned long) (-d);
+}
+static var *copyvar(var *dest, const var *src)
+{
if (dest != src) {
clrvar(dest);
- dest->type |= (src->type & ~VF_DONTTOUCH);
+ dest->type |= (src->type & ~(VF_DONTTOUCH | VF_FSTR));
dest->number = src->number;
if (src->string)
- dest->string = bb_xstrdup(src->string);
+ dest->string = xstrdup(src->string);
}
handle_special(dest);
return dest;
}
-static var *incvar(var *v) {
-
- return setvar_i(v, getvar_i(v)+1.);
+static var *incvar(var *v)
+{
+ return setvar_i(v, getvar_i(v) + 1.);
}
/* return true if v is number or numeric string */
-static int is_numeric(var *v) {
-
+static int is_numeric(var *v)
+{
getvar_i(v);
return ((v->type ^ VF_DIRTY) & (VF_NUMBER | VF_USER | VF_DIRTY));
}
/* return 1 when value of v corresponds to true, 0 otherwise */
-static int istrue(var *v) {
-
+static int istrue(var *v)
+{
if (is_numeric(v))
return (v->number == 0) ? 0 : 1;
- else
- return (v->string && *(v->string)) ? 1 : 0;
+ return (v->string && *(v->string)) ? 1 : 0;
}
-/* temporary varables allocator. Last allocated should be first freed */
-static var *nvalloc(int n) {
-
+/* temporary variables allocator. Last allocated should be first freed */
+static var *nvalloc(int n)
+{
nvblock *pb = NULL;
var *v, *r;
int size;
- while (cb) {
- pb = cb;
- if ((cb->pos - cb->nv) + n <= cb->size) break;
- cb = cb->next;
+ while (g_cb) {
+ pb = g_cb;
+ if ((g_cb->pos - g_cb->nv) + n <= g_cb->size) break;
+ g_cb = g_cb->next;
}
- if (! cb) {
+ if (!g_cb) {
size = (n <= MINNVBLOCK) ? MINNVBLOCK : n;
- cb = (nvblock *)xmalloc(sizeof(nvblock) + size * sizeof(var));
- cb->size = size;
- cb->pos = cb->nv;
- cb->prev = pb;
- cb->next = NULL;
- if (pb) pb->next = cb;
+ g_cb = xzalloc(sizeof(nvblock) + size * sizeof(var));
+ g_cb->size = size;
+ g_cb->pos = g_cb->nv;
+ g_cb->prev = pb;
+ /*g_cb->next = NULL; - xzalloc did it */
+ if (pb) pb->next = g_cb;
}
- v = r = cb->pos;
- cb->pos += n;
+ v = r = g_cb->pos;
+ g_cb->pos += n;
- while (v < cb->pos) {
+ while (v < g_cb->pos) {
v->type = 0;
v->string = NULL;
v++;
@@ -817,15 +897,15 @@ static var *nvalloc(int n) {
return r;
}
-static void nvfree(var *v) {
-
+static void nvfree(var *v)
+{
var *p;
- if (v < cb->nv || v >= cb->pos)
- runtime_error(EMSG_INTERNAL_ERROR);
+ if (v < g_cb->nv || v >= g_cb->pos)
+ syntax_error(EMSG_INTERNAL_ERROR);
- for (p=v; p<cb->pos; p++) {
- if ((p->type & (VF_ARRAY|VF_CHILD)) == VF_ARRAY) {
+ for (p = v; p < g_cb->pos; p++) {
+ if ((p->type & (VF_ARRAY | VF_CHILD)) == VF_ARRAY) {
clear_array(iamarray(p));
free(p->x.array->items);
free(p->x.array);
@@ -836,56 +916,57 @@ static void nvfree(var *v) {
clrvar(p);
}
- cb->pos = v;
- while (cb->prev && cb->pos == cb->nv) {
- cb = cb->prev;
+ g_cb->pos = v;
+ while (g_cb->prev && g_cb->pos == g_cb->nv) {
+ g_cb = g_cb->prev;
}
}
/* ------- awk program text parsing ------- */
-/* Parse next token pointed by global pos, place results into global t.
+/* Parse next token pointed by global pos, place results into global ttt.
* If token isn't expected, give away. Return token class
*/
-static unsigned long next_token(unsigned long expected) {
+static uint32_t next_token(uint32_t expected)
+{
+#define concat_inserted (G.next_token__concat_inserted)
+#define save_tclass (G.next_token__save_tclass)
+#define save_info (G.next_token__save_info)
+/* Initialized to TC_OPTERM: */
+#define ltclass (G.next_token__ltclass)
char *p, *pp, *s;
- char *tl;
- unsigned long tc, *ti;
+ const char *tl;
+ uint32_t tc;
+ const uint32_t *ti;
int l;
- static int concat_inserted = FALSE;
- static unsigned long save_tclass, save_info;
- static unsigned long ltclass = TC_OPTERM;
-
- if (t.rollback) {
- t.rollback = FALSE;
+ if (t_rollback) {
+ t_rollback = FALSE;
} else if (concat_inserted) {
-
concat_inserted = FALSE;
- t.tclass = save_tclass;
- t.info = save_info;
+ t_tclass = save_tclass;
+ t_info = save_info;
} else {
-
- p = pos;
-
- readnext:
+ p = g_pos;
+ readnext:
skip_spaces(&p);
- lineno = t.lineno;
+ g_lineno = t_lineno;
if (*p == '#')
- while (*p != '\n' && *p != '\0') p++;
+ while (*p != '\n' && *p != '\0')
+ p++;
if (*p == '\n')
- t.lineno++;
+ t_lineno++;
if (*p == '\0') {
tc = TC_EOF;
} else if (*p == '\"') {
/* it's a string */
- t.string = s = ++p;
+ t_string = s = ++p;
while (*p != '\"') {
if (*p == '\0' || *p == '\n')
syntax_error(EMSG_UNEXP_EOS);
@@ -897,15 +978,18 @@ static unsigned long next_token(unsigned long expected) {
} else if ((expected & TC_REGEXP) && *p == '/') {
/* it's regexp */
- t.string = s = ++p;
+ t_string = s = ++p;
while (*p != '/') {
if (*p == '\0' || *p == '\n')
syntax_error(EMSG_UNEXP_EOS);
- if ((*s++ = *p++) == '\\') {
+ *s = *p++;
+ if (*s++ == '\\') {
pp = p;
*(s-1) = bb_process_escape_sequence((const char **)&p);
- if (*pp == '\\') *s++ = '\\';
- if (p == pp) *s++ = *p++;
+ if (*pp == '\\')
+ *s++ = '\\';
+ if (p == pp)
+ *s++ = *p++;
}
}
p++;
@@ -914,7 +998,7 @@ static unsigned long next_token(unsigned long expected) {
} else if (*p == '.' || isdigit(*p)) {
/* it's a number */
- t.number = strtod(p, &p);
+ t_double = my_strtod(&p);
if (*p == '.')
syntax_error(EMSG_UNEXP_TOKEN);
tc = TC_NUMBER;
@@ -934,10 +1018,11 @@ static unsigned long next_token(unsigned long expected) {
* matches and it's not a longer word,
* then this is what we are looking for
*/
- if ((tc & (expected | TC_WORD | TC_NEWLINE)) &&
- *tl == *p && strncmp(p, tl, l) == 0 &&
- !((tc & TC_WORD) && isalnum_(*(p + l)))) {
- t.info = *ti;
+ if ((tc & (expected | TC_WORD | TC_NEWLINE))
+ && *tl == *p && strncmp(p, tl, l) == 0
+ && !((tc & TC_WORD) && isalnum_(p[l]))
+ ) {
+ t_info = *ti;
p += l;
break;
}
@@ -945,23 +1030,25 @@ static unsigned long next_token(unsigned long expected) {
tl += l;
}
- if (! *tl) {
+ if (!*tl) {
/* it's a name (var/array/function),
* otherwise it's something wrong
*/
- if (! isalnum_(*p))
+ if (!isalnum_(*p))
syntax_error(EMSG_UNEXP_TOKEN);
- t.string = --p;
- while(isalnum_(*(++p))) {
+ t_string = --p;
+ while (isalnum_(*(++p))) {
*(p-1) = *p;
}
*(p-1) = '\0';
tc = TC_VARIABLE;
+ /* also consume whitespace between functionname and bracket */
+ if (!(expected & TC_VARIABLE))
+ skip_spaces(&p);
if (*p == '(') {
tc = TC_FUNCTION;
} else {
- skip_spaces(&p);
if (*p == '[') {
p++;
tc = TC_ARRAY;
@@ -969,80 +1056,88 @@ static unsigned long next_token(unsigned long expected) {
}
}
}
- pos = p;
+ g_pos = p;
/* skipping newlines in some cases */
if ((ltclass & TC_NOTERM) && (tc & TC_NEWLINE))
goto readnext;
/* insert concatenation operator when needed */
- if ((ltclass&TC_CONCAT1) && (tc&TC_CONCAT2) && (expected&TC_BINOP)) {
+ if ((ltclass & TC_CONCAT1) && (tc & TC_CONCAT2) && (expected & TC_BINOP)) {
concat_inserted = TRUE;
save_tclass = tc;
- save_info = t.info;
+ save_info = t_info;
tc = TC_BINOP;
- t.info = OC_CONCAT | SS | P(35);
+ t_info = OC_CONCAT | SS | P(35);
}
- t.tclass = tc;
+ t_tclass = tc;
}
- ltclass = t.tclass;
+ ltclass = t_tclass;
/* Are we ready for this? */
- if (! (ltclass & expected))
+ if (!(ltclass & expected))
syntax_error((ltclass & (TC_NEWLINE | TC_EOF)) ?
- EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
+ EMSG_UNEXP_EOS : EMSG_UNEXP_TOKEN);
return ltclass;
+#undef concat_inserted
+#undef save_tclass
+#undef save_info
+#undef ltclass
}
-static void rollback_token(void) { t.rollback = TRUE; }
-
-static node *new_node(unsigned long info) {
+static void rollback_token(void)
+{
+ t_rollback = TRUE;
+}
- register node *n;
+static node *new_node(uint32_t info)
+{
+ node *n;
- n = (node *)xcalloc(sizeof(node), 1);
+ n = xzalloc(sizeof(node));
n->info = info;
- n->lineno = lineno;
+ n->lineno = g_lineno;
return n;
}
-static node *mk_re_node(char *s, node *n, regex_t *re) {
-
+static node *mk_re_node(const char *s, node *n, regex_t *re)
+{
n->info = OC_REGEXP;
n->l.re = re;
n->r.ire = re + 1;
xregcomp(re, s, REG_EXTENDED);
- xregcomp(re+1, s, REG_EXTENDED | REG_ICASE);
+ xregcomp(re + 1, s, REG_EXTENDED | REG_ICASE);
return n;
}
-static node *condition(void) {
-
+static node *condition(void)
+{
next_token(TC_SEQSTART);
return parse_expr(TC_SEQTERM);
}
/* parse expression terminated by given argument, return ptr
* to built subtree. Terminator is eaten by parse_expr */
-static node *parse_expr(unsigned long iexp) {
-
+static node *parse_expr(uint32_t iexp)
+{
node sn;
node *cn = &sn;
node *vn, *glptr;
- unsigned long tc, xtc;
+ uint32_t tc, xtc;
var *v;
sn.info = PRIMASK;
sn.r.n = glptr = NULL;
xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP | iexp;
- while (! ((tc = next_token(xtc)) & iexp)) {
- if (glptr && (t.info == (OC_COMPARE|VV|P(39)|2))) {
+ while (!((tc = next_token(xtc)) & iexp)) {
+ if (glptr && (t_info == (OC_COMPARE | VV | P(39) | 2))) {
/* input redirection (<) attached to glptr node */
- cn = glptr->l.n = new_node(OC_CONCAT|SS|P(37));
+ cn = glptr->l.n = new_node(OC_CONCAT | SS | P(37));
+ cn->a.n = glptr;
xtc = TC_OPERAND | TC_UOPPRE;
glptr = NULL;
@@ -1050,17 +1145,17 @@ static node *parse_expr(unsigned long iexp) {
/* for binary and postfix-unary operators, jump back over
* previous operators with higher priority */
vn = cn;
- while ( ((t.info & PRIMASK) > (vn->a.n->info & PRIMASK2)) ||
- ((t.info == vn->info) && ((t.info & OPCLSMASK) == OC_COLON)) )
+ while ( ((t_info & PRIMASK) > (vn->a.n->info & PRIMASK2))
+ || ((t_info == vn->info) && ((t_info & OPCLSMASK) == OC_COLON)) )
vn = vn->a.n;
- if ((t.info & OPCLSMASK) == OC_TERNARY)
- t.info += P(6);
- cn = vn->a.n->r.n = new_node(t.info);
+ if ((t_info & OPCLSMASK) == OC_TERNARY)
+ t_info += P(6);
+ cn = vn->a.n->r.n = new_node(t_info);
cn->a.n = vn->a.n;
if (tc & TC_BINOP) {
cn->l.n = vn;
xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
- if ((t.info & OPCLSMASK) == OC_PGETLINE) {
+ if ((t_info & OPCLSMASK) == OC_PGETLINE) {
/* it's a pipe */
next_token(TC_GETLINE);
/* give maximum priority to this pipe */
@@ -1077,62 +1172,61 @@ static node *parse_expr(unsigned long iexp) {
/* for operands and prefix-unary operators, attach them
* to last node */
vn = cn;
- cn = vn->r.n = new_node(t.info);
+ cn = vn->r.n = new_node(t_info);
cn->a.n = vn;
xtc = TC_OPERAND | TC_UOPPRE | TC_REGEXP;
if (tc & (TC_OPERAND | TC_REGEXP)) {
- xtc = TC_UOPPRE | TC_BINOP | TC_OPERAND | iexp;
- /* one should be very careful with switch on tclass -
+ xtc = TC_UOPPRE | TC_UOPPOST | TC_BINOP | TC_OPERAND | iexp;
+ /* one should be very careful with switch on tclass -
* only simple tclasses should be used! */
switch (tc) {
- case TC_VARIABLE:
- case TC_ARRAY:
+ case TC_VARIABLE:
+ case TC_ARRAY:
cn->info = OC_VAR;
- if ((v = hash_search(ahash, t.string)) != NULL) {
+ v = hash_search(ahash, t_string);
+ if (v != NULL) {
cn->info = OC_FNARG;
cn->l.i = v->x.aidx;
} else {
- cn->l.v = newvar(t.string);
+ cn->l.v = newvar(t_string);
}
if (tc & TC_ARRAY) {
cn->info |= xS;
cn->r.n = parse_expr(TC_ARRTERM);
}
- xtc = TC_UOPPOST | TC_UOPPRE | TC_BINOP | TC_OPERAND | iexp;
break;
-
- case TC_NUMBER:
- case TC_STRING:
+
+ case TC_NUMBER:
+ case TC_STRING:
cn->info = OC_VAR;
- v = cn->l.v = xcalloc(sizeof(var), 1);
+ v = cn->l.v = xzalloc(sizeof(var));
if (tc & TC_NUMBER)
- setvar_i(v, t.number);
+ setvar_i(v, t_double);
else
- setvar_s(v, t.string);
+ setvar_s(v, t_string);
break;
- case TC_REGEXP:
- mk_re_node(t.string, cn,
- (regex_t *)xcalloc(sizeof(regex_t),2));
+ case TC_REGEXP:
+ mk_re_node(t_string, cn, xzalloc(sizeof(regex_t)*2));
break;
- case TC_FUNCTION:
- cn->info = OC_FUNC;
- cn->r.f = newfunc(t.string);
+ case TC_FUNCTION:
+ cn->info = OC_FUNC;
+ cn->r.f = newfunc(t_string);
cn->l.n = condition();
break;
- case TC_SEQSTART:
+ case TC_SEQSTART:
cn = vn->r.n = parse_expr(TC_SEQTERM);
cn->a.n = vn;
break;
- case TC_GETLINE:
+ case TC_GETLINE:
glptr = cn;
xtc = TC_OPERAND | TC_UOPPRE | TC_BINOP | iexp;
break;
- case TC_BUILTIN:
+ case TC_BUILTIN:
cn->l.n = condition();
break;
}
@@ -1143,17 +1237,17 @@ static node *parse_expr(unsigned long iexp) {
}
/* add node to chain. Return ptr to alloc'd node */
-static node *chain_node(unsigned long info) {
-
- register node *n;
+static node *chain_node(uint32_t info)
+{
+ node *n;
- if (! seq->first)
+ if (!seq->first)
seq->first = seq->last = new_node(0);
- if (seq->programname != programname) {
- seq->programname = programname;
+ if (seq->programname != g_progname) {
+ seq->programname = g_progname;
n = chain_node(OC_NEWSOURCE);
- n->l.s = bb_xstrdup(programname);
+ n->l.s = xstrdup(g_progname);
}
n = seq->last;
@@ -1163,18 +1257,18 @@ static node *chain_node(unsigned long info) {
return n;
}
-static void chain_expr(unsigned long info) {
-
+static void chain_expr(uint32_t info)
+{
node *n;
n = chain_node(info);
n->l.n = parse_expr(TC_OPTERM | TC_GRPTERM);
- if (t.tclass & TC_GRPTERM)
+ if (t_tclass & TC_GRPTERM)
rollback_token();
}
-static node *chain_loop(node *nn) {
-
+static node *chain_loop(node *nn)
+{
node *n, *n2, *save_brk, *save_cont;
save_brk = break_ptr;
@@ -1197,9 +1291,9 @@ static node *chain_loop(node *nn) {
}
/* parse group and attach it to chain */
-static void chain_group(void) {
-
- unsigned long c;
+static void chain_group(void)
+{
+ uint32_t c;
node *n, *n2, *n3;
do {
@@ -1207,7 +1301,8 @@ static void chain_group(void) {
} while (c & TC_NEWLINE);
if (c & TC_GRPSTART) {
- while(next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
+ while (next_token(TC_GRPSEQ | TC_GRPTERM) != TC_GRPTERM) {
+ if (t_tclass & TC_NEWLINE) continue;
rollback_token();
chain_group();
}
@@ -1215,100 +1310,99 @@ static void chain_group(void) {
rollback_token();
chain_expr(OC_EXEC | Vx);
} else { /* TC_STATEMNT */
- switch (t.info & OPCLSMASK) {
- case ST_IF:
- n = chain_node(OC_BR | Vx);
- n->l.n = condition();
+ switch (t_info & OPCLSMASK) {
+ case ST_IF:
+ n = chain_node(OC_BR | Vx);
+ n->l.n = condition();
+ chain_group();
+ n2 = chain_node(OC_EXEC);
+ n->r.n = seq->last;
+ if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE) == TC_ELSE) {
chain_group();
- n2 = chain_node(OC_EXEC);
- n->r.n = seq->last;
- if (next_token(TC_GRPSEQ | TC_GRPTERM | TC_ELSE)==TC_ELSE) {
- chain_group();
- n2->a.n = seq->last;
- } else {
- rollback_token();
- }
- break;
-
- case ST_WHILE:
- n2 = condition();
- n = chain_loop(NULL);
- n->l.n = n2;
- break;
+ n2->a.n = seq->last;
+ } else {
+ rollback_token();
+ }
+ break;
- case ST_DO:
- n2 = chain_node(OC_EXEC);
- n = chain_loop(NULL);
- n2->a.n = n->a.n;
- next_token(TC_WHILE);
- n->l.n = condition();
- break;
+ case ST_WHILE:
+ n2 = condition();
+ n = chain_loop(NULL);
+ n->l.n = n2;
+ break;
- case ST_FOR:
- next_token(TC_SEQSTART);
- n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
- if (t.tclass & TC_SEQTERM) { /* for-in */
- if ((n2->info & OPCLSMASK) != OC_IN)
- syntax_error(EMSG_UNEXP_TOKEN);
- n = chain_node(OC_WALKINIT | VV);
- n->l.n = n2->l.n;
- n->r.n = n2->r.n;
- n = chain_loop(NULL);
- n->info = OC_WALKNEXT | Vx;
- n->l.n = n2->l.n;
- } else { /* for(;;) */
- n = chain_node(OC_EXEC | Vx);
- n->l.n = n2;
- n2 = parse_expr(TC_SEMICOL);
- n3 = parse_expr(TC_SEQTERM);
- n = chain_loop(n3);
- n->l.n = n2;
- if (! n2)
- n->info = OC_EXEC;
- }
- break;
+ case ST_DO:
+ n2 = chain_node(OC_EXEC);
+ n = chain_loop(NULL);
+ n2->a.n = n->a.n;
+ next_token(TC_WHILE);
+ n->l.n = condition();
+ break;
- case OC_PRINT:
- case OC_PRINTF:
- n = chain_node(t.info);
- n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
- if (t.tclass & TC_OUTRDR) {
- n->info |= t.info;
- n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
- }
- if (t.tclass & TC_GRPTERM)
- rollback_token();
- break;
+ case ST_FOR:
+ next_token(TC_SEQSTART);
+ n2 = parse_expr(TC_SEMICOL | TC_SEQTERM);
+ if (t_tclass & TC_SEQTERM) { /* for-in */
+ if ((n2->info & OPCLSMASK) != OC_IN)
+ syntax_error(EMSG_UNEXP_TOKEN);
+ n = chain_node(OC_WALKINIT | VV);
+ n->l.n = n2->l.n;
+ n->r.n = n2->r.n;
+ n = chain_loop(NULL);
+ n->info = OC_WALKNEXT | Vx;
+ n->l.n = n2->l.n;
+ } else { /* for (;;) */
+ n = chain_node(OC_EXEC | Vx);
+ n->l.n = n2;
+ n2 = parse_expr(TC_SEMICOL);
+ n3 = parse_expr(TC_SEQTERM);
+ n = chain_loop(n3);
+ n->l.n = n2;
+ if (!n2)
+ n->info = OC_EXEC;
+ }
+ break;
- case OC_BREAK:
- n = chain_node(OC_EXEC);
- n->a.n = break_ptr;
- break;
+ case OC_PRINT:
+ case OC_PRINTF:
+ n = chain_node(t_info);
+ n->l.n = parse_expr(TC_OPTERM | TC_OUTRDR | TC_GRPTERM);
+ if (t_tclass & TC_OUTRDR) {
+ n->info |= t_info;
+ n->r.n = parse_expr(TC_OPTERM | TC_GRPTERM);
+ }
+ if (t_tclass & TC_GRPTERM)
+ rollback_token();
+ break;
- case OC_CONTINUE:
- n = chain_node(OC_EXEC);
- n->a.n = continue_ptr;
- break;
+ case OC_BREAK:
+ n = chain_node(OC_EXEC);
+ n->a.n = break_ptr;
+ break;
- /* delete, next, nextfile, return, exit */
- default:
- chain_expr(t.info);
+ case OC_CONTINUE:
+ n = chain_node(OC_EXEC);
+ n->a.n = continue_ptr;
+ break;
+ /* delete, next, nextfile, return, exit */
+ default:
+ chain_expr(t_info);
}
}
}
-static void parse_program(char *p) {
-
- unsigned long tclass;
+static void parse_program(char *p)
+{
+ uint32_t tclass;
node *cn;
func *f;
var *v;
- pos = p;
- t.lineno = 1;
- while((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
- TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
+ g_pos = p;
+ t_lineno = 1;
+ while ((tclass = next_token(TC_EOF | TC_OPSEQ | TC_GRPSTART |
+ TC_OPTERM | TC_BEGIN | TC_END | TC_FUNCDECL)) != TC_EOF) {
if (tclass & TC_OPTERM)
continue;
@@ -1324,12 +1418,12 @@ static void parse_program(char *p) {
} else if (tclass & TC_FUNCDECL) {
next_token(TC_FUNCTION);
- pos++;
- f = newfunc(t.string);
+ g_pos++;
+ f = newfunc(t_string);
f->body.first = NULL;
f->nargs = 0;
- while(next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
- v = findvar(ahash, t.string);
+ while (next_token(TC_VARIABLE | TC_SEQTERM) & TC_VARIABLE) {
+ v = findvar(ahash, t_string);
v->x.aidx = (f->nargs)++;
if (next_token(TC_COMMA | TC_SEQTERM) & TC_SEQTERM)
@@ -1343,7 +1437,7 @@ static void parse_program(char *p) {
rollback_token();
cn = chain_node(OC_TEST);
cn->l.n = parse_expr(TC_OPTERM | TC_EOF | TC_GRPSTART);
- if (t.tclass & TC_GRPSTART) {
+ if (t_tclass & TC_GRPSTART) {
rollback_token();
chain_group();
} else {
@@ -1361,142 +1455,172 @@ static void parse_program(char *p) {
/* -------- program execution part -------- */
-static node *mk_splitter(char *s, tsplitter *spl) {
-
- register regex_t *re, *ire;
+static node *mk_splitter(const char *s, tsplitter *spl)
+{
+ regex_t *re, *ire;
node *n;
re = &spl->re[0];
ire = &spl->re[1];
n = &spl->n;
- if ((n->info && OPCLSMASK) == OC_REGEXP) {
+ if ((n->info & OPCLSMASK) == OC_REGEXP) {
regfree(re);
- regfree(ire);
+ regfree(ire); // TODO: nuke ire, use re+1?
}
- if (bb_strlen(s) > 1) {
+ if (strlen(s) > 1) {
mk_re_node(s, n, re);
} else {
- n->info = (unsigned long) *s;
+ n->info = (uint32_t) *s;
}
return n;
}
/* use node as a regular expression. Supplied with node ptr and regex_t
- * storage space. Return ptr to regex (if result points to preg, it shuold
+ * storage space. Return ptr to regex (if result points to preg, it should
* be later regfree'd manually
*/
-static regex_t *as_regex(node *op, regex_t *preg) {
-
+static regex_t *as_regex(node *op, regex_t *preg)
+{
+ int cflags;
var *v;
- char *s;
+ const char *s;
if ((op->info & OPCLSMASK) == OC_REGEXP) {
return icase ? op->r.ire : op->l.re;
- } else {
- v = nvalloc(1);
- s = getvar_s(evaluate(op, v));
- xregcomp(preg, s, icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED);
- nvfree(v);
- return preg;
}
+ v = nvalloc(1);
+ s = getvar_s(evaluate(op, v));
+
+ cflags = icase ? REG_EXTENDED | REG_ICASE : REG_EXTENDED;
+ /* Testcase where REG_EXTENDED fails (unpaired '{'):
+ * echo Hi | awk 'gsub("@(samp|code|file)\{","");'
+ * gawk 3.1.5 eats this. We revert to ~REG_EXTENDED
+ * (maybe gsub is not supposed to use REG_EXTENDED?).
+ */
+ if (regcomp(preg, s, cflags)) {
+ cflags &= ~REG_EXTENDED;
+ xregcomp(preg, s, cflags);
+ }
+ nvfree(v);
+ return preg;
}
/* gradually increasing buffer */
-static void qrealloc(char **b, int n, int *size) {
-
- if (! *b || n >= *size)
- *b = xrealloc(*b, *size = n + (n>>1) + 80);
+static void qrealloc(char **b, int n, int *size)
+{
+ if (!*b || n >= *size) {
+ *size = n + (n>>1) + 80;
+ *b = xrealloc(*b, *size);
+ }
}
/* resize field storage space */
-static void fsrealloc(int size) {
-
- static int maxfields = 0;
+static void fsrealloc(int size)
+{
int i;
if (size >= maxfields) {
i = maxfields;
maxfields = size + 16;
- Fields = (var *)xrealloc(Fields, maxfields * sizeof(var));
- for (; i<maxfields; i++) {
+ Fields = xrealloc(Fields, maxfields * sizeof(var));
+ for (; i < maxfields; i++) {
Fields[i].type = VF_SPECIAL;
Fields[i].string = NULL;
}
}
if (size < nfields) {
- for (i=size; i<nfields; i++) {
- clrvar(Fields+i);
+ for (i = size; i < nfields; i++) {
+ clrvar(Fields + i);
}
}
nfields = size;
}
-static int awk_split(char *s, node *spl, char **slist) {
-
- int l, n=0;
+static int awk_split(const char *s, node *spl, char **slist)
+{
+ int l, n = 0;
char c[4];
char *s1;
- regmatch_t pmatch[2];
+ regmatch_t pmatch[2]; // TODO: why [2]? [1] is enough...
/* in worst case, each char would be a separate field */
- *slist = s1 = bb_xstrndup(s, bb_strlen(s) * 2 + 3);
+ *slist = s1 = xzalloc(strlen(s) * 2 + 3);
+ strcpy(s1, s);
c[0] = c[1] = (char)spl->info;
c[2] = c[3] = '\0';
- if (*getvar_s(V[RS]) == '\0') c[2] = '\n';
-
- if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
- while (*s) {
- l = strcspn(s, c+2);
- if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0 &&
- pmatch[0].rm_so <= l) {
+ if (*getvar_s(intvar[RS]) == '\0')
+ c[2] = '\n';
+
+ if ((spl->info & OPCLSMASK) == OC_REGEXP) { /* regex split */
+ if (!*s)
+ return n; /* "": zero fields */
+ n++; /* at least one field will be there */
+ do {
+ l = strcspn(s, c+2); /* len till next NUL or \n */
+ if (regexec(icase ? spl->r.ire : spl->l.re, s, 1, pmatch, 0) == 0
+ && pmatch[0].rm_so <= l
+ ) {
l = pmatch[0].rm_so;
- if (pmatch[0].rm_eo == 0) { l++; pmatch[0].rm_eo++; }
+ if (pmatch[0].rm_eo == 0) {
+ l++;
+ pmatch[0].rm_eo++;
+ }
+ n++; /* we saw yet another delimiter */
} else {
pmatch[0].rm_eo = l;
- if (*(s+l)) pmatch[0].rm_eo++;
+ if (s[l])
+ pmatch[0].rm_eo++;
}
-
memcpy(s1, s, l);
- *(s1+l) = '\0';
+ /* make sure we remove *all* of the separator chars */
+ do {
+ s1[l] = '\0';
+ } while (++l < pmatch[0].rm_eo);
nextword(&s1);
s += pmatch[0].rm_eo;
+ } while (*s);
+ return n;
+ }
+ if (c[0] == '\0') { /* null split */
+ while (*s) {
+ *s1++ = *s++;
+ *s1++ = '\0';
n++;
}
- } else if (c[0] == '\0') { /* null split */
- while(*s) {
- *(s1++) = *(s++);
- *(s1++) = '\0';
- n++;
- }
- } else if (c[0] != ' ') { /* single-character split */
+ return n;
+ }
+ if (c[0] != ' ') { /* single-character split */
if (icase) {
c[0] = toupper(c[0]);
c[1] = tolower(c[1]);
}
if (*s1) n++;
while ((s1 = strpbrk(s1, c))) {
- *(s1++) = '\0';
- n++;
- }
- } else { /* space split */
- while (*s) {
- while (isspace(*s)) s++;
- if (! *s) break;
+ *s1++ = '\0';
n++;
- while (*s && !isspace(*s))
- *(s1++) = *(s++);
- *(s1++) = '\0';
}
+ return n;
+ }
+ /* space split */
+ while (*s) {
+ s = skip_whitespace(s);
+ if (!*s) break;
+ n++;
+ while (*s && !isspace(*s))
+ *s1++ = *s++;
+ *s1++ = '\0';
}
return n;
}
-static void split_f0(void) {
+static void split_f0(void)
+{
+/* static char *fstrings; */
+#define fstrings (G.split_f0__fstrings)
- static char *fstrings = NULL;
int i, n;
char *s;
@@ -1506,42 +1630,44 @@ static void split_f0(void) {
is_f0_split = TRUE;
free(fstrings);
fsrealloc(0);
- n = awk_split(getvar_s(V[F0]), &fsplitter.n, &fstrings);
+ n = awk_split(getvar_s(intvar[F0]), &fsplitter.n, &fstrings);
fsrealloc(n);
s = fstrings;
- for (i=0; i<n; i++) {
+ for (i = 0; i < n; i++) {
Fields[i].string = nextword(&s);
Fields[i].type |= (VF_FSTR | VF_USER | VF_DIRTY);
}
/* set NF manually to avoid side effects */
- clrvar(V[NF]);
- V[NF]->type = VF_NUMBER | VF_SPECIAL;
- V[NF]->number = nfields;
+ clrvar(intvar[NF]);
+ intvar[NF]->type = VF_NUMBER | VF_SPECIAL;
+ intvar[NF]->number = nfields;
+#undef fstrings
}
/* perform additional actions when some internal variables changed */
-static void handle_special(var *v) {
-
+static void handle_special(var *v)
+{
int n;
- char *b, *sep, *s;
+ char *b;
+ const char *sep, *s;
int sl, l, len, i, bsize;
- if (! (v->type & VF_SPECIAL))
+ if (!(v->type & VF_SPECIAL))
return;
- if (v == V[NF]) {
+ if (v == intvar[NF]) {
n = (int)getvar_i(v);
fsrealloc(n);
/* recalculate $0 */
- sep = getvar_s(V[OFS]);
- sl = bb_strlen(sep);
+ sep = getvar_s(intvar[OFS]);
+ sl = strlen(sep);
b = NULL;
len = 0;
- for (i=0; i<n; i++) {
+ for (i = 0; i < n; i++) {
s = getvar_s(&Fields[i]);
- l = bb_strlen(s);
+ l = strlen(s);
if (b) {
memcpy(b+len, sep, sl);
len += sl;
@@ -1550,32 +1676,33 @@ static void handle_special(var *v) {
memcpy(b+len, s, l);
len += l;
}
- b[len] = '\0';
- setvar_p(V[F0], b);
+ if (b)
+ b[len] = '\0';
+ setvar_p(intvar[F0], b);
is_f0_split = TRUE;
- } else if (v == V[F0]) {
+ } else if (v == intvar[F0]) {
is_f0_split = FALSE;
- } else if (v == V[FS]) {
+ } else if (v == intvar[FS]) {
mk_splitter(getvar_s(v), &fsplitter);
- } else if (v == V[RS]) {
+ } else if (v == intvar[RS]) {
mk_splitter(getvar_s(v), &rsplitter);
- } else if (v == V[IGNORECASE]) {
+ } else if (v == intvar[IGNORECASE]) {
icase = istrue(v);
- } else { /* $n */
- n = getvar_i(V[NF]);
- setvar_i(V[NF], n > v-Fields ? n : v-Fields+1);
+ } else { /* $n */
+ n = getvar_i(intvar[NF]);
+ setvar_i(intvar[NF], n > v-Fields ? n : v-Fields+1);
/* right here v is invalid. Just to note... */
}
}
/* step through func/builtin/etc arguments */
-static node *nextarg(node **pn) {
-
+static node *nextarg(node **pn)
+{
node *n;
n = *pn;
@@ -1588,21 +1715,21 @@ static node *nextarg(node **pn) {
return n;
}
-static void hashwalk_init(var *v, xhash *array) {
-
+static void hashwalk_init(var *v, xhash *array)
+{
char **w;
hash_item *hi;
- int i;
+ unsigned i;
if (v->type & VF_WALK)
free(v->x.walker);
v->type |= VF_WALK;
- w = v->x.walker = (char **)xcalloc(2 + 2*sizeof(char *) + array->glen, 1);
- *w = *(w+1) = (char *)(w + 2);
- for (i=0; i<array->csize; i++) {
+ w = v->x.walker = xzalloc(2 + 2*sizeof(char *) + array->glen);
+ w[0] = w[1] = (char *)(w + 2);
+ for (i = 0; i < array->csize; i++) {
hi = array->items[i];
- while(hi) {
+ while (hi) {
strcpy(*w, hi->name);
nextword(w);
hi = hi->next;
@@ -1610,12 +1737,12 @@ static void hashwalk_init(var *v, xhash *array) {
}
}
-static int hashwalk_next(var *v) {
-
+static int hashwalk_next(var *v)
+{
char **w;
w = v->x.walker;
- if (*(w+1) == *w)
+ if (w[1] == w[0])
return FALSE;
setvar_s(v, nextword(w+1));
@@ -1623,38 +1750,41 @@ static int hashwalk_next(var *v) {
}
/* evaluate node, return 1 when result is true, 0 otherwise */
-static int ptest(node *pattern) {
- static var v;
-
- return istrue(evaluate(pattern, &v));
+static int ptest(node *pattern)
+{
+ /* ptest__v is "static": to save stack space? */
+ return istrue(evaluate(pattern, &G.ptest__v));
}
/* read next record from stream rsm into a variable v */
-static int awk_getline(rstream *rsm, var *v) {
-
+static int awk_getline(rstream *rsm, var *v)
+{
char *b;
regmatch_t pmatch[2];
- int p, pp=0, size;
+ int a, p, pp=0, size;
int fd, so, eo, r, rp;
- char c, *s;
+ char c, *m, *s;
/* we're using our own buffer since we need access to accumulating
* characters
*/
fd = fileno(rsm->F);
- b = rsm->buffer;
+ m = rsm->buffer;
+ a = rsm->adv;
p = rsm->pos;
size = rsm->size;
c = (char) rsplitter.n.info;
rp = 0;
+
+ if (!m) qrealloc(&m, 256, &size);
do {
- qrealloc(&b, p+128, &size);
+ b = m + a;
so = eo = p;
r = 1;
if (p > 0) {
if ((rsplitter.n.info & OPCLSMASK) == OC_REGEXP) {
if (regexec(icase ? rsplitter.n.r.ire : rsplitter.n.l.re,
- b, 1, pmatch, 0) == 0) {
+ b, 1, pmatch, 0) == 0) {
so = pmatch[0].rm_so;
eo = pmatch[0].rm_eo;
if (b[eo] != '\0')
@@ -1662,6 +1792,7 @@ static int awk_getline(rstream *rsm, var *v) {
}
} else if (c != '\0') {
s = strchr(b+pp, c);
+ if (!s) s = memchr(b+pp, '\0', p - pp);
if (s) {
so = eo = s-b;
eo++;
@@ -1680,12 +1811,20 @@ static int awk_getline(rstream *rsm, var *v) {
}
}
+ if (a > 0) {
+ memmove(m, (const void *)(m+a), p+1);
+ b = m;
+ a = 0;
+ }
+
+ qrealloc(&m, a+p+128, &size);
+ b = m + a;
pp = p;
p += safe_read(fd, b+p, size-p-1);
if (p < pp) {
p = 0;
r = 0;
- setvar_i(V[ERRNO], errno);
+ setvar_i(intvar[ERRNO], errno);
}
b[p] = '\0';
@@ -1699,77 +1838,79 @@ static int awk_getline(rstream *rsm, var *v) {
v->type |= VF_USER;
b[so] = c;
c = b[eo]; b[eo] = '\0';
- setvar_s(V[RT], b+so);
+ setvar_s(intvar[RT], b+so);
b[eo] = c;
}
- p -= eo;
- if (p) memmove(b, (const void *)(b+eo), p+1);
-
- rsm->buffer = b;
- rsm->pos = p;
+ rsm->buffer = m;
+ rsm->adv = a + eo;
+ rsm->pos = p - eo;
rsm->size = size;
return r;
}
-static int fmt_num(char *b, int size, char *format, double n, int int_as_int) {
-
- int r=0;
- char c, *s=format;
+static int fmt_num(char *b, int size, const char *format, double n, int int_as_int)
+{
+ int r = 0;
+ char c;
+ const char *s = format;
if (int_as_int && n == (int)n) {
r = snprintf(b, size, "%d", (int)n);
} else {
- do { c = *s; } while (*s && *++s);
+ do { c = *s; } while (c && *++s);
if (strchr("diouxX", c)) {
r = snprintf(b, size, format, (int)n);
} else if (strchr("eEfgG", c)) {
r = snprintf(b, size, format, n);
} else {
- runtime_error(EMSG_INV_FMT);
+ syntax_error(EMSG_INV_FMT);
}
}
return r;
}
-
/* formatted output into an allocated buffer, return ptr to buffer */
-static char *awk_printf(node *n) {
-
+static char *awk_printf(node *n)
+{
char *b = NULL;
- char *fmt, *s, *s1, *f;
+ char *fmt, *s, *f;
+ const char *s1;
int i, j, incr, bsize;
char c, c1;
var *v, *arg;
v = nvalloc(1);
- fmt = f = bb_xstrdup(getvar_s(evaluate(nextarg(&n), v)));
+ fmt = f = xstrdup(getvar_s(evaluate(nextarg(&n), v)));
i = 0;
while (*f) {
s = f;
while (*f && (*f != '%' || *(++f) == '%'))
f++;
- while (*f && !isalpha(*f))
+ while (*f && !isalpha(*f)) {
+ if (*f == '*')
+ syntax_error("%*x formats are not supported");
f++;
+ }
incr = (f - s) + MAXVARFMT;
- qrealloc(&b, incr+i, &bsize);
- c = *f; if (c != '\0') f++;
- c1 = *f ; *f = '\0';
+ qrealloc(&b, incr + i, &bsize);
+ c = *f;
+ if (c != '\0') f++;
+ c1 = *f;
+ *f = '\0';
arg = evaluate(nextarg(&n), v);
j = i;
if (c == 'c' || !c) {
- i += sprintf(b+i, s,
- is_numeric(arg) ? (char)getvar_i(arg) : *getvar_s(arg));
-
+ i += sprintf(b+i, s, is_numeric(arg) ?
+ (char)getvar_i(arg) : *getvar_s(arg));
} else if (c == 's') {
- s1 = getvar_s(arg);
- qrealloc(&b, incr+i+bb_strlen(s1), &bsize);
+ s1 = getvar_s(arg);
+ qrealloc(&b, incr+i+strlen(s1), &bsize);
i += sprintf(b+i, s, s1);
-
} else {
i += fmt_num(b+i, incr, s, getvar_i(arg), FALSE);
}
@@ -1777,10 +1918,9 @@ static char *awk_printf(node *n) {
/* if there was an error while sprintf, return value is negative */
if (i < j) i = j;
-
}
- b = xrealloc(b, i+1);
+ b = xrealloc(b, i + 1);
free(fmt);
nvfree(v);
b[i] = '\0';
@@ -1793,22 +1933,23 @@ static char *awk_printf(node *n) {
* all matches. If src or dst is NULL, use $0. If ex=TRUE, enable
* subexpression matching (\1-\9)
*/
-static int awk_sub(node *rn, char *repl, int nm, var *src, var *dest, int ex) {
-
+static int awk_sub(node *rn, const char *repl, int nm, var *src, var *dest, int ex)
+{
char *ds = NULL;
- char *sp, *s;
+ const char *s;
+ const char *sp;
int c, i, j, di, rl, so, eo, nbs, n, dssize;
regmatch_t pmatch[10];
regex_t sreg, *re;
re = as_regex(rn, &sreg);
- if (! src) src = V[F0];
- if (! dest) dest = V[F0];
+ if (!src) src = intvar[F0];
+ if (!dest) dest = intvar[F0];
i = di = 0;
sp = getvar_s(src);
- rl = bb_strlen(repl);
- while (regexec(re, sp, 10, pmatch, sp==getvar_s(src) ? 0:REG_NOTBOL) == 0) {
+ rl = strlen(repl);
+ while (regexec(re, sp, 10, pmatch, sp==getvar_s(src) ? 0 : REG_NOTBOL) == 0) {
so = pmatch[0].rm_so;
eo = pmatch[0].rm_eo;
@@ -1848,7 +1989,8 @@ static int awk_sub(node *rn, char *repl, int nm, var *src, var *dest, int ex) {
sp += eo;
if (i == nm) break;
if (eo == so) {
- if (! (ds[di++] = *sp++)) break;
+ ds[di] = *sp++;
+ if (!ds[di++]) break;
}
}
@@ -1859,18 +2001,19 @@ static int awk_sub(node *rn, char *repl, int nm, var *src, var *dest, int ex) {
return i;
}
-static var *exec_builtin(node *op, var *res) {
+static var *exec_builtin(node *op, var *res)
+{
+#define tspl (G.exec_builtin__tspl)
int (*to_xxx)(int);
var *tv;
node *an[4];
- var *av[4];
- char *as[4];
+ var *av[4];
+ const char *as[4];
regmatch_t pmatch[2];
regex_t sreg, *re;
- static tsplitter tspl;
node *spl;
- unsigned long isr, info;
+ uint32_t isr, info;
int nargs;
time_t tt;
char *s, *s1;
@@ -1881,7 +2024,7 @@ static var *exec_builtin(node *op, var *res) {
op = op->l.n;
av[2] = av[3] = NULL;
- for (i=0 ; i<4 && op ; i++) {
+ for (i = 0; i < 4 && op; i++) {
an[i] = nextarg(&op);
if (isr & 0x09000000) av[i] = evaluate(an[i], &tv[i]);
if (isr & 0x08000000) as[i] = getvar_s(av[i]);
@@ -1889,20 +2032,20 @@ static var *exec_builtin(node *op, var *res) {
}
nargs = i;
- if (nargs < (info >> 30))
- runtime_error(EMSG_TOO_FEW_ARGS);
+ if ((uint32_t)nargs < (info >> 30))
+ syntax_error(EMSG_TOO_FEW_ARGS);
switch (info & OPNMASK) {
- case B_a2:
-#ifdef CONFIG_FEATURE_AWK_MATH
- setvar_i(res, atan2(getvar_i(av[i]), getvar_i(av[1])));
+ case B_a2:
+#if ENABLE_FEATURE_AWK_LIBM
+ setvar_i(res, atan2(getvar_i(av[0]), getvar_i(av[1])));
#else
- runtime_error(EMSG_NO_MATH);
+ syntax_error(EMSG_NO_MATH);
#endif
break;
- case B_sp:
+ case B_sp:
if (nargs > 2) {
spl = (an[2]->info & OPCLSMASK) == OC_REGEXP ?
an[2] : mk_splitter(getvar_s(evaluate(an[2], &tv[2])), &tspl);
@@ -1913,32 +2056,57 @@ static var *exec_builtin(node *op, var *res) {
n = awk_split(as[0], spl, &s);
s1 = s;
clear_array(iamarray(av[1]));
- for (i=1; i<=n; i++)
+ for (i = 1; i <= n; i++)
setari_u(av[1], i, nextword(&s1));
free(s);
setvar_i(res, n);
break;
- case B_ss:
- l = bb_strlen(as[0]);
+ case B_ss:
+ l = strlen(as[0]);
i = getvar_i(av[1]) - 1;
- if (i>l) i=l; if (i<0) i=0;
+ if (i > l) i = l;
+ if (i < 0) i = 0;
n = (nargs > 2) ? getvar_i(av[2]) : l-i;
- if (n<0) n=0;
- s = xmalloc(n+1);
- strncpy(s, as[0]+i, n);
- s[n] = '\0';
+ if (n < 0) n = 0;
+ s = xstrndup(as[0]+i, n);
setvar_p(res, s);
break;
- case B_lo:
+ /* Bitwise ops must assume that operands are unsigned. GNU Awk 3.1.5:
+ * awk '{ print or(-1,1) }' gives "4.29497e+09", not "-2.xxxe+09" */
+ case B_an:
+ setvar_i(res, getvar_i_int(av[0]) & getvar_i_int(av[1]));
+ break;
+
+ case B_co:
+ setvar_i(res, ~getvar_i_int(av[0]));
+ break;
+
+ case B_ls:
+ setvar_i(res, getvar_i_int(av[0]) << getvar_i_int(av[1]));
+ break;
+
+ case B_or:
+ setvar_i(res, getvar_i_int(av[0]) | getvar_i_int(av[1]));
+ break;
+
+ case B_rs:
+ setvar_i(res, getvar_i_int(av[0]) >> getvar_i_int(av[1]));
+ break;
+
+ case B_xo:
+ setvar_i(res, getvar_i_int(av[0]) ^ getvar_i_int(av[1]));
+ break;
+
+ case B_lo:
to_xxx = tolower;
goto lo_cont;
- case B_up:
+ case B_up:
to_xxx = toupper;
-lo_cont:
- s1 = s = bb_xstrdup(as[0]);
+ lo_cont:
+ s1 = s = xstrdup(as[0]);
while (*s1) {
*s1 = (*to_xxx)(*s1);
s1++;
@@ -1946,12 +2114,12 @@ lo_cont:
setvar_p(res, s);
break;
- case B_ix:
+ case B_ix:
n = 0;
- ll = bb_strlen(as[1]);
- l = bb_strlen(as[0]) - ll;
+ ll = strlen(as[1]);
+ l = strlen(as[0]) - ll;
if (ll > 0 && l >= 0) {
- if (! icase) {
+ if (!icase) {
s = strstr(as[0], as[1]);
if (s) n = (s - as[0]) + 1;
} else {
@@ -1969,18 +2137,20 @@ lo_cont:
setvar_i(res, n);
break;
- case B_ti:
+ case B_ti:
if (nargs > 1)
tt = getvar_i(av[1]);
else
time(&tt);
- s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
- i = strftime(buf, MAXVARFMT, s, localtime(&tt));
- buf[i] = '\0';
- setvar_s(res, buf);
+ //s = (nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y";
+ i = strftime(g_buf, MAXVARFMT,
+ ((nargs > 0) ? as[0] : "%a %b %d %H:%M:%S %Z %Y"),
+ localtime(&tt));
+ g_buf[i] = '\0';
+ setvar_s(res, g_buf);
break;
- case B_ma:
+ case B_ma:
re = as_regex(an[1], &sreg);
n = regexec(re, as[0], 1, pmatch, 0);
if (n == 0) {
@@ -1996,21 +2166,22 @@ lo_cont:
if (re == &sreg) regfree(re);
break;
- case B_ge:
+ case B_ge:
awk_sub(an[0], as[1], getvar_i(av[2]), av[3], res, TRUE);
break;
- case B_gs:
+ case B_gs:
setvar_i(res, awk_sub(an[0], as[1], 0, av[2], av[2], FALSE));
break;
- case B_su:
+ case B_su:
setvar_i(res, awk_sub(an[0], as[1], 1, av[2], av[2], FALSE));
break;
}
nvfree(tv);
return res;
+#undef tspl
}
/*
@@ -2019,43 +2190,44 @@ lo_cont:
*/
#define XC(n) ((n) >> 8)
-static var *evaluate(node *op, var *res) {
+static var *evaluate(node *op, var *res)
+{
+/* This procedure is recursive so we should count every byte */
+#define fnargs (G.evaluate__fnargs)
+/* seed is initialized to 1 */
+#define seed (G.evaluate__seed)
+#define sreg (G.evaluate__sreg)
- /* This procedure is recursive so we should count every byte */
- static var *fnargs = NULL;
- static unsigned int seed = 1;
- static regex_t sreg;
node *op1;
var *v1;
union {
var *v;
- char *s;
+ const char *s;
double d;
int i;
} L, R;
- unsigned long opinfo;
- short opn;
+ uint32_t opinfo;
+ int opn;
union {
char *s;
rstream *rsm;
FILE *F;
var *v;
regex_t *re;
- unsigned long info;
+ uint32_t info;
} X;
- if (! op)
+ if (!op)
return setvar_s(res, NULL);
v1 = nvalloc(2);
while (op) {
-
opinfo = op->info;
- opn = (short)(opinfo & OPNMASK);
- lineno = op->lineno;
+ opn = (opinfo & OPNMASK);
+ g_lineno = op->lineno;
- /* execute inevitable things */
+ /* execute inevitable things */
op1 = op->l.n;
if (opinfo & OF_RES1) X.v = L.v = evaluate(op1, v1);
if (opinfo & OF_RES2) R.v = evaluate(op->r.n, v1+1);
@@ -2065,10 +2237,10 @@ static var *evaluate(node *op, var *res) {
switch (XC(opinfo & OPCLSMASK)) {
- /* -- iterative node type -- */
+ /* -- iterative node type -- */
- /* test pattern */
- case XC( OC_TEST ):
+ /* test pattern */
+ case XC( OC_TEST ):
if ((op1->info & OPCLSMASK) == OC_COMMA) {
/* it's range pattern */
if ((opinfo & OF_CHECKED) || ptest(op1->l.n)) {
@@ -2085,80 +2257,81 @@ static var *evaluate(node *op, var *res) {
}
break;
- /* just evaluate an expression, also used as unconditional jump */
- case XC( OC_EXEC ):
- break;
+ /* just evaluate an expression, also used as unconditional jump */
+ case XC( OC_EXEC ):
+ break;
- /* branch, used in if-else and various loops */
- case XC( OC_BR ):
- op = istrue(L.v) ? op->a.n : op->r.n;
+ /* branch, used in if-else and various loops */
+ case XC( OC_BR ):
+ op = istrue(L.v) ? op->a.n : op->r.n;
break;
- /* initialize for-in loop */
- case XC( OC_WALKINIT ):
- hashwalk_init(L.v, iamarray(R.v));
+ /* initialize for-in loop */
+ case XC( OC_WALKINIT ):
+ hashwalk_init(L.v, iamarray(R.v));
break;
- /* get next array item */
- case XC( OC_WALKNEXT ):
+ /* get next array item */
+ case XC( OC_WALKNEXT ):
op = hashwalk_next(L.v) ? op->a.n : op->r.n;
break;
- case XC( OC_PRINT ):
- case XC( OC_PRINTF ):
+ case XC( OC_PRINT ):
+ case XC( OC_PRINTF ):
X.F = stdout;
- if (op->r.n) {
+ if (op->r.n) {
X.rsm = newfile(R.s);
- if (! X.rsm->F) {
+ if (!X.rsm->F) {
if (opn == '|') {
- if((X.rsm->F = popen(R.s, "w")) == NULL)
+ X.rsm->F = popen(R.s, "w");
+ if (X.rsm->F == NULL)
bb_perror_msg_and_die("popen");
X.rsm->is_pipe = 1;
} else {
- X.rsm->F = bb_xfopen(R.s, opn=='w' ? "w" : "a");
+ X.rsm->F = xfopen(R.s, opn=='w' ? "w" : "a");
}
}
X.F = X.rsm->F;
}
if ((opinfo & OPCLSMASK) == OC_PRINT) {
- if (! op1) {
- fputs(getvar_s(V[F0]), X.F);
+ if (!op1) {
+ fputs(getvar_s(intvar[F0]), X.F);
} else {
while (op1) {
L.v = evaluate(nextarg(&op1), v1);
if (L.v->type & VF_NUMBER) {
- fmt_num(buf, MAXVARFMT, getvar_s(V[OFMT]),
- getvar_i(L.v), TRUE);
- fputs(buf, X.F);
+ fmt_num(g_buf, MAXVARFMT, getvar_s(intvar[OFMT]),
+ getvar_i(L.v), TRUE);
+ fputs(g_buf, X.F);
} else {
fputs(getvar_s(L.v), X.F);
}
- if (op1) fputs(getvar_s(V[OFS]), X.F);
+ if (op1) fputs(getvar_s(intvar[OFS]), X.F);
}
}
- fputs(getvar_s(V[ORS]), X.F);
+ fputs(getvar_s(intvar[ORS]), X.F);
} else { /* OC_PRINTF */
L.s = awk_printf(op1);
fputs(L.s, X.F);
- free(L.s);
+ free((char*)L.s);
}
fflush(X.F);
break;
- case XC( OC_DELETE ):
- X.info = op1->info & OPCLSMASK;
- if (X.info == OC_VAR) {
+ case XC( OC_DELETE ):
+ X.info = op1->info & OPCLSMASK;
+ if (X.info == OC_VAR) {
R.v = op1->l.v;
} else if (X.info == OC_FNARG) {
R.v = &fnargs[op1->l.i];
} else {
- runtime_error(EMSG_NOT_ARRAY);
+ syntax_error(EMSG_NOT_ARRAY);
}
- if (op1->r.n) {
+ if (op1->r.n) {
clrvar(L.v);
L.s = getvar_s(evaluate(op1->r.n, v1));
hash_remove(iamarray(R.v), L.s);
@@ -2167,77 +2340,76 @@ static var *evaluate(node *op, var *res) {
}
break;
- case XC( OC_NEWSOURCE ):
- programname = op->l.s;
+ case XC( OC_NEWSOURCE ):
+ g_progname = op->l.s;
break;
- case XC( OC_RETURN ):
+ case XC( OC_RETURN ):
copyvar(res, L.v);
break;
- case XC( OC_NEXTFILE ):
- nextfile = TRUE;
- case XC( OC_NEXT ):
- nextrec = TRUE;
- case XC( OC_DONE ):
+ case XC( OC_NEXTFILE ):
+ nextfile = TRUE;
+ case XC( OC_NEXT ):
+ nextrec = TRUE;
+ case XC( OC_DONE ):
clrvar(res);
break;
- case XC( OC_EXIT ):
- awk_exit(L.d);
+ case XC( OC_EXIT ):
+ awk_exit(L.d);
- /* -- recursive node type -- */
+ /* -- recursive node type -- */
- case XC( OC_VAR ):
- L.v = op->l.v;
- if (L.v == V[NF])
+ case XC( OC_VAR ):
+ L.v = op->l.v;
+ if (L.v == intvar[NF])
split_f0();
goto v_cont;
- case XC( OC_FNARG ):
- L.v = &fnargs[op->l.i];
-
-v_cont:
- res = (op->r.n) ? findvar(iamarray(L.v), R.s) : L.v;
+ case XC( OC_FNARG ):
+ L.v = &fnargs[op->l.i];
+ v_cont:
+ res = op->r.n ? findvar(iamarray(L.v), R.s) : L.v;
break;
- case XC( OC_IN ):
+ case XC( OC_IN ):
setvar_i(res, hash_search(iamarray(R.v), L.s) ? 1 : 0);
break;
- case XC( OC_REGEXP ):
- op1 = op;
- L.s = getvar_s(V[F0]);
+ case XC( OC_REGEXP ):
+ op1 = op;
+ L.s = getvar_s(intvar[F0]);
goto re_cont;
- case XC( OC_MATCH ):
- op1 = op->r.n;
-re_cont:
+ case XC( OC_MATCH ):
+ op1 = op->r.n;
+ re_cont:
X.re = as_regex(op1, &sreg);
R.i = regexec(X.re, L.s, 0, NULL, 0);
if (X.re == &sreg) regfree(X.re);
setvar_i(res, (R.i == 0 ? 1 : 0) ^ (opn == '!' ? 1 : 0));
break;
- case XC( OC_MOVE ):
- /* if source is a temporary string, jusk relink it to dest */
+ case XC( OC_MOVE ):
+ /* if source is a temporary string, jusk relink it to dest */
if (R.v == v1+1 && R.v->string) {
res = setvar_p(L.v, R.v->string);
R.v->string = NULL;
} else {
- res = copyvar(L.v, R.v);
+ res = copyvar(L.v, R.v);
}
break;
- case XC( OC_TERNARY ):
- if ((op->r.n->info & OPCLSMASK) != OC_COLON)
- runtime_error(EMSG_POSSIBLE_ERROR);
+ case XC( OC_TERNARY ):
+ if ((op->r.n->info & OPCLSMASK) != OC_COLON)
+ syntax_error(EMSG_POSSIBLE_ERROR);
res = evaluate(istrue(L.v) ? op->r.n->l.n : op->r.n->r.n, res);
break;
- case XC( OC_FUNC ):
- if (! op->r.f->body.first)
- runtime_error(EMSG_UNDEF_FUNC);
+ case XC( OC_FUNC ):
+ if (!op->r.f->body.first)
+ syntax_error(EMSG_UNDEF_FUNC);
X.v = R.v = nvalloc(op->r.f->nargs+1);
while (op1) {
@@ -2252,115 +2424,114 @@ re_cont:
R.v = fnargs;
fnargs = X.v;
- L.s = programname;
+ L.s = g_progname;
res = evaluate(op->r.f->body.first, res);
- programname = L.s;
+ g_progname = L.s;
nvfree(fnargs);
fnargs = R.v;
break;
- case XC( OC_GETLINE ):
- case XC( OC_PGETLINE ):
- if (op1) {
+ case XC( OC_GETLINE ):
+ case XC( OC_PGETLINE ):
+ if (op1) {
X.rsm = newfile(L.s);
- if (! X.rsm->F) {
+ if (!X.rsm->F) {
if ((opinfo & OPCLSMASK) == OC_PGETLINE) {
X.rsm->F = popen(L.s, "r");
X.rsm->is_pipe = TRUE;
} else {
- X.rsm->F = fopen(L.s, "r"); /* not bb_xfopen! */
+ X.rsm->F = fopen_for_read(L.s); /* not xfopen! */
}
}
} else {
- if (! iF) iF = next_input_file();
+ if (!iF) iF = next_input_file();
X.rsm = iF;
}
- if (! X.rsm->F) {
- setvar_i(V[ERRNO], errno);
+ if (!X.rsm->F) {
+ setvar_i(intvar[ERRNO], errno);
setvar_i(res, -1);
break;
}
- if (! op->r.n)
- R.v = V[F0];
+ if (!op->r.n)
+ R.v = intvar[F0];
L.i = awk_getline(X.rsm, R.v);
if (L.i > 0) {
- if (! op1) {
- incvar(V[FNR]);
- incvar(V[NR]);
+ if (!op1) {
+ incvar(intvar[FNR]);
+ incvar(intvar[NR]);
}
}
setvar_i(res, L.i);
break;
- /* simple builtins */
- case XC( OC_FBLTIN ):
- switch (opn) {
+ /* simple builtins */
+ case XC( OC_FBLTIN ):
+ switch (opn) {
- case F_in:
- R.d = (int)L.d;
+ case F_in:
+ R.d = (int)L.d;
break;
- case F_rn:
- R.d = (double)rand() / (double)RAND_MAX;
+ case F_rn:
+ R.d = (double)rand() / (double)RAND_MAX;
break;
-
-#ifdef CONFIG_FEATURE_AWK_MATH
- case F_co:
- R.d = cos(L.d);
+#if ENABLE_FEATURE_AWK_LIBM
+ case F_co:
+ R.d = cos(L.d);
break;
- case F_ex:
- R.d = exp(L.d);
+ case F_ex:
+ R.d = exp(L.d);
break;
- case F_lg:
- R.d = log(L.d);
+ case F_lg:
+ R.d = log(L.d);
break;
- case F_si:
- R.d = sin(L.d);
+ case F_si:
+ R.d = sin(L.d);
break;
- case F_sq:
- R.d = sqrt(L.d);
+ case F_sq:
+ R.d = sqrt(L.d);
break;
#else
- case F_co:
- case F_ex:
- case F_lg:
- case F_si:
- case F_sq:
- runtime_error(EMSG_NO_MATH);
+ case F_co:
+ case F_ex:
+ case F_lg:
+ case F_si:
+ case F_sq:
+ syntax_error(EMSG_NO_MATH);
break;
#endif
-
- case F_sr:
+ case F_sr:
R.d = (double)seed;
- seed = op1 ? (unsigned int)L.d : (unsigned int)time(NULL);
+ seed = op1 ? (unsigned)L.d : (unsigned)time(NULL);
srand(seed);
break;
- case F_ti:
+ case F_ti:
R.d = time(NULL);
break;
- case F_le:
- if (! op1)
- L.s = getvar_s(V[F0]);
- R.d = bb_strlen(L.s);
+ case F_le:
+ if (!op1)
+ L.s = getvar_s(intvar[F0]);
+ R.d = strlen(L.s);
break;
- case F_sy:
+ case F_sy:
fflush(NULL);
- R.d = (L.s && *L.s) ? system(L.s) : 0;
+ R.d = (ENABLE_FEATURE_ALLOW_EXEC && L.s && *L.s)
+ ? (system(L.s) >> 8) : 0;
break;
- case F_ff:
- if (! op1)
+ case F_ff:
+ if (!op1)
fflush(stdout);
else {
if (L.s && *L.s) {
@@ -2372,7 +2543,7 @@ re_cont:
}
break;
- case F_cl:
+ case F_cl:
X.rsm = (rstream *)hash_search(fdhash, L.s);
if (X.rsm) {
R.i = X.rsm->is_pipe ? pclose(X.rsm->F) : fclose(X.rsm->F);
@@ -2380,118 +2551,117 @@ re_cont:
hash_remove(fdhash, L.s);
}
if (R.i != 0)
- setvar_i(V[ERRNO], errno);
+ setvar_i(intvar[ERRNO], errno);
R.d = (double)R.i;
break;
}
setvar_i(res, R.d);
break;
- case XC( OC_BUILTIN ):
+ case XC( OC_BUILTIN ):
res = exec_builtin(op, res);
break;
- case XC( OC_SPRINTF ):
- setvar_p(res, awk_printf(op1));
+ case XC( OC_SPRINTF ):
+ setvar_p(res, awk_printf(op1));
break;
- case XC( OC_UNARY ):
- X.v = R.v;
- L.d = R.d = getvar_i(R.v);
- switch (opn) {
- case 'P':
- L.d = ++R.d;
+ case XC( OC_UNARY ):
+ X.v = R.v;
+ L.d = R.d = getvar_i(R.v);
+ switch (opn) {
+ case 'P':
+ L.d = ++R.d;
goto r_op_change;
- case 'p':
- R.d++;
+ case 'p':
+ R.d++;
goto r_op_change;
- case 'M':
- L.d = --R.d;
+ case 'M':
+ L.d = --R.d;
goto r_op_change;
- case 'm':
- R.d--;
+ case 'm':
+ R.d--;
goto r_op_change;
- case '!':
- L.d = istrue(X.v) ? 0 : 1;
+ case '!':
+ L.d = istrue(X.v) ? 0 : 1;
break;
- case '-':
- L.d = -R.d;
+ case '-':
+ L.d = -R.d;
break;
- r_op_change:
+ r_op_change:
setvar_i(X.v, R.d);
}
setvar_i(res, L.d);
break;
- case XC( OC_FIELD ):
- R.i = (int)getvar_i(R.v);
- if (R.i == 0) {
- res = V[F0];
+ case XC( OC_FIELD ):
+ R.i = (int)getvar_i(R.v);
+ if (R.i == 0) {
+ res = intvar[F0];
} else {
split_f0();
if (R.i > nfields)
fsrealloc(R.i);
-
- res = &Fields[R.i-1];
+ res = &Fields[R.i - 1];
}
break;
- /* concatenation (" ") and index joining (",") */
- case XC( OC_CONCAT ):
- case XC( OC_COMMA ):
- opn = bb_strlen(L.s) + bb_strlen(R.s) + 2;
- X.s = (char *)xmalloc(opn);
+ /* concatenation (" ") and index joining (",") */
+ case XC( OC_CONCAT ):
+ case XC( OC_COMMA ):
+ opn = strlen(L.s) + strlen(R.s) + 2;
+ X.s = xmalloc(opn);
strcpy(X.s, L.s);
if ((opinfo & OPCLSMASK) == OC_COMMA) {
- L.s = getvar_s(V[SUBSEP]);
- X.s = (char *)xrealloc(X.s, opn + bb_strlen(L.s));
+ L.s = getvar_s(intvar[SUBSEP]);
+ X.s = xrealloc(X.s, opn + strlen(L.s));
strcat(X.s, L.s);
}
strcat(X.s, R.s);
setvar_p(res, X.s);
break;
- case XC( OC_LAND ):
+ case XC( OC_LAND ):
setvar_i(res, istrue(L.v) ? ptest(op->r.n) : 0);
break;
- case XC( OC_LOR ):
+ case XC( OC_LOR ):
setvar_i(res, istrue(L.v) ? 1 : ptest(op->r.n));
break;
- case XC( OC_BINARY ):
- case XC( OC_REPLACE ):
- R.d = getvar_i(R.v);
+ case XC( OC_BINARY ):
+ case XC( OC_REPLACE ):
+ R.d = getvar_i(R.v);
switch (opn) {
- case '+':
- L.d += R.d;
+ case '+':
+ L.d += R.d;
break;
- case '-':
- L.d -= R.d;
+ case '-':
+ L.d -= R.d;
break;
- case '*':
- L.d *= R.d;
+ case '*':
+ L.d *= R.d;
break;
- case '/':
- if (R.d == 0) runtime_error(EMSG_DIV_BY_ZERO);
- L.d /= R.d;
+ case '/':
+ if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO);
+ L.d /= R.d;
break;
- case '&':
-#ifdef CONFIG_FEATURE_AWK_MATH
- L.d = pow(L.d, R.d);
+ case '&':
+#if ENABLE_FEATURE_AWK_LIBM
+ L.d = pow(L.d, R.d);
#else
- runtime_error(EMSG_NO_MATH);
+ syntax_error(EMSG_NO_MATH);
#endif
break;
- case '%':
- if (R.d == 0) runtime_error(EMSG_DIV_BY_ZERO);
- L.d -= (int)(L.d / R.d) * R.d;
+ case '%':
+ if (R.d == 0) syntax_error(EMSG_DIV_BY_ZERO);
+ L.d -= (int)(L.d / R.d) * R.d;
break;
}
- res = setvar_i(((opinfo&OPCLSMASK) == OC_BINARY) ? res : X.v, L.d);
+ res = setvar_i(((opinfo & OPCLSMASK) == OC_BINARY) ? res : X.v, L.d);
break;
- case XC( OC_COMPARE ):
+ case XC( OC_COMPARE ):
if (is_numeric(L.v) && is_numeric(R.v)) {
L.d = getvar_i(L.v) - getvar_i(R.v);
} else {
@@ -2500,21 +2670,21 @@ re_cont:
L.d = icase ? strcasecmp(L.s, R.s) : strcmp(L.s, R.s);
}
switch (opn & 0xfe) {
- case 0:
- R.i = (L.d > 0);
+ case 0:
+ R.i = (L.d > 0);
break;
- case 2:
- R.i = (L.d >= 0);
+ case 2:
+ R.i = (L.d >= 0);
break;
- case 4:
- R.i = (L.d == 0);
+ case 4:
+ R.i = (L.d == 0);
break;
}
setvar_i(res, (opn & 0x1 ? R.i : !R.i) ? 1 : 0);
break;
- default:
- runtime_error(EMSG_POSSIBLE_ERROR);
+ default:
+ syntax_error(EMSG_POSSIBLE_ERROR);
}
if ((opinfo & OPCLSMASK) <= SHIFT_TIL_THIS)
op = op->a.n;
@@ -2525,20 +2695,32 @@ re_cont:
}
nvfree(v1);
return res;
+#undef fnargs
+#undef seed
+#undef sreg
}
/* -------- main & co. -------- */
-static int awk_exit(int r) {
-
- unsigned int i;
+static int awk_exit(int r)
+{
+ var tv;
+ unsigned i;
hash_item *hi;
+ zero_out_var(&tv);
+
+ if (!exiting) {
+ exiting = TRUE;
+ nextrec = FALSE;
+ evaluate(endseq.first, &tv);
+ }
+
/* waiting for children */
- for (i=0; i<fdhash->csize; i++) {
+ for (i = 0; i < fdhash->csize; i++) {
hi = fdhash->items[i];
- while(hi) {
+ while (hi) {
if (hi->data.rs.F && hi->data.rs.is_pipe)
pclose(hi->data.rs.F);
hi = hi->next;
@@ -2550,11 +2732,11 @@ static int awk_exit(int r) {
/* if expr looks like "var=value", perform assignment and return 1,
* otherwise return 0 */
-static int is_assignment(char *expr) {
-
+static int is_assignment(const char *expr)
+{
char *exprc, *s, *s0, *s1;
- exprc = bb_xstrdup(expr);
+ exprc = xstrdup(expr);
if (!isalnum_(*exprc) || (s = strchr(exprc, '=')) == NULL) {
free(exprc);
return FALSE;
@@ -2572,51 +2754,65 @@ static int is_assignment(char *expr) {
}
/* switch to next input file */
-static rstream *next_input_file(void) {
+static rstream *next_input_file(void)
+{
+#define rsm (G.next_input_file__rsm)
+#define files_happen (G.next_input_file__files_happen)
- static rstream rsm;
FILE *F = NULL;
- char *fname, *ind;
- static int files_happen = FALSE;
+ const char *fname, *ind;
if (rsm.F) fclose(rsm.F);
rsm.F = NULL;
- rsm.pos = 0;
+ rsm.pos = rsm.adv = 0;
do {
- if (getvar_i(V[ARGIND])+1 >= getvar_i(V[ARGC])) {
+ if (getvar_i(intvar[ARGIND])+1 >= getvar_i(intvar[ARGC])) {
if (files_happen)
return NULL;
fname = "-";
F = stdin;
} else {
- ind = getvar_s(incvar(V[ARGIND]));
- fname = getvar_s(findvar(iamarray(V[ARGV]), ind));
+ ind = getvar_s(incvar(intvar[ARGIND]));
+ fname = getvar_s(findvar(iamarray(intvar[ARGV]), ind));
if (fname && *fname && !is_assignment(fname))
- F = afopen(fname, "r");
+ F = xfopen_stdin(fname);
}
} while (!F);
files_happen = TRUE;
- setvar_s(V[FILENAME], fname);
+ setvar_s(intvar[FILENAME], fname);
rsm.F = F;
return &rsm;
+#undef rsm
+#undef files_happen
}
-extern int awk_main(int argc, char **argv) {
-
- char *s, *s1;
- int i, j, c;
+int awk_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int awk_main(int argc, char **argv)
+{
+ unsigned opt;
+ char *opt_F, *opt_W;
+ llist_t *list_v = NULL;
+ llist_t *list_f = NULL;
+ int i, j;
var *v;
- static var tv;
+ var tv;
char **envp;
- static int from_file = FALSE;
- rstream *rsm;
- FILE *F, *stdfiles[3];
- static char * stdnames = "/dev/stdin\0/dev/stdout\0/dev/stderr";
+ char *vnames = (char *)vNames; /* cheat */
+ char *vvalues = (char *)vValues;
+
+ INIT_G();
+
+ /* Undo busybox.c, or else strtod may eat ','! This breaks parsing:
+ * $1,$2 == '$1,' '$2', NOT '$1' ',' '$2' */
+ if (ENABLE_LOCALE_SUPPORT)
+ setlocale(LC_NUMERIC, "C");
+
+ zero_out_var(&tv);
/* allocate global buffer */
- buf = xmalloc(MAXVARFMT+1);
+ g_buf = xmalloc(MAXVARFMT + 1);
vhash = hash_init();
ahash = hash_init();
@@ -2624,118 +2820,111 @@ extern int awk_main(int argc, char **argv) {
fnhash = hash_init();
/* initialize variables */
- for (i=0; *vNames; i++) {
- V[i] = v = newvar(nextword(&vNames));
- if (*vValues != '\377')
- setvar_s(v, nextword(&vValues));
+ for (i = 0; *vnames; i++) {
+ intvar[i] = v = newvar(nextword(&vnames));
+ if (*vvalues != '\377')
+ setvar_s(v, nextword(&vvalues));
else
setvar_i(v, 0);
- if (*vNames == '*') {
+ if (*vnames == '*') {
v->type |= VF_SPECIAL;
- vNames++;
+ vnames++;
}
}
- handle_special(V[FS]);
- handle_special(V[RS]);
-
- stdfiles[0] = stdin;
- stdfiles[1] = stdout;
- stdfiles[2] = stderr;
- for (i=0; i<3; i++) {
- rsm = newfile(nextword(&stdnames));
- rsm->F = stdfiles[i];
- }
-
- for (envp=environ; *envp; envp++) {
- s = bb_xstrdup(*envp);
- s1 = strchr(s, '=');
- *(s1++) = '\0';
- setvar_u(findvar(iamarray(V[ENVIRON]), s), s1);
- free(s);
- }
-
- while((c = getopt(argc, argv, "F:v:f:W:")) != EOF) {
- switch (c) {
- case 'F':
- setvar_s(V[FS], optarg);
- break;
- case 'v':
- if (! is_assignment(optarg))
- bb_show_usage();
- break;
- case 'f':
- from_file = TRUE;
- F = afopen(programname = optarg, "r");
- s = NULL;
- /* one byte is reserved for some trick in next_token */
- for (i=j=1; j>0; i+=j) {
- s = (char *)xrealloc(s, i+4096);
- j = fread(s+i, 1, 4094, F);
- }
- s[i] = '\0';
- fclose(F);
- parse_program(s+1);
- free(s);
- break;
- case 'W':
- bb_error_msg("Warning: unrecognized option '-W %s' ignored\n", optarg);
- break;
-
- default:
- bb_show_usage();
+ handle_special(intvar[FS]);
+ handle_special(intvar[RS]);
+
+ newfile("/dev/stdin")->F = stdin;
+ newfile("/dev/stdout")->F = stdout;
+ newfile("/dev/stderr")->F = stderr;
+
+ /* Huh, people report that sometimes environ is NULL. Oh well. */
+ if (environ) for (envp = environ; *envp; envp++) {
+ /* environ is writable, thus we don't strdup it needlessly */
+ char *s = *envp;
+ char *s1 = strchr(s, '=');
+ if (s1) {
+ *s1 = '\0';
+ /* Both findvar and setvar_u take const char*
+ * as 2nd arg -> environment is not trashed */
+ setvar_u(findvar(iamarray(intvar[ENVIRON]), s), s1 + 1);
+ *s1 = '=';
}
}
-
- if (!from_file) {
- if (argc == optind)
+ opt_complementary = "v::f::"; /* -v and -f can occur multiple times */
+ opt = getopt32(argv, "F:v:f:W:", &opt_F, &list_v, &list_f, &opt_W);
+ argv += optind;
+ argc -= optind;
+ if (opt & 0x1)
+ setvar_s(intvar[FS], opt_F); // -F
+ while (list_v) { /* -v */
+ if (!is_assignment(llist_pop(&list_v)))
bb_show_usage();
- programname="cmd. line";
- parse_program(argv[optind++]);
-
}
+ if (list_f) { /* -f */
+ do {
+ char *s = NULL;
+ FILE *from_file;
+
+ g_progname = llist_pop(&list_f);
+ from_file = xfopen_stdin(g_progname);
+ /* one byte is reserved for some trick in next_token */
+ for (i = j = 1; j > 0; i += j) {
+ s = xrealloc(s, i + 4096);
+ j = fread(s + i, 1, 4094, from_file);
+ }
+ s[i] = '\0';
+ fclose(from_file);
+ parse_program(s + 1);
+ free(s);
+ } while (list_f);
+ argc++;
+ } else { // no -f: take program from 1st parameter
+ if (!argc)
+ bb_show_usage();
+ g_progname = "cmd. line";
+ parse_program(*argv++);
+ }
+ if (opt & 0x8) // -W
+ bb_error_msg("warning: unrecognized option '-W %s' ignored", opt_W);
/* fill in ARGV array */
- setvar_i(V[ARGC], argc - optind + 1);
- setari_u(V[ARGV], 0, "awk");
- for(i=optind; i < argc; i++)
- setari_u(V[ARGV], i+1-optind, argv[i]);
+ setvar_i(intvar[ARGC], argc);
+ setari_u(intvar[ARGV], 0, "awk");
+ i = 0;
+ while (*argv)
+ setari_u(intvar[ARGV], ++i, *argv++);
evaluate(beginseq.first, &tv);
- if (! mainseq.first && ! endseq.first)
+ if (!mainseq.first && !endseq.first)
awk_exit(EXIT_SUCCESS);
/* input file could already be opened in BEGIN block */
- if (! iF) iF = next_input_file();
+ if (!iF) iF = next_input_file();
/* passing through input files */
while (iF) {
-
nextfile = FALSE;
- setvar_i(V[FNR], 0);
-
- while ((c = awk_getline(iF, V[F0])) > 0) {
+ setvar_i(intvar[FNR], 0);
+ while ((i = awk_getline(iF, intvar[F0])) > 0) {
nextrec = FALSE;
- incvar(V[NR]);
- incvar(V[FNR]);
+ incvar(intvar[NR]);
+ incvar(intvar[FNR]);
evaluate(mainseq.first, &tv);
if (nextfile)
break;
}
- if (c < 0)
- runtime_error(strerror(errno));
+ if (i < 0)
+ syntax_error(strerror(errno));
iF = next_input_file();
-
}
- evaluate(endseq.first, &tv);
awk_exit(EXIT_SUCCESS);
-
- return 0;
+ /*return 0;*/
}
-
diff --git a/release/src/router/busybox/editors/cmp.c b/release/src/router/busybox/editors/cmp.c
new file mode 100644
index 00000000..2e98e6e2
--- /dev/null
+++ b/release/src/router/busybox/editors/cmp.c
@@ -0,0 +1,135 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini cmp implementation for busybox
+ *
+ * Copyright (C) 2000,2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+/* BB_AUDIT SUSv3 (virtually) compliant -- uses nicer GNU format for -l. */
+/* http://www.opengroup.org/onlinepubs/007904975/utilities/cmp.html */
+
+/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
+ *
+ * Original version majorly reworked for SUSv3 compliance, bug fixes, and
+ * size optimizations. Changes include:
+ * 1) Now correctly distinguishes between errors and actual file differences.
+ * 2) Proper handling of '-' args.
+ * 3) Actual error checking of i/o.
+ * 4) Accept SUSv3 -l option. Note that we use the slightly nicer gnu format
+ * in the '-l' case.
+ */
+
+#include "libbb.h"
+
+static const char fmt_eof[] ALIGN1 = "cmp: EOF on %s\n";
+static const char fmt_differ[] ALIGN1 = "%s %s differ: char %"OFF_FMT"d, line %d\n";
+// This fmt_l_opt uses gnu-isms. SUSv3 would be "%.0s%.0s%"OFF_FMT"d %o %o\n"
+static const char fmt_l_opt[] ALIGN1 = "%.0s%.0s%"OFF_FMT"d %3o %3o\n";
+
+static const char opt_chars[] ALIGN1 = "sl";
+#define CMP_OPT_s (1<<0)
+#define CMP_OPT_l (1<<1)
+
+int cmp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int cmp_main(int argc UNUSED_PARAM, char **argv)
+{
+ FILE *fp1, *fp2, *outfile = stdout;
+ const char *filename1, *filename2 = "-";
+ USE_DESKTOP(off_t skip1 = 0, skip2 = 0;)
+ off_t char_pos = 0;
+ int line_pos = 1; /* Hopefully won't overflow... */
+ const char *fmt;
+ int c1, c2;
+ unsigned opt;
+ int retval = 0;
+
+ xfunc_error_retval = 2; /* 1 is returned if files are different. */
+
+ opt_complementary = "-1"
+ USE_DESKTOP(":?4")
+ SKIP_DESKTOP(":?2")
+ ":l--s:s--l";
+ opt = getopt32(argv, opt_chars);
+ argv += optind;
+
+ filename1 = *argv;
+ fp1 = xfopen_stdin(filename1);
+
+ if (*++argv) {
+ filename2 = *argv;
+#if ENABLE_DESKTOP
+ if (*++argv) {
+ skip1 = XATOOFF(*argv);
+ if (*++argv) {
+ skip2 = XATOOFF(*argv);
+ }
+ }
+#endif
+ }
+
+ fp2 = xfopen_stdin(filename2);
+ if (fp1 == fp2) { /* Paranoia check... stdin == stdin? */
+ /* Note that we don't bother reading stdin. Neither does gnu wc.
+ * But perhaps we should, so that other apps down the chain don't
+ * get the input. Consider 'echo hello | (cmp - - && cat -)'.
+ */
+ return 0;
+ }
+
+ if (opt & CMP_OPT_l)
+ fmt = fmt_l_opt;
+ else
+ fmt = fmt_differ;
+
+#if ENABLE_DESKTOP
+ while (skip1) { getc(fp1); skip1--; }
+ while (skip2) { getc(fp2); skip2--; }
+#endif
+ do {
+ c1 = getc(fp1);
+ c2 = getc(fp2);
+ ++char_pos;
+ if (c1 != c2) { /* Remember: a read error may have occurred. */
+ retval = 1; /* But assume the files are different for now. */
+ if (c2 == EOF) {
+ /* We know that fp1 isn't at EOF or in an error state. But to
+ * save space below, things are setup to expect an EOF in fp1
+ * if an EOF occurred. So, swap things around.
+ */
+ fp1 = fp2;
+ filename1 = filename2;
+ c1 = c2;
+ }
+ if (c1 == EOF) {
+ die_if_ferror(fp1, filename1);
+ fmt = fmt_eof; /* Well, no error, so it must really be EOF. */
+ outfile = stderr;
+ /* There may have been output to stdout (option -l), so
+ * make sure we fflush before writing to stderr. */
+ xfflush_stdout();
+ }
+ if (!(opt & CMP_OPT_s)) {
+ if (opt & CMP_OPT_l) {
+ line_pos = c1; /* line_pos is unused in the -l case. */
+ }
+ fprintf(outfile, fmt, filename1, filename2, char_pos, line_pos, c2);
+ if (opt) { /* This must be -l since not -s. */
+ /* If we encountered an EOF,
+ * the while check will catch it. */
+ continue;
+ }
+ }
+ break;
+ }
+ if (c1 == '\n') {
+ ++line_pos;
+ }
+ } while (c1 != EOF);
+
+ die_if_ferror(fp1, filename1);
+ die_if_ferror(fp2, filename2);
+
+ fflush_stdout_and_exit(retval);
+}
diff --git a/release/src/router/busybox/editors/diff.c b/release/src/router/busybox/editors/diff.c
new file mode 100644
index 00000000..7fce70d0
--- /dev/null
+++ b/release/src/router/busybox/editors/diff.c
@@ -0,0 +1,1342 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Mini diff implementation for busybox, adapted from OpenBSD diff.
+ *
+ * Copyright (C) 2006 by Robert Sullivan <cogito.ergo.cogito@hotmail.com>
+ * Copyright (c) 2003 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Sponsored in part by the Defense Advanced Research Projects
+ * Agency (DARPA) and Air Force Research Laboratory, Air Force
+ * Materiel Command, USAF, under agreement number F39502-99-1-0512.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
+
+#include "libbb.h"
+
+// #define FSIZE_MAX 32768
+
+/* NOINLINEs added to prevent gcc from merging too much into diffreg()
+ * (it bites more than it can (efficiently) chew). */
+
+/*
+ * Output flags
+ */
+enum {
+ /* Print a header/footer between files */
+ /* D_HEADER = 1, - unused */
+ /* Treat file as empty (/dev/null) */
+ D_EMPTY1 = 2 * ENABLE_FEATURE_DIFF_DIR,
+ D_EMPTY2 = 4 * ENABLE_FEATURE_DIFF_DIR,
+};
+
+/*
+ * Status values for print_status() and diffreg() return values
+ * Guide:
+ * D_SAME - files are the same
+ * D_DIFFER - files differ
+ * D_BINARY - binary files differ
+ * D_COMMON - subdirectory common to both dirs
+ * D_ONLY - file only exists in one dir
+ * D_ISDIR1 - path1 a dir, path2 a file
+ * D_ISDIR2 - path1 a file, path2 a dir
+ * D_ERROR - error occurred
+ * D_SKIPPED1 - skipped path1 as it is a special file
+ * D_SKIPPED2 - skipped path2 as it is a special file
+ */
+#define D_SAME 0
+#define D_DIFFER (1 << 0)
+#define D_BINARY (1 << 1)
+#define D_COMMON (1 << 2)
+/*#define D_ONLY (1 << 3) - unused */
+#define D_ISDIR1 (1 << 4)
+#define D_ISDIR2 (1 << 5)
+#define D_ERROR (1 << 6)
+#define D_SKIPPED1 (1 << 7)
+#define D_SKIPPED2 (1 << 8)
+
+/* Command line options */
+#define FLAG_a (1 << 0)
+#define FLAG_b (1 << 1)
+#define FLAG_d (1 << 2)
+#define FLAG_i (1 << 3)
+#define FLAG_L (1 << 4)
+#define FLAG_N (1 << 5)
+#define FLAG_q (1 << 6)
+#define FLAG_r (1 << 7)
+#define FLAG_s (1 << 8)
+#define FLAG_S (1 << 9)
+#define FLAG_t (1 << 10)
+#define FLAG_T (1 << 11)
+#define FLAG_U (1 << 12)
+#define FLAG_w (1 << 13)
+
+
+struct cand {
+ int x;
+ int y;
+ int pred;
+};
+
+struct line {
+ int serial;
+ int value;
+};
+
+/*
+ * The following struct is used to record change information
+ * doing a "context" or "unified" diff. (see routine "change" to
+ * understand the highly mnemonic field names)
+ */
+struct context_vec {
+ int a; /* start line in old file */
+ int b; /* end line in old file */
+ int c; /* start line in new file */
+ int d; /* end line in new file */
+};
+
+
+#define g_read_buf bb_common_bufsiz1
+
+struct globals {
+ bool anychange;
+ smallint exit_status;
+ int opt_U_context;
+ size_t max_context; /* size of context_vec_start */
+ USE_FEATURE_DIFF_DIR(int dl_count;)
+ USE_FEATURE_DIFF_DIR(char **dl;)
+ char *opt_S_start;
+ const char *label1;
+ const char *label2;
+ int *J; /* will be overlaid on class */
+ int clen;
+ int pref, suff; /* length of prefix and suffix */
+ int nlen[2];
+ int slen[2];
+ int clistlen; /* the length of clist */
+ struct cand *clist; /* merely a free storage pot for candidates */
+ long *ixnew; /* will be overlaid on nfile[1] */
+ long *ixold; /* will be overlaid on klist */
+ struct line *nfile[2];
+ struct line *sfile[2]; /* shortened by pruning common prefix/suffix */
+ struct context_vec *context_vec_start;
+ struct context_vec *context_vec_end;
+ struct context_vec *context_vec_ptr;
+ char *tempname1, *tempname2;
+ struct stat stb1, stb2;
+};
+#define G (*ptr_to_globals)
+#define anychange (G.anychange )
+#define exit_status (G.exit_status )
+#define opt_U_context (G.opt_U_context )
+#define max_context (G.max_context )
+#define dl_count (G.dl_count )
+#define dl (G.dl )
+#define opt_S_start (G.opt_S_start )
+#define label1 (G.label1 )
+#define label2 (G.label2 )
+#define J (G.J )
+#define clen (G.clen )
+#define pref (G.pref )
+#define suff (G.suff )
+#define nlen (G.nlen )
+#define slen (G.slen )
+#define clistlen (G.clistlen )
+#define clist (G.clist )
+#define ixnew (G.ixnew )
+#define ixold (G.ixold )
+#define nfile (G.nfile )
+#define sfile (G.sfile )
+#define context_vec_start (G.context_vec_start )
+#define context_vec_end (G.context_vec_end )
+#define context_vec_ptr (G.context_vec_ptr )
+#define stb1 (G.stb1 )
+#define stb2 (G.stb2 )
+#define tempname1 (G.tempname1 )
+#define tempname2 (G.tempname2 )
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+ opt_U_context = 3; \
+ max_context = 64; \
+} while (0)
+
+
+#if ENABLE_FEATURE_DIFF_DIR
+static void print_only(const char *path, const char *entry)
+{
+ printf("Only in %s: %s\n", path, entry);
+}
+#endif
+
+
+static void print_status(int val, char *_path1, char *_path2)
+{
+ /*const char *const _entry = entry ? entry : "";*/
+ /*char *const _path1 = entry ? concat_path_file(path1, _entry) : path1;*/
+ /*char *const _path2 = entry ? concat_path_file(path2, _entry) : path2;*/
+
+ switch (val) {
+/* case D_ONLY:
+ print_only(path1, entry);
+ break;
+*/
+ case D_COMMON:
+ printf("Common subdirectories: %s and %s\n", _path1, _path2);
+ break;
+ case D_BINARY:
+ printf("Binary files %s and %s differ\n", _path1, _path2);
+ break;
+ case D_DIFFER:
+ if (option_mask32 & FLAG_q)
+ printf("Files %s and %s differ\n", _path1, _path2);
+ break;
+ case D_SAME:
+ if (option_mask32 & FLAG_s)
+ printf("Files %s and %s are identical\n", _path1, _path2);
+ break;
+ case D_ISDIR1:
+ printf("File %s is a %s while file %s is a %s\n",
+ _path1, "directory", _path2, "regular file");
+ break;
+ case D_ISDIR2:
+ printf("File %s is a %s while file %s is a %s\n",
+ _path1, "regular file", _path2, "directory");
+ break;
+ case D_SKIPPED1:
+ printf("File %s is not a regular file or directory and was skipped\n",
+ _path1);
+ break;
+ case D_SKIPPED2:
+ printf("File %s is not a regular file or directory and was skipped\n",
+ _path2);
+ break;
+ }
+/*
+ if (entry) {
+ free(_path1);
+ free(_path2);
+ }
+*/
+}
+
+
+/* Read line, return its nonzero hash. Return 0 if EOF.
+ *
+ * Hash function taken from Robert Sedgewick, Algorithms in C, 3d ed., p 578.
+ */
+static ALWAYS_INLINE int fiddle_sum(int sum, int t)
+{
+ return sum * 127 + t;
+}
+static int readhash(FILE *fp)
+{
+ int i, t, space;
+ int sum;
+
+ sum = 1;
+ space = 0;
+ i = 0;
+ if (!(option_mask32 & (FLAG_b | FLAG_w))) {
+ while ((t = getc(fp)) != '\n') {
+ if (t == EOF) {
+ if (i == 0)
+ return 0;
+ break;
+ }
+ sum = fiddle_sum(sum, t);
+ i = 1;
+ }
+ } else {
+ while (1) {
+ switch (t = getc(fp)) {
+ case '\t':
+ case '\r':
+ case '\v':
+ case '\f':
+ case ' ':
+ space = 1;
+ continue;
+ default:
+ if (space && !(option_mask32 & FLAG_w)) {
+ i = 1;
+ space = 0;
+ }
+ sum = fiddle_sum(sum, t);
+ i = 1;
+ continue;
+ case EOF:
+ if (i == 0)
+ return 0;
+ /* FALLTHROUGH */
+ case '\n':
+ break;
+ }
+ break;
+ }
+ }
+ /*
+ * There is a remote possibility that we end up with a zero sum.
+ * Zero is used as an EOF marker, so return 1 instead.
+ */
+ return (sum == 0 ? 1 : sum);
+}
+
+
+/* Our diff implementation is using seek.
+ * When we meet non-seekable file, we must make a temp copy.
+ */
+static char *make_temp(FILE *f, struct stat *sb)
+{
+ char *name;
+ int fd;
+
+ if (S_ISREG(sb->st_mode) || S_ISBLK(sb->st_mode))
+ return NULL;
+ name = xstrdup("/tmp/difXXXXXX");
+ fd = mkstemp(name);
+ if (fd < 0)
+ bb_perror_msg_and_die("mkstemp");
+ if (bb_copyfd_eof(fileno(f), fd) < 0) {
+ clean_up:
+ unlink(name);
+ xfunc_die(); /* error message is printed by bb_copyfd_eof */
+ }
+ fstat(fd, sb);
+ close(fd);
+ if (freopen(name, "r+", f) == NULL) {
+ bb_perror_msg("freopen");
+ goto clean_up;
+ }
+ return name;
+}
+
+
+/*
+ * Check to see if the given files differ.
+ * Returns 0 if they are the same, 1 if different, and -1 on error.
+ */
+static NOINLINE int files_differ(FILE *f1, FILE *f2)
+{
+ size_t i, j;
+
+ /* Prevent making copies for "/dev/null" (too common) */
+ /* Deal with input from pipes etc */
+ tempname1 = make_temp(f1, &stb1);
+ tempname2 = make_temp(f2, &stb2);
+ if (stb1.st_size != stb2.st_size) {
+ return 1;
+ }
+ while (1) {
+ i = fread(g_read_buf, 1, COMMON_BUFSIZE/2, f1);
+ j = fread(g_read_buf + COMMON_BUFSIZE/2, 1, COMMON_BUFSIZE/2, f2);
+ if (i != j)
+ return 1;
+ if (i == 0)
+ return (ferror(f1) || ferror(f2)) ? -1 : 0;
+ if (memcmp(g_read_buf,
+ g_read_buf + COMMON_BUFSIZE/2, i) != 0)
+ return 1;
+ }
+}
+
+
+static void prepare(int i, FILE *fp /*, off_t filesize*/)
+{
+ struct line *p;
+ int h;
+ size_t j, sz;
+
+ rewind(fp);
+
+ /*sz = (filesize <= FSIZE_MAX ? filesize : FSIZE_MAX) / 25;*/
+ /*if (sz < 100)*/
+ sz = 100;
+
+ p = xmalloc((sz + 3) * sizeof(p[0]));
+ j = 0;
+ while ((h = readhash(fp)) != 0) { /* while not EOF */
+ if (j == sz) {
+ sz = sz * 3 / 2;
+ p = xrealloc(p, (sz + 3) * sizeof(p[0]));
+ }
+ p[++j].value = h;
+ }
+ nlen[i] = j;
+ nfile[i] = p;
+}
+
+
+static void prune(void)
+{
+ int i, j;
+
+ for (pref = 0; pref < nlen[0] && pref < nlen[1] &&
+ nfile[0][pref + 1].value == nfile[1][pref + 1].value; pref++)
+ continue;
+ for (suff = 0; suff < nlen[0] - pref && suff < nlen[1] - pref &&
+ nfile[0][nlen[0] - suff].value == nfile[1][nlen[1] - suff].value;
+ suff++)
+ continue;
+ for (j = 0; j < 2; j++) {
+ sfile[j] = nfile[j] + pref;
+ slen[j] = nlen[j] - pref - suff;
+ for (i = 0; i <= slen[j]; i++)
+ sfile[j][i].serial = i;
+ }
+}
+
+
+static void equiv(struct line *a, int n, struct line *b, int m, int *c)
+{
+ int i, j;
+
+ i = j = 1;
+ while (i <= n && j <= m) {
+ if (a[i].value < b[j].value)
+ a[i++].value = 0;
+ else if (a[i].value == b[j].value)
+ a[i++].value = j;
+ else
+ j++;
+ }
+ while (i <= n)
+ a[i++].value = 0;
+ b[m + 1].value = 0;
+ j = 0;
+ while (++j <= m) {
+ c[j] = -b[j].serial;
+ while (b[j + 1].value == b[j].value) {
+ j++;
+ c[j] = b[j].serial;
+ }
+ }
+ c[j] = -1;
+}
+
+
+static int isqrt(int n)
+{
+ int y, x;
+
+ if (n == 0)
+ return 0;
+ x = 1;
+ do {
+ y = x;
+ x = n / x;
+ x += y;
+ x /= 2;
+ } while ((x - y) > 1 || (x - y) < -1);
+
+ return x;
+}
+
+
+static int newcand(int x, int y, int pred)
+{
+ struct cand *q;
+
+ if (clen == clistlen) {
+ clistlen = clistlen * 11 / 10;
+ clist = xrealloc(clist, clistlen * sizeof(struct cand));
+ }
+ q = clist + clen;
+ q->x = x;
+ q->y = y;
+ q->pred = pred;
+ return clen++;
+}
+
+
+static int search(int *c, int k, int y)
+{
+ int i, j, l, t;
+
+ if (clist[c[k]].y < y) /* quick look for typical case */
+ return k + 1;
+ i = 0;
+ j = k + 1;
+ while (1) {
+ l = i + j;
+ if ((l >>= 1) <= i)
+ break;
+ t = clist[c[l]].y;
+ if (t > y)
+ j = l;
+ else if (t < y)
+ i = l;
+ else
+ return l;
+ }
+ return l + 1;
+}
+
+
+static int stone(int *a, int n, int *b, int *c)
+{
+ int i, k, y, j, l;
+ int oldc, tc, oldl;
+ unsigned int numtries;
+#if ENABLE_FEATURE_DIFF_MINIMAL
+ const unsigned int bound =
+ (option_mask32 & FLAG_d) ? UINT_MAX : MAX(256, isqrt(n));
+#else
+ const unsigned int bound = MAX(256, isqrt(n));
+#endif
+
+ k = 0;
+ c[0] = newcand(0, 0, 0);
+ for (i = 1; i <= n; i++) {
+ j = a[i];
+ if (j == 0)
+ continue;
+ y = -b[j];
+ oldl = 0;
+ oldc = c[0];
+ numtries = 0;
+ do {
+ if (y <= clist[oldc].y)
+ continue;
+ l = search(c, k, y);
+ if (l != oldl + 1)
+ oldc = c[l - 1];
+ if (l <= k) {
+ if (clist[c[l]].y <= y)
+ continue;
+ tc = c[l];
+ c[l] = newcand(i, y, oldc);
+ oldc = tc;
+ oldl = l;
+ numtries++;
+ } else {
+ c[l] = newcand(i, y, oldc);
+ k++;
+ break;
+ }
+ } while ((y = b[++j]) > 0 && numtries < bound);
+ }
+ return k;
+}
+
+
+static void unravel(int p)
+{
+ struct cand *q;
+ int i;
+
+ for (i = 0; i <= nlen[0]; i++)
+ J[i] = i <= pref ? i : i > nlen[0] - suff ? i + nlen[1] - nlen[0] : 0;
+ for (q = clist + p; q->y != 0; q = clist + q->pred)
+ J[q->x + pref] = q->y + pref;
+}
+
+
+static void unsort(struct line *f, int l, int *b)
+{
+ int *a, i;
+
+ a = xmalloc((l + 1) * sizeof(int));
+ for (i = 1; i <= l; i++)
+ a[f[i].serial] = f[i].value;
+ for (i = 1; i <= l; i++)
+ b[i] = a[i];
+ free(a);
+}
+
+
+static int skipline(FILE *f)
+{
+ int i, c;
+
+ for (i = 1; (c = getc(f)) != '\n' && c != EOF; i++)
+ continue;
+ return i;
+}
+
+
+/*
+ * Check does double duty:
+ * 1. ferret out any fortuitous correspondences due
+ * to confounding by hashing (which result in "jackpot")
+ * 2. collect random access indexes to the two files
+ */
+static NOINLINE void check(FILE *f1, FILE *f2)
+{
+ int i, j, jackpot, c, d;
+ long ctold, ctnew;
+
+ rewind(f1);
+ rewind(f2);
+ j = 1;
+ ixold[0] = ixnew[0] = 0;
+ jackpot = 0;
+ ctold = ctnew = 0;
+ for (i = 1; i <= nlen[0]; i++) {
+ if (J[i] == 0) {
+ ixold[i] = ctold += skipline(f1);
+ continue;
+ }
+ while (j < J[i]) {
+ ixnew[j] = ctnew += skipline(f2);
+ j++;
+ }
+ if (option_mask32 & (FLAG_b | FLAG_w | FLAG_i)) {
+ while (1) {
+ c = getc(f1);
+ d = getc(f2);
+ /*
+ * GNU diff ignores a missing newline
+ * in one file if bflag || wflag.
+ */
+ if ((option_mask32 & (FLAG_b | FLAG_w))
+ && ((c == EOF && d == '\n') || (c == '\n' && d == EOF))
+ ) {
+ break;
+ }
+ ctold++;
+ ctnew++;
+ if ((option_mask32 & FLAG_b) && isspace(c) && isspace(d)) {
+ do {
+ if (c == '\n')
+ break;
+ ctold++;
+ c = getc(f1);
+ } while (isspace(c));
+ do {
+ if (d == '\n')
+ break;
+ ctnew++;
+ d = getc(f2);
+ } while (isspace(d));
+ } else if (option_mask32 & FLAG_w) {
+ while (isspace(c) && c != '\n') {
+ c = getc(f1);
+ ctold++;
+ }
+ while (isspace(d) && d != '\n') {
+ d = getc(f2);
+ ctnew++;
+ }
+ }
+ if (c != d) {
+ jackpot++;
+ J[i] = 0;
+ if (c != '\n' && c != EOF)
+ ctold += skipline(f1);
+ if (d != '\n' && c != EOF)
+ ctnew += skipline(f2);
+ break;
+ }
+ if (c == '\n' || c == EOF)
+ break;
+ }
+ } else {
+ while (1) {
+ ctold++;
+ ctnew++;
+ c = getc(f1);
+ d = getc(f2);
+ if (c != d) {
+ J[i] = 0;
+ if (c != '\n' && c != EOF)
+ ctold += skipline(f1);
+/* was buggy? "if (d != '\n' && c != EOF)" */
+ if (d != '\n' && d != EOF)
+ ctnew += skipline(f2);
+ break;
+ }
+ if (c == '\n' || c == EOF)
+ break;
+ }
+ }
+ ixold[i] = ctold;
+ ixnew[j] = ctnew;
+ j++;
+ }
+ for (; j <= nlen[1]; j++)
+ ixnew[j] = ctnew += skipline(f2);
+}
+
+
+/* shellsort CACM #201 */
+static void sort(struct line *a, int n)
+{
+ struct line *ai, *aim, w;
+ int j, m = 0, k;
+
+ if (n == 0)
+ return;
+ for (j = 1; j <= n; j *= 2)
+ m = 2 * j - 1;
+ for (m /= 2; m != 0; m /= 2) {
+ k = n - m;
+ for (j = 1; j <= k; j++) {
+ for (ai = &a[j]; ai > a; ai -= m) {
+ aim = &ai[m];
+ if (aim < ai)
+ break; /* wraparound */
+ if (aim->value > ai[0].value
+ || (aim->value == ai[0].value && aim->serial > ai[0].serial)
+ ) {
+ break;
+ }
+ w.value = ai[0].value;
+ ai[0].value = aim->value;
+ aim->value = w.value;
+ w.serial = ai[0].serial;
+ ai[0].serial = aim->serial;
+ aim->serial = w.serial;
+ }
+ }
+ }
+}
+
+
+static void uni_range(int a, int b)
+{
+ if (a < b)
+ printf("%d,%d", a, b - a + 1);
+ else if (a == b)
+ printf("%d", b);
+ else
+ printf("%d,0", b);
+}
+
+
+static void fetch(long *f, int a, int b, FILE *lb, int ch)
+{
+ int i, j, c, lastc, col, nc;
+
+ if (a > b)
+ return;
+ for (i = a; i <= b; i++) {
+ fseek(lb, f[i - 1], SEEK_SET);
+ nc = f[i] - f[i - 1];
+ if (ch != '\0') {
+ putchar(ch);
+ if (option_mask32 & FLAG_T)
+ putchar('\t');
+ }
+ col = 0;
+ for (j = 0, lastc = '\0'; j < nc; j++, lastc = c) {
+ c = getc(lb);
+ if (c == EOF) {
+ printf("\n\\ No newline at end of file\n");
+ return;
+ }
+ if (c == '\t' && (option_mask32 & FLAG_t)) {
+ do {
+ putchar(' ');
+ } while (++col & 7);
+ } else {
+ putchar(c);
+ col++;
+ }
+ }
+ }
+}
+
+
+#if ENABLE_FEATURE_DIFF_BINARY
+static int asciifile(FILE *f)
+{
+ int i, cnt;
+
+ if (option_mask32 & FLAG_a)
+ return 1;
+ rewind(f);
+ cnt = fread(g_read_buf, 1, COMMON_BUFSIZE, f);
+ for (i = 0; i < cnt; i++) {
+ if (!isprint(g_read_buf[i])
+ && !isspace(g_read_buf[i])
+ ) {
+ return 0;
+ }
+ }
+ return 1;
+}
+#else
+#define asciifile(f) 1
+#endif
+
+
+/* dump accumulated "unified" diff changes */
+static void dump_unified_vec(FILE *f1, FILE *f2)
+{
+ struct context_vec *cvp = context_vec_start;
+ int lowa, upb, lowc, upd;
+ int a, b, c, d;
+ char ch;
+
+ if (context_vec_start > context_vec_ptr)
+ return;
+
+ b = d = 0; /* gcc */
+ lowa = MAX(1, cvp->a - opt_U_context);
+ upb = MIN(nlen[0], context_vec_ptr->b + opt_U_context);
+ lowc = MAX(1, cvp->c - opt_U_context);
+ upd = MIN(nlen[1], context_vec_ptr->d + opt_U_context);
+
+ printf("@@ -");
+ uni_range(lowa, upb);
+ printf(" +");
+ uni_range(lowc, upd);
+ printf(" @@\n");
+
+ /*
+ * Output changes in "unified" diff format--the old and new lines
+ * are printed together.
+ */
+ for (; cvp <= context_vec_ptr; cvp++) {
+ a = cvp->a;
+ b = cvp->b;
+ c = cvp->c;
+ d = cvp->d;
+
+ /*
+ * c: both new and old changes
+ * d: only changes in the old file
+ * a: only changes in the new file
+ */
+ if (a <= b && c <= d)
+ ch = 'c';
+ else
+ ch = (a <= b) ? 'd' : 'a';
+#if 0
+ switch (ch) {
+ case 'c':
+// fetch() seeks!
+ fetch(ixold, lowa, a - 1, f1, ' ');
+ fetch(ixold, a, b, f1, '-');
+ fetch(ixnew, c, d, f2, '+');
+ break;
+ case 'd':
+ fetch(ixold, lowa, a - 1, f1, ' ');
+ fetch(ixold, a, b, f1, '-');
+ break;
+ case 'a':
+ fetch(ixnew, lowc, c - 1, f2, ' ');
+ fetch(ixnew, c, d, f2, '+');
+ break;
+ }
+#else
+ if (ch == 'c' || ch == 'd') {
+ fetch(ixold, lowa, a - 1, f1, ' ');
+ fetch(ixold, a, b, f1, '-');
+ }
+ if (ch == 'a')
+ fetch(ixnew, lowc, c - 1, f2, ' ');
+ if (ch == 'c' || ch == 'a')
+ fetch(ixnew, c, d, f2, '+');
+#endif
+ lowa = b + 1;
+ lowc = d + 1;
+ }
+ fetch(ixnew, d + 1, upd, f2, ' ');
+
+ context_vec_ptr = context_vec_start - 1;
+}
+
+
+static void print_header(const char *file1, const char *file2)
+{
+ if (label1)
+ printf("--- %s\n", label1);
+ else
+ printf("--- %s\t%s", file1, ctime(&stb1.st_mtime));
+ if (label2)
+ printf("+++ %s\n", label2);
+ else
+ printf("+++ %s\t%s", file2, ctime(&stb2.st_mtime));
+}
+
+
+/*
+ * Indicate that there is a difference between lines a and b of the from file
+ * to get to lines c to d of the to file. If a is greater than b then there
+ * are no lines in the from file involved and this means that there were
+ * lines appended (beginning at b). If c is greater than d then there are
+ * lines missing from the to file.
+ */
+static void change(const char *file1, FILE *f1, const char *file2, FILE *f2,
+ int a, int b, int c, int d)
+{
+ if ((a > b && c > d) || (option_mask32 & FLAG_q)) {
+ anychange = 1;
+ return;
+ }
+
+ /*
+ * Allocate change records as needed.
+ */
+ if (context_vec_ptr == context_vec_end - 1) {
+ ptrdiff_t offset = context_vec_ptr - context_vec_start;
+
+ max_context <<= 1;
+ context_vec_start = xrealloc(context_vec_start,
+ max_context * sizeof(struct context_vec));
+ context_vec_end = context_vec_start + max_context;
+ context_vec_ptr = context_vec_start + offset;
+ }
+ if (anychange == 0) {
+ /*
+ * Print the context/unidiff header first time through.
+ */
+ print_header(file1, file2);
+ } else if (a > context_vec_ptr->b + (2 * opt_U_context) + 1
+ && c > context_vec_ptr->d + (2 * opt_U_context) + 1
+ ) {
+ /*
+ * If this change is more than 'context' lines from the
+ * previous change, dump the record and reset it.
+ */
+// dump_unified_vec() seeks!
+ dump_unified_vec(f1, f2);
+ }
+ context_vec_ptr++;
+ context_vec_ptr->a = a;
+ context_vec_ptr->b = b;
+ context_vec_ptr->c = c;
+ context_vec_ptr->d = d;
+ anychange = 1;
+}
+
+
+static void output(const char *file1, FILE *f1, const char *file2, FILE *f2)
+{
+ /* Note that j0 and j1 can't be used as they are defined in math.h.
+ * This also allows the rather amusing variable 'j00'... */
+ int m, i0, i1, j00, j01;
+
+ rewind(f1);
+ rewind(f2);
+ m = nlen[0];
+ J[0] = 0;
+ J[m + 1] = nlen[1] + 1;
+ for (i0 = 1; i0 <= m; i0 = i1 + 1) {
+ while (i0 <= m && J[i0] == J[i0 - 1] + 1)
+ i0++;
+ j00 = J[i0 - 1] + 1;
+ i1 = i0 - 1;
+ while (i1 < m && J[i1 + 1] == 0)
+ i1++;
+ j01 = J[i1 + 1] - 1;
+ J[i1] = j01;
+// change() seeks!
+ change(file1, f1, file2, f2, i0, i1, j00, j01);
+ }
+ if (m == 0) {
+// change() seeks!
+ change(file1, f1, file2, f2, 1, 0, 1, nlen[1]);
+ }
+ if (anychange != 0 && !(option_mask32 & FLAG_q)) {
+// dump_unified_vec() seeks!
+ dump_unified_vec(f1, f2);
+ }
+}
+
+/*
+ * The following code uses an algorithm due to Harold Stone,
+ * which finds a pair of longest identical subsequences in
+ * the two files.
+ *
+ * The major goal is to generate the match vector J.
+ * J[i] is the index of the line in file1 corresponding
+ * to line i in file0. J[i] = 0 if there is no
+ * such line in file1.
+ *
+ * Lines are hashed so as to work in core. All potential
+ * matches are located by sorting the lines of each file
+ * on the hash (called "value"). In particular, this
+ * collects the equivalence classes in file1 together.
+ * Subroutine equiv replaces the value of each line in
+ * file0 by the index of the first element of its
+ * matching equivalence in (the reordered) file1.
+ * To save space equiv squeezes file1 into a single
+ * array member in which the equivalence classes
+ * are simply concatenated, except that their first
+ * members are flagged by changing sign.
+ *
+ * Next the indices that point into member are unsorted into
+ * array class according to the original order of file0.
+ *
+ * The cleverness lies in routine stone. This marches
+ * through the lines of file0, developing a vector klist
+ * of "k-candidates". At step i a k-candidate is a matched
+ * pair of lines x,y (x in file0, y in file1) such that
+ * there is a common subsequence of length k
+ * between the first i lines of file0 and the first y
+ * lines of file1, but there is no such subsequence for
+ * any smaller y. x is the earliest possible mate to y
+ * that occurs in such a subsequence.
+ *
+ * Whenever any of the members of the equivalence class of
+ * lines in file1 matable to a line in file0 has serial number
+ * less than the y of some k-candidate, that k-candidate
+ * with the smallest such y is replaced. The new
+ * k-candidate is chained (via pred) to the current
+ * k-1 candidate so that the actual subsequence can
+ * be recovered. When a member has serial number greater
+ * that the y of all k-candidates, the klist is extended.
+ * At the end, the longest subsequence is pulled out
+ * and placed in the array J by unravel
+ *
+ * With J in hand, the matches there recorded are
+ * checked against reality to assure that no spurious
+ * matches have crept in due to hashing. If they have,
+ * they are broken, and "jackpot" is recorded--a harmless
+ * matter except that a true match for a spuriously
+ * mated line may now be unnecessarily reported as a change.
+ *
+ * Much of the complexity of the program comes simply
+ * from trying to minimize core utilization and
+ * maximize the range of doable problems by dynamically
+ * allocating what is needed and reusing what is not.
+ * The core requirements for problems larger than somewhat
+ * are (in words) 2*length(file0) + length(file1) +
+ * 3*(number of k-candidates installed), typically about
+ * 6n words for files of length n.
+ */
+/* NB: files can be not REGular. The only sure thing that they
+ * are not both DIRectories. */
+static unsigned diffreg(const char *file1, const char *file2, int flags)
+{
+ int *member; /* will be overlaid on nfile[1] */
+ int *class; /* will be overlaid on nfile[0] */
+ int *klist; /* will be overlaid on nfile[0] after class */
+ FILE *f1;
+ FILE *f2;
+ unsigned rval;
+ int i;
+
+ anychange = 0;
+ context_vec_ptr = context_vec_start - 1;
+ tempname1 = tempname2 = NULL;
+
+ /* Is any of them a directory? Then it's simple */
+ if (S_ISDIR(stb1.st_mode) != S_ISDIR(stb2.st_mode))
+ return (S_ISDIR(stb1.st_mode) ? D_ISDIR1 : D_ISDIR2);
+
+ /* None of them are directories */
+ rval = D_SAME;
+
+ if (flags & D_EMPTY1)
+ /* can't be stdin, but xfopen_stdin() is smaller code */
+ file1 = bb_dev_null;
+ f1 = xfopen_stdin(file1);
+ if (flags & D_EMPTY2)
+ file2 = bb_dev_null;
+ f2 = xfopen_stdin(file2);
+
+ /* NB: if D_EMPTY1/2 is set, other file is always a regular file,
+ * not pipe/fifo/chardev/etc - D_EMPTY is used by "diff -r" only,
+ * and it never diffs non-ordinary files in subdirs. */
+ if (!(flags & (D_EMPTY1 | D_EMPTY2))) {
+ /* Quick check whether they are different */
+ /* NB: copies non-REG files to tempfiles and fills tempname1/2 */
+ i = files_differ(f1, f2);
+ if (i != 1) { /* not different? */
+ if (i != 0) /* error? */
+ exit_status |= 2;
+ goto closem;
+ }
+ }
+
+ if (!asciifile(f1) || !asciifile(f2)) {
+ rval = D_BINARY;
+ exit_status |= 1;
+ goto closem;
+ }
+
+// Rewind inside!
+ prepare(0, f1 /*, stb1.st_size*/);
+ prepare(1, f2 /*, stb2.st_size*/);
+ prune();
+ sort(sfile[0], slen[0]);
+ sort(sfile[1], slen[1]);
+
+ member = (int *) nfile[1];
+ equiv(sfile[0], slen[0], sfile[1], slen[1], member);
+//TODO: xrealloc_vector?
+ member = xrealloc(member, (slen[1] + 2) * sizeof(int));
+
+ class = (int *) nfile[0];
+ unsort(sfile[0], slen[0], class);
+ class = xrealloc(class, (slen[0] + 2) * sizeof(int));
+
+ klist = xmalloc((slen[0] + 2) * sizeof(int));
+ clen = 0;
+ clistlen = 100;
+ clist = xmalloc(clistlen * sizeof(struct cand));
+ i = stone(class, slen[0], member, klist);
+ free(member);
+ free(class);
+
+ J = xrealloc(J, (nlen[0] + 2) * sizeof(int));
+ unravel(klist[i]);
+ free(clist);
+ free(klist);
+
+ ixold = xrealloc(ixold, (nlen[0] + 2) * sizeof(long));
+ ixnew = xrealloc(ixnew, (nlen[1] + 2) * sizeof(long));
+// Rewind inside!
+ check(f1, f2);
+// Rewind inside!
+ output(file1, f1, file2, f2);
+
+ closem:
+ if (anychange) {
+ exit_status |= 1;
+ if (rval == D_SAME)
+ rval = D_DIFFER;
+ }
+ fclose_if_not_stdin(f1);
+ fclose_if_not_stdin(f2);
+ if (tempname1) {
+ unlink(tempname1);
+ free(tempname1);
+ }
+ if (tempname2) {
+ unlink(tempname2);
+ free(tempname2);
+ }
+ return rval;
+}
+
+
+#if ENABLE_FEATURE_DIFF_DIR
+static void do_diff(char *dir1, char *path1, char *dir2, char *path2)
+{
+ int flags = 0; /*D_HEADER;*/
+ int val;
+ char *fullpath1 = NULL; /* if -N */
+ char *fullpath2 = NULL;
+
+ if (path1)
+ fullpath1 = concat_path_file(dir1, path1);
+ if (path2)
+ fullpath2 = concat_path_file(dir2, path2);
+
+ if (!fullpath1 || stat(fullpath1, &stb1) != 0) {
+ flags |= D_EMPTY1;
+ memset(&stb1, 0, sizeof(stb1));
+ if (path2) {
+ free(fullpath1);
+ fullpath1 = concat_path_file(dir1, path2);
+ }
+ }
+ if (!fullpath2 || stat(fullpath2, &stb2) != 0) {
+ flags |= D_EMPTY2;
+ memset(&stb2, 0, sizeof(stb2));
+ stb2.st_mode = stb1.st_mode;
+ if (path1) {
+ free(fullpath2);
+ fullpath2 = concat_path_file(dir2, path1);
+ }
+ }
+
+ if (stb1.st_mode == 0)
+ stb1.st_mode = stb2.st_mode;
+
+ if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
+ printf("Common subdirectories: %s and %s\n", fullpath1, fullpath2);
+ goto ret;
+ }
+
+ if (!S_ISREG(stb1.st_mode) && !S_ISDIR(stb1.st_mode))
+ val = D_SKIPPED1;
+ else if (!S_ISREG(stb2.st_mode) && !S_ISDIR(stb2.st_mode))
+ val = D_SKIPPED2;
+ else {
+ /* Both files are either REGular or DIRectories */
+ val = diffreg(fullpath1, fullpath2, flags);
+ }
+
+ print_status(val, fullpath1, fullpath2 /*, NULL*/);
+ ret:
+ free(fullpath1);
+ free(fullpath2);
+}
+#endif
+
+
+#if ENABLE_FEATURE_DIFF_DIR
+/* This function adds a filename to dl, the directory listing. */
+static int FAST_FUNC add_to_dirlist(const char *filename,
+ struct stat *sb UNUSED_PARAM,
+ void *userdata,
+ int depth UNUSED_PARAM)
+{
+ dl = xrealloc_vector(dl, 5, dl_count);
+ dl[dl_count] = xstrdup(filename + (int)(ptrdiff_t)userdata);
+ dl_count++;
+ return TRUE;
+}
+
+
+/* This returns a sorted directory listing. */
+static char **get_recursive_dirlist(char *path)
+{
+ dl_count = 0;
+ dl = xzalloc(sizeof(dl[0]));
+
+ /* We need to trim root directory prefix.
+ * Using void *userdata to specify its length,
+ * add_to_dirlist will remove it. */
+ if (option_mask32 & FLAG_r) {
+ recursive_action(path, ACTION_RECURSE|ACTION_FOLLOWLINKS,
+ add_to_dirlist, /* file_action */
+ NULL, /* dir_action */
+ (void*)(ptrdiff_t)(strlen(path) + 1),
+ 0);
+ } else {
+ DIR *dp;
+ struct dirent *ep;
+
+ dp = warn_opendir(path);
+ while ((ep = readdir(dp))) {
+ if (!strcmp(ep->d_name, "..") || LONE_CHAR(ep->d_name, '.'))
+ continue;
+ add_to_dirlist(ep->d_name, NULL, (void*)(int)0, 0);
+ }
+ closedir(dp);
+ }
+
+ /* Sort dl alphabetically. */
+ qsort_string_vector(dl, dl_count);
+
+ dl[dl_count] = NULL;
+ return dl;
+}
+
+
+static void diffdir(char *p1, char *p2)
+{
+ char **dirlist1, **dirlist2;
+ char *dp1, *dp2;
+ int pos;
+
+ /* Check for trailing slashes. */
+ dp1 = last_char_is(p1, '/');
+ if (dp1 != NULL)
+ *dp1 = '\0';
+ dp2 = last_char_is(p2, '/');
+ if (dp2 != NULL)
+ *dp2 = '\0';
+
+ /* Get directory listings for p1 and p2. */
+ dirlist1 = get_recursive_dirlist(p1);
+ dirlist2 = get_recursive_dirlist(p2);
+
+ /* If -S was set, find the starting point. */
+ if (opt_S_start) {
+ while (*dirlist1 != NULL && strcmp(*dirlist1, opt_S_start) < 0)
+ dirlist1++;
+ while (*dirlist2 != NULL && strcmp(*dirlist2, opt_S_start) < 0)
+ dirlist2++;
+ if ((*dirlist1 == NULL) || (*dirlist2 == NULL))
+ bb_error_msg(bb_msg_invalid_arg, "NULL", "-S");
+ }
+
+ /* Now that both dirlist1 and dirlist2 contain sorted directory
+ * listings, we can start to go through dirlist1. If both listings
+ * contain the same file, then do a normal diff. Otherwise, behaviour
+ * is determined by whether the -N flag is set. */
+ while (*dirlist1 != NULL || *dirlist2 != NULL) {
+ dp1 = *dirlist1;
+ dp2 = *dirlist2;
+ pos = dp1 == NULL ? 1 : (dp2 == NULL ? -1 : strcmp(dp1, dp2));
+ if (pos == 0) {
+ do_diff(p1, dp1, p2, dp2);
+ dirlist1++;
+ dirlist2++;
+ } else if (pos < 0) {
+ if (option_mask32 & FLAG_N)
+ do_diff(p1, dp1, p2, NULL);
+ else
+ print_only(p1, dp1);
+ dirlist1++;
+ } else {
+ if (option_mask32 & FLAG_N)
+ do_diff(p1, NULL, p2, dp2);
+ else
+ print_only(p2, dp2);
+ dirlist2++;
+ }
+ }
+}
+#endif
+
+
+int diff_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int diff_main(int argc UNUSED_PARAM, char **argv)
+{
+ int gotstdin = 0;
+ char *f1, *f2;
+ llist_t *L_arg = NULL;
+
+ INIT_G();
+
+ /* exactly 2 params; collect multiple -L <label>; -U N */
+ opt_complementary = "=2:L::U+";
+ getopt32(argv, "abdiL:NqrsS:tTU:wu"
+ "p" /* ignored (for compatibility) */,
+ &L_arg, &opt_S_start, &opt_U_context);
+ /*argc -= optind;*/
+ argv += optind;
+ while (L_arg) {
+ if (label1 && label2)
+ bb_show_usage();
+ if (label1) /* then label2 is NULL */
+ label2 = label1;
+ label1 = llist_pop(&L_arg);
+ }
+
+ /*
+ * Do sanity checks, fill in stb1 and stb2 and call the appropriate
+ * driver routine. Both drivers use the contents of stb1 and stb2.
+ */
+ f1 = argv[0];
+ f2 = argv[1];
+ if (LONE_DASH(f1)) {
+ fstat(STDIN_FILENO, &stb1);
+ gotstdin++;
+ } else
+ xstat(f1, &stb1);
+ if (LONE_DASH(f2)) {
+ fstat(STDIN_FILENO, &stb2);
+ gotstdin++;
+ } else
+ xstat(f2, &stb2);
+
+ if (gotstdin && (S_ISDIR(stb1.st_mode) || S_ISDIR(stb2.st_mode)))
+ bb_error_msg_and_die("can't compare stdin to a directory");
+
+ if (S_ISDIR(stb1.st_mode) && S_ISDIR(stb2.st_mode)) {
+#if ENABLE_FEATURE_DIFF_DIR
+ diffdir(f1, f2);
+ return exit_status;
+#else
+ bb_error_msg_and_die("no support for directory comparison");
+#endif
+ }
+
+ if (S_ISDIR(stb1.st_mode)) { /* "diff dir file" */
+ /* NB: "diff dir dir2/dir3/file" must become
+ * "diff dir/file dir2/dir3/file" */
+ char *slash = strrchr(f2, '/');
+ f1 = concat_path_file(f1, slash ? slash + 1 : f2);
+ xstat(f1, &stb1);
+ }
+ if (S_ISDIR(stb2.st_mode)) {
+ char *slash = strrchr(f1, '/');
+ f2 = concat_path_file(f2, slash ? slash + 1 : f1);
+ xstat(f2, &stb2);
+ }
+
+ /* diffreg can get non-regular files here,
+ * they are not both DIRestories */
+ print_status((gotstdin > 1 ? D_SAME : diffreg(f1, f2, 0)),
+ f1, f2 /*, NULL*/);
+ return exit_status;
+}
diff --git a/release/src/router/busybox/editors/ed.c b/release/src/router/busybox/editors/ed.c
new file mode 100644
index 00000000..9084a17f
--- /dev/null
+++ b/release/src/router/busybox/editors/ed.c
@@ -0,0 +1,1049 @@
+/* vi: set sw=4 ts=4: */
+/*
+ * Copyright (c) 2002 by David I. Bell
+ * Permission is granted to use, distribute, or modify this source,
+ * provided that this copyright notice remains intact.
+ *
+ * The "ed" built-in command (much simplified)
+ */
+
+#include "libbb.h"
+
+typedef struct LINE {
+ struct LINE *next;
+ struct LINE *prev;
+ int len;
+ char data[1];
+} LINE;
+
+
+#define searchString bb_common_bufsiz1
+
+enum {
+ USERSIZE = sizeof(searchString) > 1024 ? 1024
+ : sizeof(searchString) - 1, /* max line length typed in by user */
+ INITBUF_SIZE = 1024, /* initial buffer size */
+};
+
+struct globals {
+ int curNum;
+ int lastNum;
+ int bufUsed;
+ int bufSize;
+ LINE *curLine;
+ char *bufBase;
+ char *bufPtr;
+ char *fileName;
+ LINE lines;
+ smallint dirty;
+ int marks[26];
+};
+#define G (*ptr_to_globals)
+#define curLine (G.curLine )
+#define bufBase (G.bufBase )
+#define bufPtr (G.bufPtr )
+#define fileName (G.fileName )
+#define curNum (G.curNum )
+#define lastNum (G.lastNum )
+#define bufUsed (G.bufUsed )
+#define bufSize (G.bufSize )
+#define dirty (G.dirty )
+#define lines (G.lines )
+#define marks (G.marks )
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+} while (0)
+
+
+static void doCommands(void);
+static void subCommand(const char *cmd, int num1, int num2);
+static int getNum(const char **retcp, smallint *retHaveNum, int *retNum);
+static int setCurNum(int num);
+static void addLines(int num);
+static int insertLine(int num, const char *data, int len);
+static void deleteLines(int num1, int num2);
+static int printLines(int num1, int num2, int expandFlag);
+static int writeLines(const char *file, int num1, int num2);
+static int readLines(const char *file, int num);
+static int searchLines(const char *str, int num1, int num2);
+static LINE *findLine(int num);
+static int findString(const LINE *lp, const char * str, int len, int offset);
+
+
+static int bad_nums(int num1, int num2, const char *for_what)
+{
+ if ((num1 < 1) || (num2 > lastNum) || (num1 > num2)) {
+ bb_error_msg("bad line range for %s", for_what);
+ return 1;
+ }
+ return 0;
+}
+
+
+static char *skip_blank(const char *cp)
+{
+ while (isblank(*cp))
+ cp++;
+ return (char *)cp;
+}
+
+
+int ed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int ed_main(int argc UNUSED_PARAM, char **argv)
+{
+ INIT_G();
+
+ bufSize = INITBUF_SIZE;
+ bufBase = xmalloc(bufSize);
+ bufPtr = bufBase;
+ lines.next = &lines;
+ lines.prev = &lines;
+
+ if (argv[1]) {
+ fileName = xstrdup(argv[1]);
+ if (!readLines(fileName, 1)) {
+ return EXIT_SUCCESS;
+ }
+ if (lastNum)
+ setCurNum(1);
+ dirty = FALSE;
+ }
+
+ doCommands();
+ return EXIT_SUCCESS;
+}
+
+/*
+ * Read commands until we are told to stop.
+ */
+static void doCommands(void)
+{
+ const char *cp;
+ char *endbuf, buf[USERSIZE];
+ int len, num1, num2;
+ smallint have1, have2;
+
+ while (TRUE) {
+ /* Returns:
+ * -1 on read errors or EOF, or on bare Ctrl-D.
+ * 0 on ctrl-C,
+ * >0 length of input string, including terminating '\n'
+ */
+ len = read_line_input(": ", buf, sizeof(buf), NULL);
+ if (len <= 0)
+ return;
+ endbuf = &buf[len - 1];
+ while ((endbuf > buf) && isblank(endbuf[-1]))
+ endbuf--;
+ *endbuf = '\0';
+
+ cp = skip_blank(buf);
+ have1 = FALSE;
+ have2 = FALSE;
+
+ if ((curNum == 0) && (lastNum > 0)) {
+ curNum = 1;
+ curLine = lines.next;
+ }
+
+ if (!getNum(&cp, &have1, &num1))
+ continue;
+
+ cp = skip_blank(cp);
+
+ if (*cp == ',') {
+ cp++;
+ if (!getNum(&cp, &have2, &num2))
+ continue;
+ if (!have1)
+ num1 = 1;
+ if (!have2)
+ num2 = lastNum;
+ have1 = TRUE;
+ have2 = TRUE;
+ }
+ if (!have1)
+ num1 = curNum;
+ if (!have2)
+ num2 = num1;
+
+ switch (*cp++) {
+ case 'a':
+ addLines(num1 + 1);
+ break;
+
+ case 'c':
+ deleteLines(num1, num2);
+ addLines(num1);
+ break;
+
+ case 'd':
+ deleteLines(num1, num2);
+ break;
+
+ case 'f':
+ if (*cp && !isblank(*cp)) {
+ bb_error_msg("bad file command");
+ break;
+ }
+ cp = skip_blank(cp);
+ if (*cp == '\0') {
+ if (fileName)
+ printf("\"%s\"\n", fileName);
+ else
+ printf("No file name\n");
+ break;
+ }
+ free(fileName);
+ fileName = xstrdup(cp);
+ break;
+
+ case 'i':
+ addLines(num1);
+ break;
+
+ case 'k':
+ cp = skip_blank(cp);
+ if ((*cp < 'a') || (*cp > 'z') || cp[1]) {
+ bb_error_msg("bad mark name");
+ break;
+ }
+ marks[*cp - 'a'] = num2;
+ break;
+
+ case 'l':
+ printLines(num1, num2, TRUE);
+ break;
+
+ case 'p':
+ printLines(num1, num2, FALSE);
+ break;
+
+ case 'q':
+ cp = skip_blank(cp);
+ if (have1 || *cp) {
+ bb_error_msg("bad quit command");
+ break;
+ }
+ if (!dirty)
+ return;
+ len = read_line_input("Really quit? ", buf, 16, NULL);
+ /* read error/EOF - no way to continue */
+ if (len < 0)
+ return;
+ cp = skip_blank(buf);
+ if ((*cp | 0x20) == 'y') /* Y or y */
+ return;
+ break;
+
+ case 'r':
+ if (*cp && !isblank(*cp)) {
+ bb_error_msg("bad read command");
+ break;
+ }
+ cp = skip_blank(cp);
+ if (*cp == '\0') {
+ bb_error_msg("no file name");
+ break;
+ }
+ if (!have1)
+ num1 = lastNum;
+ if (readLines(cp, num1 + 1))
+ break;
+ if (fileName == NULL)
+ fileName = xstrdup(cp);
+ break;
+
+ case 's':
+ subCommand(cp, num1, num2);
+ break;
+
+ case 'w':
+ if (*cp && !isblank(*cp)) {
+ bb_error_msg("bad write command");
+ break;
+ }
+ cp = skip_blank(cp);
+ if (!have1) {
+ num1 = 1;
+ num2 = lastNum;
+ }
+ if (*cp == '\0')
+ cp = fileName;
+ if (cp == NULL) {
+ bb_error_msg("no file name specified");
+ break;
+ }
+ writeLines(cp, num1, num2);
+ break;
+
+ case 'z':
+ switch (*cp) {
+ case '-':
+ printLines(curNum - 21, curNum, FALSE);
+ break;
+ case '.':
+ printLines(curNum - 11, curNum + 10, FALSE);
+ break;
+ default:
+ printLines(curNum, curNum + 21, FALSE);
+ break;
+ }
+ break;
+
+ case '.':
+ if (have1) {
+ bb_error_msg("no arguments allowed");
+ break;
+ }
+ printLines(curNum, curNum, FALSE);
+ break;
+
+ case '-':
+ if (setCurNum(curNum - 1))
+ printLines(curNum, curNum, FALSE);
+ break;
+
+ case '=':
+ printf("%d\n", num1);
+ break;
+ case '\0':
+ if (have1) {
+ printLines(num2, num2, FALSE);
+ break;
+ }
+ if (setCurNum(curNum + 1))
+ printLines(curNum, curNum, FALSE);
+ break;
+
+ default:
+ bb_error_msg("unimplemented command");
+ break;
+ }
+ }
+}
+
+
+/*
+ * Do the substitute command.
+ * The current line is set to the last substitution done.
+ */
+static void subCommand(const char *cmd, int num1, int num2)
+{
+ char *cp, *oldStr, *newStr, buf[USERSIZE];
+ int delim, oldLen, newLen, deltaLen, offset;
+ LINE *lp, *nlp;
+ int globalFlag, printFlag, didSub, needPrint;
+
+ if (bad_nums(num1, num2, "substitute"))
+ return;
+
+ globalFlag = FALSE;
+ printFlag = FALSE;
+ didSub = FALSE;
+ needPrint = FALSE;
+
+ /*
+ * Copy the command so we can modify it.
+ */
+ strcpy(buf, cmd);
+ cp = buf;
+
+ if (isblank(*cp) || (*cp == '\0')) {
+ bb_error_msg("bad delimiter for substitute");
+ return;
+ }
+
+ delim = *cp++;
+ oldStr = cp;
+
+ cp = strchr(cp, delim);
+ if (cp == NULL) {
+ bb_error_msg("missing 2nd delimiter for substitute");
+ return;
+ }
+
+ *cp++ = '\0';
+
+ newStr = cp;
+ cp = strchr(cp, delim);
+
+ if (cp)
+ *cp++ = '\0';
+ else
+ cp = (char*)"";
+
+ while (*cp) switch (*cp++) {
+ case 'g':
+ globalFlag = TRUE;
+ break;
+ case 'p':
+ printFlag = TRUE;
+ break;
+ default:
+ bb_error_msg("unknown option for substitute");
+ return;
+ }
+
+ if (*oldStr == '\0') {
+ if (searchString[0] == '\0') {
+ bb_error_msg("no previous search string");
+ return;
+ }
+ oldStr = searchString;
+ }
+
+ if (oldStr != searchString)
+ strcpy(searchString, oldStr);
+
+ lp = findLine(num1);
+ if (lp == NULL)
+ return;
+
+ oldLen = strlen(oldStr);
+ newLen = strlen(newStr);
+ deltaLen = newLen - oldLen;
+ offset = 0;
+ nlp = NULL;
+
+ while (num1 <= num2) {
+ offset = findString(lp, oldStr, oldLen, offset);
+
+ if (offset < 0) {
+ if (needPrint) {
+ printLines(num1, num1, FALSE);
+ needPrint = FALSE;
+ }
+ offset = 0;
+ lp = lp->next;
+ num1++;
+ continue;
+ }
+
+ needPrint = printFlag;
+ didSub = TRUE;
+ dirty = TRUE;
+
+ /*
+ * If the replacement string is the same size or shorter
+ * than the old string, then the substitution is easy.
+ */
+ if (deltaLen <= 0) {
+ memcpy(&lp->data[offset], newStr, newLen);
+ if (deltaLen) {
+ memcpy(&lp->data[offset + newLen],
+ &lp->data[offset + oldLen],
+ lp->len - offset - oldLen);
+
+ lp->len += deltaLen;
+ }
+ offset += newLen;
+ if (globalFlag)
+ continue;
+ if (needPrint) {
+ printLines(num1, num1, FALSE);
+ needPrint = FALSE;
+ }
+ lp = lp->next;
+ num1++;
+ continue;
+ }
+
+ /*
+ * The new string is larger, so allocate a new line
+ * structure and use that. Link it in in place of
+ * the old line structure.
+ */
+ nlp = malloc(sizeof(LINE) + lp->len + deltaLen);
+ if (nlp == NULL) {
+ bb_error_msg("cannot get memory for line");
+ return;
+ }
+
+ nlp->len = lp->len + deltaLen;
+
+ memcpy(nlp->data, lp->data, offset);
+ memcpy(&nlp->data[offset], newStr, newLen);
+ memcpy(&nlp->data[offset + newLen],
+ &lp->data[offset + oldLen],
+ lp->len - offset - oldLen);
+
+ nlp->next = lp->next;
+ nlp->prev = lp->prev;
+ nlp->prev->next = nlp;
+ nlp->next->prev = nlp;
+
+ if (curLine == lp)
+ curLine = nlp;
+
+ free(lp);
+ lp = nlp;
+
+ offset += newLen;
+
+ if (globalFlag)
+ continue;
+
+ if (needPrint) {
+ printLines(num1, num1, FALSE);
+ needPrint = FALSE;
+ }
+
+ lp = lp->next;
+ num1++;
+ }
+
+ if (!didSub)
+ bb_error_msg("no substitutions found for \"%s\"", oldStr);
+}
+
+
+/*
+ * Search a line for the specified string starting at the specified
+ * offset in the line. Returns the offset of the found string, or -1.
+ */
+static int findString(const LINE *lp, const char *str, int len, int offset)
+{
+ int left;
+ const char *cp, *ncp;
+
+ cp = &lp->data[offset];
+ left = lp->len - offset;
+
+ while (left >= len) {
+ ncp = memchr(cp, *str, left);
+ if (ncp == NULL)
+ return -1;
+ left -= (ncp - cp);
+ if (left < len)
+ return -1;
+ cp = ncp;
+ if (memcmp(cp, str, len) == 0)
+ return (cp - lp->data);
+ cp++;
+ left--;
+ }
+
+ return -1;
+}
+
+
+/*
+ * Add lines which are typed in by the user.
+ * The lines are inserted just before the specified line number.
+ * The lines are terminated by a line containing a single dot (ugly!),
+ * or by an end of file.
+ */
+static void addLines(int num)
+{
+ int len;
+ char buf[USERSIZE + 1];
+
+ while (1) {
+ /* Returns:
+ * -1 on read errors or EOF, or on bare Ctrl-D.
+ * 0 on ctrl-C,
+ * >0 length of input string, including terminating '\n'
+ */
+ len = read_line_input("", buf, sizeof(buf), NULL);
+ if (len <= 0) {
+ /* Previously, ctrl-C was exiting to shell.
+ * Now we exit to ed prompt. Is in important? */
+ return;
+ }
+ if ((buf[0] == '.') && (buf[1] == '\n') && (buf[2] == '\0'))
+ return;
+ if (!insertLine(num++, buf, len))
+ return;
+ }
+}
+
+
+/*
+ * Parse a line number argument if it is present. This is a sum
+ * or difference of numbers, '.', '$', 'x, or a search string.
+ * Returns TRUE if successful (whether or not there was a number).
+ * Returns FALSE if there was a parsing error, with a message output.
+ * Whether there was a number is returned indirectly, as is the number.
+ * The character pointer which stopped the scan is also returned.
+ */
+static int getNum(const char **retcp, smallint *retHaveNum, int *retNum)
+{
+ const char *cp;
+ char *endStr, str[USERSIZE];
+ int value, num;
+ smallint haveNum, minus;
+
+ cp = *retcp;
+ value = 0;
+ haveNum = FALSE;
+ minus = 0;
+
+ while (TRUE) {
+ cp = skip_blank(cp);
+
+ switch (*cp) {
+ case '.':
+ haveNum = TRUE;
+ num = curNum;
+ cp++;
+ break;
+
+ case '$':
+ haveNum = TRUE;
+ num = lastNum;
+ cp++;
+ break;
+
+ case '\'':
+ cp++;
+ if ((*cp < 'a') || (*cp > 'z')) {
+ bb_error_msg("bad mark name");
+ return FALSE;
+ }
+ haveNum = TRUE;
+ num = marks[*cp++ - 'a'];
+ break;
+
+ case '/':
+ strcpy(str, ++cp);
+ endStr = strchr(str, '/');
+ if (endStr) {
+ *endStr++ = '\0';
+ cp += (endStr - str);
+ } else
+ cp = "";
+ num = searchLines(str, curNum, lastNum);
+ if (num == 0)
+ return FALSE;
+ haveNum = TRUE;
+ break;
+
+ default:
+ if (!isdigit(*cp)) {
+ *retcp = cp;
+ *retHaveNum = haveNum;
+ *retNum = value;
+ return TRUE;
+ }
+ num = 0;
+ while (isdigit(*cp))
+ num = num * 10 + *cp++ - '0';
+ haveNum = TRUE;
+ break;
+ }
+
+ value += (minus ? -num : num);
+
+ cp = skip_blank(cp);
+
+ switch (*cp) {
+ case '-':
+ minus = 1;
+ cp++;
+ break;
+
+ case '+':
+ minus = 0;
+ cp++;
+ break;
+
+ default:
+ *retcp = cp;
+ *retHaveNum = haveNum;
+ *retNum = value;
+ return TRUE;
+ }
+ }
+}
+
+
+/*
+ * Read lines from a file at the specified line number.
+ * Returns TRUE if the file was successfully read.
+ */
+static int readLines(const char *file, int num)
+{
+ int fd, cc;
+ int len, lineCount, charCount;
+ char *cp;
+
+ if ((num < 1) || (num > lastNum + 1)) {
+ bb_error_msg("bad line for read");
+ return FALSE;
+ }
+
+ fd = open(file, 0);
+ if (fd < 0) {
+ perror(file);
+ return FALSE;
+ }
+
+ bufPtr = bufBase;
+ bufUsed = 0;
+ lineCount = 0;
+ charCount = 0;
+ cc = 0;
+
+ printf("\"%s\", ", file);
+ fflush(stdout);
+
+ do {
+ cp = memchr(bufPtr, '\n', bufUsed);
+
+ if (cp) {
+ len = (cp - bufPtr) + 1;
+ if (!insertLine(num, bufPtr, len)) {
+ close(fd);
+ return FALSE;
+ }
+ bufPtr += len;
+ bufUsed -= len;
+ charCount += len;
+ lineCount++;
+ num++;
+ continue;
+ }
+
+ if (bufPtr != bufBase) {
+ memcpy(bufBase, bufPtr, bufUsed);
+ bufPtr = bufBase + bufUsed;
+ }
+
+ if (bufUsed >= bufSize) {
+ len = (bufSize * 3) / 2;
+ cp = realloc(bufBase, len);
+ if (cp == NULL) {
+ bb_error_msg("no memory for buffer");
+ close(fd);
+ return FALSE;
+ }
+ bufBase = cp;
+ bufPtr = bufBase + bufUsed;
+ bufSize = len;
+ }
+
+ cc = safe_read(fd, bufPtr, bufSize - bufUsed);
+ bufUsed += cc;
+ bufPtr = bufBase;
+
+ } while (cc > 0);
+
+ if (cc < 0) {
+ perror(file);
+ close(fd);
+ return FALSE;
+ }
+
+ if (bufUsed) {
+ if (!insertLine(num, bufPtr, bufUsed)) {
+ close(fd);
+ return -1;
+ }
+ lineCount++;
+ charCount += bufUsed;
+ }
+
+ close(fd);
+
+ printf("%d lines%s, %d chars\n", lineCount,
+ (bufUsed ? " (incomplete)" : ""), charCount);
+
+ return TRUE;
+}
+
+
+/*
+ * Write the specified lines out to the specified file.
+ * Returns TRUE if successful, or FALSE on an error with a message output.
+ */
+static int writeLines(const char *file, int num1, int num2)
+{
+ LINE *lp;
+ int fd, lineCount, charCount;
+
+ if (bad_nums(num1, num2, "write"))
+ return FALSE;
+
+ lineCount = 0;
+ charCount = 0;
+
+ fd = creat(file, 0666);
+ if (fd < 0) {
+ perror(file);
+ return FALSE;
+ }
+
+ printf("\"%s\", ", file);
+ fflush(stdout);
+
+ lp = findLine(num1);
+ if (lp == NULL) {
+ close(fd);
+ return FALSE;
+ }
+
+ while (num1++ <= num2) {
+ if (full_write(fd, lp->data, lp->len) != lp->len) {
+ perror(file);
+ close(fd);
+ return FALSE;
+ }
+ charCount += lp->len;
+ lineCount++;
+ lp = lp->next;
+ }
+
+ if (close(fd) < 0) {
+ perror(file);
+ return FALSE;
+ }
+
+ printf("%d lines, %d chars\n", lineCount, charCount);
+ return TRUE;
+}
+
+
+/*
+ * Print lines in a specified range.
+ * The last line printed becomes the current line.
+ * If expandFlag is TRUE, then the line is printed specially to
+ * show magic characters.
+ */
+static int printLines(int num1, int num2, int expandFlag)
+{
+ const LINE *lp;
+ const char *cp;
+ int ch, count;
+
+ if (bad_nums(num1, num2, "print"))
+ return FALSE;
+
+ lp = findLine(num1);
+ if (lp == NULL)
+ return FALSE;
+
+ while (num1 <= num2) {
+ if (!expandFlag) {
+ write(STDOUT_FILENO, lp->data, lp->len);
+ setCurNum(num1++);
+ lp = lp->next;
+ continue;
+ }
+
+ /*
+ * Show control characters and characters with the
+ * high bit set specially.
+ */
+ cp = lp->data;
+ count = lp->len;
+
+ if ((count > 0) && (cp[count - 1] == '\n'))
+ count--;
+
+ while (count-- > 0) {
+ ch = (unsigned char) *cp++;
+ fputc_printable(ch | PRINTABLE_META, stdout);
+ }
+
+ fputs("$\n", stdout);
+
+ setCurNum(num1++);
+ lp = lp->next;
+ }
+
+ return TRUE;
+}
+
+
+/*
+ * Insert a new line with the specified text.
+ * The line is inserted so as to become the specified line,
+ * thus pushing any existing and further lines down one.
+ * The inserted line is also set to become the current line.
+ * Returns TRUE if successful.
+ */
+static int insertLine(int num, const char *data, int len)
+{
+ LINE *newLp, *lp;
+
+ if ((num < 1) || (num > lastNum + 1)) {
+ bb_error_msg("inserting at bad line number");
+ return FALSE;
+ }
+
+ newLp = malloc(sizeof(LINE) + len - 1);
+ if (newLp == NULL) {
+ bb_error_msg("failed to allocate memory for line");
+ return FALSE;
+ }
+
+ memcpy(newLp->data, data, len);
+ newLp->len = len;
+
+ if (num > lastNum)
+ lp = &lines;
+ else {
+ lp = findLine(num);
+ if (lp == NULL) {
+ free((char *) newLp);
+ return FALSE;
+ }
+ }
+
+ newLp->next = lp;
+ newLp->prev = lp->prev;
+ lp->prev->next = newLp;
+ lp->prev = newLp;
+
+ lastNum++;
+ dirty = TRUE;
+ return setCurNum(num);
+}
+
+
+/*
+ * Delete lines from the given range.
+ */
+static void deleteLines(int num1, int num2)
+{
+ LINE *lp, *nlp, *plp;
+ int count;
+
+ if (bad_nums(num1, num2, "delete"))
+ return;
+
+ lp = findLine(num1);
+ if (lp == NULL)
+ return;
+
+ if ((curNum >= num1) && (curNum <= num2)) {
+ if (num2 < lastNum)
+ setCurNum(num2 + 1);
+ else if (num1 > 1)
+ setCurNum(num1 - 1);
+ else
+ curNum = 0;
+ }
+
+ count = num2 - num1 + 1;
+ if (curNum > num2)
+ curNum -= count;
+ lastNum -= count;
+
+ while (count-- > 0) {
+ nlp = lp->next;
+ plp = lp->prev;
+ plp->next = nlp;
+ nlp->prev = plp;
+ free(lp);
+ lp = nlp;
+ }
+
+ dirty = TRUE;
+}
+
+
+/*
+ * Search for a line which contains the specified string.
+ * If the string is "", then the previously searched for string
+ * is used. The currently searched for string is saved for future use.
+ * Returns the line number which matches, or 0 if there was no match
+ * with an error printed.
+ */
+static int searchLines(const char *str, int num1, int num2)
+{
+ const LINE *lp;
+ int len;
+
+ if (bad_nums(num1, num2, "search"))
+ return 0;
+
+ if (*str == '\0') {
+ if (searchString[0] == '\0') {
+ bb_error_msg("no previous search string");
+ return 0;
+ }
+ str = searchString;
+ }
+
+ if (str != searchString)
+ strcpy(searchString, str);
+
+ len = strlen(str);
+
+ lp = findLine(num1);
+ if (lp == NULL)
+ return 0;
+
+ while (num1 <= num2) {
+ if (findString(lp, str, len, 0) >= 0)
+ return num1;
+ num1++;
+ lp = lp->next;
+ }
+
+ bb_error_msg("cannot find string \"%s\"", str);
+ return 0;
+}
+
+
+/*
+ * Return a pointer to the specified line number.
+ */
+static LINE *findLine(int num)
+{
+ LINE *lp;
+ int lnum;
+
+ if ((num < 1) || (num > lastNum)) {
+ bb_error_msg("line number %d does not exist", num);
+ return NULL;
+ }
+
+ if (curNum <= 0) {
+ curNum = 1;
+ curLine = lines.next;
+ }
+
+ if (num == curNum)
+ return curLine;
+
+ lp = curLine;
+ lnum = curNum;
+ if (num < (curNum / 2)) {
+ lp = lines.next;
+ lnum = 1;
+ } else if (num > ((curNum + lastNum) / 2)) {
+ lp = lines.prev;
+ lnum = lastNum;
+ }
+
+ while (lnum < num) {
+ lp = lp->next;
+ lnum++;
+ }
+
+ while (lnum > num) {
+ lp = lp->prev;
+ lnum--;
+ }
+ return lp;
+}
+
+
+/*
+ * Set the current line number.
+ * Returns TRUE if successful.
+ */
+static int setCurNum(int num)
+{
+ LINE *lp;
+
+ lp = findLine(num);
+ if (lp == NULL)
+ return FALSE;
+ curNum = num;
+ curLine = lp;
+ return TRUE;
+}
diff --git a/release/src/router/busybox/editors/patch.c b/release/src/router/busybox/editors/patch.c
index 1587919b..e8482a7a 100644
--- a/release/src/router/busybox/editors/patch.c
+++ b/release/src/router/busybox/editors/patch.c
@@ -1,23 +1,9 @@
/* vi: set sw=4 ts=4: */
/*
* busybox patch applet to handle the unified diff format.
- * Copyright (C) 2003 Glenn McGrath <bug1@optushome.com.au>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- *
+ * Copyright (C) 2003 Glenn McGrath
*
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*
* This applet is written to work with patches generated by GNU diff,
* where there is equivalent functionality busybox patch shall behave
@@ -29,262 +15,240 @@
*
* Issues
* - Non-interactive
- * - Patches must apply cleanly or the hunk will fail.
+ * - Patches must apply cleanly or patch (not just one hunk) will fail.
* - Reject file isnt saved
- * -
*/
-#include <getopt.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include "busybox.h"
#include "libbb.h"
-static int copy_lines(FILE *src_stream, FILE *dest_stream, const unsigned int lines_count)
+static unsigned copy_lines(FILE *src_stream, FILE *dst_stream, unsigned lines_count)
{
- int i = 0;
-
- while (src_stream && (i < lines_count)) {
+ while (src_stream && lines_count) {
char *line;
- line = bb_get_line_from_file(src_stream);
+ line = xmalloc_fgets(src_stream);
if (line == NULL) {
break;
}
- if (fputs(line, dest_stream) == EOF) {
- bb_perror_msg_and_die("Error writing to new file");
+ if (fputs(line, dst_stream) == EOF) {
+ bb_perror_msg_and_die("error writing to new file");
}
free(line);
-
- i++;
+ lines_count--;
}
- return(i);
+ return lines_count;
}
/* If patch_level is -1 it will remove all directory names
* char *line must be greater than 4 chars
* returns NULL if the file doesnt exist or error
* returns malloc'ed filename
+ * NB: frees 1st argument!
*/
-
-static unsigned char *extract_filename(char *line, unsigned short patch_level)
+static char *extract_filename(char *line, int patch_level, const char *pat)
{
- char *filename_start_ptr = line + 4;
- int i;
+ char *temp = NULL, *filename_start_ptr = line + 4;
- /* Terminate string at end of source filename */
- {
- char *line_ptr;
- line_ptr = strchr(filename_start_ptr, '\t');
- if (!line_ptr) {
- bb_perror_msg("Malformed line %s", line);
- return(NULL);
- }
- *line_ptr = '\0';
- }
+ if (strncmp(line, pat, 4) == 0) {
+ /* Terminate string at end of source filename */
+ line[strcspn(line, "\t\n\r")] = '\0';
- /* Skip over (patch_level) number of leading directories */
- for (i = 0; i < patch_level; i++) {
- char *dirname_ptr;
-
- dirname_ptr = strchr(filename_start_ptr, '/');
- if (!dirname_ptr) {
- break;
+ /* Skip over (patch_level) number of leading directories */
+ while (patch_level--) {
+ temp = strchr(filename_start_ptr, '/');
+ if (!temp)
+ break;
+ filename_start_ptr = temp + 1;
}
- filename_start_ptr = dirname_ptr + 1;
+ temp = xstrdup(filename_start_ptr);
}
-
- return(bb_xstrdup(filename_start_ptr));
-}
-
-static int file_doesnt_exist(const char *filename)
-{
- struct stat statbuf;
- return(stat(filename, &statbuf));
+ free(line);
+ return temp;
}
-extern int patch_main(int argc, char **argv)
+int patch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int patch_main(int argc UNUSED_PARAM, char **argv)
{
- unsigned int patch_level = -1;
+ struct stat saved_stat;
char *patch_line;
+ FILE *patch_file;
+ int patch_level;
int ret = 0;
+ char plus = '+';
- /* Handle 'p' option */
- if (argv[1] && (argv[1][0] == '-') && (argv[1][1] == 'p')) {
- patch_level = atoi(&argv[1][2]);
+ xfunc_error_retval = 2;
+ {
+ const char *p = "-1";
+ const char *i = "-"; /* compat */
+ if (getopt32(argv, "p:i:R", &p, &i) & 4)
+ plus = '-';
+ patch_level = xatoi(p); /* can be negative! */
+ patch_file = xfopen_stdin(i);
}
- patch_line = bb_get_line_from_file(stdin);
+ patch_line = xmalloc_fgetline(patch_file);
while (patch_line) {
FILE *src_stream;
FILE *dst_stream;
- char *original_filename;
+ //char *old_filename;
char *new_filename;
char *backup_filename;
- unsigned int src_cur_line = 1;
- unsigned int dest_cur_line = 0;
- unsigned int dest_beg_line;
- unsigned int bad_hunk_count = 0;
- unsigned int hunk_count = 0;
- char copy_trailing_lines_flag = 0;
+ unsigned src_cur_line = 1;
+ unsigned dst_cur_line = 0;
+ unsigned dst_beg_line;
+ unsigned bad_hunk_count = 0;
+ unsigned hunk_count = 0;
+ smallint copy_trailing_lines_flag = 0;
/* Skip everything upto the "---" marker
* No need to parse the lines "Only in <dir>", and "diff <args>"
*/
- while (patch_line && strncmp(patch_line, "--- ", 4) != 0) {
- free(patch_line);
- patch_line = bb_get_line_from_file(stdin);
+ do {
+ /* Extract the filename used before the patch was generated */
+ new_filename = extract_filename(patch_line, patch_level, "--- ");
+ // was old_filename above
+ patch_line = xmalloc_fgetline(patch_file);
+ if (!patch_line) goto quit;
+ } while (!new_filename);
+ free(new_filename); // "source" filename is irrelevant
+
+ new_filename = extract_filename(patch_line, patch_level, "+++ ");
+ if (!new_filename) {
+ bb_error_msg_and_die("invalid patch");
}
- /* Extract the filename used before the patch was generated */
- original_filename = extract_filename(patch_line, patch_level);
- free(patch_line);
-
- patch_line = bb_get_line_from_file(stdin);
- if (strncmp(patch_line, "+++ ", 4) != 0) {
- ret = 2;
- bb_error_msg("Invalid patch");
- continue;
- }
- new_filename = extract_filename(patch_line, patch_level);
- free(patch_line);
-
- if (file_doesnt_exist(new_filename)) {
- char *line_ptr;
- /* Create leading directories */
- line_ptr = strrchr(new_filename, '/');
- if (line_ptr) {
- *line_ptr = '\0';
+ /* Get access rights from the file to be patched */
+ if (stat(new_filename, &saved_stat) != 0) {
+ char *slash = strrchr(new_filename, '/');
+ if (slash) {
+ /* Create leading directories */
+ *slash = '\0';
bb_make_directory(new_filename, -1, FILEUTILS_RECUR);
- *line_ptr = '/';
+ *slash = '/';
}
- dst_stream = bb_xfopen(new_filename, "w+");
backup_filename = NULL;
- } else {
- backup_filename = xmalloc(strlen(new_filename) + 6);
- strcpy(backup_filename, new_filename);
- strcat(backup_filename, ".orig");
- if (rename(new_filename, backup_filename) == -1) {
- bb_perror_msg_and_die("Couldnt create file %s", backup_filename);
- }
- dst_stream = bb_xfopen(new_filename, "w");
- }
-
- if ((backup_filename == NULL) || file_doesnt_exist(original_filename)) {
src_stream = NULL;
+ saved_stat.st_mode = 0644;
} else {
- if (strcmp(original_filename, new_filename) == 0) {
- src_stream = bb_xfopen(backup_filename, "r");
- } else {
- src_stream = bb_xfopen(original_filename, "r");
- }
+ backup_filename = xasprintf("%s.orig", new_filename);
+ xrename(new_filename, backup_filename);
+ src_stream = xfopen_for_read(backup_filename);
}
+ dst_stream = xfopen_for_write(new_filename);
+ fchmod(fileno(dst_stream), saved_stat.st_mode);
printf("patching file %s\n", new_filename);
- /* Handle each hunk */
- patch_line = bb_get_line_from_file(stdin);
+ /* Handle all hunks for this file */
+ patch_line = xmalloc_fgets(patch_file);
while (patch_line) {
- unsigned int count;
- unsigned int src_beg_line;
- unsigned int unused;
- unsigned int hunk_offset_start = 0;
- int hunk_error = 0;
-
- /* This bit should be improved */
- if ((sscanf(patch_line, "@@ -%d,%d +%d,%d @@", &src_beg_line, &unused, &dest_beg_line, &unused) != 4) &&
- (sscanf(patch_line, "@@ -%d,%d +%d @@", &src_beg_line, &unused, &dest_beg_line) != 3) &&
- (sscanf(patch_line, "@@ -%d +%d,%d @@", &src_beg_line, &dest_beg_line, &unused) != 3)) {
+ unsigned count;
+ unsigned src_beg_line;
+ unsigned hunk_offset_start;
+ unsigned src_last_line = 1;
+ unsigned dst_last_line = 1;
+
+ if ((sscanf(patch_line, "@@ -%d,%d +%d,%d", &src_beg_line, &src_last_line, &dst_beg_line, &dst_last_line) < 3)
+ && (sscanf(patch_line, "@@ -%d +%d,%d", &src_beg_line, &dst_beg_line, &dst_last_line) < 2)
+ ) {
/* No more hunks for this file */
break;
}
- free(patch_line);
+ if (plus != '+') {
+ /* reverse patch */
+ unsigned tmp = src_last_line;
+ src_last_line = dst_last_line;
+ dst_last_line = tmp;
+ tmp = src_beg_line;
+ src_beg_line = dst_beg_line;
+ dst_beg_line = tmp;
+ }
hunk_count++;
- if (src_beg_line && dest_beg_line) {
+ if (src_beg_line && dst_beg_line) {
/* Copy unmodified lines upto start of hunk */
- /* src_beg_line will be 0 if its a new file */
+ /* src_beg_line will be 0 if it's a new file */
count = src_beg_line - src_cur_line;
- if (copy_lines(src_stream, dst_stream, count) != count) {
- bb_error_msg_and_die("Bad src file");
+ if (copy_lines(src_stream, dst_stream, count)) {
+ bb_error_msg_and_die("bad src file");
}
src_cur_line += count;
- dest_cur_line += count;
+ dst_cur_line += count;
copy_trailing_lines_flag = 1;
}
- hunk_offset_start = src_cur_line;
+ src_last_line += hunk_offset_start = src_cur_line;
+ dst_last_line += dst_cur_line;
- while ((patch_line = bb_get_line_from_file(stdin)) != NULL) {
- if ((*patch_line == '-') || (*patch_line == ' ')) {
+ while (1) {
+ free(patch_line);
+ patch_line = xmalloc_fgets(patch_file);
+ if (patch_line == NULL)
+ break; /* EOF */
+ if ((*patch_line != '-') && (*patch_line != '+')
+ && (*patch_line != ' ')
+ ) {
+ break; /* End of hunk */
+ }
+ if (*patch_line != plus) { /* '-' or ' ' */
char *src_line = NULL;
+ if (src_cur_line == src_last_line)
+ break;
if (src_stream) {
- src_line = bb_get_line_from_file(src_stream);
- if (!src_line) {
- hunk_error++;
- break;
- } else {
+ src_line = xmalloc_fgets(src_stream);
+ if (src_line) {
+ int diff = strcmp(src_line, patch_line + 1);
src_cur_line++;
+ free(src_line);
+ if (diff)
+ src_line = NULL;
}
- if (strcmp(src_line, patch_line + 1) != 0) {
- bb_error_msg("Hunk #%d FAILED at %d.", hunk_count, hunk_offset_start);
- hunk_error++;
- free(patch_line);
- break;
- }
- free(src_line);
}
- if (*patch_line == ' ') {
- fputs(patch_line + 1, dst_stream);
- dest_cur_line++;
+ if (!src_line) {
+ bb_error_msg("hunk #%u FAILED at %u", hunk_count, hunk_offset_start);
+ bad_hunk_count++;
+ break;
+ }
+ if (*patch_line != ' ') { /* '-' */
+ continue;
}
- } else if (*patch_line == '+') {
- fputs(patch_line + 1, dst_stream);
- dest_cur_line++;
- } else {
- break;
}
- free(patch_line);
- }
- if (hunk_error) {
- bad_hunk_count++;
- }
- }
+ if (dst_cur_line == dst_last_line)
+ break;
+ fputs(patch_line + 1, dst_stream);
+ dst_cur_line++;
+ } /* end of while loop handling one hunk */
+ } /* end of while loop handling one file */
/* Cleanup last patched file */
if (copy_trailing_lines_flag) {
- copy_lines(src_stream, dst_stream, -1);
+ copy_lines(src_stream, dst_stream, (unsigned)(-1));
}
if (src_stream) {
fclose(src_stream);
}
- if (dst_stream) {
- fclose(dst_stream);
- }
+ fclose(dst_stream);
if (bad_hunk_count) {
- if (!ret) {
- ret = 1;
- }
- bb_error_msg("%d out of %d hunk FAILED", bad_hunk_count, hunk_count);
+ ret = 1;
+ bb_error_msg("%u out of %u hunk FAILED", bad_hunk_count, hunk_count);
} else {
/* It worked, we can remove the backup */
if (backup_filename) {
unlink(backup_filename);
}
- if ((dest_cur_line == 0) || (dest_beg_line == 0)) {
+ if ((dst_cur_line == 0) || (dst_beg_line == 0)) {
/* The new patched file is empty, remove it */
- if (unlink(new_filename) == -1) {
- bb_perror_msg_and_die("Couldnt remove file %s", new_filename);
- }
- if (unlink(original_filename) == -1) {
- bb_perror_msg_and_die("Couldnt remove original file %s", new_filename);
- }
+ xunlink(new_filename);
+ // /* old_filename and new_filename may be the same file */
+ // unlink(old_filename);
}
}
- }
-
- /* 0 = SUCCESS
+ free(backup_filename);
+ //free(old_filename);
+ free(new_filename);
+ } /* end of "while there are patch lines" */
+ quit:
+ /* 0 = SUCCESS
* 1 = Some hunks failed
- * 2 = More serious problems
+ * 2 = More serious problems (exited earlier)
*/
- return(ret);
+ return ret;
}
diff --git a/release/src/router/busybox/editors/sed.c b/release/src/router/busybox/editors/sed.c
index 6beba066..eb31f7d2 100644
--- a/release/src/router/busybox/editors/sed.c
+++ b/release/src/router/busybox/editors/sed.c
@@ -1,27 +1,39 @@
+/* vi: set sw=4 ts=4: */
/*
* sed.c - very minimalist version of sed
*
* Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
* Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
* Copyright (C) 2002 Matt Kraai
- * Copyright (C) 2003 by Glenn McGrath <bug1@optushome.com.au>
+ * Copyright (C) 2003 by Glenn McGrath
+ * Copyright (C) 2003,2004 by Rob Landley <rob@landley.net>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * MAINTAINER: Rob Landley <rob@landley.net>
*
+ * Licensed under GPL version 2, see file LICENSE in this tarball for details.
*/
+/* Code overview.
+
+ Files are laid out to avoid unnecessary function declarations. So for
+ example, every function add_cmd calls occurs before add_cmd in this file.
+
+ add_cmd() is called on each line of sed command text (from a file or from
+ the command line). It calls get_address() and parse_cmd_args(). The
+ resulting sed_cmd_t structures are appended to a linked list
+ (G.sed_cmd_head/G.sed_cmd_tail).
+
+ add_input_file() adds a FILE* to the list of input files. We need to
+ know all input sources ahead of time to find the last line for the $ match.
+
+ process_files() does actual sedding, reading data lines from each input FILE *
+ (which could be stdin) and applying the sed command list (sed_cmd_head) to
+ each of the resulting lines.
+
+ sed_main() is where external code calls into this, with a command line.
+*/
+
+
/*
Supported features and commands in this version of sed:
@@ -30,114 +42,104 @@
- commands: (p)rint, (d)elete, (s)ubstitue (with g & I flags)
- edit commands: (a)ppend, (i)nsert, (c)hange
- file commands: (r)ead
- - backreferences in substitution expressions (\1, \2...\9)
+ - backreferences in substitution expressions (\0, \1, \2...\9)
- grouped commands: {cmd1;cmd2}
- transliteration (y/source-chars/dest-chars/)
- pattern space hold space storing / swapping (g, h, x)
- - labels / branching (: label, b, t)
+ - labels / branching (: label, b, t, T)
(Note: Specifying an address (range) to match is *optional*; commands
default to the whole pattern space if no specific address match was
requested.)
- Unsupported features:
-
- - GNU extensions
- - and lots, lots more.
-
- Bugs:
-
- - Cant subst globally using ^ or $ in regex, eg. "aah" | sed 's/^a/b/g'
+ Todo:
+ - Create a wrapper around regex to make libc's regex conform with sed
Reference http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
*/
-#include <stdio.h>
-#include <unistd.h> /* for getopt() */
-#include <regex.h>
-#include <string.h> /* for strdup() */
-#include <errno.h>
-#include <ctype.h> /* for isspace() */
-#include <stdlib.h>
-#include "busybox.h"
+#include "libbb.h"
+#include "xregex.h"
+/* Each sed command turns into one of these structures. */
typedef struct sed_cmd_s {
- /* Order by alignment requirements */
+ /* Ordered by alignment requirements: currently 36 bytes on x86 */
+ struct sed_cmd_s *next; /* Next command (linked list, NULL terminated) */
/* address storage */
- regex_t *beg_match; /* sed -e '/match/cmd' */
- regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
-
- int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */
- int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */
-
- /* inversion flag */
- int invert; /* the '!' after the address */
-// int block_cmd; /* This command is part of a group that has a command address */
-
- /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
-
- /* sed -e 's/sub_match/replace/' */
- regex_t *sub_match;
- char *replace;
+ regex_t *beg_match; /* sed -e '/match/cmd' */
+ regex_t *end_match; /* sed -e '/match/,/end_match/cmd' */
+ regex_t *sub_match; /* For 's/sub_match/string/' */
+ int beg_line; /* 'sed 1p' 0 == apply commands to all lines */
+ int end_line; /* 'sed 1,3p' 0 == one line only. -1 = last line ($) */
- /* EDIT COMMAND (a,i,c) SPECIFIC FIELDS */
- char *editline;
+ FILE *sw_file; /* File (sw) command writes to, -1 for none. */
+ char *string; /* Data string for (saicytb) commands. */
- /* FILE COMMAND (r) SPECIFIC FIELDS */
- char *filename;
+ unsigned which_match; /* (s) Which match to replace (0 for all) */
- /* SUBSTITUTION COMMAND SPECIFIC FIELDS */
+ /* Bitfields (gcc won't group them if we don't) */
+ unsigned invert:1; /* the '!' after the address */
+ unsigned in_match:1; /* Next line also included in match? */
+ unsigned sub_p:1; /* (s) print option */
- unsigned int num_backrefs:4; /* how many back references (\1..\9) */
- /* Note: GNU/POSIX sed does not save more than nine backrefs, so
- * we only use 4 bits to hold the number */
- unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */
- unsigned int sub_p:1; /* sed -e 's/foo/bar/p' (print substitution) */
-
- /* TRANSLATE COMMAND */
- char *translate;
+ char sw_last_char; /* Last line written by (sw) had no '\n' */
/* GENERAL FIELDS */
- /* the command */
- char cmd; /* p,d,s (add more at your leisure :-) */
+ char cmd; /* The command char: abcdDgGhHilnNpPqrstwxy:={} */
+} sed_cmd_t;
- /* Branch commands */
- char *label;
+static const char semicolon_whitespace[] ALIGN1 = "; \n\r\t\v";
- /* next command in list (sequential list of specified commands) */
- struct sed_cmd_s *next;
+struct globals {
+ /* options */
+ int be_quiet, regex_type;
+ FILE *nonstdout;
+ char *outname, *hold_space;
-} sed_cmd_t;
+ /* List of input files */
+ int input_file_count, current_input_file;
+ FILE **input_file_list;
+ regmatch_t regmatch[10];
+ regex_t *previous_regex_ptr;
-/* externs */
-extern void xregcomp(regex_t * preg, const char *regex, int cflags);
-extern int optind; /* in unistd.h */
-extern char *optarg; /* ditto */
+ /* linked list of sed commands */
+ sed_cmd_t sed_cmd_head, *sed_cmd_tail;
-/* globals */
-/* options */
-static int be_quiet = 0;
-static const char bad_format_in_subst[] =
- "bad format in substitution expression";
+ /* Linked list of append lines */
+ llist_t *append_head;
-/* linked list of sed commands */
-static sed_cmd_t sed_cmd_head;
-static sed_cmd_t *sed_cmd_tail = &sed_cmd_head;
+ char *add_cmd_line;
-const char *const semicolon_whitespace = "; \n\r\t\v\0";
-static regex_t *previous_regex_ptr = NULL;
+ struct pipeline {
+ char *buf; /* Space to hold string */
+ int idx; /* Space used */
+ int len; /* Space allocated */
+ } pipeline;
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
+void BUG_sed_globals_too_big(void);
+#define INIT_G() do { \
+ if (sizeof(struct globals) > COMMON_BUFSIZE) \
+ BUG_sed_globals_too_big(); \
+ G.sed_cmd_tail = &G.sed_cmd_head; \
+} while (0)
-#ifdef CONFIG_FEATURE_CLEAN_UP
-static void destroy_cmd_strs(void)
+#if ENABLE_FEATURE_CLEAN_UP
+static void sed_free_and_close_stuff(void)
{
- sed_cmd_t *sed_cmd = sed_cmd_head.next;
+ sed_cmd_t *sed_cmd = G.sed_cmd_head.next;
+
+ llist_free(G.append_head, free);
while (sed_cmd) {
sed_cmd_t *sed_cmd_next = sed_cmd->next;
+ if (sed_cmd->sw_file)
+ xprint_and_close_file(sed_cmd->sw_file);
+
if (sed_cmd->beg_match) {
regfree(sed_cmd->beg_match);
free(sed_cmd->beg_match);
@@ -150,75 +152,119 @@ static void destroy_cmd_strs(void)
regfree(sed_cmd->sub_match);
free(sed_cmd->sub_match);
}
- free(sed_cmd->replace);
+ free(sed_cmd->string);
free(sed_cmd);
sed_cmd = sed_cmd_next;
}
+
+ free(G.hold_space);
+
+ while (G.current_input_file < G.input_file_count)
+ fclose(G.input_file_list[G.current_input_file++]);
}
+#else
+void sed_free_and_close_stuff(void);
#endif
+/* If something bad happens during -i operation, delete temp file */
+
+static void cleanup_outname(void)
+{
+ if (G.outname) unlink(G.outname);
+}
+
+/* strcpy, replacing "\from" with 'to'. If to is NUL, replacing "\any" with 'any' */
+
+static void parse_escapes(char *dest, const char *string, int len, char from, char to)
+{
+ int i = 0;
+
+ while (i < len) {
+ if (string[i] == '\\') {
+ if (!to || string[i+1] == from) {
+ *dest++ = to ? to : string[i+1];
+ i += 2;
+ continue;
+ }
+ *dest++ = string[i++];
+ }
+ /* TODO: is it safe wrt a string with trailing '\\' ? */
+ *dest++ = string[i++];
+ }
+ *dest = '\0';
+}
+
+static char *copy_parsing_escapes(const char *string, int len)
+{
+ char *dest = xmalloc(len + 1);
+
+ parse_escapes(dest, string, len, 'n', '\n');
+ /* GNU sed also recognizes \t */
+ parse_escapes(dest, dest, strlen(dest), 't', '\t');
+ return dest;
+}
+
+
/*
* index_of_next_unescaped_regexp_delim - walks left to right through a string
* beginning at a specified index and returns the index of the next regular
- * expression delimiter (typically a forward * slash ('/')) not preceeded by
- * a backslash ('\').
+ * expression delimiter (typically a forward slash ('/')) not preceded by
+ * a backslash ('\'). A negative delimiter disables square bracket checking.
*/
-static int index_of_next_unescaped_regexp_delim(const char delimiter,
- const char *str)
+static int index_of_next_unescaped_regexp_delim(int delimiter, const char *str)
{
int bracket = -1;
int escaped = 0;
int idx = 0;
char ch;
+ if (delimiter < 0) {
+ bracket--;
+ delimiter = -delimiter;
+ }
+
for (; (ch = str[idx]); idx++) {
- if (bracket != -1) {
+ if (bracket >= 0) {
if (ch == ']' && !(bracket == idx - 1 || (bracket == idx - 2
- && str[idx - 1] == '^')))
+ && str[idx - 1] == '^')))
bracket = -1;
} else if (escaped)
escaped = 0;
else if (ch == '\\')
escaped = 1;
- else if (ch == '[')
+ else if (bracket == -1 && ch == '[')
bracket = idx;
else if (ch == delimiter)
return idx;
}
/* if we make it to here, we've hit the end of the string */
- return -1;
+ bb_error_msg_and_die("unmatched '%c'", delimiter);
}
+/*
+ * Returns the index of the third delimiter
+ */
static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
{
const char *cmdstr_ptr = cmdstr;
char delimiter;
int idx = 0;
- /* verify that the 's' is followed by something. That something
+ /* verify that the 's' or 'y' is followed by something. That something
* (typically a 'slash') is now our regexp delimiter... */
if (*cmdstr == '\0')
- bb_error_msg_and_die(bad_format_in_subst);
- else
- delimiter = *cmdstr_ptr;
-
- cmdstr_ptr++;
+ bb_error_msg_and_die("bad format in substitution expression");
+ delimiter = *cmdstr_ptr++;
/* save the match string */
idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
- if (idx == -1) {
- bb_error_msg_and_die(bad_format_in_subst);
- }
- *match = bb_xstrndup(cmdstr_ptr, idx);
+ *match = copy_parsing_escapes(cmdstr_ptr, idx);
/* save the replacement string */
cmdstr_ptr += idx + 1;
- idx = index_of_next_unescaped_regexp_delim(delimiter, cmdstr_ptr);
- if (idx == -1) {
- bb_error_msg_and_die(bad_format_in_subst);
- }
- *replace = bb_xstrndup(cmdstr_ptr, idx);
+ idx = index_of_next_unescaped_regexp_delim(-delimiter, cmdstr_ptr);
+ *replace = copy_parsing_escapes(cmdstr_ptr, idx);
return ((cmdstr_ptr - cmdstr) + idx);
}
@@ -226,99 +272,133 @@ static int parse_regex_delim(const char *cmdstr, char **match, char **replace)
/*
* returns the index in the string just past where the address ends.
*/
-static int get_address(char *my_str, int *linenum, regex_t ** regex)
+static int get_address(const char *my_str, int *linenum, regex_t ** regex)
{
- int idx = 0;
-
- if (isdigit(my_str[idx])) {
- char *endstr;
+ const char *pos = my_str;
- *linenum = strtol(my_str, &endstr, 10);
+ if (isdigit(*my_str)) {
+ *linenum = strtol(my_str, (char**)&pos, 10);
/* endstr shouldnt ever equal NULL */
- idx = endstr - my_str;
- } else if (my_str[idx] == '$') {
+ } else if (*my_str == '$') {
*linenum = -1;
- idx++;
- } else if (my_str[idx] == '/' || my_str[idx] == '\\') {
- int idx_start = 1;
+ pos++;
+ } else if (*my_str == '/' || *my_str == '\\') {
+ int next;
char delimiter;
+ char *temp;
delimiter = '/';
- if (my_str[idx] == '\\') {
- idx_start++;
- delimiter = my_str[++idx];
- }
+ if (*my_str == '\\') delimiter = *++pos;
+ next = index_of_next_unescaped_regexp_delim(delimiter, ++pos);
+ temp = copy_parsing_escapes(pos, next);
+ *regex = xmalloc(sizeof(regex_t));
+ xregcomp(*regex, temp, G.regex_type|REG_NEWLINE);
+ free(temp);
+ /* Move position to next character after last delimiter */
+ pos += (next+1);
+ }
+ return pos - my_str;
+}
+
+/* Grab a filename. Whitespace at start is skipped, then goes to EOL. */
+static int parse_file_cmd(/*sed_cmd_t *sed_cmd,*/ const char *filecmdstr, char **retval)
+{
+ int start = 0, idx, hack = 0;
+
+ /* Skip whitespace, then grab filename to end of line */
+ while (isspace(filecmdstr[start]))
+ start++;
+ idx = start;
+ while (filecmdstr[idx] && filecmdstr[idx] != '\n')
idx++;
- idx += index_of_next_unescaped_regexp_delim(delimiter, my_str + idx);
- if (idx == -1) {
- bb_error_msg_and_die("unterminated match expression");
- }
- my_str[idx] = '\0';
- *regex = (regex_t *) xmalloc(sizeof(regex_t));
- xregcomp(*regex, my_str + idx_start, REG_NEWLINE);
- idx++; /* so it points to the next character after the last '/' */
- }
+ /* If lines glued together, put backslash back. */
+ if (filecmdstr[idx] == '\n')
+ hack = 1;
+ if (idx == start)
+ bb_error_msg_and_die("empty filename");
+ *retval = xstrndup(filecmdstr+start, idx-start+hack+1);
+ if (hack)
+ (*retval)[idx] = '\\';
+
return idx;
}
-static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
+static int parse_subst_cmd(sed_cmd_t *sed_cmd, const char *substr)
{
- int cflags = 0;
+ int cflags = G.regex_type;
char *match;
- int idx = 0;
- int j;
+ int idx;
/*
- * the string that gets passed to this function should look like this:
- * s/match/replace/gIp
+ * A substitution command should look something like this:
+ * s/match/replace/ #gIpw
* || | |||
* mandatory optional
- *
- * (all three of the '/' slashes are mandatory)
*/
- idx = parse_regex_delim(substr, &match, &sed_cmd->replace);
+ idx = parse_regex_delim(substr, &match, &sed_cmd->string);
/* determine the number of back references in the match string */
/* Note: we compute this here rather than in the do_subst_command()
* function to save processor time, at the expense of a little more memory
* (4 bits) per sed_cmd */
- /* sed_cmd->num_backrefs = 0; *//* XXX: not needed? --apparently not */
- for (j = 0; match[j]; j++) {
- /* GNU/POSIX sed does not save more than nine backrefs */
- if (match[j] == '\\' && match[j + 1] == '('
- && sed_cmd->num_backrefs <= 9)
- sed_cmd->num_backrefs++;
- }
-
/* process the flags */
+
+ sed_cmd->which_match = 1;
while (substr[++idx]) {
+ /* Parse match number */
+ if (isdigit(substr[idx])) {
+ if (match[0] != '^') {
+ /* Match 0 treated as all, multiple matches we take the last one. */
+ const char *pos = substr + idx;
+/* FIXME: error check? */
+ sed_cmd->which_match = (unsigned)strtol(substr+idx, (char**) &pos, 10);
+ idx = pos - substr;
+ }
+ continue;
+ }
+ /* Skip spaces */
+ if (isspace(substr[idx])) continue;
+
switch (substr[idx]) {
+ /* Replace all occurrences */
case 'g':
- sed_cmd->sub_g = 1;
- break;
- /* Hmm, i dont see the I option mentioned in the standard */
- case 'I':
- cflags |= REG_ICASE;
+ if (match[0] != '^')
+ sed_cmd->which_match = 0;
break;
+ /* Print pattern space */
case 'p':
sed_cmd->sub_p = 1;
break;
+ /* Write to file */
+ case 'w':
+ {
+ char *temp;
+ idx += parse_file_cmd(/*sed_cmd,*/ substr+idx, &temp);
+ break;
+ }
+ /* Ignore case (gnu exension) */
+ case 'I':
+ cflags |= REG_ICASE;
+ break;
+ /* Comment */
+ case '#':
+ while (substr[++idx]) /*skip all*/;
+ /* Fall through */
+ /* End of command */
+ case ';':
+ case '}':
+ goto out;
default:
- /* any whitespace or semicolon trailing after a s/// is ok */
- if (strchr(semicolon_whitespace, substr[idx]))
- goto out;
- /* else */
bb_error_msg_and_die("bad option in substitution expression");
}
}
-
- out:
+out:
/* compile the match string into a regex */
if (*match != '\0') {
/* If match is empty, we use last regex used at runtime */
- sed_cmd->sub_match = (regex_t *) xmalloc(sizeof(regex_t));
+ sed_cmd->sub_match = xmalloc(sizeof(regex_t));
xregcomp(sed_cmd->sub_match, match, cflags);
}
free(match);
@@ -326,855 +406,943 @@ static int parse_subst_cmd(sed_cmd_t * const sed_cmd, const char *substr)
return idx;
}
-static void replace_slash_n(char *string)
-{
- int i;
- int remaining = strlen(string);
-
- for (i = 0; string[i]; i++) {
- if ((string[i] == '\\') && (string[i + 1] == 'n')) {
- string[i] = '\n';
- memmove(string + i + 1, string + i + 1, remaining - 1);
- } else {
- remaining--;
- }
- }
-}
-
-static int parse_translate_cmd(sed_cmd_t * const sed_cmd, const char *cmdstr)
-{
- char *match;
- char *replace;
- int idx;
- int i;
-
- idx = parse_regex_delim(cmdstr, &match, &replace);
- replace_slash_n(match);
- replace_slash_n(replace);
- sed_cmd->translate = xcalloc(1, (strlen(match) + 1) * 2);
- for (i = 0; (match[i] != 0) && (replace[i] != 0); i++) {
- sed_cmd->translate[i * 2] = match[i];
- sed_cmd->translate[(i * 2) + 1] = replace[i];
- }
- return (idx + 1);
-}
-
-static int parse_edit_cmd(sed_cmd_t * sed_cmd, const char *editstr)
-{
- int i, j;
-
- /*
- * the string that gets passed to this function should look like this:
- *
- * need one of these
- * |
- * | this backslash (immediately following the edit command) is mandatory
- * | |
- * [aic]\
- * TEXT1\
- * TEXT2\
- * TEXTN
- *
- * as soon as we hit a TEXT line that has no trailing '\', we're done.
- * this means a command like:
- *
- * i\
- * INSERTME
- *
- * is a-ok.
- *
- */
- if ((*editstr != '\\') || ((editstr[1] != '\n') && (editstr[1] != '\r'))) {
- bb_error_msg_and_die("bad format in edit expression");
- }
-
- /* store the edit line text */
- sed_cmd->editline = xmalloc(strlen(&editstr[2]) + 2);
- for (i = 2, j = 0;
- editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL; i++, j++) {
- if ((editstr[i] == '\\') && strchr("\n\r", editstr[i + 1]) != NULL) {
- sed_cmd->editline[j] = '\n';
- i++;
- } else
- sed_cmd->editline[j] = editstr[i];
- }
-
- /* figure out if we need to add a newline */
- if (sed_cmd->editline[j - 1] != '\n')
- sed_cmd->editline[j++] = '\n';
-
- /* terminate string */
- sed_cmd->editline[j] = '\0';
-
- return i;
-}
-
-
-static int parse_file_cmd(sed_cmd_t * sed_cmd, const char *filecmdstr)
-{
- int idx = 0;
- int filenamelen = 0;
-
- /*
- * the string that gets passed to this function should look like this:
- * '[ ]filename'
- * | |
- * | a filename
- * |
- * optional whitespace
-
- * re: the file to be read, the GNU manual says the following: "Note that
- * if filename cannot be read, it is treated as if it were an empty file,
- * without any error indication." Thus, all of the following commands are
- * perfectly leagal:
- *
- * sed -e '1r noexist'
- * sed -e '1r ;'
- * sed -e '1r'
- */
-
- /* the file command may be followed by whitespace; move past it. */
- while (isspace(filecmdstr[++idx])) {;
- }
-
- /* the first non-whitespace we get is a filename. the filename ends when we
- * hit a normal sed command terminator or end of string */
- filenamelen = strcspn(&filecmdstr[idx], semicolon_whitespace);
- sed_cmd->filename = xmalloc(filenamelen + 1);
- safe_strncpy(sed_cmd->filename, &filecmdstr[idx], filenamelen + 1);
- return idx + filenamelen;
-}
-
/*
* Process the commands arguments
*/
-static char *parse_cmd_str(sed_cmd_t * sed_cmd, char *cmdstr)
+static const char *parse_cmd_args(sed_cmd_t *sed_cmd, const char *cmdstr)
{
/* handle (s)ubstitution command */
- if (sed_cmd->cmd == 's') {
+ if (sed_cmd->cmd == 's')
cmdstr += parse_subst_cmd(sed_cmd, cmdstr);
- }
/* handle edit cmds: (a)ppend, (i)nsert, and (c)hange */
else if (strchr("aic", sed_cmd->cmd)) {
if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
bb_error_msg_and_die
("only a beginning address can be specified for edit commands");
- cmdstr += parse_edit_cmd(sed_cmd, cmdstr);
- }
+ for (;;) {
+ if (*cmdstr == '\n' || *cmdstr == '\\') {
+ cmdstr++;
+ break;
+ } else if (isspace(*cmdstr))
+ cmdstr++;
+ else
+ break;
+ }
+ sed_cmd->string = xstrdup(cmdstr);
+ /* "\anychar" -> "anychar" */
+ parse_escapes(sed_cmd->string, sed_cmd->string, strlen(cmdstr), '\0', '\0');
+ cmdstr += strlen(cmdstr);
/* handle file cmds: (r)ead */
- else if (sed_cmd->cmd == 'r') {
+ } else if (strchr("rw", sed_cmd->cmd)) {
if (sed_cmd->end_line || sed_cmd->end_match)
- bb_error_msg_and_die("Command only uses one address");
- cmdstr += parse_file_cmd(sed_cmd, cmdstr);
- }
+ bb_error_msg_and_die("command only uses one address");
+ cmdstr += parse_file_cmd(/*sed_cmd,*/ cmdstr, &sed_cmd->string);
+ if (sed_cmd->cmd == 'w') {
+ sed_cmd->sw_file = xfopen_for_write(sed_cmd->string);
+ sed_cmd->sw_last_char = '\n';
+ }
/* handle branch commands */
- else if (strchr(":bt", sed_cmd->cmd)) {
+ } else if (strchr(":btT", sed_cmd->cmd)) {
int length;
- cmdstr += strspn(cmdstr, " ");
- length = strcspn(cmdstr, "; \n");
- sed_cmd->label = strndup(cmdstr, length);
- cmdstr += length;
+ cmdstr = skip_whitespace(cmdstr);
+ length = strcspn(cmdstr, semicolon_whitespace);
+ if (length) {
+ sed_cmd->string = xstrndup(cmdstr, length);
+ cmdstr += length;
+ }
}
/* translation command */
else if (sed_cmd->cmd == 'y') {
- cmdstr += parse_translate_cmd(sed_cmd, cmdstr);
+ char *match, *replace;
+ int i = cmdstr[0];
+
+ cmdstr += parse_regex_delim(cmdstr, &match, &replace)+1;
+ /* \n already parsed, but \delimiter needs unescaping. */
+ parse_escapes(match, match, strlen(match), i, i);
+ parse_escapes(replace, replace, strlen(replace), i, i);
+
+ sed_cmd->string = xzalloc((strlen(match) + 1) * 2);
+ for (i = 0; match[i] && replace[i]; i++) {
+ sed_cmd->string[i*2] = match[i];
+ sed_cmd->string[i*2+1] = replace[i];
+ }
+ free(match);
+ free(replace);
}
/* if it wasnt a single-letter command that takes no arguments
* then it must be an invalid command.
*/
- else if (strchr("dgGhHnNpPqx={}", sed_cmd->cmd) == 0) {
- bb_error_msg_and_die("Unsupported command %c", sed_cmd->cmd);
+ else if (strchr("dDgGhHlnNpPqx={}", sed_cmd->cmd) == 0) {
+ bb_error_msg_and_die("unsupported command %c", sed_cmd->cmd);
}
/* give back whatever's left over */
- return (cmdstr);
+ return cmdstr;
}
-static char *add_cmd(sed_cmd_t * sed_cmd, char *cmdstr)
+
+/* Parse address+command sets, skipping comment lines. */
+
+static void add_cmd(const char *cmdstr)
{
- /* Skip over leading whitespace and semicolons */
- cmdstr += strspn(cmdstr, semicolon_whitespace);
+ sed_cmd_t *sed_cmd;
+ int temp;
- /* if we ate the whole thing, that means there was just trailing
- * whitespace or a final / no-op semicolon. either way, get out */
- if (*cmdstr == '\0') {
- return (NULL);
+ /* Append this line to any unfinished line from last time. */
+ if (G.add_cmd_line) {
+ char *tp = xasprintf("%s\n%s", G.add_cmd_line, cmdstr);
+ free(G.add_cmd_line);
+ cmdstr = G.add_cmd_line = tp;
}
- /* if this is a comment, jump past it and keep going */
- if (*cmdstr == '#') {
- /* "#n" is the same as using -n on the command line */
- if (cmdstr[1] == 'n') {
- be_quiet++;
- }
- return (strpbrk(cmdstr, "\n\r"));
+ /* If this line ends with backslash, request next line. */
+ temp = strlen(cmdstr);
+ if (temp && cmdstr[--temp] == '\\') {
+ if (!G.add_cmd_line)
+ G.add_cmd_line = xstrdup(cmdstr);
+ G.add_cmd_line[temp] = '\0';
+ return;
}
- /* parse the command
- * format is: [addr][,addr]cmd
- * |----||-----||-|
- * part1 part2 part3
- */
+ /* Loop parsing all commands in this line. */
+ while (*cmdstr) {
+ /* Skip leading whitespace and semicolons */
+ cmdstr += strspn(cmdstr, semicolon_whitespace);
+
+ /* If no more commands, exit. */
+ if (!*cmdstr) break;
+
+ /* if this is a comment, jump past it and keep going */
+ if (*cmdstr == '#') {
+ /* "#n" is the same as using -n on the command line */
+ if (cmdstr[1] == 'n')
+ G.be_quiet++;
+ cmdstr = strpbrk(cmdstr, "\n\r");
+ if (!cmdstr) break;
+ continue;
+ }
- /* first part (if present) is an address: either a '$', a number or a /regex/ */
- cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
+ /* parse the command
+ * format is: [addr][,addr][!]cmd
+ * |----||-----||-|
+ * part1 part2 part3
+ */
- /* second part (if present) will begin with a comma */
- if (*cmdstr == ',') {
- int idx;
+ sed_cmd = xzalloc(sizeof(sed_cmd_t));
- cmdstr++;
- idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
- if (idx == 0) {
- bb_error_msg_and_die("get_address: no address found in string\n"
- "\t(you probably didn't check the string you passed me)");
- }
- cmdstr += idx;
- }
+ /* first part (if present) is an address: either a '$', a number or a /regex/ */
+ cmdstr += get_address(cmdstr, &sed_cmd->beg_line, &sed_cmd->beg_match);
- /* skip whitespace before the command */
- while (isspace(*cmdstr)) {
- cmdstr++;
- }
-
- /* there my be the inversion flag between part2 and part3 */
- if (*cmdstr == '!') {
- sed_cmd->invert = 1;
- cmdstr++;
+ /* second part (if present) will begin with a comma */
+ if (*cmdstr == ',') {
+ int idx;
-#ifdef SED_FEATURE_STRICT_CHECKING
- /* According to the spec
- * It is unspecified whether <blank>s can follow a '!' character,
- * and conforming applications shall not follow a '!' character
- * with <blank>s.
- */
- if (isblank(cmdstr[idx]) {
- bb_error_msg_and_die("blank follows '!'");}
-#else
- /* skip whitespace before the command */
- while (isspace(*cmdstr)) {
cmdstr++;
+ idx = get_address(cmdstr, &sed_cmd->end_line, &sed_cmd->end_match);
+ if (!idx)
+ bb_error_msg_and_die("no address after comma");
+ cmdstr += idx;
}
-#endif
- }
- /* last part (mandatory) will be a command */
- if (*cmdstr == '\0')
- bb_error_msg_and_die("missing command");
-
- sed_cmd->cmd = *cmdstr;
- cmdstr++;
+ /* skip whitespace before the command */
+ cmdstr = skip_whitespace(cmdstr);
- cmdstr = parse_cmd_str(sed_cmd, cmdstr);
+ /* Check for inversion flag */
+ if (*cmdstr == '!') {
+ sed_cmd->invert = 1;
+ cmdstr++;
- /* Add the command to the command array */
- sed_cmd_tail->next = sed_cmd;
- sed_cmd_tail = sed_cmd_tail->next;
+ /* skip whitespace before the command */
+ cmdstr = skip_whitespace(cmdstr);
+ }
- return (cmdstr);
-}
+ /* last part (mandatory) will be a command */
+ if (!*cmdstr)
+ bb_error_msg_and_die("missing command");
+ sed_cmd->cmd = *(cmdstr++);
+ cmdstr = parse_cmd_args(sed_cmd, cmdstr);
-static void add_cmd_str(char *cmdstr)
-{
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
- char *cmdstr_ptr = cmdstr;
-
- /* HACK: convert "\n" to match tranlated '\n' string */
- while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
- cmdstr = xrealloc(cmdstr, strlen(cmdstr) + 2);
- cmdstr_ptr = strstr(cmdstr, "\\n");
- memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
- cmdstr_ptr[0] = '\\';
- cmdstr_ptr += 3;
+ /* Add the command to the command array */
+ G.sed_cmd_tail->next = sed_cmd;
+ G.sed_cmd_tail = G.sed_cmd_tail->next;
}
-#endif
- do {
- sed_cmd_t *sed_cmd;
-
- sed_cmd = xcalloc(1, sizeof(sed_cmd_t));
- cmdstr = add_cmd(sed_cmd, cmdstr);
- } while (cmdstr && strlen(cmdstr));
-}
-
-static void load_cmd_file(char *filename)
-{
- FILE *cmdfile;
- char *line;
- char *nextline;
- char *e;
-
- cmdfile = bb_xfopen(filename, "r");
-
- while ((line = bb_get_line_from_file(cmdfile)) != NULL) {
- /* if a line ends with '\' it needs the next line appended to it */
- while (((e = last_char_is(line, '\n')) != NULL)
- && (e > line) && (e[-1] == '\\')
- && ((nextline = bb_get_line_from_file(cmdfile)) != NULL)) {
- line = xrealloc(line, (e - line) + 1 + strlen(nextline) + 1);
- strcat(line, nextline);
- free(nextline);
- }
- /* eat trailing newline (if any) --if I don't do this, edit commands
- * (aic) will print an extra newline */
- chomp(line);
- add_cmd_str(line);
- free(line);
- }
+ /* If we glued multiple lines together, free the memory. */
+ free(G.add_cmd_line);
+ G.add_cmd_line = NULL;
}
-struct pipeline {
- char *buf;
- int idx;
- int len;
-};
+/* Append to a string, reallocating memory as necessary. */
-#define PIPE_MAGIC 0x7f
#define PIPE_GROW 64
-void pipe_putc(struct pipeline *const pipeline, char c)
+static void pipe_putc(char c)
{
- if (pipeline->buf[pipeline->idx] == PIPE_MAGIC) {
- pipeline->buf = xrealloc(pipeline->buf, pipeline->len + PIPE_GROW);
- memset(pipeline->buf + pipeline->len, 0, PIPE_GROW);
- pipeline->len += PIPE_GROW;
- pipeline->buf[pipeline->len - 1] = PIPE_MAGIC;
+ if (G.pipeline.idx == G.pipeline.len) {
+ G.pipeline.buf = xrealloc(G.pipeline.buf,
+ G.pipeline.len + PIPE_GROW);
+ G.pipeline.len += PIPE_GROW;
}
- pipeline->buf[pipeline->idx++] = (c);
+ G.pipeline.buf[G.pipeline.idx++] = c;
}
-#define pipeputc(c) pipe_putc(pipeline, c)
-
-#if 0
-{
- if (pipeline[pipeline_idx] == PIPE_MAGIC) {
- pipeline = xrealloc(pipeline, pipeline_len + PIPE_GROW);
- memset(pipeline + pipeline_len, 0, PIPE_GROW);
- pipeline_len += PIPE_GROW;
- pipeline[pipeline_len - 1] = PIPE_MAGIC;
- }
- pipeline[pipeline_idx++] = (c);
-}
-#endif
-
-static void print_subst_w_backrefs(const char *line, const char *replace,
- regmatch_t * regmatch, struct pipeline *const pipeline, int matches)
+static void do_subst_w_backrefs(char *line, char *replace)
{
- int i;
+ int i,j;
/* go through the replacement string */
for (i = 0; replace[i]; i++) {
/* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
- if (replace[i] == '\\' && isdigit(replace[i + 1])) {
- int j;
- char tmpstr[2];
- int backref;
-
- ++i; /* i now indexes the backref number, instead of the leading slash */
- tmpstr[0] = replace[i];
- tmpstr[1] = 0;
- backref = atoi(tmpstr);
- /* print out the text held in regmatch[backref] */
- if (backref <= matches && regmatch[backref].rm_so != -1)
- for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo;
- j++)
- pipeputc(line[j]);
- }
-
- /* if we find a backslash escaped character, print the character */
- else if (replace[i] == '\\') {
- ++i;
- pipeputc(replace[i]);
+ if (replace[i] == '\\') {
+ unsigned backref = replace[++i] - '0';
+ if (backref <= 9) {
+ /* print out the text held in G.regmatch[backref] */
+ if (G.regmatch[backref].rm_so != -1) {
+ j = G.regmatch[backref].rm_so;
+ while (j < G.regmatch[backref].rm_eo)
+ pipe_putc(line[j++]);
+ }
+ continue;
+ }
+ /* I _think_ it is impossible to get '\' to be
+ * the last char in replace string. Thus we dont check
+ * for replace[i] == NUL. (counterexample anyone?) */
+ /* if we find a backslash escaped character, print the character */
+ pipe_putc(replace[i]);
+ continue;
}
-
- /* if we find an unescaped '&' print out the whole matched text.
- * fortunately, regmatch[0] contains the indicies to the whole matched
- * expression (kinda seems like it was designed for just such a
- * purpose...) */
- else if (replace[i] == '&') {
- int j;
-
- for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
- pipeputc(line[j]);
+ /* if we find an unescaped '&' print out the whole matched text. */
+ if (replace[i] == '&') {
+ j = G.regmatch[0].rm_so;
+ while (j < G.regmatch[0].rm_eo)
+ pipe_putc(line[j++]);
+ continue;
}
- /* nothing special, just print this char of the replacement string to stdout */
- else
- pipeputc(replace[i]);
+ /* Otherwise just output the character. */
+ pipe_putc(replace[i]);
}
}
-static int do_subst_command(sed_cmd_t * sed_cmd, char **line)
+static int do_subst_command(sed_cmd_t *sed_cmd, char **line)
{
- char *hackline = *line;
- struct pipeline thepipe = { NULL, 0, 0 };
- struct pipeline *const pipeline = &thepipe;
+ char *oldline = *line;
int altered = 0;
- int result;
- regmatch_t *regmatch = NULL;
+ unsigned match_count = 0;
regex_t *current_regex;
+ /* Handle empty regex. */
if (sed_cmd->sub_match == NULL) {
- current_regex = previous_regex_ptr;
- } else {
- previous_regex_ptr = current_regex = sed_cmd->sub_match;
- }
- result = regexec(current_regex, hackline, 0, NULL, 0);
-
- /* we only proceed if the substitution 'search' expression matches */
- if (result == REG_NOMATCH) {
+ current_regex = G.previous_regex_ptr;
+ if (!current_regex)
+ bb_error_msg_and_die("no previous regexp");
+ } else
+ G.previous_regex_ptr = current_regex = sed_cmd->sub_match;
+
+ /* Find the first match */
+ if (REG_NOMATCH == regexec(current_regex, oldline, 10, G.regmatch, 0))
return 0;
- }
- /* whaddaya know, it matched. get the number of back references */
- regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs + 1));
-
- /* allocate more PIPE_GROW bytes
- if replaced string is larger than original */
- thepipe.len = strlen(hackline) + PIPE_GROW;
- thepipe.buf = xcalloc(1, thepipe.len);
- /* buffer magic */
- thepipe.buf[thepipe.len - 1] = PIPE_MAGIC;
-
- /* and now, as long as we've got a line to try matching and if we can match
- * the search string, we make substitutions */
- while ((*hackline || !altered)
- && (regexec(current_regex, hackline, sed_cmd->num_backrefs + 1,
- regmatch, 0) != REG_NOMATCH)) {
+ /* Initialize temporary output buffer. */
+ G.pipeline.buf = xmalloc(PIPE_GROW);
+ G.pipeline.len = PIPE_GROW;
+ G.pipeline.idx = 0;
+
+ /* Now loop through, substituting for matches */
+ do {
int i;
+ /* Work around bug in glibc regexec, demonstrated by:
+ echo " a.b" | busybox sed 's [^ .]* x g'
+ The match_count check is so not to break
+ echo "hi" | busybox sed 's/^/!/g' */
+ if (!G.regmatch[0].rm_so && !G.regmatch[0].rm_eo && match_count) {
+ pipe_putc(*oldline++);
+ continue;
+ }
+
+ match_count++;
+
+ /* If we aren't interested in this match, output old line to
+ end of match and continue */
+ if (sed_cmd->which_match
+ && (sed_cmd->which_match != match_count)
+ ) {
+ for (i = 0; i < G.regmatch[0].rm_eo; i++)
+ pipe_putc(*oldline++);
+ continue;
+ }
+
/* print everything before the match */
- for (i = 0; i < regmatch[0].rm_so; i++)
- pipeputc(hackline[i]);
+ for (i = 0; i < G.regmatch[0].rm_so; i++)
+ pipe_putc(oldline[i]);
/* then print the substitution string */
- print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch, pipeline,
- sed_cmd->num_backrefs);
+ do_subst_w_backrefs(oldline, sed_cmd->string);
/* advance past the match */
- hackline += regmatch[0].rm_eo;
+ oldline += G.regmatch[0].rm_eo;
/* flag that something has changed */
altered++;
/* if we're not doing this globally, get out now */
- if (!sed_cmd->sub_g) {
+ if (sed_cmd->which_match)
break;
- }
- }
- for (; *hackline; hackline++)
- pipeputc(*hackline);
- if (thepipe.buf[thepipe.idx] == PIPE_MAGIC)
- thepipe.buf[thepipe.idx] = 0;
+ } while (*oldline && (regexec(current_regex, oldline, 10, G.regmatch, 0) != REG_NOMATCH));
+
+ /* Copy rest of string into output pipeline */
- /* cleanup */
- free(regmatch);
+ while (*oldline)
+ pipe_putc(*oldline++);
+ pipe_putc(0);
free(*line);
- *line = thepipe.buf;
+ *line = G.pipeline.buf;
return altered;
}
-static sed_cmd_t *branch_to(const char *label)
+/* Set command pointer to point to this label. (Does not handle null label.) */
+static sed_cmd_t *branch_to(char *label)
{
sed_cmd_t *sed_cmd;
- for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
- if ((sed_cmd->label) && (strcmp(sed_cmd->label, label) == 0)) {
- break;
+ for (sed_cmd = G.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
+ if (sed_cmd->cmd == ':' && sed_cmd->string && !strcmp(sed_cmd->string, label)) {
+ return sed_cmd;
}
}
+ bb_error_msg_and_die("can't find label for jump to '%s'", label);
+}
- /* If no match returns last command */
- return (sed_cmd);
+static void append(char *s)
+{
+ llist_add_to_end(&G.append_head, xstrdup(s));
}
-static void process_file(FILE * file)
+static void flush_append(void)
{
- char *pattern_space; /* Posix requires it be able to hold at least 8192 bytes */
- char *hold_space = NULL; /* Posix requires it be able to hold at least 8192 bytes */
- static int linenum = 0; /* GNU sed does not restart counting lines at EOF */
- unsigned int still_in_range = 0;
- int altered;
- int force_print;
-
- pattern_space = bb_get_chomped_line_from_file(file);
- if (pattern_space == NULL) {
- return;
- }
-
- /* go through every line in the file */
- do {
- char *next_line;
- sed_cmd_t *sed_cmd;
- int substituted = 0;
- /* This enables whole blocks of commands to be mask'ed out if the lead address doesnt match */
- int block_mask = 1;
-
- /* Read one line in advance so we can act on the last line, the '$' address */
- next_line = bb_get_chomped_line_from_file(file);
-
- linenum++;
- altered = 0;
- force_print = 0;
-
- /* for every line, go through all the commands */
- for (sed_cmd = sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
- int deleted = 0;
-
- /*
- * entry point into sedding...
- */
- int matched = (
- /* no range necessary */
- (sed_cmd->beg_line == 0 && sed_cmd->end_line == 0
- && sed_cmd->beg_match == NULL
- && sed_cmd->end_match == NULL) ||
- /* this line number is the first address we're looking for */
- (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum)) ||
- /* this line matches our first address regex */
- (sed_cmd->beg_match
- && (regexec(sed_cmd->beg_match, pattern_space, 0, NULL,
- 0) == 0)) ||
- /* we are currently within the beginning & ending address range */
- still_in_range || ((sed_cmd->beg_line == -1)
- && (next_line == NULL))
- );
-
- if (sed_cmd->cmd == '{') {
- block_mask = block_mask & matched;
- }
-// matched &= block_mask;
+ char *data;
- if (sed_cmd->invert ^ (matched & block_mask)) {
- /* Update last used regex incase a blank substitute BRE is found */
- if (sed_cmd->beg_match) {
- previous_regex_ptr = sed_cmd->beg_match;
- }
+ /* Output appended lines. */
+ while ((data = (char *)llist_pop(&G.append_head))) {
+ fprintf(G.nonstdout, "%s\n", data);
+ free(data);
+ }
+}
- /*
- * actual sedding
- */
- switch (sed_cmd->cmd) {
- case '=':
- printf("%d\n", linenum);
- break;
- case 'P':{
- /* Write the current pattern space upto the first newline */
- char *tmp = strchr(pattern_space, '\n');
+static void add_input_file(FILE *file)
+{
+ G.input_file_list = xrealloc_vector(G.input_file_list, 2, G.input_file_count);
+ G.input_file_list[G.input_file_count++] = file;
+}
- if (tmp) {
- *tmp = '\0';
- }
+/* Get next line of input from G.input_file_list, flushing append buffer and
+ * noting if we ran out of files without a newline on the last line we read.
+ */
+enum {
+ NO_EOL_CHAR = 1,
+ LAST_IS_NUL = 2,
+};
+static char *get_next_line(char *gets_char)
+{
+ char *temp = NULL;
+ int len;
+ char gc;
+
+ flush_append();
+
+ /* will be returned if last line in the file
+ * doesn't end with either '\n' or '\0' */
+ gc = NO_EOL_CHAR;
+ while (G.current_input_file < G.input_file_count) {
+ FILE *fp = G.input_file_list[G.current_input_file];
+ /* Read line up to a newline or NUL byte, inclusive,
+ * return malloc'ed char[]. length of the chunk read
+ * is stored in len. NULL if EOF/error */
+ temp = bb_get_chunk_from_file(fp, &len);
+ if (temp) {
+ /* len > 0 here, it's ok to do temp[len-1] */
+ char c = temp[len-1];
+ if (c == '\n' || c == '\0') {
+ temp[len-1] = '\0';
+ gc = c;
+ if (c == '\0') {
+ int ch = fgetc(fp);
+ if (ch != EOF)
+ ungetc(ch, fp);
+ else
+ gc = LAST_IS_NUL;
}
- case 'p': /* Write the current pattern space to output */
- puts(pattern_space);
- break;
- case 'd':
- altered++;
- deleted = 1;
- break;
+ }
+ /* else we put NO_EOL_CHAR into *gets_char */
+ break;
- case 's':
-
- /*
- * Some special cases for 's' printing to make it compliant with
- * GNU sed printing behavior (aka "The -n | s///p Matrix"):
- *
- * -n ONLY = never print anything regardless of any successful
- * substitution
- *
- * s///p ONLY = always print successful substitutions, even if
- * the pattern_space is going to be printed anyway (pattern_space
- * will be printed twice).
- *
- * -n AND s///p = print ONLY a successful substitution ONE TIME;
- * no other lines are printed - this is the reason why the 'p'
- * flag exists in the first place.
- */
-
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
- /* HACK: escape newlines twice so regex can match them */
- {
- int offset = 0;
-
- while (strchr(pattern_space + offset, '\n') != NULL) {
- char *tmp;
-
- pattern_space =
- xrealloc(pattern_space,
- strlen(pattern_space) + 2);
- tmp = strchr(pattern_space + offset, '\n');
- memmove(tmp + 1, tmp, strlen(tmp) + 1);
- tmp[0] = '\\';
- tmp[1] = 'n';
- offset = tmp - pattern_space + 2;
- }
- }
-#endif
- /* we print the pattern_space once, unless we were told to be quiet */
- substituted = do_subst_command(sed_cmd, &pattern_space);
+ /* NB: I had the idea of peeking next file(s) and returning
+ * NO_EOL_CHAR only if it is the *last* non-empty
+ * input file. But there is a case where this won't work:
+ * file1: "a woo\nb woo"
+ * file2: "c no\nd no"
+ * sed -ne 's/woo/bang/p' input1 input2 => "a bang\nb bang"
+ * (note: *no* newline after "b bang"!) */
+ }
+ /* Close this file and advance to next one */
+ fclose(fp);
+ G.current_input_file++;
+ }
+ *gets_char = gc;
+ return temp;
+}
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
- /* undo HACK: escape newlines twice so regex can match them */
- {
- char *tmp = pattern_space;
+/* Output line of text. */
+/* Note:
+ * The tricks with NO_EOL_CHAR and last_puts_char are there to emulate gnu sed.
+ * Without them, we had this:
+ * echo -n thingy >z1
+ * echo -n again >z2
+ * >znull
+ * sed "s/i/z/" z1 z2 znull | hexdump -vC
+ * output:
+ * gnu sed 4.1.5:
+ * 00000000 74 68 7a 6e 67 79 0a 61 67 61 7a 6e |thzngy.agazn|
+ * bbox:
+ * 00000000 74 68 7a 6e 67 79 61 67 61 7a 6e |thzngyagazn|
+ */
+static void puts_maybe_newline(char *s, FILE *file, char *last_puts_char, char last_gets_char)
+{
+ char lpc = *last_puts_char;
- while ((tmp = strstr(tmp, "\\n")) != NULL) {
- memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
- tmp[0] = '\n';
- }
- }
-#endif
- altered |= substituted;
- if (!be_quiet && altered && ((sed_cmd->next == NULL)
- || (sed_cmd->next->cmd != 's'))) {
- force_print = 1;
- }
+ /* Need to insert a '\n' between two files because first file's
+ * last line wasn't terminated? */
+ if (lpc != '\n' && lpc != '\0') {
+ fputc('\n', file);
+ lpc = '\n';
+ }
+ fputs(s, file);
+
+ /* 'x' - just something which is not '\n', '\0' or NO_EOL_CHAR */
+ if (s[0])
+ lpc = 'x';
+
+ /* had trailing '\0' and it was last char of file? */
+ if (last_gets_char == LAST_IS_NUL) {
+ fputc('\0', file);
+ lpc = 'x'; /* */
+ } else
+ /* had trailing '\n' or '\0'? */
+ if (last_gets_char != NO_EOL_CHAR) {
+ fputc(last_gets_char, file);
+ lpc = last_gets_char;
+ }
- /* we also print the line if we were given the 'p' flag
- * (this is quite possibly the second printing) */
- if ((sed_cmd->sub_p) && altered) {
- puts(pattern_space);
- }
- break;
- case 'a':
- puts(pattern_space);
- fputs(sed_cmd->editline, stdout);
- altered++;
- break;
+ if (ferror(file)) {
+ xfunc_error_retval = 4; /* It's what gnu sed exits with... */
+ bb_error_msg_and_die(bb_msg_write_error);
+ }
+ *last_puts_char = lpc;
+}
- case 'i':
- fputs(sed_cmd->editline, stdout);
- break;
+#define sed_puts(s, n) (puts_maybe_newline(s, G.nonstdout, &last_puts_char, n))
- case 'c':
- /* single-address case */
- if ((sed_cmd->end_match == NULL && sed_cmd->end_line == 0)
- /* multi-address case */
- /* - matching text */
- || (sed_cmd->end_match
- && (regexec(sed_cmd->end_match, pattern_space, 0,
- NULL, 0) == 0))
- /* - matching line numbers */
- || (sed_cmd->end_line > 0
- && sed_cmd->end_line == linenum)) {
- fputs(sed_cmd->editline, stdout);
- }
- altered++;
+static int beg_match(sed_cmd_t *sed_cmd, const char *pattern_space)
+{
+ int retval = sed_cmd->beg_match && !regexec(sed_cmd->beg_match, pattern_space, 0, NULL, 0);
+ if (retval)
+ G.previous_regex_ptr = sed_cmd->beg_match;
+ return retval;
+}
- break;
+/* Process all the lines in all the files */
- case 'r':{
- FILE *outfile;
-
- outfile = fopen(sed_cmd->filename, "r");
- if (outfile) {
- char *line;
-
- while ((line =
- bb_get_chomped_line_from_file(outfile)) !=
- NULL) {
- pattern_space =
- xrealloc(pattern_space,
- strlen(line) + strlen(pattern_space) + 2);
- strcat(pattern_space, "\n");
- strcat(pattern_space, line);
- }
- bb_xprint_and_close_file(outfile);
- }
+static void process_files(void)
+{
+ char *pattern_space, *next_line;
+ int linenum = 0;
+ char last_puts_char = '\n';
+ char last_gets_char, next_gets_char;
+ sed_cmd_t *sed_cmd;
+ int substituted;
+
+ /* Prime the pump */
+ next_line = get_next_line(&next_gets_char);
+
+ /* go through every line in each file */
+ again:
+ substituted = 0;
+
+ /* Advance to next line. Stop if out of lines. */
+ pattern_space = next_line;
+ if (!pattern_space) return;
+ last_gets_char = next_gets_char;
+
+ /* Read one line in advance so we can act on the last line,
+ * the '$' address */
+ next_line = get_next_line(&next_gets_char);
+ linenum++;
+ restart:
+ /* for every line, go through all the commands */
+ for (sed_cmd = G.sed_cmd_head.next; sed_cmd; sed_cmd = sed_cmd->next) {
+ int old_matched, matched;
+
+ old_matched = sed_cmd->in_match;
+
+ /* Determine if this command matches this line: */
+
+ /* Are we continuing a previous multi-line match? */
+ sed_cmd->in_match = sed_cmd->in_match
+ /* Or is no range necessary? */
+ || (!sed_cmd->beg_line && !sed_cmd->end_line
+ && !sed_cmd->beg_match && !sed_cmd->end_match)
+ /* Or did we match the start of a numerical range? */
+ || (sed_cmd->beg_line > 0 && (sed_cmd->beg_line == linenum))
+ /* Or does this line match our begin address regex? */
+ || (beg_match(sed_cmd, pattern_space))
+ /* Or did we match last line of input? */
+ || (sed_cmd->beg_line == -1 && next_line == NULL);
+
+ /* Snapshot the value */
+
+ matched = sed_cmd->in_match;
+
+ /* Is this line the end of the current match? */
+
+ if (matched) {
+ sed_cmd->in_match = !(
+ /* has the ending line come, or is this a single address command? */
+ (sed_cmd->end_line ?
+ sed_cmd->end_line == -1 ?
+ !next_line
+ : (sed_cmd->end_line <= linenum)
+ : !sed_cmd->end_match
+ )
+ /* or does this line matches our last address regex */
+ || (sed_cmd->end_match && old_matched
+ && (regexec(sed_cmd->end_match,
+ pattern_space, 0, NULL, 0) == 0))
+ );
+ }
+ /* Skip blocks of commands we didn't match. */
+ if (sed_cmd->cmd == '{') {
+ if (sed_cmd->invert ? matched : !matched) {
+ while (sed_cmd->cmd != '}') {
+ sed_cmd = sed_cmd->next;
+ if (!sed_cmd)
+ bb_error_msg_and_die("unterminated {");
}
- break;
- case 'q': /* Branch to end of script and quit */
- deleted = 1;
- /* Exit the outer while loop */
- free(next_line);
- next_line = NULL;
- break;
- case 'n': /* Read next line from input */
- free(pattern_space);
- pattern_space = next_line;
- next_line = bb_get_chomped_line_from_file(file);
- linenum++;
- break;
- case 'N': /* Append the next line to the current line */
- if (next_line) {
- pattern_space =
- realloc(pattern_space,
- strlen(pattern_space) + strlen(next_line) + 2);
- strcat(pattern_space, "\n");
- strcat(pattern_space, next_line);
- next_line = bb_get_chomped_line_from_file(file);
- linenum++;
- }
- break;
- case 'b':
- sed_cmd = branch_to(sed_cmd->label);
- break;
- case 't':
- if (substituted) {
- sed_cmd = branch_to(sed_cmd->label);
- }
- break;
- case 'y':{
- int i;
+ }
+ continue;
+ }
+
+ /* Okay, so did this line match? */
+ if (sed_cmd->invert ? !matched : matched) {
+ /* Update last used regex in case a blank substitute BRE is found */
+ if (sed_cmd->beg_match) {
+ G.previous_regex_ptr = sed_cmd->beg_match;
+ }
- for (i = 0; pattern_space[i] != 0; i++) {
- int j;
+ /* actual sedding */
+ switch (sed_cmd->cmd) {
- for (j = 0; sed_cmd->translate[j]; j += 2) {
- if (pattern_space[i] == sed_cmd->translate[j]) {
- pattern_space[i] = sed_cmd->translate[j + 1];
- }
- }
- }
- }
+ /* Print line number */
+ case '=':
+ fprintf(G.nonstdout, "%d\n", linenum);
+ break;
+
+ /* Write the current pattern space up to the first newline */
+ case 'P':
+ {
+ char *tmp = strchr(pattern_space, '\n');
+
+ if (tmp) {
+ *tmp = '\0';
+ /* TODO: explain why '\n' below */
+ sed_puts(pattern_space, '\n');
+ *tmp = '\n';
break;
- case 'g': /* Replace pattern space with hold space */
+ }
+ /* Fall Through */
+ }
+
+ /* Write the current pattern space to output */
+ case 'p':
+ /* NB: we print this _before_ the last line
+ * (of current file) is printed. Even if
+ * that line is nonterminated, we print
+ * '\n' here (gnu sed does the same) */
+ sed_puts(pattern_space, '\n');
+ break;
+ /* Delete up through first newline */
+ case 'D':
+ {
+ char *tmp = strchr(pattern_space, '\n');
+
+ if (tmp) {
+ tmp = xstrdup(tmp+1);
free(pattern_space);
- pattern_space = strdup(hold_space);
- break;
- case 'G': { /* Append newline and hold space to pattern space */
- int pattern_space_size = 0;
- if (pattern_space) {
- pattern_space_size = strlen(pattern_space);
- }
- pattern_space = xrealloc(pattern_space, pattern_space_size + strlen(hold_space) + 2);
- strcat(pattern_space, "\n");
- strcat(pattern_space, hold_space);
- break;
+ pattern_space = tmp;
+ goto restart;
}
- case 'h': /* Replace hold space with pattern space */
- free(hold_space);
- hold_space = strdup(pattern_space);
- break;
- case 'H': { /* Append newline and pattern space to hold space */
- int hold_space_size = 0;
- if (hold_space) {
- hold_space_size = strlen(hold_space);
- }
- hold_space = xrealloc(hold_space, hold_space_size + strlen(pattern_space) + 2);
- strcat(hold_space, "\n");
- strcat(hold_space, pattern_space);
+ }
+ /* discard this line. */
+ case 'd':
+ goto discard_line;
+
+ /* Substitute with regex */
+ case 's':
+ if (!do_subst_command(sed_cmd, &pattern_space))
break;
+ substituted |= 1;
+
+ /* handle p option */
+ if (sed_cmd->sub_p)
+ sed_puts(pattern_space, last_gets_char);
+ /* handle w option */
+ if (sed_cmd->sw_file)
+ puts_maybe_newline(
+ pattern_space, sed_cmd->sw_file,
+ &sed_cmd->sw_last_char, last_gets_char);
+ break;
+
+ /* Append line to linked list to be printed later */
+ case 'a':
+ append(sed_cmd->string);
+ break;
+
+ /* Insert text before this line */
+ case 'i':
+ sed_puts(sed_cmd->string, '\n');
+ break;
+
+ /* Cut and paste text (replace) */
+ case 'c':
+ /* Only triggers on last line of a matching range. */
+ if (!sed_cmd->in_match)
+ sed_puts(sed_cmd->string, NO_EOL_CHAR);
+ goto discard_line;
+
+ /* Read file, append contents to output */
+ case 'r':
+ {
+ FILE *rfile;
+
+ rfile = fopen_for_read(sed_cmd->string);
+ if (rfile) {
+ char *line;
+
+ while ((line = xmalloc_fgetline(rfile))
+ != NULL)
+ append(line);
+ xprint_and_close_file(rfile);
}
- case 'x':{
- /* Swap hold and pattern space */
- char *tmp = pattern_space;
- pattern_space = hold_space;
- hold_space = tmp;
+
+ break;
+ }
+
+ /* Write pattern space to file. */
+ case 'w':
+ puts_maybe_newline(
+ pattern_space, sed_cmd->sw_file,
+ &sed_cmd->sw_last_char, last_gets_char);
+ break;
+
+ /* Read next line from input */
+ case 'n':
+ if (!G.be_quiet)
+ sed_puts(pattern_space, last_gets_char);
+ if (next_line) {
+ free(pattern_space);
+ pattern_space = next_line;
+ last_gets_char = next_gets_char;
+ next_line = get_next_line(&next_gets_char);
+ substituted = 0;
+ linenum++;
break;
}
+ /* fall through */
+
+ /* Quit. End of script, end of input. */
+ case 'q':
+ /* Exit the outer while loop */
+ free(next_line);
+ next_line = NULL;
+ goto discard_commands;
+
+ /* Append the next line to the current line */
+ case 'N':
+ {
+ int len;
+ /* If no next line, jump to end of script and exit. */
+ if (next_line == NULL) {
+ /* Jump to end of script and exit */
+ free(next_line);
+ next_line = NULL;
+ goto discard_line;
+ /* append next_line, read new next_line. */
}
+ len = strlen(pattern_space);
+ pattern_space = realloc(pattern_space, len + strlen(next_line) + 2);
+ pattern_space[len] = '\n';
+ strcpy(pattern_space + len+1, next_line);
+ last_gets_char = next_gets_char;
+ next_line = get_next_line(&next_gets_char);
+ linenum++;
+ break;
}
- /*
- * exit point from sedding...
- */
- if (matched) {
- if (
- /* this is a single-address command or... */
- (sed_cmd->end_line == 0 && sed_cmd->end_match == NULL)
- /* If only one address */
- /* we were in the middle of our address range (this
- * isn't the first time through) and.. */
- || ((still_in_range == 1)
- /* this line number is the last address we're looking for or... */
- && ((sed_cmd->end_line > 0
- && (sed_cmd->end_line == linenum))
- /* this line matches our last address regex */
- || (sed_cmd->end_match
- && (regexec(sed_cmd->end_match, pattern_space,
- 0, NULL, 0) == 0))))) {
- /* we're out of our address range */
- still_in_range = 0;
- } else {
- /* didn't hit the exit? then we're still in the middle of an address range */
- still_in_range = 1;
+ /* Test/branch if substitution occurred */
+ case 't':
+ if (!substituted) break;
+ substituted = 0;
+ /* Fall through */
+ /* Test/branch if substitution didn't occur */
+ case 'T':
+ if (substituted) break;
+ /* Fall through */
+ /* Branch to label */
+ case 'b':
+ if (!sed_cmd->string) goto discard_commands;
+ else sed_cmd = branch_to(sed_cmd->string);
+ break;
+ /* Transliterate characters */
+ case 'y':
+ {
+ int i, j;
+
+ for (i = 0; pattern_space[i]; i++) {
+ for (j = 0; sed_cmd->string[j]; j += 2) {
+ if (pattern_space[i] == sed_cmd->string[j]) {
+ pattern_space[i] = sed_cmd->string[j + 1];
+ break;
+ }
+ }
}
+
+ break;
}
+ case 'g': /* Replace pattern space with hold space */
+ free(pattern_space);
+ pattern_space = xstrdup(G.hold_space ? G.hold_space : "");
+ break;
+ case 'G': /* Append newline and hold space to pattern space */
+ {
+ int pattern_space_size = 2;
+ int hold_space_size = 0;
+
+ if (pattern_space)
+ pattern_space_size += strlen(pattern_space);
+ if (G.hold_space)
+ hold_space_size = strlen(G.hold_space);
+ pattern_space = xrealloc(pattern_space,
+ pattern_space_size + hold_space_size);
+ if (pattern_space_size == 2)
+ pattern_space[0] = 0;
+ strcat(pattern_space, "\n");
+ if (G.hold_space)
+ strcat(pattern_space, G.hold_space);
+ last_gets_char = '\n';
- if (sed_cmd->cmd == '}') {
- block_mask = 1;
+ break;
}
+ case 'h': /* Replace hold space with pattern space */
+ free(G.hold_space);
+ G.hold_space = xstrdup(pattern_space);
+ break;
+ case 'H': /* Append newline and pattern space to hold space */
+ {
+ int hold_space_size = 2;
+ int pattern_space_size = 0;
+
+ if (G.hold_space)
+ hold_space_size += strlen(G.hold_space);
+ if (pattern_space)
+ pattern_space_size = strlen(pattern_space);
+ G.hold_space = xrealloc(G.hold_space,
+ hold_space_size + pattern_space_size);
+
+ if (hold_space_size == 2)
+ *G.hold_space = 0;
+ strcat(G.hold_space, "\n");
+ if (pattern_space)
+ strcat(G.hold_space, pattern_space);
- if (deleted)
break;
+ }
+ case 'x': /* Exchange hold and pattern space */
+ {
+ char *tmp = pattern_space;
+ pattern_space = G.hold_space ? : xzalloc(1);
+ last_gets_char = '\n';
+ G.hold_space = tmp;
+ break;
+ }
+ }
}
+ }
- /* we will print the line unless we were told to be quiet or if the
- * line was altered (via a 'd'elete or 's'ubstitution), in which case
- * the altered line was already printed */
- if ((!be_quiet && !altered) || force_print) {
- puts(pattern_space);
+ /*
+ * exit point from sedding...
+ */
+ discard_commands:
+ /* we will print the line unless we were told to be quiet ('-n')
+ or if the line was suppressed (ala 'd'elete) */
+ if (!G.be_quiet)
+ sed_puts(pattern_space, last_gets_char);
+
+ /* Delete and such jump here. */
+ discard_line:
+ flush_append();
+ free(pattern_space);
+
+ goto again;
+}
+
+/* It is possible to have a command line argument with embedded
+ * newlines. This counts as multiple command lines.
+ * However, newline can be escaped: 's/e/z\<newline>z/'
+ * We check for this.
+ */
+
+static void add_cmd_block(char *cmdstr)
+{
+ char *sv, *eol;
+
+ cmdstr = sv = xstrdup(cmdstr);
+ do {
+ eol = strchr(cmdstr, '\n');
+ next:
+ if (eol) {
+ /* Count preceding slashes */
+ int slashes = 0;
+ char *sl = eol;
+
+ while (sl != cmdstr && *--sl == '\\')
+ slashes++;
+ /* Odd number of preceding slashes - newline is escaped */
+ if (slashes & 1) {
+ overlapping_strcpy(eol - 1, eol);
+ eol = strchr(eol, '\n');
+ goto next;
+ }
+ *eol = '\0';
}
- free(pattern_space);
- pattern_space = next_line;
- } while (pattern_space);
+ add_cmd(cmdstr);
+ cmdstr = eol + 1;
+ } while (eol);
+ free(sv);
}
-extern int sed_main(int argc, char **argv)
+int sed_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int sed_main(int argc UNUSED_PARAM, char **argv)
{
- int opt, status = EXIT_SUCCESS;
+ enum {
+ OPT_in_place = 1 << 0,
+ };
+ unsigned opt;
+ llist_t *opt_e, *opt_f;
+ int status = EXIT_SUCCESS;
+
+ INIT_G();
-#ifdef CONFIG_FEATURE_CLEAN_UP
/* destroy command strings on exit */
- if (atexit(destroy_cmd_strs) == -1)
- bb_perror_msg_and_die("atexit");
-#endif
+ if (ENABLE_FEATURE_CLEAN_UP) atexit(sed_free_and_close_stuff);
- /* do normal option parsing */
- while ((opt = getopt(argc, argv, "ne:f:")) > 0) {
- switch (opt) {
- case 'n':
- be_quiet++;
- break;
- case 'e':{
- char *str_cmd = strdup(optarg);
+ /* Lie to autoconf when it starts asking stupid questions. */
+ if (argv[1] && !strcmp(argv[1], "--version")) {
+ puts("This is not GNU sed version 4.0");
+ return 0;
+ }
- add_cmd_str(str_cmd);
- free(str_cmd);
- break;
- }
- case 'f':
- load_cmd_file(optarg);
- break;
- default:
- bb_show_usage();
+ /* do normal option parsing */
+ opt_e = opt_f = NULL;
+ opt_complementary = "e::f::" /* can occur multiple times */
+ "nn"; /* count -n */
+ opt = getopt32(argv, "irne:f:", &opt_e, &opt_f,
+ &G.be_quiet); /* counter for -n */
+ //argc -= optind;
+ argv += optind;
+ if (opt & OPT_in_place) { // -i
+ atexit(cleanup_outname);
+ }
+ if (opt & 0x2) G.regex_type |= REG_EXTENDED; // -r
+ //if (opt & 0x4) G.be_quiet++; // -n
+ while (opt_e) { // -e
+ add_cmd_block(llist_pop(&opt_e));
+ }
+ while (opt_f) { // -f
+ char *line;
+ FILE *cmdfile;
+ cmdfile = xfopen_for_read(llist_pop(&opt_f));
+ while ((line = xmalloc_fgetline(cmdfile)) != NULL) {
+ add_cmd(line);
+ free(line);
}
+ fclose(cmdfile);
}
-
- /* if we didn't get a pattern from a -e and no command file was specified,
- * argv[optind] should be the pattern. no pattern, no worky */
- if (sed_cmd_head.next == NULL) {
- if (argv[optind] == NULL)
+ /* if we didn't get a pattern from -e or -f, use argv[0] */
+ if (!(opt & 0x18)) {
+ if (!*argv)
bb_show_usage();
- else {
- char *str_cmd = strdup(argv[optind]);
-
- add_cmd_str(strdup(str_cmd));
- free(str_cmd);
- optind++;
- }
+ add_cmd_block(*argv++);
}
+ /* Flush any unfinished commands. */
+ add_cmd("");
+
+ /* By default, we write to stdout */
+ G.nonstdout = stdout;
- /* argv[(optind)..(argc-1)] should be names of file to process. If no
+ /* argv[0..(argc-1)] should be names of file to process. If no
* files were specified or '-' was specified, take input from stdin.
* Otherwise, we process all the files specified. */
- if (argv[optind] == NULL || (strcmp(argv[optind], "-") == 0)) {
- process_file(stdin);
+ if (argv[0] == NULL) {
+ if (opt & OPT_in_place)
+ bb_error_msg_and_die(bb_msg_requires_arg, "-i");
+ add_input_file(stdin);
+ process_files();
} else {
int i;
FILE *file;
- for (i = optind; i < argc; i++) {
- file = bb_wfopen(argv[i], "r");
- if (file) {
- process_file(file);
- fclose(file);
- } else
+ for (i = 0; argv[i]; i++) {
+ struct stat statbuf;
+ int nonstdoutfd;
+
+ if (LONE_DASH(argv[i]) && !(opt & OPT_in_place)) {
+ add_input_file(stdin);
+ process_files();
+ continue;
+ }
+ file = fopen_or_warn(argv[i], "r");
+ if (!file) {
status = EXIT_FAILURE;
+ continue;
+ }
+ if (!(opt & OPT_in_place)) {
+ add_input_file(file);
+ continue;
+ }
+
+ G.outname = xasprintf("%sXXXXXX", argv[i]);
+ nonstdoutfd = mkstemp(G.outname);
+ if (-1 == nonstdoutfd)
+ bb_perror_msg_and_die("cannot create temp file %s", G.outname);
+ G.nonstdout = fdopen(nonstdoutfd, "w");
+
+ /* Set permissions of output file */
+
+ fstat(fileno(file), &statbuf);
+ fchmod(nonstdoutfd, statbuf.st_mode);
+ add_input_file(file);
+ process_files();
+ fclose(G.nonstdout);
+
+ G.nonstdout = stdout;
+ /* unlink(argv[i]); */
+ xrename(G.outname, argv[i]);
+ free(G.outname);
+ G.outname = NULL;
}
+ if (G.input_file_count > G.current_input_file)
+ process_files();
}
return status;
diff --git a/release/src/router/busybox/editors/sed1line.txt b/release/src/router/busybox/editors/sed1line.txt
new file mode 100644
index 00000000..11a2e36b
--- /dev/null
+++ b/release/src/router/busybox/editors/sed1line.txt
@@ -0,0 +1,425 @@
+http://www.student.northpark.edu/pemente/sed/sed1line.txt
+-------------------------------------------------------------------------
+HANDY ONE-LINERS FOR SED (Unix stream editor) Apr. 26, 2004
+compiled by Eric Pement - pemente[at]northpark[dot]edu version 5.4
+Latest version of this file is usually at:
+ http://sed.sourceforge.net/sed1line.txt
+ http://www.student.northpark.edu/pemente/sed/sed1line.txt
+This file is also available in Portuguese at:
+ http://www.lrv.ufsc.br/wmaker/sed_ptBR.html
+
+FILE SPACING:
+
+ # double space a file
+ sed G
+
+ # double space a file which already has blank lines in it. Output file
+ # should contain no more than one blank line between lines of text.
+ sed '/^$/d;G'
+
+ # triple space a file
+ sed 'G;G'
+
+ # undo double-spacing (assumes even-numbered lines are always blank)
+ sed 'n;d'
+
+ # insert a blank line above every line which matches "regex"
+ sed '/regex/{x;p;x;}'
+
+ # insert a blank line below every line which matches "regex"
+ sed '/regex/G'
+
+ # insert a blank line above and below every line which matches "regex"
+ sed '/regex/{x;p;x;G;}'
+
+NUMBERING:
+
+ # number each line of a file (simple left alignment). Using a tab (see
+ # note on '\t' at end of file) instead of space will preserve margins.
+ sed = filename | sed 'N;s/\n/\t/'
+
+ # number each line of a file (number on left, right-aligned)
+ sed = filename | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /'
+
+ # number each line of file, but only print numbers if line is not blank
+ sed '/./=' filename | sed '/./N; s/\n/ /'
+
+ # count lines (emulates "wc -l")
+ sed -n '$='
+
+TEXT CONVERSION AND SUBSTITUTION:
+
+ # IN UNIX ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
+ sed 's/.$//' # assumes that all lines end with CR/LF
+ sed 's/^M$//' # in bash/tcsh, press Ctrl-V then Ctrl-M
+ sed 's/\x0D$//' # gsed 3.02.80, but top script is easier
+
+ # IN UNIX ENVIRONMENT: convert Unix newlines (LF) to DOS format
+ sed "s/$/`echo -e \\\r`/" # command line under ksh
+ sed 's/$'"/`echo \\\r`/" # command line under bash
+ sed "s/$/`echo \\\r`/" # command line under zsh
+ sed 's/$/\r/' # gsed 3.02.80
+
+ # IN DOS ENVIRONMENT: convert Unix newlines (LF) to DOS format
+ sed "s/$//" # method 1
+ sed -n p # method 2
+
+ # IN DOS ENVIRONMENT: convert DOS newlines (CR/LF) to Unix format
+ # Can only be done with UnxUtils sed, version 4.0.7 or higher.
+ # Cannot be done with other DOS versions of sed. Use "tr" instead.
+ sed "s/\r//" infile >outfile # UnxUtils sed v4.0.7 or higher
+ tr -d \r <infile >outfile # GNU tr version 1.22 or higher
+
+ # delete leading whitespace (spaces, tabs) from front of each line
+ # aligns all text flush left
+ sed 's/^[ \t]*//' # see note on '\t' at end of file
+
+ # delete trailing whitespace (spaces, tabs) from end of each line
+ sed 's/[ \t]*$//' # see note on '\t' at end of file
+
+ # delete BOTH leading and trailing whitespace from each line
+ sed 's/^[ \t]*//;s/[ \t]*$//'
+
+ # insert 5 blank spaces at beginning of each line (make page offset)
+ sed 's/^/ /'
+
+ # align all text flush right on a 79-column width
+ sed -e :a -e 's/^.\{1,78\}$/ &/;ta' # set at 78 plus 1 space
+
+ # center all text in the middle of 79-column width. In method 1,
+ # spaces at the beginning of the line are significant, and trailing
+ # spaces are appended at the end of the line. In method 2, spaces at
+ # the beginning of the line are discarded in centering the line, and
+ # no trailing spaces appear at the end of lines.
+ sed -e :a -e 's/^.\{1,77\}$/ & /;ta' # method 1
+ sed -e :a -e 's/^.\{1,77\}$/ &/;ta' -e 's/\( *\)\1/\1/' # method 2
+
+ # substitute (find and replace) "foo" with "bar" on each line
+ sed 's/foo/bar/' # replaces only 1st instance in a line
+ sed 's/foo/bar/4' # replaces only 4th instance in a line
+ sed 's/foo/bar/g' # replaces ALL instances in a line
+ sed 's/\(.*\)foo\(.*foo\)/\1bar\2/' # replace the next-to-last case
+ sed 's/\(.*\)foo/\1bar/' # replace only the last case
+
+ # substitute "foo" with "bar" ONLY for lines which contain "baz"
+ sed '/baz/s/foo/bar/g'
+
+ # substitute "foo" with "bar" EXCEPT for lines which contain "baz"
+ sed '/baz/!s/foo/bar/g'
+
+ # change "scarlet" or "ruby" or "puce" to "red"
+ sed 's/scarlet/red/g;s/ruby/red/g;s/puce/red/g' # most seds
+ gsed 's/scarlet\|ruby\|puce/red/g' # GNU sed only
+
+ # reverse order of lines (emulates "tac")
+ # bug/feature in HHsed v1.5 causes blank lines to be deleted
+ sed '1!G;h;$!d' # method 1
+ sed -n '1!G;h;$p' # method 2
+
+ # reverse each character on the line (emulates "rev")
+ sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'
+
+ # join pairs of lines side-by-side (like "paste")
+ sed '$!N;s/\n/ /'
+
+ # if a line ends with a backslash, append the next line to it
+ sed -e :a -e '/\\$/N; s/\\\n//; ta'
+
+ # if a line begins with an equal sign, append it to the previous line
+ # and replace the "=" with a single space
+ sed -e :a -e '$!N;s/\n=/ /;ta' -e 'P;D'
+
+ # add commas to numeric strings, changing "1234567" to "1,234,567"
+ gsed ':a;s/\B[0-9]\{3\}\>/,&/;ta' # GNU sed
+ sed -e :a -e 's/\(.*[0-9]\)\([0-9]\{3\}\)/\1,\2/;ta' # other seds
+
+ # add commas to numbers with decimal points and minus signs (GNU sed)
+ gsed ':a;s/\(^\|[^0-9.]\)\([0-9]\+\)\([0-9]\{3\}\)/\1\2,\3/g;ta'
+
+ # add a blank line every 5 lines (after lines 5, 10, 15, 20, etc.)
+ gsed '0~5G' # GNU sed only
+ sed 'n;n;n;n;G;' # other seds
+
+SELECTIVE PRINTING OF CERTAIN LINES:
+
+ # print first 10 lines of file (emulates behavior of "head")
+ sed 10q
+
+ # print first line of file (emulates "head -1")
+ sed q
+
+ # print the last 10 lines of a file (emulates "tail")
+ sed -e :a -e '$q;N;11,$D;ba'
+
+ # print the last 2 lines of a file (emulates "tail -2")
+ sed '$!N;$!D'
+
+ # print the last line of a file (emulates "tail -1")
+ sed '$!d' # method 1
+ sed -n '$p' # method 2
+
+ # print only lines which match regular expression (emulates "grep")
+ sed -n '/regexp/p' # method 1
+ sed '/regexp/!d' # method 2
+
+ # print only lines which do NOT match regexp (emulates "grep -v")
+ sed -n '/regexp/!p' # method 1, corresponds to above
+ sed '/regexp/d' # method 2, simpler syntax
+
+ # print the line immediately before a regexp, but not the line
+ # containing the regexp
+ sed -n '/regexp/{g;1!p;};h'
+
+ # print the line immediately after a regexp, but not the line
+ # containing the regexp
+ sed -n '/regexp/{n;p;}'
+
+ # print 1 line of context before and after regexp, with line number
+ # indicating where the regexp occurred (similar to "grep -A1 -B1")
+ sed -n -e '/regexp/{=;x;1!p;g;$!N;p;D;}' -e h
+
+ # grep for AAA and BBB and CCC (in any order)
+ sed '/AAA/!d; /BBB/!d; /CCC/!d'
+
+ # grep for AAA and BBB and CCC (in that order)
+ sed '/AAA.*BBB.*CCC/!d'
+
+ # grep for AAA or BBB or CCC (emulates "egrep")
+ sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d # most seds
+ gsed '/AAA\|BBB\|CCC/!d' # GNU sed only
+
+ # print paragraph if it contains AAA (blank lines separate paragraphs)
+ # HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
+ sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
+
+ # print paragraph if it contains AAA and BBB and CCC (in any order)
+ sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'
+
+ # print paragraph if it contains AAA or BBB or CCC
+ sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
+ gsed '/./{H;$!d;};x;/AAA\|BBB\|CCC/b;d' # GNU sed only
+
+ # print only lines of 65 characters or longer
+ sed -n '/^.\{65\}/p'
+
+ # print only lines of less than 65 characters
+ sed -n '/^.\{65\}/!p' # method 1, corresponds to above
+ sed '/^.\{65\}/d' # method 2, simpler syntax
+
+ # print section of file from regular expression to end of file
+ sed -n '/regexp/,$p'
+
+ # print section of file based on line numbers (lines 8-12, inclusive)
+ sed -n '8,12p' # method 1
+ sed '8,12!d' # method 2
+
+ # print line number 52
+ sed -n '52p' # method 1
+ sed '52!d' # method 2
+ sed '52q;d' # method 3, efficient on large files
+
+ # beginning at line 3, print every 7th line
+ gsed -n '3~7p' # GNU sed only
+ sed -n '3,${p;n;n;n;n;n;n;}' # other seds
+
+ # print section of file between two regular expressions (inclusive)
+ sed -n '/Iowa/,/Montana/p' # case sensitive
+
+SELECTIVE DELETION OF CERTAIN LINES:
+
+ # print all of file EXCEPT section between 2 regular expressions
+ sed '/Iowa/,/Montana/d'
+
+ # delete duplicate, consecutive lines from a file (emulates "uniq").
+ # First line in a set of duplicate lines is kept, rest are deleted.
+ sed '$!N; /^\(.*\)\n\1$/!P; D'
+
+ # delete duplicate, nonconsecutive lines from a file. Beware not to
+ # overflow the buffer size of the hold space, or else use GNU sed.
+ sed -n 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
+
+ # delete all lines except duplicate lines (emulates "uniq -d").
+ sed '$!N; s/^\(.*\)\n\1$/\1/; t; D'
+
+ # delete the first 10 lines of a file
+ sed '1,10d'
+
+ # delete the last line of a file
+ sed '$d'
+
+ # delete the last 2 lines of a file
+ sed 'N;$!P;$!D;$d'
+
+ # delete the last 10 lines of a file
+ sed -e :a -e '$d;N;2,10ba' -e 'P;D' # method 1
+ sed -n -e :a -e '1,10!{P;N;D;};N;ba' # method 2
+
+ # delete every 8th line
+ gsed '0~8d' # GNU sed only
+ sed 'n;n;n;n;n;n;n;d;' # other seds
+
+ # delete ALL blank lines from a file (same as "grep '.' ")
+ sed '/^$/d' # method 1
+ sed '/./!d' # method 2
+
+ # delete all CONSECUTIVE blank lines from file except the first; also
+ # deletes all blank lines from top and end of file (emulates "cat -s")
+ sed '/./,/^$/!d' # method 1, allows 0 blanks at top, 1 at EOF
+ sed '/^$/N;/\n$/D' # method 2, allows 1 blank at top, 0 at EOF
+
+ # delete all CONSECUTIVE blank lines from file except the first 2:
+ sed '/^$/N;/\n$/N;//D'
+
+ # delete all leading blank lines at top of file
+ sed '/./,$!d'
+
+ # delete all trailing blank lines at end of file
+ sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' # works on all seds
+ sed -e :a -e '/^\n*$/N;/\n$/ba' # ditto, except for gsed 3.02*
+
+ # delete the last line of each paragraph
+ sed -n '/^$/{p;h;};/./{x;/./p;}'
+
+SPECIAL APPLICATIONS:
+
+ # remove nroff overstrikes (char, backspace) from man pages. The 'echo'
+ # command may need an -e switch if you use Unix System V or bash shell.
+ sed "s/.`echo \\\b`//g" # double quotes required for Unix environment
+ sed 's/.^H//g' # in bash/tcsh, press Ctrl-V and then Ctrl-H
+ sed 's/.\x08//g' # hex expression for sed v1.5
+
+ # get Usenet/e-mail message header
+ sed '/^$/q' # deletes everything after first blank line
+
+ # get Usenet/e-mail message body
+ sed '1,/^$/d' # deletes everything up to first blank line
+
+ # get Subject header, but remove initial "Subject: " portion
+ sed '/^Subject: */!d; s///;q'
+
+ # get return address header
+ sed '/^Reply-To:/q; /^From:/h; /./d;g;q'
+
+ # parse out the address proper. Pulls out the e-mail address by itself
+ # from the 1-line return address header (see preceding script)
+ sed 's/ *(.*)//; s/>.*//; s/.*[:<] *//'
+
+ # add a leading angle bracket and space to each line (quote a message)
+ sed 's/^/> /'
+
+ # delete leading angle bracket & space from each line (unquote a message)
+ sed 's/^> //'
+
+ # remove most HTML tags (accommodates multiple-line tags)
+ sed -e :a -e 's/<[^>]*>//g;/</N;//ba'
+
+ # extract multi-part uuencoded binaries, removing extraneous header
+ # info, so that only the uuencoded portion remains. Files passed to
+ # sed must be passed in the proper order. Version 1 can be entered
+ # from the command line; version 2 can be made into an executable
+ # Unix shell script. (Modified from a script by Rahul Dhesi.)
+ sed '/^end/,/^begin/d' file1 file2 ... fileX | uudecode # vers. 1
+ sed '/^end/,/^begin/d' "$@" | uudecode # vers. 2
+
+ # zip up each .TXT file individually, deleting the source file and
+ # setting the name of each .ZIP file to the basename of the .TXT file
+ # (under DOS: the "dir /b" switch returns bare filenames in all caps).
+ echo @echo off >zipup.bat
+ dir /b *.txt | sed "s/^\(.*\)\.TXT/pkzip -mo \1 \1.TXT/" >>zipup.bat
+
+TYPICAL USE: Sed takes one or more editing commands and applies all of
+them, in sequence, to each line of input. After all the commands have
+been applied to the first input line, that line is output and a second
+input line is taken for processing, and the cycle repeats. The
+preceding examples assume that input comes from the standard input
+device (i.e, the console, normally this will be piped input). One or
+more filenames can be appended to the command line if the input does
+not come from stdin. Output is sent to stdout (the screen). Thus:
+
+ cat filename | sed '10q' # uses piped input
+ sed '10q' filename # same effect, avoids a useless "cat"
+ sed '10q' filename > newfile # redirects output to disk
+
+For additional syntax instructions, including the way to apply editing
+commands from a disk file instead of the command line, consult "sed &
+awk, 2nd Edition," by Dale Dougherty and Arnold Robbins (O'Reilly,
+1997; http://www.ora.com), "UNIX Text Processing," by Dale Dougherty
+and Tim O'Reilly (Hayden Books, 1987) or the tutorials by Mike Arst
+distributed in U-SEDIT2.ZIP (many sites). To fully exploit the power
+of sed, one must understand "regular expressions." For this, see
+"Mastering Regular Expressions" by Jeffrey Friedl (O'Reilly, 1997).
+The manual ("man") pages on Unix systems may be helpful (try "man
+sed", "man regexp", or the subsection on regular expressions in "man
+ed"), but man pages are notoriously difficult. They are not written to
+teach sed use or regexps to first-time users, but as a reference text
+for those already acquainted with these tools.
+
+QUOTING SYNTAX: The preceding examples use single quotes ('...')
+instead of double quotes ("...") to enclose editing commands, since
+sed is typically used on a Unix platform. Single quotes prevent the
+Unix shell from intrepreting the dollar sign ($) and backquotes
+(`...`), which are expanded by the shell if they are enclosed in
+double quotes. Users of the "csh" shell and derivatives will also need
+to quote the exclamation mark (!) with the backslash (i.e., \!) to
+properly run the examples listed above, even within single quotes.
+Versions of sed written for DOS invariably require double quotes
+("...") instead of single quotes to enclose editing commands.
+
+USE OF '\t' IN SED SCRIPTS: For clarity in documentation, we have used
+the expression '\t' to indicate a tab character (0x09) in the scripts.
+However, most versions of sed do not recognize the '\t' abbreviation,
+so when typing these scripts from the command line, you should press
+the TAB key instead. '\t' is supported as a regular expression
+metacharacter in awk, perl, and HHsed, sedmod, and GNU sed v3.02.80.
+
+VERSIONS OF SED: Versions of sed do differ, and some slight syntax
+variation is to be expected. In particular, most do not support the
+use of labels (:name) or branch instructions (b,t) within editing
+commands, except at the end of those commands. We have used the syntax
+which will be portable to most users of sed, even though the popular
+GNU versions of sed allow a more succinct syntax. When the reader sees
+a fairly long command such as this:
+
+ sed -e '/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d
+
+it is heartening to know that GNU sed will let you reduce it to:
+
+ sed '/AAA/b;/BBB/b;/CCC/b;d' # or even
+ sed '/AAA\|BBB\|CCC/b;d'
+
+In addition, remember that while many versions of sed accept a command
+like "/one/ s/RE1/RE2/", some do NOT allow "/one/! s/RE1/RE2/", which
+contains space before the 's'. Omit the space when typing the command.
+
+OPTIMIZING FOR SPEED: If execution speed needs to be increased (due to
+large input files or slow processors or hard disks), substitution will
+be executed more quickly if the "find" expression is specified before
+giving the "s/.../.../" instruction. Thus:
+
+ sed 's/foo/bar/g' filename # standard replace command
+ sed '/foo/ s/foo/bar/g' filename # executes more quickly
+ sed '/foo/ s//bar/g' filename # shorthand sed syntax
+
+On line selection or deletion in which you only need to output lines
+from the first part of the file, a "quit" command (q) in the script
+will drastically reduce processing time for large files. Thus:
+
+ sed -n '45,50p' filename # print line nos. 45-50 of a file
+ sed -n '51q;45,50p' filename # same, but executes much faster
+
+If you have any additional scripts to contribute or if you find errors
+in this document, please send e-mail to the compiler. Indicate the
+version of sed you used, the operating system it was compiled for, and
+the nature of the problem. Various scripts in this file were written
+or contributed by:
+
+ Al Aab <af137@freenet.toronto.on.ca> # "seders" list moderator
+ Edgar Allen <era@sky.net> # various
+ Yiorgos Adamopoulos <adamo@softlab.ece.ntua.gr>
+ Dale Dougherty <dale@songline.com> # author of "sed & awk"
+ Carlos Duarte <cdua@algos.inesc.pt> # author of "do it with sed"
+ Eric Pement <pemente@northpark.edu> # author of this document
+ Ken Pizzini <ken@halcyon.com> # author of GNU sed v3.02
+ S.G. Ravenhall <stew.ravenhall@totalise.co.uk> # great de-html script
+ Greg Ubben <gsu@romulus.ncsc.mil> # many contributions & much help
+-------------------------------------------------------------------------
diff --git a/release/src/router/busybox/editors/sed_summary.htm b/release/src/router/busybox/editors/sed_summary.htm
new file mode 100644
index 00000000..34e72b0f
--- /dev/null
+++ b/release/src/router/busybox/editors/sed_summary.htm
@@ -0,0 +1,223 @@
+<html>
+
+<head><title>Command Summary for sed (sed & awk, Second Edition)</title>
+</head>
+
+<body>
+
+<h2>Command Summary for sed</h2>
+
+<dl>
+
+<dt><b>: </b> <b> :</b><em>label</em></dt>
+<dd>Label a line in the script for the transfer of control by
+<b>b</b> or <b>t</b>.
+<em>label</em> may contain up to seven characters.
+(The POSIX standard says that an implementation can allow longer
+labels if it wishes to. GNU sed allows labels to be of any length.)
+</p></dd>
+
+
+<dt><b>=</b> [<em>address</em>]<b>=</b></dt>
+<dd>Write to standard output the line number of addressed line.</p></dd>
+
+
+<dt><b>a</b> [<em>address</em>]<b>a\</b></dt>
+<dd><em>text</em></p>
+
+<p>Append <em>text</em>
+following each line matched by <em>address</em>. If
+<em>text</em> goes over more than one line, newlines
+must be "hidden" by preceding them with a backslash. The
+<em>text</em> will be terminated by the first
+newline that is not hidden in this way. The
+<em>text</em> is not available in the pattern space
+and subsequent commands cannot be applied to it. The results of this
+command are sent to standard output when the list of editing commands
+is finished, regardless of what happens to the current line in the
+pattern space.</p></dd>
+
+
+<dt><b>b</b> [<em>address1</em>[,<em>address2</em>]]<b>b</b>[<em>label</em>]</dt>
+<dd>Transfer control unconditionally (branch) to
+<b>:</b><em>label</em> elsewhere in
+script. That is, the command following the
+<em>label</em> is the next command applied to the
+current line. If no <em>label</em> is specified,
+control falls through to the end of the script, so no more commands
+are applied to the current line.</p></dd>
+
+
+<dt><b>c</b> [<em>address1</em>[,<em>address2</em>]]<b>c\</b></dt>
+<dd><em>text</em></p>
+
+<p>Replace (change) the lines selected by the address with
+<em>text</em>. When a range of lines is specified,
+all lines as a group are replaced by a single copy of
+<em>text</em>. The newline following each line of
+<em>text</em> must be escaped by a backslash, except
+the last line. The contents of the pattern space are, in effect,
+deleted and no subsequent editing commands can be applied to it (or to
+<em>text</em>).</p></dd>
+
+
+<dt><b>d</b> [<em>address1</em>[,<em>address2</em>]]<b>d</b></dt>
+<dd>Delete line(s) from pattern space. Thus, the line is not passed to standard
+output. A new line of input is read and editing resumes with first
+command in script.</p></dd>
+
+
+<dt><b>D</b> [<em>address1</em>[,<em>address2</em>]]<b>D</b></dt>
+<dd>Delete first part (up to embedded newline) of multiline pattern space created
+by <b>N</b> command and resume editing with first command in
+script. If this command empties the pattern space, then a new line
+of input is read, as if the <b>d</b> command had been executed.</p></dd>
+
+
+<dt><b>g</b> [<em>address1</em>[,<em>address2</em>]]<b>g</b></dt>
+<dd>Copy (get) contents of hold space (see <b>h</b> or
+<b>H</b> command) into the pattern space, wiping out
+previous contents.</p></dd>
+
+
+<dt><b>G</b> [<em>address1</em>[,<em>address2</em>]]<b>G</b></dt>
+<dd>Append newline followed by contents of hold space (see
+<b>h</b> or <b>H</b> command) to contents of
+the pattern space. If hold space is empty, a newline is still
+appended to the pattern space.</p></dd>
+
+
+<dt><b>h</b> [<em>address1</em>[,<em>address2</em>]]<b>h</b></dt>
+<dd>Copy pattern space into hold space, a special temporary buffer.
+Previous contents of hold space are wiped out.</p></dd>
+
+
+<dt><b>H</b> [<em>address1</em>[,<em>address2</em>]]<b>H</b></dt>
+<dd>Append newline and contents of pattern space to contents of the hold
+space. Even if hold space is empty, this command still appends the
+newline first.</p></dd>
+
+
+<dt><b>i</b> [<em>address1</em>]<b>i\</b></dt>
+<dd><em>text</em></p>
+
+<p>Insert <em>text</em> before each line matched by
+<em>address</em>. (See <b>a</b> for
+details on <em>text</em>.)</p></dd>
+
+
+<dt><b>l</b> [<em>address1</em>[,<em>address2</em>]]<b>l</b></dt>
+<dd>List the contents of the pattern space, showing nonprinting characters
+as ASCII codes. Long lines are wrapped.</p></dd>
+
+
+<dt><b>n</b> [<em>address1</em>[,<em>address2</em>]]<b>n</b></dt>
+<dd>Read next line of input into pattern space. Current line is sent to
+standard output. New line becomes current line and increments line
+counter. Control passes to command following <b>n</b>
+instead of resuming at the top of the script.</p></dd>
+
+
+<dt><b>N</b> [<em>address1</em>[,<em>address2</em>]]<b>N</b></dt>
+<dd>Append next input line to contents of pattern space; the new line is
+separated from the previous contents of the pattern space by a newline.
+(This command is designed to allow pattern matches across two
+lines. Using \n to match the embedded newline, you can match
+patterns across multiple lines.)</p></dd>
+
+
+<dt><b>p</b> [<em>address1</em>[,<em>address2</em>]]<b>p</b></dt>
+<dd>Print the addressed line(s). Note that this can result in duplicate
+output unless default output is suppressed by using "#n" or
+the <span class="option">-n</span>
+
+command-line option. Typically used before commands that change flow
+control (<b>d</b>, <b>n</b>,
+<b>b</b>) and might prevent the current line from being
+output.</p></dd>
+
+
+<dt><b>P</b> [<em>address1</em>[,<em>address2</em>]]<b>P</b></dt>
+<dd>Print first part (up to embedded newline) of multiline pattern space
+created by <b>N</b> command. Same as <b>p</b>
+if <b>N</b> has not been applied to a line.</p></dd>
+
+
+<dt><b>q</b> [<em>address</em>]<b>q</b></dt>
+<dd>Quit when <em>address</em> is encountered. The
+addressed line is first written to output (if default output is not
+suppressed), along with any text appended to it by previous
+<b>a</b> or <b>r</b> commands.</p></dd>
+
+
+<dt><b>r</b> [<em>address</em>]<b>r</b> <em>file</em></dt>
+<dd>Read contents of <em>file</em> and append after the
+contents of the pattern space. Exactly one space must be put between
+<b>r</b> and the filename.</p></dd>
+
+
+<dt><b>s</b> [<em>address1</em>[,<em>address2</em>]]<b>s</b>/<em>pattern</em>/<em>replacement</em>/[<em>flags</em>]</dt>
+<dd>Substitute <em>replacement</em> for
+<em>pattern</em> on each addressed line. If pattern
+addresses are used, the pattern <b>//</b> represents the
+last pattern address specified. The following flags can be specified:</p>
+
+ <dl>
+
+ <dt><b>n</b></dt>
+ <dd>Replace <em>n</em>th instance of
+ /<em>pattern</em>/ on each addressed line.
+ <em>n</em> is any number in the range 1 to 512, and
+ the default is 1.</p></dd>
+
+ <dt><b>g</b></dt>
+ <dd>Replace all instances of /<em>pattern</em>/ on each
+ addressed line, not just the first instance.</p></dd>
+
+ <dt><b>I</b></dt>
+ <dd>Matching is case-insensitive.<p></p></dd>
+
+ <dt><b>p</b></dt>
+ <dd>Print the line if a successful substitution is done. If several
+ successful substitutions are done, multiple copies of the line will be
+ printed.</p></dd>
+
+ <dt><b>w</b> <em>file</em></dt>
+ <dd>Write the line to <em>file</em> if a replacement
+ was done. A maximum of 10 different <em>files</em> can be opened.</p></dd>
+
+ </dl>
+
+</dd>
+
+
+<dt><b>t</b> [<em>address1</em>[,<em>address2</em>]]<b>t </b>[<em>label</em>]</dt>
+<dd>Test if successful substitutions have been made on addressed lines,
+and if so, branch to line marked by :<em>label</em>.
+(See <b>b</b> and <b>:</b>.) If label is not
+specified, control falls through to bottom of script.</p></dd>
+
+
+<dt><b>w</b> [<em>address1</em>[,<em>address2</em>]]<b>w</b> <em>file</em></dt>
+<dd>Append contents of pattern space to <em>file</em>.
+This action occurs when the command is encountered rather than when
+the pattern space is output. Exactly one space must separate the
+<b>w</b> and the filename. A maximum of 10 different
+files can be opened in a script. This command will create the file if
+it does not exist; if the file exists, its contents will be
+overwritten each time the script is executed. Multiple write commands
+that direct output to the same file append to the end of the file.</p></dd>
+
+
+<dt><b>x</b> [<em>address1</em>[,<em>address2</em>]]<b>x</b></dt>
+<dd>Exchange contents of the pattern space with the contents of the hold
+space.</p></dd>
+
+
+<dt><b>y</b> [<em>address1</em>[,<em>address2</em>]]<b>y</b>/<em>abc</em>/<em>xyz</em>/</dt>
+<dd>Transform each character by position in string
+<em>abc</em> to its equivalent in string
+<em>xyz</em>.</p></dd>
+
+
+</dl>
diff --git a/release/src/router/busybox/editors/vi.c b/release/src/router/busybox/editors/vi.c
index bcbbbb36..4885ee56 100644
--- a/release/src/router/busybox/editors/vi.c
+++ b/release/src/router/busybox/editors/vi.c
@@ -1,32 +1,9 @@
-/* vi: set sw=8 ts=8: */
+/* vi: set sw=4 ts=4: */
/*
* tiny vi.c: A small 'vi' clone
* Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
-static const char vi_Version[] =
- "$Id: vi.c,v 1.1.3.1 2004/12/29 07:07:44 honor Exp $";
-
-/*
- * To compile for standalone use:
- * gcc -Wall -Os -s -DSTANDALONE -o vi vi.c
- * or
- * gcc -Wall -Os -s -DSTANDALONE -DCONFIG_FEATURE_VI_CRASHME -o vi vi.c # include testing features
- * strip vi
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
/*
@@ -36,7 +13,6 @@ static const char vi_Version[] =
* add magic to search /foo.*bar
* add :help command
* :map macros
- * how about mode lines: vi: set sw=8 ts=8:
* if mark[] values were line numbers rather than pointers
* it would be easier to change the mark when add/delete lines
* More intelligence in refresh()
@@ -45,115 +21,92 @@ static const char vi_Version[] =
* An "ex" line oriented mode- maybe using "cmdedit"
*/
-//---- Feature -------------- Bytes to immplement
-#ifdef STANDALONE
-#define vi_main main
-#define CONFIG_FEATURE_VI_COLON // 4288
-#define CONFIG_FEATURE_VI_YANKMARK // 1408
-#define CONFIG_FEATURE_VI_SEARCH // 1088
-#define CONFIG_FEATURE_VI_USE_SIGNALS // 1056
-#define CONFIG_FEATURE_VI_DOT_CMD // 576
-#define CONFIG_FEATURE_VI_READONLY // 128
-#define CONFIG_FEATURE_VI_SETOPTS // 576
-#define CONFIG_FEATURE_VI_SET // 224
-#define CONFIG_FEATURE_VI_WIN_RESIZE // 256 WIN_RESIZE
-// To test editor using CRASHME:
-// vi -C filename
-// To stop testing, wait until all to text[] is deleted, or
-// Ctrl-Z and kill -9 %1
-// while in the editor Ctrl-T will toggle the crashme function on and off.
-//#define CONFIG_FEATURE_VI_CRASHME // randomly pick commands to execute
-#endif /* STANDALONE */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <termios.h>
-#include <unistd.h>
-#include <sys/ioctl.h>
-#include <sys/time.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <setjmp.h>
-#include <regex.h>
-#include <ctype.h>
-#include <assert.h>
-#include <errno.h>
-#include <stdarg.h>
-#ifndef STANDALONE
-#include "busybox.h"
-#endif /* STANDALONE */
-
-#ifdef CONFIG_LOCALE_SUPPORT
-#define Isprint(c) isprint((c))
+#include "libbb.h"
+
+/* the CRASHME code is unmaintained, and doesn't currently build */
+#define ENABLE_FEATURE_VI_CRASHME 0
+
+
+#if ENABLE_LOCALE_SUPPORT
+
+#if ENABLE_FEATURE_VI_8BIT
+#define Isprint(c) isprint(c)
#else
-#define Isprint(c) ( (c) >= ' ' && (c) != 127 && (c) != ((unsigned char)'\233') )
-#endif
-
-#ifndef TRUE
-#define TRUE ((int)1)
-#define FALSE ((int)0)
-#endif /* TRUE */
-#define MAX_SCR_COLS BUFSIZ
-
-// Misc. non-Ascii keys that report an escape sequence
-#define VI_K_UP 128 // cursor key Up
-#define VI_K_DOWN 129 // cursor key Down
-#define VI_K_RIGHT 130 // Cursor Key Right
-#define VI_K_LEFT 131 // cursor key Left
-#define VI_K_HOME 132 // Cursor Key Home
-#define VI_K_END 133 // Cursor Key End
-#define VI_K_INSERT 134 // Cursor Key Insert
-#define VI_K_PAGEUP 135 // Cursor Key Page Up
-#define VI_K_PAGEDOWN 136 // Cursor Key Page Down
-#define VI_K_FUN1 137 // Function Key F1
-#define VI_K_FUN2 138 // Function Key F2
-#define VI_K_FUN3 139 // Function Key F3
-#define VI_K_FUN4 140 // Function Key F4
-#define VI_K_FUN5 141 // Function Key F5
-#define VI_K_FUN6 142 // Function Key F6
-#define VI_K_FUN7 143 // Function Key F7
-#define VI_K_FUN8 144 // Function Key F8
-#define VI_K_FUN9 145 // Function Key F9
-#define VI_K_FUN10 146 // Function Key F10
-#define VI_K_FUN11 147 // Function Key F11
-#define VI_K_FUN12 148 // Function Key F12
+#define Isprint(c) (isprint(c) && (unsigned char)(c) < 0x7f)
+#endif
+
+#else
+
+/* 0x9b is Meta-ESC */
+#if ENABLE_FEATURE_VI_8BIT
+#define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
+#else
+#define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f)
+#endif
+
+#endif
+
+
+enum {
+ MAX_TABSTOP = 32, // sanity limit
+ // User input len. Need not be extra big.
+ // Lines in file being edited *can* be bigger than this.
+ MAX_INPUT_LEN = 128,
+ // Sanity limits. We have only one buffer of this size.
+ MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
+ MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
+};
/* vt102 typical ESC sequence */
/* terminal standout start/normal ESC sequence */
-static const char SOs[] = "\033[7m";
-static const char SOn[] = "\033[0m";
+static const char SOs[] ALIGN1 = "\033[7m";
+static const char SOn[] ALIGN1 = "\033[0m";
/* terminal bell sequence */
-static const char bell[] = "\007";
+static const char bell[] ALIGN1 = "\007";
/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
-static const char Ceol[] = "\033[0K";
-static const char Ceos [] = "\033[0J";
+static const char Ceol[] ALIGN1 = "\033[0K";
+static const char Ceos[] ALIGN1 = "\033[0J";
/* Cursor motion arbitrary destination ESC sequence */
-static const char CMrc[] = "\033[%d;%dH";
+static const char CMrc[] ALIGN1 = "\033[%d;%dH";
/* Cursor motion up and down ESC sequence */
-static const char CMup[] = "\033[A";
-static const char CMdown[] = "\n";
+static const char CMup[] ALIGN1 = "\033[A";
+static const char CMdown[] ALIGN1 = "\n";
+
+#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
+// cmds modifying text[]
+// vda: removed "aAiIs" as they switch us into insert mode
+// and remembering input for replay after them makes no sense
+static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
+#endif
+enum {
+ YANKONLY = FALSE,
+ YANKDEL = TRUE,
+ FORWARD = 1, // code depends on "1" for array index
+ BACK = -1, // code depends on "-1" for array index
+ LIMITED = 0, // how much of text[] in char_search
+ FULL = 1, // how much of text[] in char_search
+
+ S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
+ S_TO_WS = 2, // used in skip_thing() for moving "dot"
+ S_OVER_WS = 3, // used in skip_thing() for moving "dot"
+ S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
+ S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
+};
-static const int YANKONLY = FALSE;
-static const int YANKDEL = TRUE;
-static const int FORWARD = 1; // code depends on "1" for array index
-static const int BACK = -1; // code depends on "-1" for array index
-static const int LIMITED = 0; // how much of text[] in char_search
-static const int FULL = 1; // how much of text[] in char_search
-static const int S_BEFORE_WS = 1; // used in skip_thing() for moving "dot"
-static const int S_TO_WS = 2; // used in skip_thing() for moving "dot"
-static const int S_OVER_WS = 3; // used in skip_thing() for moving "dot"
-static const int S_END_PUNCT = 4; // used in skip_thing() for moving "dot"
-static const int S_END_ALNUM = 5; // used in skip_thing() for moving "dot"
+/* vi.c expects chars to be unsigned. */
+/* busybox build system provides that, but it's better */
+/* to audit and fix the source */
-typedef unsigned char Byte;
+struct globals {
+ /* many references - keep near the top of globals */
+ char *text, *end; // pointers to the user data in memory
+ char *dot; // where all the action takes place
+ int text_size; // size of the allocated buffer
-static int vi_setops;
+ /* the rest */
+ smallint vi_setops;
#define VI_AUTOINDENT 1
#define VI_SHOWMATCH 2
#define VI_IGNORECASE 4
@@ -164,76 +117,177 @@ static int vi_setops;
/* indicate error with beep or flash */
#define err_method (vi_setops & VI_ERR_METHOD)
+#if ENABLE_FEATURE_VI_READONLY
+ smallint readonly_mode;
+#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
+#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
+#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
+#else
+#define SET_READONLY_FILE(flags) ((void)0)
+#define SET_READONLY_MODE(flags) ((void)0)
+#define UNSET_READONLY_FILE(flags) ((void)0)
+#endif
+
+ smallint editing; // >0 while we are editing a file
+ // [code audit says "can be 0, 1 or 2 only"]
+ smallint cmd_mode; // 0=command 1=insert 2=replace
+ int file_modified; // buffer contents changed (counter, not flag!)
+ int last_file_modified; // = -1;
+ int fn_start; // index of first cmd line file name
+ int save_argc; // how many file names on cmd line
+ int cmdcnt; // repetition count
+ unsigned rows, columns; // the terminal screen is this size
+ int crow, ccol; // cursor is on Crow x Ccol
+ int offset; // chars scrolled off the screen to the left
+ int have_status_msg; // is default edit status needed?
+ // [don't make smallint!]
+ int last_status_cksum; // hash of current status line
+ char *current_filename;
+ char *screenbegin; // index into text[], of top line on the screen
+ char *screen; // pointer to the virtual screen buffer
+ int screensize; // and its size
+ int tabstop;
+ int last_forward_char; // last char searched for with 'f' (int because of Unicode)
+ char erase_char; // the users erase character
+ char last_input_char; // last char read from user
+
+ smalluint chars_to_parse;
+#if ENABLE_FEATURE_VI_DOT_CMD
+ smallint adding2q; // are we currently adding user input to q
+ int lmc_len; // length of last_modifying_cmd
+ char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
+#endif
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ int last_row; // where the cursor was last moved to
+#endif
+#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
+ int my_pid;
+#endif
+#if ENABLE_FEATURE_VI_SEARCH
+ char *last_search_pattern; // last pattern from a '/' or '?' search
+#endif
+
+ /* former statics */
+#if ENABLE_FEATURE_VI_YANKMARK
+ char *edit_file__cur_line;
+#endif
+ int refresh__old_offset;
+ int format_edit_status__tot;
+
+ /* a few references only */
+#if ENABLE_FEATURE_VI_YANKMARK
+ int YDreg, Ureg; // default delete register and orig line for "U"
+ char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
+ char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
+ char *context_start, *context_end;
+#endif
+#if ENABLE_FEATURE_VI_USE_SIGNALS
+ sigjmp_buf restart; // catch_sig()
+#endif
+ struct termios term_orig, term_vi; // remember what the cooked mode was
+#if ENABLE_FEATURE_VI_COLON
+ char *initial_cmds[3]; // currently 2 entries, NULL terminated
+#endif
+ // Should be just enough to hold a key sequence,
+ // but CRASHME mode uses it as generated command buffer too
+#if ENABLE_FEATURE_VI_CRASHME
+ char readbuffer[128];
+#else
+ char readbuffer[KEYCODE_BUFFER_SIZE];
+#endif
+#define STATUS_BUFFER_LEN 200
+ char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
+#if ENABLE_FEATURE_VI_DOT_CMD
+ char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
+#endif
+ char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
-static int editing; // >0 while we are editing a file
-static int cmd_mode; // 0=command 1=insert
-static int file_modified; // buffer contents changed
-static int fn_start; // index of first cmd line file name
-static int save_argc; // how many file names on cmd line
-static int cmdcnt; // repetition count
-static fd_set rfds; // use select() for small sleeps
-static struct timeval tv; // use select() for small sleeps
-static int rows, columns; // the terminal screen is this size
-static int crow, ccol, offset; // cursor is on Crow x Ccol with Horz Ofset
-static Byte *status_buffer; // mesages to the user
-static Byte *cfn; // previous, current, and next file name
-static Byte *text, *end, *textend; // pointers to the user data in memory
-static Byte *screen; // pointer to the virtual screen buffer
-static int screensize; // and its size
-static Byte *screenbegin; // index into text[], of top line on the screen
-static Byte *dot; // where all the action takes place
-static int tabstop;
-static struct termios term_orig, term_vi; // remember what the cooked mode was
-static Byte erase_char; // the users erase character
-static Byte last_input_char; // last char read from user
-static Byte last_forward_char; // last char searched for with 'f'
-
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
-static int last_row; // where the cursor was last moved to
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
-static jmp_buf restart; // catch_sig()
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
-#if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME)
-static int my_pid;
-#endif
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
-static struct winsize winsize; // remember the window size
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
-static int adding2q; // are we currently adding user input to q
-static Byte *last_modifying_cmd; // last modifying cmd for "."
-static Byte *ioq, *ioq_start; // pointer to string for get_one_char to "read"
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
-#if defined(CONFIG_FEATURE_VI_DOT_CMD) || defined(CONFIG_FEATURE_VI_YANKMARK)
-static Byte *modifying_cmds; // cmds that modify text[]
-#endif /* CONFIG_FEATURE_VI_DOT_CMD || CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_READONLY
-static int vi_readonly, readonly;
-#endif /* CONFIG_FEATURE_VI_READONLY */
-#ifdef CONFIG_FEATURE_VI_YANKMARK
-static Byte *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
-static int YDreg, Ureg; // default delete register and orig line for "U"
-static Byte *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
-static Byte *context_start, *context_end;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_SEARCH
-static Byte *last_search_pattern; // last pattern from a '/' or '?' search
-#endif /* CONFIG_FEATURE_VI_SEARCH */
-
-
-static void edit_file(Byte *); // edit one file
-static void do_cmd(Byte); // execute a command
-static void sync_cursor(Byte *, int *, int *); // synchronize the screen cursor to dot
-static Byte *begin_line(Byte *); // return pointer to cur line B-o-l
-static Byte *end_line(Byte *); // return pointer to cur line E-o-l
-static Byte *prev_line(Byte *); // return pointer to prev line B-o-l
-static Byte *next_line(Byte *); // return pointer to next line B-o-l
-static Byte *end_screen(void); // get pointer to last char on screen
-static int count_lines(Byte *, Byte *); // count line from start to stop
-static Byte *find_line(int); // find begining of line #li
-static Byte *move_to_col(Byte *, int); // move "p" to column l
-static int isblnk(Byte); // is the char a blank or tab
+ char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
+};
+#define G (*ptr_to_globals)
+#define text (G.text )
+#define text_size (G.text_size )
+#define end (G.end )
+#define dot (G.dot )
+#define reg (G.reg )
+
+#define vi_setops (G.vi_setops )
+#define editing (G.editing )
+#define cmd_mode (G.cmd_mode )
+#define file_modified (G.file_modified )
+#define last_file_modified (G.last_file_modified )
+#define fn_start (G.fn_start )
+#define save_argc (G.save_argc )
+#define cmdcnt (G.cmdcnt )
+#define rows (G.rows )
+#define columns (G.columns )
+#define crow (G.crow )
+#define ccol (G.ccol )
+#define offset (G.offset )
+#define status_buffer (G.status_buffer )
+#define have_status_msg (G.have_status_msg )
+#define last_status_cksum (G.last_status_cksum )
+#define current_filename (G.current_filename )
+#define screen (G.screen )
+#define screensize (G.screensize )
+#define screenbegin (G.screenbegin )
+#define tabstop (G.tabstop )
+#define last_forward_char (G.last_forward_char )
+#define erase_char (G.erase_char )
+#define last_input_char (G.last_input_char )
+#define chars_to_parse (G.chars_to_parse )
+#if ENABLE_FEATURE_VI_READONLY
+#define readonly_mode (G.readonly_mode )
+#else
+#define readonly_mode 0
+#endif
+#define adding2q (G.adding2q )
+#define lmc_len (G.lmc_len )
+#define ioq (G.ioq )
+#define ioq_start (G.ioq_start )
+#define last_row (G.last_row )
+#define my_pid (G.my_pid )
+#define last_search_pattern (G.last_search_pattern)
+
+#define edit_file__cur_line (G.edit_file__cur_line)
+#define refresh__old_offset (G.refresh__old_offset)
+#define format_edit_status__tot (G.format_edit_status__tot)
+
+#define YDreg (G.YDreg )
+#define Ureg (G.Ureg )
+#define mark (G.mark )
+#define context_start (G.context_start )
+#define context_end (G.context_end )
+#define restart (G.restart )
+#define term_orig (G.term_orig )
+#define term_vi (G.term_vi )
+#define initial_cmds (G.initial_cmds )
+#define readbuffer (G.readbuffer )
+#define scr_out_buf (G.scr_out_buf )
+#define last_modifying_cmd (G.last_modifying_cmd )
+#define get_input_line__buf (G.get_input_line__buf)
+
+#define INIT_G() do { \
+ SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
+ last_file_modified = -1; \
+ /* "" but has space for 2 chars: */ \
+ USE_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
+} while (0)
+
+
+static int init_text_buffer(char *); // init from file or create new
+static void edit_file(char *); // edit one file
+static void do_cmd(int); // execute a command
+static int next_tabstop(int);
+static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
+static char *begin_line(char *); // return pointer to cur line B-o-l
+static char *end_line(char *); // return pointer to cur line E-o-l
+static char *prev_line(char *); // return pointer to prev line B-o-l
+static char *next_line(char *); // return pointer to next line B-o-l
+static char *end_screen(void); // get pointer to last char on screen
+static int count_lines(char *, char *); // count line from start to stop
+static char *find_line(int); // find begining of line #li
+static char *move_to_col(char *, int); // move "p" to column l
static void dot_left(void); // move dot left- dont leave line
static void dot_right(void); // move dot right- dont leave line
static void dot_begin(void); // move dot to B-o-l
@@ -243,87 +297,97 @@ static void dot_prev(void); // move dot to prev line B-o-l
static void dot_scroll(int, int); // move the screen up or down
static void dot_skip_over_ws(void); // move dot pat WS
static void dot_delete(void); // delete the char at 'dot'
-static Byte *bound_dot(Byte *); // make sure text[0] <= P < "end"
-static Byte *new_screen(int, int); // malloc virtual screen memory
-static Byte *new_text(int); // malloc memory for text[] buffer
-static Byte *char_insert(Byte *, Byte); // insert the char c at 'p'
-static Byte *stupid_insert(Byte *, Byte); // stupidly insert the char c at 'p'
-static Byte find_range(Byte **, Byte **, Byte); // return pointers for an object
-static int st_test(Byte *, int, int, Byte *); // helper for skip_thing()
-static Byte *skip_thing(Byte *, int, int, int); // skip some object
-static Byte *find_pair(Byte *, Byte); // find matching pair () [] {}
-static Byte *text_hole_delete(Byte *, Byte *); // at "p", delete a 'size' byte hole
-static Byte *text_hole_make(Byte *, int); // at "p", make a 'size' byte hole
-static Byte *yank_delete(Byte *, Byte *, int, int); // yank text[] into register then delete
+static char *bound_dot(char *); // make sure text[0] <= P < "end"
+static char *new_screen(int, int); // malloc virtual screen memory
+static char *char_insert(char *, char); // insert the char c at 'p'
+// might reallocate text[]! use p += stupid_insert(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
+static int find_range(char **, char **, char); // return pointers for an object
+static int st_test(char *, int, int, char *); // helper for skip_thing()
+static char *skip_thing(char *, int, int, int); // skip some object
+static char *find_pair(char *, char); // find matching pair () [] {}
+static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
+// might reallocate text[]! use p += text_hole_make(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
+static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
static void show_help(void); // display some help info
static void rawmode(void); // set "raw" mode on tty
static void cookmode(void); // return to "cooked" mode on tty
-static int mysleep(int); // sleep for 'h' 1/100 seconds
-static Byte readit(void); // read (maybe cursor) key from stdin
-static Byte get_one_char(void); // read 1 char from stdin
-static int file_size(const Byte *); // what is the byte size of "fn"
-static int file_insert(Byte *, Byte *, int);
-static int file_write(Byte *, Byte *, Byte *);
+// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
+static int mysleep(int);
+static int readit(void); // read (maybe cursor) key from stdin
+static int get_one_char(void); // read 1 char from stdin
+static int file_size(const char *); // what is the byte size of "fn"
+#if !ENABLE_FEATURE_VI_READONLY
+#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
+#endif
+// file_insert might reallocate text[]!
+static int file_insert(const char *, char *, int);
+static int file_write(char *, char *, char *);
+#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+#define place_cursor(a, b, optimize) place_cursor(a, b)
+#endif
static void place_cursor(int, int, int);
static void screen_erase(void);
static void clear_to_eol(void);
static void clear_to_eos(void);
+static void go_bottom_and_clear_to_eol(void);
static void standout_start(void); // send "start reverse video" sequence
static void standout_end(void); // send "end reverse video" sequence
static void flash(int); // flash the terminal screen
static void show_status_line(void); // put a message on the bottom line
-static void psb(const char *, ...); // Print Status Buf
-static void psbs(const char *, ...); // Print Status Buf in standout mode
-static void ni(Byte *); // display messages
-static void edit_status(void); // show file status on status line
+static void status_line(const char *, ...); // print to status buf
+static void status_line_bold(const char *, ...);
+static void not_implemented(const char *); // display "Not implemented" message
+static int format_edit_status(void); // format file status on status line
static void redraw(int); // force a full screen refresh
-static void format_line(Byte*, Byte*, int);
+static char* format_line(char* /*, int*/);
static void refresh(int); // update the terminal from screen[]
static void Indicate_Error(void); // use flash or beep to indicate error
#define indicate_error(c) Indicate_Error()
-
-#ifdef CONFIG_FEATURE_VI_SEARCH
-static Byte *char_search(Byte *, Byte *, int, int); // search for pattern starting at p
-static int mycmp(Byte *, Byte *, int); // string cmp based in "ignorecase"
-#endif /* CONFIG_FEATURE_VI_SEARCH */
-#ifdef CONFIG_FEATURE_VI_COLON
static void Hit_Return(void);
-static Byte *get_one_address(Byte *, int *); // get colon addr, if present
-static Byte *get_address(Byte *, int *, int *); // get two colon addrs, if present
-static void colon(Byte *); // execute the "colon" mode cmds
-#endif /* CONFIG_FEATURE_VI_COLON */
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
+
+#if ENABLE_FEATURE_VI_SEARCH
+static char *char_search(char *, const char *, int, int); // search for pattern starting at p
+static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
+#endif
+#if ENABLE_FEATURE_VI_COLON
+static char *get_one_address(char *, int *); // get colon addr, if present
+static char *get_address(char *, int *, int *); // get two colon addrs, if present
+static void colon(char *); // execute the "colon" mode cmds
+#endif
+#if ENABLE_FEATURE_VI_USE_SIGNALS
static void winch_sig(int); // catch window size changes
static void suspend_sig(int); // catch ctrl-Z
static void catch_sig(int); // catch ctrl-C and alarm time-outs
-static void core_sig(int); // catch a core dump signal
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
-static void start_new_cmd_q(Byte); // new queue for command
+#endif
+#if ENABLE_FEATURE_VI_DOT_CMD
+static void start_new_cmd_q(char); // new queue for command
static void end_cmd_q(void); // stop saving input chars
-#else /* CONFIG_FEATURE_VI_DOT_CMD */
-#define end_cmd_q()
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
-static void window_size_get(int); // find out what size the window is
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
-#ifdef CONFIG_FEATURE_VI_SETOPTS
-static void showmatching(Byte *); // show the matching pair () [] {}
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
-#if defined(CONFIG_FEATURE_VI_YANKMARK) || defined(CONFIG_FEATURE_VI_COLON) || defined(CONFIG_FEATURE_VI_CRASHME)
-static Byte *string_insert(Byte *, Byte *); // insert the string at 'p'
-#endif /* CONFIG_FEATURE_VI_YANKMARK || CONFIG_FEATURE_VI_COLON || CONFIG_FEATURE_VI_CRASHME */
-#ifdef CONFIG_FEATURE_VI_YANKMARK
-static Byte *text_yank(Byte *, Byte *, int); // save copy of "p" into a register
-static Byte what_reg(void); // what is letter of current YDreg
-static void check_context(Byte); // remember context for '' command
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#else
+#define end_cmd_q() ((void)0)
+#endif
+#if ENABLE_FEATURE_VI_SETOPTS
+static void showmatching(char *); // show the matching pair () [] {}
+#endif
+#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
+// might reallocate text[]! use p += string_insert(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
+#endif
+#if ENABLE_FEATURE_VI_YANKMARK
+static char *text_yank(char *, char *, int); // save copy of "p" into a register
+static char what_reg(void); // what is letter of current YDreg
+static void check_context(char); // remember context for '' command
+#endif
+#if ENABLE_FEATURE_VI_CRASHME
static void crash_dummy();
static void crash_test();
static int crashme = 0;
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif
static void write1(const char *out)
@@ -331,61 +395,60 @@ static void write1(const char *out)
fputs(out, stdout);
}
-extern int vi_main(int argc, char **argv)
+int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int vi_main(int argc, char **argv)
{
int c;
- RESERVE_CONFIG_BUFFER(STATUS_BUFFER, 200);
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- int i;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME)
+ INIT_G();
+
+#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
my_pid = getpid();
#endif
-#ifdef CONFIG_FEATURE_VI_CRASHME
- (void) srand((long) my_pid);
-#endif /* CONFIG_FEATURE_VI_CRASHME */
-
- status_buffer = STATUS_BUFFER;
-
-#ifdef CONFIG_FEATURE_VI_READONLY
- vi_readonly = readonly = FALSE;
- if (strncmp(argv[0], "view", 4) == 0) {
- readonly = TRUE;
- vi_readonly = TRUE;
- }
-#endif /* CONFIG_FEATURE_VI_READONLY */
- vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE | VI_ERR_METHOD;
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- for (i = 0; i < 28; i++) {
- reg[i] = 0;
- } // init the yank regs
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#if defined(CONFIG_FEATURE_VI_DOT_CMD) || defined(CONFIG_FEATURE_VI_YANKMARK)
- modifying_cmds = (Byte *) "aAcCdDiIJoOpPrRsxX<>~"; // cmds modifying text[]
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
-
- // 1- process $HOME/.exrc file
+#if ENABLE_FEATURE_VI_CRASHME
+ srand((long) my_pid);
+#endif
+#ifdef NO_SUCH_APPLET_YET
+ /* If we aren't "vi", we are "view" */
+ if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
+ SET_READONLY_MODE(readonly_mode);
+ }
+#endif
+
+ vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
+ // 1- process $HOME/.exrc file (not inplemented yet)
// 2- process EXINIT variable from environment
// 3- process command line args
- while ((c = getopt(argc, argv, "hCR")) != -1) {
+#if ENABLE_FEATURE_VI_COLON
+ {
+ char *p = getenv("EXINIT");
+ if (p && *p)
+ initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
+ }
+#endif
+ while ((c = getopt(argc, argv, "hCRH" USE_FEATURE_VI_COLON("c:"))) != -1) {
switch (c) {
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#if ENABLE_FEATURE_VI_CRASHME
case 'C':
crashme = 1;
break;
-#endif /* CONFIG_FEATURE_VI_CRASHME */
-#ifdef CONFIG_FEATURE_VI_READONLY
+#endif
+#if ENABLE_FEATURE_VI_READONLY
case 'R': // Read-only flag
- readonly = TRUE;
+ SET_READONLY_MODE(readonly_mode);
break;
-#endif /* CONFIG_FEATURE_VI_READONLY */
- //case 'r': // recover flag- ignore- we don't use tmp file
- //case 'x': // encryption flag- ignore
- //case 'c': // execute command first
- //case 'h': // help -- just use default
- default:
+#endif
+#if ENABLE_FEATURE_VI_COLON
+ case 'c': // cmd line vi command
+ if (*optarg)
+ initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
+ break;
+#endif
+ case 'H':
show_help();
+ /* fall through */
+ default:
+ bb_show_usage();
return 1;
}
}
@@ -397,207 +460,218 @@ extern int vi_main(int argc, char **argv)
//----- This is the main file handling loop --------------
if (optind >= argc) {
- editing = 1; // 0= exit, 1= one file, 2= multiple files
edit_file(0);
} else {
for (; optind < argc; optind++) {
- editing = 1; // 0=exit, 1=one file, 2+ =many files
- free(cfn);
- cfn = (Byte *) bb_xstrdup(argv[optind]);
- edit_file(cfn);
+ edit_file(argv[optind]);
}
}
//-----------------------------------------------------------
- return (0);
+ return 0;
}
-static void edit_file(Byte * fn)
+/* read text from file or create an empty buf */
+/* will also update current_filename */
+static int init_text_buffer(char *fn)
{
- Byte c;
- int cnt, size, ch;
+ int rc;
+ int size = file_size(fn); // file size. -1 means does not exist.
+
+ /* allocate/reallocate text buffer */
+ free(text);
+ text_size = size + 10240;
+ screenbegin = dot = end = text = xzalloc(text_size);
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
+ if (fn != current_filename) {
+ free(current_filename);
+ current_filename = xstrdup(fn);
+ }
+ if (size < 0) {
+ // file dont exist. Start empty buf with dummy line
+ char_insert(text, '\n');
+ rc = 0;
+ } else {
+ rc = file_insert(fn, text, 1);
+ }
+ file_modified = 0;
+ last_file_modified = -1;
+#if ENABLE_FEATURE_VI_YANKMARK
+ /* init the marks. */
+ memset(mark, 0, sizeof(mark));
+#endif
+ return rc;
+}
+
+static void edit_file(char *fn)
+{
+#if ENABLE_FEATURE_VI_YANKMARK
+#define cur_line edit_file__cur_line
+#endif
+ int c;
+ int size;
+#if ENABLE_FEATURE_VI_USE_SIGNALS
int sig;
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- static Byte *cur_line;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
+ editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
rawmode();
rows = 24;
columns = 80;
- ch= -1;
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
- window_size_get(0);
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
+ size = 0;
+ if (ENABLE_FEATURE_VI_WIN_RESIZE) {
+ get_terminal_width_height(0, &columns, &rows);
+ if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
+ if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
+ }
new_screen(rows, columns); // get memory for virtual screen
+ init_text_buffer(fn);
- cnt = file_size(fn); // file size
- size = 2 * cnt; // 200% of file size
- new_text(size); // get a text[] buffer
- screenbegin = dot = end = text;
- if (fn != 0) {
- ch= file_insert(fn, text, cnt);
- }
- if (ch < 1) {
- (void) char_insert(text, '\n'); // start empty buf with dummy line
- }
- file_modified = FALSE;
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
YDreg = 26; // default Yank/Delete reg
Ureg = 27; // hold orig line for "U" cmd
- for (cnt = 0; cnt < 28; cnt++) {
- mark[cnt] = 0;
- } // init the marks
mark[26] = mark[27] = text; // init "previous context"
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
last_forward_char = last_input_char = '\0';
crow = 0;
ccol = 0;
- edit_status();
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
+#if ENABLE_FEATURE_VI_USE_SIGNALS
catch_sig(0);
- core_sig(0);
signal(SIGWINCH, winch_sig);
signal(SIGTSTP, suspend_sig);
- sig = setjmp(restart);
+ sig = sigsetjmp(restart, 1);
if (sig != 0) {
- const char *msg = "";
-
- if (sig == SIGWINCH)
- msg = "(window resize)";
- if (sig == SIGHUP)
- msg = "(hangup)";
- if (sig == SIGINT)
- msg = "(interrupt)";
- if (sig == SIGTERM)
- msg = "(terminate)";
- if (sig == SIGBUS)
- msg = "(bus error)";
- if (sig == SIGSEGV)
- msg = "(I tried to touch invalid memory)";
- if (sig == SIGALRM)
- msg = "(alarm)";
-
- psbs("-- caught signal %d %s--", sig, msg);
screenbegin = dot = text;
}
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
+#endif
- editing = 1;
cmd_mode = 0; // 0=command 1=insert 2='R'eplace
cmdcnt = 0;
tabstop = 8;
offset = 0; // no horizontal offset
c = '\0';
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
- free(last_modifying_cmd);
+#if ENABLE_FEATURE_VI_DOT_CMD
free(ioq_start);
- ioq = ioq_start = last_modifying_cmd = 0;
+ ioq = ioq_start = NULL;
+ lmc_len = 0;
adding2q = 0;
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
- redraw(FALSE); // dont force every col re-draw
- show_status_line();
- fflush(stdout);
+#endif
+#if ENABLE_FEATURE_VI_COLON
+ {
+ char *p, *q;
+ int n = 0;
+
+ while ((p = initial_cmds[n])) {
+ do {
+ q = p;
+ p = strchr(q, '\n');
+ if (p)
+ while (*p == '\n')
+ *p++ = '\0';
+ if (*q)
+ colon(q);
+ } while (p);
+ free(initial_cmds[n]);
+ initial_cmds[n] = NULL;
+ n++;
+ }
+ }
+#endif
+ redraw(FALSE); // dont force every col re-draw
//------This is the main Vi cmd handling loop -----------------------
while (editing > 0) {
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#if ENABLE_FEATURE_VI_CRASHME
if (crashme > 0) {
if ((end - text) > 1) {
crash_dummy(); // generate a random command
} else {
crashme = 0;
- dot =
- string_insert(text, (Byte *) "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
+ string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
+ dot = text;
refresh(FALSE);
}
}
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif
last_input_char = c = get_one_char(); // get a cmd from user
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
// save a copy of the current line- for the 'U" command
if (begin_line(dot) != cur_line) {
cur_line = begin_line(dot);
text_yank(begin_line(dot), end_line(dot), Ureg);
}
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
+#endif
+#if ENABLE_FEATURE_VI_DOT_CMD
// These are commands that change text[].
// Remember the input for the "." command
- if (!adding2q && ioq_start == 0
- && strchr((char *) modifying_cmds, c) != NULL) {
+ if (!adding2q && ioq_start == NULL
+ && cmd_mode == 0 // command mode
+ && c > '\0' // exclude NUL and non-ASCII chars
+ && c < 0x7f // (Unicode and such)
+ && strchr(modifying_cmds, c)
+ ) {
start_new_cmd_q(c);
}
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
+#endif
do_cmd(c); // execute the user command
- //
+
// poll to see if there is input already waiting. if we are
// not able to display output fast enough to keep up, skip
// the display update until we catch up with input.
- if (mysleep(0) == 0) {
- // no input pending- so update output
+ if (!chars_to_parse && mysleep(0) == 0) {
+ // no input pending - so update output
refresh(FALSE);
show_status_line();
}
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#if ENABLE_FEATURE_VI_CRASHME
if (crashme > 0)
crash_test(); // test editor variables
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif
}
//-------------------------------------------------------------------
- place_cursor(rows, 0, FALSE); // go to bottom of screen
- clear_to_eol(); // Erase to end of line
+ go_bottom_and_clear_to_eol();
cookmode();
+#undef cur_line
}
//----- The Colon commands -------------------------------------
-#ifdef CONFIG_FEATURE_VI_COLON
-static Byte *get_one_address(Byte * p, int *addr) // get colon addr, if present
+#if ENABLE_FEATURE_VI_COLON
+static char *get_one_address(char *p, int *addr) // get colon addr, if present
{
int st;
- Byte *q;
-
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- Byte c;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_SEARCH
- Byte *pat, buf[BUFSIZ];
-#endif /* CONFIG_FEATURE_VI_SEARCH */
+ char *q;
+ USE_FEATURE_VI_YANKMARK(char c;)
+ USE_FEATURE_VI_SEARCH(char *pat;)
*addr = -1; // assume no addr
if (*p == '.') { // the current line
p++;
q = begin_line(dot);
*addr = count_lines(text, q);
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- } else if (*p == '\'') { // is this a mark addr
+ }
+#if ENABLE_FEATURE_VI_YANKMARK
+ else if (*p == '\'') { // is this a mark addr
p++;
c = tolower(*p);
p++;
if (c >= 'a' && c <= 'z') {
// we have a mark
c = c - 'a';
- q = mark[(int) c];
+ q = mark[(unsigned char) c];
if (q != NULL) { // is mark valid
- *addr = count_lines(text, q); // count lines
+ *addr = count_lines(text, q);
}
}
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_SEARCH
- } else if (*p == '/') { // a search pattern
- q = buf;
- for (p++; *p; p++) {
- if (*p == '/')
- break;
- *q++ = *p;
- *q = '\0';
- }
- pat = (Byte *) bb_xstrdup((char *) buf); // save copy of pattern
+ }
+#endif
+#if ENABLE_FEATURE_VI_SEARCH
+ else if (*p == '/') { // a search pattern
+ q = strchrnul(++p, '/');
+ pat = xstrndup(p, q - p); // save copy of pattern
+ p = q;
if (*p == '/')
p++;
q = char_search(dot, pat, FORWARD, FULL);
@@ -605,26 +679,27 @@ static Byte *get_one_address(Byte * p, int *addr) // get colon addr, if present
*addr = count_lines(text, q);
}
free(pat);
-#endif /* CONFIG_FEATURE_VI_SEARCH */
- } else if (*p == '$') { // the last line in file
+ }
+#endif
+ else if (*p == '$') { // the last line in file
p++;
q = begin_line(end - 1);
*addr = count_lines(text, q);
} else if (isdigit(*p)) { // specific line number
- sscanf((char *) p, "%d%n", addr, &st);
+ sscanf(p, "%d%n", addr, &st);
p += st;
- } else { // I don't reconise this
- // unrecognised address- assume -1
+ } else {
+ // unrecognised address - assume -1
*addr = -1;
}
- return (p);
+ return p;
}
-static Byte *get_address(Byte *p, int *b, int *e) // get two colon addrs, if present
+static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
{
//----- get the address' i.e., 1,3 'a,'b -----
// get FIRST addr, if present
- while (isblnk(*p))
+ while (isblank(*p))
p++; // skip over leading spaces
if (*p == '%') { // alias for 1,$
p++;
@@ -633,45 +708,46 @@ static Byte *get_address(Byte *p, int *b, int *e) // get two colon addrs, if pre
goto ga0;
}
p = get_one_address(p, b);
- while (isblnk(*p))
+ while (isblank(*p))
p++;
- if (*p == ',') { // is there a address seperator
+ if (*p == ',') { // is there a address separator
p++;
- while (isblnk(*p))
+ while (isblank(*p))
p++;
// get SECOND addr, if present
p = get_one_address(p, e);
}
-ga0:
- while (isblnk(*p))
+ ga0:
+ while (isblank(*p))
p++; // skip over trailing spaces
- return (p);
+ return p;
}
-#ifdef CONFIG_FEATURE_VI_SETOPTS
-static void setops(const Byte *args, const char *opname, int flg_no,
+#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
+static void setops(const char *args, const char *opname, int flg_no,
const char *short_opname, int opt)
{
- const char *a = (char *) args + flg_no;
+ const char *a = args + flg_no;
int l = strlen(opname) - 1; /* opname have + ' ' */
- if (strncasecmp(a, opname, l) == 0 ||
- strncasecmp(a, short_opname, 2) == 0) {
- if(flg_no)
+ if (strncasecmp(a, opname, l) == 0
+ || strncasecmp(a, short_opname, 2) == 0
+ ) {
+ if (flg_no)
vi_setops &= ~opt;
- else
+ else
vi_setops |= opt;
}
}
#endif
-static void colon(Byte * buf)
+// buf must be no longer than MAX_INPUT_LEN!
+static void colon(char *buf)
{
- Byte c, *orig_buf, *buf1, *q, *r;
- Byte *fn, cmd[BUFSIZ], args[BUFSIZ];
- int i, l, li, ch, st, b, e;
- int useforce, forced;
- struct stat st_buf;
+ char c, *orig_buf, *buf1, *q, *r;
+ char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
+ int i, l, li, ch, b, e;
+ int useforce, forced = FALSE;
// :3154 // if (-e line 3154) goto it else stay put
// :4,33w! foo // write a portion of buffer to file "foo"
@@ -687,20 +763,18 @@ static void colon(Byte * buf)
// :s/find/replace/ // substitute pattern "find" with "replace"
// :!<cmd> // run <cmd> then return
//
- if (strlen((char *) buf) <= 0)
+
+ if (!buf[0])
goto vc1;
if (*buf == ':')
buf++; // move past the ':'
- forced = useforce = FALSE;
- li = st = ch = i = 0;
+ li = ch = i = 0;
b = e = -1;
q = text; // assume 1,$ for the range
r = end - 1;
li = count_lines(text, end - 1);
- fn = cfn; // default to current file
- memset(cmd, '\0', BUFSIZ); // clear cmd[]
- memset(args, '\0', BUFSIZ); // clear args[]
+ fn = current_filename;
// look for optional address(es) :. :1 :1,9 :'q,'a :%
buf = get_address(buf, &b, &e);
@@ -715,11 +789,13 @@ static void colon(Byte * buf)
break;
*buf1++ = *buf++;
}
+ *buf1 = '\0';
// get any ARGuments
- while (isblnk(*buf))
+ while (isblank(*buf))
buf++;
- strcpy((char *) args, (char *) buf);
- buf1 = last_char_is((char *)cmd, '!');
+ strcpy(args, buf);
+ useforce = FALSE;
+ buf1 = last_char_is(cmd, '!');
if (buf1) {
useforce = TRUE;
*buf1 = '\0'; // get rid of !
@@ -741,95 +817,60 @@ static void colon(Byte * buf)
li = e - b + 1;
}
// ------------ now look for the command ------------
- i = strlen((char *) cmd);
+ i = strlen(cmd);
if (i == 0) { // :123CR goto line #123
if (b >= 0) {
dot = find_line(b); // what line is #b
dot_skip_over_ws();
}
- } else if (strncmp((char *) cmd, "!", 1) == 0) { // run a cmd
+ }
+#if ENABLE_FEATURE_ALLOW_EXEC
+ else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
+ int retcode;
// :!ls run the <cmd>
- (void) alarm(0); // wait for input- no alarms
- place_cursor(rows - 1, 0, FALSE); // go to Status line
- clear_to_eol(); // clear the line
+ go_bottom_and_clear_to_eol();
cookmode();
- system(orig_buf+1); // run the cmd
+ retcode = system(orig_buf + 1); // run the cmd
+ if (retcode)
+ printf("\nshell returned %i\n\n", retcode);
rawmode();
Hit_Return(); // let user see results
- (void) alarm(3); // done waiting for input
- } else if (strncmp((char *) cmd, "=", i) == 0) { // where is the address
+ }
+#endif
+ else if (strncmp(cmd, "=", i) == 0) { // where is the address
if (b < 0) { // no addr given- use defaults
b = e = count_lines(text, dot);
}
- psb("%d", b);
- } else if (strncasecmp((char *) cmd, "delete", i) == 0) { // delete lines
+ status_line("%d", b);
+ } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
if (b < 0) { // no addr given- use defaults
q = begin_line(dot); // assume .,. for the range
r = end_line(dot);
}
dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
dot_skip_over_ws();
- } else if (strncasecmp((char *) cmd, "edit", i) == 0) { // Edit a file
- int sr;
- sr= 0;
+ } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
// don't edit, if the current file has been modified
- if (file_modified && ! useforce) {
- psbs("No write since last change (:edit! overrides)");
+ if (file_modified && !useforce) {
+ status_line_bold("No write since last change (:edit! overrides)");
goto vc1;
}
- if (strlen(args) > 0) {
+ if (args[0]) {
// the user supplied a file name
- fn= args;
- } else if (cfn != 0 && strlen(cfn) > 0) {
+ fn = args;
+ } else if (current_filename && current_filename[0]) {
// no user supplied name- use the current filename
- fn= cfn;
- goto vc5;
+ // fn = current_filename; was set by default
} else {
// no user file name, no current name- punt
- psbs("No current filename");
+ status_line_bold("No current filename");
goto vc1;
}
- // see if file exists- if not, its just a new file request
- if ((sr=stat((char*)fn, &st_buf)) < 0) {
- // This is just a request for a new file creation.
- // The file_insert below will fail but we get
- // an empty buffer with a file name. Then the "write"
- // command can do the create.
- } else {
- if ((st_buf.st_mode & (S_IFREG)) == 0) {
- // This is not a regular file
- psbs("\"%s\" is not a regular file", fn);
- goto vc1;
- }
- if ((st_buf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) {
- // dont have any read permissions
- psbs("\"%s\" is not readable", fn);
- goto vc1;
- }
- }
-
- // There is a read-able regular file
- // make this the current file
- q = (Byte *) bb_xstrdup((char *) fn); // save the cfn
- free(cfn); // free the old name
- cfn = q; // remember new cfn
-
- vc5:
- // delete all the contents of text[]
- new_text(2 * file_size(fn));
- screenbegin = dot = end = text;
-
- // insert new file
- ch = file_insert(fn, text, file_size(fn));
+ if (init_text_buffer(fn) < 0)
+ goto vc1;
- if (ch < 1) {
- // start empty buf with dummy line
- (void) char_insert(text, '\n');
- ch= 1;
- }
- file_modified = FALSE;
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
free(reg[Ureg]); // free orig line reg- for 'U'
reg[Ureg]= 0;
@@ -838,79 +879,73 @@ static void colon(Byte * buf)
free(reg[YDreg]); // free default yank/delete register
reg[YDreg]= 0;
}
- for (li = 0; li < 28; li++) {
- mark[li] = 0;
- } // init the marks
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
// how many lines in text[]?
li = count_lines(text, end - 1);
- psb("\"%s\"%s"
-#ifdef CONFIG_FEATURE_VI_READONLY
- "%s"
-#endif /* CONFIG_FEATURE_VI_READONLY */
- " %dL, %dC", cfn,
- (sr < 0 ? " [New file]" : ""),
-#ifdef CONFIG_FEATURE_VI_READONLY
- ((vi_readonly || readonly) ? " [Read only]" : ""),
-#endif /* CONFIG_FEATURE_VI_READONLY */
+ status_line("\"%s\"%s"
+ USE_FEATURE_VI_READONLY("%s")
+ " %dL, %dC", current_filename,
+ (file_size(fn) < 0 ? " [New file]" : ""),
+ USE_FEATURE_VI_READONLY(
+ ((readonly_mode) ? " [Readonly]" : ""),
+ )
li, ch);
- } else if (strncasecmp((char *) cmd, "file", i) == 0) { // what File is this
+ } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
if (b != -1 || e != -1) {
- ni((Byte *) "No address allowed on this command");
+ not_implemented("No address allowed on this command");
goto vc1;
}
- if (strlen((char *) args) > 0) {
+ if (args[0]) {
// user wants a new filename
- free(cfn);
- cfn = (Byte *) bb_xstrdup((char *) args);
+ free(current_filename);
+ current_filename = xstrdup(args);
} else {
// user wants file status info
- edit_status();
+ last_status_cksum = 0; // force status update
}
- } else if (strncasecmp((char *) cmd, "features", i) == 0) { // what features are available
+ } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
// print out values of all features
- place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
- clear_to_eol(); // clear the line
+ go_bottom_and_clear_to_eol();
cookmode();
show_help();
rawmode();
Hit_Return();
- } else if (strncasecmp((char *) cmd, "list", i) == 0) { // literal print line
+ } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
if (b < 0) { // no addr given- use defaults
q = begin_line(dot); // assume .,. for the range
r = end_line(dot);
}
- place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
- clear_to_eol(); // clear the line
+ go_bottom_and_clear_to_eol();
puts("\r");
for (; q <= r; q++) {
int c_is_no_print;
c = *q;
- c_is_no_print = c > 127 && !Isprint(c);
+ c_is_no_print = (c & 0x80) && !Isprint(c);
if (c_is_no_print) {
c = '.';
standout_start();
- }
+ }
if (c == '\n') {
write1("$\r");
} else if (c < ' ' || c == 127) {
- putchar('^');
- if(c == 127)
+ bb_putchar('^');
+ if (c == 127)
c = '?';
- else
- c += '@';
+ else
+ c += '@';
}
- putchar(c);
+ bb_putchar(c);
if (c_is_no_print)
standout_end();
}
-#ifdef CONFIG_FEATURE_VI_SET
- vc2:
-#endif /* CONFIG_FEATURE_VI_SET */
+#if ENABLE_FEATURE_VI_SET
+ vc2:
+#endif
Hit_Return();
- } else if ((strncasecmp((char *) cmd, "quit", i) == 0) || // Quit
- (strncasecmp((char *) cmd, "next", i) == 0)) { // edit next file
+ } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
+ || strncasecmp(cmd, "next", i) == 0 // edit next file
+ ) {
if (useforce) {
// force end of argv list
if (*cmd == 'q') {
@@ -921,24 +956,24 @@ static void colon(Byte * buf)
}
// don't exit if the file been modified
if (file_modified) {
- psbs("No write since last change (:%s! overrides)",
+ status_line_bold("No write since last change (:%s! overrides)",
(*cmd == 'q' ? "quit" : "next"));
goto vc1;
}
// are there other file to edit
if (*cmd == 'q' && optind < save_argc - 1) {
- psbs("%d more file to edit", (save_argc - optind - 1));
+ status_line_bold("%d more file to edit", (save_argc - optind - 1));
goto vc1;
}
if (*cmd == 'n' && optind >= save_argc - 1) {
- psbs("No more files to edit");
+ status_line_bold("No more files to edit");
goto vc1;
}
editing = 0;
- } else if (strncasecmp((char *) cmd, "read", i) == 0) { // read file into text[]
+ } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
fn = args;
- if (strlen((char *) fn) <= 0) {
- psbs("No filename given");
+ if (!fn[0]) {
+ status_line_bold("No filename given");
goto vc1;
}
if (b < 0) { // no addr given- use defaults
@@ -947,49 +982,46 @@ static void colon(Byte * buf)
// read after current line- unless user said ":0r foo"
if (b != 0)
q = next_line(q);
-#ifdef CONFIG_FEATURE_VI_READONLY
- l= readonly; // remember current files' status
-#endif
- ch = file_insert(fn, q, file_size(fn));
-#ifdef CONFIG_FEATURE_VI_READONLY
- readonly= l;
-#endif
+ { // dance around potentially-reallocated text[]
+ uintptr_t ofs = q - text;
+ ch = file_insert(fn, q, 0);
+ q = text + ofs;
+ }
if (ch < 0)
goto vc1; // nothing was inserted
// how many lines in text[]?
li = count_lines(q, q + ch - 1);
- psb("\"%s\""
-#ifdef CONFIG_FEATURE_VI_READONLY
- "%s"
-#endif /* CONFIG_FEATURE_VI_READONLY */
+ status_line("\"%s\""
+ USE_FEATURE_VI_READONLY("%s")
" %dL, %dC", fn,
-#ifdef CONFIG_FEATURE_VI_READONLY
- ((vi_readonly || readonly) ? " [Read only]" : ""),
-#endif /* CONFIG_FEATURE_VI_READONLY */
+ USE_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
li, ch);
if (ch > 0) {
// if the insert is before "dot" then we need to update
if (q <= dot)
dot += ch;
- file_modified = TRUE;
+ /*file_modified++; - done by file_insert */
}
- } else if (strncasecmp((char *) cmd, "rewind", i) == 0) { // rewind cmd line args
- if (file_modified && ! useforce) {
- psbs("No write since last change (:rewind! overrides)");
+ } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
+ if (file_modified && !useforce) {
+ status_line_bold("No write since last change (:rewind! overrides)");
} else {
// reset the filenames to edit
optind = fn_start - 1;
editing = 0;
}
-#ifdef CONFIG_FEATURE_VI_SET
- } else if (strncasecmp((char *) cmd, "set", i) == 0) { // set or clear features
+#if ENABLE_FEATURE_VI_SET
+ } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
+#if ENABLE_FEATURE_VI_SETOPTS
+ char *argp;
+#endif
i = 0; // offset into args
- if (strlen((char *) args) == 0) {
+ // only blank is regarded as args delmiter. What about tab '\t' ?
+ if (!args[0] || strcasecmp(args, "all") == 0) {
// print out values of all options
- place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
- clear_to_eol(); // clear the line
+ go_bottom_and_clear_to_eol();
printf("----------------------------------------\r\n");
-#ifdef CONFIG_FEATURE_VI_SETOPTS
+#if ENABLE_FEATURE_VI_SETOPTS
if (!autoindent)
printf("no");
printf("autoindent ");
@@ -1003,27 +1035,35 @@ static void colon(Byte * buf)
printf("no");
printf("showmatch ");
printf("tabstop=%d ", tabstop);
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
+#endif
printf("\r\n");
goto vc2;
}
- if (strncasecmp((char *) args, "no", 2) == 0)
- i = 2; // ":set noautoindent"
-#ifdef CONFIG_FEATURE_VI_SETOPTS
- setops(args, "autoindent ", i, "ai", VI_AUTOINDENT);
- setops(args, "flash ", i, "fl", VI_ERR_METHOD);
- setops(args, "ignorecase ", i, "ic", VI_IGNORECASE);
- setops(args, "showmatch ", i, "ic", VI_SHOWMATCH);
- if (strncasecmp((char *) args + i, "tabstop=%d ", 7) == 0) {
- sscanf(strchr((char *) args + i, '='), "=%d", &ch);
- if (ch > 0 && ch < columns - 1)
- tabstop = ch;
- }
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
-#endif /* CONFIG_FEATURE_VI_SET */
-#ifdef CONFIG_FEATURE_VI_SEARCH
- } else if (strncasecmp((char *) cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
- Byte *ls, *F, *R;
+#if ENABLE_FEATURE_VI_SETOPTS
+ argp = args;
+ while (*argp) {
+ if (strncasecmp(argp, "no", 2) == 0)
+ i = 2; // ":set noautoindent"
+ setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
+ setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
+ setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
+ setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
+ /* tabstopXXXX */
+ if (strncasecmp(argp + i, "tabstop=%d ", 7) == 0) {
+ sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
+ if (ch > 0 && ch <= MAX_TABSTOP)
+ tabstop = ch;
+ }
+ while (*argp && *argp != ' ')
+ argp++; // skip to arg delimiter (i.e. blank)
+ while (*argp && *argp == ' ')
+ argp++; // skip all delimiting blanks
+ }
+#endif /* FEATURE_VI_SETOPTS */
+#endif /* FEATURE_VI_SET */
+#if ENABLE_FEATURE_VI_SEARCH
+ } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
+ char *ls, *F, *R;
int gflag;
// F points to the "find" pattern
@@ -1032,11 +1072,13 @@ static void colon(Byte * buf)
gflag = 0; // global replace flag
c = orig_buf[1]; // what is the delimiter
F = orig_buf + 2; // start of "find"
- R = (Byte *) strchr((char *) F, c); // middle delimiter
- if (!R) goto colon_s_fail;
+ R = strchr(F, c); // middle delimiter
+ if (!R)
+ goto colon_s_fail;
*R++ = '\0'; // terminate "find"
- buf1 = (Byte *) strchr((char *) R, c);
- if (!buf1) goto colon_s_fail;
+ buf1 = strchr(R, c);
+ if (!buf1)
+ goto colon_s_fail;
*buf1++ = '\0'; // terminate "replace"
if (*buf1 == 'g') { // :s/foo/bar/g
buf1++;
@@ -1051,39 +1093,45 @@ static void colon(Byte * buf)
e = b; // maybe :.s/foo/bar/
for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
ls = q; // orig line start
- vc4:
+ vc4:
buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
- if (buf1 != NULL) {
- // we found the "find" pattern- delete it
- (void) text_hole_delete(buf1, buf1 + strlen((char *) F) - 1);
+ if (buf1) {
+ uintptr_t bias;
+ // we found the "find" pattern - delete it
+ text_hole_delete(buf1, buf1 + strlen(F) - 1);
// inset the "replace" patern
- (void) string_insert(buf1, R); // insert the string
+ bias = string_insert(buf1, R); // insert the string
+ buf1 += bias;
+ ls += bias;
+ /*q += bias; - recalculated anyway */
// check for "global" :s/foo/bar/g
if (gflag == 1) {
- if ((buf1 + strlen((char *) R)) < end_line(ls)) {
- q = buf1 + strlen((char *) R);
+ if ((buf1 + strlen(R)) < end_line(ls)) {
+ q = buf1 + strlen(R);
goto vc4; // don't let q move past cur line
}
}
}
q = next_line(ls);
}
-#endif /* CONFIG_FEATURE_VI_SEARCH */
- } else if (strncasecmp((char *) cmd, "version", i) == 0) { // show software version
- psb("%s", vi_Version);
- } else if ((strncasecmp((char *) cmd, "write", i) == 0) || // write text to file
- (strncasecmp((char *) cmd, "wq", i) == 0) ||
- (strncasecmp((char *) cmd, "x", i) == 0)) {
+#endif /* FEATURE_VI_SEARCH */
+ } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
+ status_line(BB_VER " " BB_BT);
+ } else if (strncasecmp(cmd, "write", i) == 0 // write text to file
+ || strncasecmp(cmd, "wq", i) == 0
+ || strncasecmp(cmd, "wn", i) == 0
+ || strncasecmp(cmd, "x", i) == 0
+ ) {
// is there a file name to write to?
- if (strlen((char *) args) > 0) {
+ if (args[0]) {
fn = args;
}
-#ifdef CONFIG_FEATURE_VI_READONLY
- if ((vi_readonly || readonly) && ! useforce) {
- psbs("\"%s\" File is read only", fn);
+#if ENABLE_FEATURE_VI_READONLY
+ if (readonly_mode && !useforce) {
+ status_line_bold("\"%s\" File is read only", fn);
goto vc3;
}
-#endif /* CONFIG_FEATURE_VI_READONLY */
+#endif
// how many lines in text[]?
li = count_lines(q, r);
ch = r - q + 1;
@@ -1101,71 +1149,81 @@ static void colon(Byte * buf)
// system(syscmd);
forced = FALSE;
}
- psb("\"%s\" %dL, %dC", fn, li, l);
- if (q == text && r == end - 1 && l == ch)
- file_modified = FALSE;
- if ((cmd[0] == 'x' || cmd[1] == 'q') && l == ch) {
- editing = 0;
+ if (l < 0) {
+ if (l == -1)
+ status_line_bold("\"%s\" %s", fn, strerror(errno));
+ } else {
+ status_line("\"%s\" %dL, %dC", fn, li, l);
+ if (q == text && r == end - 1 && l == ch) {
+ file_modified = 0;
+ last_file_modified = -1;
+ }
+ if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
+ cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
+ && l == ch) {
+ editing = 0;
+ }
}
-#ifdef CONFIG_FEATURE_VI_READONLY
- vc3:;
-#endif /* CONFIG_FEATURE_VI_READONLY */
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- } else if (strncasecmp((char *) cmd, "yank", i) == 0) { // yank lines
+#if ENABLE_FEATURE_VI_READONLY
+ vc3:;
+#endif
+#if ENABLE_FEATURE_VI_YANKMARK
+ } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
if (b < 0) { // no addr given- use defaults
q = begin_line(dot); // assume .,. for the range
r = end_line(dot);
}
text_yank(q, r, YDreg);
li = count_lines(q, r);
- psb("Yank %d lines (%d chars) into [%c]",
- li, strlen((char *) reg[YDreg]), what_reg());
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+ status_line("Yank %d lines (%d chars) into [%c]",
+ li, strlen(reg[YDreg]), what_reg());
+#endif
} else {
// cmd unknown
- ni((Byte *) cmd);
+ not_implemented(cmd);
}
- vc1:
+ vc1:
dot = bound_dot(dot); // make sure "dot" is valid
return;
-#ifdef CONFIG_FEATURE_VI_SEARCH
-colon_s_fail:
- psb(":s expression missing delimiters");
+#if ENABLE_FEATURE_VI_SEARCH
+ colon_s_fail:
+ status_line(":s expression missing delimiters");
#endif
}
+#endif /* FEATURE_VI_COLON */
+
static void Hit_Return(void)
{
- char c;
+ int c;
- standout_start(); // start reverse video
+ standout_start();
write1("[Hit return to continue]");
- standout_end(); // end reverse video
- while ((c = get_one_char()) != '\n' && c != '\r') /*do nothing */
- ;
+ standout_end();
+ while ((c = get_one_char()) != '\n' && c != '\r')
+ continue;
redraw(TRUE); // force redraw all
}
-#endif /* CONFIG_FEATURE_VI_COLON */
+
+static int next_tabstop(int col)
+{
+ return col + ((tabstop - 1) - (col % tabstop));
+}
//----- Synchronize the cursor to Dot --------------------------
-static void sync_cursor(Byte * d, int *row, int *col)
+static void sync_cursor(char *d, int *row, int *col)
{
- Byte *beg_cur, *end_cur; // begin and end of "d" line
- Byte *beg_scr, *end_scr; // begin and end of screen
- Byte *tp;
+ char *beg_cur; // begin and end of "d" line
+ char *tp;
int cnt, ro, co;
beg_cur = begin_line(d); // first char of cur line
- end_cur = end_line(d); // last char of cur line
-
- beg_scr = end_scr = screenbegin; // first char of screen
- end_scr = end_screen(); // last char of screen
if (beg_cur < screenbegin) {
- // "d" is before top line on screen
+ // "d" is before top line on screen
// how many lines do we have to move
cnt = count_lines(beg_cur, screenbegin);
- sc1:
+ sc1:
screenbegin = beg_cur;
if (cnt > (rows - 1) / 2) {
// we moved too many lines. put "dot" in middle of screen
@@ -1173,18 +1231,22 @@ static void sync_cursor(Byte * d, int *row, int *col)
screenbegin = prev_line(screenbegin);
}
}
- } else if (beg_cur > end_scr) {
- // "d" is after bottom line on screen
- // how many lines do we have to move
- cnt = count_lines(end_scr, beg_cur);
- if (cnt > (rows - 1) / 2)
- goto sc1; // too many lines
- for (ro = 0; ro < cnt - 1; ro++) {
- // move screen begin the same amount
- screenbegin = next_line(screenbegin);
- // now, move the end of screen
- end_scr = next_line(end_scr);
- end_scr = end_line(end_scr);
+ } else {
+ char *end_scr; // begin and end of screen
+ end_scr = end_screen(); // last char of screen
+ if (beg_cur > end_scr) {
+ // "d" is after bottom line on screen
+ // how many lines do we have to move
+ cnt = count_lines(end_scr, beg_cur);
+ if (cnt > (rows - 1) / 2)
+ goto sc1; // too many lines
+ for (ro = 0; ro < cnt - 1; ro++) {
+ // move screen begin the same amount
+ screenbegin = next_line(screenbegin);
+ // now, move the end of screen
+ end_scr = next_line(end_scr);
+ end_scr = end_line(end_scr);
+ }
}
}
// "d" is on screen- find out which row
@@ -1197,16 +1259,21 @@ static void sync_cursor(Byte * d, int *row, int *col)
// find out what col "d" is on
co = 0;
- do { // drive "co" to correct column
- if (*tp == '\n' || *tp == '\0')
+ while (tp < d) { // drive "co" to correct column
+ if (*tp == '\n') //vda || *tp == '\0')
break;
if (*tp == '\t') {
- // 7 - (co % 8 )
- co += ((tabstop - 1) - (co % tabstop));
- } else if (*tp < ' ' || *tp == 127) {
- co++; // display as ^X, use 2 columns
+ // handle tabs like real vi
+ if (d == tp && cmd_mode) {
+ break;
+ }
+ co = next_tabstop(co);
+ } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
+ co++; // display as ^X, use 2 columns
}
- } while (tp++ < d && ++co);
+ co++;
+ tp++;
+ }
// "co" is the column where "dot" is.
// The screen has "columns" columns.
@@ -1239,51 +1306,57 @@ static void sync_cursor(Byte * d, int *row, int *col)
}
//----- Text Movement Routines ---------------------------------
-static Byte *begin_line(Byte * p) // return pointer to first char cur line
+static char *begin_line(char *p) // return pointer to first char cur line
{
- while (p > text && p[-1] != '\n')
- p--; // go to cur line B-o-l
- return (p);
+ if (p > text) {
+ p = memrchr(text, '\n', p - text);
+ if (!p)
+ return text;
+ return p + 1;
+ }
+ return p;
}
-static Byte *end_line(Byte * p) // return pointer to NL of cur line line
+static char *end_line(char *p) // return pointer to NL of cur line
{
- while (p < end - 1 && *p != '\n')
- p++; // go to cur line E-o-l
- return (p);
+ if (p < end - 1) {
+ p = memchr(p, '\n', end - p - 1);
+ if (!p)
+ return end - 1;
+ }
+ return p;
}
-static inline Byte *dollar_line(Byte * p) // return pointer to just before NL line
+static char *dollar_line(char *p) // return pointer to just before NL line
{
- while (p < end - 1 && *p != '\n')
- p++; // go to cur line E-o-l
+ p = end_line(p);
// Try to stay off of the Newline
if (*p == '\n' && (p - begin_line(p)) > 0)
p--;
- return (p);
+ return p;
}
-static Byte *prev_line(Byte * p) // return pointer first char prev line
+static char *prev_line(char *p) // return pointer first char prev line
{
p = begin_line(p); // goto begining of cur line
- if (p[-1] == '\n' && p > text)
+ if (p > text && p[-1] == '\n')
p--; // step to prev line
p = begin_line(p); // goto begining of prev line
- return (p);
+ return p;
}
-static Byte *next_line(Byte * p) // return pointer first char next line
+static char *next_line(char *p) // return pointer first char next line
{
p = end_line(p);
- if (*p == '\n' && p < end - 1)
+ if (p < end - 1 && *p == '\n')
p++; // step to next line
- return (p);
+ return p;
}
//----- Text Information Routines ------------------------------
-static Byte *end_screen(void)
+static char *end_screen(void)
{
- Byte *q;
+ char *q;
int cnt;
// find new bottom line
@@ -1291,36 +1364,39 @@ static Byte *end_screen(void)
for (cnt = 0; cnt < rows - 2; cnt++)
q = next_line(q);
q = end_line(q);
- return (q);
+ return q;
}
-static int count_lines(Byte * start, Byte * stop) // count line from start to stop
+// count line from start to stop
+static int count_lines(char *start, char *stop)
{
- Byte *q;
+ char *q;
int cnt;
- if (stop < start) { // start and stop are backwards- reverse them
+ if (stop < start) { // start and stop are backwards- reverse them
q = start;
start = stop;
stop = q;
}
cnt = 0;
- stop = end_line(stop); // get to end of this line
- for (q = start; q <= stop && q <= end - 1; q++) {
- if (*q == '\n')
+ stop = end_line(stop);
+ while (start <= stop && start <= end - 1) {
+ start = end_line(start);
+ if (*start == '\n')
cnt++;
+ start++;
}
- return (cnt);
+ return cnt;
}
-static Byte *find_line(int li) // find begining of line #li
+static char *find_line(int li) // find begining of line #li
{
- Byte *q;
+ char *q;
for (q = text; li > 1; li--) {
q = next_line(q);
}
- return (q);
+ return q;
}
//----- Dot Movement Routines ----------------------------------
@@ -1346,23 +1422,24 @@ static void dot_end(void)
dot = end_line(dot); // return pointer to last char cur line
}
-static Byte *move_to_col(Byte * p, int l)
+static char *move_to_col(char *p, int l)
{
int co;
p = begin_line(p);
co = 0;
- do {
- if (*p == '\n' || *p == '\0')
+ while (co < l && p < end) {
+ if (*p == '\n') //vda || *p == '\0')
break;
if (*p == '\t') {
- // 7 - (co % 8 )
- co += ((tabstop - 1) - (co % tabstop));
+ co = next_tabstop(co);
} else if (*p < ' ' || *p == 127) {
- co++; // display as ^X, use 2 columns
+ co++; // display as ^X, use 2 columns
}
- } while (++co <= l && p++ < end);
- return (p);
+ co++;
+ p++;
+ }
+ return p;
}
static void dot_next(void)
@@ -1377,16 +1454,16 @@ static void dot_prev(void)
static void dot_scroll(int cnt, int dir)
{
- Byte *q;
+ char *q;
for (; cnt > 0; cnt--) {
if (dir < 0) {
// scroll Backwards
- // ctrl-Y scroll up one line
+ // ctrl-Y scroll up one line
screenbegin = prev_line(screenbegin);
} else {
// scroll Forwards
- // ctrl-E scroll down one line
+ // ctrl-E scroll down one line
screenbegin = next_line(screenbegin);
}
}
@@ -1408,10 +1485,10 @@ static void dot_skip_over_ws(void)
static void dot_delete(void) // delete the char at 'dot'
{
- (void) text_hole_delete(dot, dot);
+ text_hole_delete(dot, dot);
}
-static Byte *bound_dot(Byte * p) // make sure text[0] <= P < "end"
+static char *bound_dot(char *p) // make sure text[0] <= P < "end"
{
if (p >= end && end > text) {
p = end - 1;
@@ -1421,7 +1498,7 @@ static Byte *bound_dot(Byte * p) // make sure text[0] <= P < "end"
p = text;
indicate_error('2');
}
- return (p);
+ return p;
}
//----- Helper Utility Routines --------------------------------
@@ -1437,63 +1514,49 @@ static Byte *bound_dot(Byte * p) // make sure text[0] <= P < "end"
* DO NOT COUNT NEWLINE AS WHITESPACE
*/
-static Byte *new_screen(int ro, int co)
+static char *new_screen(int ro, int co)
{
int li;
free(screen);
screensize = ro * co + 8;
- screen = (Byte *) xmalloc(screensize);
+ screen = xmalloc(screensize);
// initialize the new screen. assume this will be a empty file.
screen_erase();
- // non-existant text[] lines start with a tilde (~).
+ // non-existent text[] lines start with a tilde (~).
for (li = 1; li < ro - 1; li++) {
screen[(li * co) + 0] = '~';
}
- return (screen);
-}
-
-static Byte *new_text(int size)
-{
- if (size < 10240)
- size = 10240; // have a minimum size for new files
- free(text);
- text = (Byte *) xmalloc(size + 8);
- memset(text, '\0', size); // clear new text[]
- //text += 4; // leave some room for "oops"
- textend = text + size - 1;
- //textend -= 4; // leave some root for "oops"
- return (text);
+ return screen;
}
-#ifdef CONFIG_FEATURE_VI_SEARCH
-static int mycmp(Byte * s1, Byte * s2, int len)
+#if ENABLE_FEATURE_VI_SEARCH
+static int mycmp(const char *s1, const char *s2, int len)
{
int i;
- i = strncmp((char *) s1, (char *) s2, len);
-#ifdef CONFIG_FEATURE_VI_SETOPTS
- if (ignorecase) {
- i = strncasecmp((char *) s1, (char *) s2, len);
+ i = strncmp(s1, s2, len);
+ if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
+ i = strncasecmp(s1, s2, len);
}
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
- return (i);
+ return i;
}
-static Byte *char_search(Byte * p, Byte * pat, int dir, int range) // search for pattern starting at p
+// search for pattern starting at p
+static char *char_search(char *p, const char *pat, int dir, int range)
{
#ifndef REGEX_SEARCH
- Byte *start, *stop;
+ char *start, *stop;
int len;
- len = strlen((char *) pat);
+ len = strlen(pat);
if (dir == FORWARD) {
stop = end - 1; // assume range is p - end-1
if (range == LIMITED)
stop = next_line(p); // range is to next line
for (start = p; start < stop; start++) {
if (mycmp(start, pat, len) == 0) {
- return (start);
+ return start;
}
}
} else if (dir == BACK) {
@@ -1502,13 +1565,13 @@ static Byte *char_search(Byte * p, Byte * pat, int dir, int range) // search for
stop = prev_line(p); // range is to prev line
for (start = p - len; start >= stop; start--) {
if (mycmp(start, pat, len) == 0) {
- return (start);
+ return start;
}
}
}
// pattern not found
- return (NULL);
-#else /*REGEX_SEARCH */
+ return NULL;
+#else /* REGEX_SEARCH */
char *q;
struct re_pattern_buffer preg;
int i;
@@ -1535,10 +1598,10 @@ static Byte *char_search(Byte * p, Byte * pat, int dir, int range) // search for
// RANGE could be negative if we are searching backwards
range = q - p;
- q = (char *) re_compile_pattern(pat, strlen((char *) pat), &preg);
+ q = re_compile_pattern(pat, strlen(pat), &preg);
if (q != 0) {
// The pattern was not compiled
- psbs("bad search pattern: \"%s\": %s", pat, q);
+ status_line_bold("bad search pattern: \"%s\": %s", pat, q);
i = 0; // return p if pattern not compiled
goto cs1;
}
@@ -1562,91 +1625,87 @@ static Byte *char_search(Byte * p, Byte * pat, int dir, int range) // search for
p = 0;
i = 0; // return NULL if pattern not found
}
- cs1:
+ cs1:
if (dir == FORWARD) {
p = p + i;
} else {
p = p - i;
}
- return (p);
-#endif /*REGEX_SEARCH */
+ return p;
+#endif /* REGEX_SEARCH */
}
-#endif /* CONFIG_FEATURE_VI_SEARCH */
+#endif /* FEATURE_VI_SEARCH */
-static Byte *char_insert(Byte * p, Byte c) // insert the char c at 'p'
+static char *char_insert(char *p, char c) // insert the char c at 'p'
{
if (c == 22) { // Is this an ctrl-V?
- p = stupid_insert(p, '^'); // use ^ to indicate literal next
- p--; // backup onto ^
+ p += stupid_insert(p, '^'); // use ^ to indicate literal next
refresh(FALSE); // show the ^
c = get_one_char();
*p = c;
p++;
- file_modified = TRUE; // has the file been modified
+ file_modified++;
} else if (c == 27) { // Is this an ESC?
cmd_mode = 0;
cmdcnt = 0;
end_cmd_q(); // stop adding to q
- strcpy((char *) status_buffer, " "); // clear the status buffer
- if ((p[-1] != '\n') && (dot>text)) {
+ last_status_cksum = 0; // force status update
+ if ((p[-1] != '\n') && (dot > text)) {
p--;
}
- } else if (c == erase_char) { // Is this a BS
+ } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
// 123456789
if ((p[-1] != '\n') && (dot>text)) {
p--;
p = text_hole_delete(p, p); // shrink buffer 1 char
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
- // also rmove char from last_modifying_cmd
- if (last_modifying_cmd != 0 && strlen((char *) last_modifying_cmd) > 0) {
- Byte *q;
-
- q = last_modifying_cmd;
- q[strlen((char *) q) - 1] = '\0'; // erase BS
- q[strlen((char *) q) - 1] = '\0'; // erase prev char
- }
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
}
} else {
// insert a char into text[]
- Byte *sp; // "save p"
+ char *sp; // "save p"
if (c == 13)
c = '\n'; // translate \r to \n
sp = p; // remember addr of insert
- p = stupid_insert(p, c); // insert the char
-#ifdef CONFIG_FEATURE_VI_SETOPTS
+ p += 1 + stupid_insert(p, c); // insert the char
+#if ENABLE_FEATURE_VI_SETOPTS
if (showmatch && strchr(")]}", *sp) != NULL) {
showmatching(sp);
}
if (autoindent && c == '\n') { // auto indent the new line
- Byte *q;
-
- q = prev_line(p); // use prev line as templet
- for (; isblnk(*q); q++) {
- p = stupid_insert(p, *q); // insert the char
+ char *q;
+ size_t len;
+ q = prev_line(p); // use prev line as template
+ len = strspn(q, " \t"); // space or tab
+ if (len) {
+ uintptr_t bias;
+ bias = text_hole_make(p, len);
+ p += bias;
+ q += bias;
+ memcpy(p, q, len);
+ p += len;
}
}
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
+#endif
}
- return (p);
+ return p;
}
-static Byte *stupid_insert(Byte * p, Byte c) // stupidly insert the char c at 'p'
+// might reallocate text[]! use p += stupid_insert(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
{
- p = text_hole_make(p, 1);
- if (p != 0) {
- *p = c;
- file_modified = TRUE; // has the file been modified
- p++;
- }
- return (p);
+ uintptr_t bias;
+ bias = text_hole_make(p, 1);
+ p += bias;
+ *p = c;
+ //file_modified++; - done by text_hole_make()
+ return bias;
}
-static Byte find_range(Byte ** start, Byte ** stop, Byte c)
+static int find_range(char **start, char **stop, char c)
{
- Byte *save_dot, *p, *q;
- int cnt;
+ char *save_dot, *p, *q, *t;
+ int cnt, multiline = 0;
save_dot = dot;
p = q = dot;
@@ -1658,13 +1717,18 @@ static Byte find_range(Byte ** start, Byte ** stop, Byte c)
q = next_line(q);
}
q = end_line(q);
- } else if (strchr("^%$0bBeEft", c)) {
+ } else if (strchr("^%$0bBeEfth\b\177", c)) {
// These cmds operate on char positions
do_cmd(c); // execute movement cmd
q = dot;
} else if (strchr("wW", c)) {
do_cmd(c); // execute movement cmd
- if (dot > text)
+ // if we are at the next word's first char
+ // step back one char
+ // but check the possibilities when it is true
+ if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
+ || (ispunct(dot[-1]) && !ispunct(dot[0]))
+ || (isalnum(dot[-1]) && !isalnum(dot[0]))))
dot--; // move back off of next word
if (dot > text && *dot == '\n')
dot--; // stay off NL
@@ -1682,22 +1746,38 @@ static Byte find_range(Byte ** start, Byte ** stop, Byte c)
dot_end(); // find NL
q = dot;
} else {
- c = 27; // error- return an ESC char
- //break;
+ // nothing -- this causes any other values of c to
+ // represent the one-character range under the
+ // cursor. this is correct for ' ' and 'l', but
+ // perhaps no others.
+ //
}
- *start = p;
- *stop = q;
if (q < p) {
- *start = q;
- *stop = p;
+ t = q;
+ q = p;
+ p = t;
}
+
+ // backward char movements don't include start position
+ if (q > p && strchr("^0bBh\b\177", c)) q--;
+
+ multiline = 0;
+ for (t = p; t <= q; t++) {
+ if (*t == '\n') {
+ multiline = 1;
+ break;
+ }
+ }
+
+ *start = p;
+ *stop = q;
dot = save_dot;
- return (c);
+ return multiline;
}
-static int st_test(Byte * p, int type, int dir, Byte * tested)
+static int st_test(char *p, int type, int dir, char *tested)
{
- Byte c, c0, ci;
+ char c, c0, ci;
int test, inc;
inc = dir;
@@ -1726,12 +1806,12 @@ static int st_test(Byte * p, int type, int dir, Byte * tested)
test = ((isalnum(c)) || c == '_');
}
*tested = c;
- return (test);
+ return test;
}
-static Byte *skip_thing(Byte * p, int linecnt, int dir, int type)
+static char *skip_thing(char *p, int linecnt, int dir, int type)
{
- Byte c;
+ char c;
while (st_test(p, type, dir, &c)) {
// make sure we limit search to correct number of lines
@@ -1743,40 +1823,25 @@ static Byte *skip_thing(Byte * p, int linecnt, int dir, int type)
break;
p += dir; // move to next char
}
- return (p);
+ return p;
}
// find matching char of pair () [] {}
-static Byte *find_pair(Byte * p, Byte c)
+static char *find_pair(char *p, const char c)
{
- Byte match, *q;
+ char match, *q;
int dir, level;
match = ')';
level = 1;
dir = 1; // assume forward
switch (c) {
- case '(':
- match = ')';
- break;
- case '[':
- match = ']';
- break;
- case '{':
- match = '}';
- break;
- case ')':
- match = '(';
- dir = -1;
- break;
- case ']':
- match = '[';
- dir = -1;
- break;
- case '}':
- match = '{';
- dir = -1;
- break;
+ case '(': match = ')'; break;
+ case '[': match = ']'; break;
+ case '{': match = '}'; break;
+ case ')': match = '('; dir = -1; break;
+ case ']': match = '['; dir = -1; break;
+ case '}': match = '{'; dir = -1; break;
}
for (q = p + dir; text <= q && q < end; q += dir) {
// look for match, count levels of pairs (( ))
@@ -1789,14 +1854,14 @@ static Byte *find_pair(Byte * p, Byte c)
}
if (level != 0)
q = NULL; // indicate no match
- return (q);
+ return q;
}
-#ifdef CONFIG_FEATURE_VI_SETOPTS
+#if ENABLE_FEATURE_VI_SETOPTS
// show the matching char of a pair, () [] {}
-static void showmatching(Byte * p)
+static void showmatching(char *p)
{
- Byte *q, *save_dot;
+ char *q, *save_dot;
// we found half of a pair
q = find_pair(p, *p); // get loc of matching char
@@ -1807,38 +1872,44 @@ static void showmatching(Byte * p)
save_dot = dot; // remember where we are
dot = q; // go to new loc
refresh(FALSE); // let the user see it
- (void) mysleep(40); // give user some time
+ mysleep(40); // give user some time
dot = save_dot; // go back to old loc
refresh(FALSE);
}
}
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
+#endif /* FEATURE_VI_SETOPTS */
-// open a hole in text[]
-static Byte *text_hole_make(Byte * p, int size) // at "p", make a 'size' byte hole
+// open a hole in text[]
+// might reallocate text[]! use p += text_hole_make(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
{
- Byte *src, *dest;
- int cnt;
+ uintptr_t bias = 0;
if (size <= 0)
- goto thm0;
- src = p;
- dest = p + size;
- cnt = end - src; // the rest of buffer
- if (memmove(dest, src, cnt) != dest) {
- psbs("can't create room for new characters");
- }
+ return bias;
+ end += size; // adjust the new END
+ if (end >= (text + text_size)) {
+ char *new_text;
+ text_size += end - (text + text_size) + 10240;
+ new_text = xrealloc(text, text_size);
+ bias = (new_text - text);
+ screenbegin += bias;
+ dot += bias;
+ end += bias;
+ p += bias;
+ text = new_text;
+ }
+ memmove(p + size, p, end - size - p);
memset(p, ' ', size); // clear new hole
- end = end + size; // adjust the new END
- file_modified = TRUE; // has the file been modified
- thm0:
- return (p);
+ file_modified++;
+ return bias;
}
// close a hole in text[]
-static Byte *text_hole_delete(Byte * p, Byte * q) // delete "p" thru "q", inclusive
+static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
{
- Byte *src, *dest;
+ char *src, *dest;
int cnt, hole_size;
// move forwards, from beginning
@@ -1857,26 +1928,24 @@ static Byte *text_hole_delete(Byte * p, Byte * q) // delete "p" thru "q", inclus
goto thd0;
if (src >= end)
goto thd_atend; // just delete the end of the buffer
- if (memmove(dest, src, cnt) != dest) {
- psbs("can't delete the character");
- }
- thd_atend:
+ memmove(dest, src, cnt);
+ thd_atend:
end = end - hole_size; // adjust the new END
if (dest >= end)
dest = end - 1; // make sure dest in below end-1
if (end <= text)
dest = end = text; // keep pointers valid
- file_modified = TRUE; // has the file been modified
- thd0:
- return (dest);
+ file_modified++;
+ thd0:
+ return dest;
}
// copy text into register, then delete text.
// if dist <= 0, do not include, or go past, a NewLine
//
-static Byte *yank_delete(Byte * start, Byte * stop, int dist, int yf)
+static char *yank_delete(char *start, char *stop, int dist, int yf)
{
- Byte *p;
+ char *p;
// make sure start <= stop
if (start > stop) {
@@ -1886,10 +1955,10 @@ static Byte *yank_delete(Byte * start, Byte * stop, int dist, int yf)
stop = p;
}
if (dist <= 0) {
- // we can not cross NL boundaries
+ // we cannot cross NL boundaries
p = start;
if (*p == '\n')
- return (p);
+ return p;
// dont go past a NewLine
for (; p + 1 <= stop; p++) {
if (p[1] == '\n') {
@@ -1899,172 +1968,133 @@ static Byte *yank_delete(Byte * start, Byte * stop, int dist, int yf)
}
}
p = start;
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
text_yank(start, stop, YDreg);
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
if (yf == YANKDEL) {
p = text_hole_delete(start, stop);
} // delete lines
- return (p);
+ return p;
}
static void show_help(void)
{
puts("These features are available:"
-#ifdef CONFIG_FEATURE_VI_SEARCH
+#if ENABLE_FEATURE_VI_SEARCH
"\n\tPattern searches with / and ?"
-#endif /* CONFIG_FEATURE_VI_SEARCH */
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
+#endif
+#if ENABLE_FEATURE_VI_DOT_CMD
"\n\tLast command repeat with \'.\'"
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- "\n\tLine marking with 'x"
- "\n\tNamed buffers with \"x"
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-#ifdef CONFIG_FEATURE_VI_READONLY
+#endif
+#if ENABLE_FEATURE_VI_YANKMARK
+ "\n\tLine marking with 'x"
+ "\n\tNamed buffers with \"x"
+#endif
+#if ENABLE_FEATURE_VI_READONLY
"\n\tReadonly if vi is called as \"view\""
"\n\tReadonly with -R command line arg"
-#endif /* CONFIG_FEATURE_VI_READONLY */
-#ifdef CONFIG_FEATURE_VI_SET
+#endif
+#if ENABLE_FEATURE_VI_SET
"\n\tSome colon mode commands with \':\'"
-#endif /* CONFIG_FEATURE_VI_SET */
-#ifdef CONFIG_FEATURE_VI_SETOPTS
+#endif
+#if ENABLE_FEATURE_VI_SETOPTS
"\n\tSettable options with \":set\""
-#endif /* CONFIG_FEATURE_VI_SETOPTS */
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
+#endif
+#if ENABLE_FEATURE_VI_USE_SIGNALS
"\n\tSignal catching- ^C"
"\n\tJob suspend and resume with ^Z"
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
+#endif
+#if ENABLE_FEATURE_VI_WIN_RESIZE
"\n\tAdapt to window re-sizes"
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
+#endif
);
}
-static inline void print_literal(Byte * buf, Byte * s) // copy s to buf, convert unprintable
+#if ENABLE_FEATURE_VI_DOT_CMD
+static void start_new_cmd_q(char c)
{
- Byte c, b[2];
-
- b[1] = '\0';
- strcpy((char *) buf, ""); // init buf
- if (strlen((char *) s) <= 0)
- s = (Byte *) "(NULL)";
- for (; *s > '\0'; s++) {
- int c_is_no_print;
-
- c = *s;
- c_is_no_print = c > 127 && !Isprint(c);
- if (c_is_no_print) {
- strcat((char *) buf, SOn);
- c = '.';
- }
- if (c < ' ' || c == 127) {
- strcat((char *) buf, "^");
- if(c == 127)
- c = '?';
- else
- c += '@';
- }
- b[0] = c;
- strcat((char *) buf, (char *) b);
- if (c_is_no_print)
- strcat((char *) buf, SOs);
- if (*s == '\n') {
- strcat((char *) buf, "$");
- }
- }
-}
-
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
-static void start_new_cmd_q(Byte c)
-{
- // release old cmd
- free(last_modifying_cmd);
// get buffer for new cmd
- last_modifying_cmd = (Byte *) xmalloc(BUFSIZ);
- memset(last_modifying_cmd, '\0', BUFSIZ); // clear new cmd queue
// if there is a current cmd count put it in the buffer first
- if (cmdcnt > 0)
- sprintf((char *) last_modifying_cmd, "%d", cmdcnt);
- // save char c onto queue
- last_modifying_cmd[strlen((char *) last_modifying_cmd)] = c;
+ if (cmdcnt > 0) {
+ lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
+ } else { // just save char c onto queue
+ last_modifying_cmd[0] = c;
+ lmc_len = 1;
+ }
adding2q = 1;
- return;
}
static void end_cmd_q(void)
{
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
YDreg = 26; // go back to default Yank/Delete reg
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
adding2q = 0;
- return;
}
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
+#endif /* FEATURE_VI_DOT_CMD */
-#if defined(CONFIG_FEATURE_VI_YANKMARK) || defined(CONFIG_FEATURE_VI_COLON) || defined(CONFIG_FEATURE_VI_CRASHME)
-static Byte *string_insert(Byte * p, Byte * s) // insert the string at 'p'
+#if ENABLE_FEATURE_VI_YANKMARK \
+ || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
+ || ENABLE_FEATURE_VI_CRASHME
+// might reallocate text[]! use p += string_insert(p, ...),
+// and be careful to not use pointers into potentially freed text[]!
+static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
{
- int cnt, i;
+ uintptr_t bias;
+ int i;
- i = strlen((char *) s);
- p = text_hole_make(p, i);
- strncpy((char *) p, (char *) s, i);
- for (cnt = 0; *s != '\0'; s++) {
- if (*s == '\n')
- cnt++;
+ i = strlen(s);
+ bias = text_hole_make(p, i);
+ p += bias;
+ memcpy(p, s, i);
+#if ENABLE_FEATURE_VI_YANKMARK
+ {
+ int cnt;
+ for (cnt = 0; *s != '\0'; s++) {
+ if (*s == '\n')
+ cnt++;
+ }
+ status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
}
-#ifdef CONFIG_FEATURE_VI_YANKMARK
- psb("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
- return (p);
+#endif
+ return bias;
}
-#endif /* CONFIG_FEATURE_VI_YANKMARK || CONFIG_FEATURE_VI_COLON || CONFIG_FEATURE_VI_CRASHME */
+#endif
-#ifdef CONFIG_FEATURE_VI_YANKMARK
-static Byte *text_yank(Byte * p, Byte * q, int dest) // copy text into a register
+#if ENABLE_FEATURE_VI_YANKMARK
+static char *text_yank(char *p, char *q, int dest) // copy text into a register
{
- Byte *t;
- int cnt;
-
- if (q < p) { // they are backwards- reverse them
- t = q;
- q = p;
- p = t;
+ int cnt = q - p;
+ if (cnt < 0) { // they are backwards- reverse them
+ p = q;
+ cnt = -cnt;
}
- cnt = q - p + 1;
- t = reg[dest];
- free(t); // if already a yank register, free it
- t = (Byte *) xmalloc(cnt + 1); // get a new register
- memset(t, '\0', cnt + 1); // clear new text[]
- strncpy((char *) t, (char *) p, cnt); // copy text[] into bufer
- reg[dest] = t;
- return (p);
+ free(reg[dest]); // if already a yank register, free it
+ reg[dest] = xstrndup(p, cnt + 1);
+ return p;
}
-static Byte what_reg(void)
+static char what_reg(void)
{
- Byte c;
- int i;
+ char c;
- i = 0;
c = 'D'; // default to D-reg
if (0 <= YDreg && YDreg <= 25)
- c = 'a' + (Byte) YDreg;
+ c = 'a' + (char) YDreg;
if (YDreg == 26)
c = 'D';
if (YDreg == 27)
c = 'U';
- return (c);
+ return c;
}
-static void check_context(Byte cmd)
+static void check_context(char cmd)
{
// A context is defined to be "modifying text"
// Any modifying command establishes a new context.
if (dot < context_start || dot > context_end) {
- if (strchr((char *) modifying_cmds, cmd) != NULL) {
+ if (strchr(modifying_cmds, cmd) != NULL) {
// we are trying to modify text[]- make this the current context
mark[27] = mark[26]; // move cur to prev
mark[26] = dot; // move local to cur
@@ -2073,12 +2103,11 @@ static void check_context(Byte cmd)
//loiter= start_loiter= now;
}
}
- return;
}
-static inline Byte *swap_context(Byte * p) // goto new context for '' command make this the current context
+static char *swap_context(char *p) // goto new context for '' command make this the current context
{
- Byte *tmp;
+ char *tmp;
// the current context is in mark[26]
// the previous context is in mark[27]
@@ -2091,14 +2120,9 @@ static inline Byte *swap_context(Byte * p) // goto new context for '' command ma
context_start = prev_line(prev_line(prev_line(p)));
context_end = next_line(next_line(next_line(p)));
}
- return (p);
-}
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
-
-static int isblnk(Byte c) // is the char a blank or tab
-{
- return (c == ' ' || c == '\t');
+ return p;
}
+#endif /* FEATURE_VI_YANKMARK */
//----- Set terminal attributes --------------------------------
static void rawmode(void)
@@ -2108,73 +2132,50 @@ static void rawmode(void)
term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
term_vi.c_iflag &= (~IXON & ~ICRNL);
term_vi.c_oflag &= (~ONLCR);
-#ifndef linux
term_vi.c_cc[VMIN] = 1;
term_vi.c_cc[VTIME] = 0;
-#endif
erase_char = term_vi.c_cc[VERASE];
- tcsetattr(0, TCSANOW, &term_vi);
+ tcsetattr_stdin_TCSANOW(&term_vi);
}
static void cookmode(void)
{
fflush(stdout);
- tcsetattr(0, TCSANOW, &term_orig);
-}
-
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
-//----- See what the window size currently is --------------------
-static void window_size_get(int sig)
-{
- int i;
-
- i = ioctl(0, TIOCGWINSZ, &winsize);
- if (i != 0) {
- // force 24x80
- winsize.ws_row = 24;
- winsize.ws_col = 80;
- }
- if (winsize.ws_row <= 1) {
- winsize.ws_row = 24;
- }
- if (winsize.ws_col <= 1) {
- winsize.ws_col = 80;
- }
- rows = (int) winsize.ws_row;
- columns = (int) winsize.ws_col;
+ tcsetattr_stdin_TCSANOW(&term_orig);
}
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
//----- Come here when we get a window resize signal ---------
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
-static void winch_sig(int sig)
+#if ENABLE_FEATURE_VI_USE_SIGNALS
+static void winch_sig(int sig UNUSED_PARAM)
{
+ // FIXME: do it in main loop!!!
signal(SIGWINCH, winch_sig);
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
- window_size_get(0);
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
+ if (ENABLE_FEATURE_VI_WIN_RESIZE) {
+ get_terminal_width_height(0, &columns, &rows);
+ if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
+ if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
+ }
new_screen(rows, columns); // get memory for virtual screen
redraw(TRUE); // re-draw the screen
}
//----- Come here when we get a continue signal -------------------
-static void cont_sig(int sig)
+static void cont_sig(int sig UNUSED_PARAM)
{
- rawmode(); // terminal to "raw"
- *status_buffer = '\0'; // clear the status buffer
- redraw(TRUE); // re-draw the screen
+ rawmode(); // terminal to "raw"
+ last_status_cksum = 0; // force status update
+ redraw(TRUE); // re-draw the screen
signal(SIGTSTP, suspend_sig);
signal(SIGCONT, SIG_DFL);
- kill(my_pid, SIGCONT);
+ kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
}
//----- Come here when we get a Suspend signal -------------------
-static void suspend_sig(int sig)
+static void suspend_sig(int sig UNUSED_PARAM)
{
- place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
- clear_to_eol(); // Erase to end of line
- cookmode(); // terminal to "cooked"
+ go_bottom_and_clear_to_eol();
+ cookmode(); // terminal to "cooked"
signal(SIGCONT, cont_sig);
signal(SIGTSTP, SIG_DFL);
@@ -2184,185 +2185,42 @@ static void suspend_sig(int sig)
//----- Come here when we get a signal ---------------------------
static void catch_sig(int sig)
{
- signal(SIGHUP, catch_sig);
signal(SIGINT, catch_sig);
- signal(SIGTERM, catch_sig);
- signal(SIGALRM, catch_sig);
- if(sig)
- longjmp(restart, sig);
+ if (sig)
+ siglongjmp(restart, sig);
}
-
-//----- Come here when we get a core dump signal -----------------
-static void core_sig(int sig)
-{
- signal(SIGQUIT, core_sig);
- signal(SIGILL, core_sig);
- signal(SIGTRAP, core_sig);
- signal(SIGIOT, core_sig);
- signal(SIGABRT, core_sig);
- signal(SIGFPE, core_sig);
- signal(SIGBUS, core_sig);
- signal(SIGSEGV, core_sig);
-#ifdef SIGSYS
- signal(SIGSYS, core_sig);
-#endif
-
- if(sig) { // signaled
- dot = bound_dot(dot); // make sure "dot" is valid
-
- longjmp(restart, sig);
- }
-}
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
+#endif /* FEATURE_VI_USE_SIGNALS */
static int mysleep(int hund) // sleep for 'h' 1/100 seconds
{
- // Don't hang- Wait 5/100 seconds- 1 Sec= 1000000
- fflush(stdout);
- FD_ZERO(&rfds);
- FD_SET(0, &rfds);
- tv.tv_sec = 0;
- tv.tv_usec = hund * 10000;
- select(1, &rfds, NULL, NULL, &tv);
- return (FD_ISSET(0, &rfds));
-}
+ struct pollfd pfd[1];
-static Byte readbuffer[BUFSIZ];
-static int readed_for_parse;
+ pfd[0].fd = 0;
+ pfd[0].events = POLLIN;
+ return safe_poll(pfd, 1, hund*10) > 0;
+}
//----- IO Routines --------------------------------------------
-static Byte readit(void) // read (maybe cursor) key from stdin
+static int readit(void) // read (maybe cursor) key from stdin
{
- Byte c;
- int n;
- struct esc_cmds {
- const char *seq;
- Byte val;
- };
-
- static const struct esc_cmds esccmds[] = {
- {"OA", (Byte) VI_K_UP}, // cursor key Up
- {"OB", (Byte) VI_K_DOWN}, // cursor key Down
- {"OC", (Byte) VI_K_RIGHT}, // Cursor Key Right
- {"OD", (Byte) VI_K_LEFT}, // cursor key Left
- {"OH", (Byte) VI_K_HOME}, // Cursor Key Home
- {"OF", (Byte) VI_K_END}, // Cursor Key End
- {"[A", (Byte) VI_K_UP}, // cursor key Up
- {"[B", (Byte) VI_K_DOWN}, // cursor key Down
- {"[C", (Byte) VI_K_RIGHT}, // Cursor Key Right
- {"[D", (Byte) VI_K_LEFT}, // cursor key Left
- {"[H", (Byte) VI_K_HOME}, // Cursor Key Home
- {"[F", (Byte) VI_K_END}, // Cursor Key End
- {"[1~", (Byte) VI_K_HOME}, // Cursor Key Home
- {"[2~", (Byte) VI_K_INSERT}, // Cursor Key Insert
- {"[4~", (Byte) VI_K_END}, // Cursor Key End
- {"[5~", (Byte) VI_K_PAGEUP}, // Cursor Key Page Up
- {"[6~", (Byte) VI_K_PAGEDOWN}, // Cursor Key Page Down
- {"OP", (Byte) VI_K_FUN1}, // Function Key F1
- {"OQ", (Byte) VI_K_FUN2}, // Function Key F2
- {"OR", (Byte) VI_K_FUN3}, // Function Key F3
- {"OS", (Byte) VI_K_FUN4}, // Function Key F4
- {"[15~", (Byte) VI_K_FUN5}, // Function Key F5
- {"[17~", (Byte) VI_K_FUN6}, // Function Key F6
- {"[18~", (Byte) VI_K_FUN7}, // Function Key F7
- {"[19~", (Byte) VI_K_FUN8}, // Function Key F8
- {"[20~", (Byte) VI_K_FUN9}, // Function Key F9
- {"[21~", (Byte) VI_K_FUN10}, // Function Key F10
- {"[23~", (Byte) VI_K_FUN11}, // Function Key F11
- {"[24~", (Byte) VI_K_FUN12}, // Function Key F12
- {"[11~", (Byte) VI_K_FUN1}, // Function Key F1
- {"[12~", (Byte) VI_K_FUN2}, // Function Key F2
- {"[13~", (Byte) VI_K_FUN3}, // Function Key F3
- {"[14~", (Byte) VI_K_FUN4}, // Function Key F4
- };
-
-#define ESCCMDS_COUNT (sizeof(esccmds)/sizeof(struct esc_cmds))
+ int c;
- (void) alarm(0); // turn alarm OFF while we wait for input
fflush(stdout);
- n = readed_for_parse;
- // get input from User- are there already input chars in Q?
- if (n <= 0) {
- ri0:
- // the Q is empty, wait for a typed char
- n = read(0, readbuffer, BUFSIZ - 1);
- if (n < 0) {
- if (errno == EINTR)
- goto ri0; // interrupted sys call
- if (errno == EBADF)
- editing = 0;
- if (errno == EFAULT)
- editing = 0;
- if (errno == EINVAL)
- editing = 0;
- if (errno == EIO)
- editing = 0;
- errno = 0;
- }
- if(n <= 0)
- return 0; // error
- if (readbuffer[0] == 27) {
- // This is an ESC char. Is this Esc sequence?
- // Could be bare Esc key. See if there are any
- // more chars to read after the ESC. This would
- // be a Function or Cursor Key sequence.
- FD_ZERO(&rfds);
- FD_SET(0, &rfds);
- tv.tv_sec = 0;
- tv.tv_usec = 50000; // Wait 5/100 seconds- 1 Sec=1000000
-
- // keep reading while there are input chars and room in buffer
- while (select(1, &rfds, NULL, NULL, &tv) > 0 && n <= (BUFSIZ - 5)) {
- // read the rest of the ESC string
- int r = read(0, (void *) (readbuffer + n), BUFSIZ - n);
- if (r > 0) {
- n += r;
- }
- }
- }
- readed_for_parse = n;
+ c = read_key(STDIN_FILENO, &chars_to_parse, readbuffer);
+ if (c == -1) { // EOF/error
+ go_bottom_and_clear_to_eol();
+ cookmode(); // terminal to "cooked"
+ bb_error_msg_and_die("can't read user input");
}
- c = readbuffer[0];
- if(c == 27 && n > 1) {
- // Maybe cursor or function key?
- const struct esc_cmds *eindex;
-
- for (eindex = esccmds; eindex < &esccmds[ESCCMDS_COUNT]; eindex++) {
- int cnt = strlen(eindex->seq);
-
- if(n <= cnt)
- continue;
- if(strncmp(eindex->seq, (char *) readbuffer + 1, cnt))
- continue;
- // is a Cursor key- put derived value back into Q
- c = eindex->val;
- // for squeeze out the ESC sequence
- n = cnt + 1;
- break;
- }
- if(eindex == &esccmds[ESCCMDS_COUNT]) {
- /* defined ESC sequence not found, set only one ESC */
- n = 1;
- }
- } else {
- n = 1;
- }
- // remove key sequence from Q
- readed_for_parse -= n;
- memmove(readbuffer, readbuffer + n, BUFSIZ - n);
- (void) alarm(3); // we are done waiting for input, turn alarm ON
- return (c);
+ return c;
}
//----- IO Routines --------------------------------------------
-static Byte get_one_char()
+static int get_one_char(void)
{
- static Byte c;
+ int c;
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
- // ! adding2q && ioq == 0 read()
- // ! adding2q && ioq != 0 *ioq
- // adding2q *last_modifying_cmd= read()
+#if ENABLE_FEATURE_VI_DOT_CMD
if (!adding2q) {
// we are not adding to the q.
// but, we may be reading from a q
@@ -2371,7 +2229,8 @@ static Byte get_one_char()
c = readit(); // get the users input
} else {
// there is a queue to get chars from first
- c = *ioq++;
+ // careful with correct sign expansion!
+ c = (unsigned char)*ioq++;
if (c == '\0') {
// the end of the q, read from STDIN
free(ioq_start);
@@ -2382,258 +2241,256 @@ static Byte get_one_char()
} else {
// adding STDIN chars to q
c = readit(); // get the users input
- if (last_modifying_cmd != 0) {
- int len = strlen((char *) last_modifying_cmd);
- if (len + 1 >= BUFSIZ) {
- psbs("last_modifying_cmd overrun");
- } else {
- // add new char to q
- last_modifying_cmd[len] = c;
- }
+ if (lmc_len >= MAX_INPUT_LEN - 1) {
+ status_line_bold("last_modifying_cmd overrun");
+ } else {
+ // add new char to q
+ last_modifying_cmd[lmc_len++] = c;
}
}
-#else /* CONFIG_FEATURE_VI_DOT_CMD */
+#else
c = readit(); // get the users input
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
- return (c); // return the char, where ever it came from
+#endif /* FEATURE_VI_DOT_CMD */
+ return c;
}
-static Byte *get_input_line(Byte * prompt) // get input line- use "status line"
+// Get input line (uses "status line" area)
+static char *get_input_line(const char *prompt)
{
- Byte buf[BUFSIZ];
- Byte c;
+ // char [MAX_INPUT_LEN]
+#define buf get_input_line__buf
+
+ int c;
int i;
- static Byte *obufp = NULL;
- strcpy((char *) buf, (char *) prompt);
- *status_buffer = '\0'; // clear the status buffer
- place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
- clear_to_eol(); // clear the line
+ strcpy(buf, prompt);
+ last_status_cksum = 0; // force status update
+ go_bottom_and_clear_to_eol();
write1(prompt); // write out the :, /, or ? prompt
- for (i = strlen((char *) buf); i < BUFSIZ;) {
- c = get_one_char(); // read user input
+ i = strlen(buf);
+ while (i < MAX_INPUT_LEN) {
+ c = get_one_char();
if (c == '\n' || c == '\r' || c == 27)
- break; // is this end of input
- if (c == erase_char) { // user wants to erase prev char
- i--; // backup to prev char
- buf[i] = '\0'; // erase the char
- buf[i + 1] = '\0'; // null terminate buffer
- write1("\b \b"); // erase char on screen
- if (i <= 0) { // user backs up before b-o-l, exit
+ break; // this is end of input
+ if (c == erase_char || c == 8 || c == 127) {
+ // user wants to erase prev char
+ buf[--i] = '\0';
+ write1("\b \b"); // erase char on screen
+ if (i <= 0) // user backs up before b-o-l, exit
break;
- }
- } else {
- buf[i] = c; // save char in buffer
- buf[i + 1] = '\0'; // make sure buffer is null terminated
- putchar(c); // echo the char back to user
- i++;
+ } else if (c > 0 && c < 256) { // exclude Unicode
+ // (TODO: need to handle Unicode)
+ buf[i] = c;
+ buf[++i] = '\0';
+ bb_putchar(c);
}
}
refresh(FALSE);
- free(obufp);
- obufp = (Byte *) bb_xstrdup((char *) buf);
- return (obufp);
+ return buf;
+#undef buf
}
-static int file_size(const Byte * fn) // what is the byte size of "fn"
+static int file_size(const char *fn) // what is the byte size of "fn"
{
struct stat st_buf;
- int cnt, sr;
+ int cnt;
- if (fn == 0 || strlen(fn) <= 0)
- return (-1);
cnt = -1;
- sr = stat((char *) fn, &st_buf); // see if file exists
- if (sr >= 0) {
+ if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
cnt = (int) st_buf.st_size;
- }
- return (cnt);
+ return cnt;
}
-static int file_insert(Byte * fn, Byte * p, int size)
+// might reallocate text[]!
+static int file_insert(const char *fn, char *p, int update_ro_status)
{
- int fd, cnt;
+ int cnt = -1;
+ int fd, size;
+ struct stat statbuf;
- cnt = -1;
-#ifdef CONFIG_FEATURE_VI_READONLY
- readonly = FALSE;
-#endif /* CONFIG_FEATURE_VI_READONLY */
- if (fn == 0 || strlen((char*) fn) <= 0) {
- psbs("No filename given");
+ /* Validate file */
+ if (stat(fn, &statbuf) < 0) {
+ status_line_bold("\"%s\" %s", fn, strerror(errno));
goto fi0;
}
- if (size == 0) {
- // OK- this is just a no-op
- cnt = 0;
- goto fi0;
- }
- if (size < 0) {
- psbs("Trying to insert a negative number (%d) of characters", size);
+ if (!S_ISREG(statbuf.st_mode)) {
+ // This is not a regular file
+ status_line_bold("\"%s\" Not a regular file", fn);
goto fi0;
}
if (p < text || p > end) {
- psbs("Trying to insert file outside of memory");
+ status_line_bold("Trying to insert file outside of memory");
goto fi0;
}
- // see if we can open the file
-#ifdef CONFIG_FEATURE_VI_READONLY
- if (vi_readonly) goto fi1; // do not try write-mode
-#endif
- fd = open((char *) fn, O_RDWR); // assume read & write
+ // read file to buffer
+ fd = open(fn, O_RDONLY);
if (fd < 0) {
- // could not open for writing- maybe file is read only
-#ifdef CONFIG_FEATURE_VI_READONLY
- fi1:
-#endif
- fd = open((char *) fn, O_RDONLY); // try read-only
- if (fd < 0) {
- psbs("\"%s\" %s", fn, "could not open file");
- goto fi0;
- }
-#ifdef CONFIG_FEATURE_VI_READONLY
- // got the file- read-only
- readonly = TRUE;
-#endif /* CONFIG_FEATURE_VI_READONLY */
- }
- p = text_hole_make(p, size);
- cnt = read(fd, p, size);
- close(fd);
+ status_line_bold("\"%s\" %s", fn, strerror(errno));
+ goto fi0;
+ }
+ size = statbuf.st_size;
+ p += text_hole_make(p, size);
+ cnt = safe_read(fd, p, size);
if (cnt < 0) {
- cnt = -1;
+ status_line_bold("\"%s\" %s", fn, strerror(errno));
p = text_hole_delete(p, p + size - 1); // un-do buffer insert
- psbs("could not read file \"%s\"", fn);
} else if (cnt < size) {
// There was a partial read, shrink unused space text[]
p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
- psbs("could not read all of file \"%s\"", fn);
+ status_line_bold("cannot read all of file \"%s\"", fn);
}
if (cnt >= size)
- file_modified = TRUE;
- fi0:
- return (cnt);
+ file_modified++;
+ close(fd);
+ fi0:
+#if ENABLE_FEATURE_VI_READONLY
+ if (update_ro_status
+ && ((access(fn, W_OK) < 0) ||
+ /* root will always have access()
+ * so we check fileperms too */
+ !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
+ )
+ ) {
+ SET_READONLY_FILE(readonly_mode);
+ }
+#endif
+ return cnt;
}
-static int file_write(Byte * fn, Byte * first, Byte * last)
+static int file_write(char *fn, char *first, char *last)
{
int fd, cnt, charcnt;
if (fn == 0) {
- psbs("No current filename");
- return (-1);
+ status_line_bold("No current filename");
+ return -2;
}
charcnt = 0;
- // FIXIT- use the correct umask()
- fd = open((char *) fn, (O_WRONLY | O_CREAT | O_TRUNC), 0664);
+ /* By popular request we do not open file with O_TRUNC,
+ * but instead ftruncate() it _after_ successful write.
+ * Might reduce amount of data lost on power fail etc.
+ */
+/* fd = open(fn, (O_WRONLY | O_CREAT), 0666); */
+ fd = open(fn, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
if (fd < 0)
- return (-1);
+ return -1;
cnt = last - first + 1;
- charcnt = write(fd, first, cnt);
+ charcnt = full_write(fd, first, cnt);
+/* ftruncate(fd, charcnt); buggy for us - zzz */
if (charcnt == cnt) {
// good write
- //file_modified= FALSE; // the file has not been modified
+ //file_modified = FALSE;
} else {
charcnt = 0;
}
close(fd);
- return (charcnt);
+ return charcnt;
}
//----- Terminal Drawing ---------------------------------------
// The terminal is made up of 'rows' line of 'columns' columns.
-// classicly this would be 24 x 80.
+// classically this would be 24 x 80.
// screen coordinates
// 0,0 ... 0,79
// 1,0 ... 1,79
// . ... .
// . ... .
// 22,0 ... 22,79
-// 23,0 ... 23,79 status line
-//
+// 23,0 ... 23,79 <- status line
//----- Move the cursor to row x col (count from 0, not 1) -------
-static void place_cursor(int row, int col, int opti)
-{
- char cm1[BUFSIZ];
+static void place_cursor(int row, int col, int optimize)
+{
+ char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ enum {
+ SZ_UP = sizeof(CMup),
+ SZ_DN = sizeof(CMdown),
+ SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
+ };
+ char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
+#endif
char *cm;
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- char cm2[BUFSIZ];
- Byte *screenp;
- // char cm3[BUFSIZ];
- int Rrow= last_row;
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
-
- memset(cm1, '\0', BUFSIZ - 1); // clear the buffer
if (row < 0) row = 0;
if (row >= rows) row = rows - 1;
if (col < 0) col = 0;
if (col >= columns) col = columns - 1;
-
+
//----- 1. Try the standard terminal ESC sequence
- sprintf((char *) cm1, CMrc, row + 1, col + 1);
- cm= cm1;
- if (! opti) goto pc0;
-
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- //----- find the minimum # of chars to move cursor -------------
- //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
- memset(cm2, '\0', BUFSIZ - 1); // clear the buffer
-
- // move to the correct row
- while (row < Rrow) {
- // the cursor has to move up
- strcat(cm2, CMup);
- Rrow--;
- }
- while (row > Rrow) {
- // the cursor has to move down
- strcat(cm2, CMdown);
- Rrow++;
- }
-
- // now move to the correct column
- strcat(cm2, "\r"); // start at col 0
- // just send out orignal source char to get to correct place
- screenp = &screen[row * columns]; // start of screen line
- strncat(cm2, screenp, col);
-
- //----- 3. Try some other way of moving cursor
- //---------------------------------------------
-
- // pick the shortest cursor motion to send out
- cm= cm1;
- if (strlen(cm2) < strlen(cm)) {
- cm= cm2;
- } /* else if (strlen(cm3) < strlen(cm)) {
- cm= cm3;
- } */
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
- pc0:
- write1(cm); // move the cursor
+ sprintf(cm1, CMrc, row + 1, col + 1);
+ cm = cm1;
+
+#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
+ if (optimize && col < 16) {
+ char *screenp;
+ int Rrow = last_row;
+ int diff = Rrow - row;
+
+ if (diff < -5 || diff > 5)
+ goto skip;
+
+ //----- find the minimum # of chars to move cursor -------------
+ //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
+ cm2[0] = '\0';
+
+ // move to the correct row
+ while (row < Rrow) {
+ // the cursor has to move up
+ strcat(cm2, CMup);
+ Rrow--;
+ }
+ while (row > Rrow) {
+ // the cursor has to move down
+ strcat(cm2, CMdown);
+ Rrow++;
+ }
+
+ // now move to the correct column
+ strcat(cm2, "\r"); // start at col 0
+ // just send out orignal source char to get to correct place
+ screenp = &screen[row * columns]; // start of screen line
+ strncat(cm2, screenp, col);
+
+ // pick the shortest cursor motion to send out
+ if (strlen(cm2) < strlen(cm)) {
+ cm = cm2;
+ }
+ skip: ;
+ }
+ last_row = row;
+#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
+ write1(cm);
}
//----- Erase from cursor to end of line -----------------------
-static void clear_to_eol()
+static void clear_to_eol(void)
{
write1(Ceol); // Erase from cursor to end of line
}
+static void go_bottom_and_clear_to_eol(void)
+{
+ place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
+ clear_to_eol(); // erase to end of line
+}
+
//----- Erase from cursor to end of screen -----------------------
-static void clear_to_eos()
+static void clear_to_eos(void)
{
write1(Ceos); // Erase from cursor to end of screen
}
//----- Start standout mode ------------------------------------
-static void standout_start() // send "start reverse video" sequence
+static void standout_start(void) // send "start reverse video" sequence
{
write1(SOs); // Start reverse video mode
}
//----- End standout mode --------------------------------------
-static void standout_end() // send "end reverse video" sequence
+static void standout_end(void) // send "end reverse video" sequence
{
write1(SOn); // End reverse video mode
}
@@ -2643,17 +2500,17 @@ static void flash(int h)
{
standout_start(); // send "start reverse video" sequence
redraw(TRUE);
- (void) mysleep(h);
+ mysleep(h);
standout_end(); // send "end reverse video" sequence
redraw(TRUE);
}
static void Indicate_Error(void)
{
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#if ENABLE_FEATURE_VI_CRASHME
if (crashme > 0)
return; // generate a random command
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif
if (!err_method) {
write1(bell); // send out a bell character
} else {
@@ -2663,70 +2520,147 @@ static void Indicate_Error(void)
//----- Screen[] Routines --------------------------------------
//----- Erase the Screen[] memory ------------------------------
-static void screen_erase()
+static void screen_erase(void)
{
memset(screen, ' ', screensize); // clear new screen
}
+static int bufsum(char *buf, int count)
+{
+ int sum = 0;
+ char *e = buf + count;
+
+ while (buf < e)
+ sum += (unsigned char) *buf++;
+ return sum;
+}
+
//----- Draw the status line at bottom of the screen -------------
static void show_status_line(void)
{
- static int last_cksum;
- int l, cnt, cksum;
+ int cnt = 0, cksum = 0;
- cnt = strlen((char *) status_buffer);
- for (cksum= l= 0; l < cnt; l++) { cksum += (int)(status_buffer[l]); }
- // don't write the status line unless it changes
- if (cnt > 0 && last_cksum != cksum) {
- last_cksum= cksum; // remember if we have seen this line
- place_cursor(rows - 1, 0, FALSE); // put cursor on status line
+ // either we already have an error or status message, or we
+ // create one.
+ if (!have_status_msg) {
+ cnt = format_edit_status();
+ cksum = bufsum(status_buffer, cnt);
+ }
+ if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
+ last_status_cksum = cksum; // remember if we have seen this line
+ go_bottom_and_clear_to_eol();
write1(status_buffer);
- clear_to_eol();
+ if (have_status_msg) {
+ if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
+ (columns - 1) ) {
+ have_status_msg = 0;
+ Hit_Return();
+ }
+ have_status_msg = 0;
+ }
place_cursor(crow, ccol, FALSE); // put cursor back in correct place
}
+ fflush(stdout);
}
//----- format the status buffer, the bottom line of screen ------
-// print status buffer, with STANDOUT mode
-static void psbs(const char *format, ...)
+// format status buffer, with STANDOUT mode
+static void status_line_bold(const char *format, ...)
{
va_list args;
va_start(args, format);
- strcpy((char *) status_buffer, SOs); // Terminal standout mode on
- vsprintf((char *) status_buffer + strlen((char *) status_buffer), format,
- args);
- strcat((char *) status_buffer, SOn); // Terminal standout mode off
+ strcpy(status_buffer, SOs); // Terminal standout mode on
+ vsprintf(status_buffer + sizeof(SOs)-1, format, args);
+ strcat(status_buffer, SOn); // Terminal standout mode off
va_end(args);
- return;
+ have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
}
-// print status buffer
-static void psb(const char *format, ...)
+// format status buffer
+static void status_line(const char *format, ...)
{
va_list args;
va_start(args, format);
- vsprintf((char *) status_buffer, format, args);
+ vsprintf(status_buffer, format, args);
va_end(args);
- return;
+
+ have_status_msg = 1;
}
-static void ni(Byte * s) // display messages
+// copy s to buf, convert unprintable
+static void print_literal(char *buf, const char *s)
{
- Byte buf[BUFSIZ];
+ unsigned char c;
+ char b[2];
+
+ b[1] = '\0';
+ buf[0] = '\0';
+ if (!s[0])
+ s = "(NULL)";
+ for (; *s; s++) {
+ int c_is_no_print;
+
+ c = *s;
+ c_is_no_print = (c & 0x80) && !Isprint(c);
+ if (c_is_no_print) {
+ strcat(buf, SOn);
+ c = '.';
+ }
+ if (c < ' ' || c == 127) {
+ strcat(buf, "^");
+ if (c == 127)
+ c = '?';
+ else
+ c += '@';
+ }
+ b[0] = c;
+ strcat(buf, b);
+ if (c_is_no_print)
+ strcat(buf, SOs);
+ if (*s == '\n')
+ strcat(buf, "$");
+ if (strlen(buf) > MAX_INPUT_LEN - 10) // paranoia
+ break;
+ }
+}
+
+static void not_implemented(const char *s)
+{
+ char buf[MAX_INPUT_LEN];
print_literal(buf, s);
- psbs("\'%s\' is not implemented", buf);
+ status_line_bold("\'%s\' is not implemented", buf);
}
-static void edit_status(void) // show file status on status line
+// show file status on status line
+static int format_edit_status(void)
{
- int cur, tot, percent;
+ static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
+#define tot format_edit_status__tot
+
+ int cur, percent, ret, trunc_at;
+
+ // file_modified is now a counter rather than a flag. this
+ // helps reduce the amount of line counting we need to do.
+ // (this will cause a mis-reporting of modified status
+ // once every MAXINT editing operations.)
+
+ // it would be nice to do a similar optimization here -- if
+ // we haven't done a motion that could have changed which line
+ // we're on, then we shouldn't have to do this count_lines()
cur = count_lines(text, dot);
- tot = count_lines(text, end - 1);
+
+ // reduce counting -- the total lines can't have
+ // changed if we haven't done any edits.
+ if (file_modified != last_file_modified) {
+ tot = cur + count_lines(dot, end - 1) - 1;
+ last_file_modified = file_modified;
+ }
+
// current line percent
// ------------- ~~ ----------
// total lines 100
@@ -2736,69 +2670,98 @@ static void edit_status(void) // show file status on status line
cur = tot = 0;
percent = 100;
}
- psb("\"%s\""
-#ifdef CONFIG_FEATURE_VI_READONLY
- "%s"
-#endif /* CONFIG_FEATURE_VI_READONLY */
- "%s line %d of %d --%d%%--",
- (cfn != 0 ? (char *) cfn : "No file"),
-#ifdef CONFIG_FEATURE_VI_READONLY
- ((vi_readonly || readonly) ? " [Read only]" : ""),
-#endif /* CONFIG_FEATURE_VI_READONLY */
- (file_modified ? " [modified]" : ""),
+
+ trunc_at = columns < STATUS_BUFFER_LEN-1 ?
+ columns : STATUS_BUFFER_LEN-1;
+
+ ret = snprintf(status_buffer, trunc_at+1,
+#if ENABLE_FEATURE_VI_READONLY
+ "%c %s%s%s %d/%d %d%%",
+#else
+ "%c %s%s %d/%d %d%%",
+#endif
+ cmd_mode_indicator[cmd_mode & 3],
+ (current_filename != NULL ? current_filename : "No file"),
+#if ENABLE_FEATURE_VI_READONLY
+ (readonly_mode ? " [Readonly]" : ""),
+#endif
+ (file_modified ? " [Modified]" : ""),
cur, tot, percent);
+
+ if (ret >= 0 && ret < trunc_at)
+ return ret; /* it all fit */
+
+ return trunc_at; /* had to truncate */
+#undef tot
}
//----- Force refresh of all Lines -----------------------------
static void redraw(int full_screen)
{
place_cursor(0, 0, FALSE); // put cursor in correct place
- clear_to_eos(); // tel terminal to erase display
+ clear_to_eos(); // tell terminal to erase display
screen_erase(); // erase the internal screen buffer
+ last_status_cksum = 0; // force status update
refresh(full_screen); // this will redraw the entire display
+ show_status_line();
}
//----- Format a text[] line into a buffer ---------------------
-static void format_line(Byte *dest, Byte *src, int li)
+static char* format_line(char *src /*, int li*/)
{
+ unsigned char c;
int co;
- Byte c;
-
- for (co= 0; co < MAX_SCR_COLS; co++) {
- c= ' '; // assume blank
- if (li > 0 && co == 0) {
- c = '~'; // not first line, assume Tilde
- }
- // are there chars in text[] and have we gone past the end
- if (text < end && src < end) {
+ int ofs = offset;
+ char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
+
+ c = '~'; // char in col 0 in non-existent lines is '~'
+ co = 0;
+ while (co < columns + tabstop) {
+ // have we gone past the end?
+ if (src < end) {
c = *src++;
- }
- if (c == '\n')
- break;
- if (c > 127 && !Isprint(c)) {
- c = '.';
- }
- if (c < ' ' || c == 127) {
- if (c == '\t') {
- c = ' ';
- // co % 8 != 7
- for (; (co % tabstop) != (tabstop - 1); co++) {
- dest[co] = c;
+ if (c == '\n')
+ break;
+ if ((c & 0x80) && !Isprint(c)) {
+ c = '.';
+ }
+ if (c < ' ' || c == 0x7f) {
+ if (c == '\t') {
+ c = ' ';
+ // co % 8 != 7
+ while ((co % tabstop) != (tabstop - 1)) {
+ dest[co++] = c;
+ }
+ } else {
+ dest[co++] = '^';
+ if (c == 0x7f)
+ c = '?';
+ else
+ c += '@'; // Ctrl-X -> 'X'
}
- } else {
- dest[co++] = '^';
- if(c == 127)
- c = '?';
- else
- c += '@'; // make it visible
}
}
- // the co++ is done here so that the column will
- // not be overwritten when we blank-out the rest of line
- dest[co] = c;
+ dest[co++] = c;
+ // discard scrolled-off-to-the-left portion,
+ // in tabstop-sized pieces
+ if (ofs >= tabstop && co >= tabstop) {
+ memmove(dest, dest + tabstop, co);
+ co -= tabstop;
+ ofs -= tabstop;
+ }
if (src >= end)
break;
}
+ // check "short line, gigantic offset" case
+ if (co < ofs)
+ ofs = co;
+ // discard last scrolled off part
+ co -= ofs;
+ dest += ofs;
+ // fill the rest with spaces
+ if (co < columns)
+ memset(&dest[co], ' ', columns - co);
+ return dest;
}
//----- Refresh the changed screen lines -----------------------
@@ -2808,35 +2771,39 @@ static void format_line(Byte *dest, Byte *src, int li)
//
static void refresh(int full_screen)
{
- static int old_offset;
+#define old_offset refresh__old_offset
+
int li, changed;
- Byte buf[MAX_SCR_COLS];
- Byte *tp, *sp; // pointer into text[] and screen[]
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- int last_li= -2; // last line that changed- for optimizing cursor movement
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
-
-#ifdef CONFIG_FEATURE_VI_WIN_RESIZE
- window_size_get(0);
-#endif /* CONFIG_FEATURE_VI_WIN_RESIZE */
+ char *tp, *sp; // pointer into text[] and screen[]
+
+ if (ENABLE_FEATURE_VI_WIN_RESIZE) {
+ unsigned c = columns, r = rows;
+ get_terminal_width_height(0, &columns, &rows);
+ if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
+ if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
+ full_screen |= (c - columns) | (r - rows);
+ }
sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
tp = screenbegin; // index into text[] of top line
// compare text[] to screen[] and mark screen[] lines that need updating
for (li = 0; li < rows - 1; li++) {
int cs, ce; // column start & end
- memset(buf, ' ', MAX_SCR_COLS); // blank-out the buffer
- buf[MAX_SCR_COLS-1] = 0; // NULL terminate the buffer
- // format current text line into buf
- format_line(buf, tp, li);
+ char *out_buf;
+ // format current text line
+ out_buf = format_line(tp /*, li*/);
// skip to the end of the current text[] line
- while (tp < end && *tp++ != '\n') /*no-op*/ ;
+ if (tp < end) {
+ char *t = memchr(tp, '\n', end - tp);
+ if (!t) t = end - 1;
+ tp = t + 1;
+ }
- // see if there are any changes between vitual screen and buf
+ // see if there are any changes between vitual screen and out_buf
changed = FALSE; // assume no change
- cs= 0;
- ce= columns-1;
+ cs = 0;
+ ce = columns - 1;
sp = &screen[li * columns]; // start of screen line
if (full_screen) {
// force re-draw of every single column from 0 - columns-1
@@ -2844,16 +2811,16 @@ static void refresh(int full_screen)
}
// compare newly formatted buffer with virtual screen
// look forward for first difference between buf and screen
- for ( ; cs <= ce; cs++) {
- if (buf[cs + offset] != sp[cs]) {
+ for (; cs <= ce; cs++) {
+ if (out_buf[cs] != sp[cs]) {
changed = TRUE; // mark for redraw
break;
}
}
- // look backward for last difference between buf and screen
- for ( ; ce >= cs; ce--) {
- if (buf[ce + offset] != sp[ce]) {
+ // look backward for last difference between out_buf and screen
+ for (; ce >= cs; ce--) {
+ if (out_buf[ce] != sp[ce]) {
changed = TRUE; // mark for redraw
break;
}
@@ -2862,61 +2829,37 @@ static void refresh(int full_screen)
// if horz offset has changed, force a redraw
if (offset != old_offset) {
- re0:
+ re0:
changed = TRUE;
}
// make a sanity check of columns indexes
- if (cs < 0) cs= 0;
- if (ce > columns-1) ce= columns-1;
- if (cs > ce) { cs= 0; ce= columns-1; }
- // is there a change between vitual screen and buf
+ if (cs < 0) cs = 0;
+ if (ce > columns - 1) ce = columns - 1;
+ if (cs > ce) { cs = 0; ce = columns - 1; }
+ // is there a change between vitual screen and out_buf
if (changed) {
- // copy changed part of buffer to virtual screen
- memmove(sp+cs, buf+(cs+offset), ce-cs+1);
+ // copy changed part of buffer to virtual screen
+ memcpy(sp+cs, out_buf+cs, ce-cs+1);
// move cursor to column of first change
- if (offset != old_offset) {
- // opti_cur_move is still too stupid
- // to handle offsets correctly
- place_cursor(li, cs, FALSE);
- } else {
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- // if this just the next line
- // try to optimize cursor movement
- // otherwise, use standard ESC sequence
- place_cursor(li, cs, li == (last_li+1) ? TRUE : FALSE);
- last_li= li;
-#else /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
- place_cursor(li, cs, FALSE); // use standard ESC sequence
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
- }
+ //if (offset != old_offset) {
+ // // place_cursor is still too stupid
+ // // to handle offsets correctly
+ // place_cursor(li, cs, FALSE);
+ //} else {
+ place_cursor(li, cs, TRUE);
+ //}
// write line out to terminal
- {
- int nic = ce-cs+1;
- char *out = sp+cs;
-
- while(nic-- > 0) {
- putchar(*out);
- out++;
- }
- }
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- last_row = li;
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
+ fwrite(&sp[cs], ce - cs + 1, 1, stdout);
}
}
-#ifdef CONFIG_FEATURE_VI_OPTIMIZE_CURSOR
- place_cursor(crow, ccol, (crow == last_row) ? TRUE : FALSE);
- last_row = crow;
-#else
- place_cursor(crow, ccol, FALSE);
-#endif /* CONFIG_FEATURE_VI_OPTIMIZE_CURSOR */
-
- if (offset != old_offset)
- old_offset = offset;
+ place_cursor(crow, ccol, TRUE);
+
+ old_offset = offset;
+#undef old_offset
}
//---------------------------------------------------------------------
@@ -2941,32 +2884,40 @@ static void refresh(int full_screen)
//---------------------------------------------------------------------
//----- Execute a Vi Command -----------------------------------
-static void do_cmd(Byte c)
+static void do_cmd(int c)
{
- Byte c1, *p, *q, *msg, buf[9], *save_dot;
- int cnt, i, j, dir, yf;
+ const char *msg = msg; // for compiler
+ char *p, *q, *save_dot;
+ char buf[12];
+ int dir;
+ int cnt, i, j;
+ int c1;
+
+// c1 = c; // quiet the compiler
+// cnt = yf = 0; // quiet the compiler
+// msg = p = q = save_dot = buf; // quiet the compiler
+ memset(buf, '\0', 12);
- c1 = c; // quiet the compiler
- cnt = yf = dir = 0; // quiet the compiler
- p = q = save_dot = msg = buf; // quiet the compiler
- memset(buf, '\0', 9); // clear buf
+ show_status_line();
/* if this is a cursor key, skip these checks */
switch (c) {
- case VI_K_UP:
- case VI_K_DOWN:
- case VI_K_LEFT:
- case VI_K_RIGHT:
- case VI_K_HOME:
- case VI_K_END:
- case VI_K_PAGEUP:
- case VI_K_PAGEDOWN:
+ case KEYCODE_UP:
+ case KEYCODE_DOWN:
+ case KEYCODE_LEFT:
+ case KEYCODE_RIGHT:
+ case KEYCODE_HOME:
+ case KEYCODE_END:
+ case KEYCODE_PAGEUP:
+ case KEYCODE_PAGEDOWN:
+ case KEYCODE_DELETE:
goto key_cmd_mode;
}
if (cmd_mode == 2) {
// flip-flop Insert/Replace mode
- if (c == VI_K_INSERT) goto dc_i;
+ if (c == KEYCODE_INSERT)
+ goto dc_i;
// we are 'R'eplacing the current *dot with new char
if (*dot == '\n') {
// don't Replace past E-o-l
@@ -2982,7 +2933,7 @@ static void do_cmd(Byte c)
}
if (cmd_mode == 1) {
// hitting "Insert" twice means "R" replace mode
- if (c == VI_K_INSERT) goto dc5;
+ if (c == KEYCODE_INSERT) goto dc5;
// insert the char c at "dot"
if (1 <= c || Isprint(c)) {
dot = char_insert(dot, c);
@@ -2990,7 +2941,7 @@ static void do_cmd(Byte c)
goto dc1;
}
-key_cmd_mode:
+ key_cmd_mode:
switch (c) {
//case 0x01: // soh
//case 0x09: // ht
@@ -3000,11 +2951,11 @@ key_cmd_mode:
//case 0x10: // dle
//case 0x11: // dc1
//case 0x13: // dc3
-#ifdef CONFIG_FEATURE_VI_CRASHME
+#if ENABLE_FEATURE_VI_CRASHME
case 0x14: // dc4 ctrl-T
crashme = (crashme == 0) ? 1 : 0;
break;
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif
//case 0x16: // syn
//case 0x17: // etb
//case 0x18: // can
@@ -3012,29 +2963,27 @@ key_cmd_mode:
//case 0x1d: // gs
//case 0x1e: // rs
//case 0x1f: // us
- //case '!': // !-
- //case '#': // #-
- //case '&': // &-
- //case '(': // (-
- //case ')': // )-
- //case '*': // *-
- //case ',': // ,-
- //case '=': // =-
- //case '@': // @-
- //case 'F': // F-
- //case 'K': // K-
- //case 'Q': // Q-
- //case 'S': // S-
- //case 'T': // T-
- //case 'V': // V-
- //case '[': // [-
- //case '\\': // \-
- //case ']': // ]-
- //case '_': // _-
- //case '`': // `-
- //case 'g': // g-
+ //case '!': // !-
+ //case '#': // #-
+ //case '&': // &-
+ //case '(': // (-
+ //case ')': // )-
+ //case '*': // *-
+ //case '=': // =-
+ //case '@': // @-
+ //case 'F': // F-
+ //case 'K': // K-
+ //case 'Q': // Q-
+ //case 'S': // S-
+ //case 'T': // T-
+ //case 'V': // V-
+ //case '[': // [-
+ //case '\\': // \-
+ //case ']': // ]-
+ //case '_': // _-
+ //case '`': // `-
//case 'u': // u- FIXME- there is no undo
- //case 'v': // v-
+ //case 'v': // v-
default: // unrecognised command
buf[0] = c;
buf[1] = '\0';
@@ -3043,22 +2992,14 @@ key_cmd_mode:
buf[1] = c + '@';
buf[2] = '\0';
}
- ni((Byte *) buf);
+ not_implemented(buf);
end_cmd_q(); // stop adding to q
case 0x00: // nul- ignore
break;
case 2: // ctrl-B scroll up full screen
- case VI_K_PAGEUP: // Cursor Key Page Up
+ case KEYCODE_PAGEUP: // Cursor Key Page Up
dot_scroll(rows - 2, -1);
break;
-#ifdef CONFIG_FEATURE_VI_USE_SIGNALS
- case 0x03: // ctrl-C interrupt
- longjmp(restart, 1);
- break;
- case 26: // ctrl-Z suspend
- suspend_sig(SIGTSTP);
- break;
-#endif /* CONFIG_FEATURE_VI_USE_SIGNALS */
case 4: // ctrl-D scroll down half screen
dot_scroll((rows - 2) / 2, 1);
break;
@@ -3066,16 +3007,16 @@ key_cmd_mode:
dot_scroll(1, 1);
break;
case 6: // ctrl-F scroll down full screen
- case VI_K_PAGEDOWN: // Cursor Key Page Down
+ case KEYCODE_PAGEDOWN: // Cursor Key Page Down
dot_scroll(rows - 2, 1);
break;
case 7: // ctrl-G show current status
- edit_status();
+ last_status_cksum = 0; // force status update
break;
case 'h': // h- move left
- case VI_K_LEFT: // cursor key Left
- case 8: // ctrl-H- move left (This may be ERASE char)
- case 127: // DEL- move left (This may be ERASE char)
+ case KEYCODE_LEFT: // cursor key Left
+ case 8: // ctrl-H- move left (This may be ERASE char)
+ case 0x7f: // DEL- move left (This may be ERASE char)
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
@@ -3083,7 +3024,7 @@ key_cmd_mode:
break;
case 10: // Newline ^J
case 'j': // j- goto next line, same col
- case VI_K_DOWN: // cursor key Down
+ case KEYCODE_DOWN: // cursor key Down
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
@@ -3094,8 +3035,9 @@ key_cmd_mode:
case 18: // ctrl-R force redraw
place_cursor(0, 0, FALSE); // put cursor in correct place
clear_to_eos(); // tel terminal to erase display
- (void) mysleep(10);
+ mysleep(10);
screen_erase(); // erase the internal screen buffer
+ last_status_cksum = 0; // force status update
refresh(TRUE); // this will redraw the entire display
break;
case 13: // Carriage Return ^M
@@ -3117,33 +3059,30 @@ key_cmd_mode:
indicate_error(c);
cmd_mode = 0; // stop insrting
end_cmd_q();
- *status_buffer = '\0'; // clear status buffer
+ last_status_cksum = 0; // force status update
break;
case ' ': // move right
case 'l': // move right
- case VI_K_RIGHT: // Cursor Key Right
+ case KEYCODE_RIGHT: // Cursor Key Right
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
dot_right();
break;
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
case '"': // "- name a register to use for Delete/Yank
- c1 = get_one_char();
- c1 = tolower(c1);
- if (islower(c1)) {
- YDreg = c1 - 'a';
+ c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
+ if ((unsigned)c1 <= 25) { // a-z?
+ YDreg = c1;
} else {
indicate_error(c);
}
break;
case '\'': // '- goto a specific mark
- c1 = get_one_char();
- c1 = tolower(c1);
- if (islower(c1)) {
- c1 = c1 - 'a';
+ c1 = (get_one_char() | 0x20) - 'a';
+ if ((unsigned)c1 <= 25) { // a-z?
// get the b-o-l
- q = mark[(int) c1];
+ q = mark[c1];
if (text <= q && q < end) {
dot = q;
dot_begin(); // go to B-o-l
@@ -3162,12 +3101,10 @@ key_cmd_mode:
// between text[0] and dot then this mark will not point to the
// correct location! It could be off by many lines!
// Well..., at least its quick and dirty.
- c1 = get_one_char();
- c1 = tolower(c1);
- if (islower(c1)) {
- c1 = c1 - 'a';
+ c1 = (get_one_char() | 0x20) - 'a';
+ if ((unsigned)c1 <= 25) { // a-z?
// remember the line
- mark[(int) c1] = dot;
+ mark[c1] = dot;
} else {
indicate_error(c);
}
@@ -3175,12 +3112,12 @@ key_cmd_mode:
case 'P': // P- Put register before
case 'p': // p- put register after
p = reg[YDreg];
- if (p == 0) {
- psbs("Nothing in register %c", what_reg());
+ if (p == NULL) {
+ status_line_bold("Nothing in register %c", what_reg());
break;
}
// are we putting whole lines or strings
- if (strchr((char *) p, '\n') != NULL) {
+ if (strchr(p, '\n') != NULL) {
if (c == 'P') {
dot_begin(); // putting lines- Put above
}
@@ -3196,7 +3133,7 @@ key_cmd_mode:
if (c == 'p')
dot_right(); // move to right, can move to NL
}
- dot = string_insert(dot, p); // insert the string
+ string_insert(dot, p); // insert the string
end_cmd_q(); // stop adding to q
break;
case 'U': // U- Undo; replace current line with original version
@@ -3204,18 +3141,18 @@ key_cmd_mode:
p = begin_line(dot);
q = end_line(dot);
p = text_hole_delete(p, q); // delete cur line
- p = string_insert(p, reg[Ureg]); // insert orig line
+ p += string_insert(p, reg[Ureg]); // insert orig line
dot = p;
dot_skip_over_ws();
}
break;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif /* FEATURE_VI_YANKMARK */
case '$': // $- goto end of line
- case VI_K_END: // Cursor Key End
+ case KEYCODE_END: // Cursor Key End
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
- dot = end_line(dot + 1);
+ dot = end_line(dot);
break;
case '%': // %- find matching char of pair () [] {}
for (q = dot; q < end && *q != '\n'; q++) {
@@ -3236,14 +3173,15 @@ key_cmd_mode:
case 'f': // f- forward to a user specified char
last_forward_char = get_one_char(); // get the search char
//
- // dont seperate these two commands. 'f' depends on ';'
+ // dont separate these two commands. 'f' depends on ';'
//
- //**** fall thru to ... 'i'
+ //**** fall through to ... ';'
case ';': // ;- look at rest of line for last forward char
if (cmdcnt-- > 1) {
do_cmd(';');
} // repeat cnt
- if (last_forward_char == 0) break;
+ if (last_forward_char == 0)
+ break;
q = dot + 1;
while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
q++;
@@ -3251,6 +3189,20 @@ key_cmd_mode:
if (*q == last_forward_char)
dot = q;
break;
+ case ',': // repeat latest 'f' in opposite direction
+ if (cmdcnt-- > 1) {
+ do_cmd(',');
+ } // repeat cnt
+ if (last_forward_char == 0)
+ break;
+ q = dot - 1;
+ while (q >= text && *q != '\n' && *q != last_forward_char) {
+ q--;
+ }
+ if (q >= text && *q == last_forward_char)
+ dot = q;
+ break;
+
case '-': // -- goto prev line
if (cmdcnt-- > 1) {
do_cmd(c);
@@ -3258,27 +3210,31 @@ key_cmd_mode:
dot_prev();
dot_skip_over_ws();
break;
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
+#if ENABLE_FEATURE_VI_DOT_CMD
case '.': // .- repeat the last modifying command
// Stuff the last_modifying_cmd back into stdin
// and let it be re-executed.
- if (last_modifying_cmd != 0) {
- ioq = ioq_start = (Byte *) bb_xstrdup((char *) last_modifying_cmd);
+ if (lmc_len > 0) {
+ last_modifying_cmd[lmc_len] = 0;
+ ioq = ioq_start = xstrdup(last_modifying_cmd);
}
break;
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
-#ifdef CONFIG_FEATURE_VI_SEARCH
+#endif
+#if ENABLE_FEATURE_VI_SEARCH
case '?': // /- search for a pattern
case '/': // /- search for a pattern
buf[0] = c;
buf[1] = '\0';
q = get_input_line(buf); // get input line- use "status line"
- if (strlen((char *) q) == 1)
- goto dc3; // if no pat re-use old pat
- if (strlen((char *) q) > 1) { // new pat- save it and find
+ if (q[0] && !q[1]) {
+ if (last_search_pattern[0])
+ last_search_pattern[0] = c;
+ goto dc3; // if no pat re-use old pat
+ }
+ if (q[0]) { // strlen(q) > 1: new pat- save it and find
// there is a new pat
free(last_search_pattern);
- last_search_pattern = (Byte *) bb_xstrdup((char *) q);
+ last_search_pattern = xstrdup(q);
goto dc3; // now find the pattern
}
// user changed mind and erased the "/"- do nothing
@@ -3301,24 +3257,18 @@ key_cmd_mode:
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
- dc3:
- if (last_search_pattern == 0) {
- msg = (Byte *) "No previous regular expression";
- goto dc2;
- }
- if (last_search_pattern[0] == '/') {
- dir = FORWARD; // assume FORWARD search
- p = dot + 1;
- }
+ dc3:
+ dir = FORWARD; // assume FORWARD search
+ p = dot + 1;
if (last_search_pattern[0] == '?') {
dir = BACK;
p = dot - 1;
}
- dc4:
+ dc4:
q = char_search(p, last_search_pattern + 1, dir, FULL);
if (q != NULL) {
dot = q; // good search, update "dot"
- msg = (Byte *) "";
+ msg = "";
goto dc2;
}
// no pattern found between "dot" and "end"- continue at top
@@ -3329,39 +3279,40 @@ key_cmd_mode:
q = char_search(p, last_search_pattern + 1, dir, FULL);
if (q != NULL) { // found something
dot = q; // found new pattern- goto it
- msg = (Byte *) "search hit BOTTOM, continuing at TOP";
+ msg = "search hit BOTTOM, continuing at TOP";
if (dir == BACK) {
- msg = (Byte *) "search hit TOP, continuing at BOTTOM";
+ msg = "search hit TOP, continuing at BOTTOM";
}
} else {
- msg = (Byte *) "Pattern not found";
+ msg = "Pattern not found";
}
- dc2:
- psbs("%s", msg);
+ dc2:
+ if (*msg)
+ status_line_bold("%s", msg);
break;
case '{': // {- move backward paragraph
- q = char_search(dot, (Byte *) "\n\n", BACK, FULL);
+ q = char_search(dot, "\n\n", BACK, FULL);
if (q != NULL) { // found blank line
dot = next_line(q); // move to next blank line
}
break;
case '}': // }- move forward paragraph
- q = char_search(dot, (Byte *) "\n\n", FORWARD, FULL);
+ q = char_search(dot, "\n\n", FORWARD, FULL);
if (q != NULL) { // found blank line
dot = next_line(q); // move to next blank line
}
break;
-#endif /* CONFIG_FEATURE_VI_SEARCH */
+#endif /* FEATURE_VI_SEARCH */
case '0': // 0- goto begining of line
- case '1': // 1-
- case '2': // 2-
- case '3': // 3-
- case '4': // 4-
- case '5': // 5-
- case '6': // 6-
- case '7': // 7-
- case '8': // 8-
- case '9': // 9-
+ case '1': // 1-
+ case '2': // 2-
+ case '3': // 3-
+ case '4': // 4-
+ case '5': // 5-
+ case '6': // 6-
+ case '7': // 7-
+ case '8': // 8-
+ case '9': // 9-
if (c == '0' && cmdcnt < 1) {
dot_begin(); // this was a standalone zero
} else {
@@ -3369,47 +3320,58 @@ key_cmd_mode:
}
break;
case ':': // :- the colon mode commands
- p = get_input_line((Byte *) ":"); // get input line- use "status line"
-#ifdef CONFIG_FEATURE_VI_COLON
+ p = get_input_line(":"); // get input line- use "status line"
+#if ENABLE_FEATURE_VI_COLON
colon(p); // execute the command
-#else /* CONFIG_FEATURE_VI_COLON */
+#else
if (*p == ':')
p++; // move past the ':'
- cnt = strlen((char *) p);
+ cnt = strlen(p);
if (cnt <= 0)
break;
- if (strncasecmp((char *) p, "quit", cnt) == 0 ||
- strncasecmp((char *) p, "q!", cnt) == 0) { // delete lines
+ if (strncasecmp(p, "quit", cnt) == 0
+ || strncasecmp(p, "q!", cnt) == 0 // delete lines
+ ) {
if (file_modified && p[1] != '!') {
- psbs("No write since last change (:quit! overrides)");
+ status_line_bold("No write since last change (:quit! overrides)");
} else {
editing = 0;
}
- } else if (strncasecmp((char *) p, "write", cnt) == 0 ||
- strncasecmp((char *) p, "wq", cnt) == 0 ||
- strncasecmp((char *) p, "x", cnt) == 0) {
- cnt = file_write(cfn, text, end - 1);
- file_modified = FALSE;
- psb("\"%s\" %dL, %dC", cfn, count_lines(text, end - 1), cnt);
- if (p[0] == 'x' || p[1] == 'q') {
- editing = 0;
+ } else if (strncasecmp(p, "write", cnt) == 0
+ || strncasecmp(p, "wq", cnt) == 0
+ || strncasecmp(p, "wn", cnt) == 0
+ || strncasecmp(p, "x", cnt) == 0
+ ) {
+ cnt = file_write(current_filename, text, end - 1);
+ if (cnt < 0) {
+ if (cnt == -1)
+ status_line_bold("Write error: %s", strerror(errno));
+ } else {
+ file_modified = 0;
+ last_file_modified = -1;
+ status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
+ if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
+ || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
+ ) {
+ editing = 0;
+ }
}
- } else if (strncasecmp((char *) p, "file", cnt) == 0 ) {
- edit_status(); // show current file status
- } else if (sscanf((char *) p, "%d", &j) > 0) {
+ } else if (strncasecmp(p, "file", cnt) == 0) {
+ last_status_cksum = 0; // force status update
+ } else if (sscanf(p, "%d", &j) > 0) {
dot = find_line(j); // go to line # j
dot_skip_over_ws();
} else { // unrecognised cmd
- ni((Byte *) p);
+ not_implemented(p);
}
-#endif /* CONFIG_FEATURE_VI_COLON */
+#endif /* !FEATURE_VI_COLON */
break;
case '<': // <- Left shift something
case '>': // >- Right shift something
cnt = count_lines(text, dot); // remember what line we are on
c1 = get_one_char(); // get the type of thing to delete
find_range(&p, &q, c1);
- (void) yank_delete(p, q, 1, YANKONLY); // save copy before change
+ yank_delete(p, q, 1, YANKONLY); // save copy before change
p = begin_line(p);
q = end_line(q);
i = count_lines(p, q); // # of lines we are shifting
@@ -3418,16 +3380,16 @@ key_cmd_mode:
// shift left- remove tab or 8 spaces
if (*p == '\t') {
// shrink buffer 1 char
- (void) text_hole_delete(p, p);
+ text_hole_delete(p, p);
} else if (*p == ' ') {
// we should be calculating columns, not just SPACE
for (j = 0; *p == ' ' && j < tabstop; j++) {
- (void) text_hole_delete(p, p);
+ text_hole_delete(p, p);
}
}
} else if (c == '>') {
// shift right -- add tab or 8 spaces
- (void) char_insert(p, '\t');
+ char_insert(p, '\t');
}
}
dot = find_line(cnt); // what line were we on
@@ -3436,7 +3398,7 @@ key_cmd_mode:
break;
case 'A': // A- append at e-o-l
dot_end(); // go to e-o-l
- //**** fall thru to ... 'a'
+ //**** fall through to ... 'a'
case 'a': // a- append after current char
if (*dot != '\n')
dot++;
@@ -3466,11 +3428,23 @@ key_cmd_mode:
dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
if (c == 'C')
goto dc_i; // start inserting
-#ifdef CONFIG_FEATURE_VI_DOT_CMD
+#if ENABLE_FEATURE_VI_DOT_CMD
if (c == 'D')
end_cmd_q(); // stop adding to q
-#endif /* CONFIG_FEATURE_VI_DOT_CMD */
+#endif
break;
+ case 'g': // 'gg' goto a line number (vim) (default: very first line)
+ c1 = get_one_char();
+ if (c1 != 'g') {
+ buf[0] = 'g';
+ buf[1] = c1; // TODO: if Unicode?
+ buf[2] = '\0';
+ not_implemented(buf);
+ break;
+ }
+ if (cmdcnt == 0)
+ cmdcnt = 1;
+ /* fall through */
case 'G': // G- goto to a line number (default= E-O-F)
dot = end - 1; // assume E-O-F
if (cmdcnt > 0) {
@@ -3491,12 +3465,11 @@ key_cmd_mode:
case 'I': // I- insert before first non-blank
dot_begin(); // 0
dot_skip_over_ws();
- //**** fall thru to ... 'i'
+ //**** fall through to ... 'i'
case 'i': // i- insert before current char
- case VI_K_INSERT: // Cursor Key Insert
- dc_i:
+ case KEYCODE_INSERT: // Cursor Key Insert
+ dc_i:
cmd_mode = 1; // start insrting
- psb("-- Insert --");
break;
case 'J': // J- join current and next lines together
if (cmdcnt-- > 2) {
@@ -3505,7 +3478,8 @@ key_cmd_mode:
dot_end(); // move to NL
if (dot < end - 1) { // make sure not last char in text[]
*dot++ = ' '; // replace NL with space
- while (isblnk(*dot)) { // delete leading WS
+ file_modified++;
+ while (isblank(*dot)) { // delete leading WS
dot_delete();
}
}
@@ -3543,10 +3517,12 @@ key_cmd_mode:
goto dc_i;
break;
case 'R': // R- continuous Replace char
- dc5:
+ dc5:
cmd_mode = 2;
- psb("-- Replace --");
break;
+ case KEYCODE_DELETE:
+ c = 'x';
+ // fall through
case 'X': // X- delete char before dot
case 'x': // x- delete the current char
case 's': // s- substitute the current char
@@ -3572,14 +3548,16 @@ key_cmd_mode:
indicate_error(c);
break;
}
- if (file_modified
-#ifdef CONFIG_FEATURE_VI_READONLY
- && ! vi_readonly
- && ! readonly
-#endif /* CONFIG_FEATURE_VI_READONLY */
- ) {
- cnt = file_write(cfn, text, end - 1);
- if (cnt == (end - 1 - text + 1)) {
+ if (file_modified) {
+ if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
+ status_line_bold("\"%s\" File is read only", current_filename);
+ break;
+ }
+ cnt = file_write(current_filename, text, end - 1);
+ if (cnt < 0) {
+ if (cnt == -1)
+ status_line_bold("Write error: %s", strerror(errno));
+ } else if (cnt == (end - 1 - text + 1)) {
editing = 0;
}
} else {
@@ -3612,79 +3590,87 @@ key_cmd_mode:
break;
case 'c': // c- change something
case 'd': // d- delete something
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
case 'y': // y- yank something
case 'Y': // Y- Yank a line
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
+ {
+ int yf, ml, whole = 0;
yf = YANKDEL; // assume either "c" or "d"
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
if (c == 'y' || c == 'Y')
yf = YANKONLY;
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
c1 = 'y';
if (c != 'Y')
c1 = get_one_char(); // get the type of thing to delete
- find_range(&p, &q, c1);
+ // determine range, and whether it spans lines
+ ml = find_range(&p, &q, c1);
if (c1 == 27) { // ESC- user changed mind and wants out
c = c1 = 27; // Escape- do nothing
} else if (strchr("wW", c1)) {
if (c == 'c') {
// don't include trailing WS as part of word
- while (isblnk(*q)) {
+ while (isblank(*q)) {
if (q <= text || q[-1] == '\n')
break;
q--;
}
}
- dot = yank_delete(p, q, 0, yf); // delete word
- } else if (strchr("^0bBeEft$", c1)) {
- // single line copy text into a register and delete
- dot = yank_delete(p, q, 0, yf); // delete word
- } else if (strchr("cdykjHL%+-{}\r\n", c1)) {
- // multiple line copy text into a register and delete
- dot = yank_delete(p, q, 1, yf); // delete lines
+ dot = yank_delete(p, q, ml, yf); // delete word
+ } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
+ // partial line copy text into a register and delete
+ dot = yank_delete(p, q, ml, yf); // delete word
+ } else if (strchr("cdykjHL+-{}\r\n", c1)) {
+ // whole line copy text into a register and delete
+ dot = yank_delete(p, q, ml, yf); // delete lines
+ whole = 1;
+ } else {
+ // could not recognize object
+ c = c1 = 27; // error-
+ ml = 0;
+ indicate_error(c);
+ }
+ if (ml && whole) {
if (c == 'c') {
dot = char_insert(dot, '\n');
// on the last line of file don't move to prev line
- if (dot != (end-1)) {
+ if (whole && dot != (end-1)) {
dot_prev();
}
} else if (c == 'd') {
dot_begin();
dot_skip_over_ws();
}
- } else {
- // could not recognize object
- c = c1 = 27; // error-
- indicate_error(c);
}
if (c1 != 27) {
// if CHANGING, not deleting, start inserting after the delete
if (c == 'c') {
- strcpy((char *) buf, "Change");
+ strcpy(buf, "Change");
goto dc_i; // start inserting
}
if (c == 'd') {
- strcpy((char *) buf, "Delete");
+ strcpy(buf, "Delete");
}
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
if (c == 'y' || c == 'Y') {
- strcpy((char *) buf, "Yank");
+ strcpy(buf, "Yank");
}
p = reg[YDreg];
- q = p + strlen((char *) p);
+ q = p + strlen(p);
for (cnt = 0; p <= q; p++) {
if (*p == '\n')
cnt++;
}
- psb("%s %d lines (%d chars) using [%c]",
- buf, cnt, strlen((char *) reg[YDreg]), what_reg());
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+ status_line("%s %d lines (%d chars) using [%c]",
+ buf, cnt, strlen(reg[YDreg]), what_reg());
+#endif
end_cmd_q(); // stop adding to q
}
break;
+ }
case 'k': // k- goto prev line, same col
- case VI_K_UP: // cursor key Up
+ case KEYCODE_UP: // cursor key Up
if (cmdcnt-- > 1) {
do_cmd(c);
} // repeat cnt
@@ -3695,16 +3681,16 @@ key_cmd_mode:
c1 = get_one_char(); // get the replacement char
if (*dot != '\n') {
*dot = c1;
- file_modified = TRUE; // has the file been modified
+ file_modified++;
}
end_cmd_q(); // stop adding to q
break;
case 't': // t- move to char prior to next x
- last_forward_char = get_one_char();
- do_cmd(';');
- if (*dot == last_forward_char)
- dot_left();
- last_forward_char= 0;
+ last_forward_char = get_one_char();
+ do_cmd(';');
+ if (*dot == last_forward_char)
+ dot_left();
+ last_forward_char = 0;
break;
case 'w': // w- forward a word
if (cmdcnt-- > 1) {
@@ -3740,47 +3726,49 @@ key_cmd_mode:
} // repeat cnt
if (islower(*dot)) {
*dot = toupper(*dot);
- file_modified = TRUE; // has the file been modified
+ file_modified++;
} else if (isupper(*dot)) {
*dot = tolower(*dot);
- file_modified = TRUE; // has the file been modified
+ file_modified++;
}
dot_right();
end_cmd_q(); // stop adding to q
break;
//----- The Cursor and Function Keys -----------------------------
- case VI_K_HOME: // Cursor Key Home
+ case KEYCODE_HOME: // Cursor Key Home
dot_begin();
break;
// The Fn keys could point to do_macro which could translate them
- case VI_K_FUN1: // Function Key F1
- case VI_K_FUN2: // Function Key F2
- case VI_K_FUN3: // Function Key F3
- case VI_K_FUN4: // Function Key F4
- case VI_K_FUN5: // Function Key F5
- case VI_K_FUN6: // Function Key F6
- case VI_K_FUN7: // Function Key F7
- case VI_K_FUN8: // Function Key F8
- case VI_K_FUN9: // Function Key F9
- case VI_K_FUN10: // Function Key F10
- case VI_K_FUN11: // Function Key F11
- case VI_K_FUN12: // Function Key F12
+#if 0
+ case KEYCODE_FUN1: // Function Key F1
+ case KEYCODE_FUN2: // Function Key F2
+ case KEYCODE_FUN3: // Function Key F3
+ case KEYCODE_FUN4: // Function Key F4
+ case KEYCODE_FUN5: // Function Key F5
+ case KEYCODE_FUN6: // Function Key F6
+ case KEYCODE_FUN7: // Function Key F7
+ case KEYCODE_FUN8: // Function Key F8
+ case KEYCODE_FUN9: // Function Key F9
+ case KEYCODE_FUN10: // Function Key F10
+ case KEYCODE_FUN11: // Function Key F11
+ case KEYCODE_FUN12: // Function Key F12
break;
+#endif
}
- dc1:
+ dc1:
// if text[] just became empty, add back an empty line
if (end == text) {
- (void) char_insert(text, '\n'); // start empty buf with dummy line
+ char_insert(text, '\n'); // start empty buf with dummy line
dot = text;
}
// it is OK for dot to exactly equal to end, otherwise check dot validity
if (dot != end) {
dot = bound_dot(dot); // make sure "dot" is valid
}
-#ifdef CONFIG_FEATURE_VI_YANKMARK
+#if ENABLE_FEATURE_VI_YANKMARK
check_context(c); // update the current context
-#endif /* CONFIG_FEATURE_VI_YANKMARK */
+#endif
if (!isdigit(c))
cmdcnt = 0; // cmd was not a number, reset cmdcnt
@@ -3790,7 +3778,8 @@ key_cmd_mode:
dot--;
}
-#ifdef CONFIG_FEATURE_VI_CRASHME
+/* NB! the CRASHME code is unmaintained, and doesn't currently build */
+#if ENABLE_FEATURE_VI_CRASHME
static int totalcmds = 0;
static int Mp = 85; // Movement command Probability
static int Np = 90; // Non-movement command Probability
@@ -3799,14 +3788,15 @@ static int Ip = 97; // Insert command Probability
static int Yp = 98; // Yank command Probability
static int Pp = 99; // Put command Probability
static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
-char chars[20] = "\t012345 abcdABCD-=.$";
-char *words[20] = { "this", "is", "a", "test",
+static const char chars[20] = "\t012345 abcdABCD-=.$";
+static const char *const words[20] = {
+ "this", "is", "a", "test",
"broadcast", "the", "emergency", "of",
"system", "quick", "brown", "fox",
"jumped", "over", "lazy", "dogs",
"back", "January", "Febuary", "March"
};
-char *lines[20] = {
+static const char *const lines[20] = {
"You should have received a copy of the GNU General Public License\n",
"char c, cm, *cmd, *cmd1;\n",
"generate a command by percentages\n",
@@ -3828,7 +3818,7 @@ char *lines[20] = {
"The last command will be automatically run.\n",
"This is too much english for a computer geek.\n",
};
-char *multilines[20] = {
+static char *multilines[20] = {
"You should have received a copy of the GNU General Public License\n",
"char c, cm, *cmd, *cmd1;\n",
"generate a command by percentages\n",
@@ -3862,12 +3852,12 @@ static void crash_dummy()
cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
// is there already a command running?
- if (readed_for_parse > 0)
+ if (chars_to_parse > 0)
goto cd1;
- cd0:
+ cd0:
startrbi = rbi = 0;
sleeptime = 0; // how long to pause between commands
- memset(readbuffer, '\0', BUFSIZ); // clear the read buffer
+ memset(readbuffer, '\0', sizeof(readbuffer));
// generate a command by percentages
percent = (int) lrand48() % 100; // get a number from 0-99
if (percent < Mp) { // Movement commands
@@ -3926,71 +3916,69 @@ static void crash_dummy()
if (thing == 0) { // insert chars
readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
} else if (thing == 1) { // insert words
- strcat((char *) readbuffer, words[(int) lrand48() % 20]);
- strcat((char *) readbuffer, " ");
+ strcat(readbuffer, words[(int) lrand48() % 20]);
+ strcat(readbuffer, " ");
sleeptime = 0; // how fast to type
} else if (thing == 2) { // insert lines
- strcat((char *) readbuffer, lines[(int) lrand48() % 20]);
+ strcat(readbuffer, lines[(int) lrand48() % 20]);
sleeptime = 0; // how fast to type
} else { // insert multi-lines
- strcat((char *) readbuffer, multilines[(int) lrand48() % 20]);
+ strcat(readbuffer, multilines[(int) lrand48() % 20]);
sleeptime = 0; // how fast to type
}
}
- strcat((char *) readbuffer, "\033");
+ strcat(readbuffer, "\033");
}
- readed_for_parse = strlen(readbuffer);
- cd1:
+ chars_to_parse = strlen(readbuffer);
+ cd1:
totalcmds++;
if (sleeptime > 0)
- (void) mysleep(sleeptime); // sleep 1/100 sec
+ mysleep(sleeptime); // sleep 1/100 sec
}
// test to see if there are any errors
static void crash_test()
{
static time_t oldtim;
+
time_t tim;
- char d[2], msg[BUFSIZ];
+ char d[2], msg[80];
msg[0] = '\0';
if (end < text) {
- strcat((char *) msg, "end<text ");
+ strcat(msg, "end<text ");
}
if (end > textend) {
- strcat((char *) msg, "end>textend ");
+ strcat(msg, "end>textend ");
}
if (dot < text) {
- strcat((char *) msg, "dot<text ");
+ strcat(msg, "dot<text ");
}
if (dot > end) {
- strcat((char *) msg, "dot>end ");
+ strcat(msg, "dot>end ");
}
if (screenbegin < text) {
- strcat((char *) msg, "screenbegin<text ");
+ strcat(msg, "screenbegin<text ");
}
if (screenbegin > end - 1) {
- strcat((char *) msg, "screenbegin>end-1 ");
+ strcat(msg, "screenbegin>end-1 ");
}
- if (strlen(msg) > 0) {
- alarm(0);
+ if (msg[0]) {
printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
totalcmds, last_input_char, msg, SOs, SOn);
fflush(stdout);
- while (read(0, d, 1) > 0) {
+ while (safe_read(STDIN_FILENO, d, 1) > 0) {
if (d[0] == '\n' || d[0] == '\r')
break;
}
- alarm(3);
}
- tim = (time_t) time((time_t *) 0);
+ tim = time(NULL);
if (tim >= (oldtim + 3)) {
- sprintf((char *) status_buffer,
+ sprintf(status_buffer,
"Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
totalcmds, M, N, I, D, Y, P, U, end - text + 1);
oldtim = tim;
}
- return;
}
-#endif /* CONFIG_FEATURE_VI_CRASHME */
+#endif