Browse Source

Wrong/incompleted commands is logged now and machine interface retval is 2

Serj Kalichev 2 years ago
parent
commit
d1fcc97023
3 changed files with 40 additions and 15 deletions
  1. 5 0
      clish/shell/shell_execute.c
  2. 20 13
      clish/shell/shell_parse.c
  3. 15 2
      clish/shell/shell_tinyrl.c

+ 5 - 0
clish/shell/shell_execute.c

@@ -168,6 +168,11 @@ int clish_shell_execute(clish_context_t *context, char **out)
 	}
 
 	// Machine oriented protocol outputs return value
+	// Note there is some ugly architecture aspect - machine retval
+	// and logging are executed within clish_shell_tinyrl_key_enter()
+	// function also. This function is for completed
+	// commands but ...key_enter() do the same for wrong/incompleted
+	// commands.
 	clish_shell_machine_retval(this, result);
 
 	/* Unlock the lockfile */

+ 20 - 13
clish/shell/shell_parse.c

@@ -84,10 +84,10 @@ clish_pargv_status_e clish_shell_parse(
 	const clish_command_t **ret_cmd, clish_pargv_t **pargv)
 {
 	clish_pargv_status_e result = CLISH_BAD_CMD;
-	clish_context_t context;
-	const clish_command_t *cmd;
+	clish_context_t context = {};
+	const clish_command_t *cmd = NULL;
 	lub_argv_t *argv = NULL;
-	unsigned int idx;
+	unsigned int idx = 0;
 
 	*ret_cmd = cmd = clish_shell_resolve_command(this, line);
 	if (!cmd)
@@ -106,6 +106,7 @@ clish_pargv_status_e clish_shell_parse(
 		clish_command__get_paramv(cmd),
 		argv, &idx, NULL, 0);
 	lub_argv_delete(argv);
+
 	if (CLISH_LINE_OK != result) {
 		clish_pargv_delete(*pargv);
 		*pargv = NULL;
@@ -143,13 +144,13 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 	const lub_argv_t *argv,
 	unsigned *idx, clish_pargv_t *last, unsigned need_index)
 {
-	unsigned argc = lub_argv__get_count(argv);
-	unsigned index = 0;
-	unsigned nopt_index = 0;
+	unsigned int argc = lub_argv__get_count(argv);
+	unsigned int index = 0;
+	unsigned int nopt_index = 0;
 	clish_param_t *nopt_param = NULL;
-	unsigned i;
-	clish_pargv_status_e retval;
-	unsigned paramc = clish_paramv__get_count(paramv);
+	unsigned int i;
+	clish_pargv_status_e retval = CLISH_LINE_OK;
+	unsigned int paramc = clish_paramv__get_count(paramv);
 	int up_level = 0; /* Is it a first level of param nesting? */
 
 	assert(pargv);
@@ -186,7 +187,7 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 		if (last && (*idx == need_index) &&
 			(NULL == clish_pargv_find_arg(pargv, clish_param__get_name(param)))) {
 			if (is_switch) {
-				unsigned rec_paramc = clish_param__get_param_count(param);
+				unsigned int rec_paramc = clish_param__get_param_count(param);
 				for (i = 0; i < rec_paramc; i++) {
 					cparam = clish_param__get_param(param, i);
 					if (!cparam)
@@ -226,7 +227,7 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 			char *validated = NULL;
 			clish_paramv_t *rec_paramv =
 			    clish_param__get_paramv(param);
-			unsigned rec_paramc =
+			unsigned int rec_paramc =
 			    clish_param__get_param_count(param);
 
 			/* Save the index of last non-option parameter
@@ -328,8 +329,8 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 
 	/* Check for non-optional parameters without values */
 	if ((*idx >= argc) && (index < paramc)) {
-		unsigned j = index;
-		const clish_param_t *param;
+		unsigned int j = index;
+		const clish_param_t *param = NULL;
 		while (j < paramc) {
 			param = clish_paramv__get_param(paramv, j++);
 			if (BOOL_TRUE != clish_param__get_optional(param))
@@ -389,6 +390,12 @@ clish_pargv_status_e clish_shell_parse_pargv(clish_pargv_t *pargv,
 		lub_string_free(args);
 	}
 
+	/* If command has no actions at all consider it as incompleted.
+	 * Often it's a command parts like "no" prefix for "no iface" command.
+	 */
+	if (clish_command_is_incomplete(cmd))
+		return CLISH_LINE_PARTIAL;
+
 	return CLISH_LINE_OK;
 }
 

+ 15 - 2
clish/shell/shell_tinyrl.c

@@ -295,7 +295,7 @@ static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
 			errmsg = "Illegal parameter";
 			break;
 		case CLISH_LINE_PARTIAL:
-			errmsg = "The command is not completed";
+			errmsg = "Incompleted command";
 			break;
 		default:
 			errmsg = "Unknown problem";
@@ -315,8 +315,21 @@ static bool_t clish_shell_tinyrl_key_enter(tinyrl_t *this, int key)
 			"%s\n", fname, shell->current_file->line, line, errmsg);
 		}
 		// Wrong line must return bad retval for machine oriented proto
-		// Let retval=2 means wrong command
+		// Let retval=2 means wrong/incompleted command.
+		// Note there is some ugly architecture aspect - machine retval
+		// and logging are executed within clish_shell_execute()
+		// function also. The clish_shell_execute() is for completed
+		// commands but this function for wrong/incompleted commands.
 		clish_shell_machine_retval(shell, 2);
+		/* Call logging callback */
+		if (clish_shell__get_log(shell) &&
+			clish_shell_check_hook(context, CLISH_SYM_TYPE_LOG)) {
+			char *s = NULL;
+			lub_string_cat(&s, "Syntax error: ");
+			lub_string_cat(&s, line);
+			clish_shell_exec_log(context, s, 2);
+			lub_string_free(s);
+		}
 	}
 
 	tinyrl_done(this);