summaryrefslogtreecommitdiff
path: root/src/set.c
blob: a201fe790fde2ec753a1604be1285f590c696562 (plain)
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
331
332
333
334
335
336
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>

#include "cli_val.h"
#include "cli_objects.h"
#include "cli_path_utils.h"

static void make_dir(void);
static void handle_defaults(void);

static void make_dir()
{
  touch_dir(m_path.path);
}
/***************************************************
  set_validate:
    validate value against definition
    return TRUE if OK, FALSE otherwise
****************************************************/
boolean set_validate(vtw_def *defp, char *valp, boolean empty_val)
{  
  boolean res;
  int status;
  struct stat    statbuf;
  char* path_end=NULL;

  if (!empty_val) {
    int i = 0;
    int val_len = strlen(valp);

    for (i = 0; i < val_len; i++) {
      if (valp[i] == '\'') {
        fprintf(out_stream, "Cannot use the \"'\" (single quote) character "
                            "in a value string\n");
        exit(1);
      }
    }

    {
      clind_path_ref tp = clind_path_construct(t_path.path);
      if(tp) {
	path_end=clind_path_pop_string(tp);
      }
      clind_path_destruct(&tp);
    }

    if (strcmp(path_end, TAG_NAME) == 0) {
      /* it was a tag, so the def is at 1 level up */
      pop_path(&t_path);
    } else {
      /* it's actual value, so the tmpl path is fine */
      free(path_end);
      path_end = NULL;
    }
  }
  push_path(&t_path, DEF_NAME);
  if (lstat(t_path.path, &statbuf) < 0 || 
      (statbuf.st_mode & S_IFMT) != S_IFREG) {
    fprintf(out_stream, "The specified configuration node is not valid\n");
    bye("Can not set value (4), no definition for %s, template %s", 
        m_path.path,t_path.path);
  }
  /* defniition present */
  memset(defp, 0, sizeof(vtw_def));
  if ((status = parse_def(defp, t_path.path, FALSE)))
    exit(status);
  pop_path(&t_path);
  if(path_end) {
    push_path(&t_path,path_end);
    free(path_end);
    path_end=NULL;
  }
  if (defp->def_type == ERROR_TYPE) {
    /* no type in def. setting value not allowed. */
    fprintf(out_stream, "The specified configuration node is not valid\n");
    bye("Can not set value: no type for %s, template %s", 
        m_path.path, t_path.path);
  }
  if (empty_val) {
    if (defp->def_type != TEXT_TYPE || defp->tag || defp->multi){
      printf("Empty string may be assigned only to TEXT type leaf node\n");
      fprintf(out_stream, "Empty value is not allowed\n");
      return FALSE;
    }
    return TRUE;
  }
  res = validate_value(defp, valp);
  return res;
}

