home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume23 / trn / part14 / unipatch.c < prev    next >
C/C++ Source or Header  |  1991-08-22  |  1KB  |  64 lines

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