Anope IRC Services  Version 1.8
version.sh.c
Go to the documentation of this file.
1 /* version file handler for win32.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  * Written by Dominick Meglio <codemastr@unrealircd.com>
12  *
13  */
14 
15 /* Needed due to Windows lack of a decent interpreter */
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 #define CTRL "version.log"
21 
23 char *version_extra = NULL;
24 char version[1024];
25 char version_dotted[1024];
26 
27 
28 void load_ctrl(FILE *);
29 long get_value(char *);
30 char *get_value_str(char *);
31 char *strip(char *);
32 void parse_version(FILE *);
33 void write_version(FILE *);
34 void parse_line(FILE *, char *);
35 
36 int main()
37 {
38  FILE *fd = fopen(CTRL, "r");
39 
40 
41  if (!fd) {
42  fprintf(stderr, "Error: Unable to find control file: " CTRL "\n");
43  exit(0);
44  }
45 
46  load_ctrl(fd);
47  fclose(fd);
48 
49  _snprintf(version, 1024, "%d.%d.%d%s (%d)", version_major, version_minor,
51 
52  _snprintf(version_dotted, 1024, "%d.%d.%d%s.%d", version_major, version_minor,
53  version_patch, (version_extra ? version_extra : ""), version_build);
54 
55  fd = fopen("version.h", "r");
56 
57  if (fd) {
58  parse_version(fd);
59  fclose(fd);
60  } else
61  build = 1;
62 
63 
64  fd = fopen("version.h", "w");
65  write_version(fd);
66  fclose(fd);
67 
68  if (version_extra)
69  free(version_extra);
70 }
71 
72 void load_ctrl(FILE * fd)
73 {
74  char buf[512];
75  while (fgets(buf, 511, fd)) {
76  char *var;
77 
78  strip(buf);
79 
80  var = strtok(buf, "=");
81  if (!var)
82  continue;
83  if (!strcmp(var, "VERSION_MAJOR"))
84  version_major = get_value(strtok(NULL, ""));
85  else if (!strcmp(var, "VERSION_MINOR"))
86  version_minor = get_value(strtok(NULL, ""));
87  else if (!strcmp(var, "VERSION_PATCH"))
88  version_patch = get_value(strtok(NULL, ""));
89  else if (!strcmp(var, "VERSION_BUILD"))
90  version_build = get_value(strtok(NULL, ""));
91  else if (!strcmp(var, "VERSION_EXTRA"))
92  version_extra = get_value_str(strtok(NULL, ""));
93 
94  }
95 }
96 
97 char *strip(char *str)
98 {
99  char *c;
100  if ((c = strchr(str, '\n')))
101  *c = 0;
102  if ((c = strchr(str, '\r')))
103  *c = 0;
104  return str;
105 }
106 
107 long get_value(char *string)
108 {
109  return atol(get_value_str(string));
110 }
111 
112 char *get_value_str(char *string)
113 {
114  int len;
115 
116  if (*string == '"')
117  string++;
118 
119  len = strlen(string);
120 
121  if (string[len - 1] == '"')
122  string[len - 1] = 0;
123  if (!*string)
124  return NULL;
125  return strdup(string);
126 }
127 
128 void parse_version(FILE * fd)
129 {
130  char buf[1024];
131 
132  while (fgets(buf, 1023, fd)) {
133  char *para1;
134 
135  strip(buf);
136  para1 = strtok(buf, " \t");
137 
138  if (!para1)
139  continue;
140 
141  if (!strcmp(para1, "#define")) {
142  char *para2 = strtok(NULL, " \t");
143 
144  if (!para2)
145  continue;
146 
147  if (!strcmp(para2, "BUILD")) {
148  char *value = strtok(NULL, "");
149  build = get_value(value);
150  build++;
151  return;
152  }
153  }
154  }
155  build = 1;
156 }
157 
158 void write_version(FILE * fd)
159 {
160  FILE *fdin = fopen("include\\version.sh", "r");
161  char buf[1024];
162  short until_eof = 0;
163 
164  if (!fdin)
165  return;
166  while (fgets(buf, 1023, fdin)) {
167  strip(buf);
168 
169  if (until_eof)
170  if (!strcmp(buf, "EOF"))
171  break;
172  else
173  parse_line(fd, buf);
174 
175  if (!strcmp(buf, "cat >version.h <<EOF"))
176  until_eof = 1;
177  }
178  fclose(fdin);
179 }
180 
181 void parse_line(FILE * fd, char *line)
182 {
183  char *c;
184  for (c = line; *c; c++) {
185  /* It's a variable, find out which */
186  if (*c == '$') {
187  char *var, *varbegin;
188 
189  if (*(c + 1))
190  c++;
191  else
192  continue;
193  for (var = varbegin = c; var; var++) {
194  if (!isalnum(*var) && *var != '_')
195  break;
196  }
197  if (var != varbegin) {
198  char tmp = *var;
199 
200  *var = 0;
201  if (!strcmp(varbegin, "VERSION_MAJOR"))
202  fprintf(fd, "%d", version_major);
203  else if (!strcmp(varbegin, "VERSION_MINOR"))
204  fprintf(fd, "%d", version_minor);
205  else if (!strcmp(varbegin, "VERSION_PATCH"))
206  fprintf(fd, "%d", version_patch);
207  else if (!strcmp(varbegin, "VERSION_EXTRA")) {
208  if (version_extra)
209  fprintf(fd, "%s", version_extra);
210  } else if (!strcmp(varbegin, "VERSION_BUILD"))
211  fprintf(fd, "%d", version_build);
212  else if (!strcmp(varbegin, "BUILD"))
213  fprintf(fd, "%d", build);
214  else if (!strcmp(varbegin, "VERSION"))
215  fprintf(fd, "%s", version);
216  else if (!strcmp(varbegin, "VERSIONDOTTED"))
217  fprintf(fd, "%s", version_dotted);
218  fputc(tmp, fd);
219  }
220  c = var;
221  } else
222  fputc(*c, fd);
223  }
224  /* We only need \n here - we didn't open the file as binary -GD */
225  fprintf(fd, "\n");
226 }
E char * strdup(const char *s)
Definition: compat.c:94
void parse_line(FILE *, char *)
Definition: version.sh.c:181
int main()
Definition: version.sh.c:36
long version_major
Definition: version.sh.c:22
#define CTRL
Definition: version.sh.c:20
void parse_version(FILE *)
Definition: version.sh.c:128
long version_patch
Definition: version.sh.c:22
long version_minor
Definition: version.sh.c:22
char version_dotted[1024]
Definition: version.sh.c:25
void write_version(FILE *)
Definition: version.sh.c:158
char * strip(char *)
Definition: version.sh.c:97
Command * c
Definition: ns_recover.c:17
long get_value(char *)
Definition: version.sh.c:107
char * get_value_str(char *)
Definition: version.sh.c:112
void load_ctrl(FILE *)
Definition: version.sh.c:72
char * version_extra
Definition: version.sh.c:23
char version[1024]
Definition: version.sh.c:24
long version_build
Definition: version.sh.c:22
long build
Definition: version.sh.c:22