all repos — conver-tool @ 8acf0b3eb894338c22c741a661f6d315f00cc738

A command line tool to manage ConVer projects.

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
#include "conver.h"

#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_file_create_b(const char *path) {
    return fs_open_write(path, "wb", 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_write_b(const char *path, const void *data, size_t size) {
    return fs_open_write(path, "wb", data, size);
}

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);
}

conver_result_t fs_append_b(const char *path, const void *data, size_t size) {
    return fs_open_write(path, "ab", data, size);
}

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);
}

conver_result_t fs_read_b(const char *path, unsigned char **out_buf, size_t *out_len) {
    return fs_read_all(path, "rb", (void **)out_buf, out_len, 0);
}

//// 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));
    }
    return result;
}

int command_draft()
{
    char score[16];
    uint16_t version;
    int valid = 0;
    while (!valid) {
        printf("> Specify the dependability score of the release [001-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;
        }
        version = strtoul(score, NULL, 16);
        if (!version) {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        valid = 1;
    }
    version = version << 4; // left shift to make room for metadata nibble
    valid = 0;
    char size[16];
    while (!valid) {
        printf("> Specify the size of the release [S, M, L, X]: ");
        fgets(size, sizeof(size), stdin);
        if (strlen(size)> 2) {
            print_result(CONVER_ERR_INVALID_VALUE);
            continue;
        }
        if (size[0] == 's' || size[0] == 'S') {
            version += 0x3;
        }
        else if (size[0] == 'm' || size[0] == 'M') {
            version += 0x7;
        }
        else if (size[0] == 'l' || size[0] == 'L') {
            version += 0xB;
        }
        else if (size[0] == 'x' || size[0] == 'X') {
            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);
    int result = fs_write(CONVER_DRAFT_FILE, version_string);
    print_result(result);
    memset(version_string, 0, sizeof(version_string));
    memset(score, 0, sizeof(score));
    memset(size,0,sizeof(size));
    memset(breaking,0,sizeof(breaking));
    memset(enhancement,0,sizeof(enhancement));
    return result;
}

int command_release() 
{
    char *draft_string;
    size_t draft_len = 0;
    int 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 = strtoul(draft_string, NULL, 16);
    if (!draft) {
        return print_result(CONVER_ERR_NO_DRAFT);
    }
    time_t raw_time = time(NULL);
    struct tm *local_time = localtime(&raw_time);
    char date[11];
    char comment[255];
    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[275];
    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);
    }
    printf(release);
    return 0;
}

//// 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, "init") == 0))
            {
                return command_init();
            }
            if ((strcmp(arg, "draft") == 0))
            {
                return command_draft();
            }
            if ((strcmp(arg, "release") == 0))
            {
                return command_release();
            }
        }
    }
}