diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Makefile.am | 6 | ||||
-rw-r--r-- | src/cookbook.c | 162 | ||||
-rw-r--r-- | src/food.c | 193 | ||||
-rw-r--r-- | src/lib.c | 2 |
4 files changed, 362 insertions, 1 deletions
diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..99de4ca --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,6 @@ +bin_PROGRAMS = food cookbook + +extern = teeny-sha1.c pbg.c + +food_SOURCES = food.c eval.c lib.c parser.c search.c search_stuff.c types.c util.c $(extern) +cookbook_SOURCES = cookbook.c eval.c lib.c parser.c search.c search_stuff.c types.c util.c $(extern) diff --git a/src/cookbook.c b/src/cookbook.c new file mode 100644 index 0000000..08fa325 --- /dev/null +++ b/src/cookbook.c @@ -0,0 +1,162 @@ +#include <getopt.h> + +#include "util.h" +#include "parser.h" +#include "search.h" +#include "eval.h" +#include "lib.h" + +recipe ** cookbook; + +static struct opts { + int json; + int html; + int rcp; + char query[2048]; + int eval; + int list; + int hash; + int search; + int search_strict; + int help; +} opt = { + .json = 0, + .html = 0, + .rcp = 0, + .query = "", + .eval = 1, + .list = 0, + .hash = 0, + .search = 0, + .search_strict = 0, + .help = 0, +}; + +void +print_help(char * argv0) { + printf("%s [OPTION] FILE ...\n", argv0); + printf("OPTIONS:\n"); + printf("--format json,html,rcp\n"); + printf("--to-{json,html,rcp}\n"); + printf("-j json\n"); + printf("-w html\n"); + printf("-r rcp\n"); + printf("-l, --list-ingredients\n"); + printf("-s TERMS, --search TERMS\n"); + printf("-S TERMS, --strict TERMS\n"); + printf("-n, --no-eval\n"); +} + +int +main(int argc, char * argv[]) +{ + fdebug("--- Debug mode is on ---\n"); + + int c; + + while (1) { + static struct option long_options[] = + { + /* <span class="roman">These options set a flag.</span> */ + // {"verbose", no_argument, &verbose_flag, 1}, + // {"brief", no_argument, &verbose_flag, 0}, + {"help", no_argument, 0, 'h'}, + {"no-eval", no_argument, 0, 'n'}, + {"to-json", no_argument, 0, 'j'}, + {"to-html", no_argument, 0, 'w'}, + {"to-rcp", no_argument, 0, 'r'}, + {"format", required_argument, 0, 'f'}, + {"search", required_argument, 0, 's'}, + {"strict", required_argument, 0, 'S'}, + {"hash", required_argument, 0, 'H'}, + {"list-ingredients", no_argument, 0, 'l'}, + // {"to-rcp", required_argument, 0, 'r'}, + {0, 0, 0, 0} + }; + + int option_index = 0; + + c = getopt_long (argc, argv, "jnlhrwf:s:S:H:", + long_options, &option_index); + + if (c == -1) + break; + + switch (c) { + case 0: + if (long_options[option_index].flag != 0) + break; + printf ("option %s", long_options[option_index].name); + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + case 's': + opt.search = 1; + strcpy(opt.query, optarg); + break; + case 'S': + opt.search = 1; + opt.search_strict = 1; + strcpy(opt.query, optarg); + break; + case 'H': + opt.hash = 1; + strcpy(opt.query, optarg); + break; + case 'j': + opt.json = 1; + break; + case 'w': + opt.html = 1; + break; + case 'r': + opt.rcp = 1; + break; + case 'l': + opt.list = 1; + break; + case 'h': + opt.help = 1; + break; + case 'n': + opt.eval = 0; + break; + case '?': + return -1; + break; + default: + abort (); + } + } + + if (opt.help) { + print_help(argv[0]); + return 0; + } + + char ** lib = NULL; + int n = collect_library(&lib, argv, argc, optind); + + cookbook = (recipe **)malloc(sizeof(recipe *) * n); + for (int i = 0; i < n; i++) { + cookbook[i] = parse(lib[i], NULL); + if (opt.eval) { + recipe * _r = eval(cookbook[i]); + free_recipe(cookbook[i]); + cookbook[i] = _r; + } + } + free_library(lib, n); + + for (int i = 0; i < n; i++) { + listing(cookbook[i]); + } + + for (int i = 0; i < n; i++) { + free_recipe(cookbook[i]); + } + free(cookbook); + + return 0; +} diff --git a/src/food.c b/src/food.c new file mode 100644 index 0000000..a9c3c6c --- /dev/null +++ b/src/food.c @@ -0,0 +1,193 @@ +#include <getopt.h> + +#include "util.h" +#include "parser.h" +#include "search.h" +#include "eval.h" +#include "lib.h" + +recipe ** cookbook; + +static struct opts { + int json; + int html; + int rcp; + char query[2048]; + int eval; + int list; + int hash; + int search; + int search_strict; + int help; +} opt = { + .json = 0, + .html = 0, + .rcp = 0, + .query = "", + .eval = 1, + .list = 0, + .hash = 0, + .search = 0, + .search_strict = 0, + .help = 0, +}; + +void +print_help(char * argv0) { + printf("%s [OPTION] FILE ...\n", argv0); + printf("OPTIONS:\n"); + printf("--format json,html,rcp\n"); + printf("--to-{json,html,rcp}\n"); + printf("-j json\n"); + printf("-w html\n"); + printf("-r rcp\n"); + printf("-l, --list-ingredients\n"); + printf("-s TERMS, --search TERMS\n"); + printf("-S TERMS, --strict TERMS\n"); + printf("-n, --no-eval\n"); +} + +int +main(int argc, char * argv[]) +{ + fdebug("--- Debug mode is on ---\n"); + + int c; + + while (1) { + static struct option long_options[] = + { + /* <span class="roman">These options set a flag.</span> */ + // {"verbose", no_argument, &verbose_flag, 1}, + // {"brief", no_argument, &verbose_flag, 0}, + {"help", no_argument, 0, 'h'}, + {"no-eval", no_argument, 0, 'n'}, + {"to-json", no_argument, 0, 'j'}, + {"to-html", no_argument, 0, 'w'}, + {"to-rcp", no_argument, 0, 'r'}, + {"format", required_argument, 0, 'f'}, + {"search", required_argument, 0, 's'}, + {"strict", required_argument, 0, 'S'}, + {"hash", required_argument, 0, 'H'}, + {"list-ingredients", no_argument, 0, 'l'}, + // {"to-rcp", required_argument, 0, 'r'}, + {0, 0, 0, 0} + }; + + int option_index = 0; + + c = getopt_long (argc, argv, "jnlhrwf:s:S:H:", + long_options, &option_index); + + if (c == -1) + break; + + switch (c) { + case 0: + if (long_options[option_index].flag != 0) + break; + printf ("option %s", long_options[option_index].name); + if (optarg) + printf (" with arg %s", optarg); + printf ("\n"); + break; + case 'f': + if (!strcmp(optarg, "json")) + opt.json = 1; + else if (!strcmp(optarg, "rcp")) + opt.rcp = 1; + else if (!strcmp(optarg, "html")) + opt.html = 1; + else + fprintf(stderr, "invalid format: %s\n", optarg); + break; + case 's': + opt.search = 1; + strcpy(opt.query, optarg); + break; + case 'S': + opt.search = 1; + opt.search_strict = 1; + strcpy(opt.query, optarg); + break; + case 'H': + opt.hash = 1; + strcpy(opt.query, optarg); + break; + case 'j': + opt.json = 1; + break; + case 'w': + opt.html = 1; + break; + case 'r': + opt.rcp = 1; + break; + case 'l': + opt.list = 1; + break; + case 'h': + opt.help = 1; + break; + case 'n': + opt.eval = 0; + break; + case '?': + return -1; + break; + default: + abort (); + } + } + + if (opt.help) { + print_help(argv[0]); + return 0; + } + + char ** lib = NULL; + int n = collect_library(&lib, argv, argc, optind); + + for (int i = 0; i < n; i++) { + recipe * r = parse(lib[i], NULL); + if (r) { + if (opt.eval) { + recipe * r_eval = eval(r); + free_recipe(r); + r = r_eval; + } + if (opt.hash) { + if (!check_hash(r, opt.query)) { + free_recipe(r); + continue; + } + } + if (opt.search) { + int c; + if (!(c = query_for_items_pbn(r, opt.query, opt.search_strict))) { + free_recipe(r); + continue; + } + if (c < 0) + exit(1); + } + + if (opt.list) { + pprint_items(r); + } + else { + if (opt.json) tojson(r); + else if (opt.html) tohtml(r); + else if (opt.rcp) torcp(r); + else { + listing(r); + } + } + free_recipe(r); + } + } + + free_library(lib, n); + + return 0; +} @@ -20,7 +20,7 @@ collect_library(char *** dst, char * argv[], int argc, int optind) fp = popen("/bin/find /home/gramanas/code/foodtools/lib/ -name '*.rcp'", "r"); if (fp == NULL) { fprintf(stderr, "Couldn't run /bin/find\n"); - exit(1); + return 0; } /* Read the output a line at a time */ |