version.sh.c

Go to the documentation of this file.
00001 /* version file handler for win32.
00002  *
00003  * (C) 2003-2007 Anope Team
00004  * Contact us at info@anope.org
00005  *
00006  * Please read COPYING and README for furhter details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  *
00011  * Written by Dominick Meglio <codemastr@unrealircd.com>
00012  *
00013  */
00014 
00015 /* Needed due to Windows lack of a decent interpreter */
00016 
00017 #include <stdio.h>
00018 #include <string.h>
00019 
00020 #define CTRL "version.log"
00021 
00022 long version_major, version_minor, version_patch, version_build, build;
00023 char *version_extra = NULL;
00024 char version[1024];
00025 char version_dotted[1024];
00026 
00027 
00028 void load_ctrl(FILE *);
00029 long get_value(char *);
00030 char *get_value_str(char *);
00031 char *strip(char *);
00032 void parse_version(FILE *);
00033 void write_version(FILE *);
00034 void parse_line(FILE *, char *);
00035 
00036 int main()
00037 {
00038     FILE *fd = fopen(CTRL, "r");
00039 
00040 
00041     if (!fd) {
00042         fprintf(stderr, "Error: Unable to find control file: " CTRL "\n");
00043         exit(0);
00044     }
00045 
00046     load_ctrl(fd);
00047     fclose(fd);
00048 
00049     _snprintf(version, 1024, "%d.%d.%d%s (%d)", version_major, version_minor,
00050               version_patch, (version_extra ? version_extra : ""), version_build);
00051 
00052     _snprintf(version_dotted, 1024, "%d.%d.%d%s.%d", version_major, version_minor,
00053               version_patch, (version_extra ? version_extra : ""), version_build);
00054 
00055     fd = fopen("version.h", "r");
00056 
00057     if (fd) {
00058         parse_version(fd);
00059         fclose(fd);
00060     } else
00061         build = 1;
00062 
00063 
00064     fd = fopen("version.h", "w");
00065     write_version(fd);
00066     fclose(fd);
00067     
00068     if (version_extra)
00069         free(version_extra);
00070 }
00071 
00072 void load_ctrl(FILE * fd)
00073 {
00074     char buf[512];
00075     while (fgets(buf, 511, fd)) {
00076         char *var;
00077 
00078         strip(buf);
00079 
00080         var = strtok(buf, "=");
00081         if (!var)
00082             continue;
00083         if (!strcmp(var, "VERSION_MAJOR"))
00084             version_major = get_value(strtok(NULL, ""));
00085         else if (!strcmp(var, "VERSION_MINOR"))
00086             version_minor = get_value(strtok(NULL, ""));
00087         else if (!strcmp(var, "VERSION_PATCH"))
00088             version_patch = get_value(strtok(NULL, ""));
00089         else if (!strcmp(var, "VERSION_BUILD"))
00090             version_build = get_value(strtok(NULL, ""));
00091         else if (!strcmp(var, "VERSION_EXTRA"))
00092             version_extra = get_value_str(strtok(NULL, ""));
00093 
00094     }
00095 }
00096 
00097 char *strip(char *str)
00098 {
00099     char *c;
00100     if ((c = strchr(str, '\n')))
00101         *c = 0;
00102     if ((c = strchr(str, '\r')))
00103         *c = 0;
00104     return str;
00105 }
00106 
00107 long get_value(char *string)
00108 {
00109     return atol(get_value_str(string));
00110 }
00111 
00112 char *get_value_str(char *string)
00113 {
00114     int len;
00115 
00116     if (*string == '"')
00117         string++;
00118 
00119     len = strlen(string);
00120 
00121     if (string[len - 1] == '"')
00122         string[len - 1] = 0;
00123     if (!*string)
00124         return NULL;
00125     return strdup(string);
00126 }
00127 
00128 void parse_version(FILE * fd)
00129 {
00130     char buf[1024];
00131 
00132     while (fgets(buf, 1023, fd)) {
00133         char *para1;
00134 
00135         strip(buf);
00136         para1 = strtok(buf, " \t");
00137 
00138         if (!para1)
00139             continue;
00140 
00141         if (!strcmp(para1, "#define")) {
00142             char *para2 = strtok(NULL, " \t");
00143 
00144             if (!para2)
00145                 continue;
00146 
00147             if (!strcmp(para2, "BUILD")) {
00148                 char *value = strtok(NULL, "");
00149                 build = get_value(value);
00150                 build++;
00151                 return;
00152             }
00153         }
00154     }
00155     build = 1;
00156 }
00157 
00158 void write_version(FILE * fd)
00159 {
00160     FILE *fdin = fopen("include\\version.sh", "r");
00161     char buf[1024];
00162     short until_eof = 0;
00163 
00164     while (fgets(buf, 1023, fdin)) {
00165         strip(buf);
00166 
00167         if (until_eof)
00168             if (!strcmp(buf, "EOF"))
00169                 break;
00170             else
00171                 parse_line(fd, buf);
00172 
00173         if (!strcmp(buf, "cat >version.h <<EOF"))
00174             until_eof = 1;
00175     }
00176 
00177 }
00178 
00179 void parse_line(FILE * fd, char *line)
00180 {
00181     char *c;
00182     for (c = line; *c; c++) {
00183         /* It's a variable, find out which */
00184         if (*c == '$') {
00185             char *var, *varbegin;
00186 
00187             if (*(c + 1))
00188                 c++;
00189             else
00190                 continue;
00191             for (var = varbegin = c; var; var++) {
00192                 if (!isalnum(*var) && *var != '_')
00193                     break;
00194             }
00195             if (var != varbegin) {
00196                 char tmp = *var;
00197 
00198                 *var = 0;
00199                 if (!strcmp(varbegin, "VERSION_MAJOR"))
00200                     fprintf(fd, "%d", version_major);
00201                 else if (!strcmp(varbegin, "VERSION_MINOR"))
00202                     fprintf(fd, "%d", version_minor);
00203                 else if (!strcmp(varbegin, "VERSION_PATCH"))
00204                     fprintf(fd, "%d", version_patch);
00205                 else if (!strcmp(varbegin, "VERSION_EXTRA")) {
00206                     if (version_extra)
00207                         fprintf(fd, "%s", version_extra);
00208                 } else if (!strcmp(varbegin, "VERSION_BUILD"))
00209                     fprintf(fd, "%d", version_build);
00210                 else if (!strcmp(varbegin, "BUILD"))
00211                     fprintf(fd, "%d", build);
00212                 else if (!strcmp(varbegin, "VERSION"))
00213                     fprintf(fd, "%s", version);
00214                 else if (!strcmp(varbegin, "VERSIONDOTTED"))
00215                     fprintf(fd, "%s", version_dotted);
00216                 fputc(tmp, fd);
00217             }
00218             c = var;
00219         } else
00220             fputc(*c, fd);
00221     }
00222     /* We only need \n here - we didn't open the file as binary -GD */
00223     fprintf(fd, "\n");
00224 }

Generated on Sun Dec 30 09:26:45 2007 for Anope by  doxygen 1.5.1-20070107