home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 22 gnu / 22-gnu.zip / rcs567s.zip / rcs / src / rcsdiff.c < prev    next >
C/C++ Source or Header  |  1994-03-17  |  13KB  |  469 lines

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