aboutsummaryrefslogtreecommitdiffstats
path: root/src/list.c
blob: 105c101041ad25cae4ee5a998c44168c6e11f5b4 (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
#include <libgen.h>

#include "actions.h"
#include "dblayer.h"
#include "queries.h"
#include "ckerrlog.h"

ListOpt list_make_options(cklist *args) {
  list_rewind(args);
  ListOpt listOpt = {
    ._lt = LT_TREE,
    ._lst = LST_PLAIN,
    .pName = NULL,
    .attr = 0,
    .bName = 0,
    .err = 0
  };

  if (list_size(args)) {
    do {
      if (strcmp(list_get(args), "-a") == 0) {
        listOpt.attr = 1;
        continue;
      }
      if (strcmp(list_get(args), "-b") == 0) {
        listOpt.bName = 1;
        continue;
      }
      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 if (strcmp(list_get(args), "ckconf") == 0) {
        listOpt._lt = LT_CKCONF;
      }
      else if (strcmp(list_get(args), "-p") == 0) {
        if (list_next(args)) {
          listOpt._lt = LT_PROG_CONFS;
          listOpt.pName = list_get(args);
        }
        else {
          listOpt.err = 1;
          break;
        }
      }
      else {
        listOpt.err = 1;
      }
    } while(list_next(args));
  }
  list_rewind(args);
  return listOpt;
}

int list_get_paths(DB *db, cklist *ckl, int bName, int attr, const char *home) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_form_query_select_paths_with_attributes(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    return -2;
  }

  while (sqlite3_step(stmt) == SQLITE_ROW) {
    char *tmp = strdup((char *)sqlite3_column_text(stmt, 0));
    char path[STR_L] = "";
    if (bName) {
      strcat(path, basename(tmp));
    }
    else {
      char tpath[STR_L] = "";
      if (swap_tilde_with_home(tpath, tmp, home)) {
          strcat(path, tpath);
        }
      else {
        strcat(path, tmp);
      }
    }
    free(tmp);
    if (attr) {
      decorate_entry(path, sqlite3_column_int(stmt, 1),
                     sqlite3_column_int(stmt, 2),
                     (char *)sqlite3_column_text(stmt, 0));
    }
    list_add(ckl, path);
  }
  sqlite3_finalize(stmt);

  return 1;
}

int list_get_programs(DB *db, cklist *ckl) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_form_query_select_programs(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    return -2;
  }

  while (sqlite3_step(stmt) == SQLITE_ROW) {
    list_add(ckl, (char *)sqlite3_column_text(stmt, 0));
  }
  sqlite3_finalize(stmt);

  return 1;
}

int list_get_path_program_tree(DB *db, cklist *ckl, int bName, int attr, const char *home) {
  sqlite3_stmt *stmt;
  int rc;

  char sql[STR_M] = "";
  dbh_form_query_select_programs(sql);

  rc = sqlite3_prepare_v2(db->ptr, sql, -1, &stmt, 0);
  if (rc != SQLITE_OK) {
    return -2;
  }

  while (sqlite3_step(stmt) == SQLITE_ROW) {
    char programName[STR_M] = "";
    strcat(programName, (char *)sqlite3_column_text(stmt, 0));
    strcat(programName, ":");
    list_add(ckl, programName);
    sqlite3_stmt *stmt2;
    int rc2;

    char sql2[STR_L] = "";

    char selection[STR_M] = COL_CONFIG_PATH;
    strcat(selection, ",");
    strcat(selection, COL_CONFIG_SECRET);
    strcat(selection, ",");
    strcat(selection, COL_CONFIG_PRIMARY);

    char condition[STR_M] = TBL_PROGRAM;
    strcat(condition, ".");
    strcat(condition, COL_PROGRAM_NAME);

    dbh_form_query_select_from_joined_eq(sql2, selection, condition);

    rc2 = sqlite3_prepare_v2(db->ptr, sql2, -1, &stmt2, 0);
    if (rc2 != SQLITE_OK) {
      return -2;
    }

    sqlite3_bind_text(stmt2, 1, (char *)sqlite3_column_text(stmt, 0), -1, 0);
    while (sqlite3_step(stmt2) == SQLITE_ROW) {
      char treePath[STR_L] = "|- ";
      char *tmp = strdup((char *)sqlite3_column_text(stmt2, 0));
      if (bName) {
        strcat(treePath, basename(tmp));
      }
      else {
        char tpath[STR_L] = "";
        if (swap_tilde_with_home(tpath, tmp, home)) {
          strcat(treePath, tpath);
        }
        else {
          strcat(treePath, tmp);
        }
      }
      free(tmp);
      if (attr) {
        decorate_entry(treePath, sqlite3_column_int(stmt2, 1),
                       sqlite3_column_int(stmt2, 2),
                       (char *)sqlite3_column_text(stmt2, 0));
      }
      list_add(ckl, treePath);
    }
    sqlite3_finalize(stmt2);
  }
  sqlite3_finalize(stmt);

  return 1;
}