home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
- * This is Free Software, distrubited under the GNU Software Aggrement.
- */
-
- #include "simped.h"
-
- void commands(editfile, newfile, fd)
- char *editfile; /* the name of the file being edited, NULL
- * if no file name was specified. Aslo,
- * 'newfile' will be TRUE
- */
- int *newfile; /* boolean TRUE if a file is created, FALSE
- * if the file was read in.
- */
- FILE *fd;
- {
- extern char *malloc();
-
- int printf(),
- fprintf(),
- fflush(),
- getlinenum(),
- fputs(),
- puts(),
- cleanup();
-
- char **addlines(), /* add lines to the text area */
- **editline(), /* edit a line (subsutite) command */
- **deleteline(), /* delete a line in the text buffer */
- **modify(); /* modify a line of text */
-
- void listtext(),
- savefile(),
- help();
-
- char **text, /* text entered in the editor */
- *overflow=NULL, /* pointer for autowrap */
- *delimiter=NULL, /* passed back from getlinenum for edit */
- inpchar; /* command input character */
-
- int count=0, /* line count */
- startlist, /* line number to start a listing */
- abort=1, /* command aborted - don't print menu */
- linenum=0, /* line number for insert */
- valid_command=FALSE; /* boolean for command loop */
-
- /*
- initilize the text buffer area
- */
- if ( (text = (char **)malloc(PTR_CHUNK * sizeof(char *)+1)) == (char **)0 )
- {
- fprintf(stderr, "malloc: error=%d\n", errno);
- cleanup(2);
- }
-
- /*
- * Copyright
- */
- puts("\nSimped version 1, Copyright (C) 1990 - Jay Konigsberg\n");
-
- if (fd != stdin)
- {
- printf("%s: ", editfile);
- }
- if (*newfile)
- {
- puts("New file.\n");
- puts("Please enter your text now. The text will automatically wrap");
- puts("around to the next line when a line is full. Enter a Carriage");
- puts("Return on a new line to end.\n");
- }
- text = addlines(text, overflow, &count, 1, fd, *newfile);
-
- if (fd != stdin && ! *newfile)
- {
- if (count >= PAUSE)
- {
- startlist = count - PAUSE + 1;
- printf("\nThe last %d lines read in:\n", PAUSE);
- }
- else
- {
- startlist = 1;
- }
- listtext(text, count, startlist);
- }
-
- while (! valid_command)
- {
- /*
- * abort will be 0 when returning from a function via a bs,
- * thus the strange looking test.
- */
- if (abort)
- {
- puts("\nOptions: S)ave and quit, A)bort/cancel, L)ist message, E)dit line,");
- puts(" I)nsert line, D)elete line, C)ontinue, M)odify, H)elp\n");
- fputs("Command? ", stdout);
- }
-
- abort=1;
- fflush(stdout);
- valid_command=FALSE;
- inpchar = getchar();
- putchar(inpchar);
- fflush(stdout);
-
- switch (inpchar)
- {
- case 'S': /* save the file and quit */
- case 's':
- for (;;)
- {
- if ( (inpchar=getchar()) == '\b' )
- {
- putchar(inpchar);
- putchar(' ');
- putchar(inpchar);
- abort=0;
- break;
- }
- else if ( inpchar == '\n' )
- {
- savefile(editfile, newfile, fd, text, count);
- break;
- }
- else
- putchar(BELL);
- }
- break;
- case 'A': /* abort editing session */
- case 'a':
- for (;;)
- {
- if ( (inpchar=getchar()) == '\b' )
- {
- putchar(inpchar);
- putchar(' ');
- putchar(inpchar);
- abort=0;
- break;
- }
- else if ( inpchar == '\n' )
- {
- fputs("\nQuit without saving (return=n/Y)? ", stdout);
- if ( (inpchar=getchar()) == 'Y' )
- {
- putchar(inpchar);
- fflush(stdout);
- cleanup(2);
- break;
- }
- else
- {
- putchar(inpchar);
- fflush(stdout);
- puts("");
- break;
- }
- }
- }
- break;
- case 'Q': /* because q to quit is so common */
- case 'q':
- cleanup(2);
- break;
- case 'L': /* list the file */
- case 'l':
- if ( (linenum=getlinenum(count, "cr=1", "")) != -1 )
- {
- if ( linenum != 0 )
- {
- puts("");
- listtext(text, count, linenum);
- }
- else
- abort=0;
- }
- break;
- case 'E': /* edit a line - sudsutite command */
- case 'e':
- /* if (delimiter)
- if (*delimiter)
- *delimiter='\0';
- */
- if (delimiter)
- {
- free(delimiter);
- }
- else
- {
- malloc(delimiter, 2);
- *delimiter='\0';
- }
-
- if ( (linenum=getlinenum(count, "/?=cr", delimiter)) != -1 )
- {
- if ( linenum != 0 )
- {
- if ( ! *delimiter )
- puts("");
- text = editline(text, linenum, *delimiter);
- }
- else
- abort=0;
- }
- break;
- case 'I': /* insert a line */
- case 'i':
- if ( (linenum=getlinenum(count, "", "")) != -1)
- {
- if ( linenum != 0 )
- {
- puts("");
- text=addlines(text,overflow,&count,linenum,stdin,FALSE);
- }
- else
- abort=0;
- }
- break;
- case 'D': /* delete a line */
- case 'd':
- if ( (linenum=getlinenum(count, "", "")) != -1)
- {
- if (linenum != 0)
- {
- puts("");
- text=deleteline(text, &count, linenum);
- }
- else
- abort=0;
- }
- break;
- case 'C': /* continue editing at EOF */
- case 'c':
- for (;;)
- {
- if ( (inpchar=getchar()) == '\b' )
- {
- putchar(inpchar);
- putchar(' ');
- putchar(inpchar);
- abort=0;
- break;
- }
- else if ( inpchar == '\n' )
- {
- puts("");
- text=addlines(text,overflow,&count,count+1,stdin,FALSE);
- break;
- }
- else
- putchar(BELL);
- }
- break;
- case 'M': /* modify - multi use line editing */
- case 'm':
- if ( (linenum=getlinenum(count, "", "")) != -1 )
- {
- if ( linenum != 0 )
- {
- puts("");
- text=modify(text, linenum);
- }
- else
- abort=0;
- }
- break;
- case '\b':
- putchar(' ');
- putchar(BELL);
- fflush(stdout);
- abort=0; /* do not print menu again */
- break;
- case '\n':
- fputs("Command? ", stdout);
- abort=0; /* do not print menu again */
- break;
- case 'H':
- case 'h':
- for (;;)
- {
- if ( (inpchar=getchar()) == '\b' )
- {
- putchar(inpchar);
- putchar(' ');
- putchar(inpchar);
- abort=0;
- break;
- }
- else if ( inpchar == '\n' )
- {
- help();
- break;
- }
- else
- putchar(BELL);
- }
- break;
- case 'j':
- puts("\n\nAuthor: Jay Konigsberg\n");
- puts("Date : June 1990\n");
- puts("uucp : jak@sactoh0\n");
- break;
- default :
- printf("%c: not a valid command.\n", inpchar);
- }
- }
- }
-