"Fossies" - the Fresh Open Source Software Archive

Member "replace-2.24/text.c" (7 Oct 2004, 7965 Bytes) of package /linux/privat/old/replace-2.24-src-11.11.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /* text.c - Routines to handle replacements in text files
    2    (C) Richard K. Lloyd 2001-2004 */
    3 
    4 #define Extern extern
    5 #include "replace.h"
    6 
    7 static int linereps,maxreplineswarn; /* Replacement counts/flags */
    8 static size_t newlinepos; /* Where we've written to in the new line buffer */
    9 static char *ostr,*nstr; /* Old and new strings */
   10 static size_t osl,nsl; /* Old and new string sizes */
   11 static size_t oldlen; /* Old string size */
   12 static int asked; /* Asked about replacement for this string yet ? */
   13 
   14 #ifdef __STDC__
   15 static void copy_char(char thechar)
   16 #else
   17 static void copy_char(thechar)
   18 char thechar;
   19 #endif
   20 {
   21    /* Copy a character into the new line buffer, allocating
   22       extra memory if necessary */
   23    if (newlinepos+1>=newlinesize)
   24       newline=alloc_mem(newline,&newlinesize,newlinesize*2+1);
   25    newline[newlinepos++]=thechar;
   26 }
   27 
   28 #ifdef __STDC__
   29 static int display_replace(char *oldline,size_t linecol)
   30 #else
   31 static int display_replace(oldline,linecol)
   32 char *oldline;
   33 size_t linecol;
   34 #endif
   35 {
   36    /* Show replacement if in verbose mode level 2.
   37       Return 0 if to continue replacement or 1 if to abort */
   38    if (verbose==2)
   39    {
   40       char tmpold[BUFSIZ],tmpnew[BUFSIZ];
   41       (void)strncpy(tmpold,&oldline[linecol],osl); tmpold[osl]='\0';
   42       (void)strncpy(tmpnew,&newline[newlinepos-nsl],nsl); tmpnew[nsl]='\0';
   43       if (!asked)
   44       {
   45          char nicemess[BUFSIZ];
   46          (void)snprintf(nicemess,BUFSIZ,"\"%s\" with \"%s\" in line %d",ostr,nstr,linenum);
   47          (void)fprintf(stderr,"Line %d: %s\n",linenum,oldline);
   48          if (!ask_user(2,nicemess))
   49          {
   50             size_t retline;
   51             for (retline=0;retline<=oldlen;retline++)
   52             newline[retline]=oldline[retline];
   53             return(1);
   54          }
   55          asked=1;
   56       }
   57       (void)fprintf(stderr,"\"%s\" -> \"%s\" (line %d, column %d)\n",
   58                     tmpold,tmpnew,linenum,(int)(linecol+1));
   59    }
   60    repcount++;
   61    if (!linereps) linecount++;
   62    linereps++;
   63    return(0);
   64 }
   65 
   66 #ifdef __STDC__
   67 static char *replace_line(char *oldline,size_t strnum)
   68 #else
   69 static char *replace_line(oldline,strnum)
   70 char *oldline;
   71 size_t strnum;
   72 #endif
   73 {
   74    /* Replace strings in "oldline" using old/new strings number "strnum" */
   75    size_t oldmax,a=0,comppos,c;
   76    char newchar;
   77    int maxtimeswarn=0;
   78                                                                                 
   79    asked=(prompt!=2);
   80    ostr=oldstr[strnum],nstr=newstr[strnum];
   81    newlinepos=0; osl=oldstrlen[strnum]; nsl=newstrlen[strnum];
   82    oldlen=strlen(oldline); oldmax=oldlen-osl;
   83 
   84    if (linenum>=startline && osl<=oldlen)
   85    {
   86       if (oldlen && osl)
   87       {
   88          newline=alloc_mem(newline,&newlinesize,oldlen);
   89          while (a<startcol && a<oldlen) copy_char(oldline[a++]);
   90          while (a<=oldmax)
   91          {
   92             if (sensitive)
   93                c=(size_t)strncmp(&oldline[a],ostr,osl);
   94             else
   95                c=strncasecmp(&oldline[a],ostr,osl);
   96             if (word && !c)
   97             {
   98                if (a) c=isalnum((int)oldline[a-1]);
   99                if (!c && a<oldmax) c=isalnum((int)oldline[a+osl]);
  100             }
  101             if (c) copy_char(oldline[a++]);
  102             else
  103             if (maxtimes && linereps==maxtimes)
  104             {
  105                if (!maxtimeswarn)
  106                {
  107                   (void)fprintf(stderr,"WARNING: Attempt to exceed ");
  108                   plural("replacement",(LONG_LONG)maxtimes);
  109                   (void)fprintf(stderr," at line %d ignored\n",linenum);
  110                   maxtimeswarn=1;
  111                }
  112                copy_char(oldline[a++]);
  113             }
  114             else
  115             if (maxreplines && !linereps && linecount==maxreplines)
  116             {
  117                if (!maxreplineswarn)
  118                {
  119                   (void)fprintf(stderr,"WARNING: Attempt to exceed ");
  120                   plural("line replacement",(LONG_LONG)maxreplines);
  121                   (void)fprintf(stderr," in %s ignored\n",filetype);
  122                   maxreplineswarn=1;
  123                }
  124                copy_char(oldline[a++]);
  125             }
  126             else
  127             {
  128                comppos=a;
  129                for (c=0;c<nsl;c++)
  130                {
  131                   newchar=nstr[c];
  132                   if (!sensitive)
  133                   {
  134                      if (isupper((int)oldline[comppos++]))
  135                         newchar=(char)toupper(newchar);
  136                      else newchar=(char)tolower(newchar);
  137                      if (c>=osl-1) comppos--;
  138                   }
  139                   copy_char(newchar);
  140                }
  141                if (display_replace(oldline,a)) return(newline);
  142                a+=osl;
  143             }
  144          }
  145       }
  146       else
  147       /* If old string is "" and the line is also "", then copy the
  148          new string in. Otherwise, do nothing to line if it's "" */
  149       if (!oldlen && !osl)
  150       {
  151          for (c=0;c<nsl;c++) copy_char(nstr[c]);
  152          if (display_replace(oldline,a)) return(newline);
  153       }
  154    } 
  155    while (a<oldlen) copy_char(oldline[a++]);
  156    copy_char('\0');
  157    return(newline);
  158 }
  159 
  160 #ifdef __STDC__
  161 static char *replace_fgets(char *str,size_t bufsize,FILE *stream)
  162 #else
  163 static char *replace_fgets(str,bufsize,stream)
  164 char *str;
  165 size_t bufsize;
  166 FILE *stream;
  167 #endif
  168 {
  169    size_t bufpos=0;
  170    /* Routine that behaves like fgets(), but additionally will retrieve
  171       from 256-byte buffer used to determine file type. This is because
  172       you can't rewind(stdin) or fwrite() to stdin either to retrieve the
  173       stdin bytes. If the 256 bytes become exhausted during this routine,
  174       pre-pend what's read in and fgets() the additional bytes. If exhausted
  175       *before* the routine is called, just call fgets() directly. */
  176    if (autobinread<autobinsize)
  177    {
  178       int gotnl=0;
  179       while (autobinread<autobinsize && bufpos<bufsize-1 && !gotnl)
  180       {
  181          gotnl=(binchunkptr[autobinread]=='\n');
  182          str[bufpos++]=binchunkptr[autobinread++];
  183       }
  184       if (gotnl || bufpos==bufsize-1)
  185       {
  186          str[bufpos]='\0'; return(str);
  187       }
  188    }
  189    return(fgets(&str[bufpos],(int)(bufsize-bufpos),stream));
  190 }
  191 
  192 #ifdef __STDC__
  193 int text_io(FILE *fd,FILE *fdout)
  194 #else
  195 int text_io(fd,fdout)
  196 FILE *fd,*fdout;
  197 #endif
  198 {
  199    /* Read/write from/to a text file. Returns zero if success, != 0 if fails */
  200    int eof=0,gotfail=0;
  201    linereps=0; linecount=0; maxreplineswarn=0;
  202    thestring=alloc_mem(thestring,&thestringsize,BIN_CHUNK);
  203    while (!eof && !gotfail)
  204    {
  205       int eol=0,boff=0;
  206       char eolc,eolstr[2];
  207       eolstr[0]='\0';
  208       while (!eol && !eof && !gotfail)
  209       {
  210          thestring[boff]='\0';
  211          eof=(replace_fgets(&thestring[boff],thestringsize-boff,fd)==(char *)NULL && !thestring[boff]);
  212          gotfail=ferror(fd);
  213          if (!eof && !gotfail)
  214          {
  215             boff+=(strlen(&thestring[boff])-1);
  216             eolc=thestring[boff];
  217             if (eolc=='\n' || eolc=='\r')
  218             {
  219                thestring[boff]='\0'; eol=1;
  220                (void)strcpy(eolstr,"\n");
  221             }
  222             else
  223             {
  224                boff++;
  225                thestring=alloc_mem(thestring,&thestringsize,thestringsize+BIN_CHUNK);
  226             }
  227          }
  228       }
  229       if ((boff || eol) && !gotfail)
  230       {
  231          linenum++;
  232          if (numstrs==1)
  233          {
  234             if (fake) (void)replace_line(thestring,1);
  235             else (void)fprintf(fdout,"%s%s",replace_line(thestring,1),eolstr);
  236          }
  237          else
  238          {
  239             size_t eachstr;
  240             for (eachstr=1;eachstr<=numstrs;eachstr++)
  241             {
  242                char *tempstr=replace_line(thestring,eachstr);
  243                thestring=alloc_mem(thestring,&thestringsize,strlen(tempstr));
  244                (void)strcpy(thestring,tempstr);
  245             }
  246             if (!fake) (void)fprintf(fdout,"%s%s",thestring,eolstr);
  247          }
  248          linereps=0;
  249          if (!fake) gotfail=ferror(fdout);
  250       }
  251    }
  252    return(gotfail);
  253 }