home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / compiler / small_c / cb / sources / edt2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1985-08-11  |  8.2 KB  |  379 lines

  1.  
  2. /*
  3. ** edt2.c -- edit part 2
  4. */
  5.  
  6. /*
  7. ** getnum -- convert one term to a line number
  8. */
  9. getnum(pnum) int *pnum; {
  10.   int stat;
  11.   char *digits;
  12.   digits="0123456789";
  13.   stat=OK;
  14.   if(index(digits, lin[i]) >= 0) {
  15.     *pnum=ctoi(lin, &i);
  16.     --i;  /** backup then bump at end **/
  17.     }
  18.   else if(lin[i]==CURLINE) *pnum=curln;
  19.   else if(lin[i]==LASTLINE) *pnum=lastln;
  20.   else if((lin[i]==SCAN)|(lin[i]==BACKSCAN)) {
  21.     if(optpat()==ERR) stat=ERR;
  22.     else if(lin[i]==SCAN) stat=ptscan(FORWARD, pnum);
  23.     else stat=ptscan(BACKWARD, pnum);
  24.     }
  25.   else if((lin[i]==PLUS)|(lin[i]==MINUS)) --i;
  26.   else stat=EOF;
  27.   if(stat==OK) ++i;
  28.   return (status=stat);
  29.   }
  30.  
  31. /*
  32. ** optpat -- make pattern if specified at lin[i]
  33. */
  34. optpat() {
  35.   if(lin[i]==NULL) i = ERR;
  36.   else if(lin[i+1]==NULL) i = ERR;
  37.   else if(lin[i+1]==lin[i]) ++i;
  38.   else i = makpat(lin, i+1, lin[i], pat);
  39.   if(pat[0]==NULL) i = ERR;
  40.   if(i==ERR) {
  41.     pat[0]=NULL;
  42.     return ERR;
  43.     }
  44.   return OK;
  45.   }
  46.  
  47. /*
  48. ** ptscan -- scan for next occurrence of pattern
  49. */
  50. ptscan(way, num) int way, *num; {
  51.   *num=curln;
  52.   while(YES) {
  53.     if(poll(YES)==ESC) return (ERR);
  54.     if(way==FORWARD) *num=nextln(*num);
  55.     else *num=prevln(*num);
  56.     if(match(buf+getind(*num)+TEXT, pat)==YES) return OK;
  57.     if(*num==curln) break;
  58.     }
  59.   return ERR;
  60.   }
  61.  
  62. /*
  63. ** ckglob -- if global prefix, mark lines to be affected
  64. */
  65. ckglob() {
  66.   int gflag, k, line;
  67.   if((same(lin[i], GLOBAL)==NO)&(same(lin[i], EXCLUDE)==NO))
  68.     return (status=EOF);
  69.   if(same(lin[i], GLOBAL)) gflag=YES;
  70.   else gflag=NO;
  71.   ++i;
  72.   if((optpat()==ERR)|(defalt(1, lastln)==ERR))
  73.     return (status=ERR);
  74.   ++i;
  75.   line=line1;
  76.   while(line<=line2) {
  77.     if(poll(YES)==ESC) return (status=ERR);
  78.     k=gettxt(line++);
  79.     if(match(txt, pat)==gflag) buf[k+MARK]=YES;
  80.     else buf[k+MARK]=NO;
  81.     }
  82.   line=nextln(line2);
  83.   while(line!=line1) {
  84.     if(poll(YES)==ESC) return (status=ERR);
  85.     k=getind(line);
  86.     buf[k+MARK]=NO;
  87.     line=nextln(line);
  88.     }
  89.   return (status=OK);
  90.   }
  91.  
  92. /*
  93. ** defalt -- set defaulted line numbers
  94. */
  95. defalt(def1, def2) int def1, def2; {
  96.   if(nlines==0) {
  97.     line1=def1;
  98.     line2=def2;
  99.     }
  100.   if((line1>line2)|(line1<=0)|(line2>lastln))
  101.     return (status=ERR);
  102.   return (status=OK);
  103.   }
  104.  
  105. /*
  106. ** doglob -- do command at lin[i] on all marked lines
  107. */
  108. doglob() {
  109.   int count, istart, k, line;
  110.   status=OK;
  111.   count=0;
  112.   line=line1;
  113.   istart=i;
  114.   while(YES) {
  115.     if(poll(YES)==ESC) return (status=ERR);
  116.     k=getind(line);
  117.     if(buf[k+MARK]==YES) {
  118.       buf[k+MARK]=NO;
  119.       cursav=curln=line;
  120.       i=istart;
  121.       if(getlst()==OK) {
  122.         if(docmd(YES)==OK) count=0;
  123.         }
  124.       }
  125.     else {
  126.       line=nextln(line);
  127.       ++count;
  128.       }
  129.     if((count>lastln)|(status!=OK)) break;
  130.     }
  131.   return status;
  132.   }
  133.  
  134. /*
  135. ** append -- append lines after ln
  136. */
  137. append(ln, glob) int ln, glob; {
  138.   int stat;
  139.   if(glob==YES) return ERR;
  140.   curln=ln;
  141.   stat=NOSTATUS;
  142.   while(stat==NOSTATUS) {
  143.     fputc(' ', stderr);
  144.     if(fgets(lin, MAXLINE, stdin)==NULL) stat=EOF;
  145.     else {
  146.       trim(lin);
  147.       if((lin[0]==PERIOD)&(lin[1]==NULL)) stat=OK;
  148.       else if(inject(lin)==ERR) stat=ERR;
  149.       }
  150.     }
  151.   return stat;
  152.   }
  153.  
  154. /*
  155. ** kill -- delete lines from through to
  156. */
  157. kill(from, to) int from, to; {
  158.   int k1, k2;
  159.   if((from==1)&(to==lastln)) {
  160.     setbuf();
  161.     updtflag=NO;
  162.     return (status=OK);
  163.     }
  164.   if(from<=0) return (status=ERR);
  165.   k2=getind(nextln(to));
  166.   k1=getind(prevln(from));
  167.   /** leaves gotline & gotind below affected area **/
  168.   lastln=lastln-(to-from+1);
  169.   curln=prevln(from);
  170.   relink(k1, k2, k1, k2);
  171.   return (status=OK);
  172.   }
  173.  
  174. /*
  175. ** ckp -- check for "p" after command
  176. */
  177. ckp(lin, i, pflag) char lin[]; int i, *pflag; {
  178.   if(same(lin[i], PRINT)) {
  179.     *pflag=YES;
  180.     ++i;
  181.     }
  182.   else *pflag=NO;
  183.   if(view) *pflag=YES;
  184.   if(lin[i]==NULL) status=OK;
  185.   else status=ERR;
  186.   return status;
  187.   }
  188.  
  189. /*
  190. ** move -- move line1 through line2 after line3
  191. */
  192. move(line3) int line3; {
  193.   int k0, k1, k2, k3, k4, k5;
  194.   if((line1<=0)|((line1<=line3)&(line3<=line2))) return ERR;
  195.   k1=getind(line1);
  196.   k2=getind(line2);
  197.   k3=getind(nextln(line2));
  198.   k0=getind(prevln(line1));
  199.   /** leaves gotline & gotind below affected area **/
  200.   relink(k0, k3, k0, k3);
  201.   if(line3>line1) {
  202.     curln=line3;
  203.     line3=line3-(line2-line1+1);
  204.     }
  205.   else curln=line3+(line2-line1+1);
  206.   k5=getind(nextln(line3));
  207.   k4=getind(line3);
  208.   /** leaves gotline & gotind below affected area **/
  209.   relink(k4, k1, k2, k5);
  210.   relink(k2, k5, k4, k1);
  211.   return OK;
  212.   }
  213.  
  214. /*
  215. ** getrhs -- get substitution string for "s" command
  216. */
  217. getrhs(lin, i, sub, gflag) char lin[], sub[]; int *i, *gflag; {
  218.   if(lin[*i]==NULL) return ERR;
  219.   if(lin[*i+1]==NULL) return ERR;
  220.   *i=maksub(lin, *i+1, lin[*i], sub);
  221.   if(*i==ERR) return ERR;
  222.   if(same(lin[*i+1], GLOBAL)) {
  223.     *i = *i + 1;
  224.     *gflag=YES;
  225.     }
  226.   else *gflag=NO;
  227.   return OK;
  228.   }
  229.  
  230. /*
  231. ** subst -- substitute "sub" for occurrences of pattern
  232. */
  233. subst(sub, gflag) char sub[]; int gflag; {
  234.   char new[MAXLINE];
  235.   int j, k, lastn, line, n, subbed;
  236.   if(line1<=0) return ERR;
  237.   line=line1;
  238.   while(line<=line2) {
  239.     if(poll(YES)==ESC) return (ERR);
  240.     j=0;
  241.     subbed=NO;
  242.     gettxt(line);
  243.     lastn=-1;
  244.     k=0;
  245.     while(YES) {
  246.       if((gflag==YES)|(subbed==NO)) n=amatch(txt, k, pat);
  247.       else n=-1;
  248.       if((n>=0)&(lastn!=n)) {    /** replace matched text **/
  249.         subbed=YES;
  250.         catsub(txt, k, n, sub, new, &j, MAXLINE);
  251.         lastn=n;
  252.         }
  253.       if(txt[k]==NULL) break;
  254.       if((n==-1)|(n==k)) {       /** no match or null match **/
  255.         addset(txt[k], new, &j, MAXLINE);
  256.         ++k;
  257.         }
  258.       else k=n;                  /** skip matched text **/
  259.       }
  260.     if(subbed==YES) {
  261.       if(addset(NULL, new, &j, MAXLINE)==NO) return ERR;
  262.       curln=prevln(line);
  263.       if(inject(new)==ERR) {
  264.         curln=line;
  265.         return ERR;
  266.         }
  267.       kill(curln+1, curln+1);
  268.       }
  269.     ++line;
  270.     }
  271.   return OK;
  272.   }
  273.  
  274. /*
  275. ** getfn -- get file name from lin[i]
  276. */
  277. getfn(lin, i, file, max) char lin[], file[]; int i, max; {
  278.   int j, k, stat;
  279.   stat=ERR;
  280.   if(lin[i+1]==BLANK) {
  281.     j=i+2;  /** get new file name **/
  282.     skipbl(lin, &j);
  283.     k=0;
  284.     while(file[k++]=lin[j++]) if(--max < 1) break;
  285.     if((k > 1)&(max > 0)) stat=OK;
  286.     }
  287.   else if((lin[i+1]==NULL)&(savfil[0]!=NULL)) {
  288.     scopy(savfil, 0, file, 0);
  289.     stat=OK;
  290.     }
  291.   if((stat==OK)&(savfil[0]==NULL))
  292.     scopy(file, 0, savfil, 0);  /** save if no old one **/
  293.   return stat;
  294.   }
  295.  
  296. /*
  297. ** dowrit -- write "from" through "to" into file
  298. */
  299. dowrit(from, to, file) int from, to; char file[]; {
  300.   char tmp[MAXFN], *ptr;
  301.   int fd, line;
  302.  
  303.   strcpy(tmp, file);
  304.   if((ptr = strchr(tmp, '.')) == 0) ptr = tmp + strlen(tmp);
  305.   strcpy(ptr, ".$$$");
  306.   rename(file, tmp);
  307.  
  308.   if((fd=fopen(file, "w"))==NULL) return ERR;
  309.   line=from;
  310.   while(line<=to) {
  311.     fputs(buf+getind(line++)+TEXT, fd);
  312.     if(fputc('\n', fd)==EOF) {
  313.       fputs("write ", stderr);
  314.       fclose(fd);
  315.       return ERR;
  316.       }
  317.     }
  318.   if(fclose(fd)) return ERR;
  319.   unlink(tmp);
  320.   updtflag=NO;
  321.   return OK;
  322.   }
  323.  
  324. /*
  325. ** doprnt -- print lines from through to
  326. */
  327. doprnt(from, to, glob) int from, to, glob; {
  328.   int j, k;
  329.   char pref;
  330.   if(from<=0) return OK;
  331.   if(same(lin[i], PRINT)) {
  332.     ++i;
  333.     if((lin[i]>='0')&(lin[i]<='9')) {
  334.       if(lin[i+1]==NULL) context=lin[i]-'0';
  335.       else return ERR;
  336.       }
  337.     else if(lin[i]!=NULL) return ERR;
  338.     }
  339.   if((from==to)&(glob==NO)) {
  340.     j=from-context;
  341.     k=to+context;
  342.     }
  343.   else {
  344.     j=from;
  345.     k=to;
  346.     }
  347.   if(j < 1) j=1;
  348.   if(k > lastln) k=lastln;
  349.   if(glob==NO) fputs(CLEAR, stdout);
  350.   while(j <= k) {
  351.     if(poll(YES)) {
  352.       /* underflow handled in docmd() */
  353.       curln = j - context;
  354.       return OK;
  355.       }
  356.     if((j==to)&(glob==NO)) pref=CLFLAG;
  357.     else pref=' ';
  358.     gettxt(j++);
  359.     fputc(pref, stdout);
  360.     fputs(txt, stdout);
  361.     fputc('\n', stdout);
  362.     }
  363.   curln=to;
  364.   return OK;
  365.   }
  366.  
  367. /*
  368. ** enter -- enter a new file
  369. */
  370. enter(name) char name[]; {
  371.   int err;
  372.   err=doread(0, name);
  373.   if(view) doprnt(1, 1, NO);
  374.   curln=1;
  375.   updtflag=NO;
  376.   return err;
  377.   }
  378.  
  379.