src/conver.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
#include "conver.h"
conver_release_t history[0xFFFF];
uint16_t history_size = 0;
const char *versioning = "This project uses the Convergent Versioning system.\n\n"
"The version number is a 2-byte hexadecimal integer from 0x0000 to 0xFFFF, with the first three digits"
"indicating the project _dependability score_ and the last digit encoding the version impact, compatibility, and purpose. \n\n"
"A version number compliant with Semantic Versioning can be derived from the project history, "
"and will still be used if required by package managers or similar software. \n\n"
"For more information, see the [Convergent Versioning specification](https://h3rald.com/conver).\n";
#if defined(_WIN32) || defined(_WIN64)
# define FS_WINDOWS
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
#else
# define FS_POSIX
# include <errno.h>
# include <sys/stat.h>
#endif
int print_result(conver_result_t result)
{
if (result == 0)
{
fprintf(stdout, "-> %s\n", conver_errors[result]);
return result;
}
fprintf(stderr, "ERROR: %s\n", conver_errors[result]);
return result;
}
int fs_dir_exists(const char *path) {
#ifdef FS_WINDOWS
DWORD attr = GetFileAttributesA(path);
return (attr != INVALID_FILE_ATTRIBUTES) && (attr & FILE_ATTRIBUTE_DIRECTORY);
#else
struct stat st;
return (stat(path, &st) == 0) && S_ISDIR(st.st_mode);
#endif
}
int fs_file_exists(const char *path) {
#ifdef FS_WINDOWS
DWORD attr = GetFileAttributesA(path);
return (attr != INVALID_FILE_ATTRIBUTES) &&
!(attr & FILE_ATTRIBUTE_DIRECTORY);
#else
struct stat st;
return (stat(path, &st) == 0) && S_ISREG(st.st_mode);
#endif
}
conver_result_t fs_dir_create(const char *path, int ok_if_exists) {
#ifdef FS_WINDOWS
if (CreateDirectoryA(path, NULL)) {
return CONVER_OK;
}
if (ok_if_exists && GetLastError() == ERROR_ALREADY_EXISTS && fs_dir_exists(path))
{
return CONVER_OK;
}
return CONVER_ERR_MKDIR;
#else
if (mkdir(path, 0755) == 0) {
return CONVER_OK;
}
if (ok_if_exists && errno == EEXIST && fs_dir_exists(path))
{
return CONVER_OK;
}
return CONVER_ERR_MKDIR;
#endif
}
static conver_result_t fs_open_write(const char *path, const char *mode,
const void *data, size_t size) {
FILE *f = fopen(path, mode);
if (!f) return CONVER_ERR_OPEN;
if (size > 0) {
size_t written = fwrite(data, 1, size, f);
if (written != size) {
fclose(f);
return CONVER_ERR_WRITE;
}
}
fclose(f);
return CONVER_OK;
}
conver_result_t fs_file_create(const char *path) {
return fs_open_write(path, "w", NULL, 0);
}
conver_result_t fs_write(const char *path, const char *text) {
size_t len = 0;
const char *p = text;
while (*p++) len++;
return fs_open_write(path, "w", text, len);
}
conver_result_t fs_append(const char *path, const char *text) {
size_t len = 0;
const char *p = text;
while (*p++) len++;
return fs_open_write(path, "a", text, len);
}
static conver_result_t fs_read_all(const char *path, const char *mode, void **out_buf, size_t *out_len, int nul_term) {
*out_buf = NULL;
if (out_len) *out_len = 0;
FILE *f = fopen(path, mode);
if (!f) return CONVER_ERR_OPEN;
if (fseek(f, 0, SEEK_END) != 0) { fclose(f); return CONVER_ERR_READ; }
long file_size = ftell(f);
if (file_size < 0) { fclose(f); return CONVER_ERR_READ; }
rewind(f);
size_t alloc_size = (size_t)file_size + (nul_term ? 1 : 0);
unsigned char *buf = (unsigned char *)malloc(alloc_size ? alloc_size : 1);
if (!buf) { fclose(f); return CONVER_ERR_ALLOC; }
size_t bytes_read = fread(buf, 1, (size_t)file_size, f);
/* On Windows, text mode converts \r\n → \n so bytes_read <= file_size.
Check ferror *before* fclose while the FILE* is still valid. */
int read_err = ferror(f);
fclose(f);
if (read_err || (bytes_read == 0 && file_size > 0)) {
free(buf);
return CONVER_ERR_READ;
}
if (nul_term) buf[bytes_read] = '\0';
*out_buf = buf;
if (out_len) *out_len = bytes_read;
return CONVER_OK;
}
conver_result_t fs_read(const char *path, char **out_buf, size_t *out_len) {
return fs_read_all(path, "r", (void **)out_buf, out_len, 1);
}
static bool set_release_date(conver_release_t *r, uint16_t year, uint16_t month, uint16_t day) {
struct tm t = {0};
t.tm_year = year - 1900;
t.tm_mon = month - 1;
t.tm_mday = day;
struct tm original = t;
time_t date = mktime(&t);
if (date == (time_t)-1) { return false; }
r->date = t;
// If mktime normalized the fields, then the date is wrong
return t.tm_year == original.tm_year &&
t.tm_mon == original.tm_mon &&
t.tm_mday == original.tm_mday;
}
uint16_t score(uint16_t version)
{
return version >> 4;
}
uint16_t metadata(uint16_t version)
{
return version & 0x0F;
}
conver_stage_t stage(uint16_t version)
{
uint16_t vscore = score(version);
if (vscore >= 0x001 && vscore <= 0x400) { return CONVER_STAGE_PROTOTYPE; }
if (vscore >= 0x401 && vscore <= 0x800) { return CONVER_STAGE_OPERATIONAL; }
if (vscore >= 0x801 && vscore <= 0xC00) { return CONVER_STAGE_CONSOLIDATED; }
return CONVER_STAGE_BEDROCK;
}
conver_impact_t impact(uint16_t version)
{
uint16_t meta = metadata(version);
if (meta >= 0xC) { return CONVER_IMPACT_C; }
if (meta >= 0x8) { return CONVER_IMPACT_H; }
if (meta >= 0x4) { return CONVER_IMPACT_M; }
return CONVER_IMPACT_S;
}
conver_compatibility_t compatibility(uint16_t version)
{
uint16_t meta = metadata(version);
if (meta == 0x0 || meta == 0x1 || meta == 0x4 || meta == 0x5 ||
meta == 0x8 || meta == 0x9 || meta == 0xC || meta == 0xD) { return CONVER_COMPAT_PRES; }
return CONVER_COMPAT_BREAK;
}
conver_purpose_t purpose(uint16_t version)
{
uint16_t meta = metadata(version);
return (meta % 2) ? CONVER_PURPOSE_ENHANCE : CONVER_PURPOSE_MAINT;
}
uint16_t version(conver_release_t r)
{
return (r.score << 4) + r.metadata;
}
char *date(const struct tm d, char *dest, size_t dest_size)
{
snprintf(dest, dest_size, "%04d-%02d-%02d", d.tm_year + 1900, d.tm_mon + 1, d.tm_mday);
return dest;
}
int parse_hex(const char *str, uint16_t *out_val) {
if (str == NULL || *str == '\0') {
return CONVER_ERR_INVALID_VALUE;
}
char *endptr;
errno = 0;
uint16_t val = strtoul(str, &endptr, 16);
if (errno == ERANGE) {
return CONVER_ERR_INVALID_VALUE;
}
if (endptr == str) {
return CONVER_ERR_INVALID_VALUE;
}
while (*endptr != '\0') {
if (!isspace((unsigned char)*endptr)) {
return CONVER_ERR_INVALID_VALUE;
}
endptr++;
}
*out_val = val;
return CONVER_OK;
}
int parse_release(const char *line, conver_release_t *r) {
char sign;
char number[5];
uint16_t year, month, day;
/* Parse date, sign, and hex digits */
if (sscanf(line, "%hd-%hd-%hd: %c%4s", &year, &month, &day, &sign, number) != 5)
{
return print_result(CONVER_ERR_INVALID_RELEASE);
}
if (!set_release_date(r, year, month, day)) {
return print_result(CONVER_ERR_INVALID_DATE);
}
uint16_t version;
int result = parse_hex(number, &version);
if (result != CONVER_OK) {
return print_result(CONVER_ERR_INVALID_DATE);
}
r->score = score(version);
r->metadata = metadata(version);
if (sign == '+'){ r->widthdrawn = false; }
else if (sign == '-') { r->widthdrawn = true; }
else { return print_result(CONVER_ERR_INVALID_RELEASE); }
// Extract comment after '#'
r->comment[0] = '\0';
const char *hash = strchr(line, '#');
if (hash) {
// Skip the '#' and any leading spaces
hash++;
while (*hash == ' ') { hash++; };
strncpy(r->comment, hash, sizeof(r->comment) - 1);
r->comment[sizeof(r->comment) - 1] = '\0';
// Strip trailing newline and whitespace
int len = strlen(r->comment);
while (len > 0 && (r->comment[len-1] == '\n' ||
r->comment[len-1] == '\r' ||
r->comment[len-1] == ' ')) {
r->comment[--len] = '\0';
}
}
return 0;
}
void print_version(uint16_t version)
{
printf(" v%03X-%1X (Stage: %s)\n", score(version), metadata(version), conver_stages[stage(version)]);
printf(" Impact: %s - Compatibility: %s - Purpose: %s\n", conver_impacts[impact(version)], conver_compatibilities[compatibility(version)], conver_purposes[purpose(version)]);
}
void print_release(conver_release_t r)
{
char date_str[11];
printf("-- %s\n", date(r.date, date_str, sizeof(date_str)));
print_version(version(r));
printf(" %s\n\n", r.comment);
}
int parse_history() {
char *contents;
size_t contents_len = 0;
int result = fs_read(CONVER_HISTORY_FILE, &contents, &contents_len);
if (result != CONVER_OK) {
return print_result(result);
}
int count = 0;
const char *line = contents;
time_t timestamp = time(NULL);
while (*line && count < sizeof(history)) {
// Find end of line
const char *end = strchr(line, '\n');
size_t len = end ? (size_t)(end - line) : strlen(line);
// Copy line into a temporary buffer
char buf[512];
if (len < sizeof(buf)) {
memcpy(buf, line, len);
buf[len] = '\0';
result = parse_release(buf, &history[count]);
if (result != CONVER_OK) {
fprintf(stderr, "%s - Line %d: ", CONVER_HISTORY_FILE, count+1);
return print_result(result);
}
if (count > 0) {
// Check date follows previous date
if (timestamp <= mktime(&history[count-1].date))
{
fprintf(stderr, "%s - Line %d: ", CONVER_HISTORY_FILE, count+1);
return print_result(CONVER_ERR_PAST_DATE);
}
if (history[count].score <= history[count-1].score )
{
fprintf(stderr, "%s - Line %d: ", CONVER_HISTORY_FILE, count+1);
return print_result(CONVER_ERR_LOW_SCORE);
}
}
if (mktime(&history[count].date) > timestamp) {
fprintf(stderr, "%s - Line %d: ", CONVER_HISTORY_FILE, count+1);
return print_result(CONVER_ERR_FUTURE_DATE);
}
count++;
history_size = count;
}
if (!end) { break; }; // last line had no trailing newline
line = end + 1;
}
if (history_size > 0)
{
// Write release file
conver_release_t release = history[history_size-1];
char version_string[5];
snprintf(version_string, sizeof(version_string), "%03X%1X", release.score, release.metadata);
fs_write(CONVER_RELEASE_FILE, version_string);
}
return CONVER_OK;
}
void print_help()
{
printf("conver - Convergent Versioning Tool v%03X-%1X", score(CONVER_VERSION), metadata(CONVER_VERSION));
printf("\n");
printf("(c) Copyright 2026 Fabio Cevasco\n\n");
printf("SYNTAX\n");
printf(" conver [<flag> | <command>]\n\n");
printf(" Where:\n");
printf(" <flag> can be one of the following:\n");
printf(" -v, --version: Print the version of the program.\n");
printf(" -h, --help: Print this help message.\n\n");
printf(" <command> can be one of the following:\n");
printf(" init: Initialize the project for ConVer usage.\n");
printf(" draft: Draft a new release by specifying score and metadata.\n");
printf(" status: Display the current Draft and Release versions, if any.\n");
printf(" release: Move the current draft version to release.\n");
printf(" history: Print the full version history of the project.\n");
}
//// Commands
int command_init()
{
int write_all = 0;
int result = 0;
if (!fs_dir_exists(CONVER_DIR))
{
write_all = 1;
result = fs_dir_create(CONVER_DIR, 1);
if (result != CONVER_OK) {
print_result(result);
return result;
}
}
if (write_all || !fs_file_exists(CONVER_DRAFT_FILE))
{
printf("Creating %s...\n", CONVER_DRAFT_FILE);
result += print_result(fs_file_create(CONVER_DRAFT_FILE));
}
if (write_all || !fs_file_exists(CONVER_RELEASE_FILE))
{
printf("Creating %s...\n", CONVER_RELEASE_FILE);
result += print_result(fs_file_create(CONVER_RELEASE_FILE));
}
if (write_all || !fs_file_exists(CONVER_LEGACY_FILE))
{
printf("Creating %s...\n", CONVER_LEGACY_FILE);
result += print_result(fs_file_create(CONVER_LEGACY_FILE));
}
if (write_all || !fs_file_exists(CONVER_HISTORY_FILE))
{
printf("Creating %s...\n", CONVER_HISTORY_FILE);
result += print_result(fs_file_create(CONVER_HISTORY_FILE));
}
if (!fs_file_exists(CONVER_VERSIONING_FILE))
{
printf("Writing %s...\n", CONVER_VERSIONING_FILE);
result += print_result(fs_write(CONVER_VERSIONING_FILE, versioning));
}
return result;
}
int command_draft()
{
int result = parse_history();
if (result != CONVER_OK) {
return result;
}
char score[16];
uint16_t version;
int valid = 0;
while (!valid) {
printf("> Specify the dependability score of the release [000-FFF]: ");
fgets(score, sizeof(score), stdin);
if (strlen(score) > 4) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
int invalid_digit = 0;
for (int i=0; i<strlen(score)-1; i++){
if (!isxdigit(score[i])){
invalid_digit = 1;
printf("Invalid: %c", score[i]);
break;
}
}
if (invalid_digit) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
result = parse_hex(score, &version);
if (result != CONVER_OK) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
if (history_size > 0)
{
uint16_t last_release = history[history_size-1].score;
if ((last_release) > version) {
print_result(CONVER_ERR_LOW_SCORE);
continue;
}
}
valid = 1;
}
version = version << 4; // left shift to make room for metadata nibble
valid = 0;
char impact[16];
while (!valid) {
printf("> Specify the impact of the release [L, M, H, C]: ");
fgets(impact, sizeof(impact), stdin);
if (strlen(impact)> 2) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
if (impact[0] == 'l' || impact[0] == 'L') {
version += 0x3;
}
else if (impact[0] == 'm' || impact[0] == 'M') {
version += 0x7;
}
else if (impact[0] == 'h' || impact[0] == 'H') {
version += 0xB;
}
else if (impact[0] == 'c' || impact[0] == 'C') {
version += 0xF;
}
else {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
valid = 1;
}
valid = 0;
char breaking[16];
while (!valid) {
printf("> Is this release breaking compatibility? [y/n]: ");
fgets(breaking, sizeof(breaking), stdin);
if (strlen(breaking)> 2) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
if (breaking[0] == 'n' || breaking[0] == 'N') {
version -= 0x2;
}
else if (breaking[0] != 'y' && breaking[0] != 'Y')
{
printf("Breaking: %s", breaking);
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
valid = 1;
}
valid = 0;
char enhancement[16];
while (!valid) {
printf("> Is this release adding new enhancements or features? [y/n]: ");
fgets(enhancement, sizeof(enhancement), stdin);
if (strlen(enhancement)> 2) {
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
if (enhancement[0] == 'n' || enhancement[0] == 'N') {
version -= 0x1;
}
else if (enhancement[0] != 'y' && enhancement[0] != 'Y')
{
print_result(CONVER_ERR_INVALID_VALUE);
continue;
}
valid = 1;
}
printf("Saving new draft version: %04X\n", version);
char version_string[5];
snprintf(version_string, sizeof(version_string), "%04X", version);
result = fs_write(CONVER_DRAFT_FILE, version_string);
print_result(result);
memset(version_string, 0, sizeof(version_string));
memset(score, 0, sizeof(score));
memset(impact, 0, sizeof(impact));
memset(breaking, 0, sizeof(breaking));
memset(enhancement, 0, sizeof(enhancement));
return result;
}
int command_release()
{
int result = parse_history();
if (result != CONVER_OK) {
return result;
}
char *draft_string;
size_t draft_len = 0;
result = fs_read(CONVER_DRAFT_FILE, &draft_string, &draft_len);
if (result != CONVER_OK) {
return print_result(result);
}
if (strlen(draft_string) != 4) {
return print_result(CONVER_ERR_NO_DRAFT);
}
uint16_t draft;
result = parse_hex(draft_string, &draft);
if (result != CONVER_OK) {
return print_result(CONVER_ERR_INVALID_DRAFT);
}
time_t timestamp = time(NULL);
struct tm *local_time = localtime(×tamp);
if (history_size > 0)
{
if (timestamp <= mktime(&history[history_size-1].date))
{
return print_result(CONVER_ERR_PAST_DATE);
}
if (score(draft) <= history[history_size-1].score)
{
return print_result(CONVER_ERR_LOW_SCORE);
}
}
char date[11];
char comment[256];
strftime(date, sizeof(date), "%Y-%m-%d", local_time);
printf("> Enter a brief comment/note for this release (max 255 chars): ");
fgets(comment, sizeof(comment), stdin);
char release[280];
snprintf(release, sizeof(release), "%s: +%04X # %s", date, draft, comment);
result = fs_append(CONVER_HISTORY_FILE, release);
if (result != CONVER_OK) {
return print_result(result);
}
result = fs_write(CONVER_RELEASE_FILE, draft_string);
if (result != CONVER_OK) {
return print_result(result);
}
// Clear draft;
fs_file_create(CONVER_DRAFT_FILE);
printf(release);
memset(date, 0, sizeof(date));
memset(comment, 0, sizeof(comment));
memset(release, 0, sizeof(release));
return 0;
}
int command_status()
{
int result = parse_history();
if (result != CONVER_OK) {
return result;
}
uint16_t draft = 0;
uint16_t release = 0;
char *draft_string;
char *release_string;
size_t draft_len = 0;
size_t release_len = 0;
result = fs_read(CONVER_DRAFT_FILE, &draft_string, &draft_len);
if (result != CONVER_OK) {
return print_result(result);
}
if (strlen(draft_string) == 4) {
result = parse_hex(draft_string, &draft);
if (result != CONVER_OK) {
return print_result(CONVER_ERR_INVALID_DRAFT);
}
}
result = fs_read(CONVER_RELEASE_FILE, &release_string, &release_len);
if (result != CONVER_OK) {
return print_result(result);
}
if (strlen(release_string) == 4) {
result = parse_hex(release_string, &release);
if (result != CONVER_OK) {
return print_result(CONVER_ERR_INVALID_RELEASE);
}
}
if (!release && !draft)
{
printf("No current release available.\n");
printf("No draft release available.\n");
}
if (release) {
printf("Current Release:\n");
print_version(release);
}
if (draft) {
printf("Draft Release:\n");
print_version(draft);
}
return CONVER_OK;
}
int command_history()
{
int result = parse_history();
if (result != CONVER_OK){
return result;
}
if (history_size == 0) {
printf("No project history available.\n");
return CONVER_OK;
}
for (int i=0; i<history_size; i++)
{
print_release(history[i]);
}
return CONVER_OK;
}
//// Main
int main(int argc, char *argv[])
{
if (argc > 1)
{
for (int i = 1; i < argc; i++)
{
char *arg = argv[i];
if ((strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0))
{
printf("%04X\n", CONVER_VERSION);
return 0;
}
if ((strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0))
{
print_help();
return 0;
}
if ((strcmp(arg, "init") == 0))
{
return command_init();
}
if ((strcmp(arg, "draft") == 0))
{
return command_draft();
}
if ((strcmp(arg, "release") == 0))
{
return command_release();
}
if ((strcmp(arg, "status") == 0))
{
return command_status();
}
if ((strcmp(arg, "history") == 0))
{
return command_history();
}
}
}
print_help();
}
|