home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OS9000 / APPS / rcs.lzh / rcs1 / puttot.c < prev    next >
C/C++ Source or Header  |  1996-04-22  |  8KB  |  302 lines

  1. static char RCSid[]="$Header: puttot.c_v 1.4 96/04/23 02:01:48 hiro Exp $";
  2. /********************************************************************
  3. Copyright (c) 1993 ARK Systems USA
  4. All rights reserved.
  5.  
  6. Program: puttot - put symbolic label at top of RCS trunk
  7.    File: puttot.c
  8.  
  9. $Date: 96/04/23 02:01:48 $
  10. $Revision: 1.4 $
  11.  
  12. $Log:    puttot.c_v $
  13.  * Revision 1.4  96/04/23  02:01:48  hiro
  14.  * Excluded inclusion of <stdlib.h> for OS-9.
  15.  * 
  16.  * Revision 1.3  96/04/21  01:21:44  hiro
  17.  * Ported to OS-9000.
  18.  * 
  19.  * Revision 1.2  93/04/04  01:46:41  hiro
  20.  * Prototype
  21.  * ================================================================
  22.  * Made work!
  23.  * ================================================================
  24.  * 
  25. ********************************************************************/
  26. #ifdef _UCC
  27. _asm("_sysedit: equ 1");
  28. #else
  29. @_sysedit: equ 1
  30. #endif
  31.  
  32. #include <stdio.h>
  33. #if defined(__STDC__) || defined(_OS9000)
  34. #include <stdlib.h>
  35. #else
  36. extern void *malloc();
  37. #endif
  38. #include <strings.h>
  39. #include <time.h>
  40. #include <errno.h>
  41. #include <ctype.h>
  42.  
  43. #include <module.h>
  44. #ifdef _OS9000
  45. #include <io.h>
  46. #else
  47. #include <sysio.h>
  48. #endif
  49. #include <modes.h>
  50. #include <rbf.h>
  51.  
  52. #ifdef _OS9000
  53. #define environ        _environ
  54. #define mod_exec    mh_com
  55. extern  int _os_fork();
  56. #else
  57. extern   int  os9forkc();
  58. #endif
  59.  
  60. FILE *namefile;
  61. char **files;
  62. char *nextfile();
  63. extern   char *_prgname();
  64. extern   mod_exec *modlink(), *modloadp();
  65.  
  66. extern   char *environ;
  67.  
  68. #define  RCPROG   "rcs"
  69. #define  RLPROG   "rlog"
  70. #define  REVSTRLEN 20
  71.  
  72. char *rcsopt, *revstr, *nopt;
  73. mod_exec *rcmp, *rlmp;
  74. int  stop;
  75.  
  76. void cleanup(err, cstr, a)
  77.      int  err;
  78.      char *cstr, *a;
  79. {
  80.      if (rcmp)
  81.           munlink(rcmp);
  82.      if (rlmp)
  83.           munlink(rlmp);
  84.      if (cstr)
  85.           exit(_errmsg(err, cstr, a));
  86.      else exit(err);
  87. }
  88.  
  89. /*---------------------------------------------------------
  90. ! fork and wait a program
  91. */
  92. int  runprog(args, waitit)
  93.      char *args[];
  94. {
  95. int  status;
  96. #ifdef _OS9000
  97. process_id pid;
  98.  
  99.      if (errno = os9kexec(_os_fork, 0, 3, *args, args, environ,
  100.                              0, &pid, mktypelang(MT_PROGRAM, ML_OBJECT), 0))
  101. #else
  102. int pid;
  103.  
  104.      if ((pid = os9exec(os9forkc, *args, args, environ, 0, 0, 3)) == -1)
  105. #endif
  106.           cleanup(errno, "can't fork \"%s\". ", *args);
  107.      if (waitit)
  108.      {
  109.           while (wait(&status) != pid)
  110.                ;
  111.           return status;
  112.      }
  113.      else return 0;
  114. }
  115.  
  116. /*---------------------------------------------------------
  117. ! process each file
  118. */
  119. void process(file)
  120. register char *file;
  121. {
  122. static char *rlargs[] = {RLPROG, "-b%h", NULL, NULL};
  123. static char *rcargs[] = {RCPROG, NULL, NULL, NULL};
  124. register char *p;
  125. int  savepn, n, skipthis = 0;
  126. unsigned int status;
  127.  
  128.      rlargs[2] = rcargs[2] = file;
  129.      rcargs[1] = rcsopt;
  130.  
  131.   /* redirect STDOUT to pipe */
  132.      savepn = dup(1);
  133.      close(1);
  134.      errno = 0;
  135.      if (creat("/pipe", S_IREAD+S_IWRITE) != 1)
  136.           cleanup(errno, "can't redirect STDIN to pipe. ", NULL);
  137.   /* get top-of-trunk revision number */
  138.      runprog(rlargs, 0);
  139.      if ((n = readln(1, revstr, REVSTRLEN)) <= 0)
  140.           skipthis = _errmsg(1, "can't read %s's output; file \"%s\" skipped.\n");
  141.      wait(&status);
  142.      if (status)
  143.           skipthis = _errmsg(1, "%s exited with error; file \"%s\" skipped.\n");
  144.   /* restore redirected STDOUT */
  145.      close(1);
  146.      dup(savepn);
  147.      close(savepn);
  148.      if (!skipthis)
  149.      {
  150.           revstr[n] = '\0';
  151.           if (revstr[--n] == '\n')
  152.                revstr[n] = '\0';
  153.           fprintf(stderr, "%s: \"%s\" assigned to revision %s.\n", file, nopt+2, revstr);
  154.           runprog(rcargs, 1);
  155.      }
  156.      if (stop)
  157.           cleanup(stop, "Stopped by signal.\n", NULL);
  158. }
  159.  
  160. mod_exec *mlinkload(mod)
  161. char *mod;
  162. {
  163. mod_exec *mp;
  164.  
  165.      if ((int)(mp = modlink(mod, mktypelang(MT_PROGRAM, ML_OBJECT))) == -1 &&
  166.          (int)(mp = modloadp(mod, S_IEXEC, NULL)) == -1 && 
  167.          (int)(mp = modloadp(mod, S_IREAD, NULL)) == -1)
  168.           cleanup(errno, "can't find \"%s\". ", mod);
  169. }
  170.  
  171. void intcpt(sig)
  172. {
  173.      stop = sig;
  174. }
  175.  
  176. main(argc, argv)
  177. int  argc;
  178. char *argv[];
  179. {
  180. register char *file;
  181. register char *p, **fpp;
  182. register int  ch, waseq = 0;
  183. long tfsize;
  184.  
  185.      files = fpp = argv;
  186.      while (--argc)
  187.      {
  188.           if (*(p = *++argv) == '-')
  189.           {
  190.                ++p;
  191.                while (ch = *p++)
  192.                {
  193.                     waseq = *p == '=';
  194.                     if (waseq)
  195.                          ++p;
  196.                     switch (ch)
  197.                     {
  198.                     case 'n':
  199.                     case 'N':
  200.                          if (nopt)
  201.                              exit(_errmsg(1, "only one symbolica label can be specified.\n"));
  202.                          else if (waseq)
  203.                              exit(_errmsg(1, "can't have '=' in -n/N options.\n"));
  204.                          else if (!isalpha(*p))
  205.                              exit(_errmsg(1, "illegal symbolic label \"%s\".\n", p));
  206.                          nopt = p - 2;
  207.                          break;
  208.                     case 'z':
  209.                          if (namefile)
  210.                               exit(_errmsg(
  211.                                     1,"only one file of names allowed.\n"));
  212.                          if (waseq)
  213.                          {
  214.                               if ((namefile = fopen(p,"r")) == NULL)
  215.                                    exit(_errmsg(errno,
  216.                                                 "can't open \"%s\". ",p));
  217.                          }
  218.                          else
  219.                          {
  220.                               namefile = stdin;
  221.                               continue;
  222.                          }
  223.                          break;
  224.                     default:
  225.                          prtuse(_errmsg(1,"unknown option '%c'.\n\n",ch));
  226.                     case '?':
  227.                          prtuse(0);
  228.                     }
  229.                     break;
  230.                }
  231.           }
  232.           else *fpp++ = p;
  233.      }
  234.      *fpp = NULL;
  235.  
  236.      if (fpp == files && namefile == NULL)
  237.           exit(_errmsg(1, "no files.\n"));
  238.      else if (!nopt)
  239.           exit(_errmsg(1, "no symbolic label.\n"));
  240.      else if (!(rcsopt = malloc(strlen(nopt) + REVSTRLEN + 5)))
  241.           exit(_errmsg(errno, "can't allocate string buffer. "));
  242.      sprintf(rcsopt, "%s:", nopt);
  243.      revstr = rcsopt + strlen(rcsopt);
  244.  
  245.      rcmp = mlinkload(RCPROG);
  246.      rlmp = mlinkload(RLPROG);
  247.  
  248.      while (file = nextfile())
  249.           process(file);
  250.      cleanup(0, NULL, NULL);
  251. }
  252.  
  253.  
  254. /**--------------------------------------------------------------*/
  255. char *nextfile()
  256. {
  257. register char *file = NULL;
  258. static char nambuf[120];
  259.  
  260.      if (file = *files)
  261.           ++files;
  262.      else if (namefile)
  263.      {
  264.           if ((file = fgets(nambuf,sizeof nambuf,namefile)) == NULL)
  265.           {
  266.                if (ferror(namefile))
  267.                     exit(_errmsg(errno,"can't read name file.\n"));
  268.                fclose(namefile);
  269.                namefile = NULL;
  270.           }
  271.           else *(file + strlen(file) - 1) = '\0';
  272.      }
  273.  
  274.      return file;
  275. }
  276.  
  277. /**--------------------------------------------------------------
  278. !    cmds[] - array of pointers to strings of help text
  279. !*/
  280. static char *cmds[] = {
  281.        "Function: put symbolic lable at top of RCS trunk\n",
  282.        "Options:\n",
  283.        "    -n=<lable>    specify symbolic label\n",
  284.        "    -N=<lable>    specify symbolic label (overwrite current)\n",
  285.        "    -z[=<file>]   read list of files from standard input or <file>\n"
  286. };
  287.  
  288. /**--------------------------------------------------------------
  289. !    printuse - print the help text to standard error.
  290. !*/
  291. static prtuse(stat)
  292.      int stat;
  293. {
  294. register char **p = cmds,
  295.               **e = cmds + sizeof cmds / sizeof (char *);
  296.  
  297.      fprintf(stderr,"Syntax: %s {<opts>} [<file>] {<opts>}\n", _prgname());
  298.      while (p < e)
  299.           fputs(*p++,stderr);
  300.      exit(stat);
  301. }
  302.