chiark / gitweb /
journalctl: allow both "-n 55" and "-n55" on the command line, as equivalent syntaxes
[elogind.git] / src / journal / journalctl.c
index 46a7be20cea1c1e76d97d23f9cf2ecda2f4c201b..0afeef932e54b89f615cf91570b0d0c239240894 100644 (file)
 
 static OutputMode arg_output = OUTPUT_SHORT;
 static bool arg_follow = false;
+static bool arg_full = false;
 static bool arg_all = false;
 static bool arg_no_pager = false;
-static unsigned arg_lines = 0;
+static int arg_lines = -1;
 static bool arg_no_tail = false;
 static bool arg_quiet = false;
 static bool arg_merge = false;
@@ -105,6 +106,7 @@ static int help(void) {
                "  -o --output=STRING     Change journal output mode (short, short-monotonic,\n"
                "                         verbose, export, json, json-pretty, json-sse, cat)\n"
                "  -x --catalog           Add message explanations where available\n"
+               "     --full              Do not ellipsize fields\n"
                "  -a --all               Show all fields, including long and unprintable\n"
                "  -q --quiet             Don't show privilege warning\n"
                "     --no-pager          Do not pipe output into a pager\n"
@@ -140,6 +142,7 @@ static int parse_argv(int argc, char *argv[]) {
                 ARG_NO_TAIL,
                 ARG_NEW_ID128,
                 ARG_HEADER,
+                ARG_FULL,
                 ARG_SETUP_KEYS,
                 ARG_INTERVAL,
                 ARG_VERIFY,
@@ -158,6 +161,7 @@ static int parse_argv(int argc, char *argv[]) {
                 { "follow",       no_argument,       NULL, 'f'              },
                 { "output",       required_argument, NULL, 'o'              },
                 { "all",          no_argument,       NULL, 'a'              },
+                { "full",         no_argument,       NULL, ARG_FULL         },
                 { "lines",        optional_argument, NULL, 'n'              },
                 { "no-tail",      no_argument,       NULL, ARG_NO_TAIL      },
                 { "new-id128",    no_argument,       NULL, ARG_NEW_ID128    },
@@ -198,7 +202,6 @@ static int parse_argv(int argc, char *argv[]) {
 
                 case ARG_VERSION:
                         puts(PACKAGE_STRING);
-                        puts(DISTRIBUTION);
                         puts(SYSTEMD_FEATURES);
                         return 0;
 
@@ -226,19 +229,40 @@ static int parse_argv(int argc, char *argv[]) {
 
                         break;
 
+                case ARG_FULL:
+                        arg_full = true;
+                        break;
+
                 case 'a':
                         arg_all = true;
                         break;
 
                 case 'n':
                         if (optarg) {
-                                r = safe_atou(optarg, &arg_lines);
-                                if (r < 0 || arg_lines <= 0) {
+                                r = safe_atoi(optarg, &arg_lines);
+                                if (r < 0 || arg_lines < 0) {
                                         log_error("Failed to parse lines '%s'", optarg);
                                         return -EINVAL;
                                 }
-                        } else
-                                arg_lines = 10;
+                        } else {
+                                int n;
+
+                                /* Hmm, no argument? Maybe the next
+                                 * word on the command line is
+                                 * supposed to be the argument? Let's
+                                 * see if there is one, and is
+                                 * parsable as a positive
+                                 * integer... */
+
+                                if (optind < argc &&
+                                    safe_atoi(argv[optind], &n) >= 0 &&
+                                    n >= 0) {
+
+                                        arg_lines = n;
+                                        optind++;
+                                } else
+                                        arg_lines = 10;
+                        }
 
                         break;
 
@@ -406,7 +430,7 @@ static int parse_argv(int argc, char *argv[]) {
                 }
         }
 
-        if (arg_follow && !arg_no_tail && arg_lines <= 0)
+        if (arg_follow && !arg_no_tail && arg_lines < 0)
                 arg_lines = 10;
 
         if (arg_since_set && arg_until_set && arg_since_set > arg_until_set) {
@@ -842,8 +866,8 @@ int main(int argc, char *argv[]) {
         sd_journal *j = NULL;
         bool need_seek = false;
         sd_id128_t previous_boot_id;
-        bool previous_boot_id_valid = false;
-        unsigned n_shown = 0;
+        bool previous_boot_id_valid = false, first_line = true;
+        int n_shown = 0;
 
         setlocale(LC_ALL, "");
         log_parse_environment();
@@ -867,6 +891,8 @@ int main(int argc, char *argv[]) {
 
         if (arg_action == ACTION_LIST_CATALOG)  {
                 r = catalog_list(stdout);
+                if (r < 0)
+                        log_error("Failed to list catalog: %s", strerror(-r));
                 goto finish;
         }
 
@@ -928,6 +954,11 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 goto finish;
 
+        /* Opening the fd now means the first sd_journal_wait() will actually wait */
+        r = sd_journal_get_fd(j);
+        if (r < 0)
+                goto finish;
+
         if (arg_field) {
                 const void *data;
                 size_t size;
@@ -941,7 +972,7 @@ int main(int argc, char *argv[]) {
                 SD_JOURNAL_FOREACH_UNIQUE(j, data, size) {
                         const void *eq;
 
-                        if (arg_lines > 0 && n_shown >= arg_lines)
+                        if (arg_lines >= 0 && n_shown >= arg_lines)
                                 break;
 
                         eq = memchr(data, '=', size);
@@ -974,7 +1005,7 @@ int main(int argc, char *argv[]) {
                 }
                 r = sd_journal_next(j);
 
-        } else if (arg_lines > 0) {
+        } else if (arg_lines >= 0) {
                 r = sd_journal_seek_tail(j);
                 if (r < 0) {
                         log_error("Failed to seek to tail: %s", strerror(-r));
@@ -1023,7 +1054,7 @@ int main(int argc, char *argv[]) {
         }
 
         for (;;) {
-                while (arg_lines == 0 || arg_follow || n_shown < arg_lines) {
+                while (arg_lines < 0 || n_shown < arg_lines || (arg_follow && !first_line)) {
                         int flags;
 
                         if (need_seek) {
@@ -1063,12 +1094,12 @@ int main(int argc, char *argv[]) {
 
                         flags =
                                 arg_all * OUTPUT_SHOW_ALL |
-                                (!on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
+                                (arg_full || !on_tty() || pager_have()) * OUTPUT_FULL_WIDTH |
                                 on_tty() * OUTPUT_COLOR |
                                 arg_catalog * OUTPUT_CATALOG;
 
                         r = output_journal(stdout, j, arg_output, 0, flags);
-                        if (r < 0)
+                        if (r < 0 || ferror(stdout))
                                 goto finish;
 
                         need_seek = true;
@@ -1083,6 +1114,8 @@ int main(int argc, char *argv[]) {
                         log_error("Couldn't wait for journal event: %s", strerror(-r));
                         goto finish;
                 }
+
+                first_line = false;
         }
 
 finish: