Skip to content
getoptions.c 1.08 KiB
Newer Older
Seblu's avatar
Seblu committed
/*
Seblu's avatar
Seblu committed
** getoptions.c for 42sh
Seblu's avatar
Seblu committed
**
** Made by Seblu
** Login   <seblu@epita.fr>
**
** Started on  Sun Jul 30 03:28:26 2006 Seblu
Seblu's avatar
Seblu committed
** Last update Tue Nov 14 15:38:32 2006 seblu
Seblu's avatar
Seblu committed
*/

#include <stdio.h>
#include <string.h>
Seblu's avatar
Seblu committed
#include "option.h"
#include "shell.h"
Seblu's avatar
Seblu committed
#include "../common/function.h"
Seblu's avatar
Seblu committed

/*!
** Parse the command line
**
** @param argc program argc
** @param argv program argv
** @param opt shell opt structure to set with options
*/
Seblu's avatar
Seblu committed
void	getoptions(s_option *opt, int argc, char **argv)
Seblu's avatar
Seblu committed
#if DEBUG_OPTION == 1
Seblu's avatar
Seblu committed
  printf("* Option Parser\n");
#endif
  if (argc == 1)
    return;
  for (int i = 1; i < argc; ++i) {
Seblu's avatar
Seblu committed
#if DEBUG_OPTION == 1
    printf("argv[%d]=%s\n", i, argv[i]);
Seblu's avatar
Seblu committed
#endif
    const char *copt = argv[i];
    if (!strcmp(copt, "-c") || !strcmp(copt, "--command")) {
      opt->command = strvmerge((const char**)argv + i + 1);
Seblu's avatar
Seblu committed
#if DEBUG_OPTION == 1
      printf("option c: %s\n", opt->command);
Seblu's avatar
Seblu committed
#endif
    else if (!strcmp(copt, "--ast-print"))
Seblu's avatar
Seblu committed
      option_set(shell->option, "ast_print");
    else {
      printf("Error: Invalid option %s\n", copt);
      exit(1);
    }