home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Interactive Guide / c-cplusplus-interactive-guide.iso / c_ref / csource5 / 331_01 / se.c < prev    next >
Text File  |  1990-06-12  |  21KB  |  806 lines

  1. /*
  2. HEADER:         CUG000.00;
  3. TITLE:          SE (nee E) stack editor -- main();
  4. DATE:           05/19/87;
  5.  
  6. DESCRIPTION:   "A full screen editor descended from Gilbert's 'e'(1981)
  7.                 through Haefner's qed and Tearle's ged.  Similar to
  8.                 Wordstar, with text stack, undo/redo, and other enhancements.
  9.                 Utilizes large RAM if available, virtual memory text storage
  10.                 in small systems.";
  11.  
  12. KEYWORDS:       Word processing, e, editor, qed, ged, full screen editor,
  13.                 text editor, program editor;
  14.  
  15. SYSTEM:         MS-DOS;
  16. FILENAME:       SE.C;
  17. SEE-ALSO:       CUG133, CUG133, CUG199;
  18. AUTHORS:        G. Nigel Gilbert, James W. Haefner, Mel Tearle, G. Osborn;
  19. COMPILERS:      Microsoft 4.00;
  20. */
  21.  
  22. /*   e/qed/ged screen editor
  23.  
  24.     (C) G. Nigel Gilbert, MICROLOGY, 1981
  25.            licensed for private non-profitmaking use 1983
  26.            August-December 1981
  27.  
  28.     Modified:   Aug-Dec   1984:  BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  29.                 March     1985:  BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  30.                 May       1986:  converted to ged - Mel Tearle
  31.  
  32.     FUNCTIONS:  main, initialise, edit, finish, dispose, xit,
  33.                 askforfile, seldisk
  34.  
  35.     PURPOSE:    initialise; process commands;
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <ctype.h>
  40. #include <setjmp.h>
  41. #include "ged.h"
  42.  
  43. int cbreak;
  44. int linel;
  45. int pagingdisk;
  46.  
  47. main(argc,argv)
  48. int argc;
  49. char **argv;
  50. {
  51.     int argn;
  52.     int i;
  53.     char *dig;
  54.  
  55. /* --------------------- Default option settings ---------------------*/
  56.  
  57.     initjmp =    1;     /* jmp to line 1*/
  58.     autoin  =  YES;     /* auto indent  [YES/NO]*/
  59.     backup  =  YES;     /* make ".BAK" file [YES/NO]*/
  60.     ctrl = NO;          /* Control characters stripped from input file */
  61.     trail   =   NO;     /* leave trailing blanks [YES/NO]*/
  62.     charmask =  0xFF;   /* allow input ASCII 0x80 to 0xFF */
  63.     tabwidth    =   8;  /* tab stops every n cols [number]*/
  64.     fullmem =  (long) RAMSLOTS*PAGESIZE/1024;
  65.     displaypos  = YES;  /* display line:column at top of screen*/
  66.     blockscroll = YES;  /* horizontal scroll whole page, not just current line*/
  67.     eofchar = YES;      /* write ^Z end of file */
  68.     pagingdisk  =   0;  /* create buffer file on this disk -
  69.     set to either 0 (for currently logged-in disk)
  70.     or to desired disk letter  (eg 'B') */
  71.  
  72.     defext[0] = '\0';   /* default extension (.c and .doc temporarilly hard coded*/
  73.  
  74. /* ------------------ End of default option settings -------------------- */
  75.  
  76.     inbufp = 0;
  77.     rax = 0x30 << 8;
  78. /* syscall is not a library function */
  79.     syscall();
  80.  
  81.     if ( ( rax & 0x00ff ) < 2 )  {
  82.         error1( "Must use MSDOS vers > 2.0" );
  83.         exit(0);
  84.     }
  85.  
  86.     filename[0] = name[0] = '\0';
  87.  
  88.     argn = 0;
  89.     while ( ++argn < argc )
  90.         if ( *argv[argn] == '-' )  {
  91.             dig = argv[argn]+1;
  92.             switch( toupper(*dig) )  {
  93.             case 'A' :
  94.                 autoin=!autoin;
  95.                 break;
  96.             case 'B' :
  97.                 backup=!backup;
  98.                 break;
  99.             case 'C' :
  100.                 ctrl = !ctrl;
  101.                 break;
  102.             case 'D' :
  103.                 pagingdisk = toupper(*(dig+1));
  104.                 if ( pagingdisk < 'A' || pagingdisk > 'Z' )
  105.                     goto argerr;
  106.                 break;
  107.             case 'H' :
  108.                 blockscroll=!blockscroll;
  109.                 break;
  110.  
  111. /* limit amount of RAM that will be allocated. number is in kb */
  112.             case 'M' :
  113.                 i = 0;
  114.                 while (*++dig)
  115.                     i = i*10 + *dig - '0';
  116.                 i = max(i, PAGESIZE * 3/1024);
  117.                 fullmem = min(fullmem, i);
  118.                 break;
  119.  
  120.             case 'P':
  121.                 charmask = 0x7F;   /* import Wordstar document mode */
  122.                 break;
  123.             case 'T' :
  124.                 tabwidth=0;
  125.                 while (*++dig) tabwidth=tabwidth*10+*dig-'0';
  126.                 if (tabwidth < 1)
  127.                     tabwidth = 1;
  128.                 break;
  129.             case 'S' :
  130.                 trail=!trail;
  131.                 break;
  132.             case 'Z' :
  133.                 eofchar = !eofchar;
  134.                 break;
  135.             default  :
  136.                 if ( isdigit(*dig) )  {
  137.                     initjmp = atoi((argv[argn]+1));
  138.                     break;
  139.                 }
  140. argerr:
  141.                 putstr("Illegal option: ");
  142.                 putstr(argv[argn]);
  143.                 exit(0);
  144.             }
  145.         }
  146.         else {
  147.             strcpy( filename[0] ? name : filename, argv[argn] );
  148.         }
  149.     ans[0] = patt[0] = opts[0] = '\0';
  150.  
  151. /* --------- start here ----------- */
  152.  
  153. /* remember state of ^C abort setting, then turn it off so that ^C can
  154.  * be used for cursor control.
  155.  */
  156.     rax = 0x3300;
  157.     syscall();
  158.     cbreak = rdx & 0x00ff;
  159.     rax = 0x3301;
  160.     rdx = 0;
  161.     syscall();
  162.  
  163.     keytranslate();
  164.     do {
  165.         initialise();
  166.         edit();
  167.     }
  168.     while (YES);
  169. }
  170.  
  171. /* ---------- end main ------------ */
  172.  
  173.  
  174. initialise()
  175. {
  176.     int i, warn;
  177.  
  178.     lastl = cursorx = charn = offset = lastoff = 0;
  179.     histptr = histcnt = ncommand = 0;
  180.  
  181.     cursory  = topline  = findir = jmpto = 1;
  182.     blocking = isdim = repeat = NO;
  183.     replace  = warn  = NO;
  184.     blankedmess = YES;
  185.     goteof  = YES;
  186.  
  187.     fbuf = &fbuf1;
  188.     textbuf = &tbuf1;
  189.  
  190.     rax = 0x19 << 8;
  191.     syscall();       /* get current disc */
  192.     curdsk  = rax & 0xff;
  193.     pagingfile[0] = (pagingdisk) ? pagingdisk : ( curdsk+'A' ) ;
  194.     strcpy( pagingfile+1, ":e$$$.@@@" );
  195.  
  196.     initvm();
  197.  
  198.     text[0] = '\0';
  199.     cline   = 1;
  200.     inject(0,text);  /* create null line 1 */
  201.  
  202.     terminit();
  203.     setstatusname();
  204.  
  205.     if ( filename[0] )  {
  206.         curson(NO);
  207.         cleareop(0);
  208.         gotoxy(8,9);
  209.         putstr("qed screen editor  version ");
  210.         putstr(VERSION);
  211.         putstr("  MICROLOGY 1983 and JWH 1985");
  212.         gotoxy(17,10);
  213.         putstr("ged 1.05 for Compaq, etc. - Mel Tearle 1986 ");
  214.         gotoxy(17,11);
  215.         putstr("Stack Editor version 1.00 G. Osborn 6-12-90");
  216.         gotoxy(18,13);
  217.         putstr("C Users' Group     Public Domain Software");
  218.         gotoxy(0,21);
  219.         putstr("F1 = help");
  220.  
  221. /* open a file, if it fails then try again to
  222.  * open it using filename plus default extension
  223.  * file handle is in textbuf structure.
  224.  */
  225.         while ( opentext(filename) == FAIL )  {
  226.             askforfile();
  227.             if ( !filename[0] )  goto  newfile;
  228.         }
  229.         lastl = 0;
  230.         goteof = NO;
  231.  
  232.         if ( name[0] )  {
  233.             strcpy(filename,name);
  234.             name[0] = '\0';
  235.         }
  236.     }       /* end - if filename */
  237.  
  238. newfile:
  239.     format( filename );
  240.  
  241.     for ( i = lastl; !(goteof); i += 1 )
  242.         readtext(i);
  243.     fclose(textbuf);
  244.     setstatusname();
  245.     gettext(1, charn);
  246.     if ( initjmp> 2)  {              /* (2-1) is a bad jump from line 1 */
  247.         jumpline(initjmp-cline);    /* not possible to do init jump to 2*/
  248.         initjmp = 0;
  249.     }
  250.     linel = lastl;
  251. }       /* end - initialise()  */
  252.  
  253.  
  254. /* command processor */
  255. edit()
  256. {
  257.     unsigned char inkey();
  258.     unsigned char getkey();
  259.     unsigned char  c;
  260.     unsigned char  oldcrx, inc;
  261.     int   i, j, k, to;
  262.     jmp_buf env;   /* define data storage for setjmp (Microsoft) */
  263.  
  264.     setjmp(env);
  265.  
  266.     putstatusline(linel);  /* show the flie size until editing starts */
  267.     displine = cline;
  268.     blankedmess =  NO;
  269.     calp();  /* initialization call */
  270.     i = displaypos;
  271.     displaypos = NO;
  272.     putpage();
  273.     displaypos = i;
  274.     curson(YES);
  275.     linem1 = 0;
  276.     linem2 = 0;
  277.     linem3 = 0;
  278.     lastc = 0;
  279.  
  280. /* command processing loop */
  281. nextchar:;
  282.     goodline = cline;     /* restore environment */
  283.     c = getkey();
  284.     ncommand++;
  285.     storehist = YES;
  286.  
  287.     switch(c)  {
  288.     case UPPAGE :
  289.     case DOWNPAGE :
  290.         if (c == UPPAGE)
  291.             movepage(-1);
  292.         else
  293.             movepage(0);
  294. /* don't let inpu