summaryrefslogblamecommitdiffstats
path: root/src/cook.c
blob: 80c7f9fe426bbe24d9b81501c485d7083540ecc4 (plain) (tree)
1
2
3
4
5
6
7
8
9
10









                     
           










                           
            


















                                               







                                                                                                                                   




                         
                                                  

                                                  
                           






















                                                     
                                



                     


                   

























                                                                                                                  










                                              
                
                              
                          
                                    


                        
                              



                                                
                                            




                                           
                                            


       





                                



                   

   
 

           
#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;
  int eval;
  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,
  .eval = 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          },
      {"eval",       no_argument,       0, 'e', "Eval recipe before output",                            0,            0          },
      {"new",        required_argument, 0, 'n', "Create recipe",                                        "TITLE",      "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, "ehI:n:c:t:i:s:",
                     long_options, &option_index);

    printf("ASDASDASDA\n");
    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;
      strcpy(opt.title, optarg);
      break;
    case 'c':
      opt.change = 1;
      break;
    case 'e':
      opt.eval = 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.step_n) */
  /*     printf("\n---\n\n"); */
  /*   for (int i = 0; i < opt.step_n; i++) */
  /*     printf("%s\n", opt.step[i]); */
  /* } */

  if (opt.new) {
    recipe * r = new_recipe();
    r->path = strdup(".");
    r->filename = strdup(opt.title);
    char err[1000] = "";
    int rc = 0;

    if (strcmp(opt.title, ""))
      r->title = strdup(opt.title);
    for (int i = 0; i < opt.ing_n; i++) {
      rc = parse_item(opt.ing[i], r, NULL, err);
      if (rc) {
        fprintf(stderr, "ERROR: %s\n", err);
      }
    }
    for (int i = 0; i < opt.step_n; i++) {
      rc = parse_step(opt.step[i], r, err);
      if (rc) {
        fprintf(stderr, "ERROR: %s\n", err);
      }
    }

    if (opt.eval) {
      recipe * r_eval = eval(r);
      free_recipe(r);
      r = r_eval;
    }

    if (!rc)
      torcp(r);

    free_recipe(r);
  }


  return 0;
}