schema BUGFIX several tests and bugfixes of lys_getnext()
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 21fc337..cabccb2 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -53,7 +53,7 @@
                 next = last = (const struct lysc_node*)&((struct lysc_node_choice*)parent)->cases[0];
             } else {
                 snode = lysc_node_children_p(parent, (options & LYS_GETNEXT_OUTPUT) ? LYS_CONFIG_R : LYS_CONFIG_W);
-                /* do not return anything if the augment does not have any children */
+                /* do not return anything if the node does not have any children */
                 if (!snode || !(*snode)) {
                     return NULL;
                 }
@@ -66,15 +66,12 @@
         if (!next) {
             /* try to get action or notification */
             goto repeat;
-        } else if (!(options & LYS_GETNEXT_NOSTATECHECK)) {
-            if (!lys_is_disabled(next, 0)) {
-                return next;
-            }
-            /* continue to find next available node */
-        } else {
-            return next;
         }
+        /* test if the next can be returned */
+        goto check;
+
     } else if (last->nodetype == LYS_ACTION) {
+        action_flag = 1;
         if (last->parent) {
             actions = lysc_node_actions(last->parent);
         } else {
@@ -90,6 +87,7 @@
         }
         goto repeat;
     } else if (last->nodetype == LYS_NOTIF) {
+        action_flag = notif_flag = 1;
         if (last->parent) {
             notifs = lysc_node_notifs(last->parent);
         } else {
@@ -115,7 +113,7 @@
     }
     if (!next) {
         /* possibly go back to parent */
-        if (last->parent != parent) {
+        if (last && last->parent != parent) {
             last = last->parent;
             next = last->next;
         } else if (!action_flag) {
@@ -129,6 +127,7 @@
         }
         goto repeat;
     }
+check:
     switch (next->nodetype) {
     case LYS_ACTION:
     case LYS_NOTIF:
@@ -158,7 +157,7 @@
         } else {
             /* go into */
             if (options & LYS_GETNEXT_WITHCASE) {
-                next = (struct lysc_node*)&((struct lysc_node_choice *)next)->cases;
+                next = (struct lysc_node*)((struct lysc_node_choice *)next)->cases;
             } else {
                 next = ((struct lysc_node_choice *)next)->cases->child;
             }
diff --git a/src/tree_schema_compile.c b/src/tree_schema_compile.c
index c2a6a54..bbf0d9b 100644
--- a/src/tree_schema_compile.c
+++ b/src/tree_schema_compile.c
@@ -3082,6 +3082,12 @@
     struct lysp_node *child_p;
     unsigned int u;
 
+    if (options & LYSC_OPT_RPC_MASK) {
+        LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LYVE_SEMANTICS,
+               "Action \"%s\" is placed inside another RPC/action.", action_p->name);
+        return LY_EVALID;
+    }
+
     action->nodetype = LYS_ACTION;
     action->module = ctx->mod;
     action->parent = parent;
@@ -3104,14 +3110,14 @@
     COMPILE_ARRAY_GOTO(ctx, action_p->input.musts, action->input.musts, options, u, lys_compile_must, ret, cleanup);
     COMPILE_ARRAY_GOTO(ctx, action_p->input.exts, action->input.exts, options, u, lys_compile_ext, ret, cleanup);
     LY_LIST_FOR(action_p->input.data, child_p) {
-        LY_CHECK_RET(lys_compile_node(ctx, child_p, options & LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status));
+        LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_INPUT, (struct lysc_node*)action, uses_status));
     }
 
     /* output */
     COMPILE_ARRAY_GOTO(ctx, action_p->output.musts, action->output.musts, options, u, lys_compile_must, ret, cleanup);
     COMPILE_ARRAY_GOTO(ctx, action_p->output.exts, action->output.exts, options, u, lys_compile_ext, ret, cleanup);
     LY_LIST_FOR(action_p->output.data, child_p) {
-        LY_CHECK_RET(lys_compile_node(ctx, child_p, options & LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status));
+        LY_CHECK_RET(lys_compile_node(ctx, child_p, options | LYSC_OPT_RPC_OUTPUT, (struct lysc_node*)action, uses_status));
     }
 
 cleanup:
@@ -3728,17 +3734,18 @@
  * @param[in] parent Parent node holding the children list, in case of node from a choice's case,
  * the choice itself is expected instead of a specific case node.
  * @param[in] node Schema node to connect into the list.
+ * @param[in] options Compile options to distinguish input/output when placing node into RPC/action.
  * @return LY_ERR value - LY_SUCCESS or LY_EEXIST.
  */
 static LY_ERR
-lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node)
+lys_compile_node_connect(struct lysc_ctx *ctx, struct lysc_node *parent, struct lysc_node *node, int options)
 {
     struct lysc_node **children;
 
     if (node->nodetype == LYS_CASE) {
         children = (struct lysc_node**)&((struct lysc_node_choice*)parent)->cases;
     } else {
-        children = lysc_node_children_p(parent, node->flags);
+        children = lysc_node_children_p(parent, options);
     }
     if (children) {
         if (!(*children)) {
@@ -3848,7 +3855,7 @@
     cs->module = ctx->mod;
     cs->prev = (struct lysc_node*)cs;
     cs->nodetype = LYS_CASE;
-    lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs);
+    lys_compile_node_connect(ctx, (struct lysc_node*)ch, (struct lysc_node*)cs, options);
     cs->parent = (struct lysc_node*)ch;
     cs->child = child;
 
@@ -4094,10 +4101,10 @@
 
         /* compile the children */
         if (node_p->nodetype != LYS_CASE) {
-            LY_CHECK_RET(lys_compile_node(ctx, node_p, options, target, 0));
+            LY_CHECK_RET(lys_compile_node(ctx, node_p, options | flags, target, 0));
         } else {
             LY_LIST_FOR(((struct lysp_node_case *)node_p)->child, case_node_p) {
-                LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options, target, 0));
+                LY_CHECK_RET(lys_compile_node(ctx, case_node_p, options | flags, target, 0));
             }
         }
 
@@ -4140,13 +4147,13 @@
 
     switch (target->nodetype) {
     case LYS_CONTAINER:
-        COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)node)->actions, target,
-                            options, u, lys_compile_action, 0, ret, error);
+        COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_container*)target)->actions, target,
+                            options | flags, u, lys_compile_action, 0, ret, error);
         /* TODO notifications */
         break;
     case LYS_LIST:
-        COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)node)->actions, target,
-                            options, u, lys_compile_action, 0, ret, error);
+        COMPILE_ARRAY1_GOTO(ctx, aug_p->actions, ((struct lysc_node_list*)target)->actions, target,
+                            options | flags, u, lys_compile_action, 0, ret, error);
         /* TODO notifications */
         break;
     default:
@@ -4753,7 +4760,7 @@
         } else { /* other than choice */
             node->parent = parent;
         }
-        LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node), LY_EVALID);
+        LY_CHECK_RET(lys_compile_node_connect(ctx, parent->nodetype == LYS_CASE ? parent->parent : parent, node, options), LY_EVALID);
     } else {
         /* top-level element */
         if (!ctx->mod->compiled->data) {
diff --git a/src/tree_schema_helpers.c b/src/tree_schema_helpers.c
index 6b89cb3..f5b9174 100644
--- a/src/tree_schema_helpers.c
+++ b/src/tree_schema_helpers.c
@@ -95,6 +95,7 @@
     const struct lys_module *mod;
     const char *nodeid_type;
     int getnext_extra_flag = 0;
+    int current_nodetype = 0;
 
     assert(nodeid);
     assert(target);
@@ -147,11 +148,15 @@
             /* move through input/output manually */
             if (!strncmp("input", name, name_len)) {
                 (*result_flag) |= LYSC_OPT_RPC_INPUT;
-            } else if (!strncmp("input", name, name_len)) {
+            } else if (!strncmp("output", name, name_len)) {
                 (*result_flag) |= LYSC_OPT_RPC_OUTPUT;
                 getnext_extra_flag = LYS_GETNEXT_OUTPUT;
+            } else {
+                goto getnext;
             }
+            current_nodetype = LYS_INOUT;
         } else {
+getnext:
             context_node = lys_child(context_node, mod, name, name_len, 0,
                                      getnext_extra_flag | LYS_GETNEXT_NOSTATECHECK | LYS_GETNEXT_WITHCHOICE | LYS_GETNEXT_WITHCASE);
             if (!context_node) {
@@ -160,8 +165,9 @@
                 return LY_ENOTFOUND;
             }
             getnext_extra_flag = 0;
+            current_nodetype = context_node->nodetype;
 
-            if (context_node->nodetype == LYS_NOTIF) {
+            if (current_nodetype == LYS_NOTIF) {
                 (*result_flag) |= LYSC_OPT_NOTIFICATION;
             }
         }
@@ -179,7 +185,10 @@
 
     if (ret == LY_SUCCESS) {
         *target = context_node;
-        if (nodetype && !(context_node->nodetype & nodetype)) {
+        if (nodetype & LYS_INOUT) {
+            /* instead of input/output nodes, the RPC/action node is actually returned */
+        }
+        if (nodetype && !(current_nodetype & nodetype)) {
             return LY_EDENIED;
         }
     } else {