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

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