aboutsummaryrefslogtreecommitdiffstats
path: root/src/clparser.c
blob: 081c98190999729183ff002d549698ca3c8c1af2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* clparser.c - Command line parser for ck -----------------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2018  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include "ckutil.h"
#include "clparser.h"
#include "confparser.h"
#include "ckinfo.h"
#include "ckerrlog.h"
#include "actions.h"

ERRLOG(parser);

/* accepted commands */
/* [0] is the count */
const char* const strINIT[]     = {"3", "init",   "i",  "-i"};
const char* const strADD[]      = {"3", "add",    "a",  "-a"};
const char* const strDEL[]      = {"4", "delete", "del","d",  "-d"};
const char* const strEDIT[]     = {"3", "edit",   "e",  "-e"};
const char* const strLIST[]     = {"5", "list",   "ls", "l",  "-l", "-ls"};
const char* const strSEARCH[]   = {"4", "search", "grep", "s",  "-s"};
const char* const strHELP[]     = {"5", "help",   "h",  "-?", "-h", "--help"};
const char* const strRESTORE[]  = {"3", "restore","r",  "-r"};
const char* const strConfDir[]  = {"2", "--config", "-c"};
const char* const strVerbose1[] = {"2", "--verbose", "-v"};
const char* const strVersion[]  = {"2", "version", "--version"};
const char* const strVerbose2[] = {"2", "--Verbose", "-V"};

/* Number of opts */
static int optNum;

/* holds the list of the opts
 * as given by the user */
static const char **opts;

/* points to the current token */
static const char *token;

/* the position to be read */
static int pos = 0;

/* Advance one token.
 * Returns 1 if it exists
 * 0 otherwise */
int next_token() {
  if (pos < optNum) {
    token = opts[pos];
    pos++;
    return 1;
  }
  else {
    token = NULL;
    return 0;
  }
}

static void fill_args_list(int arg_num, UserOpt *opt) {
  for (int i = 0; i < arg_num; i++) {
    if (next_token()) {
      list_add(opt->args, token);
    }
  }
}

#define X(ACTION, MIN, MAX)                     \
  static int parse_ ##ACTION(UserOpt *opt) {    \
    if (optNum < pos + MIN                      \
        || optNum > pos + MAX) {                \
      opt->err = PERR_ ##ACTION## _WRONG;       \
      return -1;                                \
    }                                           \
    int arg_num = optNum - pos;                 \
    fill_args_list(arg_num, opt);               \
    return 0;                                   \
  }
CK_ACTIONS
#undef X

static void determine_action(UserOpt *opt) {
  /* get action */
  if (!next_token()) {
    opt->action = CK_WRONG_ACTION;
    return;
  }

  char actionName[STR_S] = "";
  opt->action = parser_get_action(token, actionName);
  if (opt->action != CK_WRONG_ACTION) {
    LOG("Action to perform: %s", actionName);
  }
  return;
}

static int parse_vals(UserOpt *opt) {
  /* find the action */
  determine_action(opt);
  if (opt->action == CK_WRONG_ACTION) {
    opt->err = PERR_UNKNOWN_ACTION;
    return -1;
  }

  switch (opt->action) {
#define X(ACTION, MIN, MAX)                                \
    case CKA_##ACTION:                           \
      return parse_##ACTION(opt);
    CK_ACTIONS
#undef X
  default:
    return -1;
  }
}

