• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14872145375

06 May 2025 09:07PM UTC coverage: 72.232% (+0.02%) from 72.214%
14872145375

push

github

DaanDeMeyer
string-table: annotate _to_string and _from_string with _const_ and _pure_, respectively

Follow-up for c94f6ab1b

297286 of 411572 relevant lines covered (72.23%)

695615.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.84
/src/shared/parse-argument.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include "format-table.h"
4
#include "log.h"
5
#include "parse-argument.h"
6
#include "parse-util.h"
7
#include "path-util.h"
8
#include "signal-util.h"
9
#include "stdio-util.h"
10
#include "string-table.h"
11
#include "string-util.h"
12

13
/* All functions in this file emit warnings. */
14

15
int parse_boolean_argument(const char *optname, const char *s, bool *ret) {
1,087✔
16
        int r;
1,087✔
17

18
        /* Returns the result through *ret and the return value. */
19

20
        if (s) {
1,087✔
21
                r = parse_boolean(s);
1,082✔
22
                if (r < 0)
1,082✔
23
                        return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s);
33✔
24

25
                if (ret)
1,049✔
26
                        *ret = r;
822✔
27
                return r;
1,049✔
28
        } else {
29
                /* s may be NULL. This is controlled by getopt_long() parameters. */
30
                if (ret)
5✔
31
                        *ret = true;
5✔
32
                return true;
5✔
33
        }
34
}
35

36
int parse_tristate_argument(const char *optname, const char *s, int *ret) {
2✔
37
        int r;
2✔
38

39
        if (s) {
2✔
40
                r = parse_boolean(s);
2✔
41
                if (r < 0)
2✔
42
                        return log_error_errno(r, "Failed to parse boolean argument to %s: %s.", optname, s);
×
43

44
                if (ret)
2✔
45
                        *ret = r;
2✔
46

47
                return r;
2✔
48
        } else {
49
                if (ret)
×
50
                        *ret = -1;
×
51

52
                return 0;
×
53
        }
54
}
55

56
int parse_json_argument(const char *s, sd_json_format_flags_t *ret) {
290✔
57
        assert(s);
290✔
58
        assert(ret);
290✔
59

60
        if (streq(s, "pretty"))
290✔
61
                *ret = SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_COLOR_AUTO;
98✔
62
        else if (streq(s, "short"))
192✔
63
                *ret = SD_JSON_FORMAT_NEWLINE;
170✔
64
        else if (streq(s, "off"))
22✔
65
                *ret = SD_JSON_FORMAT_OFF;
11✔
66
        else if (streq(s, "help")) {
11✔
67
                puts("pretty\n"
3✔
68
                     "short\n"
69
                     "off");
70
                return 0; /* 0 means → we showed a brief help, exit now */
3✔
71
        } else
72
                return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown argument to --json= switch: %s", s);
8✔
73

74
        return 1; /* 1 means → properly parsed */
75
}
76

77
int parse_path_argument(const char *path, bool suppress_root, char **arg) {
2,111✔
78
        char *p;
2,111✔
79
        int r;
2,111✔
80

81
        /*
82
         * This function is intended to be used in command line parsers, to handle paths that are passed
83
         * in. It makes the path absolute, and reduces it to NULL if omitted or root (the latter optionally).
84
         *
85
         * NOTE THAT THIS WILL FREE THE PREVIOUS ARGUMENT POINTER ON SUCCESS!
86
         * Hence, do not pass in uninitialized pointers.
87
         */
88

89
        if (isempty(path)) {
2,111✔
90
                *arg = mfree(*arg);
8✔
91
                return 0;
8✔
92
        }
93

94
        r = path_make_absolute_cwd(path, &p);
2,103✔
95
        if (r < 0)
2,103✔
96
                return log_error_errno(r, "Failed to parse path \"%s\" and make it absolute: %m", path);
×
97

98
        path_simplify(p);
2,103✔
99
        if (suppress_root && empty_or_root(p))
2,103✔
100
                p = mfree(p);
3✔
101

102
        return free_and_replace(*arg, p);
2,103✔
103
}
104

105
int parse_signal_argument(const char *s, int *ret) {
30✔
106
        int r;
30✔
107

108
        assert(s);
30✔
109
        assert(ret);
30✔
110

111
        if (streq(s, "help")) {
30✔
112
                DUMP_STRING_TABLE(signal, int, _NSIG);
132✔
113
                return 0;
2✔
114
        }
115

116
        if (streq(s, "list")) {
28✔
117
                _cleanup_(table_unrefp) Table *table = NULL;
1✔
118

119
                table = table_new("signal", "name");
1✔
120
                if (!table)
1✔
121
                        return log_oom();
×
122

123
                for (int i = 1; i < _NSIG; i++) {
65✔
124
                        r = table_add_many(
64✔
125
                                        table,
126
                                        TABLE_INT, i,
127
                                        TABLE_SIGNAL, i);
128
                        if (r < 0)
64✔
129
                                return table_log_add_error(r);
×
130
                }
131

132
                r = table_print(table, NULL);
1✔
133
                if (r < 0)
1✔
134
                        return table_log_print_error(r);
×
135

136
                return 0;
137
        }
138

139
        r = signal_from_string(s);
27✔
140
        if (r < 0)
27✔
141
                return log_error_errno(r, "Failed to parse signal string \"%s\".", s);
×
142

143
        *ret = r;
27✔
144
        return 1; /* work to do */
27✔
145
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc