home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1456 / commands.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  6.0 KB  |  311 lines

  1. /*
  2.  * Copyright (C) 1990 Jay Konigsberg - see Makefile for details
  3.  * This is Free Software, distrubited under the GNU Software Aggrement.
  4.  */
  5.  
  6. #include "simped.h"
  7.  
  8. void commands(editfile, newfile, fd)
  9. char *editfile;        /* the name of the file being edited, NULL
  10.              * if no file name was specified. Aslo,
  11.              * 'newfile' will be TRUE
  12.              */
  13. int  *newfile;        /* boolean TRUE if a file is created, FALSE
  14.              * if the file was read in.
  15.              */
  16. FILE *fd;
  17. {
  18. extern char *malloc();
  19.  
  20. int    printf(),
  21.     fprintf(),
  22.     fflush(),
  23.     getlinenum(),
  24.     fputs(),
  25.     puts(),
  26.     cleanup();
  27.  
  28. char     **addlines(),        /* add lines to the text area */
  29.     **editline(),        /* edit a line (subsutite) command */
  30.     **deleteline(),        /* delete a line in the text buffer */
  31.     **modify();        /* modify a line of text */
  32.  
  33. void    listtext(),
  34.     savefile(),
  35.     help();
  36.  
  37. char    **text,            /* text entered in the editor */
  38.     *overflow=NULL,        /* pointer for autowrap */
  39.     *delimiter=NULL,    /* passed back from getlinenum for edit */
  40.     inpchar;        /* command input character */
  41.  
  42. int    count=0,        /* line count */
  43.     startlist,        /* line number to start a listing */
  44.     abort=1,        /* command aborted - don't print menu */
  45.     linenum=0,        /* line number for insert */
  46.     valid_command=FALSE;    /* boolean for command loop */
  47.  
  48. /*
  49. initilize the text buffer area
  50. */
  51. if ( (text = (char **)malloc(PTR_CHUNK * sizeof(char *)+1)) == (char **)0 )
  52.     {
  53.     fprintf(stderr, "malloc: error=%d\n", errno);
  54.     cleanup(2);
  55.     }
  56.  
  57. /*
  58.  * Copyright
  59.  */
  60. puts("\nSimped version 1, Copyright (C) 1990 - Jay Konigsberg\n");
  61.  
  62. if (fd != stdin)
  63.     {
  64.     printf("%s: ", editfile);
  65.     }
  66. if (*newfile)
  67.     {
  68.     puts("New file.\n");
  69.     puts("Please enter your text now. The text will automatically wrap");
  70.     puts("around to the next line when a line is full. Enter a Carriage");
  71.     puts("Return on a new line to end.\n");
  72.     }
  73. text = addlines(text, overflow, &count, 1, fd, *newfile);
  74.  
  75. if (fd != stdin && ! *newfile)
  76.     {
  77.     if (count >= PAUSE)
  78.     {
  79.     startlist = count - PAUSE + 1;
  80.     printf("\nThe last %d lines read in:\n", PAUSE);
  81.     }
  82.     else
  83.     {
  84.     startlist = 1;
  85.     }
  86.     listtext(text, count, startlist);
  87.     }
  88.  
  89. while (! valid_command)
  90.     {
  91.     /*
  92.      * abort will be 0 when returning from a function via a bs,
  93.      * thus the strange looking test.
  94.      */
  95.     if (abort)
  96.     {
  97.    puts("\nOptions: S)ave and quit, A)bort/cancel, L)ist message, E)dit line,");
  98.    puts("         I)nsert line, D)elete line, C)ontinue, M)odify, H)elp\n");
  99.    fputs("Command? ", stdout);
  100.     }
  101.  
  102.     abort=1;
  103.     fflush(stdout);
  104.     valid_command=FALSE;
  105.     inpchar = getchar();
  106.     putchar(inpchar);
  107.     fflush(stdout);
  108.  
  109.     switch (inpchar)
  110.     {
  111.     case 'S': /* save the file and quit */
  112.     case 's':
  113.         for (;;)
  114.         {
  115.         if ( (inpchar=getchar()) == '\b' )
  116.             {
  117.             putchar(inpchar);
  118.             putchar(' ');
  119.             putchar(inpchar);
  120.             abort=0;
  121.             break;
  122.             }
  123.         else if ( inpchar == '\n' )
  124.             {
  125.             savefile(editfile, newfile, fd, text, count);
  126.             break;
  127.             }
  128.         else
  129.             putchar(BELL);
  130.         }
  131.         break;
  132.     case 'A': /* abort editing session */
  133.     case 'a':
  134.         for (;;)
  135.         {
  136.         if ( (inpchar=getchar()) == '\b' )
  137.             {
  138.             putchar(inpchar);
  139.             putchar(' ');
  140.             putchar(inpchar);
  141.             abort=0;
  142.             break;
  143.             }
  144.         else if ( inpchar == '\n' )
  145.             {
  146.             fputs("\nQuit without saving (return=n/Y)? ", stdout);
  147.             if ( (inpchar=getchar()) == 'Y' )
  148.             {
  149.             putchar(inpchar);
  150.             fflush(stdout);
  151.             cleanup(2);
  152.             break;
  153.             }
  154.             else
  155.             {
  156.             putchar(inpchar);
  157.             fflush(stdout);
  158.             puts("");
  159.             break;
  160.             }
  161.             }
  162.         }
  163.         break;
  164.     case 'Q': /* because q to quit is so common */
  165.     case 'q':
  166.         cleanup(2);
  167.         break;
  168.     case 'L': /* list the file */
  169.     case 'l':
  170.         if ( (linenum=getlinenum(count, "cr=1", "")) != -1 )
  171.         {
  172.         if ( linenum != 0 )
  173.             {
  174.             puts("");
  175.             listtext(text, count, linenum);
  176.             }
  177.         else
  178.             abort=0;
  179.         }
  180.         break;
  181.     case 'E': /* edit a line - sudsutite command */
  182.     case 'e':
  183.         /* if (delimiter)
  184.         if (*delimiter)
  185.             *delimiter='\0';
  186.         */
  187.         if (delimiter)
  188.         {
  189.         free(delimiter);
  190.         }
  191.         else
  192.         {
  193.         malloc(delimiter, 2);
  194.         *delimiter='\0';
  195.         }
  196.  
  197.         if ( (linenum=getlinenum(count, "/?=cr", delimiter)) != -1 )
  198.         {
  199.         if ( linenum != 0 )
  200.             {
  201.             if ( ! *delimiter )
  202.             puts("");
  203.             text = editline(text, linenum, *delimiter);
  204.             }
  205.         else
  206.             abort=0;
  207.         }
  208.         break;
  209.     case 'I': /* insert a line */
  210.     case 'i':
  211.         if ( (linenum=getlinenum(count, "", "")) != -1)
  212.         {
  213.         if ( linenum != 0 )
  214.             {
  215.             puts("");
  216.             text=addlines(text,overflow,&count,linenum,stdin,FALSE);
  217.             }
  218.         else
  219.             abort=0;
  220.         }
  221.         break;
  222.     case 'D': /* delete a line */
  223.     case 'd':
  224.         if ( (linenum=getlinenum(count, "", "")) != -1)
  225.         {
  226.         if (linenum != 0)
  227.             {
  228.             puts("");
  229.             text=deleteline(text, &count, linenum);
  230.             }
  231.         else
  232.             abort=0;
  233.         }
  234.         break;
  235.     case 'C': /* continue editing at EOF */
  236.     case 'c':
  237.         for (;;)
  238.         {
  239.         if ( (inpchar=getchar()) == '\b' )
  240.         {
  241.             putchar(inpchar);
  242.             putchar(' ');
  243.             putchar(inpchar);
  244.             abort=0;
  245.             break;
  246.         }
  247.         else if ( inpchar == '\n' )
  248.             {
  249.             puts("");
  250.             text=addlines(text,overflow,&count,count+1,stdin,FALSE);
  251.             break;
  252.             }
  253.         else
  254.             putchar(BELL);
  255.         }
  256.         break;
  257.     case 'M': /* modify - multi use line editing */
  258.     case 'm':
  259.         if ( (linenum=getlinenum(count, "", "")) != -1 )
  260.         {
  261.         if ( linenum != 0 )
  262.             {
  263.             puts("");
  264.             text=modify(text, linenum);
  265.             }
  266.         else
  267.             abort=0;
  268.         }
  269.         break;
  270.     case '\b':
  271.         putchar(' ');
  272.         putchar(BELL);
  273.         fflush(stdout);
  274.         abort=0; /* do not print menu again */
  275.         break;
  276.     case '\n':
  277.         fputs("Command? ", stdout);
  278.         abort=0; /* do not print menu again */
  279.         break;
  280.     case 'H':
  281.     case 'h':
  282.         for (;;)
  283.         {
  284.         if ( (inpchar=getchar()) == '\b' )
  285.             {
  286.             putchar(inpchar);
  287.             putchar(' ');
  288.             putchar(inpchar);
  289.             abort=0;
  290.             break;
  291.             }
  292.         else if ( inpchar == '\n' )
  293.             {
  294.             help();
  295.             break;
  296.             }
  297.         else
  298.             putchar(BELL);
  299.         }
  300.         break;
  301.     case 'j':
  302.         puts("\n\nAuthor: Jay Konigsberg\n");
  303.         puts("Date  : June 1990\n");
  304.         puts("uucp  : jak@sactoh0\n");
  305.         break;
  306.     default :
  307.         printf("%c: not a valid command.\n", inpchar);
  308.     }
  309.     }
  310. }
  311.