int main(int argc, char *argv[]) { char *revdb_file = "revdb"; char *verdb_file = "verdb"; char *version_file = "VERSION"; char *output_file = "version.c"; char *build_file = ".build"; int i = 1, r; unsigned long summ = 0, oldsumm; int major, minor; int revsumm, patch; int enable_new_version = 0; int disable_patch_inc = 0; int has_norev = 0; int build; int use_cvs = 0; int do_not_commit_revdb = 0; if (argc == 1) report_usage(); while (1) { if (!strcmp(argv[i], "-r")) { if (++i >= argc) { fprintf(stderr, "option '-r' expects an argument\n"); exit(1); } revdb_file = argv[i++]; } else if (!strcmp(argv[i], "-d")) { if (++i >= argc) { fprintf(stderr, "option '-d' expects an argument\n"); exit(1); } verdb_file = argv[i++]; } else if (!strcmp(argv[i], "-v")) { if (++i >= argc) { fprintf(stderr, "option '-v' expects an argument\n"); exit(1); } version_file = argv[i++]; } else if (!strcmp(argv[i], "-V")) { if (++i >= argc) { fprintf(stderr, "option '-V' expects an argument\n"); exit(1); } version_file = 0; strcpy(major_version, argv[i++]); } else if (!strcmp(argv[i], "-b")) { if (++i >= argc) { fprintf(stderr, "option '-b' expects an argument\n"); exit(1); } build_file = argv[i++]; } else if (!strcmp(argv[i], "-P")) { if (++i >= argc) { fprintf(stderr, "option '-P' expects an argument\n"); exit(1); } variable_prefix = argv[i++]; } else if (!strcmp(argv[i], "-o")) { if (++i >= argc) { fprintf(stderr, "option '-o' expects an argument\n"); exit(1); } output_file = argv[i++]; } else if (!strcmp(argv[i], "-n")) { i++; enable_new_version = 1; } else if (!strcmp(argv[i], "-p")) { i++; disable_patch_inc = 1; } else if (!strcmp(argv[i], "-C")) { i++; use_cvs = 1; } else if (!strcmp(argv[i], "-S")) { i++; svn_mode = 1; } else if (!strcmp(argv[i], "-x")) { i++; do_not_commit_revdb = 1; } else { break; } } if (i >= argc) report_usage(); if (getenv("REVINFO_NO_COMMIT")) do_not_commit_revdb = 1; if (version_file) read_version(version_file); read_revdb(revdb_file); read_verdb(verdb_file); for (; i < argc; i++) { r = scan_file(argv[i], &major, &minor, &summ); if (r < 0) return 1; /* FIXME: is it ok? */ if (r == 0) { /* no revision information */ add_files(argv[i], 0, 0, 1); has_norev = 1; } else { add_files(argv[i], major, minor, 0); r = lookup_revdb(argv[i], major, minor, &oldsumm); if (!r) { fprintf(stderr,"%s,v %d.%d not in database\n", argv[i], major, minor); add_revdb_entry(argv[i], major, minor, summ); revdb_updated = 1; } else { if (summ != oldsumm) { fprintf(stderr,"%s,v %d.%d is modified\n", argv[i], major, minor); has_modified = 1; } } } } /* scan for missing files */ for (i = 0; i < revdb_u; i++) { int j, max_major, max_minor, f_major, f_minor, f_norev; if (revdb[i].flag) continue; /* already seen */ revdb[i].flag = 1; max_major = revdb[i].major; max_minor = revdb[i].minor; for (j = i + 1; j < revdb_u; j++) { if (revdb[j].flag) continue; if (strcmp(revdb[i].file, revdb[j].file)) continue; revdb[j].flag = 1; if (revdb[j].major > max_major) { max_major = revdb[j].major; max_minor = revdb[j].minor; } else if (revdb[j].major == max_major && revdb[j].minor > max_minor) { max_minor = revdb[j].minor; } } if (!lookup_files(revdb[i].file, &f_major, &f_minor, &f_norev)) { fprintf(stderr, "%s,v %d.%d deleted?\n", revdb[i].file, max_major, max_minor); //return 1; /* FIXME */ } else { if (f_norev) { fprintf(stderr, "%s,v %d.%d replaced with new file?\n", revdb[i].file, max_major, max_minor); return 1; /* FIXME */ } if (f_major < max_major || (f_major == max_major && f_minor < max_minor)) { fprintf(stderr, "%s,v %d.%d decreased revision number\n", revdb[i].file, max_major, max_minor); return 1; /* FIXME */ } } } /* calculate revision summ */ if (svn_mode) { revsumm = -1; for (i = 0; i < files_u; i++) { if (!files[i].norev && revsumm < files[i].major) revsumm = files[i].major; } if (revdb_major > revsumm) revsumm = revdb_major; printf("max revision: %d\n", revsumm); } else { revsumm = 0; for (i = 0; i < files_u; i++) { if (files[i].norev) { /*revsumm++;*/ } else revsumm += files[i].major + files[i].minor; } printf("revision summ: %d\n", revsumm); } if (!lookup_verdb(major_version, revsumm, &patch) && patch == -1 && !enable_new_version) { strcat(major_version, "pre"); enable_new_version = 1; } if (!lookup_verdb(major_version, revsumm, &patch)) { /* no version corresponds to the given revsumm */ if (patch == -1) { /* don't even have this major */ if (has_modified || has_norev || disable_patch_inc) { /* probably we're in preparation for new version */ /* delay new version until files are checked in */ read_build(build_file, &build); build++; write_build(build_file, build); sprintf(version_string, "<new version> #%d", build); } else { patch = first_patch(major_version); add_verdb(major_version, patch, revsumm, time(0)); verdb_updated = 1; write_build(build_file, 0); make_full_version(major_version, patch); sprintf(version_string, "%s", full_version); } } else { if (!has_modified && !has_norev && disable_patch_inc && use_cvs && svn_mode) { write_build(build_file, 0); make_full_version(major_version, patch); sprintf(version_string, "%s+ (SVN r%d)", full_version, revsumm); } else if ((has_modified || has_norev) && disable_patch_inc && use_cvs && svn_mode) { read_build(build_file, &build); build++; write_build(build_file, build); make_full_version(major_version, patch); sprintf(version_string, "%s+ (SVN r%d) #%d", full_version, revsumm, build); } else if (has_modified || has_norev || disable_patch_inc) { read_build(build_file, &build); build++; write_build(build_file, build); make_full_version(major_version, patch); sprintf(version_string, "%s #%d", full_version, build); } else { /* increase a minor */ patch++; add_verdb(major_version, patch, revsumm, time(0)); verdb_updated = 1; write_build(build_file, 0); make_full_version(major_version, patch); sprintf(version_string, "%s", full_version); } } } else { if (has_modified || has_norev) { /* a modified version */ read_build(build_file, &build); build++; write_build(build_file, build); make_full_version(major_version, patch); sprintf(version_string, "%s #%d", full_version, build); } else { /* got stock version */ make_full_version(major_version, patch); sprintf(version_string, "%s", full_version); } } write_output(output_file); if (revdb_updated) { write_revdb(revdb_file); if (use_cvs && !do_not_commit_revdb) { if (svn_mode) { sprintf(cmdline, "svn ci -m \"\" %s", revdb_file); } else { sprintf(cmdline, "cvs ci -m \"\" %s", revdb_file); } printf("doing: %s\n", cmdline); system(cmdline); } } if (verdb_updated) { write_verdb(verdb_file); if (use_cvs) { if (svn_mode) { sprintf(cmdline, "svn ci -m \"\" %s", verdb_file); } else { sprintf(cmdline, "cvs ci -m \"\" %s", verdb_file); } printf("doing: %s\n", cmdline); system(cmdline); } } return 0; }
static void parse_args(int argc, char **argv) { int err = 0; int c; char *temp_pid_file = NULL; char temp_buf[32]; /* 32 bytes should be more than enough */ opt_no_banner = 1; opt_foreground = 0; opt_stderr_loglevel = -1; opt_file_loglevel = -1; opt_passwd_filename = NULL; opt_log_filename = NULL; opt_active_filename = NULL; opt_cl_listen_port = -1; opt_pid_file[0] = '\0'; opt_fbs_prefix = NULL; opt_join_sessions = 0; opt_bind_ip = NULL; opt_request_tight = 0; opt_tight_level = -1; while (!err && (c = getopt(argc, argv, "hqjv:f:p:a:c:g:l:i:s:b:tT:")) != -1) { switch (c) { case 'h': err = 1; break; case 'q': opt_no_banner = 1; break; case 'j': opt_join_sessions = 1; break; case 'v': if (opt_file_loglevel != -1) err = 1; else opt_file_loglevel = atoi(optarg); break; case 'f': opt_foreground = 1; if (opt_stderr_loglevel != -1) err = 1; else opt_stderr_loglevel = atoi(optarg); break; case 'p': if (opt_passwd_filename != NULL) err = 1; else opt_passwd_filename = optarg; break; case 'g': if (opt_log_filename != NULL) err = 1; else opt_log_filename = optarg; break; case 'a': if (opt_active_filename != NULL) err = 1; else opt_active_filename = optarg; break; case 'c': if (opt_actions_filename != NULL) err = 1; else opt_actions_filename = optarg; break; case 'l': if (opt_cl_listen_port != -1) err = 1; else { opt_cl_listen_port = atoi(optarg); if (opt_cl_listen_port <= 0) err = 1; } break; case 'i': if (temp_pid_file != NULL) err = 1; else temp_pid_file = optarg; break; case 's': if (opt_fbs_prefix != NULL) err = 1; else opt_fbs_prefix = optarg; break; case 'b': if (opt_bind_ip != NULL) err = 1; else opt_bind_ip = optarg; break; case 't': if (opt_request_tight) err = 1; else opt_request_tight = 1; break; case 'T': if (opt_request_tight) { err = 1; } else { opt_request_tight = 1; opt_tight_level = atoi(optarg); if (opt_tight_level <= 0 || opt_tight_level > 9) err = 1; } break; default: err = 1; } } /* Print usage help on error */ if (err || optind != argc - 1) { report_usage(argv[0]); exit(1); } /* Provide reasonable defaults for some options */ if (opt_file_loglevel == -1) opt_file_loglevel = LL_INFO; if (opt_passwd_filename == NULL) opt_passwd_filename = "passwd"; if (opt_log_filename == NULL) opt_log_filename = "reflector.log"; if (opt_cl_listen_port == -1) opt_cl_listen_port = 5999; /* Append listening port number to pid filename */ if (temp_pid_file != NULL) { sprintf(temp_buf, "%d", opt_cl_listen_port); sprintf(opt_pid_file, "%.*s.%s", (int)(255 - strlen(temp_buf) - 1), temp_pid_file, temp_buf); } /* Save pointer to host info filename */ opt_host_info_file = argv[optind]; }