/* * versmr.h * * Copyright 2026 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * * */ #if (!defined(__LIB_versmr_H_)) #define __LIB_versmr_H_ #include #include #include #define true 1 #define false 0 typedef unsigned char bool_t; #define VERSMR_VERSION "1.0" static void __help(void) { puts( "Console VERSMR V" VERSMR_VERSION "\n" " " "Simple version control" "\n" "Using:" "\n" " " "versmr [-v/--version] - view versmr version" "\n" " " "versmr [-h/--help] - show this help" "\n" " " "versmr [-s/--set] [version] - init version" "\n" "To update version, call (one of the options):" "\n" " " "bash .versmr.bash" "\n" " " "make -f versup.mk" "\n" " " "chmod +x .versmr.bash && ./.versmr.bash" "\n" "To set version:" "\n" " " "versmr --set VERSION" "\0" ); } static void versmr_error(char* _err) { printf("\033[1mversmr: \033[91m%s\033[0m\n", _err); } static int __versmr_begin(char* version) { FILE* bs = fopen(".versmr.bash", "w"); if (bs == NULL) { versmr_error("Unable to find configuration file!"); return EXIT_FAILURE; } fputs("#!/bin/bash\nvers=$(cat .versmr)\nvers=$(($vers+1))\necho $vers > .versmr", bs); fclose(bs); FILE* mk = fopen("versup.mk", "w"); if (mk == NULL) { versmr_error("Unable to find configuration file!"); return EXIT_FAILURE; } fputs("all:\n\tbash .versmr.bash\n", mk); fclose(mk); FILE* fp = fopen(".versmr", "w"); if (fp == NULL) { versmr_error("Unable to find configuration file!"); return EXIT_FAILURE; } unsigned long vers = 0; if (sscanf(version, "%lu", &vers) == 0) { versmr_error("Incorrect version format!"); fclose(fp); return EXIT_FAILURE; }; fprintf(fp, "%lu", vers); fclose(fp); return EXIT_SUCCESS; } static int __get_version(void) { FILE* fp = fopen(".versmr", "r"); if (fp == NULL) { versmr_error("Unable to find configuration file!"); return EXIT_FAILURE; } unsigned long vers = 0; if (fscanf(fp, "%lu", &vers) == 0) { fclose(fp); versmr_error("Unable to find configuration file!"); return EXIT_FAILURE; }; fprintf(fp, "%lu", vers); fclose(fp); return EXIT_SUCCESS; } int _lib_versmr_main(int argc, char** argv) { unsigned int pos = 1; bool_t get_name = false; char* set_version = NULL; while (pos != argc) { char* str = argv[pos]; if (str[0] == '-') { bool_t cn_size = false; if (str[1] == '-') cn_size = true; switch (str[1 + cn_size]) { case 'e': return __get_version(); case 'h': __help(); return EXIT_SUCCESS; case 'v': puts(VERSMR_VERSION); return EXIT_SUCCESS; case 's': get_name = true; break; default: versmr_error("Unknown argument!"); return EXIT_FAILURE; } } else { if (get_name) set_version = str; else set_version = str; get_name = false; } pos ++; } if (get_name == true) { versmr_error("Cannot find option in \"--set\" argument!"); return EXIT_FAILURE; } if (set_version == NULL) set_version = "1"; return __versmr_begin(set_version); } #endif /* __LIB_versmr_H_ */