home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / utilities / setrev.lha / SetRev.c next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  8.1 KB  |  447 lines

  1. /**
  2. ***  SetRev.c    Set revision number due to the Amiga guidelines
  3. ***
  4. ***  This is a small BumpRev replacement (yes, yet another :-).
  5. ***  Unlike BumpRev, UpRev and others this doesn't need to use
  6. ***  a special revision file. Instead it scans a source file for
  7. ***  certain patterns which are replaced.
  8. ***
  9. ***
  10. ***  Compiler: Dice 3.01
  11. ***
  12. ***  Computer: Amiga 1200
  13. ***
  14. ***  Author:    Jochen Wiedmann
  15. ***        Am Eisteich 9
  16. ***        72555 Metzingen
  17. ***        Germany
  18. ***
  19. ***        Phone: (49)7123 / 14881
  20. ***        Internet: wiedmann@zdv.uni-tuebingen.de
  21. ***
  22. ***
  23. ***  This program is in the public domain; use it as you want,
  24. ***  but WITHOUT ANY WARRANTY!
  25. **/
  26.  
  27.  
  28. /**
  29. ***  These are the patterns that will be replaced.
  30. **/
  31. #define VERSION        1
  32. #define REVISION       1
  33. #define DATE    "30.10.94"
  34. #define VERS    "SetRev 1.1"
  35. #define VSTRING "SetRev 1.1 (30.10.94)"
  36. #define VERSTAG "\0$VER: SetRev 1.1 (30.10.94)"
  37.  
  38.  
  39.  
  40.  
  41.  
  42. /**
  43. ***  Include files
  44. **/
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <time.h>
  49. #include <errno.h>
  50.  
  51. #if defined(__GNUC__)
  52. #include <unistd.h>
  53. #define stricmp strcasecmp
  54. #define strnicmp strncasecmp
  55. #endif
  56.  
  57.  
  58. #ifndef FALSE
  59. #define FALSE 0
  60. #endif
  61. #ifndef TRUE
  62. #define TRUE (!FALSE)
  63. #endif
  64.  
  65.  
  66.  
  67.  
  68.  
  69. /**
  70. ***  SetRev cannot scan files with lines longer than MAXLINE character.
  71. **/
  72. #define MAXLINE 4096
  73.  
  74.  
  75.  
  76.  
  77. /**
  78. ***  Global variables
  79. **/
  80. int CurrentVersion = 1;
  81. int CurrentRevision = 1;
  82. char *CurrentName;
  83. char CurrentDate[20];
  84.  
  85. char line[MAXLINE];
  86.  
  87. int Created = FALSE;
  88.  
  89.  
  90.  
  91. /**
  92. ***  This function determines the current name, version
  93. ***  and revision.
  94. **/
  95. void FirstScan(char *file)
  96.  
  97.   {
  98.     FILE *fh;
  99.     int linenum = 1;
  100.  
  101.     if (!(fh = fopen(file, "r")))
  102.       {
  103.     if (errno == ENOENT)
  104.       {
  105.         /**
  106.         ***  File doesn't exist, create it.
  107.         **/
  108.         if (!(fh = fopen(file, "w")))
  109.           {
  110.         perror("SetRev");
  111.         exit(10);
  112.           }
  113.  
  114.         if (fprintf(fh,
  115.                "#define VERSION     %d\n",
  116.                CurrentVersion)    <  0                ||
  117.         fprintf(fh,
  118.                "#define REVISION    %d\n",
  119.                CurrentRevision)  <  0                ||
  120.         fprintf(fh,
  121.                "#define DATE        \"%s\"\n",
  122.                CurrentDate)  <    0                ||
  123.         fprintf(fh,
  124.                "#define VERS        \"%s %d.%d\"\n",
  125.                CurrentName, CurrentVersion,
  126.                CurrentRevision)  <  0                ||
  127.         fprintf(fh,
  128.                "#define VSTRING     \"%s %d.%d (%s)\\r\\n\"\n",
  129.                CurrentName, CurrentVersion,
  130.                CurrentRevision, CurrentDate)  <  0        ||
  131.         fprintf(fh,
  132.                "#define VERSTAG     \"\\0$VER: %s %d.%d (%s)\"\n",
  133.                CurrentName, CurrentVersion,
  134.                CurrentRevision, CurrentDate)  <  0        ||
  135.         fclose(fh))
  136.           {
  137.         perror("SetRev");
  138.         exit(10);
  139.           }
  140.  
  141.         Created = TRUE;
  142.  
  143.         return;
  144.       }
  145.     perror("SetRev");
  146.     exit(10);
  147.       }
  148.  
  149.     errno = 0;
  150.     while(fgets(line, MAXLINE, fh)  &&  !errno)
  151.       {
  152.     int len = strlen(line);
  153.  
  154.     if (len+1 == MAXLINE  &&  line[MAXLINE-2] != '\n')
  155.       {
  156.         fprintf(stderr,
  157.             "SetRev warning: Line %d exceeds %d characters\n",
  158.             linenum, MAXLINE);
  159.       }
  160.  
  161.     if (strncmp(line, "#define", 7) == 0)
  162.       {
  163.         char *ptr = line+7;
  164.  
  165.         while (*ptr == ' '  ||  *ptr == '\t')
  166.           {
  167.         ++ptr;
  168.           }
  169.  
  170.         if (strncmp(ptr, "VERSION", 7) == 0  &&
  171.         (ptr[7] == ' ' || ptr[7] == '\t'))
  172.           {
  173.         CurrentVersion = atoi(&ptr[7]);
  174.           }
  175.         else if (strncmp(ptr, "REVISION", 8) == 0  &&
  176.              (ptr[8] == ' '  ||  ptr[8] == '\t'))
  177.           {
  178.         CurrentRevision = atoi(&ptr[8]);
  179.           }
  180.       }
  181.  
  182.     if (line[len-1] == '\n')
  183.       {
  184.         ++linenum;
  185.       }
  186.       }
  187.     fclose(fh);
  188.   }
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /**
  195. ***  This function inserts the new version, revision,
  196. ***  projectname and other stuff in a second scan.
  197. **/
  198. void SecondScan(char *file)
  199.  
  200.   {
  201.     char *bakfile;
  202.     FILE *fh, *bfh;
  203.     int linenum = 1;
  204.  
  205.     if (!(bakfile = malloc(strlen(file)+5)))
  206.       {
  207.     perror("SetRev");
  208.     exit(10);
  209.       }
  210.     strcpy(bakfile, file);
  211.     strcat(bakfile, ".bak");
  212.  
  213.     if (!(fh = fopen(file, "r"))  ||  !(bfh = fopen(bakfile, "w")))
  214.       {
  215.     perror("SetRev");
  216.     exit(10);
  217.       }
  218.  
  219.     errno = 0;
  220.     while(fgets(line, MAXLINE, fh)  &&  !errno)
  221.       {
  222.     int len = strlen(line);
  223.  
  224.     if (len+1 == MAXLINE  &&  line[MAXLINE-2] != '\n')
  225.       {
  226.         fprintf(stderr,
  227.             "SetRev warning: Line %d exceeds %d characters\n",
  228.             linenum, MAXLINE);
  229.       }
  230.  
  231.     if (strncmp(line, "#define", 7) == 0)
  232.       {
  233.         char *ptr = line+7;
  234.  
  235.         while (*ptr == ' '  ||  *ptr == '\t')
  236.           {
  237.         ++ptr;
  238.           }
  239.  
  240.         if (strncmp(ptr, "VERSION", 7) == 0  &&
  241.         (ptr[7] == ' ' || ptr[7] == '\t'))
  242.           {
  243.         ptr += 7;
  244.         while (*ptr == ' '  ||  *ptr == '\t')
  245.           {
  246.             ++ptr;
  247.           }
  248.         sprintf(ptr, "%d\n", CurrentVersion);
  249.           }
  250.         else if (strncmp(ptr, "REVISION", 8) == 0  &&
  251.              (ptr[8] == ' '  ||  ptr[8] == '\t'))
  252.           {
  253.         ptr += 8;
  254.         while (*ptr == ' '  ||  *ptr == '\t')
  255.           {
  256.             ++ptr;
  257.           }
  258.         sprintf(ptr, "%d\n", CurrentRevision);
  259.           }
  260.         else if (strncmp(ptr, "DATE", 4) == 0  &&
  261.              (ptr[4] == ' '  ||  ptr[4] == '\t'))
  262.           {
  263.         ptr += 4;
  264.         while (*ptr == ' '  ||  *ptr == '\t')
  265.           {
  266.             ++ptr;
  267.           }
  268.         sprintf(ptr, "\"%s\"\n", CurrentDate);
  269.           }
  270.         else if (strncmp(ptr, "VERS", 4) == 0  &&
  271.              (ptr[4] == ' '  ||  ptr[4] == '\t'))
  272.           {
  273.         ptr += 4;
  274.         while (*ptr == ' '  ||  *ptr == '\t')
  275.           {
  276.             ++ptr;
  277.           }
  278.         sprintf(ptr, "\"%s %d.%d\"\n", CurrentName,
  279.             CurrentVersion, CurrentRevision);
  280.           }
  281.         else if (strncmp(ptr, "VSTRING", 7) == 0  &&
  282.              (ptr[7] == ' '  ||  ptr[7] == '\t'))
  283.           {
  284.         ptr += 7;
  285.         while (*ptr == ' '  ||  *ptr == '\t')
  286.           {
  287.             ++ptr;
  288.           }
  289.         sprintf(ptr, "\"%s %d.%d (%s)\"\n",
  290.             CurrentName, CurrentVersion,
  291.             CurrentRevision, CurrentDate);
  292.           }
  293.         else if (strncmp(ptr, "VERSTAG", 7) == 0  &&
  294.              (ptr[7] == ' '  ||  ptr[7] == '\t'))
  295.           {
  296.         ptr += 7;
  297.         while (*ptr == ' '  ||  *ptr == '\t')
  298.           {
  299.             ++ptr;
  300.           }
  301.         sprintf(ptr, "\"\\0$VER: %s %d.%d (%s)\"\n",
  302.             CurrentName, CurrentVersion,
  303.             CurrentRevision, CurrentDate);
  304.           }
  305.       }
  306.     fputs(line, bfh);
  307.       }
  308.     fclose(fh);
  309.     fclose(bfh);
  310.  
  311.     unlink(file);
  312.     rename(bakfile, file);
  313.   }
  314.  
  315.  
  316.  
  317.  
  318.  
  319. /**
  320. ***  This prints out the Usage: message.
  321. **/
  322. void Usage(void)
  323.  
  324.   {
  325.     printf("Usage: SetRev PROJECT/A,VERSION/N,FILE/K\n\n");
  326.     printf("The given FILE (default: PROJECT_rev.h) will be searched for\n");
  327.     printf("version and revision definitions and bumped to the next\n");
  328.     printf("revision number.\n\n");
  329.     printf("%s © 1994  by  Jochen Wiedmann\n\n", VSTRING);
  330.     printf("This program is in the public domain, use it as you want, but\n");
  331.     printf("WITHOUT ANY WARRANTY.\n");
  332.     exit(5);
  333.   }
  334.  
  335.  
  336.  
  337.  
  338.  
  339. /**
  340. ***  Finally main()
  341. **/
  342. int main(int argc, char *argv[])
  343.  
  344.   {
  345.     int i;
  346.     char *file;
  347.     int versionseen, version;
  348.     time_t currenttime = time(NULL);
  349.     struct tm *localtm = localtime(¤ttime);
  350.  
  351.     /**
  352.     *** Get local time
  353.     **/
  354.     currenttime = time(NULL);
  355.     localtm = localtime(¤ttime);
  356.     strftime(CurrentDate, sizeof(CurrentDate), "%d.%m.%y", localtm);
  357.  
  358.     if (strcmp(argv[1], "?") == 0  ||
  359.     strcmp(argv[1], "-h") == 0  ||
  360.     strcmp(argv[1], "--help") == 0)
  361.       {
  362.     Usage();
  363.       }
  364.  
  365.     CurrentName = NULL;
  366.     versionseen = FALSE;
  367.     file = NULL;
  368.     for (i = 1;  i < argc;  i++)
  369.       {
  370.     if (stricmp(argv[i], "file") == 0  &&  i+1 < argc)
  371.       {
  372.         if (file)
  373.           {
  374.         Usage();
  375.           }
  376.         file = argv[++i];
  377.       }
  378.     else if (strnicmp(argv[i], "file=", 5) == 0)
  379.       {
  380.         if (file)
  381.           {
  382.         Usage();
  383.           }
  384.         file = &argv[i][5];
  385.       }
  386.     else if (!CurrentName)
  387.       {
  388.         CurrentName = argv[i];
  389.       }
  390.     else if (!versionseen)
  391.       {
  392.         if ((version = atoi(argv[i])) == 0)
  393.           {
  394.         Usage();
  395.           }
  396.         versionseen = TRUE;
  397.       }
  398.     else
  399.       {
  400.         Usage();
  401.       }
  402.       }
  403.     if (!CurrentName)
  404.       {
  405.     Usage();
  406.       }
  407.  
  408.     if (!file)
  409.       {
  410.     if (!(file = malloc(strlen(CurrentName) + 7)))
  411.       {
  412.         errno = ENOMEM;
  413.         perror("SetRev");
  414.         exit(20);
  415.       }
  416.     strcpy(file, CurrentName);
  417.     strcat(file, "_rev.h");
  418.       }
  419.  
  420.     if (versionseen)
  421.       {
  422.     CurrentVersion = version;
  423.       }
  424.  
  425.     FirstScan(file);
  426.  
  427.     if (!Created)
  428.       {
  429.     if (versionseen  &&  version != CurrentVersion)
  430.       {
  431.         CurrentRevision = 1;
  432.       }
  433.     else
  434.       {
  435.         ++CurrentRevision;
  436.       }
  437.  
  438.     if (versionseen)
  439.       {
  440.         CurrentVersion = version;
  441.       }
  442.  
  443.     SecondScan(file);
  444.       }
  445.     exit(0);
  446.   }
  447.