]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
analyze: split out loop innards into a separate function
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Feb 2019 08:18:42 +0000 (09:18 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 22 Feb 2019 08:32:17 +0000 (09:32 +0100)
This was intended to be just a refactoring, but it also fixes a minor bug:
after printing "never", we would skip subsequent expressions:

$ systemd-analyze calendar --iterations=20 @0 @1
systemd-analyze calendar --iterations=20 @0 @1
  Original form: @0
Normalized form: 1970-01-01 00:00:00 UTC
    Next elapse: never

(the second expression was skipped).

src/analyze/analyze.c

index 064525d64d31c2e7ecb2a064451eeceeb039c1db..337089a40dafa68f4b448cbe0099a9765f20fc5a 100644 (file)
@@ -1671,78 +1671,76 @@ static int dump_timespan(int argc, char *argv[], void *userdata) {
         return EXIT_SUCCESS;
 }
 
-static int test_calendar(int argc, char *argv[], void *userdata) {
-        int ret = 0, r;
-        char **p;
-        usec_t n;
+static int test_calendar_one(usec_t n, const char *p) {
+        _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
+        _cleanup_free_ char *t = NULL;
+        int r;
 
-        n = now(CLOCK_REALTIME);
+        r = calendar_spec_from_string(p, &spec);
+        if (r < 0)
+                return log_error_errno(r, "Failed to parse calendar specification '%s': %m", p);
 
-        STRV_FOREACH(p, strv_skip(argv, 1)) {
-                _cleanup_(calendar_spec_freep) CalendarSpec *spec = NULL;
-                _cleanup_free_ char *t = NULL;
-                unsigned i;
+        r = calendar_spec_normalize(spec);
+        if (r < 0)
+                return log_error_errno(r, "Failed to normalize calendar specification '%s': %m", p);
 
-                r = calendar_spec_from_string(*p, &spec);
-                if (r < 0) {
-                        ret = log_error_errno(r, "Failed to parse calendar specification '%s': %m", *p);
-                        continue;
-                }
+        r = calendar_spec_to_string(spec, &t);
+        if (r < 0)
+                return log_error_errno(r, "Failed to format calendar specification '%s': %m", p);
 
-                r = calendar_spec_normalize(spec);
-                if (r < 0) {
-                        ret = log_error_errno(r, "Failed to normalize calendar specification '%s': %m", *p);
-                        continue;
-                }
+        if (!streq(t, p))
+                printf("  Original form: %s\n", p);
 
-                r = calendar_spec_to_string(spec, &t);
-                if (r < 0) {
-                        ret = log_error_errno(r, "Failed to format calendar specification '%s': %m", *p);
-                        continue;
-                }
+        printf("Normalized form: %s\n", t);
 
-                if (!streq(t, *p))
-                        printf("  Original form: %s\n", *p);
+        for (unsigned i = 0; i < arg_iterations; i++) {
+                char buffer[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESTAMP_RELATIVE_MAX)];
+                usec_t next;
+
+                r = calendar_spec_next_usec(spec, n, &next);
+                if (r == -ENOENT) {
+                        if (i == 0)
+                                printf("    Next elapse: never\n");
+                        return 0;
+                }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to determine next elapse for '%s': %m", p);
 
-                printf("Normalized form: %s\n", t);
+                if (i == 0)
+                        printf("    Next elapse: %s\n", format_timestamp(buffer, sizeof(buffer), next));
+                else {
+                        int k = DECIMAL_STR_WIDTH(i+1);
 
-                for (i = 0; i < arg_iterations; i++) {
-                        char buffer[CONST_MAX(FORMAT_TIMESTAMP_MAX, FORMAT_TIMESTAMP_RELATIVE_MAX)];
-                        usec_t next;
+                        if (k < 8)
+                                k = 8 - k;
+                        else
+                                k = 0;
 
-                        r = calendar_spec_next_usec(spec, n, &next);
-                        if (r == -ENOENT) {
-                                if (i > 0)
-                                        break;
+                        printf("%*sIter. #%u: %s\n", k, "", i+1, format_timestamp(buffer, sizeof(buffer), next));
+                }
 
-                                printf("    Next elapse: never\n");
-                                return ret;
-                        }
-                        if (r < 0) {
-                                ret = log_error_errno(r, "Failed to determine next elapse for '%s': %m", *p);
-                                break;
-                        }
+                if (!in_utc_timezone())
+                        printf("       (in UTC): %s\n", format_timestamp_utc(buffer, sizeof(buffer), next));
 
-                        if (i == 0)
-                                printf("    Next elapse: %s\n", format_timestamp(buffer, sizeof(buffer), next));
-                        else {
-                                int k = DECIMAL_STR_WIDTH(i+1);
+                printf("       From now: %s\n", format_timestamp_relative(buffer, sizeof(buffer), next));
 
-                                if (k < 8)
-                                        k = 8 - k;
-                                else
-                                        k = 0;
+                n = next;
+        }
 
-                                printf("%*sIter. #%u: %s\n", k, "", i+1, format_timestamp(buffer, sizeof(buffer), next));
-                        }
+        return 0;
+}
 
-                        if (!in_utc_timezone())
-                                printf("       (in UTC): %s\n", format_timestamp_utc(buffer, sizeof(buffer), next));
+static int test_calendar(int argc, char *argv[], void *userdata) {
+        int ret = 0, r;
+        char **p;
+        usec_t n;
 
-                        printf("       From now: %s\n", format_timestamp_relative(buffer, sizeof(buffer), next));
+        n = now(CLOCK_REALTIME); /* We want to use the same "base" for all expressions */
 
-                        n = next;
-                }
+        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');