diff options
| author | Timo Savinen <tjsa@iki.fi> | 2005-10-23 11:36:39 +0000 |
|---|---|---|
| committer | Hadrien Dorio <hadrien.dorio@gmail.com> | 2017-12-16 00:24:06 +0100 |
| commit | 9794b5c44b97b5dda3064a96ca65376cb2b9ef4c (patch) | |
| tree | 9ab601eb14a3a789221334290b7c94339d387ed1 | |
| parent | d33af5172f7d8c403c097f0eadfcc35ed766e1fd (diff) | |
| download | binary-block-editor-9794b5c44b97b5dda3064a96ca65376cb2b9ef4c.tar.gz binary-block-editor-9794b5c44b97b5dda3064a96ca65376cb2b9ef4c.zip | |
0.1.5
| -rw-r--r-- | ChangeLog | 14 | ||||
| -rwxr-xr-x | configure | 20 | ||||
| -rw-r--r-- | configure.ac | 2 | ||||
| -rw-r--r-- | doc/Makefile.am | 13 | ||||
| -rw-r--r-- | doc/Makefile.in | 64 | ||||
| -rw-r--r-- | doc/bbe.1 | 7 | ||||
| -rw-r--r-- | doc/bbe.html | 531 | ||||
| -rw-r--r-- | doc/bbe.info | 42 | ||||
| -rw-r--r-- | doc/bbe.texi | 27 | ||||
| -rw-r--r-- | src/bbe.c | 7 | ||||
| -rw-r--r-- | src/bbe.h | 5 | ||||
| -rw-r--r-- | src/buffer.c | 12 | ||||
| -rw-r--r-- | src/execute.c | 129 | ||||
| -rw-r--r-- | src/xmalloc.c | 11 |
14 files changed, 806 insertions, 78 deletions
@@ -1,17 +1,23 @@ -2005-10-16 Timo Savinen <tjsa.iki.fi> +2005-10-23 Timo Savinen <tjsa@iki.fi> + + * Version 0.1.5 + * %B and %nB for block number in filename of w-command + * Documentation corrected + +2005-10-16 Timo Savinen <tjsa@iki.fi> * Version 0.1.4 * Better performace * bug in buffer definition /string/: and :/string/ fixed -2005-10-09 Timo Savinen <tjsa.iki.fi> +2005-10-09 Timo Savinen <tjsa@iki.fi> * Version 0.1.3 * bug in c BCD ASC * p b added * Typo in usage fixed -2005-10-02 Timo Savinen <tjsa.iki.fi> +2005-10-02 Timo Savinen <tjsa@iki.fi> * Version 0.1.2 * escape codes for \n \t \r \a \b \v \f @@ -20,7 +26,7 @@ * check for gnu functions, compiles ok in hp-ux with gcc. * Bitwise operators &,|,^ and ~ -2005-09-25 Timo Savinen <tjsa.iki.fi> +2005-09-25 Timo Savinen <tjsa@iki.fi> * Version 0.1.1 * Bug fixes (\\ escape in strings) @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.59 for bbe 0.1.4. +# Generated by GNU Autoconf 2.59 for bbe 0.1.5. # # Report bugs to <tjsa@iki.fi>. # @@ -269,8 +269,8 @@ SHELL=${CONFIG_SHELL-/bin/sh} # Identity of this package. PACKAGE_NAME='bbe' PACKAGE_TARNAME='bbe' -PACKAGE_VERSION='0.1.4' -PACKAGE_STRING='bbe 0.1.4' +PACKAGE_VERSION='0.1.5' +PACKAGE_STRING='bbe 0.1.5' PACKAGE_BUGREPORT='tjsa@iki.fi' ac_unique_file="src/bbe.c" @@ -780,7 +780,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures bbe 0.1.4 to adapt to many kinds of systems. +\`configure' configures bbe 0.1.5 to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -846,7 +846,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of bbe 0.1.4:";; + short | recursive ) echo "Configuration of bbe 0.1.5:";; esac cat <<\_ACEOF @@ -965,7 +965,7 @@ fi test -n "$ac_init_help" && exit 0 if $ac_init_version; then cat <<\_ACEOF -bbe configure 0.1.4 +bbe configure 0.1.5 generated by GNU Autoconf 2.59 Copyright (C) 2003 Free Software Foundation, Inc. @@ -979,7 +979,7 @@ cat >&5 <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by bbe $as_me 0.1.4, which was +It was created by bbe $as_me 0.1.5, which was generated by GNU Autoconf 2.59. Invocation command line was $ $0 $@ @@ -1623,7 +1623,7 @@ fi # Define the identity of the package. PACKAGE='bbe' - VERSION='0.1.4' + VERSION='0.1.5' cat >>confdefs.h <<_ACEOF @@ -4660,7 +4660,7 @@ _ASBOX } >&5 cat >&5 <<_CSEOF -This file was extended by bbe $as_me 0.1.4, which was +This file was extended by bbe $as_me 0.1.5, which was generated by GNU Autoconf 2.59. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -4723,7 +4723,7 @@ _ACEOF cat >>$CONFIG_STATUS <<_ACEOF ac_cs_version="\\ -bbe config.status 0.1.4 +bbe config.status 0.1.5 configured by $0, generated by GNU Autoconf 2.59, with options \\"`echo "$ac_configure_args" | sed 's/[\\""\`\$]/\\\\&/g'`\\" diff --git a/configure.ac b/configure.ac index a0bc2cc..5a6cd4c 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. -AC_INIT(bbe, 0.1.4, tjsa@iki.fi) +AC_INIT(bbe, 0.1.5, tjsa@iki.fi) AC_CONFIG_SRCDIR(src/bbe.c) AC_CONFIG_AUX_DIR(config) AM_INIT_AUTOMAKE diff --git a/doc/Makefile.am b/doc/Makefile.am index 5d42458..3dc473f 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -1,4 +1,15 @@ man_MANS = bbe.1 info_TEXINFOS = bbe.texi +CLEANFILES = bbe.html -EXTRA_DIST = $(man_MANS) $(info_TEXINFOS) +docdir = $(datadir)/doc +doc_DATA = bbe.html +AM_MAKEINFOHTMLFLAGS=--no-split + +bbe.html: bbe.texi bbe.info + +html: bbe.html + +.PHONY: html + +EXTRA_DIST = $(man_MANS) $(info_TEXINFOS) $(doc_DATA) diff --git a/doc/Makefile.in b/doc/Makefile.in index 48f5fd8..d0a0764 100644 --- a/doc/Makefile.in +++ b/doc/Makefile.in @@ -13,6 +13,7 @@ # PARTICULAR PURPOSE. @SET_MAKE@ + srcdir = @srcdir@ top_srcdir = @top_srcdir@ VPATH = @srcdir@ @@ -57,12 +58,20 @@ TEXINFOS = bbe.texi TEXI2DVI = texi2dvi TEXI2PDF = $(TEXI2DVI) --pdf --batch MAKEINFOHTML = $(MAKEINFO) --html -AM_MAKEINFOHTMLFLAGS = $(AM_MAKEINFOFLAGS) DVIPS = dvips -am__installdirs = "$(DESTDIR)$(infodir)" "$(DESTDIR)$(man1dir)" +am__installdirs = "$(DESTDIR)$(infodir)" "$(DESTDIR)$(man1dir)" \ + "$(DESTDIR)$(docdir)" man1dir = $(mandir)/man1 NROFF = nroff MANS = $(man_MANS) +am__vpath_adj_setup = srcdirstrip=`echo "$(srcdir)" | sed 's|.|.|g'`; +am__vpath_adj = case $$p in \ + $(srcdir)/*) f=`echo "$$p" | sed "s|^$$srcdirstrip/||"`;; \ + *) f=$$p;; \ + esac; +am__strip_dir = `echo $$p | sed -e 's|^.*/||'`; +docDATA_INSTALL = $(INSTALL_DATA) +DATA = $(doc_DATA) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) ACLOCAL = @ACLOCAL@ AMDEP_FALSE = @AMDEP_FALSE@ @@ -146,7 +155,11 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ man_MANS = bbe.1 info_TEXINFOS = bbe.texi -EXTRA_DIST = $(man_MANS) $(info_TEXINFOS) +CLEANFILES = bbe.html +docdir = $(datadir)/doc +doc_DATA = bbe.html +AM_MAKEINFOHTMLFLAGS = --no-split +EXTRA_DIST = $(man_MANS) $(info_TEXINFOS) $(doc_DATA) all: all-am .SUFFIXES: @@ -229,7 +242,6 @@ $(ACLOCAL_M4): $(am__aclocal_m4_deps) $(srcdir)/bbe.info: bbe.texi bbe.dvi: bbe.texi bbe.pdf: bbe.texi -bbe.html: bbe.texi .dvi.ps: TEXINPUTS="$(am__TEXINFO_TEX_DIR)$(PATH_SEPARATOR)$$TEXINPUTS" \ $(DVIPS) -o $@ $< @@ -327,6 +339,23 @@ uninstall-man1: echo " rm -f '$(DESTDIR)$(man1dir)/$$inst'"; \ rm -f "$(DESTDIR)$(man1dir)/$$inst"; \ done +install-docDATA: $(doc_DATA) + @$(NORMAL_INSTALL) + test -z "$(docdir)" || $(mkdir_p) "$(DESTDIR)$(docdir)" + @list='$(doc_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f=$(am__strip_dir) \ + echo " $(docDATA_INSTALL) '$$d$$p' '$(DESTDIR)$(docdir)/$$f'"; \ + $(docDATA_INSTALL) "$$d$$p" "$(DESTDIR)$(docdir)/$$f"; \ + done + +uninstall-docDATA: + @$(NORMAL_UNINSTALL) + @list='$(doc_DATA)'; for p in $$list; do \ + f=$(am__strip_dir) \ + echo " rm -f '$(DESTDIR)$(docdir)/$$f'"; \ + rm -f "$(DESTDIR)$(docdir)/$$f"; \ + done tags: TAGS TAGS: @@ -366,9 +395,9 @@ distdir: $(DISTFILES) dist-info check-am: all-am check: check-am -all-am: Makefile $(INFO_DEPS) $(MANS) +all-am: Makefile $(INFO_DEPS) $(MANS) $(DATA) installdirs: - for dir in "$(DESTDIR)$(infodir)" "$(DESTDIR)$(man1dir)"; do \ + for dir in "$(DESTDIR)$(infodir)" "$(DESTDIR)$(man1dir)" "$(DESTDIR)$(docdir)"; do \ test -z "$$dir" || $(mkdir_p) "$$dir"; \ done install: install-am @@ -388,6 +417,7 @@ install-strip: mostlyclean-generic: clean-generic: + -test -z "$(CLEANFILES)" || rm -f $(CLEANFILES) distclean-generic: -test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES) @@ -407,15 +437,13 @@ dvi: dvi-am dvi-am: $(DVIS) -html: html-am - html-am: $(HTMLS) info: info-am info-am: $(INFO_DEPS) -install-data-am: install-info-am install-man +install-data-am: install-docDATA install-info-am install-man install-exec-am: @@ -472,21 +500,27 @@ ps: ps-am ps-am: $(PSS) -uninstall-am: uninstall-info-am uninstall-man +uninstall-am: uninstall-docDATA uninstall-info-am uninstall-man uninstall-man: uninstall-man1 .PHONY: all all-am check check-am clean clean-generic dist-info \ distclean distclean-generic distdir dvi dvi-am html html-am \ info info-am install install-am install-data install-data-am \ - install-exec install-exec-am install-info install-info-am \ - install-man install-man1 install-strip installcheck \ - installcheck-am installdirs maintainer-clean \ + install-docDATA install-exec install-exec-am install-info \ + install-info-am install-man install-man1 install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ maintainer-clean-aminfo maintainer-clean-generic mostlyclean \ mostlyclean-aminfo mostlyclean-generic pdf pdf-am ps ps-am \ - uninstall uninstall-am uninstall-info-am uninstall-man \ - uninstall-man1 + uninstall uninstall-am uninstall-docDATA uninstall-info-am \ + uninstall-man uninstall-man1 + + +bbe.html: bbe.texi bbe.info + +html: bbe.html +.PHONY: html # Tell versions [3.59,3.63) of GNU make to not export all variables. # Otherwise a system limit (for SysV at least) may be exceeded. .NOEXPORT: @@ -25,7 +25,7 @@ .\" .br insert line break .\" .sp <n> insert n+1 empty lines .\" for manpage-specific macros, see man(7) -.TH "BBE" "1" "2005-10-05" "" "" +.TH "BBE" "1" "2005-10-20" "" "" .SH "NAME" bbe \- binary block editor .SH "SYNOPSIS" @@ -129,7 +129,7 @@ Before printing a block, the block number is printed (first block == 1) \fIf\fR can be H, D or O for Hexadecimal, Decimal or Octal format of block number. .LP \fBByte commands\fR -.br +.TP \fIn\fR in byte commands is offset from the beginning of current block (starts from zero). .TP r \fIn\fR \fIstring\fR @@ -167,7 +167,8 @@ l \fIn\fR Commands after the l\-command are ignored from \fIn\fR'th byte of the block. .TP w \fIfile\fR -Write bytes from the current block to file \fIfile\fR. Commands before w\-command have effect to what will be written. +Write bytes from the current block to file \fIfile\fR. Commands before w\-command have effect to what will be written. %B or %nB in \fIfile\fR will be replaced by current block number. n in %nB is field length, +leading zero in n causes the block number to be left padded with zeroes. .TP & \fIbyte\fR Performs binary and with \fIbyte\fR. diff --git a/doc/bbe.html b/doc/bbe.html new file mode 100644 index 0000000..5f266e3 --- /dev/null +++ b/doc/bbe.html @@ -0,0 +1,531 @@ +<html lang="en"> +<head> +<title>bbe - binary block editor</title> +<meta http-equiv="Content-Type" content="text/html"> +<meta name="description" content="bbe - binary block editor"> +<meta name="generator" content="makeinfo 4.7"> +<link title="Top" rel="top" href="#Top"> +<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> +<!-- +This file documents the `bbe' - binary block editor + +Copyright (C) 2005 Timo Savinen + + Permission is granted to make and distribute verbatim copies of + this manual provided the copyright notice and this permission + notice are preserved on all copies. + + Permission is granted to copy and distribute modified versions of + this manual under the conditions for verbatim copying, provided + that the entire resulting derived work is distributed under the + terms of a permission notice identical to this one. + + Permission is granted to copy and distribute translations of this + manual into another language, under the above conditions for + modified versions. + --> +<meta http-equiv="Content-Style-Type" content="text/css"> +<style type="text/css"><!-- + pre.display { font-family:inherit } + pre.format { font-family:inherit } + pre.smalldisplay { font-family:inherit; font-size:smaller } + pre.smallformat { font-family:inherit; font-size:smaller } + pre.smallexample { font-size:smaller } + pre.smalllisp { font-size:smaller } + span.sc { font-variant:small-caps } + span.roman { font-family: serif; font-weight: normal; } +--></style> +</head> +<body> +<h1 class="settitle">bbe - binary block editor</h1> +<div class="node"> +<p><hr> +<a name="Top"></a>Next: <a rel="next" accesskey="n" href="#Overview">Overview</a>, +Previous: <a rel="previous" accesskey="p" href="#dir">(dir)</a>, +Up: <a rel="up" accesskey="u" href="#dir">(dir)</a> +<br> +</div> + +<h2 class="unnumbered">bbe</h2> + +<p>This file documents the <span class="command">bbe</span> - binary block editor + + <p>Copyright © 2005 Timo Savinen + + <blockquote> +Permission is granted to make and distribute verbatim copies of +this manual provided the copyright notice and this permission notice +are preserved on all copies. + + <p>Permission is granted to copy and distribute modified versions of this +manual under the conditions for verbatim copying, provided that the entire +resulting derived work is distributed under the terms of a permission +notice identical to this one. + + <p>Permission is granted to copy and distribute translations of this manual +into another language, under the above conditions for modified versions. +</blockquote> + +<!-- All the menus can be updated with the EMACS command --> +<!-- texinfo-all-menus-update, which is normally bound to C-c C-u C-a. --> +<ul class="menu"> +<li><a accesskey="1" href="#Overview">Overview</a>: Preliminary information. +<li><a accesskey="2" href="#Samples">Samples</a>: Samples using <span class="command">bbe</span>. +<li><a accesskey="3" href="#Invoking-bbe">Invoking bbe</a>: How to run <span class="command">bbe</span>. +<li><a accesskey="4" href="#bbe-programs">bbe programs</a>: How <span class="command">bbe</span> works. +<li><a accesskey="5" href="#Problems">Problems</a>: Reporting bugs. +</ul> + +<div class="node"> +<p><hr> +<a name="Overview"></a>Next: <a rel="next" accesskey="n" href="#Samples">Samples</a>, +Previous: <a rel="previous" accesskey="p" href="#Top">Top</a>, +Up: <a rel="up" accesskey="u" href="#Top">Top</a> +<br> +</div> + +<h2 class="chapter">1 Preliminary information</h2> + +<p><a name="index-greetings-1"></a><a name="index-overview-2"></a> +The <span class="command">bbe</span> program is a sed-like editor for binary files. <span class="command">bbe</span> +performs basic byte related transformations on blocks of input stream. <span class="command">bbe</span> is non-interactive command line tool and +can be used as a part of a pipeline. <span class="command">bbe</span> makes only one pass over input stream. + + <p><span class="command">bbe</span> contains also grep-like features, like printing the filename, offset and block number. + +<div class="node"> +<p><hr> +<a name="Samples"></a>Next: <a rel="next" accesskey="n" href="#Invoking-bbe">Invoking bbe</a>, +Previous: <a rel="previous" accesskey="p" href="#Overview">Overview</a>, +Up: <a rel="up" accesskey="u" href="#Top">Top</a> +<br> +</div> + +<h2 class="chapter">2 Samples using <span class="command">bbe</span></h2> + +<p><a name="index-sample-3"></a> +Few examples of running <span class="command">bbe</span>: + + <dl> +<dt><span class="samp">bbe -b "/\x80\x50\x0e/:12" -e "d 0 3" -e "c BCD ASC" -e "A \x0a" -e "w /tmp/numbers" -o /dev/null /tmp/bdata</span><dd>Task here is to extract BCD coded numbers from the file <span class="file">/tmp/bdata</span> and write them +in ascii format with newline to file <span class="file">/tmp/numbers</span>. +12 bytes long blocks containing the BCD-numbers start with three byte sequence of +values <span class="samp">0x80</span>, <span class="samp">0x50</span> and <span class="samp">0x0e</span>. First three bytes (the block start sequence) +are removed (<span class="samp">d 0 3</span>) rest of the block is transformed from BCD to Ascii (<span class="samp">c BCD ASC</span>) and a newline character is +appended at the end of the block (<span class="samp">A \x0a</span>). +All transformed blocks are written to <span class="file">/tmp/numbers</span> (<span class="samp">w /tmp/numbers</span>). Nothing is written to +the output (<span class="samp">-o /dev/null</span>). +<br> + + <br><dt><span class="samp">bbe -b ":525" -e "i 524 \x0a" -o /tmp/data_with_nl /tmp/data</span><dd>A newline is added after every 525'th byte of the file <span class="file">/tmp/data</span>. Data with newlines is written to <span class="file">/tmp/data_with_nl</span>. +<br> + + <br><dt><span class="samp">bbe -b ":526" -e "d 525 1" -o /tmp/data /tmp/data_with_nl</span><dd>Every 526'th byte (inserted newline in previous example) is removed from the file <span class="file">/tmp/data_with_nl</span>. Data without newlines is written to <span class="file">/tmp/data</span>. +<br> + + <br><dt><span class="samp">bbe -e "s/\x0d\x0a/\x0a/"</span><dd>Same effect as has command <span class="command">dos2unix</span>. +</dl> + +<div class="node"> +<p><hr> +<a name="Invoking-bbe"></a>Next: <a rel="next" accesskey="n" href="#bbe-programs">bbe programs</a>, +Previous: <a rel="previous" accesskey="p" href="#Samples">Samples</a>, +Up: <a rel="up" accesskey="u" href="#Top">Top</a> +<br> +</div> + +<h2 class="chapter">3 How to run <span class="command">bbe</span></h2> + +<p><a name="index-running-bbe-4"></a><a name="index-using-5"></a> +<span class="command">bbe</span> accepts several commands to operate on blocks. Commands are +executed in the same order as they appear in command line or in a script file. +Order is significant, because the changes made to current byte by previous commands are +seen by next commands. + +<ul class="menu"> +<li><a accesskey="1" href="#Invocation">Invocation</a>: Program invocation +<li><a accesskey="2" href="#Block">Block</a>: Block definition +<li><a accesskey="3" href="#Commands">Commands</a>: bbe commands +<li><a accesskey="4" href="#Limits">Limits</a>: Limitations +</ul> + +<div class="node"> +<p><hr> +<a name="Invocation"></a>Next: <a rel="next" accesskey="n" href="#Block">Block</a>, +Up: <a rel="up" accesskey="u" href="#Invoking-bbe">Invoking bbe</a> +<br> +</div> + +<h3 class="section">3.1 Program invocation</h3> + +<p><a name="index-options-6"></a> +The format for running the <span class="command">bbe</span> program is: + +<pre class="example"> bbe <var>option</var> ... +</pre> + <p><code>bbe</code> supports the following options: + +<!-- Formatting copied from the Texinfo 4.0 manual. --> +<dl> +<dt><code>-b </code><var>BLOCK</var><dt><code>--block=</code><var>BLOCK</var><dd>Block definition. + + <br><dt><code>-e </code><var>COMMAND</var><dt><code>--expression=</code><var>COMMAND</var><dd>Add command(s) to the commands to be executed. Commands must separated by semicolon. + + <br><dt><code>-f </code><var>script-file</var><dt><code>--file=</code><var>script-file</var><dd>Add commands from <var>script-file</var> to the commands to be executed. + + <br><dt><code>-o </code><var>file</var><dt><code>--output=</code><var>file</var><dd>Write output to <var>file</var> instead of standard output. + + <br><dt><code>-s</code><dt><code>--suppress</code><dd>Suppress printing of normal output, print only block contents. + + <br><dt><code>-?</code><dt><code>--help</code><dd>Print an informative help message describing the options and then exit +successfully. + + <br><dt><code>-V</code><dt><code>--version</code><dd>Print the version number of <span class="command">bbe</span> and then exit successfully. +</dl> + + <p>All remaining options are names of input files, if no input files are specified or <code>-</code> is given, then the standard input is read. + +<div class="node"> +<p><hr> +<a name="Block"></a>Next: <a rel="next" accesskey="n" href="#Commands">Commands</a>, +Previous: <a rel="previous" accesskey="p" href="#Invocation">Invocation</a>, +Up: <a rel="up" accesskey="u" href="#Invoking-bbe">Invoking bbe</a> +<br> +</div> + +<h3 class="section">3.2 Block definition</h3> + +<p><a name="index-block-7"></a> +<span class="command">bbe</span> devides the input stream to blocks defined by the <code>-b</code> option. If block is +not defined, the whole input stream is considered as one block. Commands have effect only inside a block, rest of the input stream +remains untouched. Currently <span class="command">bbe</span> supports only one block definition per invocation. If input stream consists of different blocks, +several <span class="command">bbe</span>s can be combined in a pipeline. + + <p>A block can be defined several ways: + + <dl> +<dt><var>N</var><code>:</code><var>M</var><dd>Block starts at offset <var>N</var> of input stream (first byte is 0). Block is <var>M</var> bytes long. This definition allows +only one block to be defined. + + <br><dt><code>:</code><var>M</var><dd>The whole input stream is divided to <var>M</var>-size blocks. + + <br><dt><code>/</code><var>start</var><code>/:/</code><var>stop</var><code>/</code><dd>Blocks start with sequence <var>start</var> and end with sequence <var>stop</var>. Both <var>start</var> and <var>stop</var> are included to blocks. + + <br><dt><code>/</code><var>start</var><code>/:</code><dd>Blocks start with sequence <var>start</var> and ends at next occurrence of <var>start</var>. Only the first <var>start</var> is included to block. + + <br><dt><code>:/</code><var>stop</var><code>/</code><dd>Blocks start at the beginning of input stream or after the end of previous block. Block ends at first occurrence of <var>stop</var>. +Only the last <var>stop</var> is included to blocks. +</dl> + + <p>It is possible to use c-like byte values in <var>N</var>, <var>M</var>, <var>start</var> and <var>stop</var>. +Values in <var>start</var> and <var>stop</var> must be escaped with <code>\</code>, <code>\</code> can be escaped as <code>\\</code>. + + <p>Byte values can be expressed in decimal, octal or hexadecimal e.g. in <var>start</var> and <var>stop</var>: + + <dl> +<dt><code>\123, \32 or \0</code><dd>Decimal values + + <br><dt><code>\012, \08 or \0278</code><dd>Octal values + + <br><dt><code>\x0a, \x67 or \xff</code><dd>Hexadecimal values +</dl> + + <p>Also escape code <code>\y</code> can be used. Decimal values of <code>\y</code>'s: + + <dl> +<dt><code>\a</code><dd>7 + + <br><dt><code>\b</code><dd>8 + + <br><dt><code>\t</code><dd>9 + + <br><dt><code>\n</code><dd>10 + + <br><dt><code>\v</code><dd>11 + + <br><dt><code>\f</code><dd>12 + + <br><dt><code>\r</code><dd>13 + + <br><dt><code>\;</code><dd>59<br>Semicolon must be escaped, because it is a command delimitter. +</dl> + + <p>Values of <var>N</var> and <var>M</var> can be given in decimal, octal and hexadecimal: + + <dl> +<dt><code>123, 32 or 112232</code><dd>Decimal values + + <br><dt><code>0128, 08123 or 0</code><dd>Octal values + + <br><dt><code>x456a, x167 or xffde</code><dd>Hexadecimal values +</dl> + +<div class="node"> +<p><hr> +<a name="Commands"></a>Next: <a rel="next" accesskey="n" href="#Limits">Limits</a>, +Previous: <a rel="previous" accesskey="p" href="#Block">Block</a>, +Up: <a rel="up" accesskey="u" href="#Invoking-bbe">Invoking bbe</a> +<br> +</div> + +<h3 class="section">3.3 <span class="command">bbe</span> commands</h3> + +<p><a name="index-commands-8"></a> +Commands in <span class="command">bbe</span> can be divided in two groups: Block related commands +and byte related commands. Block related commands operate at block level e.g. remove a block. +Byte related commands work allways inside a block and they don't have effect beyond the block boundaries. + + <p>Same escape codes for byte values in <var>string</var>s can be used as in <var>start</var> and <var>stop</var> of block definition. + +<h4 class="subheading">Block commands are:</h4> + + <dl> +<dt><code>I </code><var>string</var><dd>Write the <var>string</var> to output stream before the block. + + <br><dt><code>D [</code><var>N</var><code>]</code><dd>Delete the <var>N</var>'th block. If <var>N</var> is not defined all blocks are deleted from output stream. +<strong>Note</strong>: First block is number one. + + <br><dt><code>A </code><var>string</var><dd>Write the <var>string</var> to output stream after the block. + + <br><dt><code>J </code><var>N</var><dd>Commands appearing after this command have no effect until <var>N</var> blocks are found. Means "Jump first <var>N</var> blocks". +<strong>Note</strong>: Commands that are defined before this command have effect on every block. + + <br><dt><code>L </code><var>N</var><dd>Commands appearing after this command have no effect after <var>N</var> blocks are found. Means "Leave blocks after <var>N</var>'th block". +<strong>Note</strong>: Commands that are defined before this command have effect on every block. + + <br><dt><code>N</code><dd>Before block contents the file name where the current block starts is printed with colon. + + <br><dt><code>F </code><var>f</var><dd>Before block contents the current stream offset and colon is printed in format specified by <var>f</var>. +Stream offset starts at zero. <var>f</var> can have one of following values: + <dl> +<dt><var>H</var><dd>Hexadecimal. + + <br><dt><var>D</var><dd>Decimal. + + <br><dt><var>O</var><dd>Octal. +</dl> + + <br><dt><code>B </code><var>f</var><dd>Before block contents the current block number and colon is printed in format specified by <var>f</var>. +Block numbering starts at one. <var>f</var> can have one of the sames codes as <code>F</code>-command +</dl> + +<h4 class="subheading">Byte commands are:</h4> + +<p><strong>Note</strong>: The <var>n</var> in byte commands is offset from the beginning of current block, first byte is number zero. + + <dl> +<dt><code>c </code><var>from</var> <var>to</var><dd>Converts bytes from <var>from</var> to <var>to</var>. Currently supported formats are: + <dl> +<dt><code>ASC</code><dd>Ascii + + <br><dt><code>BCD</code><dd>Binary Coded Decimal +</dl> + <strong>Note</strong>: Bytes, that cannot be converted are passed through as they are. e.g. in ASC -> BCD conversion, ASCII characters not +in range <code>'0'</code> – <code>'9'</code> are not converted. + + <br><dt><code>d </code><var>n</var> <var>m</var><dd>Delete <var>m</var> bytes starting from the offset <var>n</var>. + + <br><dt><code>i </code><var>n</var> <var>string</var><dd>Insert <var>string</var> after byte number <var>n</var>. + + <br><dt><code>j </code><var>n</var><dd>Commands appearing after <code>j</code>-command have no effect concerning bytes 0-<var>n</var> of the block. + + <br><dt><code>l </code><var>n</var><dd>Commands appearing after <code>l</code>-command have no effect concerning bytes starting from the byte number <var>n</var> of the block. + + <br><dt><code>p </code><var>format</var><dd>Contents of block is printed in formats specified by <var>format</var>. <var>format</var> can contain following format codes: + <dl> +<dt><var>H</var><dd>Hexadecimal. + + <br><dt><var>D</var><dd>Decimal. + + <br><dt><var>O</var><dd>Octal. + + <br><dt><var>A</var><dd>Ascii, nonprintable characters are printed as space. + + <br><dt><var>B</var><dd>Binary. +</dl> + + <p><var>format</var> can contain several codes, values are then separated by hyphen. + + <br><dt><code>r </code><var>n</var> <var>string</var><dd>Replace bytes with <var>string</var> starting at the byte number <var>n</var> of the block. + + <br><dt><code>s/</code><var>search</var><code>/</code><var>replace</var><code>/</code><dd>All occurences of <var>search</var> are replaced by <var>replace</var>. <var>replace</var> can be empty. Separator <code>/</code> can be replaced by any +character not present in <var>search</var> or <var>replace</var>. + + <br><dt><code>w </code><span class="file">file</span><dd>Contents of blocks are written to file <span class="file">file</span>. <strong>Note</strong>: Data inserted by commands <code>A</code> and <code>I</code> +are written to file <span class="file">file</span> and <code>j</code> and <code>l</code> commands have no effect on <code>w</code>-commands. Zero size files are not preserved.<br> +Filename can contain format string <code>%B</code> or <code>%nB</code>, these format strings are replace by current block number (starting from one), causing every block to have it's own file. +In <code>%nB</code>, the <code>n</code> is field width in range 0-99. If <code>n</code> has a leading zero, then the block numbers will be left padded with zeroes. + + <br><dt><code>y/</code><var>source</var><code>/</code><var>dest</var><code>/</code><dd>Translate bytes in <var>source</var> to the corresponding bytes in <var>dest</var>. <var>source</var> and <var>dest</var> must have equal length. +Separator <code>/</code> can be replaced by any character not present in <var>source</var> or <var>dest</var>. + + <br><dt><code>& c</code><dd>Performs binary and with <var>c</var> on block contents. + + <br><dt><code>| c</code><dd>Performs binary or with <var>c</var> on block contents. + + <br><dt><code>^ c</code><dd>Performs exclusive or with <var>c</var> on block contents. + + <br><dt><code>~</code><dd>Performs binary negation on block contents. +</dl> + +<div class="node"> +<p><hr> +<a name="Limits"></a>Previous: <a rel="previous" accesskey="p" href="#Commands">Commands</a>, +Up: <a rel="up" accesskey="u" href="#Invoking-bbe">Invoking bbe</a> +<br> +</div> + +<h3 class="section">3.4 Limitations</h3> + +<p><a name="index-big-files-9"></a><a name="index-limits-10"></a> +At least in GNU/Linux <span class="command">bbe</span> should be able to handle big files (> 4 GB), other +systems are not tested. + + <p>There are however, some limitations in block and command definitions: + + <dl> +<dt><em>Strings in block definition</em><dt><em>Search string in </em><code>s</code><em> command</em><dd>are limited to <em>16384</em> bytes. +<br> + + <br><dt><em>Total data to be inserted by </em><code>s</code><em> and </em><code>i</code><em> commands</em><dd>is <em>245760</em> bytes, that means the total data in command definitions, of course it can be inserted several times. +<br> +</dl> + +<div class="node"> +<p><hr> +<a name="bbe-programs"></a>Next: <a rel="next" accesskey="n" href="#Problems">Problems</a>, +Previous: <a rel="previous" accesskey="p" href="#Invoking-bbe">Invoking bbe</a>, +Up: <a rel="up" accesskey="u" href="#Top">Top</a> +<br> +</div> + +<h2 class="chapter">4 How <span class="command">bbe</span> works</h2> + +<p><span class="command">bbe</span> scans the input stream just once, so the last block may differ from the block definition, because <span class="command">bbe</span> doesn't +'peek' the end of the input stream. Last block may be shorter than defined, e.g. if block is defined as <span class="samp">/string/:128</span> +and if the end of input stream is found before 128'th byte of the last block is reached, the last block remains shorter. + +<h4 class="subheading">Basic execution cycle:</h4> + + <ol type=1 start=1> +<li>Start of the block is searched. If found, data before block is written to output stream (unless <span class="option">-s</span> is defined) and +step 2 is executed. + + <li>Block commands affecting the start of the block (<code>I</code>, <code>D</code>, <code>J</code>, <code>N</code>, <code>F</code> and <code>B</code>) are executed. + + <li>The block is scanned byte by byte and all byte commands (lower case letters) are executed. +<strong>Note</strong>: Commands are executed on results of previous commands, if e.g. the first byte of the block is deleted, +the following commands don't 'see' the removed byte. + + <li>When end of the block is reached the end of the block commands (<code>A</code>) are executed. + + <li>Next block is searched, data between the blocks, if not suppressed with <span class="option">-s</span>, is written to output stream. + </ol> + +<h4 class="subheading">Few examples:</h4> + + <dl> +<dt><span class="samp">echo "The quick brown fox jumps over a lazy dog" | bbe -b "/The/:21" -e "j 4" -e "s/ /X/"</span><dd>Output is + <pre class="example"> The quickXbrownXfoxXjumps over a lazy dog + </pre> + <p>The only block in this is + <pre class="example"> The quick brown fox j + </pre> + <p>All spaces in the block are converted to X's, before conversion first 4 bytes are skipped.<br> + + <br><dt><span class="samp">echo "The quick brown fox jumps over a lazy dog" | bbe -b ":/ /" -e "J 1" -e "A \x0a"</span><dd>Output is: + <pre class="example"> The quick + brown + fox + jumps + over + a + lazy + dog + + </pre> + <p>All blocks end at space, a newline character is inserted after every block except the first block.<br> + + <br><dt><span class="samp">echo "The quick brown fox jumps over a lazy dog" | bbe -e "r 4 fast\x20" -e "s/f/c/"</span><dd>Output is: + <pre class="example"> The cast brown cox jumps over a lazy dog + </pre> + <p>Also the <code>f</code> in <code>fast</code> is converted to <code>c</code>.<br> + + <br><dt><span class="samp">echo "1234567890" | bbe -b ":1" -e "L 9" -e "A -"</span><dd>Output is + <pre class="example"> 1-2-3-4-5-6-7-8-9-0 + </pre> + <p>Hyphen is inserted after every 1 byte long block,but not after 9'th block.<br> + + <br><dt><span class="samp">bbe -s -b "/First line/:/Last line/" /tmp/text</span><dd>Print lines between sentences <span class="samp">First line</span> and <span class="samp">Last line</span>.<br> + + <br><dt><span class="samp">bbe -s -b "%<a %:%</a>%" -e "s/\x0a/ /" -e "A \n" ./index.html</span><dd>Extract all links from <span class="file">./index.html</span>. To get one link per line, +all newlines are converted to spaces and newline is added after every link.<br> + + <br><dt><span class="samp">bbe -b "/\x5f\x28\x02/:10" -s -e "F d" -e "p h" -e "A \n" ./bindata</span><dd>10 bytes long sequences starting with values <code>x5f</code> <code>x28</code> and <code>x02</code> are printed as hex values. +Also the file offset is printed before each sequence and new line is added after every sequence. +Example output: + <pre class="example"> 52688:x5f x28 x02 x32 x36 x5f x81 x64 x01 x93 + 68898:x5f x28 x02 x39 x46 x5f x81 x64 x41 x05 + 69194:x5f x28 x02 x42 x36 x5f x81 x64 x41 x05 + </pre> + <p><br> + + <br><dt><span class="samp">bbe -b "/Linux/:5" -s -e "N;D;A \x0a" /bin/* | uniq</span><dd>Print the files names of those programs in /bin directory which contains word <span class="samp">Linux</span>. +Example output: + <pre class="example"> /bin/loadkeys: + /bin/mkbimage: + /bin/ps: + /bin/uname: + </pre> + <p><br> + + <br><dt><span class="samp">bbe -b "/\x5f\x81\x18\x06/:10" -s -e "B d;d 0 4;c BCD ASC;A \n" ./bindata</span><dd>Print BCD numbers and their block numbers in ascii format. Numbers start with sequence <code>x5f</code> <code>x81</code> <code>x18</code> <code>x06</code>. +The start sequence is not printed.<br> + + <br><dt><span class="samp">bbe -b "/\x5f/:2" -e "j 1;& \xf0" -o newdata bindata</span><dd>The least significant nybble of bytes after <code>x5f</code> is cleared.<br> + + <br><dt><span class="samp">bbe -b "/\xff\xd8\xff/:/\xff\xd9/" -s -e "w pic%02B.jpg" -o /dev/null manual.pdf</span><dd>Extract jpg-images from pdf-file to separate jpg-files (assuming that the jpg start/stop sequences +does not appear in other context than jpg-images). +Files will be named as <span class="file">pic01.jpg</span>, <span class="file">pic02.jpg</span>, <span class="file">pic03.jpg</span>,... +</dl> + +<div class="node"> +<p><hr> +<a name="Problems"></a>Previous: <a rel="previous" accesskey="p" href="#bbe-programs">bbe programs</a>, +Up: <a rel="up" accesskey="u" href="#Top">Top</a> +<br> +</div> + +<h2 class="chapter">5 Reporting Bugs</h2> + +<p><a name="index-bugs-11"></a><a name="index-problems-12"></a> +If you find a bug in <span class="command">bbe</span>, please send electronic mail to +<a href="mailto:tjsa@iki.fi">tjsa@iki.fi</a>. Include the version number, which you can find by +running <span class="samp">bbe --version</span><!-- /@w -->. Also include in your message the +output that the program produced and the output you expected. + + <p>If you have other questions, comments or suggestions about +<span class="command">bbe</span>, contact the author via electronic mail to +<a href="mailto:tjsa@iki.fi">tjsa@iki.fi</a>. The author will try to help you out, although he +may not have time to fix your problems. + + <div class="contents"> +<h2>Table of Contents</h2> +<ul> +<li><a name="toc_Top" href="#Top">bbe</a> +<li><a name="toc_Overview" href="#Overview">1 Preliminary information</a> +<li><a name="toc_Samples" href="#Samples">2 Samples using <span class="command">bbe</span></a> +<li><a name="toc_Invoking-bbe" href="#Invoking-bbe">3 How to run <span class="command">bbe</span></a> +<ul> +<li><a href="#Invocation">3.1 Program invocation</a> +<li><a href="#Block">3.2 Block definition</a> +<li><a href="#Commands">3.3 <span class="command">bbe</span> commands</a> +<li><a href="#Limits">3.4 Limitations</a> +</li></ul> +<li><a name="toc_bbe-programs" href="#bbe-programs">4 How <span class="command">bbe</span> works</a> +<li><a name="toc_Problems" href="#Problems">5 Reporting Bugs</a> +</li></ul> +</div> + +</body></html> + diff --git a/doc/bbe.info b/doc/bbe.info index 22bdb7b..771d0c0 100644 --- a/doc/bbe.info +++ b/doc/bbe.info @@ -59,7 +59,7 @@ basic byte related transformations on blocks of input stream. `bbe' is non-interactive command line tool and can be used as a part of a pipeline. `bbe' makes only one pass over input stream. - `bbe' also contains grep-like features, like printing the filename, + `bbe' contains also grep-like features, like printing the filename, offset and block number. @@ -152,8 +152,7 @@ The format for running the `bbe' program is: Print the version number of `bbe' and then exit successfully. All remaining options are names of input files, if no input files -are specified, then the standard input is read. `-' means standard -input. +are specified or `-' is given, then the standard input is read. File: bbe.info, Node: Block, Next: Commands, Prev: Invocation, Up: Invoking bbe @@ -232,7 +231,7 @@ in START and STOP: `\;' 59 - Semicolon must be escaped, because it is command delimitter. + Semicolon must be escaped, because it is a command delimitter. Values of N and M can be given in decimal, octal and hexadecimal: @@ -284,8 +283,8 @@ Block commands are: block. `N' - Before block contents the file name where the block starts is - printed with colon. + Before block contents the file name where the current block starts + is printed with colon. `F F' Before block contents the current stream offset and colon is @@ -302,7 +301,7 @@ Block commands are: `B F' Before block contents the current block number and colon is - printed in format specified by F. Block numbering starts at zero. + printed in format specified by F. Block numbering starts at one. F can have one of the sames codes as `F'-command Byte commands are: @@ -368,8 +367,14 @@ block, first byte is number zero. `w `file'' Contents of blocks are written to file `file'. *Note*: Data - inserted by commands `A' and `I' are not written to file `file' - and `J' and `L' commands have no effect on `w'-commands. + inserted by commands `A' and `I' are written to file `file' and + `j' and `l' commands have no effect on `w'-commands. Zero size + files are not preserved. + Filename can contain format string `%B' or `%nB', these format + strings are replace by current block number (starting from one), + causing every block to have it's own file. In `%nB', the `n' is + field width in range 0-99. If `n' has a leading zero, then the + block numbers will be left padded with zeroes. `y/SOURCE/DEST/' Translate bytes in SOURCE to the corresponding bytes in DEST. @@ -469,8 +474,8 @@ Few examples: `echo "1234567890" | bbe -b ":1" -e "L 9" -e "A -"' Output is 1-2-3-4-5-6-7-8-9-0 - Minus is inserted after every 1 byte long block,but not after 9'th - block. + Hyphen is inserted after every 1 byte long block,but not after + 9'th block. `bbe -s -b "/First line/:/Last line/" /tmp/text' Print lines between sentences `First line' and `Last line'. `bbe -s -b "%<a %:%</a>%" -e "s/\x0a/ /" -e "A \n" ./index.html' @@ -500,6 +505,11 @@ Few examples: is not printed. `bbe -b "/\x5f/:2" -e "j 1;& \xf0" -o newdata bindata' The least significant nybble of bytes after `x5f' is cleared. +`bbe -b "/\xff\xd8\xff/:/\xff\xd9/" -s -e "w pic%02B.jpg" -o /dev/null manual.pdf' + Extract jpg-images from pdf-file to separate jpg-files (assuming + that the jpg start/stop sequences does not appear in other context + than jpg-images). Files will be named as `pic01.jpg', + `pic02.jpg', `pic03.jpg',... File: bbe.info, Node: Problems, Prev: bbe programs, Up: Top @@ -525,10 +535,10 @@ Node: Overview1811 Node: Samples2298 Node: Invoking bbe3696 Node: Invocation4230 -Node: Block5253 -Node: Commands7297 -Node: Limits11254 -Node: bbe programs11807 -Node: Problems15588 +Node: Block5242 +Node: Commands7288 +Node: Limits11616 +Node: bbe programs12169 +Node: Problems16270 End Tag Table diff --git a/doc/bbe.texi b/doc/bbe.texi index d555db8..00f3f74 100644 --- a/doc/bbe.texi +++ b/doc/bbe.texi @@ -6,7 +6,7 @@ @finalout @c %**end of header -@set VERSION 0.1.4 +@set VERSION 0.1.5 @copying This file documents the @command{bbe} - binary block editor @@ -68,7 +68,7 @@ The @command{bbe} program is a sed-like editor for binary files. @command{bbe} performs basic byte related transformations on blocks of input stream. @command{bbe} is non-interactive command line tool and can be used as a part of a pipeline. @command{bbe} makes only one pass over input stream. -@command{bbe} also contains grep-like features, like printing the filename, offset and block number. +@command{bbe} contains also grep-like features, like printing the filename, offset and block number. @node Samples, Invoking bbe, Overview, Top @@ -168,8 +168,7 @@ successfully. Print the version number of @command{bbe} and then exit successfully. @end table -All remaining options are names of input files, if no input files are specified, then the standard input is read. -@code{-} means standard input. +All remaining options are names of input files, if no input files are specified or @code{-} is given, then the standard input is read. @node Block, Commands, Invocation, Invoking bbe @section Block definition @@ -242,7 +241,7 @@ Also escape code @code{\y} can be used. Decimal values of @code{\y}'s: 13 @item \; -59@*Semicolon must be escaped, because it is command delimitter. +59@*Semicolon must be escaped, because it is a command delimitter. @end table @@ -291,7 +290,7 @@ Commands appearing after this command have no effect after @var{N} blocks are fo @strong{Note}: Commands that are defined before this command have effect on every block. @item N -Before block contents the file name where the block starts is printed with colon. +Before block contents the file name where the current block starts is printed with colon. @item F @var{f} Before block contents the current stream offset and colon is printed in format specified by @var{f}. @@ -309,7 +308,7 @@ Octal. @item B @var{f} Before block contents the current block number and colon is printed in format specified by @var{f}. -Block numbering starts at zero. @var{f} can have one of the sames codes as @code{F}-command +Block numbering starts at one. @var{f} can have one of the sames codes as @code{F}-command @end table @subheading Byte commands are: @@ -371,7 +370,10 @@ character not present in @var{search} or @var{replace}. @item w @file{file} Contents of blocks are written to file @file{file}. @strong{Note}: Data inserted by commands @code{A} and @code{I} -are not written to file @file{file} and @code{J} and @code{L} commands have no effect on @code{w}-commands. +are written to file @file{file} and @code{j} and @code{l} commands have no effect on @code{w}-commands. Zero size files are not preserved.@* +Filename can contain format string @code{%B} or @code{%nB}, these format strings are replace by current block number (starting from one), causing every block to have it's own file. +In @code{%nB}, the @code{n} is field width in range 0-99. If @code{n} has a leading zero, then the block numbers will be left padded with zeroes. + @item y/@var{source}/@var{dest}/ Translate bytes in @var{source} to the corresponding bytes in @var{dest}. @var{source} and @var{dest} must have equal length. @@ -482,7 +484,7 @@ Output is @example 1-2-3-4-5-6-7-8-9-0 @end example -Minus is inserted after every 1 byte long block,but not after 9'th block.@* +Hyphen is inserted after every 1 byte long block,but not after 9'th block.@* @item bbe -s -b "/First line/:/Last line/" /tmp/text Print lines between sentences @samp{First line} and @samp{Last line}.@* @@ -518,7 +520,12 @@ Print BCD numbers and their block numbers in ascii format. Numbers start with se The start sequence is not printed.@* @item bbe -b "/\x5f/:2" -e "j 1;& \xf0" -o newdata bindata -The least significant nybble of bytes after @code{x5f} is cleared. +The least significant nybble of bytes after @code{x5f} is cleared.@* + +@item bbe -b "/\xff\xd8\xff/:/\xff\xd9/" -s -e "w pic%02B.jpg" -o /dev/null manual.pdf +Extract jpg-images from pdf-file to separate jpg-files (assuming that the jpg start/stop sequences +does not appear in other context than jpg-images). +Files will be named as @file{pic01.jpg}, @file{pic02.jpg}, @file{pic03.jpg},... @end table @@ -20,7 +20,7 @@ * */ -/* $Id: bbe.c,v 1.37 2005/10/14 14:04:45 timo Exp $ */ +/* $Id: bbe.c,v 1.39 2005/10/20 08:02:56 timo Exp $ */ #include "bbe.h" #ifdef HAVE_GETOPT_H @@ -406,8 +406,7 @@ parse_command(char *command_string) if (p[0] == 0) return; // empty line if (p[0] == '#') return; // comment - c = strdup(p); - if(c == NULL) panic("Out of memory",NULL,NULL); + c = xstrdup(p); i = 0; token[i] = strtok(c," \t\n"); @@ -468,7 +467,7 @@ parse_command(char *command_string) break; case 'w': if(i != 2 || strlen(token[0]) > 1) panic("Error in command",command_string,NULL); - new->s1 = strdup(token[1]); + new->s1 = xstrdup(token[1]); break; case 'j': case 'J': @@ -20,7 +20,7 @@ * */ -/* $Id: bbe.h,v 1.25 2005/10/13 13:48:46 timo Exp $ */ +/* $Id: bbe.h,v 1.26 2005/10/19 18:39:13 timo Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -228,6 +228,9 @@ read_pos(); extern inline unsigned char * block_end_pos(); +extern char * +xstrdup(char *str); + /* global variables */ extern struct block block; extern struct command *commands; diff --git a/src/buffer.c b/src/buffer.c index f05ffca..9566e08 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -20,7 +20,7 @@ * */ -/* $Id: buffer.c,v 1.32 2005/10/14 13:25:54 timo Exp $ */ +/* $Id: buffer.c,v 1.33 2005/10/19 18:39:13 timo Exp $ */ #include "bbe.h" #include <stdlib.h> @@ -50,13 +50,12 @@ set_output_file(char *file) if(file == NULL) { out_stream.fd = STDOUT_FILENO; - out_stream.file = "stdout"; + out_stream.file = "(stdout)"; } else { out_stream.fd = open(file,O_WRONLY | O_CREAT | O_TRUNC,S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); if(out_stream.fd == -1) panic("Cannot open for writing",file,strerror(errno)); - out_stream.file = strdup(file); - if (out_stream.file == NULL) panic("Out of memory",NULL,NULL); + out_stream.file = xstrdup(file); } } @@ -95,13 +94,12 @@ set_input_file(char *file) if(file[0] == '-' && file[1] == 0) { new->fd = STDIN_FILENO; - new->file = "stdin"; + new->file = "(stdin)"; } else { new->fd = open(file,O_RDONLY); if(new->fd == -1) panic("Cannot open file for reading",file,strerror(errno)); - new->file = strdup(file); - if (new->file == NULL) panic("Out of menory",NULL,NULL); + new->file = xstrdup(file); } } diff --git a/src/execute.c b/src/execute.c index b0a29cd..7980221 100644 --- a/src/execute.c +++ b/src/execute.c @@ -20,7 +20,7 @@ * */ -/* $Id: execute.c,v 1.27 2005/10/14 13:23:20 timo Exp $ */ +/* $Id: execute.c,v 1.31 2005/10/21 07:53:26 timo Exp $ */ #include "bbe.h" #include <stdlib.h> @@ -41,6 +41,11 @@ static int skip_this_block; /* tells if i or s commands are inserting bytes, meaningfull at end of the block */ static int inserting; +/* tells if there is w-command with file having %d + this is only for performance + */ +static int w_commands_block_num = 0; + /* command list for write_w_command */ static struct command_list *current_byte_commands; @@ -381,25 +386,119 @@ write_w_command(unsigned char *buf,size_t length) { struct command_list *c; + if(skip_this_block) return; + c = current_byte_commands; while(c != NULL) { if(c->letter == 'w') { - if(fwrite(buf,1,length,c->fd) != length) panic("Cannot write to file",c->s1,strerror(errno)); + if(fwrite(buf,1,length,c->fd) != length) panic("Cannot write to file",c->s2,strerror(errno)); + if(length) c->count = 1; // file was written } c = c->next; } } +/* finds the %B or %nB format string from the filename of w-command + returns pointer to %-postion and the length of the format string + */ +char * +find_block_w_file(char *file,int *len) +{ + char *f,*ppos; + + f = file; + + while(*f != 0) + { + if(*f == '%') + { + ppos = f; + f++; + while(f - ppos < 4 && isdigit(*f)) f++; + if(*f == 'B') + { + *len = (int) (f - ppos) + 1; + return ppos; + } + f = ppos; + } + f++; + } + return NULL; +} + +/* replaces all %B or %nB format strings with block number in a file name */ +void +bn_printf(char *file,char *str,off_t block_number) +{ + char *bstart,*f; + char num[128],format[64]; + int blen; + + f = str; + file[0] = 0; + + while((bstart = find_block_w_file(f,&blen)) != NULL) + { + num[0] = 0; + format[0] = 0; + strncat(file,f,bstart - f); + strncpy(format,bstart,blen-1); + format[blen-1] = 0; + strcat(format,"lld"); + sprintf(num,format,(long long) block_number); + if(strlen(file) + strlen(num) >= 4096) panic("Filename for w-command too long",str,NULL); + strcat(file,num); + f = bstart + blen; + } + strcat(file,f); +} + +/* close (if open) and open next w-command files for new block */ +void +open_w_files(off_t block_number) +{ + struct command_list *c; + static char file[4096]; + c = current_byte_commands; + + while(c != NULL) + { + if(c->letter == 'w' && c->offset) + { + if(c->fd != NULL) + { + if(fclose(c->fd) != 0) panic("Error closing file",c->s2,strerror(errno)); + if (!c->count && c->s2 != NULL) // remove if empty + { + unlink(c->s2); + } + c->fd = NULL; + } + + bn_printf(file,c->s1,block_number); + c->fd = fopen(file,"w"); + if(c->fd == NULL) panic("Cannot open file for writing",file,strerror(errno)); + c->count = 0; + if(c->s2 != NULL) free(c->s2); + c->s2 = xstrdup(file); + } + c = c->next; + } +} + + /* init_commands, initialize those wich need it, currently w - open file and rpos=0 for all */ void init_commands(struct commands *commands) { struct command_list *c; + int wlen; c = commands->byte; @@ -408,8 +507,20 @@ init_commands(struct commands *commands) switch(c->letter) { case 'w': - c->fd = fopen(c->s1,"w"); - if(c->fd == NULL) panic("Cannot open file for writing",c->s1,strerror(errno)); + if(find_block_w_file(c->s1,&wlen) != NULL) + { + c->fd = NULL; + c->offset = 1; + w_commands_block_num = 1; + c->s2 = NULL; + } else + { + c->fd = fopen(c->s1,"w"); + if(c->fd == NULL) panic("Cannot open file for writing",c->s1,strerror(errno)); + c->offset = 0; + c->s2 = xstrdup(c->s1); + } + c->count = 0; break; } c = c->next; @@ -430,7 +541,14 @@ close_commands(struct commands *commands) switch(c->letter) { case 'w': - if(fclose(c->fd) != 0) panic("Error in closing file",c->s1,strerror(errno)); + if(c->fd != NULL) + { + if(fclose(c->fd) != 0) panic("Error in closing file",c->s2,strerror(errno)); + if(!c->count && c->s2 != NULL) + { + unlink(c->s2); + } + } break; } c = c->next; @@ -464,6 +582,7 @@ execute_program(struct commands *commands) delete_this_block = 0; out_buffer.block_offset = 0; skip_this_block = 0; + if(w_commands_block_num) open_w_files(in_buffer.block_num); execute_commands(commands->block_start); do { diff --git a/src/xmalloc.c b/src/xmalloc.c index 01e1173..ab30c56 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -20,10 +20,11 @@ * */ -/* $Id: xmalloc.c,v 1.2 2005/09/04 15:58:29 timo Exp $ */ +/* $Id: xmalloc.c,v 1.3 2005/10/19 18:39:13 timo Exp $ */ #include "bbe.h" #include <stdlib.h> +#include <string.h> void * xmalloc (size_t size) @@ -33,3 +34,11 @@ xmalloc (size_t size) return value; } +char * +xstrdup(char *str) +{ + char *ret = strdup(str); + if (ret == NULL) panic("Out of memory",NULL,NULL); + return ret; +} + |

