fix to build with pedantic flags
[AROS.git] / tools / setrev / setrev.c
blobbc11bb8471f604ed78c5264121aad7961b76d31a
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 *** Author: Jochen Wiedmann
11 *** Am Eisteich 9
12 *** 72555 Metzingen
13 *** Germany
14 ***
15 *** Phone: (49)7123 / 14881
16 *** Internet: jochen.wiedmann@zdv.uni-tuebingen.de
17 ***
18 ***
19 *** This program is in the public domain; use it as you want,
20 *** but WITHOUT ANY WARRANTY!
21 **/
24 /**
25 *** These are the patterns that will be replaced.
26 **/
27 #define VERSION 1
28 #define REVISION 4
29 #define DATE "11.05.2011"
30 #define VERS "SetRev 1.4"
31 #define VSTRING "SetRev 1.4 (11.05.2011)"
32 #define VERSTAG "\0$VER: SetRev 1.4 (11.05.2011)"
33 const char *const VersionString=VERSTAG;
38 /**
39 *** Include files
40 **/
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44 #include <time.h>
45 #include <errno.h>
47 #if defined(__GNUC__)
48 #define stricmp strcasecmp
49 #define strnicmp strncasecmp
50 #endif
53 #ifndef FALSE
54 #define FALSE 0
55 #endif
56 #ifndef TRUE
57 #define TRUE (!FALSE)
58 #endif
64 /**
65 *** SetRev cannot scan files with lines longer than MAXLINE character.
66 **/
67 #define MAXLINE 4096
72 /**
73 *** Global variables
74 **/
75 int CurrentVersion = 1;
76 int CurrentRevision = 1;
77 char *CurrentName;
78 char CurrentDate[20];
80 char line[MAXLINE];
82 int Created = FALSE;
86 /**
87 *** This function determines the current name, version
88 *** and revision.
89 **/
90 void FirstScan(char *file)
93 FILE *fh;
94 int linenum = 1;
96 if (!(fh = fopen(file, "r")))
98 if (errno == ENOENT)
101 *** File doesn't exist, create it.
103 if (!(fh = fopen(file, "w")))
105 perror("SetRev");
106 exit(10);
109 if (fprintf(fh,
110 "#define VERSION %d\n",
111 CurrentVersion) < 0 ||
112 fprintf(fh,
113 "#define REVISION %d\n",
114 CurrentRevision) < 0 ||
115 fprintf(fh,
116 "#define DATE \"%s\"\n",
117 CurrentDate) < 0 ||
118 fprintf(fh,
119 "#define VERS \"%s %d.%d\"\n",
120 CurrentName, CurrentVersion,
121 CurrentRevision) < 0 ||
122 fprintf(fh,
123 "#define VSTRING \"%s %d.%d (%s)\\r\\n\"\n",
124 CurrentName, CurrentVersion,
125 CurrentRevision, CurrentDate) < 0 ||
126 fprintf(fh,
127 "#define VERSTAG \"\\0%sVER: %s %d.%d (%s)\"\n",
128 "$", /* Prevent "version" command from using the */
129 /* above string. */
130 CurrentName, CurrentVersion,
131 CurrentRevision, CurrentDate) < 0 ||
132 fclose(fh))
134 perror("SetRev");
135 exit(10);
138 Created = TRUE;
140 return;
142 perror("SetRev");
143 exit(10);
146 errno = 0;
147 while(fgets(line, MAXLINE, fh) && !errno)
149 int len = strlen(line);
151 if (len+1 == MAXLINE && line[MAXLINE-2] != '\n')
153 fprintf(stderr,
154 "SetRev warning: Line %d exceeds %d characters\n",
155 linenum, MAXLINE);
158 if (strncmp(line, "#define", 7) == 0)
160 char *ptr = line+7;
162 while (*ptr == ' ' || *ptr == '\t')
164 ++ptr;
167 if (strncmp(ptr, "VERSION", 7) == 0 &&
168 (ptr[7] == ' ' || ptr[7] == '\t'))
170 CurrentVersion = atoi(&ptr[7]);
172 else if (strncmp(ptr, "REVISION", 8) == 0 &&
173 (ptr[8] == ' ' || ptr[8] == '\t'))
175 CurrentRevision = atoi(&ptr[8]);
179 if (line[len-1] == '\n')
181 ++linenum;
184 fclose(fh);
192 *** This function inserts the new version, revision,
193 *** projectname and other stuff in a second scan.
195 void SecondScan(char *file)
198 char *bakfile;
199 FILE *fh, *bfh;
200 int linenum = 1;
202 if (!(bakfile = malloc(strlen(file)+5)))
204 perror("SetRev");
205 exit(10);
207 strcpy(bakfile, file);
208 strcat(bakfile, ".bak");
210 if (!(fh = fopen(file, "r")) || !(bfh = fopen(bakfile, "w")))
212 perror("SetRev");
213 exit(10);
216 errno = 0;
217 while(fgets(line, MAXLINE, fh) && !errno)
219 int len = strlen(line);
221 if (len+1 == MAXLINE && line[MAXLINE-2] != '\n')
223 fprintf(stderr,
224 "SetRev warning: Line %d exceeds %d characters\n",
225 linenum, MAXLINE);
228 if (strncmp(line, "#define", 7) == 0)
230 char *ptr = line+7;
232 while (*ptr == ' ' || *ptr == '\t')
234 ++ptr;
237 if (strncmp(ptr, "VERSION", 7) == 0 &&
238 (ptr[7] == ' ' || ptr[7] == '\t'))
240 ptr += 7;
241 while (*ptr == ' ' || *ptr == '\t')
243 ++ptr;
245 sprintf(ptr, "%d\n", CurrentVersion);
247 else if (strncmp(ptr, "REVISION", 8) == 0 &&
248 (ptr[8] == ' ' || ptr[8] == '\t'))
250 ptr += 8;
251 while (*ptr == ' ' || *ptr == '\t')
253 ++ptr;
255 sprintf(ptr, "%d\n", CurrentRevision);
257 else if (strncmp(ptr, "DATE", 4) == 0 &&
258 (ptr[4] == ' ' || ptr[4] == '\t'))
260 ptr += 4;
261 while (*ptr == ' ' || *ptr == '\t')
263 ++ptr;
265 sprintf(ptr, "\"%s\"\n", CurrentDate);
267 else if (strncmp(ptr, "VERS", 4) == 0 &&
268 (ptr[4] == ' ' || ptr[4] == '\t'))
270 ptr += 4;
271 while (*ptr == ' ' || *ptr == '\t')
273 ++ptr;
275 sprintf(ptr, "\"%s %d.%d\"\n", CurrentName,
276 CurrentVersion, CurrentRevision);
278 else if (strncmp(ptr, "VSTRING", 7) == 0 &&
279 (ptr[7] == ' ' || ptr[7] == '\t'))
281 ptr += 7;
282 while (*ptr == ' ' || *ptr == '\t')
284 ++ptr;
286 sprintf(ptr, "\"%s %d.%d (%s)\"\n",
287 CurrentName, CurrentVersion,
288 CurrentRevision, CurrentDate);
290 else if (strncmp(ptr, "VERSTAG", 7) == 0 &&
291 (ptr[7] == ' ' || ptr[7] == '\t'))
293 ptr += 7;
294 while (*ptr == ' ' || *ptr == '\t')
296 ++ptr;
298 sprintf(ptr, "\"\\0%sVER: %s %d.%d (%s)\"\n",
299 "$",
300 CurrentName, CurrentVersion,
301 CurrentRevision, CurrentDate);
304 fputs(line, bfh);
306 fclose(fh);
307 fclose(bfh);
309 remove(file);
310 rename(bakfile, file);
318 *** This prints out the Usage: message.
320 void Usage(void)
323 printf("Usage: SetRev PROJECT/A,VERSION/N,FILE/K\n\n");
324 printf("The given FILE (default: PROJECT_rev.h) will be searched for\n");
325 printf("version and revision definitions and bumped to the next\n");
326 printf("revision number.\n\n");
327 printf("%s %c 1994 by Jochen Wiedmann\n\n", VSTRING, 0xa9);
328 printf("This program is in the public domain, use it as you want, but\n");
329 printf("WITHOUT ANY WARRANTY.\n");
330 exit(5);
338 *** Finally main()
340 int main(int argc, char *argv[])
343 int i;
344 char *file;
345 int versionseen, version;
346 time_t currenttime = time(NULL);
347 struct tm *localtm = localtime(&currenttime);
350 *** Get local time
352 currenttime = time(NULL);
353 localtm = localtime(&currenttime);
354 strftime(CurrentDate, sizeof(CurrentDate), "%d.%m.%Y", localtm);
356 if (argc < 2 ||
357 strcmp(argv[1], "?") == 0 ||
358 strcmp(argv[1], "-h") == 0 ||
359 strcmp(argv[1], "--help") == 0)
361 Usage();
364 CurrentName = NULL;
365 versionseen = FALSE;
366 file = NULL;
367 for (i = 1; i < argc; i++)
369 if (stricmp(argv[i], "file") == 0 && i+1 < argc)
371 if (file)
373 Usage();
375 file = argv[++i];
377 else if (strnicmp(argv[i], "file=", 5) == 0)
379 if (file)
381 Usage();
383 file = &argv[i][5];
385 else if (!CurrentName)
387 CurrentName = argv[i];
389 else if (!versionseen)
391 if ((version = atoi(argv[i])) == 0)
393 Usage();
395 versionseen = TRUE;
397 else
399 Usage();
402 if (!CurrentName)
404 Usage();
407 if (!file)
409 if (!(file = malloc(strlen(CurrentName) + 7)))
411 errno = ENOMEM;
412 perror("SetRev");
413 exit(20);
415 strcpy(file, CurrentName);
416 strcat(file, "_rev.h");
419 if (versionseen)
421 CurrentVersion = version;
424 FirstScan(file);
426 if (!Created)
428 if (versionseen && version != CurrentVersion)
430 CurrentRevision = 1;
432 else
434 ++CurrentRevision;
437 if (versionseen)
439 CurrentVersion = version;
442 SecondScan(file);
444 exit(0);