home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / simtel / sigm / vols000 / vol076 / ed10.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-02-18  |  5.9 KB  |  365 lines

  1. /* ED10.C */
  2.  
  3. #include b:ed0.c
  4. #include b:ed1.ccc
  5. int bufcflag;
  6. char *bufp;
  7. char *bufpmax;
  8. char *bufend;
  9. int bufline;
  10. int bufmaxln;
  11. /* mbuffer is external for Software Toolworks compiler to force it beyond
  12.    code sections */
  13. extern char mbuffer[1];
  14. bufnew()
  15. {
  16.     bufp=bufpmax=mbuffer+1;
  17. /* need more room for Software Toolworks stack and buffers */
  18.     bufend=sysend()-1500;
  19.     bufline=1;
  20.     bufmaxln=0;
  21.     mbuffer[0]=CR;
  22.     bufcflag=NO;
  23. }
  24. bufln()
  25. {
  26.     return(bufline);
  27. }
  28. bufchng()
  29. {
  30.     return(bufcflag);
  31. }
  32. bufsaved()
  33. {
  34.     bufcflag=NO;
  35. }
  36. buffree()
  37. {
  38.     return(bufend-bufp);
  39. }
  40. bufgo(line) int line;
  41. {
  42.     line=min(bufmaxln+1,line);
  43.     line=max(1,line);
  44.     if (line==bufline) {
  45.         return(OK);
  46.     }
  47.     while (line<bufline) {
  48.         if (bufup()==ERR) {
  49.             return(ERR);
  50.         }
  51.     }
  52.     while (line>bufline) {
  53.         if (bufdn()==ERR) {
  54.             return(ERR);
  55.         }
  56.     }
  57.     return(OK);
  58. }
  59. bufup()
  60. {
  61. char *oldbufp;
  62.     oldbufp=bufp;
  63.     if (bufattop()) {
  64.         return(OK);
  65.     }
  66.     if (*--bufp!=CR) {
  67.         syserr("bufup:    missing CR");
  68.         bufp=oldbufp;
  69.         return(ERR);
  70.     }
  71.     while (*--bufp!=CR) {
  72.         ;
  73.     }
  74.     bufp++;
  75.     if (bufp<(mbuffer+1)) {
  76.         syserr ("bufup: bufp underflow");
  77.         bufp=oldbufp;
  78.         return(ERR);
  79.     }
  80.     bufline--;
  81.     return(OK);
  82. }
  83. bufdn()
  84. {
  85. char *oldbufp;
  86.     oldbufp=bufp;
  87.     if (bufatbot()) {
  88.         return(OK);
  89.     }
  90.     while (*bufp++!=CR) {
  91.         ;
  92.     }
  93.     if (bufp>bufpmax) {
  94.         syserr("bufdn: bufp overflow");
  95.         bufp=oldbufp;
  96.         return(ERR);
  97.     }
  98.     bufline++;
  99.     return(OK);
  100. }
  101. bufins(p,n) char *p; int n;    /* altered: a full buffer does not truncate */
  102. {                /*   line, but issues warning and returns   */
  103. int k;                 /*   ERR - allows editing of large files     */
  104.     if(bufext(n+1)==ERR) {
  105.         return(ERR);
  106.     }
  107.     k=0;
  108.     while (k<n) {
  109.         *(bufp+k)= *(p+k);
  110.         k++;
  111.     }
  112.     *(bufp+k)=CR;
  113.     bufmaxln++;
  114.     if ((n==0)&(bufnrbot())) {
  115.         ;
  116.     }
  117.     else {
  118.         bufcflag=YES;
  119.     }
  120.     return(OK);
  121. }
  122. buf1ins(p,n) char *p; int n;     /* altered: a full buffer does not truncate */
  123. {                /*   line, but issues warning and returns   */
  124. int k, x;            /*   ERR - allows editing of large files    */
  125.     x = 0;            /*   in pieces */
  126.     if(bufstrch(n+1)==ERR) {
  127.         x = 1;
  128.     }
  129.     k=0;
  130.     while (k<n) {
  131.         *(bufp+k)= *(p+k);
  132.         k++;
  133.     }
  134.     *(bufp+k)=CR;
  135.     bufmaxln++;
  136.     if ((n==0)&(bufnrbot())) {
  137.         ;
  138.     }
  139.     else {
  140.         bufcflag=YES;
  141.     }
  142.     if (x == 0) {
  143.         return(OK);
  144.     }
  145.     else {
  146.         return(ERR);
  147.     }
  148. }
  149. bufdel()
  150. {
  151.     return(bufndel(1));
  152. }
  153. bufndel(n) int n;
  154. {
  155. int oldline;
  156. int k;
  157. char *oldbufp;
  158.     oldline=bufline;
  159.     oldbufp=bufp;
  160.     k=0;
  161.     while ((n--)>0) {
  162.         if (bufatbot()) {
  163.             break;
  164.         }
  165.         if (bufdn()==ERR) {
  166.             bufline=oldline;
  167.             bufp=oldbufp;
  168.             return(ERR);
  169.         }
  170.         k++;
  171.     }
  172.     bufupmov(bufp,bufpmax-1,bufp-oldbufp);
  173.     bufpmax=bufpmax-(bufp-oldbufp);
  174.     bufp=oldbufp;
  175.     bufline=oldline;
  176.     bufmaxln=bufmaxln-k;
  177.     bufcflag=YES;
  178.     return(OK);
  179. }
  180. bufrepl(p,n) char *p; int n;
  181. {
  182. int oldlen, k;
  183. char *nextp;
  184.     if (bufatbot()) {
  185.         return(bufins(p,n));
  186.     }
  187.     if (bufdn()==ERR) {
  188.         return(ERR);
  189.     }
  190.     nextp=bufp;
  191.     if (bufup()==ERR) {
  192.         return(ERR);
  193.     }
  194.     n=n+1;
  195.     oldlen=nextp-bufp;
  196.     if (oldlen<n) {
  197.         if (bufext(n-oldlen)==ERR) {
  198.             return(ERR);
  199.         }
  200.        /*    bufpmax=bufpmax+n-oldlen;   Note: Error in original listing */
  201.     }
  202.     else if (oldlen>n) {
  203.         bufupmov(nextp,bufpmax-1,oldlen-n);
  204.         bufpmax=bufpmax-(oldlen-n);
  205.     }
  206.     k=0;
  207.     while (k<(n-1)) {
  208.         bufp[k]=p[k];
  209.         k++;
  210.     }
  211.     bufp[k]=CR;
  212.     bufcflag=YES;
  213.     return(OK);
  214. }
  215. bufgetln(p,n) char *p; int n;
  216. {
  217. int k;
  218.     if (bufatbot()) {
  219.         return(0);
  220.     }
  221.     k=0;
  222.     while (k<n) {
  223.         if (*(bufp+k)==CR) {
  224.             return(k);
  225.         }
  226.         *(p+k)= *(bufp+k);
  227.         k++;
  228.     }
  229.     while (*(bufp+k)!=CR) {
  230.         k++;
  231.     }
  232.     return(k);
  233. }
  234. bufdnmov(from,to,length) char *from, *to; int length;
  235. {
  236.     sysdnmov(to-from+1,to+length,to);
  237. }
  238. bufupmov(from,to,length) char *from, *to; int length;
  239. {
  240.     sysupmov(to-from+1,from-length,from);
  241. }
  242. bufatbot()
  243. {
  244.     return(bufline>bufmaxln);
  245. }
  246. bufnrbot()
  247. {
  248.     return(bufline>=bufmaxln);
  249. }
  250. bufattop()
  251. {
  252.     return(bufline==1);
  253. }
  254. bufout(topline,topy,nlines) int topline, topy, nlines;
  255. {
  256. int l,p;
  257.     l=bufline;
  258.     p=bufp;
  259.     while ((nlines--)>0) {
  260.         outxy(0,topy++);
  261.         buflnout(topline++);
  262.     }
  263.     bufline=l;
  264.     bufp=p;
  265. }
  266. buflnout(line) int line;
  267. {
  268. int c;
  269.     if (bufgo(line)==ERR) {
  270.         fmtsout("disk error: line deleted",0);
  271.         outdeol();
  272.         return;
  273.     }
  274.     if (bufatbot()) {
  275.         outdeol();
  276.     }
  277.     else {
  278.         c=fmtsout(bufp,0);        /* c= added */
  279.         if (c<SCRNW1) {          /* now allows full screen */
  280.         outdeol();
  281.         }
  282.     }
  283. }
  284. bufext(length) int length;
  285. {
  286.     if ((bufpmax+length)>=bufend) {
  287.         error("main buffer is full");
  288.         return(ERR);
  289.     }
  290.     bufdnmov(bufp,bufpmax-1,length);
  291.     bufpmax=bufpmax+length;
  292.     return(OK);
  293. }
  294. /* bufstrch is bufext modifed to allow entering beyond buffer with warning */
  295. bufstrch(length) int length;
  296. {
  297.     if ((bufpmax+length)>=bufend) {
  298.         bufdnmov(bufp,bufpmax-1,length);
  299.         bufpmax=bufpmax+length;
  300.         message("Caution: main buffer is full");
  301.         return(ERR);
  302.     }
  303.     bufdnmov(bufp,bufpmax-1,length);
  304.     bufpmax=bufpmax+length;
  305.     return(OK);
  306. }
  307. buflength(first,n) int first, n;   /* new: returns length of n lines from  */
  308. {                   /* first line specified */
  309. char *start;
  310. int i,len,line;
  311.     if (n<=0) {
  312.         len=0;
  313.         return(len);
  314.     }
  315.     if (bufgo(first)==ERR) {
  316.         len=0;
  317.         return(len);
  318.     }
  319.     start=bufp;
  320.     i=1;
  321.     line = first;
  322.     while (i++<=n) {
  323.         if (bufgo(++line)==ERR) {
  324.             len=0;
  325.             return(len);
  326.         }
  327.     }
  328.     len=bufp-start;
  329.     return(len);
  330. }
  331. bufcopy(from,to,n) int from,to,n;    /* new: copies "n" lines from "from"  */
  332. {                    /*    to "to" */
  333. int length;
  334. char *source,*dest;
  335.     if (from==to){
  336.         return;
  337.     }
  338.     if ((length=buflength(from,n))==0){
  339.         return;
  340.     }
  341.     if (bufgo(from)==ERR){
  342.         return;
  343.     }
  344.     source=bufp;
  345.     if (bufgo(to)==ERR){
  346.         return;
  347.     }
  348.     dest=bufp;
  349.     if ((bufpmax+length)>=bufend) {
  350.         message ("insufficient room in buffer for copy or move");
  351.         return;
  352.     }
  353.     if (bufext(length)==ERR){
  354.         return;
  355.     }
  356.     bufgo(1);
  357.     bufmaxln=bufmaxln+n;
  358.     if (from<to) {
  359.         sysupmov(length,dest,source);
  360.     }
  361.     else {
  362.         sysupmov(length,dest,source+length);
  363.     }
  364. }
  365.