summaryrefslogtreecommitdiffstats
path: root/food.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2021-11-13 21:05:38 +0200
committergramanas <anastasis.gramm2@gmail.com>2021-11-14 11:07:42 +0200
commit382da0ada99a2874052f0e6ccb2c25e012d47fc9 (patch)
treea92357f558dab48f376e666594da4aa6576f4861 /food.c
parent2e00a24994e094efc47fce4501b134caa801ea2c (diff)
downloadfoodtools-382da0ada99a2874052f0e6ccb2c25e012d47fc9.tar.gz
foodtools-382da0ada99a2874052f0e6ccb2c25e012d47fc9.tar.bz2
foodtools-382da0ada99a2874052f0e6ccb2c25e012d47fc9.zip
Update Makefile and add list ingredients draft
Diffstat (limited to 'food.c')
-rw-r--r--food.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/food.c b/food.c
new file mode 100644
index 0000000..90ce489
--- /dev/null
+++ b/food.c
@@ -0,0 +1,137 @@
+#include <getopt.h>
+
+#include "src/util.h"
+#include "src/parser.h"
+#include "src/eval.h"
+
+recipe ** cookbook;
+
+static struct opts {
+ int json;
+ int html;
+ int rcp;
+ char *query;
+ int list;
+ int help;
+} opt = {
+ .json = 0,
+ .html = 0,
+ .rcp = 0,
+ .query = NULL,
+ .list = 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");
+}
+
+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'},
+ {"to-json", no_argument, 0, 'j'},
+ {"to-html", no_argument, 0, 'w'},
+ {"to-rcp", no_argument, 0, 'r'},
+ {"format", required_argument, 0, 'f'},
+ {"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, "jlhrwf:",
+ 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 '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 '?':
+ return -1;
+ break;
+ default:
+ abort ();
+ }
+ }
+
+ if (opt.help) {
+ print_help(argv[0]);
+ return 0;
+ }
+
+ if (optind < argc) {
+ while (optind < argc) {
+ recipe * r = parse(argv[optind++], NULL);
+ if (r && opt.list) {
+ /* recipe * r_merged = new_recipe(); */
+ /* merge_items(r_merged, r); */
+ recipe * r_merged = eval(r);
+ pprint_items(r_merged);
+ free_recipe(r_merged);
+ }
+ else if (r) {
+ if (opt.json) tojson(r);
+ if (opt.html) tohtml(r);
+ if (opt.rcp) torcp(r);
+ }
+ free_recipe(r);
+ }
+ } else {
+ fprintf(stderr, "Specify filenames\n");
+ }
+
+ return 0;
+}