2. Main C file

The main C file is called worgmap.c.

<<worgmap.c>>=
#include <stdio.h>
#include <string.h>
#include "core.h"

int p_get(int argc, char *argv[]);
int p_dot(int argc, char *argv[]);
int p_html(int argc, char *argv[]);
int p_weewiki(int argc, char *argv[]);

#define NCOMMANDS 4

typedef struct {
    const char *name;
    int (*fun)(int, char **);
} worgmap_cmd;

worgmap_cmd commands[NCOMMANDS] = {
    {"get", p_get},
    {"dot", p_dot},
    {"html", p_html},
    {"weewiki", p_weewiki},
};

int main(int argc, char *argv[])
{
    int nargs;
    int n;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s subcommand\n", argv[0]);
        fprintf(stderr, "Available subcommands:\n");
        for (n = 0; n < NCOMMANDS; n++) {
            fprintf(stderr, "%s\n", commands[n].name);
        }
        return 1;
    }

    argv++;
    argc--;

    nargs = argc;

    for (n = 0; n < nargs; n++) {
        if (argv[0][0] != '-') break;
        switch (argv[0][1]) {
            case 'd':
                if (n == (nargs - 1)) {
                    fprintf(stderr,
                            "Please supply an argument\n");
                    return 1;
                }
                wmp_filename_set(argv[1]);
                argv++;
                argc--;
                n++;
                break;
            default:
                fprintf(stderr,
                        "Invalid flag '%c'\n", argv[0][1]);
                return 1;
        }
        argv++;
        argc--;
    }

    if (argc <= 0) {
        fprintf(stderr, "Please supply a command\n");
        return 1;
    }


    for (n = 0; n < NCOMMANDS; n++) {
        if (!strcmp(argv[0], commands[n].name)) {
            return commands[n].fun(argc, argv);
        }
    }

    fprintf(stderr,
            "Could not find command '%s'\n",
            argv[0]);
    return 1;
}



prev | home | next