int main(int argc, char **argv)
{
  
  int ai;
  struct stat    statbuf;
  vtw_def        def;
  boolean        last_tag;
  int            status;
  FILE          *fp;
  boolean        res;
  char          *cp;
  char          *command;
  boolean       need_mod = FALSE, not_new = FALSE;
  boolean       empty_val = FALSE;

  if (initialize_output() == -1) {
    exit(-1);
  }

  dump_log( argc, argv);
  init_edit();
  last_tag = FALSE;

  /* extend both paths per arguments given */
  /* last argument is new value */
  for (ai = 1; ai < argc; ++ai) {
    if (!*argv[ai]) { /* empty string */
      if (ai < argc -1) {
        fprintf(out_stream, "Empty value is not allowed after \"%s\"\n",
                argv[ai - 1]);
	bye("empty string in argument list \n");
      }
      empty_val = TRUE;
      last_tag = FALSE;
      break;
    }
    push_path(&t_path, argv[ai]);
    push_path(&m_path, argv[ai]);
    if (lstat(t_path.path, &statbuf) >= 0) {
      if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
	bye("INTERNAL:regular file %s in templates", t_path.path);
      }
      last_tag = FALSE;
      continue;
    } /*else */
    pop_path(&t_path);
    push_path(&t_path, TAG_NAME);
    if (lstat(t_path.path, &statbuf) >= 0) {
      if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
	bye("INTERNAL:regular file %s in templates", t_path.path);
      }
      last_tag = TRUE;
      /* every time tag match, need to verify*/
      if(!set_validate(&def, argv[ai], FALSE)) {
	exit(1);
      }
      continue;
    } 
    /* no match */
    break;
  }

  if (ai == argc) {
    /* full path found */
    /* every tag match validated already */
    /* non tag matches are OK by definition */
    /* do we already have it? */
    if (lstat(m_path.path, &statbuf) >= 0) {
      bye("Already exists %s", m_path.path + strlen(get_mdirp()));
    }
    /* else */
    /* prevent value node without actual value */
    push_path(&t_path, DEF_NAME);
    if (lstat(t_path.path, &statbuf) >= 0) {
      memset(&def, 0, sizeof(vtw_def));
      if ((status = parse_def(&def, t_path.path, FALSE)))
	exit(status);
      if (def.def_type != ERROR_TYPE && !def.tag) {
        fprintf(out_stream,
                "The specified configuration node requires a value\n");
	bye("Must provide actual value\n");
      }
      if (def.def_type == ERROR_TYPE && !def.tag) {
	pop_path(&t_path);
	if(!validate_value(&def, "")) {
	  exit(1);
	}
	push_path(&t_path, DEF_NAME);
      }
    }
    touch();
    pop_path(&t_path);
    make_dir();
    handle_defaults();
    exit(0); 
  }
  if(ai < argc -1 || last_tag) {
    fprintf(stderr, "There is no appropriate template for %s",
            m_path.path + strlen(get_mdirp()));
    fprintf(out_stream, "The specified configuration node is not valid\n");
    exit(1);
  }
  /*ai == argc -1, must be actual value */
  if (!empty_val) {
    pop_path(&m_path); /* pop the actual value at the end */
    pop_path(&t_path); /* pop the "node.tag" */
  }

  if(!set_validate(&def, argv[argc-1], empty_val)) {
    exit(1);
  }
    push_path(&m_path, VAL_NAME);
  /* set value */
  if (lstat(m_path.path, &statbuf) >= 0) {
    valstruct new_value, old_value;
    not_new = TRUE;
    if ((statbuf.st_mode & S_IFMT) != S_IFREG)
      bye("Not a regular file at path \"%s\"", m_path.path);
    /* check if this new value */
    status = char2val(&def, argv[argc - 1], &new_value);
    if (status)
      exit(0);
    cp = NULL;
    pop_path(&m_path); /* get_value will push VAL_NAME */
    status = get_value(&cp, &m_path);
    if (status != VTWERR_OK)
      bye("Cannot read old value %s\n", m_path.path);
    status = char2val(&def, cp, &old_value);
    if (status != VTWERR_OK)
      bye("Corrupted old value ---- \n%s\n-----\n", cp);
    res = val_cmp(&new_value, &old_value, IN_COND);
    if (res) {
      if (def.multi) {
        bye("Already in multivalue");
      } else {
        bye("The same value \"%s\" for path \"%s\"\n", cp, m_path.path);
      }
    }
  } else {
    pop_path(&m_path);
  }
  make_dir(); 
  push_path(&m_path, VAL_NAME);
  if(not_new && !def.multi) {
    /* it is not multi and seen from M */
    /* is it in C */
    switch_path(CPATH);
    if (lstat(m_path.path, &statbuf) < 0) 
      /* yes, we are modifying original value */
      need_mod = TRUE;
    switch_path(MPATH);
  }
  touch();
  /* in case of multi we always append, never overwrite */
  /* in case of single we always overwrite */
  /* append and overwrite work the same for new file */
  fp = fopen(m_path.path, def.multi?"a":"w"); 
  if (fp == NULL)
    bye("Can not open value file %s", m_path.path);
  if (fputs(argv[argc-1], fp) < 0 || fputc('\n',fp) < 0)
    bye("Error writing file %s", m_path.path);
  if (need_mod) {
    pop_path(&m_path); /* get rid of "value" */
    command = my_malloc(strlen(m_path.path) + 30, "set");
    sprintf(command, "touch %s/" MOD_NAME, m_path.path);
    system(command);
  }
  return 0;
}
/**********************************************
   handle_defaults:
     now deal with defaults for children 
     if child has definition and not tag, nor multi, and 
     has type, and has default, and not have value 
     already, make a default value
*/
    

static void handle_defaults()
{
  DIR    *dp;
  int     status;
  struct dirent *dirp;
  struct stat statbuf;
  FILE          *fp;
  vtw_def        def;
  char     *uename;

  if ((dp = opendir(t_path.path)) == NULL){
    INTERNAL;
  }
  while ((dirp = readdir(dp)) != NULL) {
    if (strcmp(dirp->d_name, ".")==0 ||
	strcmp(dirp->d_name, "..")==0 ||
	strcmp(dirp->d_name, MOD_NAME) == 0 ||
	strcmp(dirp->d_name, DEF_NAME)==0)
      continue;
    uename = clind_unescape(dirp->d_name);
    push_path(&t_path, uename);
    if (lstat(t_path.path, &statbuf) < 0) {
      bye("Cannot stat template directory %s\n", 
	  t_path.path);
    }
    if ((statbuf.st_mode & S_IFMT) != S_IFDIR) {
      bye("Non directory file %s\n", t_path.path);
    }
    push_path(&t_path, DEF_NAME);
    if (lstat(t_path.path, &statbuf) < 0) {
      /* no definition */
      pop_path(&t_path); /* definition */
      pop_path(&t_path); /* child */
      continue;
    }
    memset(&def, 0, sizeof(def));
    if ((status = parse_def(&def, t_path.path, 
			    FALSE)))
      exit(status);
    if (def.def_default) {
      push_path(&m_path, uename);
      push_path(&m_path, VAL_NAME);
      if (lstat(m_path.path, &statbuf) < 0) {
	/* no value, write one */
	pop_path(&m_path);
	make_dir();/* make sure directory exist */
	push_path(&m_path, VAL_NAME);
	fp = fopen(m_path.path, "w");
	if (fp == NULL)
	  bye("Can not open value file %s", m_path.path);
	if (fputs(def.def_default, fp) < 0 || 
	    fputc('\n',fp) < 0)
	  bye("Error writing file %s", m_path.path);
	fclose(fp);
      }
      pop_path(&m_path); /* value */
      pop_path(&m_path); /* child */
    }
    free_def(&def);
    pop_path(&t_path); /* definition */
    pop_path(&t_path); /* child */
  }
}