]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out calendar verb into own .c/.h file
authorLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 09:18:57 +0000 (10:18 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 21 Feb 2022 16:22:23 +0000 (17:22 +0100)
src/analyze/analyze-calendar.c [new file with mode: 0644]
src/analyze/analyze-calendar.h [new file with mode: 0644]
src/analyze/analyze.c
src/analyze/analyze.h
src/analyze/meson.build

diff --git a/src/analyze/analyze-calendar.c b/src/analyze/analyze-calendar.c
new file mode 100644 (file)
index 0000000..ac71af9
--- /dev/null
@@ -0,0 +1,145 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include "analyze.h"
+#include "analyze-calendar.h"
+#include "calendarspec.h"
+#include "format-table.h"
+#include "terminal-util.h"
+
+static int test_calendar_one(usec_t n, const char *p) {
+        _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
+        _cleanup_(table_unrefp) Table *table = NULL;
+        _cleanup_free_ char *t = NULL;
+        TableCell *cell;
+        int r;
+
+        r = calendar_spec_from_string(p, &spec);
+        if (r < 0) {
+                log_error_errno(r, "Failed to parse calendar specification '%s': %m", p);
+                time_parsing_hint(p, /* calendar= */ false, /* timestamp= */ true, /* timespan= */ true);
+                return r;
+        }
+
+        r = calendar_spec_to_string(spec, &t);
+        if (r < 0)
+                return log_error_errno(r, "Failed to format calendar specification '%s': %m", p);
+
+        table = table_new("name", "value");
+        if (!table)
+                return log_oom();
+
+        table_set_header(table, false);
+
+        assert_se(cell = table_get_cell(table, 0, 0));
+        r = table_set_ellipsize_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        r = table_set_align_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        assert_se(cell = table_get_cell(table, 0, 1));
+        r = table_set_ellipsize_percent(table, cell, 100);
+        if (r < 0)
+                return r;
+
+        if (!streq(t, p)) {
+                r = table_add_many(table,
+                                   TABLE_STRING, "Original form:",
+                                   TABLE_STRING, p);
+                if (r < 0)
+                        return table_log_add_error(r);
+        }
+
+        r = table_add_many(table,
+                           TABLE_STRING, "Normalized form:",
+                           TABLE_STRING, t);
+        if (r < 0)
+                return table_log_add_error(r);
+
+        for (unsigned i = 0; i < arg_iterations; i++) {
+                usec_t next;
+
+                r = calendar_spec_next_usec(spec, n, &next);
+                if (r == -ENOENT) {
+                        if (i == 0) {
+                                r = table_add_many(table,
+                                                   TABLE_STRING, "Next elapse:",
+                                                   TABLE_STRING, "never",
+                                                   TABLE_SET_COLOR, ansi_highlight_yellow());
+                                if (r < 0)
+                                        return table_log_add_error(r);
+                        }
+                        break;
+                }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p);
+
+                if (i == 0) {
+                        r = table_add_many(table,
+                                           TABLE_STRING, "Next elapse:",
+                                           TABLE_TIMESTAMP, next,
+                                           TABLE_SET_COLOR, ansi_highlight_blue());
+                        if (r < 0)
+                                return table_log_add_error(r);
+                } else {
+                        int k = DECIMAL_STR_WIDTH(i + 1);
+
+                        if (k < 8)
+                                k = 8 - k;
+                        else
+                                k = 0;
+
+                        r = table_add_cell_stringf(table, NULL, "Iter. #%u:", i+1);
+                        if (r < 0)
+                                return table_log_add_error(r);
+
+                        r = table_add_many(table,
+                                           TABLE_TIMESTAMP, next,
+                                           TABLE_SET_COLOR, ansi_highlight_blue());
+                        if (r < 0)
+                                return table_log_add_error(r);
+                }
+
+                if (!in_utc_timezone()) {
+                        r = table_add_many(table,
+                                           TABLE_STRING, "(in UTC):",
+                                           TABLE_TIMESTAMP_UTC, next);
+                        if (r < 0)
+                                return table_log_add_error(r);
+                }
+
+                r = table_add_many(table,
+                                   TABLE_STRING, "From now:",
+                                   TABLE_TIMESTAMP_RELATIVE, next);
+                if (r < 0)
+                        return table_log_add_error(r);
+
+                n = next;
+        }
+
+        return table_print(table, NULL);
+}
+
+int test_calendar(int argc, char *argv[], void *userdata) {
+        int ret = 0, r;
+        char **p;
+        usec_t n;
+
+        if (arg_base_time != USEC_INFINITY)
+                n = arg_base_time;
+        else
+                n = now(CLOCK_REALTIME); /* We want to use the same "base" for all expressions */
+
+        STRV_FOREACH(p, strv_skip(argv, 1)) {
+                r = test_calendar_one(n, *p);
+                if (ret == 0 && r < 0)
+                        ret = r;
+
+                if (*(p + 1))
+                        putchar('\n');
+        }
+
+        return ret;
+}
diff --git a/src/analyze/analyze-calendar.h b/src/analyze/analyze-calendar.h
new file mode 100644 (file)
index 0000000..7b73fce
--- /dev/null
@@ -0,0 +1,4 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+#pragma once
+
+int test_calendar(int argc, char *argv[], void *userdata);
index c3980928cba383984444a27ad67f81308d1f7dfc..a051a48a5d98b769b2210fe51802e84f7e0b8721 100644 (file)
@@ -13,6 +13,7 @@
 
 #include "alloc-util.h"
 #include "analyze.h"
+#include "analyze-calendar.h"
 #include "analyze-condition.h"
 #include "analyze-elf.h"
 #include "analyze-security.h"
