home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / dev / misc / hwgrcs / src / rcs.rcsfiles / rcssyn.c,v < prev    next >
Encoding:
Text File  |  1995-06-25  |  20.3 KB  |  909 lines

  1. head    5.8;
  2. branch    5.8.1;
  3. access;
  4. symbols
  5.     HWGRCSP12F:5.8.1.1
  6.     HWGRCSP11F:5.8.1.1
  7.     HWGRCSP10F:5.8.1.1
  8.     HWGRCSP9:5.8.1.1
  9.     HWGRCSP8F:5.8.1.1
  10.     HWGRCSP7F:5.8.1.1
  11.     HWGRCSP6F:5.8.1.1
  12.     HWGRCSP5F:5.8.1.1
  13.     HWGRCSp4:5.8.1.1
  14.     HWGRCSp3:5.8.1
  15.     HWGRCS_Fish:5.8.1
  16.     HWGRCS:5.8.1;
  17. locks; strict;
  18. comment    @ * @;
  19.  
  20.  
  21. 5.8
  22. date    91.08.19.03.13.55;    author eggert;    state Exp;
  23. branches
  24.     5.8.1.1;
  25. next    ;
  26.  
  27. 5.8.1.1
  28. date    93.01.18.14.39.09;    author heinz;    state Exp;
  29. branches;
  30. next    ;
  31.  
  32.  
  33. desc
  34. @Checked in with -k 16.jan.93 HWG
  35. @
  36.  
  37.  
  38. 5.8
  39. log
  40. @checked in with -k by heinz at 1993/01/17 01:53:17
  41. @
  42. text
  43. @/*
  44.  *                     RCS file input
  45.  */
  46. /*********************************************************************************
  47.  *                       Syntax Analysis.
  48.  *                       Keyword table
  49.  *                       Testprogram: define SYNTEST
  50.  *                       Compatibility with Release 2: define COMPAT2=1
  51.  *********************************************************************************
  52.  */
  53.  
  54. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  55.    Copyright 1990, 1991 by Paul Eggert
  56.    Distributed under license by the Free Software Foundation, Inc.
  57.  
  58. This file is part of RCS.
  59.  
  60. RCS is free software; you can redistribute it and/or modify
  61. it under the terms of the GNU General Public License as published by
  62. the Free Software Foundation; either version 2, or (at your option)
  63. any later version.
  64.  
  65. RCS is distributed in the hope that it will be useful,
  66. but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  68. GNU General Public License for more details.
  69.  
  70. You should have received a copy of the GNU General Public License
  71. along with RCS; see the file COPYING.  If not, write to
  72. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  73.  
  74. Report problems and direct all questions to:
  75.  
  76.     rcs-bugs@@cs.purdue.edu
  77.  
  78. */
  79.  
  80.  
  81. /* $Log: rcssyn.c,v $
  82.  * Revision 5.8  1991/08/19  03:13:55  eggert
  83.  * Tune.
  84.  *
  85.  * Revision 5.7  1991/04/21  11:58:29  eggert
  86.  * Disambiguate names on shortname hosts.
  87.  * Fix errno bug.  Add MS-DOS support.
  88.  *
  89.  * Revision 5.6  1991/02/28  19:18:51  eggert
  90.  * Fix null termination bug in reporting keyword expansion.
  91.  *
  92.  * Revision 5.5  1991/02/25  07:12:44  eggert
  93.  * Check diff output more carefully; avoid overflow.
  94.  *
  95.  * Revision 5.4  1990/11/01  05:28:48  eggert
  96.  * When ignoring unknown phrases, copy them to the output RCS file.
  97.  * Permit arbitrary data in logs and comment leaders.
  98.  * Don't check for nontext on initial checkin.
  99.  *
  100.  * Revision 5.3  1990/09/20  07:58:32  eggert
  101.  * Remove the test for non-text bytes; it caused more pain than it cured.
  102.  *
  103.  * Revision 5.2  1990/09/04  08:02:30  eggert
  104.  * Parse RCS files with no revisions.
  105.  * Don't strip leading white space from diff commands.  Count RCS lines better.
  106.  *
  107.  * Revision 5.1  1990/08/29  07:14:06  eggert
  108.  * Add -kkvl.  Clean old log messages too.
  109.  *
  110.  * Revision 5.0  1990/08/22  08:13:44  eggert
  111.  * Try to parse future RCS formats without barfing.
  112.  * Add -k.  Don't require final newline.
  113.  * Remove compile-time limits; use malloc instead.
  114.  * Don't output branch keyword if there's no default branch,
  115.  * because RCS version 3 doesn't understand it.
  116.  * Tune.  Remove lint.
  117.  * Add support for ISO 8859.  Ansify and Posixate.
  118.  * Check that a newly checked-in file is acceptable as input to 'diff'.
  119.  * Check diff's output.
  120.  *
  121.  * Revision 4.6  89/05/01  15:13:32  narten
  122.  * changed copyright header to reflect current distribution rules
  123.  * 
  124.  * Revision 4.5  88/08/09  19:13:21  eggert
  125.  * Allow cc -R; remove lint.
  126.  * 
  127.  * Revision 4.4  87/12/18  11:46:16  narten
  128.  * more lint cleanups (Guy Harris)
  129.  * 
  130.  * Revision 4.3  87/10/18  10:39:36  narten
  131.  * Updating version numbers. Changes relative to 1.1 actually relative to
  132.  * 4.1
  133.  * 
  134.  * Revision 1.3  87/09/24  14:00:49  narten
  135.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf 
  136.  * warnings)
  137.  * 
  138.  * Revision 1.2  87/03/27  14:22:40  jenkins
  139.  * Port to suns
  140.  * 
  141.  * Revision 4.1  83/03/28  11:38:49  wft
  142.  * Added parsing and printing of default branch.
  143.  * 
  144.  * Revision 3.6  83/01/15  17:46:50  wft
  145.  * Changed readdelta() to initialize selector and log-pointer.
  146.  * Changed puttree to check for selector==DELETE; putdtext() uses DELNUMFORM.
  147.  *
  148.  * Revision 3.5  82/12/08  21:58:58  wft
  149.  * renamed Commentleader to Commleader.
  150.  *
  151.  * Revision 3.4  82/12/04  13:24:40  wft
  152.  * Added routine gettree(), which updates keeplock after reading the
  153.  * delta tree.
  154.  *
  155.  * Revision 3.3  82/11/28  21:30:11  wft
  156.  * Reading and printing of Suffix removed; version COMPAT2 skips the
  157.  * Suffix for files of release 2 format. Fixed problems with printing nil.
  158.  *
  159.  * Revision 3.2  82/10/18  21:18:25  wft
  160.  * renamed putdeltatext to putdtext.
  161.  *
  162.  * Revision 3.1  82/10/11  19:45:11  wft
  163.  * made sure getc() returns into an integer.
  164.  */
  165.  
  166.  
  167.  
  168. /* version COMPAT2 reads files of the format of release 2 and 3, but
  169.  * generates files of release 3 format. Need not be defined if no
  170.  * old RCS files generated with release 2 exist.
  171.  */
  172. /* version SYNTEST inputs a RCS file and then prints out its internal
  173.  * data structures.
  174. */
  175.  
  176. #include "rcsbase.h"
  177.  
  178. libId(synId, "$Id: rcssyn.c,v 5.8 1991/08/19 03:13:55 eggert Exp $")
  179.  
  180. /* forward */
  181. static char const *getkeyval P((char const*,enum tokens,int));
  182. static int strn2expmode P((char const*,size_t));
  183.  
  184. /* keyword table */
  185.  
  186. char const
  187.     Kdesc[]     = "desc",
  188.     Klog[]      = "log",
  189.     Ktext[]     = "text";
  190.  
  191. static char const
  192.     Kaccess[]   = "access",
  193.     Kauthor[]   = "author",
  194.     Kbranch[]   = "branch",
  195.     K_branches[]= "branches",
  196.     Kcomment[]  = "comment",
  197.     Kdate[]     = "date",
  198.     Kexpand[]   = "expand",
  199.     Khead[]     = "head",
  200.     Klocks[]    = "locks",
  201.     Knext[]     = "next",
  202.     Kstate[]    = "state",
  203.     Kstrict[]   = "strict",
  204. #if COMPAT2
  205.     Ksuffix[]   = "suffix",
  206. #endif
  207.     Ksymbols[]  = "symbols";
  208.  
  209. static struct buf Commleader;
  210. static struct cbuf Ignored;
  211. struct cbuf Comment;
  212. struct access   * AccessList;
  213. struct assoc    * Symbols;
  214. struct lock     * Locks;
  215. int          Expand;
  216. int               StrictLocks;
  217. struct hshentry * Head;
  218. char const      * Dbranch;
  219. unsigned TotalDeltas;
  220.  
  221.  
  222.     static void
  223. getsemi(key)
  224.     char const *key;
  225. /* Get a semicolon to finish off a phrase started by KEY.  */
  226. {
  227.     if (!getlex(SEMI))
  228.         fatserror("missing ';' after '%s'", key);
  229. }
  230.  
  231.     static struct hshentry *
  232. getdnum()
  233. /* Get a delta number.  */
  234. {
  235.     register struct hshentry *delta = getnum();
  236.     if (delta && countnumflds(delta->num)&1)
  237.         fatserror("%s isn't a delta number", delta->num);
  238.     return delta;
  239. }
  240.  
  241.  
  242.     void
  243. getadmin()
  244. /* Read an <admin> and initialize the appropriate global variables.  */
  245. {
  246.     register char const *id;
  247.         struct access   * newaccess;
  248.         struct assoc    * newassoc;
  249.         struct lock     * newlock;
  250.         struct hshentry * delta;
  251.     struct access **LastAccess;
  252.     struct assoc **LastSymbol;
  253.     struct lock **LastLock;
  254.     struct buf b;
  255.     struct cbuf cb;
  256.  
  257.         TotalDeltas=0;
  258.  
  259.     getkey(Khead);
  260.     Head = getdnum();
  261.     getsemi(Khead);
  262.  
  263.     Dbranch = nil;
  264.     if (getkeyopt(Kbranch)) {
  265.         if ((delta = getnum()))
  266.             Dbranch = delta->num;
  267.         getsemi(Kbranch);
  268.         }
  269.  
  270.  
  271. #if COMPAT2
  272.         /* read suffix. Only in release 2 format */
  273.     if (getkeyopt(Ksuffix)) {
  274.                 if (nexttok==STRING) {
  275.             readstring(); nextlex(); /* Throw away the suffix.  */
  276.         } else if (nexttok==ID) {
  277.                         nextlex();
  278.                 }
  279.         getsemi(Ksuffix);
  280.         }
  281. #endif
  282.  
  283.     getkey(Kaccess);
  284.     LastAccess = &AccessList;
  285.         while (id=getid()) {
  286.         newaccess = ftalloc(struct access);
  287.                 newaccess->login = id;
  288.         *LastAccess = newaccess;
  289.         LastAccess = &newaccess->nextaccess;
  290.         }
  291.     *LastAccess = nil;
  292.     getsemi(Kaccess);
  293.  
  294.     getkey(Ksymbols);
  295.     LastSymbol = &Symbols;
  296.         while (id = getid()) {
  297.                 if (!getlex(COLON))
  298.             fatserror("missing ':' in symbolic name definition");
  299.                 if (!(delta=getnum())) {
  300.             fatserror("missing number in symbolic name definition");
  301.                 } else { /*add new pair to association list*/
  302.             newassoc = ftalloc(struct assoc);
  303.                         newassoc->symbol=id;
  304.             newassoc->num = delta->num;
  305.             *LastSymbol = newassoc;
  306.             LastSymbol = &newassoc->nextassoc;
  307.                 }
  308.         }
  309.     *LastSymbol = nil;
  310.     getsemi(Ksymbols);
  311.  
  312.     getkey(Klocks);
  313.     LastLock = &Locks;
  314.         while (id = getid()) {
  315.                 if (!getlex(COLON))
  316.             fatserror("missing ':' in lock");
  317.         if (!(delta=getdnum())) {
  318.             fatserror("missing number in lock");
  319.                 } else { /*add new pair to lock list*/
  320.             newlock = ftalloc(struct lock);
  321.                         newlock->login=id;
  322.                         newlock->delta=delta;
  323.             *LastLock = newlock;
  324.             LastLock = &newlock->nextlock;
  325.                 }
  326.         }
  327.     *LastLock = nil;
  328.     getsemi(Klocks);
  329.  
  330.     if ((StrictLocks = getkeyopt(Kstrict)))
  331.         getsemi(Kstrict);
  332.  
  333.     Comment.size = 0;
  334.     if (getkeyopt(Kcomment)) {
  335.         if (nexttok==STRING) {
  336.             Comment = savestring(&Commleader);
  337.             nextlex();
  338.         }
  339.         getsemi(Kcomment);
  340.         }
  341.  
  342.     Expand = KEYVAL_EXPAND;
  343.     if (getkeyopt(Kexpand)) {
  344.         if (nexttok==STRING) {
  345.             bufautobegin(&b);
  346.             cb = savestring(&b);
  347.             if ((Expand = strn2expmode(cb.string,cb.size)) < 0)
  348.                 fatserror("unknown expand mode %.*s",
  349.                 (int)cb.size, cb.string
  350.                 );
  351.             bufautoend(&b);
  352.             nextlex();
  353.         }
  354.         getsemi(Kexpand);
  355.         }
  356.     Ignored = getphrases(Kdesc);
  357. }
  358.  
  359. char const *const expand_names[] = {
  360.     /* These must agree with *_EXPAND in rcsbase.h.  */
  361.     "kv","kvl","k","v","o",
  362.     0
  363. };
  364.  
  365.     int
  366. str2expmode(s)
  367.     char const *s;
  368. /* Yield expand mode corresponding to S, or -1 if bad.  */
  369. {
  370.     return strn2expmode(s, strlen(s));
  371. }
  372.  
  373.     static int
  374. strn2expmode(s, n)
  375.     char const *s;
  376.     size_t n;
  377. {
  378.     char const *const *p;
  379.  
  380.     for (p = expand_names;  *p;  ++p)
  381.         if (memcmp(*p,s,n) == 0  &&  !(*p)[n])
  382.             return p - expand_names;
  383.     return -1;
  384. }
  385.  
  386.  
  387.     void
  388. ignorephrase()
  389. /* Ignore a phrase introduced by a later version of RCS.  */
  390. {
  391.     warnignore();
  392.     hshenter=false;
  393.     for (;;) {
  394.         switch (nexttok) {
  395.         case SEMI: hshenter=true; nextlex(); return;
  396.         case ID:
  397.         case NUM: ffree1(NextString); break;
  398.         case STRING: readstring(); break;
  399.         default: break;
  400.         }
  401.         nextlex();
  402.     }
  403. }
  404.  
  405.  
  406.     static int
  407. getdelta()
  408. /* Function: reads a delta block.
  409.  * returns false if the current block does not start with a number.
  410.  */
  411. {
  412.         register struct hshentry * Delta, * num;
  413.     struct branchhead **LastBranch, *NewBranch;
  414.  
  415.     if (!(Delta = getdnum()))
  416.         return false;
  417.  
  418.         hshenter = false; /*Don't enter dates into hashtable*/
  419.         Delta->date = getkeyval(Kdate, NUM, false);
  420.         hshenter=true;    /*reset hshenter for revision numbers.*/
  421.  
  422.         Delta->author = getkeyval(Kauthor, ID, false);
  423.  
  424.         Delta->state = getkeyval(Kstate, ID, true);
  425.  
  426.     getkey(K_branches);
  427.     LastBranch = &Delta->branches;
  428.     while ((num = getdnum())) {
  429.         NewBranch = ftalloc(struct branchhead);
  430.                 NewBranch->hsh = num;
  431.         *LastBranch = NewBranch;
  432.         LastBranch = &NewBranch->nextbranch;
  433.         }
  434.     *LastBranch = nil;
  435.     getsemi(K_branches);
  436.  
  437.     getkey(Knext);
  438.     Delta->next = num = getdnum();
  439.     getsemi(Knext);
  440.     Delta->lockedby = nil;
  441.     Delta->log.string = 0;
  442.     Delta->selector = true;
  443.     Delta->ig = getphrases(Kdesc);
  444.         TotalDeltas++;
  445.         return (true);
  446. }
  447.  
  448.  
  449.     void
  450. gettree()
  451. /* Function: Reads in the delta tree with getdelta(), then
  452.  * updates the lockedby fields.
  453.  */
  454. {
  455.     struct lock const *currlock;
  456.  
  457.         while (getdelta());
  458.         currlock=Locks;
  459.         while (currlock) {
  460.                 currlock->delta->lockedby = currlock->login;
  461.                 currlock = currlock->nextlock;
  462.         }
  463. }
  464.  
  465.  
  466.     void
  467. getdesc(prdesc)
  468. int  prdesc;
  469. /* Function: read in descriptive text
  470.  * nexttok is not advanced afterwards.
  471.  * If prdesc is set, the text is printed to stdout.
  472.  */
  473. {
  474.  
  475.     getkeystring(Kdesc);
  476.         if (prdesc)
  477.                 printstring();  /*echo string*/
  478.         else    readstring();   /*skip string*/
  479. }
  480.  
  481.  
  482.  
  483.  
  484.  
  485.  
  486.     static char const *
  487. getkeyval(keyword, token, optional)
  488.     char const *keyword;
  489.     enum tokens token;
  490.     int optional;
  491. /* reads a pair of the form
  492.  * <keyword> <token> ;
  493.  * where token is one of <id> or <num>. optional indicates whether
  494.  * <token> is optional. A pointer to
  495.  * the actual character string of <id> or <num> is returned.
  496.  */
  497. {
  498.     register char const *val = nil;
  499.  
  500.     getkey(keyword);
  501.         if (nexttok==token) {
  502.                 val = NextString;
  503.                 nextlex();
  504.         } else {
  505.         if (!optional)
  506.             fatserror("missing %s", keyword);
  507.         }
  508.     getsemi(keyword);
  509.         return(val);
  510. }
  511.  
  512.  
  513.  
  514.  
  515.     void
  516. putadmin(fout)
  517. register FILE * fout;
  518. /* Function: Print the <admin> node read with getadmin() to file fout.
  519.  * Assumption: Variables AccessList, Symbols, Locks, StrictLocks,
  520.  * and Head have been set.
  521.  */
  522. {
  523.     struct assoc const *curassoc;
  524.     struct lock const *curlock;
  525.     struct access const *curaccess;
  526.  
  527.     aprintf(fout, "%s\t%s;\n", Khead, Head?Head->num:"");
  528.     if (Dbranch && VERSION(4)<=RCSversion)
  529.         aprintf(fout, "%s\t%s;\n", Kbranch, Dbranch);
  530.  
  531.     aputs(Kaccess, fout);
  532.         curaccess = AccessList;
  533.         while (curaccess) {
  534.            aprintf(fout, "\n\t%s", curaccess->login);
  535.                curaccess = curaccess->nextaccess;
  536.         }
  537.     aprintf(fout, ";\n%s", Ksymbols);
  538.         curassoc = Symbols;
  539.         while (curassoc) {
  540.            aprintf(fout, "\n\t%s:%s", curassoc->symbol, curassoc->num);
  541.                curassoc = curassoc->nextassoc;
  542.         }
  543.     aprintf(fout, ";\n%s", Klocks);
  544.         curlock = Locks;
  545.         while (curlock) {
  546.            aprintf(fout, "\n\t%s:%s", curlock->login, curlock->delta->num);
  547.                curlock = curlock->nextlock;
  548.         }
  549.     if (StrictLocks) aprintf(fout, "; %s", Kstrict);
  550.     aprintf(fout, ";\n");
  551.     if (Comment.size) {
  552.         aprintf(fout, "%s\t", Kcomment);
  553.         putstring(fout, true, Comment, false);
  554.         aprintf(fout, ";\n");
  555.         }
  556.     if (Expand != KEYVAL_EXPAND)
  557.         aprintf(fout, "%s\t%c%s%c;\n",
  558.             Kexpand, SDELIM, expand_names[Expand], SDELIM
  559.         );
  560.     awrite(Ignored.string, Ignored.size, fout);
  561.     aputc('\n', fout);
  562. }
  563.  
  564.  
  565.  
  566.  
  567.     static void
  568. putdelta(node,fout)
  569. register struct hshentry const *node;
  570. register FILE * fout;
  571. /* Function: prints a <delta> node to fout;
  572.  */
  573. {
  574.     struct branchhead const *nextbranch;
  575.  
  576.         if (node == nil) return;
  577.  
  578.     aprintf(fout, "\n%s\n%s\t%s;\t%s %s;\t%s %s;\nbranches",
  579.         node->num,
  580.         Kdate, node->date,
  581.         Kauthor, node->author,
  582.         Kstate, node->state?node->state:""
  583.     );
  584.         nextbranch = node->branches;
  585.         while (nextbranch) {
  586.            aprintf(fout, "\n\t%s", nextbranch->hsh->num);
  587.                nextbranch = nextbranch->nextbranch;
  588.         }
  589.  
  590.     aprintf(fout, ";\n%s\t%s;\n", Knext, node->next?node->next->num:"");
  591.     awrite(node->ig.string, node->ig.size, fout);
  592. }
  593.  
  594.  
  595.  
  596.  
  597.     void
  598. puttree(root,fout)
  599. struct hshentry const *root;
  600. register FILE * fout;
  601. /* Function: prints the delta tree in preorder to fout, starting with root.
  602.  */
  603. {
  604.     struct branchhead const *nextbranch;
  605.  
  606.         if (root==nil) return;
  607.  
  608.     if (root->selector)
  609.         putdelta(root,fout);
  610.  
  611.         puttree(root->next,fout);
  612.  
  613.         nextbranch = root->branches;
  614.         while (nextbranch) {
  615.              puttree(nextbranch->hsh,fout);
  616.              nextbranch = nextbranch->nextbranch;
  617.         }
  618. }
  619.  
  620.  
  621.     static exiting void
  622. unexpected_EOF()
  623. {
  624.     faterror("unexpected EOF in diff output");
  625. }
  626.  
  627. int putdtext(num,log,srcfilename,fout,diffmt)
  628.     char const *num, *srcfilename;
  629.     struct cbuf log;
  630.     FILE *fout;
  631.     int diffmt;
  632. /* Function: write a deltatext-node to fout.
  633.  * num points to the deltanumber, log to the logmessage, and
  634.  * sourcefile contains the text. Doubles up all SDELIMs in both the
  635.  * log and the text; Makes sure the log message ends in \n.
  636.  * returns false on error.
  637.  * If diffmt is true, also checks that text is valid diff -n output.
  638.  */
  639. {
  640.     RILE *fin;
  641.     int result;
  642.     if (!(fin = Iopen(srcfilename, "r", (struct stat*)0))) {
  643.         eerror(srcfilename);
  644.         return false;
  645.     }
  646.     result = putdftext(num,log,fin,fout,diffmt);
  647.     Ifclose(fin);
  648.     return result;
  649. }
  650.  
  651.     void
  652. putstring(out, delim, s, log)
  653.     register FILE *out;
  654.     struct cbuf s;
  655.     int delim, log;
  656. /*
  657.  * Output to OUT one SDELIM if DELIM, then the string S with SDELIMs doubled.
  658.  * If LOG is set then S is a log string; append a newline if S is nonempty.
  659.  */
  660. {
  661.     register char const *sp;
  662.     register size_t ss;
  663.  
  664.     if (delim)
  665.         aputc(SDELIM, out);
  666.     sp = s.string;
  667.     for (ss = s.size;  ss;  --ss) {
  668.         if (*sp == SDELIM)
  669.             aputc(SDELIM, out);
  670.         aputc(*sp++, out);
  671.     }
  672.     if (s.size && log)
  673.         aputc('\n', out);
  674.     aputc(SDELIM, out);
  675. }
  676.  
  677.     int
  678. putdftext(num,log,finfile,foutfile,diffmt)
  679.     char const *num;
  680.     struct cbuf log;
  681.     RILE *finfile;
  682.     FILE *foutfile;
  683.     int diffmt;
  684. /* like putdtext(), except the source file is already open */
  685. {
  686.     declarecache;
  687.     register FILE *fout;
  688.     register int c;
  689.     register RILE *fin;
  690.     int ed;
  691.     struct diffcmd dc;
  692.  
  693.     fout = foutfile;
  694.     aprintf(fout,DELNUMFORM,num,Klog);
  695.         /* put log */
  696.     putstring(fout, true, log, true);
  697.         /* put text */
  698.     aprintf(fout, "\n%s\n%c", Ktext, SDELIM);
  699.     fin = finfile;
  700.     setupcache(fin);
  701.     if (!diffmt) {
  702.         /* Copy the file */
  703.         cache(fin);
  704.         for (;;) {
  705.         cachegeteof(c, break;);
  706.         if (c==SDELIM) aputc(SDELIM,fout);   /*double up SDELIM*/
  707.         aputc(c,fout);
  708.         }
  709.     } else {
  710.         initdiffcmd(&dc);
  711.         while (0  <=  (ed = getdiffcmd(fin,false,fout,&dc)))
  712.         if (ed) {
  713.             cache(fin);
  714.             while (dc.nlines--)
  715.             do {
  716.                 cachegeteof(c, { if (!dc.nlines) goto OK_EOF; unexpected_EOF(); });
  717.                 if (c == SDELIM)
  718.                 aputc(SDELIM,fout);
  719.                 aputc(c,fout);
  720.             } while (c != '\n');
  721.             uncache(fin);
  722.         }
  723.     }
  724.     OK_EOF:
  725.     aprintf(fout, "%c\n", SDELIM);
  726.     return true;
  727. }
  728.  
  729.     void
  730. initdiffcmd(dc)
  731.     register struct diffcmd *dc;
  732. /* Initialize *dc suitably for getdiffcmd(). */
  733. {
  734.     dc->adprev = 0;
  735.     dc->dafter = 0;
  736. }
  737.  
  738.     static exiting void
  739. badDiffOutput(buf)
  740.     char const *buf;
  741. {
  742.     faterror("bad diff output line: %s", buf);
  743. }
  744.  
  745.     static exiting void
  746. diffLineNumberTooLarge(buf)
  747.     char const *buf;
  748. {
  749.     faterror("diff line number too large: %s", buf);
  750. }
  751.  
  752.     int
  753. getdiffcmd(finfile, delimiter, foutfile, dc)
  754.     RILE *finfile;
  755.     FILE *foutfile;
  756.     int delimiter;
  757.     struct diffcmd *dc;
  758. /* Get a editing command output by 'diff -n' from fin.
  759.  * The input is delimited by SDELIM if delimiter is set, EOF otherwise.
  760.  * Copy a clean version of the command to fout (if nonnull).
  761.  * Yield 0 for 'd', 1 for 'a', and -1 for EOF.
  762.  * Store the command's line number and length into dc->line1 and dc->nlines.
  763.  * Keep dc->adprev and dc->dafter up to date.
  764.  */
  765. {
  766.     register int c;
  767.     declarecache;
  768.     register FILE *fout;
  769.     register char *p;
  770.     register RILE *fin;
  771.     unsigned long line1, nlines, t;
  772.     char buf[BUFSIZ];
  773.  
  774.     fin = finfile;
  775.     fout = foutfile;
  776.     setupcache(fin); cache(fin);
  777.     cachegeteof(c, { if (delimiter) unexpected_EOF(); return -1; } );
  778.     if (delimiter) {
  779.         if (c==SDELIM) {
  780.             cacheget(c);
  781.             if (c==SDELIM) {
  782.                 buf[0] = c;
  783.                 buf[1] = 0;
  784.                 badDiffOutput(buf);
  785.             }
  786.             uncache(fin);
  787.             nextc = c;
  788.             if (fout)
  789.                 aprintf(fout, "%c%c", SDELIM, c);
  790.             return -1;
  791.         }
  792.     }
  793.     p = buf;
  794.     do {
  795.         if (buf+BUFSIZ-2 <= p) {
  796.             faterror("diff output command line too long");
  797.         }
  798.         *p++ = c;
  799.         cachegeteof(c, unexpected_EOF();) ;
  800.     } while (c != '\n');
  801.     uncache(fin);
  802.     if (delimiter)
  803.         ++rcsline;
  804.     *p = '\0';
  805.     for (p = buf+1;  (c = *p++) == ' ';  )
  806.         ;
  807.     line1 = 0;
  808.     while (isdigit(c)) {
  809.         t = line1 * 10;
  810.         if (
  811.             ULONG_MAX/10 < line1  ||
  812.             (line1 = t + (c - '0'))  <  t
  813.         )
  814.             diffLineNumberTooLarge(buf);
  815.         c = *p++;
  816.     }
  817.     while (c == ' ')
  818.         c = *p++;
  819.     nlines = 0;
  820.     while (isdigit(c)) {
  821.         t = nlines * 10;
  822.         if (
  823.             ULONG_MAX/10 < nlines  ||
  824.             (nlines = t + (c - '0'))  <  t
  825.         )
  826.             diffLineNumberTooLarge(buf);
  827.         c = *p++;
  828.     }
  829.     if (c || !nlines) {
  830.         badDiffOutput(buf);
  831.     }
  832.     if (line1+nlines < line1)
  833.         diffLineNumberTooLarge(buf);
  834.     switch (buf[0]) {
  835.         case 'a':
  836.         if (line1 < dc->adprev) {
  837.             faterror("backward insertion in diff output: %s", buf);
  838.         }
  839.         dc->adprev = line1 + 1;
  840.         break;
  841.         case 'd':
  842.         if (line1 < dc->adprev  ||  line1 < dc->dafter) {
  843.             faterror("backward deletion in diff output: %s", buf);
  844.         }
  845.         dc->adprev = line1;
  846.         dc->dafter = line1 + nlines;
  847.         break;
  848.         default:
  849.         badDiffOutput(buf);
  850.     }
  851.     if (fout) {
  852.         aprintf(fout, "%s\n", buf);
  853.     }
  854.     dc->line1 = line1;
  855.     dc->nlines = nlines;
  856.     return buf[0] == 'a';
  857. }
  858.  
  859.  
  860.  
  861. #ifdef SYNTEST
  862.  
  863. char const cmdid[] = "syntest";
  864.  
  865.     int
  866. main(argc,argv)
  867. int argc; char * argv[];
  868. {
  869.  
  870.         if (argc<2) {
  871.         aputs("No input file\n",stderr);
  872.         exitmain(EXIT_FAILURE);
  873.         }
  874.     if (!(finptr = Iopen(argv[1], FOPEN_R, (struct stat*)0))) {
  875.         faterror("can't open input file %s", argv[1]);
  876.         }
  877.         Lexinit();
  878.         getadmin();
  879.         putadmin(stdout);
  880.  
  881.         gettree();
  882.         puttree(Head,stdout);
  883.  
  884.         getdesc(true);
  885.  
  886.     nextlex();
  887.  
  888.     if (!eoflex()) {
  889.         fatserror("expecting EOF");
  890.         }
  891.     exitmain(EXIT_SUCCESS);
  892. }
  893.  
  894.  
  895. exiting void exiterr() { _exit(EXIT_FAILURE); }
  896.  
  897.  
  898. #endif
  899.  
  900. @
  901.  
  902.  
  903. 5.8.1.1
  904. log
  905. @Start of the AMIGA port of RCS 5.6. I call it HWGRCS now ;^)
  906. @
  907. text
  908. @@
  909.