simped (the simple editor) version 2
Jay A. Konigsberg
jak at sactoh0.UUCP
Sat Jul 7 10:58:06 AEST 1990
simped.shar.01 / part 01 of 02
This is simped (the simple editor) version 2. Most of the changes are
bug fixes, other code cleanup and documentation updates, however, there
are two major modifications to basic operation.
simped now accepts two command line options.
-a Causes simped to always come up in append mode.
-p compatability for postnews (see simped.doc)
With just a little bit of luck, this will be the last release.
Jay
--------------------------------- cut here ---------------------------------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
# Makefile
# addlines.c
# allocate.c
# cleanup.c
# commands.c
# deleteline.c
# editline.c
# getline.c
# getlinenum.c
# help.c
# This archive created: Fri Jul 6 17:52:50 1990
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'Makefile'" '(2044 characters)'
if test -f 'Makefile'
then
echo shar: "will not over-write existing file 'Makefile'"
else
sed 's/^ X//' << \SHAR_EOF > 'Makefile'
X# simped - a simple, easy to use (line oriented) editor.
X#
X# Copyright (C) 1990 by Jay Konigsberg: (uucp: jak at sactoh0)
X# (paper mail not available)
X#
X# Permission to use, copy, modify, and distribute this software and its
X# documentation is hereby granted, provided that the above copyright
X# notice appear in all copies and that both the copyright notice and
X# this permission notice appear in supporting documentation. This software
X# is provided "as is" without express or implied warranty. However, the
X# author retains all Copyright priviliges and rights to renumeration if
X# this software is sold.
X
X#
X# LINELEN - The length of a line across the screen. This value must be
X# at least 5 characters less than the screen width to allow
X# for the line number prompt at the left. 6 in better.
XLINELEN=74
X
X#
X# PAUSE - This is the number of lines that will be listed upon
X# entering the editor with an existing file, or when
X# the L)ist command is used. The H)elp command suggests
X# it will be "about a half a screen".
XPAUSE=15
X
X#
X# BELL - Choose the bell character for your system, ascii is default.
X#
XBELL=\007
X
X#
X# Complier - choose your C complier. gcc -O -traditional is preferred
X
XCC=cc -O
X#CC=gcc -O -traditional
X#CC=gcc -O -Wall -Wshadow -Wpointer-arith
X#CC=cc -g
X
XCFLAGS=-DLINELEN=$(LINELEN) -DPAUSE=$(PAUSE) -DBELL=$(BELL)
X
X#EXEC=a.out
XEXEC=simped
X
XOBJS=main.o cleanup.o getline.o listtext.o allocate.o commands.o\
Xaddlines.o deleteline.o getlinenum.o savefile.o editline.o position.o\
Xmodify.o help.o options.o
X
XFILES=main.c cleanup.c getline.c listtext.c allocate.c commands.c\
Xaddlines.c deleteline.c getlinenum.c savefile.c editline.c position.c\
Xmodify.c help.c options.c
X
X$(EXEC): $(OBJS)
X $(CC) $(CFLAGS) $(OBJS) -o $(EXEC)
X
X$(OBJS):
X
Xclean:
X rm -f $(OBJS) core MANIFEST simped?.ar
X
Xship: tar
X compress simped.tar
X echo simped.tar.Z created
X
Xtar:
X tar cvf simped.tar $(FILES) Makefile simped.doc one mod simped.h
SHAR_EOF
if test 2044 -ne "`wc -c < 'Makefile'`"
then
echo shar: "error transmitting 'Makefile'" '(should have been 2044 characters)'
fi
fi
echo shar: "extracting 'addlines.c'" '(2131 characters)'
if test -f 'addlines.c'
then
echo shar: "will not over-write existing file 'addlines.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'addlines.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. uucp: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * addlines - add text to the text buffer. This routine
X * does double service reading from stdin or a file.
X * and handeles inserts and appends.
X */
X
X#include "simped.h"
X
Xchar **addlines(text, overflow, count, location, fd, newfile, postnews)
Xchar **text;
Xchar *overflow;
Xint *count;
Xint location; /* the line number after which the add occures */
XFILE *fd;
Xint newfile; /* newfile flag to statt in insert mode */
Xint postnews;
X{
Xint printf();
X
Xextern char *getline(),
X **allocate();
X
Xchar buffer[LINELEN+2];
X
Xint char_read_in=0, /* characters read in from fd */
X text_entered=TRUE; /* boolean to determin when to allocate
X space AND the flag for EOF */
X
Xif (*count >= 0)
X ++(*count);
Xfor(;;)
X {
X if (fd == stdin || newfile)
X {
X printf("%3d> ",location);
X overflow = getline(buffer, &text_entered, stdin, '\0', FALSE);
X }
X else
X {
X overflow = getline(buffer, &text_entered, fd, '\0', FALSE);
X
X /* see if a blank line is being read in. if so, add 1 sp */
X if (text_entered != EOF && buffer[0] == '\n')
X {
X buffer[0]=' ';
X buffer[1]='\n';
X buffer[2]='\0';
X text_entered=TRUE;
X }
X }
X if (text_entered && text_entered != EOF)
X {
X text = allocate(text, buffer, overflow, location, *count);
X char_read_in += (strlen(buffer));
X (*count)++;
X location++;
X }
X else
X break;
X }
Xif (postnews)
X {
X text = allocate(text, "--\n\0", overflow, *count, *count);
X ++(*count);
X }
X
Xif(fd != stdin && ! newfile)
X {
X printf("%d characters read in\n", char_read_in);
X }
Xif (*count >= 1)
X --(*count);
Xreturn(text);
X}
SHAR_EOF
if test 2131 -ne "`wc -c < 'addlines.c'`"
then
echo shar: "error transmitting 'addlines.c'" '(should have been 2131 characters)'
fi
fi
echo shar: "extracting 'allocate.c'" '(1854 characters)'
if test -f 'allocate.c'
then
echo shar: "will not over-write existing file 'allocate.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'allocate.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * allocate - makes room in the text buffer for whatever was just entered.
X */
X#include "simped.h"
X
Xchar **allocate(text, buffer, overflow, location, count)
Xchar **text;
Xchar buffer[];
Xchar *overflow;
Xint location; /* where to allocate space for the buffer */
Xint count; /* number of lines in the text area */
X{
Xint fprintf(),
X cleanup();
X
Xextern char *malloc(),
X *realloc();
X
Xint linelen, /* length of the input buffer */
X textinx; /* text index for insert adjustment */
X
Xif ( ! (count % PTR_CHUNK) )
X {
X if ( (text = (char **)realloc((char *)text, (unsigned)
X ((count+PTR_CHUNK) * sizeof(char *)))) == (char **)0)
X {
X fprintf(stderr, "realloc: error=%d\n", errno);
X cleanup(2);
X }
X }
Xif (location < count) /* insert vs. append */
X {
X for(textinx=count; textinx >= location; --textinx)
X text[textinx]=text[textinx-1];
X }
Xif (overflow)
X {
X linelen=strlen(buffer);
X if((text[location-1]=malloc((unsigned)(linelen+FUDGE))) == NULL)
X {
X fprintf(stderr, "malloc: error=%d\n", errno);
X cleanup(2);
X }
X text[location-1] = strcpy(text[location-1], buffer);
X text[location-1][linelen]='\n';
X text[location-1][linelen+1]='\0';
X /* text[location-1] = strcat(text[location-1], "\n"); */
X }
Xelse
X {
X text[location-1] = strdup(buffer);
X }
Xreturn(text);
X}
SHAR_EOF
if test 1854 -ne "`wc -c < 'allocate.c'`"
then
echo shar: "error transmitting 'allocate.c'" '(should have been 1854 characters)'
fi
fi
echo shar: "extracting 'cleanup.c'" '(1315 characters)'
if test -f 'cleanup.c'
then
echo shar: "will not over-write existing file 'cleanup.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'cleanup.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * cleanup - resets port settings to what they were before entering
X * raw mode.
X */
X
X#include "simped.h"
X
X/* Global for interrupt routine: cleanup() */
Xextern struct termio ttyset; /* terminal settings */
Xextern unsigned short c_lflag_hold; /* hold original values for reset */
Xextern unsigned char VEOF_hold; /* hold original value for reset */
X
Xint cleanup(sig_caught) /* Signal trap for SIGINT */
Xint sig_caught;
X{
Xvoid exit();
X
Xint ioctl(),
X fprintf();
X
X/* reset terminal charastics */
Xttyset.c_lflag = c_lflag_hold;
Xttyset.c_cc[4] = VEOF_hold;
Xif ( ioctl(0, TCSETAW, &ttyset) == -1) {
X fprintf(stderr, "ioctl: error=%d\n", errno);
X exit(2);
X}
Xif (sig_caught)
X {
X fprintf(stderr,"\nMessage aborted.\n");
X }
Xexit(2);
Xreturn(0);
X}
SHAR_EOF
if test 1315 -ne "`wc -c < 'cleanup.c'`"
then
echo shar: "error transmitting 'cleanup.c'" '(should have been 1315 characters)'
fi
fi
echo shar: "extracting 'commands.c'" '(7412 characters)'
if test -f 'commands.c'
then
echo shar: "will not over-write existing file 'commands.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'commands.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * commands - main command loop
X */
X
X/* global to replace \b */
Xextern unsigned char bs_char; /* BS char picked up from termio */
X
X#include "simped.h"
X#include "sys/stat.h"
X
Xvoid commands(editfile, newfile, fd, postnews, append)
Xchar *editfile; /* the name of the file being edited, NULL
X * if no file name was specified. Aslo,
X * 'newfile' will be TRUE
X */
Xint *newfile; /* boolean TRUE if a file is created, FALSE
X * if the file was read in.
X */
XFILE *fd;
Xint postnews;
Xint append;
X{
Xextern char *malloc();
X
Xint printf(),
X fprintf(),
X fflush(),
X getlinenum(),
X fputs(),
X puts(),
X stat(),
X unlink(),
X free(),
X cleanup();
X
Xchar **addlines(), /* add lines to the text area */
X **allocate(), /* make space in buffer for text */
X **editline(), /* edit a line (subsutite) command */
X **deleteline(), /* delete a line in the text buffer */
X **modify(); /* modify a line of text */
X
Xvoid listtext(),
X savefile(),
X help();
X
Xstruct stat *buf; /* check for 0 len file when A)bort */
X
Xchar **text, /* text entered in the editor */
X *overflow=NULL, /* pointer for autowrap */
X *delimiter=NULL, /* passed back from getlinenum for edit */
X inpchar; /* command input character */
X
Xint count=0, /* line count */
X startlist, /* line number to start a listing */
X abort=1, /* command aborted - don't print menu */
X linenum=0, /* line number for insert */
X valid_command=FALSE; /* boolean for command loop */
X
X/*
Xinitilize the text buffer area
X*/
Xif ( (text = (char **)malloc(PTR_CHUNK * sizeof(char *)+1)) == (char **)0 )
X {
X fprintf(stderr, "malloc: error=%d\n", errno);
X cleanup(2);
X }
X
X/*
X * Copyright
X */
Xputs("\nSimped version 2, Copyright (C) 1990 - Jay Konigsberg (jak at sactoh0)\n");
X
Xif (fd != stdin)
X {
X printf("%s: ", editfile);
X }
Xif (*newfile)
X {
X puts("New file.\n");
X puts("Please enter your text now. The text will automatically wrap");
X puts("around to the next line when a line is full. Enter a Carriage");
X puts("Return on a new line to end.\n");
X }
Xtext = addlines(text, overflow, &count, 1, fd, *newfile, postnews);
X
Xif (fd != stdin && ! *newfile)
X {
X if (count >= PAUSE)
X {
X startlist = count - PAUSE + 1;
X printf("\nThe last %d lines read in:\n", PAUSE);
X }
X else
X {
X startlist = 1;
X }
X listtext(text, count, startlist);
X }
X
Xif (append)
X text = addlines(text, overflow, &count, count+1, stdin, FALSE, FALSE);
X
Xwhile (! valid_command)
X {
X /*
X * abort will be 0 when returning from a function via a bs,
X * thus the strange looking test.
X */
X if (abort)
X {
X puts("\nOptions: S)ave and quit, A)bort/cancel, L)ist message, E)dit line,");
X puts(" I)nsert line, D)elete line, C)ontinue, M)odify, H)elp\n");
X fputs("Command? ", stdout);
X }
X
X abort=1;
X fflush(stdout);
X valid_command=FALSE;
X inpchar = getchar();
X putchar(inpchar);
X fflush(stdout);
X
X switch (inpchar)
X {
X case 'S': /* save the file and quit */
X case 's':
X for (;;)
X {
X if ( (inpchar=getchar()) == bs_char )
X {
X putchar(inpchar);
X putchar(' ');
X putchar(inpchar);
X abort=0;
X break;
X }
X else if ( inpchar == '\n' )
X {
X savefile(editfile, newfile, fd, text, count);
X break;
X }
X else
X putchar(BELL);
X }
X break;
X case 'A': /* abort editing session */
X case 'a':
X for (;;)
X {
X if ( (inpchar=getchar()) == bs_char )
X {
X putchar(inpchar);
X putchar(' ');
X putchar(inpchar);
X abort=0;
X break;
X }
X else if ( inpchar == '\n' )
X {
X fputs("\nQuit without saving (return=n/Y)? ", stdout);
X if ( (inpchar=getchar()) == 'Y' || inpchar == 'y' )
X {
X putchar(inpchar);
X fflush(stdout);
X buf = (struct stat *)malloc(sizeof(buf));
X /* remove file if its empty - note: the errors are
X ignored intentionally */
X if ( stat(editfile, buf) == 0 )
X {
X if (buf->st_size == (off_t)0 )
X {
X unlink(editfile);
X }
X }
X cleanup(2);
X break;
X }
X else
X {
X putchar(inpchar);
X fflush(stdout);
X puts("");
X break;
X }
X }
X }
X break;
X case 'Q': /* because q to quit is so common */
X case 'q':
X cleanup(2);
X break;
X case 'L': /* list the file */
X case 'l':
X if ( (linenum=getlinenum(count, "cr=1", "")) != -1 )
X {
X if ( linenum != 0 )
X {
X puts("");
X listtext(text, count, linenum);
X }
X else
X abort=0;
X }
X break;
X case 'E': /* edit a line - sudsutite command */
X case 'e':
X if (delimiter)
X {
X free(delimiter);
X }
X else
X {
X if ( (delimiter=malloc(2)) == NULL )
X {
X fprintf(stderr, "malloc: error=%d\n", errno);
X cleanup(2);
X }
X *delimiter='\0';
X }
X
X if ((linenum=getlinenum(count, "/?=cr", delimiter)) != -1)
X {
X if ( linenum != 0 )
X {
X if ( ! *delimiter )
X puts("");
X text = editline(text, linenum, *delimiter);
X }
X else
X abort=0;
X }
X break;
X case 'I': /* insert a line */
X case 'i':
X if ( (linenum=getlinenum(count, "", "")) != -1)
X {
X if ( linenum != 0 )
X {
X puts("");
X text=addlines(text,overflow,&count,linenum,stdin,FALSE,FALSE);
X }
X else
X abort=0;
X }
X break;
X case 'D': /* delete a line */
X case 'd':
X if ( (linenum=getlinenum(count, "", "")) != -1)
X {
X if (linenum != 0)
X {
X puts("");
X text=deleteline(text, &count, linenum);
X }
X else
X abort=0;
X }
X break;
X case 'C': /* continue editing at EOF */
X case 'c':
X for (;;)
X {
X if ( (inpchar=getchar()) == bs_char )
X {
X putchar(inpchar);
X putchar(' ');
X putchar(inpchar);
X abort=0;
X break;
X }
X else if ( inpchar == '\n' )
X {
X puts("");
X text=addlines(text,overflow,&count,count+1,stdin,FALSE,FALSE);
X break;
X }
X else
X putchar(BELL);
X }
X break;
X case 'M': /* modify - multi use line editing */
X case 'm':
X if ( (linenum=getlinenum(count, "", "")) != -1 )
X {
X if ( linenum != 0 )
X {
X puts("");
X text=modify(text, linenum);
X }
X else
X abort=0;
X }
X break;
X case '\n':
X fputs("Command? ", stdout);
X abort=0; /* do not print menu again */
X break;
X case 'H':
X case 'h':
X for (;;)
X {
X if ( (inpchar=getchar()) == bs_char )
X {
X putchar(inpchar);
X putchar(' ');
X putchar(inpchar);
X abort=0;
X break;
X }
X else if ( inpchar == '\n' )
X {
X help();
X break;
X }
X else
X putchar(BELL);
X }
X break;
X case 'j':
X puts("\n\nAuthor : Jay Konigsberg\n");
X puts("Copyright: June 1990");
X puts("Date : June 1990\n");
X puts("uucp : jak at sactoh0\n");
X break;
X default :
X if (inpchar == (int)bs_char)
X {
X putchar(' ');
X putchar(BELL);
X fflush(stdout);
X abort=0; /* do not print menu again */
X }
X else
X printf("%c: not a valid command.\n", inpchar);
X }
X }
X}
SHAR_EOF
if test 7412 -ne "`wc -c < 'commands.c'`"
then
echo shar: "error transmitting 'commands.c'" '(should have been 7412 characters)'
fi
fi
echo shar: "extracting 'deleteline.c'" '(1346 characters)'
if test -f 'deleteline.c'
then
echo shar: "will not over-write existing file 'deleteline.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'deleteline.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * deleteline - delete a line from the text buffer
X */
X
X#include "simped.h"
X
Xchar **deleteline(text, count, delline)
Xchar **text;
Xint *count;
Xint delline; /* returns the line number deleted or 0 */
X{
Xint printf(),
X fflush(),
X puts(),
X fputs();
X
Xvoid free();
X
Xint inpchar=0,
X loop;
X
Xif(delline > 0)
X {
X printf("\n%3d> ",delline);
X fputs(text[delline-1], stdout);
X fputs("Delete this line (Return=n/y): ", stdout);
X fflush(stdout);
X inpchar=getchar();
X putchar(inpchar);
X }
X
Xif (inpchar == 'y' || inpchar == 'Y')
X {
X free(text[delline-1]);
X for(loop=delline-1; loop < *count-1; ++loop)
X {
X text[loop] = text[loop+1];
X }
X text[loop]=NULL;
X --*count;
X puts("\nLine deleted.");
X }
Xelse
X {
X puts("\nLine NOT deleted.");
X }
Xreturn(text);
X}
SHAR_EOF
if test 1346 -ne "`wc -c < 'deleteline.c'`"
then
echo shar: "error transmitting 'deleteline.c'" '(should have been 1346 characters)'
fi
fi
echo shar: "extracting 'editline.c'" '(5118 characters)'
if test -f 'editline.c'
then
echo shar: "will not over-write existing file 'editline.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'editline.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * editline - edit a line in the text buffer. e.g. e/old/new
X */
X
X#include "simped.h"
X
Xchar **editline(text, linenum, delimiter)
Xchar **text;
Xint linenum;
Xchar delimiter;
X{
Xint position(), /* where is oldtext in text[], -1 if err */
X cleanup(),
X puts(),
X fputs(),
X fflush(),
X printf(),
X fprintf();
X
Xchar *realloc(),
X *getline();
X
X
Xchar *oldtext,
X *newtext,
X buffer[LINELEN+2];
X
Xint text_entered, /* boolean to tell if something was entered */
X textinx=0, /* index for the subsitutation */
X textlen, /* length of the original string */
X newlen, /* length of new text */
X oldlen, /* length of old text */
X point=0; /* subscript of text[] where oldtext begins */
X
Xtextlen=strlen(text[linenum-1]);
Xif ( ! delimiter )
X {
X printf("%3d> ", linenum);
X fputs(text[linenum-1], stdout);
X puts("Return by itself to insert at the beginning.");
X }
X for(;;)
X {
X if ( ! delimiter)
X {
X fputs("Old text: ", stdout);
X fflush(stdout);
X }
X if (getline(buffer, &text_entered, stdin, delimiter, TRUE))
X {
X if ( ! delimiter)
X {
X printf("Max line is %d characters, continuing...\n", LINELEN);
X }
X else
X {
X /* printf("%c%c", delimiter, BELL); */
X }
X buffer[LINELEN+1]='\n';
X buffer[LINELEN+2]='\0';
X }
X fflush(stdout);
X /* if (text_entered == FALSE) */
X if ( strlen(buffer) == 1) /* a single '\n' was entered */
X {
X /* setup to insert at the begining of the line */
X oldlen=0;
X break;
X }
X else
X {
X buffer[strlen(buffer)-1]='\0';
X oldtext=strdup(buffer);
X oldlen=strlen(oldtext);
X if( (point=position(oldtext, text[linenum-1])) == -1)
X {
X printf("\nstring: %s, not found in line %d\n", oldtext, linenum);
X delimiter='\0';
X }
X else
X {
X break;
X }
X }
X }
Xif ( ! delimiter )
X {
X fputs("New text: ", stdout);
X fflush(stdout);
X }
Xif(getline(buffer, &text_entered, stdin, delimiter, TRUE))
X {
X if ( ! delimiter )
X {
X printf("Max line is %d characters, continuing...\n", LINELEN);
X }
X else
X {
X /* printf("%c%c", delimiter, BELL); */
X fflush(stdout);
X }
X buffer[LINELEN+1]='\n';
X buffer[LINELEN+2]='\0';
X }
X/* if (text_entered == FALSE && oldlen == 0) */
Xif (strlen(buffer) == 1 && oldlen == 0) /* a singal '\n' was entered */
X {
X /* nothing entered, exit */
X puts("\nNo change...");
X return(text);
X }
Xif (text_entered == FALSE)
X {
X buffer[0]='\0';
X }
Xelse
X {
X buffer[strlen(buffer)-1]='\0';
X }
Xnewtext=strdup(buffer);
Xnewlen=strlen(newtext);
X/* Conditions to handle
X *
X * * no realloc
X * 1) oldlen == newlen - strings are same length
X * * relloc after change so no text is lost
X * 2) newlen == 0 & oldlen > 1 - remove some text
X * 3) newlen < oldlen - downsize text[]
X * * realloc before change so there is
X * room for more characters
X * 4) oldlen == 0 & newlen > 1 - insert at beginning of line
X * 5) newlen > oldlen - increase size-of text[]
X */
Xif (newlen == oldlen) /* case 1, same length */
X {
X for (textinx=point; textinx < point+newlen; ++textinx)
X {
X text[linenum-1][textinx] = newtext[textinx-point];
X }
X }
Xelse
X {
X if (newlen < oldlen) /* case 2 & 3 - have extra space */
X {
X /* copy over old text - non-op if new=0 */
X for (textinx=point; textinx < point+newlen; ++textinx)
X {
X text[linenum-1][textinx] = newtext[textinx-point];
X }
X /* roll remaining text back over the oldtext */
X point=textinx;
X for (textinx=point; textinx < textlen; ++textinx)
X {
X text[linenum-1][textinx]=text[linenum-1][textinx+oldlen-newlen];
X }
X text[linenum-1][textlen+newlen-oldlen]='\0';
X if((text[linenum-1]=realloc(text[linenum-1],
X (unsigned int)(textinx+FUDGE)))==NULL)
X {
X fprintf(stderr, "realloc: error=%d\n", errno);
X cleanup(2);
X }
X }
X else /* case 4 & 5 - more space is needed */
X {
X if((text[linenum-1]=realloc(text[linenum-1],
X (unsigned int)(textlen+newlen-oldlen+FUDGE)))==NULL)
X {
X fprintf(stderr, "realloc: error=%d\n", errno);
X cleanup(2);
X }
X /* roll out char to make room for insert */
X
X for (textinx=textlen+newlen-oldlen;
X textinx >= point+newlen-oldlen; --textinx)
X {
X text[linenum-1][textinx]=text[linenum-1][textinx-newlen+oldlen];
X }
X /* place newtext in the text area */
X for (textinx=point; textinx < point+newlen; ++textinx)
X {
X text[linenum-1][textinx] = newtext[textinx-point];
X }
X text[linenum-1][textlen+newlen-oldlen-1]='\n';
X text[linenum-1][textlen+newlen-oldlen]='\0';
X }
X }
Xprintf("\n%3d> ", linenum);
Xfputs(text[linenum-1], stdout);
Xreturn(text);
X}
SHAR_EOF
if test 5118 -ne "`wc -c < 'editline.c'`"
then
echo shar: "error transmitting 'editline.c'" '(should have been 5118 characters)'
fi
fi
echo shar: "extracting 'getline.c'" '(4037 characters)'
if test -f 'getline.c'
then
echo shar: "will not over-write existing file 'getline.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'getline.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * getline - gets a line of text from the fd: in_file. If the line of
X * text is longer than the allowed length (LINELEN) it strips off the
X * last word if there is whitespace, or the last char if not. The
X * overflow is saved, unless the dumpoverflow flag is set.
X *
X * The overflow is returned to the caller and maintained in static
X * space.
X */
X
X#include "simped.h"
X
X/* global to replace \b char in input */
Xextern unsigned char bs_char;
X
Xchar *getline(buffer, text_entered, in_file, delimiter, dumpoverflow)
Xchar buffer[]; /* input buffer to be passed out */
Xint *text_entered; /* flag that something was entered */
XFILE *in_file; /* stdin, or the fd of a file */
Xchar delimiter; /* if set, will return like a return will */
Xint dumpoverflow; /* boolean */
X{
Xint fflush(),
X fputs(),
X puts();
X
Xstatic char *overflow;
Xstatic char extrachar = '\0'; /* autowrap char when there is no whitespace */
X
X
Xint blankchar, /* location of blanking during autowrap */
X inpchar, /* char read in for procesing into buffer */
X curchar=0, /* input buffer pointer */
X whitespace, /* location of autowrap cut */
X have_nl=FALSE; /* newline flag */
X
X*text_entered = FALSE;
Xhave_nl = FALSE;
Xfflush(stdout);
X
Xfor (curchar=0; (curchar<=LINELEN && (! have_nl)); ++curchar)
X {
X if (overflow)
X {
X curchar = strlen(overflow);
X if (in_file == stdin)
X {
X fputs(overflow, stdout);
X fflush(stdout);
X }
X buffer[0]='\0';
X strcat(buffer, overflow);
X overflow = NULL;
X }
X else
X {
X if (extrachar)
X {
X curchar = 1;
X if (in_file == stdin)
X {
X putchar(extrachar);
X }
X buffer[0]=extrachar;
X buffer[1]=extrachar='\0';
X }
X }
X inpchar = getc(in_file);
X if (inpchar == EOF)
X {
X *text_entered = EOF;
X break;
X }
X if( inpchar == bs_char )
X {
X if ( curchar > 0)
X {
X if (in_file == stdin)
X {
X fputs("\b \b", stdout);
X }
X curchar -= 2; /* one will be added back in the for loop */
X }
X else
X {
X putchar(BELL);
X curchar--;
X }
X buffer[curchar+1]='\0';
X }
X else
X {
X if ( inpchar == '\t' )
X {
X inpchar = ' ';
X }
X else
X {
X if ( inpchar == '\n' )
X {
X have_nl = TRUE;
X }
X }
X if ( delimiter == inpchar )
X {
X putchar(delimiter);
X buffer[curchar]='\n';
X buffer[curchar+1]='\0';
X break;
X }
X if (inpchar != EOF)
X {
X buffer[curchar] = inpchar;
X buffer[curchar+1] = '\0';
X if (in_file == stdin)
X {
X putchar(inpchar);
X }
X }
X }
X fflush(stdout);
X }
X/* At least 1 char & a CR */
Xif ( curchar >= 2 || ( curchar > 1 && delimiter != '0') )
X {
X *text_entered = TRUE;
X }
X/* If the line overflowed - do an autowrap */
Xif (curchar == LINELEN+1 && ! have_nl)
X {
X /* look for the last whitespace */
X for (whitespace=curchar-1; whitespace >= 0; --whitespace)
X {
X if ( buffer[whitespace] == ' ' )
X {
X break;
X }
X }
X if (whitespace > 0)
X {
X for(blankchar=LINELEN; blankchar>whitespace; --blankchar)
X {
X if (in_file == stdin)
X {
X fputs("\b \b", stdout);
X }
X }
X if (in_file == stdin)
X {
X puts("");
X }
X overflow=(char *)&buffer[whitespace+1];
X buffer[whitespace] = '\0';
X }
X else /* No whitespace, put the last char in overflow */
X {
X if (in_file == stdin)
X {
X fputs("\b \b", stdout);
X puts("");
X }
X extrachar = buffer[LINELEN];
X buffer[LINELEN] = '\n';
X buffer[LINELEN+1] = '\0';
X }
X }
Xif (dumpoverflow)
X {
X overflow=NULL;
X extrachar='\0';
X return('\0');
X }
Xreturn(overflow);
X}
SHAR_EOF
if test 4037 -ne "`wc -c < 'getline.c'`"
then
echo shar: "error transmitting 'getline.c'" '(should have been 4037 characters)'
fi
fi
echo shar: "extracting 'getlinenum.c'" '(3031 characters)'
if test -f 'getlinenum.c'
then
echo shar: "will not over-write existing file 'getlinenum.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'getlinenum.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * getlinenum - gets a line number for use with other editor commands.
X *
X * arg 2 is a flag for special return.
X * "cr=1" will return 1 if nothing entered. Mainly for list.
X * "/?=cr" will return on / or ? instead of cr. Mainly for editline.
X * This will return ? or / in *delimiter. (arg 3), otherwise,
X * agr 3 will be null.
X * "" (null) is the default and will ask for a "Line number:" if
X * one isn't entered.
X */
X
X/* global to replace \b char in input */
Xextern unsigned char bs_char;
X
X#include "simped.h"
X#define PLACES 3 /* 3 mantissa places [0], [1], [2], [3]=NULL*/
X
Xint getlinenum(count, flag, delimiter)
Xint count;
Xchar *flag;
Xchar *delimiter;
X{
Xint printf(),
X fflush(),
X puts(),
X fputs(),
X atoi();
X
Xchar linenum[PLACES],/* the line number before atoi */
X inpdigit; /* each digit as it is read in */
X
Xint theline, /* returns the line number, 0 if the screen
X * hasnt changed or -1 on error */
X linenuminx=0, /* an index to line */
X creturn=FALSE; /* flag that "theline" may not equal zero */
X
X
Xdo
X {
X if ((inpdigit=getchar()) == '\n' && linenuminx == 0)
X {
X if (creturn)
X {
X return(theline = -1);
X }
X else
X {
X if ( ! strcmp(flag, "cr=1") )
X {
X return(theline = 1);
X }
X else
X {
X fputs("\nLine number: ", stdout);
X fflush(stdout);
X creturn = TRUE;
X inpdigit=' ';
X }
X }
X }
X else
X {
X if(inpdigit == bs_char)
X {
X fputs("\b \b", stdout);
X fflush(stdout);
X if ( --linenuminx >= 0 )
X linenum[linenuminx] = '\0';
X if (linenuminx == -1)
X {
X if (creturn)
X {
X return(theline = -1);
X }
X else /* returning from the function via bs, no screen change */
X {
X return(theline = 0);
X }
X }
X }
X else
X {
X if (linenuminx < PLACES && inpdigit >= '0' &&
X inpdigit <= '9' && inpdigit != '\n')
X {
X putchar(inpdigit);
X fflush(stdout);
X linenum[linenuminx++]=inpdigit;
X linenum[linenuminx]='\0';
X }
X else
X {
X if (inpdigit != '\n')
X {
X if ( ! strcmp(flag, "/?=cr") )
X {
X putchar(inpdigit);
X fflush(stdout);
X *delimiter=(char)inpdigit;
X inpdigit=(int)'\0';
X break;
X }
X else
X {
X putchar(BELL);
X fflush(stdout);
X }
X }
X }
X }
X }
X }
Xwhile (inpdigit != '\n');
X
Xtheline=atoi(linenum);
Xif(theline > count)
X {
X printf("\nThere are only %d lines.\n", count);
X theline = -1;
X }
Xelse
X if (theline == 0)
X {
X puts("\nThere is no line 0.");
X theline = -1;
X }
Xfflush(stdout);
Xreturn(theline);
X}
SHAR_EOF
if test 3031 -ne "`wc -c < 'getlinenum.c'`"
then
echo shar: "error transmitting 'getlinenum.c'" '(should have been 3031 characters)'
fi
fi
echo shar: "extracting 'help.c'" '(5821 characters)'
if test -f 'help.c'
then
echo shar: "will not over-write existing file 'help.c'"
else
sed 's/^ X//' << \SHAR_EOF > 'help.c'
X
X/*
X * Copyright (C) 1990 by Jay Konigsberg. mail: jak at sactoh0
X *
X * Permission to use, copy, modify, and distribute this software and its
X * documentation is hereby granted, provided that the above copyright
X * notice appear in all copies and that both the copyright notice and
X * this permission notice appear in supporting documentation. This software
X * is provided "as is" without express or implied warranty. However, the
X * author retains all Copyright priviliges and rights to renumeration if
X * this software is sold.
X */
X
X/*
X * help - on-line help information.
X */
X
X/* global to replace \b in input */
Xextern unsigned char bs_char;
X
X#include "simped.h"
X#define MARKER "--------------------------------------------------------"
X
Xvoid help()
X{
Xint puts(),
X fputs(),
X fflush();
X
Xint inpchar,
X getcr; /* get a return to go */
X
Xputs("");
Xputs("Help - S)ave");
Xputs(" A)bort");
Xputs(" L)ist");
Xputs(" E)dit");
Xputs(" I)nsert");
Xputs(" D)elete");
Xputs(" C)ontinue");
Xputs(" M)odify");
Xputs("");
Xfputs ("Enter the command you want help with? ", stdout);
Xfor(;;)
X {
X inpchar=getchar();
X putchar(inpchar);
X if ( (getcr=getchar()) == '\n')
X {
X putchar(getcr);
X break;
X }
X else
X if (getcr == bs_char)
X fputs("\b \b", stdout);
X }
Xputs(MARKER);
Xswitch(inpchar)
X {
X case 'S':
X case 's':
X puts("\n* S)ave and quit\n");
X puts("Saves the file and quits the editor.\n");
X puts("No options.");
X break;
X case 'A':
X case 'a':
X puts("\n* A)bort/cancel\n");
X puts("Verifies that no save is to be done and then abandons any");
X puts("editing that was done.\n");
X puts("No options.");
X break;
X case 'L':
X case 'l':
X puts("\n* List text\n");
X puts("Lists the text entered, pausing every half page or so. The");
X puts("number of lines listed is set at compile time.\n");
X puts("Options: A starting line number may be entered. A return");
X puts(" will start listing at line 1.");
X break;
X case 'E':
X case 'e':
X puts("\n* Edit line\n");
X puts("Substitute old text for new text on a line.\n");
X puts("Example 1:\n");
X puts("Command? e <RETURN>");
X puts("Line number: 1 <RETURN>\n");
X puts(" 1> A line of text\n");
X puts("Old text: f t <RETURN>");
X puts("New text: f modified t <RETURN>\n");
X puts(" 1> A line of modified text\n");
X puts("(returns to the command prompt)\n\n");
X fputs("Touch any key to continue: ", stdout);
X fflush(stdout);
X inpchar=getchar();
X puts("\n\nExample 2:\n");
X puts("Command? e1/f t/f modified t/\n");
X puts(" 1> A line of modified text\n");
X puts("(returns to the command prompt)\n");
X puts("To insert characters at the beginning of a line, enter");
X puts("nothing for the \"Old text:\" and the characters to be");
X puts("inserted for the \"New text:\".\n");
X puts("To delete characters, enter them in the \"Old text:\" and");
X puts("nothing in the new text.\n");
X puts("Entering nothing in both \"Old text:\" and \"New text:\" will");
X puts("result in no change.");
X break;
X case 'I':
X case 'i':
X puts("\n* Insert line\n");
X puts("Insert lines of text BEFORE the line number specified. The");
X puts("user is dropped into 'insert mode'.\n");
X puts("Option: The line number you want the inserted text to");
X puts(" appear BEFORE.");
X break;
X case 'D':
X case 'd':
X puts("\n* Delete line\n");
X puts("Delete a line. The line will be printed and you must enter");
X puts("a 'y' for the line to be deleted.\n");
X puts("Option: The line number you want deleted.");
X break;
X case 'C':
X case 'c':
X puts("\n* Continue\n");
X puts("Continue entry. Drops the user into input mode AFTER the");
X puts("last line in the file.\n");
X puts("No options.");
X break;
X case 'M':
X case 'm':
X puts("\n* Modify\n");
X puts("This is a multi-use editing function added in a effort");
X puts("to regain some of the flexibility lost in making this");
X puts("editor simple.\n");
X puts("Option: The line number you want to modify.\n");
X puts("This command can:\n");
X puts(" - replace text");
X puts(" - put blanks in place of characters");
X puts(" - delete text");
X puts(" - insert text\n");
X puts("The line is printed and and the cursor is placed under the");
X puts("first character on the next line. Changes are made by placing");
X puts("one or more of the available options below the portion of");
X puts("the line.\n");
X fputs("Touch any key to continue: ", stdout);
X fflush(stdout);
X inpchar=getchar();
X puts("\n\nOPTION EXPLANATION");
X puts("------ -----------");
X puts("^text# Inserts the characters between the '^'");
X puts(" and the '#' BEFORE the character");
X puts(" pointed to by the '^'. Inside the '^'");
X puts(" and the '#', both the '&' and the '^'");
X puts(" are treated as regular characters.\n");
X puts(" ^# Inserts a # before ^ (special case)\n");
X puts(" # (When not the first character after a");
X puts(" '^') causes the character above it to");
X puts(" be deleted and the space closed.\n");
X puts(" & Replaces the character above it with a");
X puts(" blank space.\n");
X puts("(space) No effect.\n");
X puts("ANY OTHER CHARACTER WILL REPLACE THE CHARACTER ABOVE IT!");
X puts("========================================================\n");
X fputs("Touch any key to continue: ", stdout);
X fflush(stdout);
X inpchar=getchar();
X puts("\n\nExample:\n");
X puts("Command? m1 <RETURN>\n");
X puts(" 1> Thos sentence isstoobbe mortifd");
X puts("edit> i ^is the #### #& d ^ie#\n");
X puts(" 1> This is the sentence to be modified\n");
X puts("Command? _");
X break;
X default :
X puts("\nNo such command");
X break;
X }
Xputs(MARKER);
X}
SHAR_EOF
if test 5821 -ne "`wc -c < 'help.c'`"
then
echo shar: "error transmitting 'help.c'" '(should have been 5821 characters)'
fi
fi
exit 0
# End of shell archive
--
-------------------------------------------------------------
Jay @ SAC-UNIX, Sacramento, Ca. UUCP=...pacbell!sactoh0!jak
If something is worth doing, its worth doing correctly.
More information about the Alt.sources
mailing list