libyang CHANGE extend input handler by line counter

So far, all the parsers were counting lines on their own, in the
parsers' specific variables. The patch unifies the place where the
line is counted and store it in the input handler.
diff --git a/src/xml.c b/src/xml.c
index 8a1a7a2..6f87b81 100644
--- a/src/xml.c
+++ b/src/xml.c
@@ -32,10 +32,10 @@
 #include "tree_data.h"
 
 /* Move input p by s characters, if EOF log with lyxml_ctx c */
-#define move_input(c, s) ly_in_skip(c->in, s); LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VLOG_LINE, &c->line, LY_VCODE_EOF), LY_EVALID)
+#define move_input(c, s) ly_in_skip(c->in, s); LY_CHECK_ERR_RET(!c->in->current[0], LOGVAL(c->ctx, LY_VLOG_LINE, &c->in->line, LY_VCODE_EOF), LY_EVALID)
 
 /* Ignore whitespaces in the input string p */
-#define ign_xmlws(c) while (is_xmlws(*(c)->in->current)) {if (*(c)->in->current == '\n') {++c->line;} ly_in_skip(c->in, 1);}
+#define ign_xmlws(c) while (is_xmlws(*(c)->in->current)) {ly_in_skip(c->in, 1);}
 
 static LY_ERR lyxml_next_attr_content(struct lyxml_ctx *xmlctx, const char **value, size_t *value_len, ly_bool *ws_only,
         ly_bool *dynamic);
@@ -47,18 +47,14 @@
  * Returns Boolean value whether delim was found or not.
  */
 static ly_bool
-ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *newlines, size_t *parsed)
+ign_todelim(register const char *input, const char *delim, size_t delim_len, size_t *parsed)
 {
     size_t i;
     register const char *a, *b;
 
-    (*newlines) = 0;
     (*parsed) = 0;
     for ( ; *input; ++input, ++(*parsed)) {
         if (*input != *delim) {
-            if (*input == '\n') {
-                ++(*newlines);
-            }
             continue;
         }
         a = input;
@@ -103,10 +99,10 @@
 
     /* check NameStartChar (minus colon) */
     LY_CHECK_ERR_RET(ly_getutf8(&in, &c, &parsed),
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INCHAR, in[0]),
+            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INCHAR, in[0]),
             LY_EVALID);
     LY_CHECK_ERR_RET(!is_xmlqnamestartchar(c),
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
+            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX,
             "Identifier \"%s\" starts with an invalid character.", in - parsed),
             LY_EVALID);
 
@@ -116,7 +112,7 @@
         ly_in_skip(xmlctx->in, parsed);
 
         rc = ly_getutf8(&in, &c, &parsed);
-        LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INCHAR, in[0]), LY_EVALID);
+        LY_CHECK_ERR_RET(rc, LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INCHAR, in[0]), LY_EVALID);
     } while (is_xmlqnamechar(c));
 
     *start = s;
@@ -227,7 +223,7 @@
 {
     const struct ly_ctx *ctx = xmlctx->ctx; /* shortcut */
     const char *endtag, *sectname;
-    size_t endtag_len, newlines, parsed;
+    size_t endtag_len, parsed;
     ly_bool rc;
 
     while (1) {
@@ -236,12 +232,12 @@
         if (xmlctx->in->current[0] == '\0') {
             /* EOF */
             if (xmlctx->elements.count) {
-                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
                 return LY_EVALID;
             }
             return LY_SUCCESS;
         } else if (xmlctx->in->current[0] != '<') {
-            LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
+            LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
                     xmlctx->in->current, "element tag start ('<')");
             return LY_EVALID;
         }
@@ -264,21 +260,19 @@
                 endtag_len = ly_strlen_const("]]>");
             } else if (!strncmp(xmlctx->in->current, "DOCTYPE", ly_strlen_const("DOCTYPE"))) {
                 /* Document type declaration - not supported */
-                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NSUPP, "Document Type Declaration");
+                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_NSUPP, "Document Type Declaration");
                 return LY_EVALID;
             } else {
-                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".",
+                LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX, "Unknown XML section \"%.20s\".",
                         &xmlctx->in->current[-2]);
                 return LY_EVALID;
             }
-            rc = ign_todelim(xmlctx->in->current, endtag, endtag_len, &newlines, &parsed);
-            LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NTERM, sectname), LY_EVALID);
-            xmlctx->line += newlines;
+            rc = ign_todelim(xmlctx->in->current, endtag, endtag_len, &parsed);
+            LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_NTERM, sectname), LY_EVALID);
             ly_in_skip(xmlctx->in, parsed + endtag_len);
         } else if (xmlctx->in->current[0] == '?') {
-            rc = ign_todelim(xmlctx->in->current, "?>", 2, &newlines, &parsed);
-            LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
-            xmlctx->line += newlines;
+            rc = ign_todelim(xmlctx->in->current, "?>", 2, &parsed);
+            LY_CHECK_ERR_RET(rc, LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_NTERM, "Declaration"), LY_EVALID);
             ly_in_skip(xmlctx->in, parsed + 2);
         } else {
             /* other non-WS character */
@@ -405,7 +399,7 @@
                     buf[len++] = '\"';
                     in += ly_strlen_const("&quot;");
                 } else {
-                    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
+                    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX,
                             "Entity reference \"%.*s\" not supported, only predefined references allowed.", 10, &in[offset - 1]);
                     goto error;
                 }
@@ -430,18 +424,18 @@
                         n = (LY_BASE_HEX * n) + u;
                     }
                 } else {
-                    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
+                    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX, "Invalid character reference \"%.*s\".", 12, p);
                     goto error;
 
                 }
 
                 LY_CHECK_ERR_GOTO(in[offset] != ';',
-                        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP,
+                        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP,
                         LY_VCODE_INSTREXP_len(&in[offset]), &in[offset], ";"),
                         error);
                 ++offset;
                 LY_CHECK_ERR_GOTO(ly_pututf8(&buf[len], n, &u),
-                        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
+                        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX,
                         "Invalid character reference \"%.*s\" (0x%08x).", 12, p, n),
                         error);
                 len += u;
@@ -471,7 +465,7 @@
 
             /* log lines */
             if (in[offset] == '\n') {
-                ++xmlctx->line;
+                LY_IN_NEW_LINE(xmlctx->in);
             }
 
             /* continue */
@@ -480,7 +474,7 @@
     }
 
     /* EOF reached before endchar */
-    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+    LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
 
 error:
     free(buf);
@@ -497,7 +491,7 @@
     *length = len;
     *ws_only = ws;
 
-    ly_in_skip(xmlctx->in, in - xmlctx->in->current);
+    xmlctx->in->current = in;
     return LY_SUCCESS;
 
 #undef BUFSIZE
@@ -523,7 +517,7 @@
 
     /* match opening and closing element tags */
     if (!xmlctx->elements.count) {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
                 name_len, name);
         return LY_EVALID;
     }
@@ -531,7 +525,7 @@
     e = (struct lyxml_elem *)xmlctx->elements.objs[xmlctx->elements.count - 1];
     if ((e->prefix_len != prefix_len) || (e->name_len != name_len) ||
             (prefix_len && strncmp(prefix, e->prefix, e->prefix_len)) || strncmp(name, e->name, e->name_len)) {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX,
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX,
                 "Opening (\"%.*s%s%.*s\") and closing (\"%.*s%s%.*s\") elements tag mismatch.",
                 e->prefix_len, e->prefix ? e->prefix : "", e->prefix ? ":" : "", e->name_len, e->name,
                 prefix_len, prefix ? prefix : "", prefix ? ":" : "", name_len, name);
@@ -554,7 +548,7 @@
 
     /* parse closing tag */
     if (xmlctx->in->current[0] != '>') {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
                 xmlctx->in->current, "element tag termination ('>')");
         return LY_EVALID;
     }
@@ -659,10 +653,10 @@
 
     /* skip '=' */
     if (xmlctx->in->current[0] == '\0') {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
         return LY_EVALID;
     } else if (xmlctx->in->current[0] != '=') {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
                 xmlctx->in->current, "'='");
         return LY_EVALID;
     }
@@ -673,10 +667,10 @@
 
     /* find quotes */
     if (xmlctx->in->current[0] == '\0') {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
         return LY_EVALID;
     } else if ((xmlctx->in->current[0] != '\'') && (xmlctx->in->current[0] != '\"')) {
-        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
+        LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(xmlctx->in->current),
                 xmlctx->in->current, "either single or double quotation mark");
         return LY_EVALID;
     }
@@ -720,10 +714,10 @@
     while ((xmlctx->in->current[0] != '>') && (xmlctx->in->current[0] != '/')) {
         in = xmlctx->in->current;
         if (in[0] == '\0') {
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
             return LY_EVALID;
         } else if ((ly_getutf8(&in, &c, &parsed) || !is_xmlqnamestartchar(c))) {
-            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
+            LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_INSTREXP, LY_VCODE_INSTREXP_len(in - parsed), in - parsed,
                     "element tag end ('>' or '/>') or an attribute");
             return LY_EVALID;
         }
@@ -800,7 +794,6 @@
     xmlctx = calloc(1, sizeof *xmlctx);
     LY_CHECK_ERR_RET(!xmlctx, LOGMEM(ctx), LY_EMEM);
     xmlctx->ctx = ctx;
-    xmlctx->line = 1;
     xmlctx->in = in;
 
     /* parse next element, if any */
@@ -811,7 +804,7 @@
         /* update status */
         xmlctx->status = LYXML_END;
     } else if (closing) {
-        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
+        LOGVAL(ctx, LY_VLOG_LINE, &xmlctx->in->line, LYVE_SYNTAX, "Stray closing element tag (\"%.*s\").",
                 xmlctx->name_len, xmlctx->name);
         ret = LY_EVALID;
         goto cleanup;
@@ -904,7 +897,7 @@
             /* no attributes but a closing tag */
             ly_in_skip(xmlctx->in, 1);
             if (!xmlctx->in->current[0]) {
-                LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->line, LY_VCODE_EOF);
+                LOGVAL(xmlctx->ctx, LY_VLOG_LINE, &xmlctx->in->line, LY_VCODE_EOF);
                 ret = LY_EVALID;
                 goto cleanup;
             }