home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / rcs / sources / rcsdiff.c < prev    next >
C/C++ Source or Header  |  1992-02-17  |  12KB  |  443 lines

  1. /*
  2.  *                     RCS rcsdiff operation
  3.  */
  4. /*****************************************************************************
  5.  *                       generate difference between RCS revisions
  6.  *****************************************************************************
  7.  */
  8.  
  9. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  10.    Copyright 1990, 1991, 1992 by Paul Eggert
  11.    Distributed under license by the Free Software Foundation, Inc.
  12.  
  13. This file is part of RCS.
  14.  
  15. RCS is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2, or (at your option)
  18. any later version.
  19.  
  20. RCS is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with RCS; see the file COPYING.  If not, write to
  27. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  28.  
  29. Report problems and direct all questions to:
  30.  
  31.     rcs-bugs@cs.purdue.edu
  32.  
  33. */
  34.  
  35.  
  36.  
  37.  
  38. /* $Log: rcsdiff.c,v $
  39.  * Revision 5.13  1992/02/17  23:02:23  eggert
  40.  * Output more readable context diff headers.
  41.  * Suppress needless checkout and comparison of identical revisions.
  42.  *
  43.  * Revision 5.12  1992/01/24  18:44:19  eggert
  44.  * Add GNU diff 1.15.2's new options.  lint -> RCS_lint
  45.  *
  46.  * Revision 5.11  1992/01/06  02:42:34  eggert
  47.  * Update usage string.
  48.  *
  49.  * Revision 5.10  1991/10/07  17:32:46  eggert
  50.  * Remove lint.
  51.  *
  52.  * Revision 5.9  1991/08/19  03:13:55  eggert
  53.  * Add RCSINIT, -r$.  Tune.
  54.  *
  55.  * Revision 5.8  1991/04/21  11:58:21  eggert
  56.  * Add -x, RCSINIT, MS-DOS support.
  57.  *
  58.  * Revision 5.7  1990/12/13  06:54:07  eggert
  59.  * GNU diff 1.15 has -u.
  60.  *
  61.  * Revision 5.6  1990/11/01  05:03:39  eggert
  62.  * Remove unneeded setid check.
  63.  *
  64.  * Revision 5.5  1990/10/04  06:30:19  eggert
  65.  * Accumulate exit status across files.
  66.  *
  67.  * Revision 5.4  1990/09/27  01:31:43  eggert
  68.  * Yield 1, not EXIT_FAILURE, when diffs are found.
  69.  *
  70.  * Revision 5.3  1990/09/11  02:41:11  eggert
  71.  * Simplify -kkvl test.
  72.  *
  73.  * Revision 5.2  1990/09/04  17:07:19  eggert
  74.  * Diff's argv was too small by 1.
  75.  *
  76.  * Revision 5.1  1990/08/29  07:13:55  eggert
  77.  * Add -kkvl.
  78.  *
  79.  * Revision 5.0  1990/08/22  08:12:46  eggert
  80.  * Add -k, -V.  Don't use access().  Add setuid support.
  81.  * Remove compile-time limits; use malloc instead.
  82.  * Don't pass arguments with leading '+' to diff; GNU DIFF treats them as options.
  83.  * Add GNU diff's flags.  Make lock and temp files faster and safer.
  84.  * Ansify and Posixate.
  85.  *
  86.  * Revision 4.6  89/05/01  15:12:27  narten
  87.  * changed copyright header to reflect current distribution rules
  88.  *
  89.  * Revision 4.5  88/08/09  19:12:41  eggert
  90.  * Use execv(), not system(); yield exit status like diff(1)s; allow cc -R.
  91.  *
  92.  * Revision 4.4  87/12/18  11:37:46  narten
  93.  * changes Jay Lepreau made in the 4.3 BSD version, to add support for
  94.  * "-i", "-w", and "-t" flags and to permit flags to be bundled together,
  95.  * merged in.
  96.  *
  97.  * Revision 4.3  87/10/18  10:31:42  narten
  98.  * Updating version numbers. Changes relative to 1.1 actually
  99.  * relative to 4.1
  100.  *
  101.  * Revision 1.3  87/09/24  13:59:21  narten
  102.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  103.  * warnings)
  104.  *
  105.  * Revision 1.2  87/03/27  14:22:15  jenkins
  106.  * Port to suns
  107.  *
  108.  * Revision 4.1  83/05/03  22:13:19  wft
  109.  * Added default branch, option -q, exit status like diff.
  110.  * Added fterror() to replace faterror().
  111.  *
  112.  * Revision 3.6  83/01/15  17:52:40  wft
  113.  * Expanded mainprogram to handle multiple RCS files.
  114.  *
  115.  * Revision 3.5  83/01/06  09:33:45  wft
  116.  * Fixed passing of -c (context) option to diff.
  117.  *
  118.  * Revision 3.4  82/12/24  15:28:38  wft
  119.  * Added call to catchsig().
  120.  *
  121.  * Revision 3.3  82/12/10  16:08:17  wft
  122.  * Corrected checking of return code from diff; improved error msgs.
  123.  *
  124.  * Revision 3.2  82/12/04  13:20:09  wft
  125.  * replaced getdelta() with gettree(). Changed diagnostics.
  126.  *
  127.  * Revision 3.1  82/11/28  19:25:04  wft
  128.  * Initial revision.
  129.  *
  130.  */
  131. #include "rcsbase.h"
  132.  
  133. #if DIFF_L
  134. static char const *setup_label P((struct buf*,char const*,char const[datesize]));
  135. #endif
  136. static void cleanup P((void));
  137.  
  138. static int exitstatus;
  139. static RILE *workptr;
  140. static struct stat workstat;
  141.  
  142. mainProg(rcsdiffId, "rcsdiff", "$Id: rcsdiff.c,v 5.13 1992/02/17 23:02:23 eggert Exp $")
  143. {
  144.     static char const cmdusage[] =
  145.         "\nrcsdiff usage: rcsdiff [-ksubst] [-q] [-rrev1 [-rrev2]] [-Vn] [-xsuff] [diff options] file ...";
  146.  
  147.     int  revnums;                 /* counter for revision numbers given */
  148.     char const *rev1, *rev2;    /* revision numbers from command line */
  149.     char const *xrev1, *xrev2;    /* expanded revision numbers */
  150.     char const *expandarg, *lexpandarg, *versionarg;
  151. #if DIFF_L
  152.     static struct buf labelbuf[2];
  153.     int file_labels;
  154.     char const **diff_label1, **diff_label2;
  155.     char date2[datesize];
  156. #endif
  157.     char const *cov[9];
  158.     char const **diffv, **diffp;    /* argv for subsidiary diff */
  159.     char const **pp, *p, *diffvstr;
  160.     struct buf commarg;
  161.     struct buf numericrev;    /* expanded revision number */
  162.     struct hshentries *gendeltas;    /* deltas to be generated */
  163.     struct hshentry * target;
  164.     char *a, *dcp, **newargv;
  165.     int no_diff_means_no_output;
  166.     register c;
  167.  
  168.     exitstatus = DIFF_SUCCESS;
  169.  
  170.     bufautobegin(&commarg);
  171.     bufautobegin(&numericrev);
  172.     revnums = 0;
  173.     rev1 = rev2 = xrev2 = nil;
  174. #if DIFF_L
  175.     file_labels = 0;
  176. #endif
  177.     expandarg = versionarg = 0;
  178.     no_diff_means_no_output = true;
  179.     suffixes = X_DEFAULT;
  180.  
  181.     /* Room for args + 2 i/o [+ 2 labels] + 1 file + 1 trailing null.  */
  182.     diffp = diffv = tnalloc(char const*, argc + 4 + 2*DIFF_L);
  183.     *diffp++ = nil;
  184.     *diffp++ = nil;
  185.     *diffp++ = DIFF;
  186.  
  187.     argc = getRCSINIT(argc, argv, &newargv);
  188.     argv = newargv;
  189.     while (a = *++argv,  0<--argc && *a++=='-') {
  190.     dcp = a;
  191.     while (c = *a++) switch (c) {
  192.         case 'r':
  193.             switch (++revnums) {
  194.             case 1: rev1=a; break;
  195.             case 2: rev2=a; break;
  196.             default: faterror("too many revision numbers");
  197.             }
  198.             goto option_handled;
  199.         case '-': case 'D':
  200.             no_diff_means_no_output = false;
  201.             /* fall into */
  202.         case 'C': case 'F': case 'I': case 'L': case 'W':
  203. #if DIFF_L
  204.             if (c == 'L'  &&  ++file_labels == 2)
  205.             faterror("too many -L options");
  206. #endif
  207.             *dcp++ = c;
  208.             if (*a)
  209.             do *dcp++ = *a++;
  210.             while (*a);
  211.             else {
  212.             if (!--argc)
  213.                 faterror("-%c needs following argument%s",
  214.                     c, cmdusage
  215.                 );
  216.             *diffp++ = *argv++;
  217.             }
  218.             break;
  219.         case 'y':
  220.             no_diff_means_no_output = false;
  221.             /* fall into */
  222.         case 'B': case 'H': case 'T':
  223.         case '0': case '1': case '2': case '3': case '4':
  224.         case '5': case '6': case '7': case '8': case '9':
  225.         case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
  226.         case 'h': case 'i': case 'n': case 'p':
  227.         case 't': case 'u': case 'w':
  228.             *dcp++ = c;
  229.             break;
  230.         case 'q':
  231.             quietflag=true;
  232.             break;
  233.         case 'x':
  234.             suffixes = *argv + 2;
  235.             goto option_handled;
  236.         case 'V':
  237.             versionarg = *argv;
  238.             setRCSversion(versionarg);
  239.             goto option_handled;
  240.         case 'k':
  241.             expandarg = *argv;
  242.             if (0 <= str2expmode(expandarg+2))
  243.             goto option_handled;
  244.             /* fall into */
  245.         default:
  246.             faterror("unknown option: %s%s", *argv, cmdusage);
  247.         };
  248.       option_handled:
  249.     if (dcp != *argv+1) {
  250.         *dcp = 0;
  251.         *diffp++ = *argv;
  252.     }
  253.     } /* end of option processing */
  254.  
  255.     if (argc<1) faterror("no input file%s", cmdusage);
  256.  
  257.     for (pp = diffv+3, c = 0;  pp<diffp;  )
  258.         c += strlen(*pp++) + 1;
  259.     diffvstr = a = tnalloc(char, c + 1);
  260.     for (pp = diffv+3;  pp<diffp;  ) {
  261.         p = *pp++;
  262.         *a++ = ' ';
  263.         while ((*a = *p++))
  264.             a++;
  265.     }
  266.     *a = 0;
  267.  
  268. #if DIFF_L
  269.     diff_label1 = diff_label2 = nil;
  270.     if (file_labels < 2) {
  271.         if (!file_labels)
  272.             diff_label1 = diffp++;
  273.         diff_label2 = diffp++;
  274.     }
  275. #endif
  276.     diffp[2] = nil;
  277.  
  278.     cov[0] = 0;
  279.     cov[2] = CO;
  280.     cov[3] = "-q";
  281.  
  282.     /* now handle all filenames */
  283.     do {
  284.         ffree();
  285.  
  286.         if (pairfilenames(argc, argv, rcsreadopen, true, false)  <=  0)
  287.             continue;
  288.         diagnose("===================================================================\nRCS file: %s\n",RCSfilename);
  289.         if (!rev2) {
  290.         /* Make sure work file is readable, and get its status.  */
  291.         if (!(workptr = Iopen(workfilename,FOPEN_R_WORK,&workstat))) {
  292.             eerror(workfilename);
  293.             continue;
  294.         }
  295.         }
  296.  
  297.  
  298.         gettree(); /* reads in the delta tree */
  299.  
  300.         if (Head==nil) {
  301.             error("no revisions present");
  302.             continue;
  303.         }
  304.         if (revnums==0  ||  !*rev1)
  305.             rev1  =  Dbranch ? Dbranch : Head->num;
  306.  
  307.         if (!fexpandsym(rev1, &numericrev, workptr)) continue;
  308.         if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  309.         xrev1=target->num;
  310. #if DIFF_L
  311.         if (diff_label1)
  312.         *diff_label1 = setup_label(&labelbuf[0], target->num, target->date);
  313. #endif
  314.  
  315.         lexpandarg = expandarg;
  316.         if (revnums==2) {
  317.             if (!fexpandsym(
  318.                 *rev2 ? rev2  : Dbranch ? Dbranch  : Head->num,
  319.                 &numericrev,
  320.                 workptr
  321.             ))
  322.             continue;
  323.             if (!(target=genrevs(numericrev.string,(char *)nil,(char *)nil,(char *)nil,&gendeltas))) continue;
  324.             xrev2=target->num;
  325.             if (no_diff_means_no_output  &&  xrev1 == xrev2)
  326.             continue;
  327.         } else if (
  328.             target->lockedby
  329.         &&    !lexpandarg
  330.         &&    Expand == KEYVAL_EXPAND
  331.         &&    WORKMODE(RCSstat.st_mode,true) == workstat.st_mode
  332.         )
  333.             lexpandarg = "-kkvl";
  334.         Izclose(&workptr);
  335. #if DIFF_L
  336.         if (diff_label2)
  337.         if (revnums == 2)
  338.             *diff_label2 = setup_label(&labelbuf[1], target->num, target->date);
  339.         else {
  340.             time2date(workstat.st_mtime, date2);
  341.             *diff_label2 = setup_label(&labelbuf[1], (char*)0, date2);
  342.         }
  343. #endif
  344.  
  345.         diagnose("retrieving revision %s\n", xrev1);
  346.         bufscpy(&commarg, "-p");
  347.         bufscat(&commarg, xrev1);
  348.  
  349.         cov[1] = diffp[0] = maketemp(0);
  350.         pp = &cov[4];
  351.         *pp++ = commarg.string;
  352.         if (lexpandarg)
  353.             *pp++ = lexpandarg;
  354.         if (versionarg)
  355.             *pp++ = versionarg;
  356.         *pp++ = RCSfilename;
  357.         *pp = 0;
  358.  
  359.         if (runv(cov)) {
  360.             error("co failed");
  361.             continue;
  362.         }
  363.         if (!rev2) {
  364.             diffp[1] = workfilename;
  365.             if (workfilename[0] == '+') {
  366.             /* Some diffs have options with leading '+'.  */
  367.             char *dp = ftnalloc(char, strlen(workfilename)+3);
  368.             diffp[1] = dp;
  369.             *dp++ = '.';
  370.             *dp++ = SLASH;
  371.             VOID strcpy(dp, workfilename);
  372.             }
  373.         } else {
  374.             diagnose("retrieving revision %s\n",xrev2);
  375.             bufscpy(&commarg, "-p");
  376.             bufscat(&commarg, xrev2);
  377.             cov[1] = diffp[1] = maketemp(1);
  378.             cov[4] = commarg.string;
  379.             if (runv(cov)) {
  380.                 error("co failed");
  381.                 continue;
  382.             }
  383.         }
  384.         if (!rev2)
  385.             diagnose("diff%s -r%s %s\n", diffvstr, xrev1, workfilename);
  386.         else
  387.             diagnose("diff%s -r%s -r%s\n", diffvstr, xrev1, xrev2);
  388.  
  389.         switch (runv(diffv)) {
  390.             case DIFF_SUCCESS:
  391.                 break;
  392.             case DIFF_FAILURE:
  393.                 if (exitstatus == DIFF_SUCCESS)
  394.                     exitstatus = DIFF_FAILURE;
  395.                 break;
  396.             default:
  397.                 error("diff failed");
  398.         }
  399.     } while (cleanup(),
  400.          ++argv, --argc >=1);
  401.  
  402.  
  403.     tempunlink();
  404.     exitmain(exitstatus);
  405. }
  406.  
  407.     static void
  408. cleanup()
  409. {
  410.     if (nerror) exitstatus = DIFF_TROUBLE;
  411.     Izclose(&finptr);
  412.     Izclose(&workptr);
  413. }
  414.  
  415. #if RCS_lint
  416. #    define exiterr rdiffExit
  417. #endif
  418.     exiting void
  419. exiterr()
  420. {
  421.     tempunlink();
  422.     _exit(DIFF_TROUBLE);
  423. }
  424.  
  425. #if DIFF_L
  426.     static char const *
  427. setup_label(b, num, date)
  428.     struct buf *b;
  429.     char const *num;
  430.     char const date[datesize];
  431. {
  432.     char *p;
  433.     char datestr[datesize];
  434.     VOID date2str(date, datestr);
  435.     bufalloc(b, strlen(workfilename) + datesize+3 + (num?strlen(num)+1:0));
  436.     p = b->string;
  437.     VOID sprintf(p, "-L%s\t%s", workfilename, datestr);
  438.     if (num)
  439.         VOID sprintf(p + strlen(p), "\t%s", num);
  440.     return p;
  441. }
  442. #endif
  443.