add argument parsing setup

laumann
Aug 7, 2022, 1:36 PM
FB67XX5EGNF45JNAQISW7CBLSLS36F6JEUOBTTPUMVBMOVLGA3VQC

Dependencies

Change contents

  • file addition: macros.h (----------)
    [4.1]
    #ifndef MACROS_H
    #define MACROS_H
    /**
    * ARRAY_SIZE - get the number of elements in a visibl array.
    *
    * DOES NOT WORK ON POINTERS
    */
    #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
    #endif
  • file addition: init.c (----------)
    [4.1]
    #include <unistd.h> /* getopt(3p) */
    #include <stdio.h>
    #include <stdlib.h>
    /**
    * ani init [-h|--help] [--channel <CHANNEL>] <PATH>
    */
    /**
    * Init initializes a new repository as
    *
    * mkdir .pijul/{pristine, changes}
    *
    * plus maybe a few other things?
    */
    int
    init()
    {
    /* create the directory with mkdir(2) */
    printf("init!\n");
    return 0;
    }
    /**
    * ani init [-c <channel>] [<dir>]
    */
    int cmd_init(int argc, const char **argv)
    {
    int c;
    char *channel = "main";
    /* Parse cmdline args */
    while ((c = getopt(argc, argv, "")) != -1) {
    switch (c) {
    case 'c':
    channel = optarg;
    break;
    case '?':
    fprintf(stderr, "unrecognized option '-%c'\n", optopt);
    exit(1);
    }
    }
    /* Return result of init() */
    return init();
    }
  • file addition: commands.h (----------)
    [4.1]
    int cmd_init(int argc, const char *argv[]);
  • edit in ani.c at line 2
    [2.50]
    [2.50]
    #include <stdlib.h>
    #include <string.h>
    #include "macros.h"
    #include "commands.h"
  • edit in ani.c at line 14
    [2.174]
    [2.174]
    struct cmd {
    const char *name;
    int (*fn)(int, const char **);
    };
    static struct cmd commands[] = {
    { "init", cmd_init },
    };
    /**
    * Print usage string and exit.
    */
  • edit in ani.c at line 30
    [2.222]
    [2.222]
    exit(0);
  • replacement in ani.c at line 32
    [2.224][2.224:359]()
    // TODO: impl cmdline argument parsing
    //
    // ani init Initialize an empty pijul repository
    // ani key Key generation and management
    [2.224]
    [2.359]
    static struct cmd *
    find_cmd(const char *s)
    {
    int i;
    struct cmd *p;
    for (i = 0; i < ARRAY_SIZE(commands); i++) {
    p = commands + i;
    if (!strcmp(s, p->name))
    return p;
    }
    return NULL;
    }
    int
    cmd_main(int argc, char *argv[])
    {
    char *cmd_name;
    struct cmd *command;
    cmd_name = *++argv;
    command = find_cmd(cmd_name);
    if (command == NULL) {
    fprintf(stderr, "command not found '%s'\n", cmd_name);
    return -1;
    }
    argc--;
    return (command->fn)(argc, argv);
    }
    /**
    * TODO: impl cmdline argument parsing
    *
    * ani init Initialize an empty pijul repository
    * ani key Key generation and management
    *
    */
  • edit in ani.c at line 76
    [2.422]
    [2.422]
    return cmd_main(argc, argv);
  • edit in Makefile at line 12
    [2.620]
    [2.620]
    OBJS += init.o