/*************************************************************************\ * Copyright (c) 2002 The University of Chicago, as Operator of Argonne * National Laboratory. * Copyright (c) 2002 The Regents of the University of California, as * Operator of Los Alamos National Laboratory. * This file is distributed subject to a Software License Agreement found * in the file LICENSE that is included with this distribution. \*************************************************************************/ /* * $Log: editstring.c,v $ * Revision 1.7 2004/12/16 01:45:13 borland * Added fflush() after puts() in stream mode, so data gets output promptly. * * Revision 1.6 2003/07/02 16:51:26 borland * Better version of last fix. Remove the newline from string before * editing, then emit a new newline during printout. * * Revision 1.5 2003/07/02 16:49:04 borland * No longer emits spurious blank lines in stream mode. * * Revision 1.4 2003/06/23 21:39:42 borland * Added usage for new 'x' and 'c' commands. * * Revision 1.3 2002/08/14 16:49:36 soliday * Added Open License * * Revision 1.2 2001/10/25 22:00:28 borland * Updated usage message. * * Revision 1.1 2000/04/11 19:57:29 soliday * Moved from oag/apps/src/utils * * Revision 1.2 1996/02/26 19:45:52 borland * First versions. * * * Copyright 1994 by Michael Borland and Argonne National Laboratory, * all rights reserved. * * file: editstring.c * * command-line string editing utility * * Michael Borland, 1994. */ #include "mdb.h" #include "scan.h" #include char *USAGE = "\ editstring -editcommand= {-stream | [] ...}\n\ editstring returns edited versions of strings, using editing\n\ sequences reminiscent of emacs.\n\ -stream edit stream from standard input\n\ -editcommand sequences:\n\ d delete characters\n\ f forward characters\n\ b backward characters\n\ D delete words\n\ F forward words\n\ B backward words\n\ a go to beginning of string\n\ e go to end of string\n\ i{delim}text{delim} insert text times\n\ s[?]{delim}text{delim} search for text, leave active position at end\n\ ? means that if text isn't found, stop editing\n\ S[?]{delim}text{delim} search for text, leave active position at start\n\ ? means that if text isn't found, stop editing\n\ k kill characters\n\ K kill words\n\ z kill up to \n\ Z kill up to and including , times\n\ x{delim}text{delim} kill as long as characters are in text.\n\ text may be a range in form [char1-char2]\n\ x-{delim}text{delim} kill as long as characters are not in text.\n\ y yank kill buffer, times\n\ c clear the kill buffer.\n\ %[hg]{delim}text1{delim}text2{delim} \n\ replace text1 with text2 times\n\ g means replace all occurrences ( is ignored).\n\ h means replace only if the text1 is \"here\", i.e.,\n\ at the active position.\n\ () execute editing sequence times.\n\ \nProgram by Michael Borland. (This is version 3, June 2003)\n"; #define OPT_EDITCOMMAND 0 #define OPT_STREAMINPUT 1 #define N_OPTIONS 2 static char *option[N_OPTIONS] = { "editcommand", "stream" } ; static char editbuffer[4096]; int main(int argc, char **argv) { SCANNED_ARG *scanned; long i_arg, i, strings, stream; char *editcommand; char **string; argc = scanargsg(&scanned, argc, argv); if (argc==1) bomb(NULL, USAGE); if (argc<3) bomb("supply more arguments", NULL); string = NULL; strings = 0; editcommand = NULL; stream = 0; for (i_arg=1; i_arg"); editcommand = scanned[i_arg].list[1]; break; case OPT_STREAMINPUT: stream = 1; break; default: fprintf(stderr, "error: unknown/ambiguous keyword: %s\n", scanned[i_arg].list[0]); exit(1); break; } } else { string = trealloc(string, sizeof(*string)*(strings+1)); string[strings] = scanned[i_arg].list[0]; strings++; } } if (strings==0 && !stream) bomb("one of -stream or strings-to-edit must be supplied", NULL); if (editcommand==NULL) bomb("-editcommand option not given", NULL); if (strings) { for (i=0; i