home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume27 / mthreads / part01 / unipatch.c < prev   
Encoding:
C/C++ Source or Header  |  1993-11-20  |  1.5 KB  |  62 lines

  1. /*
  2. A filter to turn a unified diff into a degenerate context diff (no '!'s)
  3. for patch. Version 1.1. Author: davison@borland.com
  4. */
  5. #include <stdio.h>
  6. #define ERR(a) {fputs(a,stderr);exit(1);}
  7. #define NUM(x) {for(x=0;*cp<='9'&&*cp>='0';)x=x*10+*cp++-'0';ch= *cp++;}
  8. struct Ln {struct Ln *lk; char t; char s[1];} r,*h,*ln;
  9. char bf[2048],*cp,ch,*malloc();
  10. long os,ol,ns,nl,ne,lncnt;
  11. main()
  12. {
  13.  for(;;){
  14.   for(;;){
  15.    if(!fgets(bf,sizeof bf,stdin)) exit(0);
  16.    lncnt++;
  17.    if(!strncmp(bf,"@@ -",4)) break;
  18.    fputs(bf,stdout);
  19.   }
  20.   ol=nl=1, cp=bf+4;
  21.   NUM(os)
  22.   if(ch==',') NUM(ol)
  23.   if(*cp++!='+') goto bad;
  24.   NUM(ns)
  25.   if(ch==',') NUM(nl)
  26.   if(*cp!='@') goto bad;
  27.   r.lk=0, h= &r, ne=ns+nl-1;
  28.   printf("***************\n*** %ld,%ld ****\n",os,os+ol-(os>0));
  29.   while(ol||nl){
  30.    if(!fgets(bf,sizeof bf,stdin)){
  31.     if(nl>2) ERR("Unexpected end of file.\n")
  32.     strcpy(bf," \n");
  33.    }
  34.    lncnt++;
  35.    if(*bf=='\t'||*bf=='\n') ch=' ', cp=bf;
  36.    else ch= *bf, cp=bf+1;
  37.    switch(ch){
  38.    case'-':if(!ol--) goto bad;
  39.     printf("- %s",cp);
  40.     break;
  41.    case'=':ch=' ';
  42.    case' ':if(!ol--) goto bad;
  43.     printf("  %s",cp);
  44.    case'+':if(!nl--) goto bad;
  45.     ln = (struct Ln*)malloc(sizeof(*ln)+strlen(cp));
  46.     if(!ln) ERR("Out of memory!\n")
  47.     ln->lk=0, ln->t=ch, strcpy(ln->s,cp);
  48.     h->lk=ln, h=ln;
  49.     break;
  50.    default:
  51. bad:    fprintf(stderr,"Malformed unified diff at line %ld: ",lncnt);
  52.     ERR(bf)
  53.    }
  54.   }
  55.   printf("--- %ld,%ld ----\n",ns,ne);
  56.   for(ln=r.lk;ln;ln=h){
  57.    printf("%c %s",ln->t,ln->s);
  58.    h=ln->lk; free(ln);
  59.   }
  60.  }
  61. }
  62.