summaryrefslogtreecommitdiff
path: root/src/cli_objects.c
blob: 9bb36f01102da507be9246867eaf53e755dba710 (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
337
338
339
340
341
342
343
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <unistd.h>
#include <dirent.h>
#include <limits.h>
#include <stdarg.h>
#include "cli_val.h"
#include "cli_parse.h"
#include <regex.h>

#include "cli_val_engine.h"

#include "cli_objects.h"

/************************ Storage area: *****************/

static char *at_string=NULL;
static boolean in_delete_action=FALSE;
static valstruct cli_value;
static boolean in_commit=FALSE; /* TRUE if in commit program*/
static boolean in_exec=FALSE; /* TRUE if in exec */
static boolean _is_echo=FALSE;
static boolean _is_silent_msg=FALSE;
static first_seg f_seg_a;
static first_seg f_seg_c;
static first_seg f_seg_m;
static int in_cond_tik=0;

/******************** Accessors: ************************/

static char at_buffer[1024]={0};

/* the string to use as $(@), must be set 
   before call to expand_string */
char* get_at_string(void) {
  if(at_string) {
    return at_string;
  } else {
    return at_buffer;
  }
}

void set_at_string(char* s) {
  if(s!=at_buffer) {
    at_string=s;
  } else {
    at_string=NULL;
  }
}

void free_at_string(void) {
  if(at_string) {
    if(at_string!=at_buffer) free(at_string);
    at_string=NULL;
  }
}

boolean is_in_delete_action(void) {
  return in_delete_action;
}

void set_in_delete_action(boolean b) {
  in_delete_action=b;
}

boolean is_in_commit(void) {
  return in_commit;
}

void set_in_commit(boolean b) {
  in_commit=b;
}

boolean is_in_exec(void) {
  return in_exec;
}

void set_in_exec(boolean b) {
  in_exec=b;
}

boolean is_echo(void) {
  return _is_echo;
}

void set_echo(boolean b) {
  _is_echo=b;
}

boolean is_silent_msg(void) {
  return _is_silent_msg;
}

void set_silent_msg(boolean b) {
  _is_silent_msg=b;
}

valstruct* get_cli_value_ptr(void) {
  return &cli_value;
}

first_seg* get_f_seg_a_ptr(void) {
  return &f_seg_a;
}

first_seg* get_f_seg_c_ptr(void) {
  return &f_seg_c;
}

first_seg* get_f_seg_m_ptr(void) {
  return &f_seg_m;
}

int is_in_cond_tik(void) {
  return in_cond_tik;
}

void set_in_cond_tik(int ict) {
  in_cond_tik=ict;
}

void dec_in_cond_tik(void) {
  --in_cond_tik;
}

const char* get_tdirp(void) {

  const char* tdirp=getenv(ENV_T_DIR);

  if (!tdirp)
    tdirp = DEF_T_DIR;
  if(!tdirp)
    tdirp="";

  return tdirp;
}

const char* get_cdirp(void) {
  return getenv(ENV_C_DIR);
}

const char* get_adirp(void) {

  const char* adirp=getenv(ENV_A_DIR);

  if (!adirp)
    adirp = DEF_A_DIR;
  if(!adirp)
    adirp="";

  return adirp;
}

const char* get_mdirp(void) {

  const char* mdirp=getenv(ENV_M_DIR);

  if(!mdirp)
    mdirp="";

  return mdirp;
}

const char* get_tmpp(void) {

  const char* tmpp=getenv(ENV_TMP_DIR);

  if(!tmpp)
    tmpp="";

  return tmpp;
}

char* get_elevp(void) {

  static char elevp_buffer[2049];
  static char* elevp=NULL;

  if(elevp==NULL) {

    const char* tmp=getenv(ENV_EDIT_LEVEL);

    if(tmp) {
      strncpy(elevp_buffer,tmp,sizeof(elevp_buffer)-1);
      elevp=elevp_buffer;
    }
  } 

  return elevp;
}

char* get_tlevp(void) {

  static char tlevp_buffer[2049];
  static char* tlevp=NULL;

  if(tlevp==NULL) {

    const char* tmp=getenv(ENV_TEMPLATE_LEVEL);

    if(tmp) {
      strncpy(tlevp_buffer,tmp,sizeof(tlevp_buffer)-1);
      tlevp=tlevp_buffer;
    }
  } 

  return tlevp;
}

/************************* Init ***************************/

void init_edit()
{
  int elevlen = 0;
  int tlevlen = 0;

  init_paths(TRUE);
  if (!get_elevp())
    bye("Not in configuration mode");
  if (!get_tlevp())
    bye("INTERNAL: environment var |%s|  is not set",ENV_TEMPLATE_LEVEL);
  elevlen = strlen(get_elevp());
  tlevlen = strlen(get_tlevp());
  if (elevlen > 0 && get_elevp()[elevlen - 1]=='/') {
    /* cut off terminateing slash */
    --elevlen;
    get_elevp()[elevlen] = 0;
  }
  if (elevlen) {
    char *slashp;
    char * scanp;
    if (*get_elevp()!='/')
	INTERNAL;
    scanp = get_elevp() + 1;
    while (TRUE) {
      slashp = strchr(scanp, '/');
      if (slashp)
	*slashp = 0;
      push_path(&m_path, scanp);
      if (slashp) {
	*slashp = '/';
	scanp = slashp+1;
      }else
	break;
    }
  }
  switch_path(MPATH);
  if (tlevlen > 0 && get_tlevp()[tlevlen - 1]=='/') {
    /* cut off terminateing slash */
    --tlevlen;
    get_tlevp()[tlevlen] = 0;
  }
  if (tlevlen) {
    char *slashp;
    char * scanp;
    if (*get_tlevp()!='/')
	INTERNAL;
    scanp = get_tlevp() + 1;
    while (TRUE) {
      slashp = strchr(scanp, '/');
      if (slashp)
	*slashp = 0;
      push_path(&t_path, scanp);
      if (slashp) {
	*slashp = '/';
	scanp = slashp+1;
      }else
	break;
    }
  }
}

void init_paths(boolean for_commit)
{
  struct stat    statbuf;
  const char* tdirp = get_tdirp();
  const char* cdirp = get_cdirp();
  const char* adirp = get_adirp();
  const char* mdirp = get_mdirp();
  const char* tmpp = get_tmpp();

  if (!mdirp || !mdirp[0])
    bye("Environment variable %s for temp configuration is not set",ENV_M_DIR);
  if (!cdirp)
    bye("INTERNAL: environment var |%s|  is not set",
	ENV_C_DIR);
  if (!tmpp || !tmpp[0])
    bye("INTERNAL: environment var |%s|  is not set",
	ENV_TMP_DIR);
  /* make sure that template root is present */

  if (lstat(tdirp, &statbuf) < 0)
    bye("Template directory |%s| isn't present\n", tdirp);
  if ((statbuf.st_mode & S_IFMT) != S_IFDIR) 
    bye("Template directory |%s| isn't a directory\n", tdirp);  
  /* set paths to current roots */
  if (for_commit) {
    int   max_len;
    const char *startp;

    /* make sure that master configuration root is present */
    if (lstat(adirp, &statbuf) < 0)
      bye("Master configuration directory |%s| isn't present\n", adirp);
    if ((statbuf.st_mode & S_IFMT) != S_IFDIR) 
      bye("Master configuration directory |%s| isn't a directory\n", adirp);  

    get_f_seg_a_ptr()->f_segp = adirp;
    get_f_seg_c_ptr()->f_segp = cdirp;
    get_f_seg_m_ptr()->f_segp = mdirp;
    get_f_seg_a_ptr()->f_seglen = strlen(adirp);
    get_f_seg_c_ptr()->f_seglen = strlen(cdirp);
    get_f_seg_m_ptr()->f_seglen = strlen(mdirp);
    if(get_f_seg_a_ptr()->f_seglen > get_f_seg_m_ptr()->f_seglen) {
      max_len = get_f_seg_a_ptr()->f_seglen;
      startp = adirp;
    }else{
      max_len = get_f_seg_m_ptr()->f_seglen;
      startp = mdirp;
    }
    if(get_f_seg_c_ptr()->f_seglen > max_len) {
      max_len = get_f_seg_c_ptr()->f_seglen;
      startp = cdirp;
    }      
    get_f_seg_a_ptr()->f_segoff = max_len - get_f_seg_a_ptr()->f_seglen;
    get_f_seg_c_ptr()->f_segoff = max_len - get_f_seg_c_ptr()->f_seglen;
    get_f_seg_m_ptr()->f_segoff = max_len - get_f_seg_m_ptr()->f_seglen;
    init_path(&m_path, startp);
    switch_path(get_f_seg_c_ptr());
    m_path.print_offset = max_len;
  }else
    init_path(&m_path, mdirp);

  init_path(&t_path, tdirp);
}

/**********************************************************/