aboutsummaryrefslogtreecommitdiffstats
path: root/src/confparser.c
blob: 0edd1557f27f8a1a4236fbfff311bf6d9d514928 (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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include "confparser.h"


const char * const CONFIG_NAME = "/ckrc";

void conf_values_initialize(Conf *c) {
  c->SCRT_dir = NULL;
  c->VC_dir = NULL;
}

int remove_newline(char buff[]) {
  int initLength = strlen(buff);
  for (int i = 0; i < initLength; i++) {
    if (buff[i] == '\n') {
      buff[i] = ' ';
    }
  }
  return strlen(buff);
}

char* read_next_line(FILE *f) {
  char nextLine[200];
  char *line;
  if (fgets(nextLine, 200, f) == NULL) {
    return NULL;
  }
  line = malloc(remove_newline(nextLine));
  strcpy(line, nextLine);
  return line;
}

int is_empty(const char *s) {
  while (*s != '\0') {
    if (!isspace((unsigned char)*s))
      return 0;
    s++;
  }
  return 1;
}

ConfVar match_variables(char *line, char matched[]) {
  if (line[0] == '#' || is_empty(line)) {
    return CV_NO_VAL_OR_COMMENT;
  }
#define X(var, str, name)                        \
  if (sscanf(line, str, matched) == 1) {         \
    return CV_##var;                             \
  }
  CONFIG_VARIABLES_TABLE
#undef X
  return -1;
}

int is_dir(char *path) {
  DIR *dir;
  dir = opendir(path);
  if (!dir) {
    return 0;
  }
  closedir(dir);
  return 1;
}

char *make_config_name(char * confPath) {
  char *db_path = strdup(confPath);

  db_path = realloc(db_path, strlen(confPath) + strlen(CONFIG_NAME)+1);
  strcat(db_path, CONFIG_NAME);

  return db_path;
}

ConfigParserResult parse(Conf *conf, UserOpt *opt) {
  conf_values_initialize(conf);
  FILE *confPtr;
  char *confName = make_config_name(opt->confDir);
  if ((confPtr = fopen(confName, "r")) == NULL) {
    free(confName);
    return CPR_NO_CONFIG_FILE;
  }
  free(confName);
  int flag = 1;
  char *line = read_next_line(confPtr);
  char matched[200];
  while (line != NULL) {
    switch(match_variables(line, matched)) {
#define X(var, str, name)                       \
      case CV_##var:                            \
        conf->var = malloc(strlen(matched)+1);  \
        strcpy(conf->var, matched);             \
        if (!is_dir(matched)) {                 \
          free(conf->var);                      \
          return CPR_WRONG_##var;               \
        }                                       \
        break;
      CONFIG_VARIABLES_TABLE
#undef X
    case CV_NO_VAL_OR_COMMENT:
      break;
    default:
      printf("%s:\n%s\n", "Config error in line", line);
    }
    free(line);
    line=read_next_line(confPtr);
  }
#define X(var, str)                               \
  if (conf->var == NULL) {                        \
    flag = 0;                                     \
    CONFIG_VARIABLES_TABLE
#undef X
  if (flag) {
    return CPR_OK;
  }
  return CPR_NO_CONFIG_FILE;
}

int config_file_parse(Conf *conf, UserOpt *opt) {
  switch (parse(conf, opt)) {
#define X(var,str,name)                                                 \
    case CPR_WRONG_##var:                                               \
      printf("Config error:\n"                                          \
             "%s: %s\n"                                                 \
             "defined in config does not exist\n", name, conf->var);    \
      return 0;                                                         \
    break;
    CONFIG_VARIABLES_TABLE
#undef X
  case CPR_NO_CONFIG_FILE:
    printf("The config file specified could not be found\n");
    return 0;
  case CPR_WRONG_CONFIG:
    printf("Config help\n");
  case CPR_OK:
    return 1;
  }
}

int init_create_config_file(UserOpt *opt) {
  struct stat st = {0};
  char tmp[200];
  if (stat(opt->argv[0], &st) == -1) {
    printf("Version control directory: %s\ndoes not exist.\n", opt->argv[0]);
    return 1;
  }

  if (stat(opt->argv[1], &st) == -1) {
    printf("Secret directory: %s\ndoes not exist.\n", opt->argv[1]);
    return 1;
  }
  
  if (stat(opt->confDir, &st)  == -1) {
    mkdir(opt->confDir, 0755);
  }

  char *confName = make_config_name(opt->confDir);
  FILE *f;
  if ((f = fopen(confName, "w")) == NULL) {
    free(confName);
    return 1;
  }
  
  strcpy(tmp, "version_control_dir = ");
  strcat(tmp, opt->argv[0]);
  strcat(tmp, "\n");
  fputs(tmp, f);

  strcpy(tmp, "secret_dir = ");
  strcat(tmp, opt->argv[1]);
  strcat(tmp, "\n");
  fputs(tmp, f);
  fclose(f);

  free(confName);
  return 0;
}