From 2d8a5ef444abc653e5976b73993a6e31948ae348 Mon Sep 17 00:00:00 2001 From: Ian C Date: Wed, 4 May 2005 01:12:19 +0000 Subject: This commit was generated by cvs2svn to compensate for changes in r2, which included commits to RCS files with non-trunk default branches. --- LICENSE | 341 +++++++++++++ Makefile | 41 ++ README | 25 + xed.1 | 197 ++++++++ xed.c | 1619 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ xedkeys.h | 94 ++++ 6 files changed, 2317 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README create mode 100644 xed.1 create mode 100644 xed.c create mode 100644 xedkeys.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..abd3cf7 --- /dev/null +++ b/LICENSE @@ -0,0 +1,341 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + 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 + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f12a66 --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# xed - Simple hex editor +# +# Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk) +# +# 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 +# +# ------------------------------------------------------------------------- +# +# $Id: Makefile,v 1.1.1.1 2005-05-04 01:12:19 ianc Exp $ +# + +# Alter this if required (for instance NCURSES is being used rather than +# a local CURSES). +# +# You may also need to change #include to #include +# +LIBS=-lcurses -ltermcap + +OPTS=-g +KEYFILE=xedkeys.h + +xed: xed.c xedkeys.h Makefile + $(CC) $(OPTS) -o xed xed.c $(LIBS) + +xed.txt: xed.1 + nroff -man xed.1 | sed 's/.//g' > xed.txt + +clean: + -rm -f xed core xed.txt diff --git a/README b/README new file mode 100644 index 0000000..d69647c --- /dev/null +++ b/README @@ -0,0 +1,25 @@ +Build instrcutions +------------------ + +Building xed should be fairly easy on most systems with a semblence of POSIX +compliance as it doesn't use anything too flash. + +You should find in this archive : + + Makefile - makefile + xed.c - The source + xedkeys.h - The keys used. Stored here for easier personalisation. + xed.1 - man page + xed.txt - ASCII version of man page + README - This file + + +To build: + + make + +Then copy xed to a directory on your PATH for ease of use. man.1 can be +installed in /usr/local/man/man1 or your system's equivalent. + +If the make fails, read Makefile for configuration info regarding +curses/ncurses. diff --git a/xed.1 b/xed.1 new file mode 100644 index 0000000..92323e9 --- /dev/null +++ b/xed.1 @@ -0,0 +1,197 @@ +.TH xed 1 +.SH NAME +xed \- CURSES based hex/ASCII file editor +.SH SYNOPSIS +.B xed +[ +.B \-x +] +[ +.B \-i +] +[ +.B \-r +] +[ +.B \-c +] +[ +.B \-R +] +.I filename +.SH DESCRIPTION +.B xed +edits +.I filename +allowing individual bytes to be altered, either as hex or as ASCII. +.SH OPTIONS +.B xed +accepts these switches +.TP +.B \-x +Edit the file directly, rather than a memory based copy. Certain commands are +disabled if this switch is provided (see USAGE). +.TP +.B \-i +Start off in insert mode. +.TP +.B \-r +Start off in regular expression search mode. +.TP +.B \-c +Start off in case insensitive search mode. +.TP +.B \-R +Switch on read only mode (note this mode cannot be toggled whilst in +.B xed +). +.SH USAGE +.B xed +is a simple hex and ASCII based file editor. The display is made up of a +banner, which describes the file being editted; the size of the file (in hex); +the cursor position within the file (in hex); whether you are operating in +overtype or insert mode; whether you are in simple string or regular +expression search mode and the case sensitivity of the search. +.LP +Below the banner is the editting area, with the file offset and hex listing on +the left, and the ASCII on the right. +.LP +.B xed +operates within two modes when editting; hex and ASCII. Below are +the key commands valid in each mode. Note that all the editting, saving and +undo keys are ignored in read-only mode. +.TP +HEX MODE +.TP +Cursors keys +Move around file +.TP +h j k l +As above +.TP +< , ^B +Move back one page +.TP +> . ^F +Move forward one page +.TP +s +Save file (not valid in \-x mode) +.TP +TAB +Switch to ASCII mode +.TP +$ +Move to the end of the file +.TP +^ +Move to the beginning of the file +.TP ++ +Jump to a specified address (Note this address defaults to decimal, but an hex +address can be entered by prefixing the number with '0x', and an octal address +by prefixing with '0') +.TP +R +Restore byte from last saved/initial version (not valid in \-x mode) +.TP +q Q ^C +Quit +.TP +1-9 a-f A-F +Enter Hex value +.TP +^A +Switch between overtype/insert (not valid in \-x mode) +.TP +^T +Switch between normal/regular expression search mode +.TP +^K +Switch between case sensitive/insensitive search mode +.TP +BS DEL +Cancel after first hex digit is entered +.TP +x +Delete character at cursor (not valid in \-x mode) +.TP +/ +Search. In regular expression mode this will treat the file as a set of +text lines, each line terminated with a newline or NULL. +.TP +n +Search next. Note that this will perform the last search, regardless of any +mode change to/from regular expression mode, though it will take account of +changes to case sensitivity. +.TP +^R +Redraw screen +.TP +ASCII MODE +.TP +Cursor keys +Move around file +.TP +^H ^J ^K ^L +As above +.TP +^B +Move back one page +.TP +^F +Move forward one page +.TP +TAB +Switch to HEX mode +.TP +^A +Switch between overtype/insert (not valid in \-x mode) +.TP +^X +Delete character at cursor (not valid in \-x mode) +.TP +^R +Redraw screen +.TP +^C +Quit +.SH ENVIRONMENT +xed will process these environment variables if present +.TP +XEDARG +Allows default arguments to be definied in the environment. If this is set +then invoking +.B xed +will be the same as invoking +.B xed $XEDARG +.SH SEE ALSO +.BR od (1) +.BR grep (1) +.BR curses (3) +.BR regexp (3) +.SH DIAGNOSTICS +The diagnostics produced by +.B xed +are intended to be self-explanatory. +.SH NOTES +Version 2.0 of +.B xed +includes these additions and fixes over 1.7 : +.TP +o +Drastically tidied up code to enter the 1990's at least. +.SH BUGS +.TP +o +Even though the input routine tries it's best to mask them, pressing certain +control keys (function keys, etc) while +.B xed +is waiting for the second hex digit when editting can send a string of +unexpected commands to +.B xed. +This may result in your file being accidentally editted and saved. +.TP +o +The display is half-inverted and sometimes corrupts on some HP-UX systems. This +is a bug in their curses implementation. diff --git a/xed.c b/xed.c new file mode 100644 index 0000000..64a5328 --- /dev/null +++ b/xed.c @@ -0,0 +1,1619 @@ +/* + xed - Simple hex editor + + Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk) + + 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 + + ------------------------------------------------------------------------- + +*/ +static const char id[]="$Id:"; + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "xedkeys.h" + +#define VERSION "V2.0" +#define USAGE "[-x] [-i] [-r] [-c] [-R] file" + +#define NONE 0 +#define HEX 1 +#define ASCII 2 +#define REGEXP 3 + +#define FILENAME_LEN 25 + +#define GETSTR_LEN 80 +#define GETSTR_DISP_LEN 50 + +#define BUFSIZE(x) ((((x)/1024)+1)*1024) + +#define CTYPE(func,c) (((c>=0)&&(c<=255)) ? func(c) : (c)) +#define CTEST(func,c) (((c>=0)&&(c<=255)) ? func(c) : (FALSE)) + +#define IPBUFFSIZE 1024 + +typedef unsigned char uchar; + +char *name; +char *filename; +char print_name[FILENAME_LEN+1]; +uchar *edit; +uchar *mirror; +long bufsize; +int in_place=FALSE; +int regexp=FALSE; +int casesense=TRUE; +int readonly=FALSE; +int fd; +long file_len; +long page_ptr; +long file_ptr; +long end_page; +long page_depth; +int edit_hex=TRUE; +int overtype=TRUE; +int editted=FALSE; + +int last_search=NONE; +char sch_str[GETSTR_LEN]; +int sch_hex[128]; +int sch_no; + +regex_t re; +int re_alloc=FALSE; + +/* Function prototypes +*/ +void Err(const char *s); +void SysErr(const char *p); +void MainCode(void); +int HandleKey(int key); +int HandleKeyHex(int key); +void HandleKeyAscii(int key); +int CompileRegExp(void); +void SearchRegExp(void); +void DoSearchRegExp(void); +void SearchString(void); +void DoSearchString(void); +void SearchHex(void); +void DoSearchHex(void); +int Read(int fd, void *buff, int no); +int Write(int fd, void *buff, int no); +void OpenFile(void); +void SaveFile(void); +int CalcHex(int x[]); +int GetByte(long cur); +char *GetByteString(long *cur, int all_lower); +int GetLastSaved(long cur); +void Display(void); +void PartialDisplay(long cur, int x, int y); +void InsertValue(long p,long b); +void DeleteValue(long p); +void ReplaceValue(long p,long b); +void UpdateValue(long p, int b); +void ShowCursor(void); +void ShowPos(void); +int ScanKey(void); +int GetKey(int allkeys); +void GetString(char s[]); +char *Get(char *p); +int GetChar(char *p, char *v); +void Message(const char *fmt, ...); +char *Tail(char s[], int l); + + +int main(int argc, char *argv[]) +{ + char *env,*arg,*list[256]; /* If you're that prepared to break it, you */ + int listc; /* DESERVE to get a segmentation fault */ + int src,dest,f; + + name=argv[0]; + + /* Prepend XEDARG variable if present + */ + src=0; + dest=0; + if (env=getenv("XEDARG")) + { + arg=strtok(env," \t"); + + while(arg) + { + list[dest++]=arg; + arg=strtok(NULL," \t"); + } + } + + for(f=1;f15) + { + file_ptr-=16; + + if (file_ptr(file_len-1)) + file_ptr=file_len-1; + + if (file_ptr>end_page) + { + page_ptr+=16; + Display(); + } + else + ShowCursor(); + } + break; + + case CURS_RIGHT1: + case CURS_RIGHT2: + if (file_ptr<(file_len-1)) + { + file_ptr++; + + if (file_ptr>end_page) + { + page_ptr+=16; + Display(); + } + else + ShowCursor(); + } + break; + + case PAGE_BACK1: + case PAGE_BACK2: + case PAGE_BACK3: + if (file_ptr>=page_depth) + { + page_ptr-=page_depth; + + if (page_ptr<0) + page_ptr=0; + + file_ptr-=page_depth; + + Display(); + } + break; + + case PAGE_FWD1: + case PAGE_FWD2: + case PAGE_FWD3: + if (page_ptr<=file_len-page_depth-1) + { + page_ptr+=page_depth; + + file_ptr+=page_depth; + + if (file_ptr>(file_len-1)) + file_ptr=file_len-1; + + Display(); + } + break; + + case SWITCH_TO_ASCII: + edit_hex=FALSE; + ShowCursor(); + break; + + case START_OF_FILE: + page_ptr=file_ptr=0L; + Display(); + break; + + case END_OF_FILE: + page_ptr=file_len-page_depth; + + if (page_ptr<0) + page_ptr=0; + + file_ptr=file_len-1; + Display(); + break; + + case JUMP_TO: + s=Get("Address to jump to (prefix with 0x for hex) : "); + + page_ptr=strtol(s,(char **)NULL,0); + + if (page_ptr<0) + page_ptr=0; + + if (page_ptr>=file_len) + page_ptr=file_len-1; + + file_ptr=page_ptr; + + if ((page_ptr>file_len-page_depth)&&(page_depthend_page) + { + page_ptr+=16; + Display(); + } + else + ShowCursor(); + } + } + } + else + Message("Can't recover in -x mode!"); + } + else + Message("File is read-only!"); + + ShowCursor(); + break; + + case REFRESH: + Display(); + break; + + case INSERT_TOGGLE: + if (!readonly) + if (in_place) + { + Message("no insert/delete in -x mode"); + ShowCursor(); + } + else + { + overtype=!overtype; + ShowPos(); + ShowCursor(); + } + break; + + case DELETE_CHAR: + if (!readonly) + { + if (in_place) + Message("no insert/delete in -x mode"); + else + if (file_len) + { + editted=TRUE; + + DeleteValue(file_ptr); + + if ((file_ptr>=file_len)&&(file_len)) + if (--file_ptrend_page) + { + page_ptr=file_ptr; + Display(); + } + else + ShowCursor(); + } + else + ShowCursor(); + } + else + { + if (!overtype) + ReplaceValue(file_ptr,old_val); + else + UpdateValue(file_ptr,old_val); + + ShowCursor(); + } + } + } + break; + } +} + + +void HandleKeyAscii(int key) +{ + switch(key) + { + case ASC_CURS_UP1: + case ASC_CURS_UP2: + if (file_ptr>15) + { + file_ptr-=16; + + if (file_ptr(file_len-1)) + file_ptr=file_len-1; + + if (file_ptr>end_page) + { + page_ptr+=16; + Display(); + } + else + ShowCursor(); + } + break; + + case ASC_CURS_RIGHT1: + case ASC_CURS_RIGHT2: + if (file_ptr<(file_len)-1) + { + file_ptr++; + + if (file_ptr>end_page) + { + page_ptr+=16; + Display(); + } + else + ShowCursor(); + } + break; + + case ASC_PAGE_BACK: + if (file_ptr>=page_depth) + { + page_ptr-=page_depth; + + if (page_ptr<0) + page_ptr=0; + + file_ptr-=page_depth; + + Display(); + } + break; + + case ASC_PAGE_FWD: + if (page_ptr<=file_len-page_depth-1) + { + page_ptr+=page_depth; + + file_ptr+=page_depth; + + if (file_ptr>(file_len-1)) + file_ptr=file_len-1; + + Display(); + } + break; + + case ASC_REFRESH: + Display(); + break; + + case SWITCH_TO_HEX: + edit_hex=TRUE; + ShowCursor(); + break; + + case INSERT_TOGGLE: + if (!readonly) + if (in_place) + { + Message("no insert/delete in -x mode"); + ShowCursor(); + } + else + { + overtype=!overtype; + ShowPos(); + ShowCursor(); + } + break; + + case ASC_DELETE_CHAR: + if (!readonly) + { + if (in_place) + Message("no insert/delete in -x mode"); + else + if (file_len) + { + DeleteValue(file_ptr); + + if ((file_ptr>=file_len)&&(file_len)) + if (--file_ptrend_page) + { + page_ptr=file_ptr; + Display(); + } + else + ShowCursor(); + } + else + ShowCursor(); + } + break; + } +} + +int CompileRegExp(void) +{ + char buff[80]; + int ret; + + if (re_alloc) + regfree(&re); + + re_alloc=FALSE; + + if (casesense) + ret=regcomp(&re,sch_str,REG_EXTENDED|REG_NEWLINE); + else + ret=regcomp(&re,sch_str,REG_EXTENDED|REG_ICASE|REG_NEWLINE); + + if(ret!=0) + { + regerror(ret,&re,buff,80); + Message ("RE error : %s",buff); + return (FALSE); + } + + re_alloc=TRUE; + + return TRUE; +} + +void SearchRegExp(void) +{ + last_search=NONE; + + strcpy(sch_str,Get("/")); + + if (!strlen(sch_str)) + return; + + if (CompileRegExp()) + { + last_search=REGEXP; + DoSearchRegExp(); + } +} + +void DoSearchRegExp(void) +{ + char *p; + long f,of; + regmatch_t re_match; + + Message("Searching...."); + + f=file_ptr+1; + + of=f; + p=GetByteString(&f,!casesense); + while(p) + { + if (regexec(&re,p,1,&re_match,0)==0) + { + file_ptr=of+re_match.rm_so; + + if ((file_ptrend_page)) + page_ptr=file_ptr; + + free(p); + return; + } + + free(p); + of=f; + p=GetByteString(&f,!casesense); + } + + Message("Not found. Press a key."); + GetKey(TRUE); + Message(""); +} + + +void SearchString(void) +{ + last_search=NONE; + + strcpy(sch_str,Get("Search string : ")); + + if (!strlen(sch_str)) + return; + + last_search=ASCII; + + DoSearchString(); +} + + +void DoSearchString(void) +{ + long f; + int l,r; + + Message("Searching...."); + + l=strlen(sch_str); + + for(f=file_ptr+1;fend_page)) + page_ptr=file_ptr; + + return; + } + } + + Message("Not found. Press a key."); + GetKey(TRUE); + Message(""); +} + + +void SearchHex(void) +{ + char *s; + + last_search=NONE; + sch_no=0; + + s=Get("Enter First number (return to quit) : "); + + while(*s) + { + sch_hex[sch_no++]=(int)strtol(s,(char **)NULL,0); + s=Get("Enter next number (return to finish) : "); + } + + if (!sch_no) + return; + + last_search=HEX; + + DoSearchHex(); +} + + +void DoSearchHex(void) +{ + int f,r; + + Message("Searching...."); + + for(f=file_ptr+1;fend_page)) + page_ptr=file_ptr; + + return; + } + } + + Message("Not found. Press a key"); + GetKey(TRUE); + Message(""); +} + + +/* Provide a skin to read(2) and write(2) +*/ +int Read(int fd, void *buff, int no) +{ + int tot,rd; + + tot=0; + while(tot=file_len) + return -1; + + if(!in_place) + return edit[cur]; + + if (first_read) + first_read=FALSE; + else + if ((cur>=buffpos)&&(cur=file_len) + return NULL; + + l=0; + + while(((b=GetByte(cp++))!=-1)&&(b!=0)&&(b!='\n')) + l++; + + if (!l) + { + if (!(p=(char *)malloc(0x1))) + SysErr(NULL); + *p=0; + *cur+=1; + return p; + } + + if (!(p=(char *)malloc((int)l+1))) + SysErr(NULL); + + for(cp=0;cp126)) + mvaddch(r,62+f,(chtype)'.'); + else + mvaddch(r,62+f,(chtype)b); + } + r++; + } + + end_page=cur-1; + page_depth=end_page-page_ptr+1; + + ShowCursor(); +} + + +void PartialDisplay(long cur, int x, int y) +{ + int r,f,b; + + r=y; + + while(r126)) + mvaddch(r,62+f,(chtype)'.'); + else + mvaddch(r,62+f,(chtype)b); + } + + x=0; + r++; + } +} + + +void InsertValue(long p,long b) +{ + if (++file_len>bufsize) + { + if (!(edit=(uchar *)realloc(edit,(bufsize=BUFSIZE(file_len))))) + SysErr(NULL); + + if (!(mirror=(uchar *)realloc(mirror,bufsize))) + SysErr(NULL); + } + + memmove(edit+p+1,edit+p,file_len-p-1); + memmove(mirror+p+1,mirror+p,file_len-p-1); + + edit[p]=(uchar)b; + mirror[p]=(uchar)b; +} + + +void DeleteValue(long p) +{ + if (file_len>1) + { + memmove(edit+p,edit+p+1,file_len-p-1); + memmove(mirror+p,mirror+p+1,file_len-p-1); + + file_len--; + } + else + file_len=0; +} + + +void ReplaceValue(long p,long b) +{ + int x,y; + uchar c=b; + + edit[p]=(uchar)b; + + y=3+(p-page_ptr)/16; + x=(p-page_ptr)%16; + + mvprintw(y,11+x*3,"%2.2X",(uchar)b); + + if ((b<32)||(b>126)) + mvaddch(y,62+x,(chtype)'.'); + else + mvaddch(y,62+x,(chtype)b); +} + + +void UpdateValue(long p, int b) +{ + int x,y; + uchar c=b; + + if (!in_place) + { + if (overtype) + edit[p]=(uchar)b; + else + InsertValue(p,b); + } + else + { + lseek(fd,p,SEEK_SET); + if (Write(fd,&c,1)==-1) + SysErr(filename); + } + + y=3+(p-page_ptr)/16; + x=(p-page_ptr)%16; + + if (overtype) + { + mvprintw(y,11+x*3,"%2.2X",(uchar)b); + + if ((b<32)||(b>126)) + mvaddch(y,62+x,(chtype)'.'); + else + mvaddch(y,62+x,(chtype)b); + } + else + PartialDisplay(p,x,y); +} + + +void ShowCursor(void) +{ + int x,y; + + ShowPos(); + + y=(file_ptr-page_ptr)/16; + x=(file_ptr-page_ptr)%16; + + if (edit_hex) + move(3+y,11+x*3); + else + move(3+y,62+x); + + refresh(); +} + + +void ShowPos(void) +{ + mvprintw(1,0,"Pos : %8.8x",file_ptr); + mvprintw(0,40,"File size : %8.8x",file_len); + + /* Print Status line */ + if (readonly) + mvaddstr(1,20,"READ ONLY"); + else + if (overtype) + mvaddstr(1,20,"OVERTYPE"); + else + mvaddstr(1,20," INSERT "); + + if (regexp) + mvaddstr(1,37,"REGEXP SEARCH"); + else + mvaddstr(1,37,"STRING SEARCH"); + + if (casesense) + mvaddstr(1,57," CASE SENSITIVE "); + else + mvaddstr(1,57,"CASE INSENSITIVE"); +} + + +int ScanKey(void) +{ + int k; + + k=getch(); + + if (k==KEY_BACKSPACE) + k=8; + + if (k==ERR) + k=3; + + /* Message("Read key %9d",k); */ + + return k; +} + + +int GetKey(int allkeys) +{ + int k; + + k=ScanKey(); + + if (!allkeys) + while(!CTEST(isascii,k)) + k=ScanKey(); + + return k; +} + + +void GetString(char s[]) +{ + int k,done,p,x,y; + + done=FALSE; + p=0; + s[p]=0; + getyx(stdscr,y,x); + + while(!done) + { + mvprintw(y,x,"%-*.*s",GETSTR_DISP_LEN,GETSTR_DISP_LEN, + Tail(s,GETSTR_DISP_LEN)); + + move(y,x+((p>=GETSTR_DISP_LEN) ? (GETSTR_DISP_LEN) : (p))); + + refresh(); + + switch (k=GetKey(TRUE)) + { + case 8: + case 127: + if (p) + s[--p]=0; + break; + + case 3: + strcpy(s,""); + done=TRUE; + break; + + case '\n': + done=TRUE; + break; + + default: + if (p-1) + ret[d--]=s[sl--]; + + ret[0]='.'; + ret[1]='.'; + } + + return ret; +} + + +/* END OF FILE */ diff --git a/xedkeys.h b/xedkeys.h new file mode 100644 index 0000000..6ecd189 --- /dev/null +++ b/xedkeys.h @@ -0,0 +1,94 @@ +/* + xed - Simple hex editor + + Copyright (C) 2005 Ian Cowburn (ianc@noddybox.demon.co.uk) + + 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 + + ------------------------------------------------------------------------- + + Include file detailing the keys in xed + + $Id$ +*/ + +#ifndef XEDKEYS_H +#define XEDKEYS_H + +/* To define control codes */ +#define CNTL(x) ((x)-'@') + +/* Keys valid in HEX mode */ + +#define CURS_UP1 KEY_UP +#define CURS_UP2 'k' +#define CURS_DOWN1 KEY_DOWN +#define CURS_DOWN2 'j' +#define CURS_LEFT1 KEY_LEFT +#define CURS_LEFT2 'h' +#define CURS_RIGHT1 KEY_RIGHT +#define CURS_RIGHT2 'l' + +#define PAGE_FWD1 '>' +#define PAGE_FWD2 '.' +#define PAGE_FWD3 CNTL('F') + +#define PAGE_BACK1 '<' +#define PAGE_BACK2 ',' +#define PAGE_BACK3 CNTL('B') + +#define SWITCH_TO_ASCII '\t' + +#define START_OF_FILE '^' +#define END_OF_FILE '$' +#define JUMP_TO '+' + +#define SAVE 's' + +#define RESTORE_BYTE 'R' + +#define REFRESH CNTL('R') + +#define INSERT_TOGGLE CNTL('A') + +#define DELETE_CHAR 'x' + +#define FIND_STRING '/' +#define FIND_NEXT 'n' +#define FIND_TYPE CNTL('T') +#define FIND_CASE CNTL('K') + + +/* Keys valid in ASCII mode */ + +#define ASC_CURS_UP1 KEY_UP +#define ASC_CURS_UP2 CNTL('K') +#define ASC_CURS_DOWN1 KEY_DOWN +#define ASC_CURS_DOWN2 CNTL('J') +#define ASC_CURS_LEFT1 KEY_LEFT +#define ASC_CURS_LEFT2 CNTL('H') +#define ASC_CURS_RIGHT1 KEY_RIGHT +#define ASC_CURS_RIGHT2 CNTL('L') + +#define ASC_PAGE_FWD CNTL('F') +#define ASC_PAGE_BACK CNTL('B') + +#define ASC_REFRESH (REFRESH) + +#define SWITCH_TO_HEX (SWITCH_TO_ASCII) + +#define ASC_DELETE_CHAR CNTL('X') + +#endif -- cgit v1.2.3