home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d5xx / d502 / autorev.lha / AutoRev / AutoRev.c < prev    next >
C/C++ Source or Header  |  1991-06-27  |  9KB  |  305 lines

  1. /*-- AutoRev header do NOT edit!
  2. *
  3. *   Program         :   AutoRev.c
  4. *   Copyright       :   © 1991 Jaba Development
  5. *   Author          :   Jan van den Baard
  6. *   Creation Date   :   04-Apr-91
  7. *   Current version :   1.1r
  8. *   Translator      :   DICE
  9. *
  10. *   REVISION HISTORY
  11. *
  12. *   Date          Version         Comment
  13. *   ---------     -------         ------------------------------------------
  14. *   03-May-91     1.1r            Uses '__regargs' now. 4924 bytes!
  15. *   14-Apr-91     1.1             Added top/bottom choice and args shortcuts
  16. *   04-Apr-91     1.0             Initial version! Requires kickstart 2.0!
  17. *
  18. *-- REV_END --*/
  19.  
  20. /*
  21.  * COMPILING:   dcc -r -mRR AutoRev.c -o AutoRev
  22.  */
  23. #include <exec/types.h>
  24. #include <exec/memory.h>
  25. #include <exec/libraries.h>
  26. #include <dos/dos.h>
  27. #include <dos/stdio.h>
  28. #include <string.h>
  29.  
  30. #include <clib/exec_protos.h>
  31. #include <clib/dos_protos.h>
  32.  
  33. #define TEMPLATE        "File/a,ASS=ASSEM/s,MOD=MODULA/s,UPD=UPDATE/s,BOT=BOTTOM/s,AUT=AUTHOR/k,COP=COPYRIGHT/k,REV=REVISION/k,COM=COMMENT/k,TRA=TRANSLATOR/k"
  34. #define USAGE           "Usage: AutoRev <filename> [ASSEM] [MODULA] [UPDATE] [BOTTOM]\n"\
  35.                         "                          [AUTHOR <name>] [COPYRIGHT <string>]\n"\
  36.                         "                          [REVISION <string>] [COMMENT <string>]\n"\
  37.                         "                          [TRANSLATOR <string>]\n\n"\
  38.                         "With:  filename    : The name of the header you want to create/update.\n"\
  39.                         "       ASSEM       :\n       MODULA      : The target language of the header.\n"\
  40.                         "       UPDATE      : Update source header.\n"\
  41.                         "       BOTTOM      : History at the bottom.\n"\
  42.                         "       AUTHOR      : Optional author name.\n"\
  43.                         "       COPYRIGHT   : Optional copyright string.\n"\
  44.                         "       REVISION    : Optional revision specifier. (e.g. 1.0 or 1.1a  etc.)\n"\
  45.                         "       COMMENT     : Optional comment string\n"\
  46.                         "       TRANSLATOR  : Optional name and version of your compiler/assembler.\n"
  47. #define COPYR           "\r\033[0;1;33mAutoRev v1.1r\033[0m - © 1991 Jaba Development\n"\
  48.                         "  \033[1mWritten by Jan van den Baard.\033[0m\n"
  49.  
  50. #define PUT(m)          Write(stdout,m,strlen(m))
  51.  
  52. struct RDArgs           *RArgs = 0L;
  53. struct RDArgs            IArgs = { 0,0,0,0,0,0,USAGE,0 };
  54.  
  55. ULONG                    size, Args[10];
  56. BPTR                     infile = 0L, outfile = 0L, stdout;
  57. UBYTE                    Language = 0L;   /* 0=C, 1=Assem, 2=Modula */
  58. UBYTE                   *Name, *Mem = 0L, Revision[9];
  59.  
  60. char *lines[]  = { "*-- AutoRev header do NOT edit!\n*\n",
  61.                    "*   Program         :   %-52.52ls\n",
  62.                    "*   Copyright       :   %-52.52ls\n",
  63.                    "*   Author          :   %-52.52ls\n",
  64.                    "*   Creation Date   :   ",
  65.                    "*   Current version :   %-7.7ls\n",
  66.                    "*   Translator      :   %-52.52ls\n*\n",
  67.                    "*   REVISION HISTORY\n*\n",
  68.                    "*   Date          Version         Comment\n",
  69.                    "*   ---------     -------         ------------------------------------------\n",
  70.                    "*-- REV_END --*" };
  71.  
  72. extern struct Library *SysBase;
  73.  
  74. int _main( void );
  75. __regargs void cleanup( char *, long );
  76. void create( void );
  77. __regargs void writecomm( BOOL );
  78. void endheader( void );
  79. void revline( void );
  80. long getrev( void );
  81. void loadold( void );
  82. void update( void );
  83. extern __stkargs void _exit( int );
  84.  
  85. __regargs void writecomm(BOOL how)
  86. {
  87.     if(!Language)
  88.         FPutC(outfile,'/');
  89.     else if(Language == 2) {
  90.         if(how)
  91.             FPutC(outfile,'(');
  92.         else
  93.             FPutC(outfile,')');
  94.     }
  95. }
  96.  
  97. void endheader(void)
  98. {
  99.     VFPrintf(outfile,lines[10],0);
  100.     if(Language != 1)
  101.         writecomm(FALSE);
  102.     VFPrintf(outfile,"\n\n",0);
  103. }
  104.  
  105. void dodate(void)
  106. {
  107.     struct DateTime  dt;
  108.     char             date[10];
  109.  
  110.     DateStamp((struct DateStamp *)&dt);
  111.     dt.dat_Format  = FORMAT_DOS;
  112.     dt.dat_StrDate = &date[0];
  113.     dt.dat_Flags   = 0;
  114.     dt.dat_StrDay  = 0;
  115.     dt.dat_StrTime = 0;
  116.     DateToStr(&dt);
  117.     VFPrintf(outfile,&date[0],0);
  118. }
  119.  
  120. void revline(void)
  121. {
  122.     VFPrintf(outfile,"*   ",0);
  123.     dodate();
  124.     VFPrintf(outfile,"     %-7.7ls",&Args[7]);
  125.     VFPrintf(outfile,"         %-42.42ls\n",&Args[8]);
  126. }
  127.  
  128. long getrev(void)
  129. {
  130.     UBYTE   *m = Mem, *m2, i = 0;
  131.  
  132.     m2 = Mem+(size-15);
  133.  
  134.     while((strncmp(m++,"Current version",15)) && (m <= m2));
  135.  
  136.     if(m > m2) return(FALSE);
  137.     m += 19;
  138.     while(*m != 0x0a) Revision[i++] = *m++;
  139.     Revision[i++] = 0x0a;
  140.     Revision[i]   = 0;
  141.     return(TRUE);
  142. }
  143.  
  144. void create(void)
  145. {
  146.     UBYTE   c = 'y';
  147.  
  148.     if((infile = Open(Name,MODE_OLDFILE))) {
  149.         Close(infile);
  150.         infile = 0L;
  151.         PUT("File already exists! Overwrite ? (y/n) : ");
  152.         c = ReadChar();
  153.     }
  154.  
  155.     if(c == 'y' || c == 'Y') {
  156.         if((outfile = Open(Name,MODE_NEWFILE))) {
  157.             SetIoErr(0L);
  158.             writecomm(TRUE);
  159.             VFPrintf(outfile,lines[0],0);
  160.             VFPrintf(outfile,lines[1],&Name);
  161.             VFPrintf(outfile,lines[2],&Args[6]);
  162.             VFPrintf(outfile,lines[3],&Args[5]);
  163.             VFPrintf(outfile,lines[4],0);
  164.             dodate();
  165.             FPutC(outfile,0x0a);
  166.             VFPrintf(outfile,lines[5],&Args[7]);
  167.             VFPrintf(outfile,lines[6],&Args[9]);
  168.             VFPrintf(outfile,lines[7],0);
  169.             VFPrintf(outfile,lines[8],0);
  170.             VFPrintf(outfile,lines[9],0);
  171.             revline();
  172.             VFPrintf(outfile,"*\n",0);
  173.             endheader();
  174.             Close(outfile);
  175.             outfile = 0L;
  176.             if(IoErr())
  177.                 cleanup("Write error!\n",20L);
  178.         } else
  179.             cleanup("Can't open output file!\n",20L);
  180.     } else
  181.         cleanup("Header not created!\n",5L);
  182. }
  183.  
  184. void update(void)
  185. {
  186.     char    old_name[32];
  187.     ULONG   curcnt = 0L, hiscnt = 0L;
  188.     UBYTE   *cm,*hm;
  189.  
  190.     loadold();
  191.  
  192.     if(!getrev())
  193.         cleanup("No revision header found!\n",20L);
  194.  
  195.     if(!Args[7])
  196.         Args[7] = &Revision[0];
  197.     else {
  198.         strcpy(&Revision[0],(char *)Args[7]);
  199.         strcat(&Revision[0],"\n");
  200.         Args[7] = &Revision[0];
  201.     }
  202.  
  203.     strncpy(old_name,Name,27);
  204.     strcat(old_name,".old");
  205.     Rename(Name,(char *)&old_name[0]);
  206.  
  207.     if(!(outfile = Open(Name,MODE_NEWFILE)))
  208.         cleanup("Can't open output file!\n",20L);
  209.  
  210.     SetIoErr(0L);
  211.  
  212.     cm = Mem;
  213.  
  214.     while(strncmp(cm++,"Current version",15))
  215.         curcnt++;
  216.  
  217.     cm     += 20;
  218.     curcnt += 20;
  219.  
  220.     FWrite(outfile,Mem,curcnt,1);
  221.     FWrite(outfile,(char *)Args[7],strlen((char *)Args[7]),1);
  222.  
  223.     Revision[strlen((char *)Args[7])-1] = 0;
  224.  
  225.     while(*cm++ != 0x0a) curcnt++;;
  226.     hm = cm;
  227.  
  228.     if(!Args[4]) {
  229.         while(strncmp(cm++,"REVISION HISTORY",16))
  230.             hiscnt++;
  231.  
  232.         cm     += 138;
  233.         hiscnt += 138;
  234.  
  235.         FWrite(outfile,hm,hiscnt,1);
  236.     } else {
  237.         while(strncmp(cm++,"*-- REV_END",11))
  238.             hiscnt++;
  239.  
  240.         cm     -= 2;
  241.         hiscnt -= 2;
  242.  
  243.         FWrite(outfile,hm,hiscnt,1);
  244.     }
  245.  
  246.     revline();
  247.     FWrite(outfile,cm-1,(size-(curcnt+hiscnt)-2),1);
  248.     Close(outfile);
  249.     outfile = 0L;
  250.     if(IoErr())
  251.         cleanup("Write error!\n",20L);
  252. }
  253.  
  254. void loadold(void)
  255. {
  256.     if((infile = Open(Name,MODE_OLDFILE))) {
  257.                Seek(infile,0,OFFSET_END);
  258.         size = Seek(infile,0,OFFSET_BEGINNING);
  259.  
  260.         if((Mem = AllocMem(size,MEMF_PUBLIC))) {
  261.             if(Read(infile,Mem,size) < size)
  262.                 cleanup("Read error!\n",20L);
  263.             Close(infile);
  264.             infile = 0L;
  265.         } else
  266.             cleanup("Out of memory!\n",20L);
  267.     } else
  268.         cleanup("Can't open input file!\n",20L);
  269. }
  270.  
  271. __regargs void cleanup(char *msg, long code)
  272. {
  273.     if(infile)      Close(infile);
  274.     if(outfile)     Close(outfile);
  275.     if(Mem)         FreeMem(Mem,size);
  276.     if(RArgs)       FreeArgs(RArgs);
  277.     if(msg)         PUT(msg);
  278.     _exit(code);
  279. }
  280.  
  281. int _main( void )
  282. {
  283.     stdout = Output();
  284.     if(SysBase->lib_Version < 36)
  285.         cleanup("You need kickstart 2.0!\n",20L);
  286.  
  287.     setmem(&Args[0],40L,0);
  288.  
  289.     if((RArgs = ReadArgs(TEMPLATE,&Args[0],&IArgs))) {
  290.         PUT(COPYR);
  291.         Name = (char *)Args[0];
  292.  
  293.         if(Args[1])      Language = 1;
  294.         else if(Args[2]) Language = 2;
  295.         else             Language = 0;
  296.     } else
  297.         cleanup("Bad args for AutoRev.\n",20L);
  298.  
  299.     if(!Args[3]) create();
  300.     else         update();
  301.  
  302.     cleanup(NULL,NULL);
  303.     return(0L);
  304. }
  305.