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

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