refactor: Purge library testing CLI, design CLI contract target

This commit is contained in:
Rachel 2024-12-29 00:09:11 -08:00
parent 48a243f18e
commit eb5553c353
8 changed files with 133 additions and 102 deletions

View File

@ -27,7 +27,8 @@ LIBSRC = $(foreach api,$(LIBAPI),$(wildcard lib/src/$(api)/*.c))
LIBOBJ = $(LIBSRC:.c=.o)
LIBDEP = $(LIBSRC:.c=.d)
CLISRC = $(wildcard cli/*.c)
CLIINC = $(wildcard cli/include/*.h)
CLISRC = $(wildcard cli/src/*.c)
CLIOBJ = $(CLISRC:.c=.o)
CLIDEP = $(CLISRC:.c=.d)
@ -59,6 +60,7 @@ clean:
$(CLITARGET): LDFLAGS += -Wl,-rpath=$(CURDIR)
$(CLITARGET): LDFLAGS += -L$(CURDIR)
$(CLITARGET): LDFLAGS += -l$(CWD_BASE)
$(CLITARGET): CFLAGS += -Icli/include
$(CLITARGET): $(CLIOBJ)
$(CC) $(LDFLAGS) -o $@ $^

15
cli/include/command.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef NARC_COMMAND_H
#define NARC_COMMAND_H
struct command {
const char *abbrev;
const char *name;
int (*main)(int argc, const char **argv);
};
int create(int argc, const char **argv);
int extract(int argc, const char **argv);
int yank(int argc, const char **argv);
int info(int argc, const char **argv);
#endif // NARC_COMMAND_H

View File

@ -1,101 +0,0 @@
/*
* Copyright 2024 <lhearachel@proton.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <narc.h>
#include "api/pack.h"
#include "defs/vfs.h"
int main(void)
{
struct narc *narc = NULL;
struct vfs_ctx vfs_ctx;
enum narc_error err = narc_load("test.narc", &narc, &vfs_ctx);
if (err == NARCERR_ERRNO) {
fprintf(stderr, "System error: %s\n", strerror(errno));
fflush(stderr);
return EXIT_FAILURE;
} else if (err != NARCERR_NONE) {
fprintf(stderr, "NARC error: %s\n", narc_strerror(err));
fflush(stderr);
return EXIT_FAILURE;
}
printf("Loaded NARC successfully!\n");
printf("NARC size: %d\n", narc->size);
printf("NARC data sample:");
for (int i = 0; i < 16; i++) {
printf(" %02x", narc->vfs[i]);
}
printf("\n");
printf("FATB offset: 0x%02x\n", vfs_ctx.fatb_ofs);
printf("FNTB offset: 0x%02x\n", vfs_ctx.fntb_ofs);
printf("FIMG offset: 0x%02x\n", vfs_ctx.fimg_ofs);
printf("VFS size: 0x%02x\n", vfs_ctx.vfs_size);
printf("Trying to dump contents...\n");
err = narc_dump(narc, &vfs_ctx, "test.narc.d");
if (err != NARCERR_NONE) {
fprintf(stderr, "NARC error: %s\n", narc_strerror(err));
fflush(stderr);
return EXIT_FAILURE;
}
printf("Starting re-pack...\n");
struct vfs_pack_ctx *pack_ctx = narc_pack_start();
for (int i = 0; i < 8; i++) {
char buf[18];
sprintf(buf, "test.narc.d/%05d", i);
printf("Reading file: %s... ", buf);
FILE *f = fopen(buf, "rb");
if (f == NULL) {
narc_pack_halt(pack_ctx);
fprintf(stderr, "Failed to open file for reading: %s\n", buf);
fflush(stderr);
return EXIT_FAILURE;
}
fseek(f, 0, SEEK_END);
size_t fsize = ftell(f);
fseek(f, 0, SEEK_SET);
printf("-> size = %zu\n", fsize);
unsigned char *image = malloc(fsize);
fread(image, 1, fsize, f);
fclose(f);
narc_pack_file(pack_ctx, image, fsize);
}
struct narc *repack = narc_pack(pack_ctx);
printf("Re-pack successful!\n");
FILE *f = fopen("repack.narc", "wb");
fwrite(repack, 1, repack->size, f);
fclose(f);
free(repack);
free(narc);
return EXIT_SUCCESS;
}

83
cli/src/narc.c Normal file
View File

@ -0,0 +1,83 @@
/*
* Copyright 2024 <lhearachel@proton.me>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <narc.h>
#include "command.h"
static bool match_opt(const char *opt, const char *shortopt, const char *longopt);
// clang-format off
static const struct command handlers[] = {
{ "c", "create", create },
{ "x", "extract", extract },
{ "y", "yank", yank },
{ "i", "info", info },
};
static const char *version = "0.1.0";
static const char *tag_line = "narc - create, extract, and explore Nitro Archive virtual filesystems\n";
static const char *short_usage = "Usage: narc [-h | --help] [-v | --version] [command] [arguments]\n";
static const char *commands = ""
"Commands:\n"
" c, create Create a NARC from a folder of physical files\n"
" x, extract Extract virtual files from the NARC to a folder\n"
" y, yank Yank individiaul files from the NARC to disk\n"
" i, info Print diagnostics about a NARC"
"";
// clang-format on
int main(int argc, const char **argv)
{
argc--;
argv++;
if (argc == 0 || match_opt(*argv, "-h", "--help")) {
fprintf(stdout, "%s\n%s\n%s\n", tag_line, short_usage, commands);
return EXIT_SUCCESS;
}
if (match_opt(*argv, "-v", "--version")) {
fprintf(stdout, "%s\n", version);
return EXIT_SUCCESS;
}
for (size_t i = 0; i < sizeof(handlers) / sizeof(handlers[0]); i++) {
if (match_opt(*argv, handlers[i].abbrev, handlers[i].name)) {
argc--;
argv++;
return handlers[i].main(argc, argv);
}
}
fprintf(stderr, "narc: Unrecognized command “%s”\n", *argv);
fprintf(stderr, "%s\n%s\n", short_usage, commands);
return EXIT_FAILURE;
}
static bool match_opt(const char *opt, const char *shortopt, const char *longopt)
{
return (shortopt != NULL && strcmp(opt, shortopt) == 0)
|| (longopt != NULL && strcmp(opt, longopt) == 0);
}

8
cli/src/narc_create.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
int create(int argc, const char **argv)
{
printf("TODO: create command\n");
return EXIT_SUCCESS;
}

8
cli/src/narc_extract.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
int extract(int argc, const char **argv)
{
printf("TODO: extract command\n");
return EXIT_SUCCESS;
}

8
cli/src/narc_info.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
int info(int argc, const char **argv)
{
printf("TODO: info command\n");
return EXIT_SUCCESS;
}

8
cli/src/narc_yank.c Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
int yank(int argc, const char **argv)
{
printf("TODO: yank command\n");
return EXIT_SUCCESS;
}