aboutsummaryrefslogtreecommitdiffstats
path: root/src/actions.c
blob: c084e8fbf76827ec5001e75ecbe2d13f7ff38962 (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
319
320
321
322
323
324
325
326
327
328
329
330
/* actions.c - ck actions ----------------------------------------------*- C -*-
 *
 * This file is part of ck, the config keeper
 *
 * -----------------------------------------------------------------------------
 *
 * Copyright (C) 2018  Anastasis Grammenos
 * GPLv3 (see LICENCE for the full notice)
 *
 * -------------------------------------------------------------------------- */
#include <limits.h>
#include <stdlib.h>

#include "actions.h"
#include "actionhelper.h"
#include "dblayer.h"
#include "ckutil.h"
#include "cklist.h"

int run_INIT(UserOpt * opt, Conf *conf) {
  UNUSED(conf);
  if (db_exists(opt)) {
    printf("Current configuration file location: %s\n", opt->confDir);
    PRINT_ERR("ck is already initialized.");
    return 0;
  }
  if (init_create_config_file(opt)) {
    return 0;
  }
  DB db = init_make_DB(opt);
  if (db.error == SQL_NO_ERR) {
    init_make_tables(&db);
  }
  sqlite3_close(db.ptr);
  return 1;
}

AddOpt make_add_options(cklist* args) {
  list_rewind(args);
  /* since we are here, the first two argumens must exist */
  AddOpt addOpt = {
    .progName = list_get(args),
    .secret = 0,
    .prime = 0,
    .err = ADD_NO_ERR
  };

  list_next(args);
  if (!util_is_file_rw(list_get(args))) {
    addOpt.err = ADD_ERR_WRONG_CONFIG;
    return addOpt;
  }
  realpath(list_get(args), addOpt.confPath);

  while (list_next(args)) {
    if (strcmp(list_get(args), "-s") == 0 && addOpt.secret == 0) {
      addOpt.secret = 1;
    } else if (strcmp(list_get(args), "-p") == 0 && addOpt.prime == 0) {
      addOpt.prime = 1;
    } else {
      addOpt.err = ADD_ERR_WRONG_FLAGS;
      return addOpt;
    }
  }
  list_rewind(args);
  return addOpt;
}

void add_print_opts(AddOpt *opt) {
  printf("Program:\t%s\nConfig:\t\t%s\n", opt->progName, opt->confPath);
  if (opt->prime && opt->secret) {
    printf("Options:\tsecret, primary\n");
  } else if (opt->prime) {
    printf("Options:\tprimary\n");
  } else if (opt->secret) {
    printf("Options:\tsecret\n");
  }
}

int run_ADD(UserOpt * opt, Conf *conf) {
  DB db = open_DB(opt);
  if (db.ptr == NULL) {
    if (db.error == SQL_ERR_NO_TABLES) {
      PRINT_ERR("The database file is currupted. Run ck init anew.");
    }
    return 0;
  }
  AddOpt addOpt = make_add_options(opt->args);
  switch (addOpt.err) {
  case ADD_NO_ERR:
    break;
  case ADD_ERR_WRONG_CONFIG:
    PRINT_ERR("The config file specified doesn't exist.");
    close_DB(&db);
    return 0;
  case ADD_ERR_WRONG_FLAGS:
    PRINT_ERR("Flags are: -s for secret and -p for primary.");
    close_DB(&db);
    return 0;
  }
  add_print_opts(&addOpt);
  if (add_transaction_begin(&db, &addOpt) == 0) {
    close_DB(&db);
    return 0;
  }
  close_DB(&db);
  add_make_link(&addOpt, conf);
  char err[STR_M];
  if (add_err_message(err)) {
    PRINT_ERR(err);
    return 0;
  }
  return 1;
}

int run_DEL(UserOpt * opt, Conf *conf) {
  printf("Running %s\n", "del");
  return 0;
}

int run_EDIT(UserOpt *opt, Conf *conf) {
  printf("Running %s\n", "edit");
  DB db = open_DB(opt);
  if (db.ptr == NULL) {
    if (db.error == SQL_ERR_NO_TABLES) {
      PRINT_ERR("The database file is currupted. Run ck init anew.");
    }
    return 0;
  }

  list_rewind(opt->args);
  char confPath[STR_M];
  if (list_size(opt->args) == 1) {
    char confName[STR_M];
    int secret = 0;
    if (edit_get_prime_config_from_program(&db, list_get(opt->args), confName, &secret) == 1) {
      str_join_dirname_with_basename(confPath, secret ? conf->SCRT_dir : conf->VC_dir, confName);
      printf("%s\n", confPath);
    } else {
      PRINT_ERR("No primary config");
      close_DB(&db);
      return 0;
    }
  } else {
    close_DB(&db);
    char confName[STR_L];
    switch (edit_get_config_or_suggestions(opt->args, confName)) {
    case ERC_OK:
      return 0;
    case ERC_ERR:
      return 1;
    case ERC_SUGGESTIONS:
      return 1;
    }
    return 1;
  }
  close_DB(&db);
    
  char *editor = getenv("EDITOR");
  char command[STR_L];
  strcpy(command, str_is_empty(editor) ? "nano" : editor);
  strcat(command, " ");
  strcat(command, confPath);
  system(command);
  return 1;
}

ListOpt make_list_options(cklist *args) {
  list_rewind(args);
  ListOpt listOpt = {
    ._lt = LT_NONE,
    ._lst = LST_PLAIN,
    .err = 0
  };

  if (list_size(args)) {
    do {
      if (strcmp(list_get(args), "-t") == 0) {
	if (!list_next(args)) {
	  listOpt.err = 1;
	  break;
	}
	if (strcmp(list_get(args), "plain") == 0) {
	  listOpt._lst = LST_PLAIN;
	}
	else if (strcmp(list_get(args), "lisp") == 0) {
	  listOpt._lst = LST_LISP;
	}
	else if (strcmp(list_get(args), "python") == 0) {
	  listOpt._lst = LST_PYTHON;
	}
	else {
	  listOpt.err = 1;
	}
      }
      else if (strcmp(list_get(args), "paths") == 0) {
	listOpt._lt = LT_PATH;
      }
      else if (strcmp(list_get(args), "programs") == 0) {
	listOpt._lt = LT_PROGRAM;
      }
      else if (strcmp(list_get(args), "tree") == 0) {
        listOpt._lt = LT_TREE;
      }
      else {
	listOpt.err = 1;
      }
    } while(list_next(args));
  }
  list_rewind(args);
  return listOpt;
}

int run_LIST(UserOpt *opt, Conf *conf) {
  UNUSED(conf);
  DB db = open_DB(opt);
  if (db.ptr == NULL) {
    if (db.error == SQL_ERR_NO_TABLES) {
      printf("no tables\n");
    }
    return 0;
  }

  cklist *list_type = list_make_new();

  ListOpt listOpt = make_list_options(opt->args);
  if (listOpt.err) {
    close_DB(&db);
    list_free(list_type);
    return 0;
  }
  switch(listOpt._lt) {
  case LT_PATH:
    list_get_paths(&db, list_type);
    break;
  case LT_PROGRAM:
    list_get_programs(&db, list_type);
    break;
  case LT_TREE:
    list_get_path_program_tree(&db, list_type);
    list_print(list_type);
    close_DB(&db);
    list_free(list_type);
    return 1;
  case LT_NONE:
    printf("What should I list? (paths, configs)\n");
    close_DB(&db);
    list_free(list_type);
    return 0;
  }
  switch(listOpt._lst) {
  case LST_PLAIN:
    list_print(list_type);
    break;
  case LST_LISP:
    list_print_lisp(list_type);
    break;
  case LST_PYTHON:
    list_print_python(list_type);
  }
  close_DB(&db);
  list_free(list_type);
  return 1;
}

int run_SEARCH(UserOpt *opt, Conf *conf) {
  printf("Running %s\n", "search");
  DB db = open_DB(opt);
  close_DB(&db);
  return 0;
}

int run_HELP(UserOpt *opt, Conf *conf) {
  printf("Running %s\n", "help");
  return 0;
}

void print_INIT_result(int ok) {
  if (ok) {
    printf("Initialized empty ckdb.\n");
  }
}

void print_ADD_result(int ok) {
  if (ok) {
    printf("ckdb updated succesfully.\n");
    return;
  }
  printf("Could not complete add transaction.\n");
}

void print_DEL_result(int ok) {
  if (ok) {
    printf("succes\n");
    return;
  }
  printf("Not Supported\n");
}

void print_EDIT_result(int ok) {
  if (ok) {
    printf("succes\n");
    return;
  }
  printf("failure\n");
}

void print_LIST_result(int ok) {
  if (ok) {
    return;
  }
  printf("Wrong list arguments\n");
}

void print_SEARCH_result(int ok) {
  if (ok) {
    printf("succes\n");
    return;
  }
  printf("Not Supported\n");
}

void print_HELP_result(int ok) {
  if (ok) {
    printf("succes\n");
    return;
  }
  printf("failure\n");
}