summaryrefslogblamecommitdiffstats
path: root/src/cook.c
blob: 74374f06e271394303d73f1e73cdf302358a9da3 (plain) (tree)
























































































































                                                                                                                                               
#include "util.h"
#include "parser.h"
#include "search.h"
#include "foodopts.h"
#include "eval.h"
#include "lib.h"

static struct opts {
  int new;
  int change;
  char title[2048];
  char includes[100][2048];
  int includes_n;
  char ing[100][2048];
  int ing_n;
  char step[100][2048];
  int step_n;
  int help;
} opt = {
  .new = 0,
  .change = 0,
  .title = "",
  .includes = {""},
  .includes_n = 0,
  .ing = {""},
  .ing_n = 0,
  .step = {""},
  .step_n = 0,
  .help = 0,
};

int
main(int argc, char * argv[])
{
  fdebug("--- Debug mode is on ---\n");

  int c;

  struct foodoption long_options[] =
    {/* name,  has_arg, flag, val, help, arg */
      {"help",              no_argument,       0, 'h', "Print this help",                                      0,            "General"       },
      {"include",           required_argument, 0, 'I', "Path to recipe library, can be passed multiple times", "PATH",       0               },
      {"new",               no_argument,       0, 'n', "Create recipe",                                        0,            "Commands"      },
      {"change",            required_argument, 0, 'c', "Change recipe",                                        "FILE",       0               },
      {"title",             required_argument, 0, 't', "Set recipe title",                                     "TITLE",      "Options"       },
      {"ingredient",        required_argument, 0, 'i', "Add an ingredient",                                    "INGREDIENT", 0               },
      {"step",              required_argument, 0, 's', "Add a step",                                           "STEP",       0               },
      {0, 0, 0, 0, 0}
    };
  while (1) {
    int option_index = 0;

    c = get_foodopt (argc, argv, "hI:nc:t:i:s:",
                     long_options, &option_index);

    if (c == -1)
      break;

    switch (c) {
    case 0:
      break;
    case 'I':
      strcpy(opt.includes[opt.includes_n++], optarg);
      break;
    case 'i':
      strcpy(opt.ing[opt.ing_n++], optarg);
      break;
    case 's':
      strcpy(opt.step[opt.step_n++], optarg);
      break;
    case 't':
      strcpy(opt.title, optarg);
      break;
    case 'h':
      opt.help = 1;
      break;
    case 'n':
      opt.new = 1;
      break;
    case 'c':
      opt.change = 1;
      break;
    case '?':
      fprintf(stderr, "%s: option '%c' not recognised, try -h for help\n", argv[0], optopt);
      return -1;
      break;
    case ':':
      switch (optopt) {
      /* case 'l': */
      /*   opt.list = 1; */
      /*   strcpy(opt.listfmt, "DEFAULT"); */
      /*   break; */
      default:
        fprintf(stderr, "%s: missing argument %s for -%c\n", argv[0], get_argument(optopt, long_options), optopt);
        return -1;
        break;
      }
      break;
    default:
      abort ();
    }
  }

  if (opt.help) {
    foodopt_help(argv[0], long_options);
    return 0;
  }

  if (opt.new) {
    if (strcmp(opt.title, ""))
      printf("@%s\n\n",  opt.title);
    for (int i = 0; i < opt.ing_n; i++)
      printf("%s\n", opt.ing[i]);
    if (opt.ing_n)
      printf("\n---\n\n");
    for (int i = 0; i < opt.step_n; i++)
      printf("%s\n", opt.step[i]);
  }

  return 0;
}