]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
erec: expose print_location() and line_location()
authorPablo Neira Ayuso <pablo@netfilter.org>
Sun, 2 Jan 2022 20:39:03 +0000 (21:39 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Sat, 15 Jan 2022 17:11:22 +0000 (18:11 +0100)
Add a few helper functions to reuse code in the new rule optimization
infrastructure.

Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/erec.h
include/rule.h
src/erec.c

index 79a162902304bf71845a0379994b1039040c423f..c17f5def530213dae415b0301743d8ade1f92438 100644 (file)
@@ -76,4 +76,9 @@ extern int __fmtstring(4, 5) __stmt_binary_error(struct eval_ctx *ctx,
 #define stmt_binary_error(ctx, s1, s2, fmt, args...) \
        __stmt_binary_error(ctx, &(s1)->location, &(s2)->location, fmt, ## args)
 
+void print_location(FILE *f, const struct input_descriptor *indesc,
+                   const struct location *loc);
+const char *line_location(const struct input_descriptor *indesc,
+                         const struct location *loc, char *buf, size_t bufsiz);
+
 #endif /* NFTABLES_EREC_H */
index be31695636df40b9dd10c4a3499b5c9598f8ca59..150576641b39baed3702c9fc25bf49cae05f8baa 100644 (file)
@@ -311,7 +311,6 @@ void rule_stmt_append(struct rule *rule, struct stmt *stmt);
 void rule_stmt_insert_at(struct rule *rule, struct stmt *nstmt,
                         struct stmt *stmt);
 
-
 /**
  * struct set - nftables set
  *
index 5c3351a512464af3c7470098f7f072599a195afc..7c9165c290d89c7b3ba44c1d4e9cd8a6563caabb 100644 (file)
@@ -81,11 +81,57 @@ struct error_record *erec_create(enum error_record_types type,
        return erec;
 }
 
+void print_location(FILE *f, const struct input_descriptor *indesc,
+                   const struct location *loc)
+{
+       const struct input_descriptor *tmp;
+       const struct location *iloc;
+
+       if (indesc->location.indesc != NULL) {
+               const char *prefix = "In file included from";
+               iloc = &indesc->location;
+               for (tmp = iloc->indesc;
+                    tmp != NULL && tmp->type != INDESC_INTERNAL;
+                    tmp = iloc->indesc) {
+                       fprintf(f, "%s %s:%u:%u-%u:\n", prefix,
+                               tmp->name,
+                               iloc->first_line, iloc->first_column,
+                               iloc->last_column);
+                       prefix = "                 from";
+                       iloc = &tmp->location;
+               }
+       }
+       if (indesc->name != NULL)
+               fprintf(f, "%s:%u:%u-%u: ", indesc->name,
+                       loc->first_line, loc->first_column,
+                       loc->last_column);
+}
+
+const char *line_location(const struct input_descriptor *indesc,
+                         const struct location *loc, char *buf, size_t bufsiz)
+{
+       const char *line;
+       FILE *f;
+
+       f = fopen(indesc->name, "r");
+       if (!f)
+               return NULL;
+
+       if (!fseek(f, loc->line_offset, SEEK_SET) &&
+           fread(buf, 1, bufsiz - 1, f) > 0) {
+               *strchrnul(buf, '\n') = '\0';
+               line = buf;
+       }
+       fclose(f);
+
+       return line;
+}
+
 void erec_print(struct output_ctx *octx, const struct error_record *erec,
                unsigned int debug_mask)
 {
-       const struct location *loc = erec->locations, *iloc;
-       const struct input_descriptor *indesc = loc->indesc, *tmp;
+       const struct location *loc = erec->locations;
+       const struct input_descriptor *indesc = loc->indesc;
        const char *line = NULL;
        char buf[1024] = {};
        char *pbuf = NULL;
@@ -100,16 +146,7 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
                *strchrnul(line, '\n') = '\0';
                break;
        case INDESC_FILE:
-               f = fopen(indesc->name, "r");
-               if (!f)
-                       break;
-
-               if (!fseek(f, loc->line_offset, SEEK_SET) &&
-                   fread(buf, 1, sizeof(buf) - 1, f) > 0) {
-                       *strchrnul(buf, '\n') = '\0';
-                       line = buf;
-               }
-               fclose(f);
+               line = line_location(indesc, loc, buf, sizeof(buf));
                break;
        case INDESC_INTERNAL:
        case INDESC_NETLINK:
@@ -132,24 +169,8 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
                return;
        }
 
-       if (indesc->location.indesc != NULL) {
-               const char *prefix = "In file included from";
-               iloc = &indesc->location;
-               for (tmp = iloc->indesc;
-                    tmp != NULL && tmp->type != INDESC_INTERNAL;
-                    tmp = iloc->indesc) {
-                       fprintf(f, "%s %s:%u:%u-%u:\n", prefix,
-                               tmp->name,
-                               iloc->first_line, iloc->first_column,
-                               iloc->last_column);
-                       prefix = "                 from";
-                       iloc = &tmp->location;
-               }
-       }
-       if (indesc->name != NULL)
-               fprintf(f, "%s:%u:%u-%u: ", indesc->name,
-                       loc->first_line, loc->first_column,
-                       loc->last_column);
+       print_location(f, indesc, loc);
+
        if (error_record_names[erec->type])
                fprintf(f, "%s: ", error_record_names[erec->type]);
        fprintf(f, "%s\n", erec->msg);