home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 208_01 / e8.c < prev    next >
Text File  |  1987-10-13  |  5KB  |  192 lines

  1. /*
  2. HEADER:        CUG208;
  3. TITLE:        'e' for CP/M68K
  4. VERSION:    1.48b
  5.  
  6. DESCRIPTION:    "a screen editor";
  7.  
  8. KEYWORDS:    editor;
  9. SYSTEM:        CP/M68K, V1.2;
  10. FILENAME:    e/e8.c
  11. WARNINGS:    "the default value is for systems with 128K bytes
  12.          of memory or more";
  13. SEE-ALSO:    cpm68k.c, e68k.doc, CUG VOL 133;
  14. AUTHORS:    G.N.Gilbert('e'), J.W.Haefner(for DeSmet C on MSDOS and UNIX)
  15. CODER:        Yoshimasa Tsuji
  16. COMPILERS:    DRI C(Alcyon C) for CP/M68K;
  17. */
  18. /*
  19.     FUNCTIONS: loc,getline,inject,delete,puttext,
  20.             readtext,balloc,error,addhistory
  21.     PURPOSE: get and put text lines into and out of storage
  22. */
  23.  
  24. #include "e.h"
  25.  
  26.  
  27. loc(line,move)    /*returns line+move, adjusted to be within text*/
  28. register int line;
  29. register int move;
  30. {
  31. register int y;
  32.  
  33.     if ( (y=line+move) < 1) y=1;
  34.     if (lastl == UNKNOWN && y > lastread) readtext(y);
  35.     if (y > lastl) return(lastl);
  36.     return(y);
  37. }
  38. /*
  39.  * signed short as an index to tp[] will create a boundary error.
  40.  *        11/June/1987 Y. Tsuji
  41.  */
  42. char *
  43. getline(line)    /*returns address of text of line 'line' and updates
  44.                 page usage*/
  45. register int line;
  46. {
  47.     register int slot;
  48.  
  49.     line = loc(line,0);
  50.     if ( (slot=pageloc[tp[(unsigned)line].page]) <= 0)
  51.         slot=swappin(tp[(unsigned)line].page);
  52.     if (usage[slot] >= INUSE) usage[slot]= ++clock;
  53.     return(slotaddr[slot] + tp[(unsigned)line].moffset);
  54. }
  55.  
  56. inject(line,txt)    /* inserts 'txt' after 'line' */
  57. register int line;
  58. char *txt;
  59. {
  60.  
  61.     int p;
  62.  
  63.     addhistory(line,HISTINSERT);
  64.     if ((char *)&(tp[(unsigned)lastread+2]) >= slotaddr[tppages]) {
  65.         /*need another slot to store tp's in */
  66.         if (tppages+2 >= slotsinmem) {
  67.             error("Too many lines of text");
  68.             return(FAIL);
  69.         }
  70.         if (usage[tppages] == NOTAVAIL) /*ie page is being alloc'ed into*/
  71.             allocp= PAGESIZE+1; /*force balloc into new page*/
  72. #ifdef DEBUG
  73.         else if(usage[tppages] == FREE)
  74.             putstr("\007\007");
  75. #endif
  76.         swapout(tppages);
  77.         usage[tppages++]= NOTAVAIL;
  78.     }
  79.  
  80.     /* CPM68K specific function blkmove
  81.      *        blkmov(to, from, bytes)
  82.      */
  83.  
  84.     if (++line < ++lastread)
  85.         blkmove(&tp[(unsigned)line+1], &tp[(unsigned)line], (lastread-line)*sizeof(struct addr) );
  86.     tp[(unsigned)line].moffset=p=balloc(1+strlen(txt));
  87.     tp[(unsigned)line].page=newpage;
  88.     strcpy(npaddr+ p,txt);
  89.     if (lastl != UNKNOWN)lastl++;
  90.     return(line);
  91. }
  92.  
  93. delete(ffrom)    /*deletes a line by shifting pointers*/
  94. register int ffrom;
  95. {
  96.  
  97.     addhistory(ffrom,HISTDELETE);
  98.     blkmove(&tp[(unsigned)ffrom], &tp[(unsigned)ffrom +1], (lastread-ffrom)*sizeof(struct addr) );
  99.     if (lastl != UNKNOWN) lastl--;
  100.     lastread--;
  101.     if (lastl < 1)lastl=1;
  102.     if (lastread < 1)lastread=1;
  103. }
  104.  
  105. puttext()    /*replaces cline's text if it has been altered*/
  106. {
  107. register int p, cp;
  108.     if (altered) {
  109.         addhistory(cline,HISTREPLACE);
  110.         altered=NO;
  111.         if (!trail) {
  112.             for(cp=strlen(text)-1;cp>=0 && isspace(text[cp]);
  113.                 cp--) text[cp]= '\0';
  114.         }
  115.         tp[(unsigned)cline].moffset=p=balloc(1+strlen(text));
  116.         tp[(unsigned)cline].page=newpage;
  117.         strcpy(npaddr+ p,text);
  118.     }
  119. }
  120.  
  121. readtext(line)    /*reads file being edited into virtual memory until 'line'
  122.           is read, or eof.  If eof, sets lastl to number of last line
  123.           read.  File is assumed to be already open*/
  124. int line;
  125. {
  126.     char txt[LLIM];
  127.     register char *t;
  128.     register int i, c, l;
  129.  
  130.     storehist=NO;
  131.     while (lastread < line) {
  132.         for (i=0, t= &txt[0]; i < LLIM && ((c= fgetc(textfp)) >= ' ' ||
  133.             c != '\n' && c != EOF); i++) {
  134.                 if (c) *t++ =c;
  135.             else i--;
  136.         }
  137.         *t='\0';
  138.         if ((l=inject(lastread,txt)) == FAIL) {lastl=lastread;break;}
  139.         else if ((lastread=l)%100 == 0) ptlineno(l);
  140.         if (goteof= c == EOF) { fclose(textfp);lastl=lastread;
  141.             break; }
  142.         checkkey();
  143.     }
  144.     storehist=YES;
  145. }
  146.  
  147. balloc(n)    /*allocate and return the offset in newpage of a vector size n*/
  148. int n;
  149. {
  150.     register short slot;
  151.     if (allocp  + n >= PAGESIZE) {
  152.         /*no room in current newpage; get another*/
  153.         if (pageloc[newpage] > 0)usage[pageloc[newpage]]=INUSE;
  154.         pageloc[++newpage]=slot=freememslot();
  155.         usage[slot]= NOTAVAIL;
  156.         allocp=0;
  157.         npaddr=slotaddr[slot];
  158.     }
  159.     allocp+=n;
  160.  
  161.     return(allocp-n);
  162. }
  163.  
  164. error(message)
  165. char *message;
  166. {
  167.     if (errmess) return;
  168.     gotoxy(32,0);
  169.     putstr(errmess=message);
  170.     resetcursor();
  171.     inbufp=0;
  172. }
  173.  
  174. addhistory(l,type)
  175. register int l;
  176. short type;
  177. {
  178.     register struct histstep *step;
  179.  
  180.     if(!storehist) return;
  181.  
  182.     step= &history[histptr];
  183.     if (++histptr == HISTLEN) histptr=0;
  184.     if (histcnt < HISTLEN) histcnt++;
  185.  
  186.     step->histp.page=tp[(unsigned)l].page;
  187.     step->histp.moffset=tp[(unsigned)l].moffset;
  188.     step->histline=l;
  189.     step->histtype=type;
  190.     step->histcomm=ncommand;
  191. }
  192.