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

  1. /*
  2.  *                     RCS revision generation
  3.  */
  4.  
  5. /* Copyright (C) 1982, 1988, 1989 Walter Tichy
  6.    Copyright 1990, 1991 by Paul Eggert
  7.    Distributed under license by the Free Software Foundation, Inc.
  8.  
  9. This file is part of RCS.
  10.  
  11. RCS is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2, or (at your option)
  14. any later version.
  15.  
  16. RCS is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with RCS; see the file COPYING.  If not, write to
  23. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. Report problems and direct all questions to:
  26.  
  27.     rcs-bugs@cs.purdue.edu
  28.  
  29. */
  30.  
  31.  
  32.  
  33. /* $Log: rcsgen.c,v $
  34.  * Revision 5.11  1992/01/24  18:44:19  eggert
  35.  * Move put routines here from rcssyn.c.
  36.  * Add support for bad_creat0.
  37.  *
  38.  * Revision 5.10  1991/10/07  17:32:46  eggert
  39.  * Fix log bugs, e.g. ci -t/dev/null when has_mmap.
  40.  *
  41.  * Revision 5.9  1991/09/10  22:15:46  eggert
  42.  * Fix test for redirected stdin.
  43.  *
  44.  * Revision 5.8  1991/08/19  03:13:55  eggert
  45.  * Add piece tables.  Tune.
  46.  *
  47.  * Revision 5.7  1991/04/21  11:58:24  eggert
  48.  * Add MS-DOS support.
  49.  *
  50.  * Revision 5.6  1990/12/27  19:54:26  eggert
  51.  * Fix bug: rcs -t inserted \n, making RCS file grow.
  52.  *
  53.  * Revision 5.5  1990/12/04  05:18:45  eggert
  54.  * Use -I for prompts and -q for diagnostics.
  55.  *
  56.  * Revision 5.4  1990/11/01  05:03:47  eggert
  57.  * Add -I and new -t behavior.  Permit arbitrary data in logs.
  58.  *
  59.  * Revision 5.3  1990/09/21  06:12:43  hammer
  60.  * made putdesc() treat stdin the same whether or not it was from a terminal
  61.  * by making it recognize that a single '.' was then end of the
  62.  * description always
  63.  *
  64.  * Revision 5.2  1990/09/04  08:02:25  eggert
  65.  * Fix `co -p1.1 -ko' bug.  Standardize yes-or-no procedure.
  66.  *
  67.  * Revision 5.1  1990/08/29  07:14:01  eggert
  68.  * Clean old log messages too.
  69.  *
  70.  * Revision 5.0  1990/08/22  08:12:52  eggert
  71.  * Remove compile-time limits; use malloc instead.
  72.  * Ansify and Posixate.
  73.  *
  74.  * Revision 4.7  89/05/01  15:12:49  narten
  75.  * changed copyright header to reflect current distribution rules
  76.  *
  77.  * Revision 4.6  88/08/28  14:59:10  eggert
  78.  * Shrink stdio code size; allow cc -R; remove lint; isatty() -> ttystdin()
  79.  *
  80.  * Revision 4.5  87/12/18  11:43:25  narten
  81.  * additional lint cleanups, and a bug fix from the 4.3BSD version that
  82.  * keeps "ci" from sticking a '\377' into the description if you run it
  83.  * with a zero-length file as the description. (Guy Harris)
  84.  *
  85.  * Revision 4.4  87/10/18  10:35:10  narten
  86.  * Updating version numbers. Changes relative to 1.1 actually relative to
  87.  * 4.2
  88.  *
  89.  * Revision 1.3  87/09/24  13:59:51  narten
  90.  * Sources now pass through lint (if you ignore printf/sprintf/fprintf
  91.  * warnings)
  92.  *
  93.  * Revision 1.2  87/03/27  14:22:27  jenkins
  94.  * Port to suns
  95.  *
  96.  * Revision 4.2  83/12/02  23:01:39  wft
  97.  * merged 4.1 and 3.3.1.1 (clearerr(stdin)).
  98.  *
  99.  * Revision 4.1  83/05/10  16:03:33  wft
  100.  * Changed putamin() to abort if trying to reread redirected stdin.
  101.  * Fixed getdesc() to output a prompt on initial newline.
  102.  *
  103.  * Revision 3.3.1.1  83/10/19  04:21:51  lepreau
  104.  * Added clearerr(stdin) for re-reading description from stdin.
  105.  *
  106.  * Revision 3.3  82/11/28  21:36:49  wft
  107.  * 4.2 prerelease
  108.  *
  109.  * Revision 3.3  82/11/28  21:36:49  wft
  110.  * Replaced ferror() followed by fclose() with ffclose().
  111.  * Putdesc() now suppresses the prompts if stdin
  112.  * is not a terminal. A pointer to the current log message is now
  113.  * inserted into the corresponding delta, rather than leaving it in a
  114.  * global variable.
  115.  *
  116.  * Revision 3.2  82/10/18  21:11:26  wft
  117.  * I added checks for write errors during editing, and improved
  118.  * the prompt on putdesc().
  119.  *
  120.  * Revision 3.1  82/10/13  15:55:09  wft
  121.  * corrected type of variables assigned to by getc (char --> int)
  122.  */
  123.  
  124.  
  125.  
  126.  
  127. #include "rcsbase.h"
  128.  
  129. libId(genId, "$Id: rcsgen.c,v 5.11 1992/01/24 18:44:19 eggert Exp $")
  130.  
  131. int interactiveflag;  /* Should we act as if stdin is a tty?  */
  132. struct buf curlogbuf;  /* buffer for current log message */
  133.  
  134. enum stringwork { enter, copy, edit, expand, edit_expand };
  135. static void scandeltatext P((struct hshentry*,enum stringwork,int));
  136.  
  137.  
  138.  
  139.  
  140.     char const *
  141. buildrevision(deltas, target, outfile, expandflag)
  142.     struct hshentries const *deltas;
  143.     struct hshentry *target;
  144.     FILE *outfile;
  145.     int expandflag;
  146. /* Function: Generates the revision given by target
  147.  * by retrieving all deltas given by parameter deltas and combining them.
  148.  * If outfile is set, the revision is output to it,
  149.  * otherwise written into a temporary file.
  150.  * Temporary files are allocated by maketemp().
  151.  * if expandflag is set, keyword expansion is performed.
  152.  * Return nil if outfile is set, the name of the temporary file otherwise.
  153.  *
  154.  * Algorithm: Copy initial revision unchanged.  Then edit all revisions but
  155.  * the last one into it, alternating input and output files (resultfile and
  156.  * editfile). The last revision is then edited in, performing simultaneous
  157.  * keyword substitution (this saves one extra pass).
  158.  * All this simplifies if only one revision needs to be generated,
  159.  * or no keyword expansion is necessary, or if output goes to stdout.
  160.  */
  161. {
  162.     if (deltas->first == target) {
  163.                 /* only latest revision to generate */
  164.         openfcopy(outfile);
  165.         scandeltatext(target, expandflag?expand:copy, true);
  166.         if (outfile)
  167.             return 0;
  168.         else {
  169.             Ozclose(&fcopy);
  170.                         return(resultfile);
  171.         }
  172.         } else {
  173.                 /* several revisions to generate */
  174.         /* Get initial revision without keyword expansion.  */
  175.         scandeltatext(deltas->first, enter, false);
  176.         while ((deltas=deltas->rest)->rest) {
  177.                         /* do all deltas except last one */
  178.             scandeltatext(deltas->first, edit, false);
  179.                 }
  180.         if (expandflag || outfile) {
  181.                         /* first, get to beginning of file*/
  182.             finishedit((struct hshentry *)nil, outfile, false);
  183.                 }
  184.         scandeltatext(deltas->first, expandflag?edit_expand:edit, true);
  185.         finishedit(
  186.             expandflag ? deltas->first : (struct hshentry*)nil,
  187.             outfile, true
  188.         );
  189.         if (outfile)
  190.             return 0;
  191.         Ozclose(&fcopy);
  192.         return resultfile;
  193.         }
  194. }
  195.  
  196.  
  197.  
  198.     static void
  199. scandeltatext(delta, func, needlog)
  200.     struct hshentry * delta;
  201.     enum stringwork func;
  202.     int needlog;
  203. /* Function: Scans delta text nodes up to and including the one given
  204.  * by delta. For the one given by delta, the log message is saved into
  205.  * delta->log if needlog is set; func specifies how to handle the text.
  206.  * Assumes the initial lexeme must be read in first.
  207.  * Does not advance nexttok after it is finished.
  208.  */
  209. {
  210.     struct hshentry const *nextdelta;
  211.     struct cbuf cb;
  212.  
  213.         for (;;) {
  214.         if (eoflex())
  215.             fatserror("can't find delta for revision %s", delta->num);
  216.                 nextlex();
  217.                 if (!(nextdelta=getnum())) {
  218.             fatserror("delta number corrupted");
  219.                 }
  220.         getkeystring(Klog);
  221.         if (needlog && delta==nextdelta) {
  222.             cb = savestring(&curlogbuf);
  223.             delta->log = cleanlogmsg(curlogbuf.string, cb.size);
  224.                 } else {readstring();
  225.                 }
  226.                 nextlex();
  227.         while (nexttok==ID && strcmp(NextString,Ktext)!=0)
  228.             ignorephrase();
  229.         getkeystring(Ktext);
  230.  
  231.         if (delta==nextdelta)
  232.             break;
  233.         readstring(); /* skip over it */
  234.  
  235.     }
  236.     switch (func) {
  237.         case enter: enterstring(); break;
  238.         case copy: copystring(); break;
  239.         case expand: xpandstring(delta); break;
  240.         case edit: editstring((struct hshentry *)nil); break;
  241.         case edit_expand: editstring(delta); break;
  242.     }
  243. }
  244.  
  245.     struct cbuf
  246. cleanlogmsg(m, s)
  247.     char *m;
  248.     size_t s;
  249. {
  250.     register char *t = m;
  251.     register char const *f = t;
  252.     struct cbuf r;
  253.     while (s) {
  254.         --s;
  255.         if ((*t++ = *f++) == '\n')
  256.         while (m < --t)
  257.             if (t[-1]!=' ' && t[-1]!='\t') {
  258.             *t++ = '\n';
  259.             break;
  260.             }
  261.     }
  262.     while (m < t  &&  (t[-1]==' ' || t[-1]=='\t' || t[-1]=='\n'))
  263.         --t;
  264.     r.string = m;
  265.     r.size = t - m;
  266.     return r;
  267. }
  268.  
  269.  
  270. int ttystdin()
  271. {
  272.     static int initialized;
  273.     if (!initialized) {
  274.         if (!interactiveflag)
  275.             interactiveflag = isatty(STDIN_FILENO);
  276.         initialized = true;
  277.     }
  278.     return interactiveflag;
  279. }
  280.  
  281.     int
  282. getcstdin()
  283. {
  284.     register FILE *in;
  285.     register int c;
  286.  
  287.     in = stdin;
  288.     if (feof(in) && ttystdin())
  289.         clearerr(in);
  290.     c = getc(in);
  291.     if (c < 0) {
  292.         testIerror(in);
  293.         if (feof(in) && ttystdin())
  294.             afputc('\n',stderr);
  295.     }
  296.     return c;
  297. }
  298.  
  299. #if has_prototypes
  300.     int
  301. yesorno(int default_answer, char const *question, ...)
  302. #else
  303.         /*VARARGS2*/ int
  304.     yesorno(default_answer, question, va_alist)
  305.         int default_answer; char const *question; va_dcl
  306. #endif
  307. {
  308.     va_list args;
  309.     register int c, r;
  310.     if (!quietflag && ttystdin()) {
  311.         oflush();
  312.         vararg_start(args, question);
  313.         fvfprintf(stderr, question, args);
  314.         va_end(args);
  315.         eflush();
  316.         r = c = getcstdin();
  317.         while (c!='\n' && !feof(stdin))
  318.             c = getcstdin();
  319.         if (r=='y' || r=='Y')
  320.             return true;
  321.         if (r=='n' || r=='N')
  322.             return false;
  323.     }
  324.     return default_answer;
  325. }
  326.  
  327.  
  328.     void
  329. putdesc(textflag, textfile)
  330.     int textflag;
  331.     char *textfile;
  332. /* Function: puts the descriptive text into file frewrite.
  333.  * if finptr && !textflag, the text is copied from the old description.
  334.  * Otherwise, if the textfile!=nil, the text is read from that
  335.  * file, or from stdin, if textfile==nil.
  336.  * A textfile with a leading '-' is treated as a string, not a file name.
  337.  * If finptr, the old descriptive text is discarded.
  338.  * Always clears foutptr.
  339.  */
  340. {
  341.     static struct buf desc;
  342.     static struct cbuf desclean;
  343.  
  344.     register FILE *txt;
  345.     register int c;
  346.     register FILE * frew;
  347.     register char *p;
  348.     register size_t s;
  349.     char const *plim;
  350.  
  351.     frew = frewrite;
  352.     if (finptr && !textflag) {
  353.                 /* copy old description */
  354.         aprintf(frew, "\n\n%s%c", Kdesc, nextc);
  355.         foutptr = frewrite;
  356.         getdesc(false);
  357.         foutptr = 0;
  358.         } else {
  359.         foutptr = 0;
  360.                 /* get new description */
  361.         if (finptr) {
  362.                         /*skip old description*/
  363.             getdesc(false);
  364.                 }
  365.         aprintf(frew,"\n\n%s\n%c",Kdesc,SDELIM);
  366.         if (!textfile)
  367.             desclean = getsstdin(
  368.                 "t-", "description",
  369.                 "NOTE: This is NOT the log message!\n", &desc
  370.             );
  371.         else if (!desclean.string) {
  372.             if (*textfile == '-') {
  373.                 p = textfile + 1;
  374.                 s = strlen(p);
  375.             } else {
  376.                 if (!(txt = fopen(textfile, "r")))
  377.                     efaterror(textfile);
  378.                 bufalloc(&desc, 1);
  379.                 p = desc.string;
  380.                 plim = p + desc.size;
  381.                 for (;;) {
  382.                     if ((c=getc(txt)) < 0) {
  383.                         testIerror(txt);
  384.                         if (feof(txt))
  385.                             break;
  386.                     }
  387.                     if (plim <= p)
  388.                         p = bufenlarge(&desc, &plim);
  389.                     *p++ = c;
  390.                 }
  391.                 if (fclose(txt) != 0)
  392.                     Ierror();
  393.                 s = p - desc.string;
  394.                 p = desc.string;
  395.             }
  396.             desclean = cleanlogmsg(p, s);
  397.         }
  398.         putstring(frew, false, desclean, true);
  399.         aputc('\n', frew);
  400.         }
  401. }
  402.  
  403.     struct cbuf
  404. getsstdin(option, name, note, buf)
  405.     char const *option, *name, *note;
  406.     struct buf *buf;
  407. {
  408.     register int c;
  409.     register char *p;
  410.     register size_t i;
  411.     register int tty = ttystdin();
  412.  
  413.     if (tty)
  414.         aprintf(stderr,
  415.         "enter %s, terminated with single '.' or end of file:\n%s>> ",
  416.         name, note
  417.         );
  418.     else if (feof(stdin))
  419.         faterror("can't reread redirected stdin for %s; use -%s<%s>",
  420.         name, option, name
  421.         );
  422.  
  423.     for (
  424.        i = 0,  p = 0;
  425.        c = getcstdin(),  !feof(stdin);
  426.        bufrealloc(buf, i+1),  p = buf->string,  p[i++] = c
  427.     )
  428.         if (c == '\n')
  429.             if (i  &&  p[i-1]=='.'  &&  (i==1 || p[i-2]=='\n')) {
  430.                 /* Remove trailing '.'.  */
  431.                 --i;
  432.                 break;
  433.             } else if (tty)
  434.                 aputs(">> ", stderr);
  435.     return cleanlogmsg(p, i);
  436. }
  437.  
  438.  
  439.     void
  440. putadmin()
  441. /* Output the admin node.  */
  442. {
  443.     register FILE *fout;
  444.     struct assoc const *curassoc;
  445.     struct lock const *curlock;
  446.     struct access const *curaccess;
  447.  
  448.     if (!(fout = frewrite)) {
  449. #        if bad_creat0
  450.             ORCSclose();
  451.             fout = fopen(makedirtemp(0), FOPEN_W);
  452. #        else
  453.             int fo = fdlock;
  454.             fdlock = -1;
  455.             fout = fdopen(fo, FOPEN_W);
  456. #        endif
  457.  
  458.         if (!(frewrite = fout))
  459.             efaterror(RCSfilename);
  460.     }
  461.  
  462.     aprintf(fout, "%s\t%s;\n", Khead, Head?Head->num:"");
  463.     if (Dbranch && VERSION(4)<=RCSversion)
  464.         aprintf(fout, "%s\t%s;\n", Kbranch, Dbranch);
  465.  
  466.     aputs(Kaccess, fout);
  467.     curaccess = AccessList;
  468.     while (curaccess) {
  469.            aprintf(fout, "\n\t%s", curaccess->login);
  470.            curaccess = curaccess->nextaccess;
  471.     }
  472.     aprintf(fout, ";\n%s", Ksymbols);
  473.     curassoc = Symbols;
  474.     while (curassoc) {
  475.            aprintf(fout, "\n\t%s:%s", curassoc->symbol, curassoc->num);
  476.            curassoc = curassoc->nextassoc;
  477.     }
  478.     aprintf(fout, ";\n%s", Klocks);
  479.     curlock = Locks;
  480.     while (curlock) {
  481.            aprintf(fout, "\n\t%s:%s", curlock->login, curlock->delta->num);
  482.            curlock = curlock->nextlock;
  483.     }
  484.     if (StrictLocks) aprintf(fout, "; %s", Kstrict);
  485.     aprintf(fout, ";\n");
  486.     if (Comment.size) {
  487.         aprintf(fout, "%s\t", Kcomment);
  488.         putstring(fout, true, Comment, false);
  489.         aprintf(fout, ";\n");
  490.     }
  491.     if (Expand != KEYVAL_EXPAND)
  492.         aprintf(fout, "%s\t%c%s%c;\n",
  493.             Kexpand, SDELIM, expand_names[Expand], SDELIM
  494.         );
  495.     awrite(Ignored.string, Ignored.size, fout);
  496.     aputc('\n', fout);
  497. }
  498.  
  499.  
  500.     static void
  501. putdelta(node, fout)
  502.     register struct hshentry const *node;
  503.     register FILE * fout;
  504. /* Output the delta NODE to FOUT.  */
  505. {
  506.     struct branchhead const *nextbranch;
  507.  
  508.     if (!node) return;
  509.  
  510.     aprintf(fout, "\n%s\n%s\t%s;\t%s %s;\t%s %s;\nbranches",
  511.         node->num,
  512.         Kdate, node->date,
  513.         Kauthor, node->author,
  514.         Kstate, node->state?node->state:""
  515.     );
  516.     nextbranch = node->branches;
  517.     while (nextbranch) {
  518.            aprintf(fout, "\n\t%s", nextbranch->hsh->num);
  519.            nextbranch = nextbranch->nextbranch;
  520.     }
  521.  
  522.     aprintf(fout, ";\n%s\t%s;\n", Knext, node->next?node->next->num:"");
  523.     awrite(node->ig.string, node->ig.size, fout);
  524. }
  525.  
  526.  
  527.     void
  528. puttree(root, fout)
  529.     struct hshentry const *root;
  530.     register FILE *fout;
  531. /* Output the delta tree with base ROOT in preorder to FOUT.  */
  532. {
  533.     struct branchhead const *nextbranch;
  534.  
  535.     if (!root) return;
  536.  
  537.     if (root->selector)
  538.         putdelta(root, fout);
  539.  
  540.     puttree(root->next, fout);
  541.  
  542.     nextbranch = root->branches;
  543.     while (nextbranch) {
  544.          puttree(nextbranch->hsh, fout);
  545.          nextbranch = nextbranch->nextbranch;
  546.     }
  547. }
  548.  
  549.  
  550.     int
  551. putdtext(num, log, srcfilename, fout, diffmt)
  552.     char const *num, *srcfilename;
  553.     struct cbuf log;
  554.     FILE *fout;
  555.     int diffmt;
  556. /*
  557.  * Output a deltatext node with delta number NUM, log message LOG and
  558.  * text SRCFILENAME to FOUT.
  559.  * Double up all SDELIMs in both the log and the text.
  560.  * Make sure the log message ends in \n.
  561.  * Return false on error.
  562.  * If DIFFMT, also check that the text is valid diff -n output.
  563.  */
  564. {
  565.     RILE *fin;
  566.     if (!(fin = Iopen(srcfilename, "r", (struct stat*)0))) {
  567.         eerror(srcfilename);
  568.         return false;
  569.     }
  570.     putdftext(num, log, fin, fout, diffmt);
  571.     Ifclose(fin);
  572.     return true;
  573. }
  574.  
  575.     void
  576. putstring(out, delim, s, log)
  577.     register FILE *out;
  578.     struct cbuf s;
  579.     int delim, log;
  580. /*
  581.  * Output to OUT one SDELIM if DELIM, then the string S with SDELIMs doubled.
  582.  * If LOG is set then S is a log string; append a newline if S is nonempty.
  583.  */
  584. {
  585.     register char const *sp;
  586.     register size_t ss;
  587.  
  588.     if (delim)
  589.         aputc(SDELIM, out);
  590.     sp = s.string;
  591.     for (ss = s.size;  ss;  --ss) {
  592.         if (*sp == SDELIM)
  593.             aputc(SDELIM, out);
  594.         aputc(*sp++, out);
  595.     }
  596.     if (s.size && log)
  597.         aputc('\n', out);
  598.     aputc(SDELIM, out);
  599. }
  600.  
  601.     void
  602. putdftext(num, log, finfile, foutfile, diffmt)
  603.     char const *num;
  604.     struct cbuf log;
  605.     RILE *finfile;
  606.     FILE *foutfile;
  607.     int diffmt;
  608. /* like putdtext(), except the source file is already open */
  609. {
  610.     declarecache;
  611.     register FILE *fout;
  612.     register int c;
  613.     register RILE *fin;
  614.     int ed;
  615.     struct diffcmd dc;
  616.  
  617.     fout = foutfile;
  618.     aprintf(fout, DELNUMFORM, num, Klog);
  619.  
  620.     /* put log */
  621.     putstring(fout, true, log, true);
  622.  
  623.     /* put text */
  624.     aprintf(fout, "\n%s\n%c", Ktext, SDELIM);
  625.  
  626.     fin = finfile;
  627.     setupcache(fin);
  628.     if (!diffmt) {
  629.         /* Copy the file */
  630.         cache(fin);
  631.         for (;;) {
  632.         cachegeteof(c, break;);
  633.         if (c==SDELIM) aputc(SDELIM, fout);   /*double up SDELIM*/
  634.         aputc(c, fout);
  635.         }
  636.     } else {
  637.         initdiffcmd(&dc);
  638.         while (0  <=  (ed = getdiffcmd(fin, false, fout, &dc)))
  639.         if (ed) {
  640.             cache(fin);
  641.             while (dc.nlines--)
  642.             do {
  643.                 cachegeteof(c, { if (!dc.nlines) goto OK_EOF; unexpected_EOF(); });
  644.                 if (c == SDELIM)
  645.                 aputc(SDELIM, fout);
  646.                 aputc(c, fout);
  647.             } while (c != '\n');
  648.             uncache(fin);
  649.         }
  650.     }
  651.     OK_EOF:
  652.     aprintf(fout, "%c\n", SDELIM);
  653. }
  654.