add argument parsing setup
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?*/intinit(){/* 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
#include <stdlib.h>#include <string.h>#include "macros.h"#include "commands.h" - edit in ani.c at line 14
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
exit(0); - replacement in ani.c at line 32
// TODO: impl cmdline argument parsing//// ani init Initialize an empty pijul repository// ani key Key generation and managementstatic 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;}intcmd_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
return cmd_main(argc, argv); - edit in Makefile at line 12
OBJS += init.o