aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/actionhelper.c7
-rw-r--r--src/actionparser.c12
-rw-r--r--src/actions.c46
3 files changed, 51 insertions, 14 deletions
diff --git a/src/actionhelper.c b/src/actionhelper.c
index 9710ef8..e0876eb 100644
--- a/src/actionhelper.c
+++ b/src/actionhelper.c
@@ -198,11 +198,14 @@ void print_LIST_result(int err) {
}
void print_SEARCH_result(int err) {
+ if (err == 2) {
+ printf("No grep avaliable. Please make sure you have grep installed.\n");
+ return;
+ }
if (!err) {
- printf("succes\n");
return;
}
- printf("Not Supported\n");
+ printf("Wrong search.\n");
}
void print_HELP_result(int err) {
diff --git a/src/actionparser.c b/src/actionparser.c
index a1fd1ef..580eb01 100644
--- a/src/actionparser.c
+++ b/src/actionparser.c
@@ -123,7 +123,15 @@ int parse_LIST(UserOpt *opt) {
return 1;
}
int parse_SEARCH(UserOpt *opt) {
- return -1;
+ /* Search expects a maximum of 1 argument */
+ if (optNum > pos + 1) {
+ opt->err = PERR_SEARCH_WRONG;
+ return -1;
+ }
+
+ int arg_num = optNum - pos;
+ fill_args_list(arg_num, opt);
+ return 1;
}
int parse_HELP(UserOpt *opt) {
return -1;
@@ -272,7 +280,7 @@ void print_parser_error(UserOpt *opt) {
sprintf(errStr, "List programs, configs and more\nUsage: %s value-to-list (or tree) [-t list-type]", names);
break;
case PERR_SEARCH_WRONG:
- sprintf(errStr, "Usage: .............");
+ sprintf(errStr, "Search through the configs with grep\nUsage: %s search-term", names);
break;
case PERR_HELP_WRONG:
sprintf(errStr, "Usage: ........");
diff --git a/src/actions.c b/src/actions.c
index e75c885..efffc1b 100644
--- a/src/actions.c
+++ b/src/actions.c
@@ -41,7 +41,7 @@ int run_ADD(UserOpt * opt, Conf *conf) {
if (db.error == SQL_ERR_NO_TABLES) {
PRINT_ERR("The database file is currupted. Run ck init anew.");
}
- return 1;
+ goto error;
}
AddOpt addOpt = add_make_options(opt->args);
switch (addOpt.err) {
@@ -49,26 +49,25 @@ int run_ADD(UserOpt * opt, Conf *conf) {
break;
case ADD_ERR_WRONG_CONFIG:
PRINT_ERR("The config file specified doesn't exist.");
- close_DB(&db);
- return 1;
+ goto error;
case ADD_ERR_WRONG_FLAGS:
PRINT_ERR("Flags are: -s for secret and -p for primary.");
- close_DB(&db);
- return 1;
+ goto error;
}
add_print_opts(&addOpt);
/* Try adding the new config to the DB */
if (add_transaction_try(&db, &addOpt)) {
- close_DB(&db);
- return 1;
+ goto error;
}
- close_DB(&db);
add_make_link(&addOpt, conf);
char err[STR_M];
if (add_err_message(err)) {
PRINT_ERR(err);
+ error:
+ close_DB(&db);
return 1;
}
+ close_DB(&db);
return 0;
}
@@ -175,11 +174,38 @@ int run_LIST(UserOpt *opt, Conf *conf) {
return 0;
}
+FILE *popen(const char *command, const char *mode);
+int pclose(FILE *stream);
+
int run_SEARCH(UserOpt *opt, Conf *conf) {
- printf("Running %s\n", "search");
+ if (system("which grep > /dev/null") != 0) {
+ return 2;
+ }
DB db = open_DB(opt);
+ cklist *paths = list_make_new();
+ list_get_paths(&db, paths);
close_DB(&db);
- return 1;
+ if (list_size(paths)) {
+ do {
+ FILE *cmd;
+ char result[4096] = "";
+ char command[STR_L] = "grep -H -n ";
+ strcat(command, list_get(opt->args));
+ strcat(command, " ");
+ strcat(command, list_get(paths));
+ cmd = popen(command, "r");
+ if (cmd == NULL) {
+ list_free(paths);
+ return 1;
+ }
+ while (fgets(result, sizeof(result), cmd)) {
+ printf("%s", result);
+ }
+ pclose(cmd);
+ } while(list_next(paths));
+ }
+ list_free(paths);
+ return 0;
}
int run_HELP(UserOpt *opt, Conf *conf) {