@@ -104,8 +105,8 @@ static char *arg_image = NULL;
 static char *arg_security_policy = NULL;
 static bool arg_offline = false;
 static unsigned arg_threshold = 100;
-static unsigned arg_iterations = 1;
-static usec_t arg_base_time = USEC_INFINITY;
+unsigned arg_iterations = 1;
+usec_t arg_base_time = USEC_INFINITY;
 static char *arg_unit = NULL;
 static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
 static bool arg_quiet = false;
@@ -2043,144 +2044,6 @@ void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timesp
                            "Use 'systemd-analyze timespan \"%s\"' instead?", p);
 }
 
-static int test_calendar_one(usec_t n, const char *p) {
-        _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
-        _cleanup_(table_unrefp) Table *table = NULL;
-        _cleanup_free_ char *t = NULL;
-        TableCell *cell;
-        int r;
-
-        r = calendar_spec_from_string(p, &spec);
-        if (r < 0) {
-                log_error_errno(r, "Failed to parse calendar specification '%s': %m", p);
-                time_parsing_hint(p, /* calendar= */ false, /* timestamp= */ true, /* timespan= */ true);
-                return r;
-        }
-
-        r = calendar_spec_to_string(spec, &t);
-        if (r < 0)
-                return log_error_errno(r, "Failed to format calendar specification '%s': %m", p);
-
-        table = table_new("name", "value");
-        if (!table)
-                return log_oom();
-
-        table_set_header(table, false);
-
-        assert_se(cell = table_get_cell(table, 0, 0));
-        r = table_set_ellipsize_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        r = table_set_align_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        assert_se(cell = table_get_cell(table, 0, 1));
-        r = table_set_ellipsize_percent(table, cell, 100);
-        if (r < 0)
-                return r;
-
-        if (!streq(t, p)) {
-                r = table_add_many(table,
-                                   TABLE_STRING, "Original form:",
-                                   TABLE_STRING, p);
-                if (r < 0)
-                        return table_log_add_error(r);
-        }
-
-        r = table_add_many(table,
-                           TABLE_STRING, "Normalized form:",
-                           TABLE_STRING, t);
-        if (r < 0)
-                return table_log_add_error(r);
-
-        for (unsigned i = 0; i < arg_iterations; i++) {
-                usec_t next;
-
-                r = calendar_spec_next_usec(spec, n, &next);
-                if (r == -ENOENT) {
-                        if (i == 0) {
-                                r = table_add_many(table,
-                                                   TABLE_STRING, "Next elapse:",
-                                                   TABLE_STRING, "never",
-                                                   TABLE_SET_COLOR, ansi_highlight_yellow());
-                                if (r < 0)
-                                        return table_log_add_error(r);
-                        }
-                        break;
-                }
-                if (r < 0)
-                        return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p);
-
-                if (i == 0) {
-                        r = table_add_many(table,
-                                           TABLE_STRING, "Next elapse:",
-                                           TABLE_TIMESTAMP, next,
-                                           TABLE_SET_COLOR, ansi_highlight_blue());
-                        if (r < 0)
-                                return table_log_add_error(r);
-                } else {
-                        int k = DECIMAL_STR_WIDTH(i + 1);
-
-                        if (k < 8)
-                                k = 8 - k;
-                        else
-                                k = 0;
-
-                        r = table_add_cell_stringf(table, NULL, "Iter. #%u:", i+1);
-                        if (r < 0)
-                                return table_log_add_error(r);
-
-                        r = table_add_many(table,
-                                           TABLE_TIMESTAMP, next,
-                                           TABLE_SET_COLOR, ansi_highlight_blue());
-                        if (r < 0)
-                                return table_log_add_error(r);
-                }
-
-                if (!in_utc_timezone()) {
-                        r = table_add_many(table,
-                                           TABLE_STRING, "(in UTC):",
-                                           TABLE_TIMESTAMP_UTC, next);
-                        if (r < 0)
-                                return table_log_add_error(r);
-                }
-
-                r = table_add_many(table,
-                                   TABLE_STRING, "From now:",
-                                   TABLE_TIMESTAMP_RELATIVE, next);
-                if (r < 0)
-                        return table_log_add_error(r);
-
-                n = next;
-        }
-
-        return table_print(table, NULL);
-}
-
-static int test_calendar(int argc, char *argv[], void *userdata) {
-        int ret = 0, r;
-        char **p;
-        usec_t n;
-
-        if (arg_base_time != USEC_INFINITY)
-                n = arg_base_time;
-        else
-                n = now(CLOCK_REALTIME); /* We want to use the same "base" for all expressions */
-
-        STRV_FOREACH(p, strv_skip(argv, 1)) {
-                r = test_calendar_one(n, *p);
-                if (ret == 0 && r < 0)
-                        ret = r;
-
-                if (*(p + 1))
-                        putchar('\n');
-        }
-
-        return ret;
-}
-
 static int service_watchdogs(int argc, char *argv[], void *userdata) {
         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
         _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
index 72824e221b53bae6cb9ee978aa1a9cca22526d4a..ff4e7b4acdc8b61766312c678c2c546afba1c884 100644 (file)
@@ -3,4 +3,9 @@
 
 #include <stdbool.h>
 
+#include "time-util.h"
+
+extern unsigned arg_iterations;
+extern usec_t arg_base_time;
+
 void time_parsing_hint(const char *p, bool calendar, bool timestamp, bool timespan);
index 0988f4a57f5e03d45b09ec96c42779d6dfaaa65b..287a43c319b94b30ec142caff6df4b21c2fd1324 100644 (file)
@@ -1,6 +1,8 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 
 systemd_analyze_sources = files('''
+        analyze-calendar.c
+        analyze-calendar.h
         analyze-condition.c
         analyze-condition.h
         analyze-elf.c