CkAction parser_get_action(const char *name, char *actionName) {
  int i;
#define X(ACTION, MIN, MAX)                                                       \
  for (i = 1; i < atoi(str##ACTION[0]) + 1; i++) {                      \
    if (strcmp(name, str##ACTION[i]) == 0) {                            \
      if (actionName) {                                                 \
        strcpy(actionName, str##ACTION[1]);                             \
      }                                                                 \
      return CKA_##ACTION;                                              \
    }                                                                   \
  }
  CK_ACTIONS;
#undef X
  return CK_WRONG_ACTION;
}

static UserOpt make_empty_user_opt() {
  UserOpt opt;
  opt.action = CK_WRONG_ACTION;
  opt.err = PERR_NOERR;
  opt.confDir = NULL;
  opt.args = list_make_new();
  return opt;
}


/* called to free the resources
 * UserOpt holds */
void free_user_opt(UserOpt *opt) {
  if (opt->confDir != NULL) {
    free(opt->confDir);
  }
  list_free(opt->args);
}

/* find the correct config */
static int get_config(UserOpt *opt) {
  /* If it's a cli option */
  if (next_token()) {
    for (int i = 1; i < atoi(strConfDir[0]) + 1; i++) {
      if (strcmp(token, strConfDir[i]) == 0) {
        if (!next_token()) {
          ERR("Config needs a value");
          return -1;
        }
        char dir[STR_L] = "";
        realpath(token, dir);
        if (!util_is_dir(dir)) {
          ERR("%s is not a directory", token);
          return -1;
        }
        opt->confDir = malloc(strlen(dir) + 1);
        strcpy(opt->confDir, dir);
        // remove trailing `/`
        if (opt->confDir[strlen(dir) - 1] == '/') {
          opt->confDir[strlen(dir) - 1] = '\0';
        }
        return 0;
      }
    }
    /* rewind */
    pos = pos - 1;
    token = opts[pos];
   }

  return find_config(opt);
}

static int version() {
  /* get first token */
  if (next_token()) {
    for (int i = 1; i < atoi(strVersion[0]) + 1; i++) {
      if (strcmp(token, strVersion[i]) == 0) {
        print_version();
        return 1;
      }
    }
    // rewind
    pos = pos - 1;
    token = opts[pos];
  }
  return 0;
}

static void verbose() {
  /* get first token */
  if (next_token()) {
    for (int i = 1; i < atoi(strVerbose1[0]) + 1; i++) {
      if (strcmp(token, strVerbose1[i]) == 0) {
        errlog_set_verbose(1);
        return;
      }
    }

    for (int i = 1; i < atoi(strVerbose2[0]) + 1; i++) {
      if (strcmp(token, strVerbose2[i]) == 0) {
        errlog_set_verbose(2);
        return;
      }
    }

    // rewind
    pos = pos - 1;
    token = opts[pos];
  }
}

char * get_possible_action_strings(char *dest, CkAction ckAction) {
  char buf[STR_M] = "";
  switch (ckAction) {
#define X(ACTION, MIN, MAX)                                         \
    case CKA_##ACTION:                                    \
      strcpy(buf, "{ ");                                  \
      for (int i = 1; i < atoi(str##ACTION[0]); i++) {    \
        strcat(buf, str##ACTION[i]);                      \
        strcat(buf, ", ");                                \
      }                                                   \
      strcat(buf, str##ACTION[atoi(str##ACTION[0])]);     \
      strcat(buf, " }");                                  \
      break;
    CK_ACTIONS
#undef X
  default:
    dest = NULL;
    return NULL;
  }

  strcpy(dest, buf);
  return dest;
}

static void print_parser_error(UserOpt *opt) {
  char names[STR_M] = "";
  get_possible_action_strings(names, opt->action);

  switch (opt->err) {
  case PERR_NOERR:
    return;
  case PERR_UNKNOWN_ACTION:
    ERR("Unknown action: %s", token);
    return;
#define X(ACTION, MIN, MAX)                               \
  case PERR_ ##ACTION## _WRONG:                 \
    HELP("Usage:\n%s", names);                  \
    print_##ACTION##_help();                    \
    break;
    CK_ACTIONS;
#undef X
  }
}

static void print_parser_help() {
  char names[STR_M] = "";
  ckhelp("ck - the config keeper");
  ckhelp("Usage:");
  ckhelp("Init\t%s", get_possible_action_strings(names, CKA_INIT));
  ckhelp("Add\t%s", get_possible_action_strings(names, CKA_ADD));
  ckhelp("Delete\t%s", get_possible_action_strings(names, CKA_DEL));
  ckhelp("Edit\t%s", get_possible_action_strings(names, CKA_EDIT));
  ckhelp("List\t%s", get_possible_action_strings(names, CKA_LIST));
  ckhelp("Search\t%s", get_possible_action_strings(names, CKA_SEARCH));
  ckhelp("Restore\t%s", get_possible_action_strings(names, CKA_RESTORE));
  ckhelp("Help\t%s", get_possible_action_strings(names, CKA_HELP));
  report_help();
}

int parse_action(int argc, const char **argv, UserOpt *opt) {
  /* make empty user opt */
  *opt = make_empty_user_opt();
  opts = argv;
  optNum = argc;
  /* skip the program name */
  next_token();
  /* set verbose level */
  verbose();
  /* handle version info */
  if (version()) {
    return -1;
  }
  /* figure what is the config file */
  if (get_config(opt)) {
    return -1;
  }
  /* If the remaining arguments are < 1
   * print help and exit */
  if (optNum - pos < 1) {
    print_parser_help();
    return -1;
  }
  /* parse values */
  if (parse_vals(opt)) {
    print_parser_error(opt);
    return -1;
  }
  if (opt->err == PERR_NOERR) {
    return 0;
  }
  print_parser_error(opt);
  return -1;
}