Skip to content

Commit dbba91d

Browse files
committed
*: unify commandline arguments in macro call
this ties together the short and long flags, with the argument spec and description. this avoids mismatches between all 3 variables (getoptstring, longopts, longopts_help)
1 parent 77a49de commit dbba91d

16 files changed

Lines changed: 223 additions & 455 deletions

File tree

src/checkpath/checkpath.c

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,20 @@ typedef enum {
4242
inode_fifo = 3,
4343
} inode_t;
4444

45+
#define opts(opt, opt_long) \
46+
opt(d, directory, no_argument, "Create a directory if not exists") \
47+
opt(D, directory-truncate, no_argument, "Create/empty directory") \
48+
opt(f, file, no_argument, "Create a file if not exists") \
49+
opt(F, file-truncate, no_argument, "Truncate file") \
50+
opt(p, pipe, no_argument, "Create a named pipe (FIFO) if not exists") \
51+
opt(m, mode, required_argument, "Mode to check") \
52+
opt(o, owner, required_argument, "Owner to check (user:group)") \
53+
opt(s, symlinks, no_argument, "follow symbolic links (irrelevant on linux)") \
54+
opt(W, writable, no_argument, "Check whether the path is writable or not")
55+
cmdline_opts(opts)
56+
4557
const char *applet = NULL;
4658
const char *extraopts ="path1 [path2] [...]";
47-
const char getoptstring[] = "dDfFpm:o:sW" getoptstring_COMMON;
48-
const struct option longopts[] = {
49-
{ "directory", 0, NULL, 'd'},
50-
{ "directory-truncate", 0, NULL, 'D'},
51-
{ "file", 0, NULL, 'f'},
52-
{ "file-truncate", 0, NULL, 'F'},
53-
{ "pipe", 0, NULL, 'p'},
54-
{ "mode", 1, NULL, 'm'},
55-
{ "owner", 1, NULL, 'o'},
56-
{ "symlinks", 0, NULL, 's'},
57-
{ "writable", 0, NULL, 'W'},
58-
longopts_COMMON
59-
};
60-
const char * const longopts_help[] = {
61-
"Create a directory if not exists",
62-
"Create/empty directory",
63-
"Create a file if not exists",
64-
"Truncate file",
65-
"Create a named pipe (FIFO) if not exists",
66-
"Mode to check",
67-
"Owner to check (user:group)",
68-
"follow symbolic links (irrelevant on linux)",
69-
"Check whether the path is writable or not",
70-
longopts_help_COMMON
71-
};
7259
const char *usagestring = NULL;
7360

7461
static int get_dirfd(char *path, bool symlinks)

src/fstabinfo/fstabinfo.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,16 @@ extern char **environ;
6868

6969
const char *applet = NULL;
7070
const char *extraopts = NULL;
71-
const char getoptstring[] = "MRbmop:t:" getoptstring_COMMON;
72-
const struct option longopts[] = {
73-
{ "mount", 0, NULL, 'M' },
74-
{ "remount", 0, NULL, 'R' },
75-
{ "blockdevice", 0, NULL, 'b' },
76-
{ "mountargs", 0, NULL, 'm' },
77-
{ "options", 0, NULL, 'o' },
78-
{ "passno", 1, NULL, 'p' },
79-
{ "fstype", 1, NULL, 't' },
80-
longopts_COMMON
81-
};
82-
const char * const longopts_help[] = {
83-
"Mounts the filesystem from the mountpoint",
84-
"Remounts the filesystem based on the information in fstab",
85-
"Extract the block device",
86-
"Show arguments needed to mount the entry",
87-
"Extract the options field",
88-
"Extract or query the pass number field",
89-
"List entries with matching file system type",
90-
longopts_help_COMMON
91-
};
9271
const char *usagestring = NULL;
72+
#define opts(opt, opt_long) \
73+
opt(M, mount, no_argument, "Mounts the filesystem from the mountpoint") \
74+
opt(R, remount, no_argument, "Remounts the filesystem based on the information in fstab") \
75+
opt(b, blockdevice, no_argument, "Extract the block device") \
76+
opt(m, mountargs, no_argument, "Show arguments needed to mount the entry") \
77+
opt(o, options, no_argument, "Extract the options field") \
78+
opt(p, passno, required_argument, "Extract or query the pass number field") \
79+
opt(t, fstype, required_argument, "List entries with matching file system type")
80+
cmdline_opts(opts)
9381

9482
#ifdef HAVE_GETMNTENT
9583
static struct mntent *

src/kill_all/kill_all.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,13 @@
3737

3838
const char *applet = NULL;
3939
const char *extraopts = "[signal number]";
40-
const char getoptstring[] = "do:" getoptstring_COMMON;
41-
const struct option longopts[] = {
42-
{ "dry-run", 0, NULL, 'd' },
43-
{ "omit", 1, NULL, 'o' },
44-
longopts_COMMON
45-
};
46-
const char * const longopts_help[] = {
47-
"print what would be done",
48-
"omit this pid (can be repeated)",
49-
longopts_help_COMMON
50-
};
5140
const char *usagestring = NULL;
5241

42+
#define opts(opt, opt_long) \
43+
opt(d, dry-run, no_argument, "print what would be done") \
44+
opt(o, omit, required_argument, "omit this pid (can be repeated)")
45+
cmdline_opts(opts)
46+
5347
static int mount_proc(void)
5448
{
5549
pid_t rc;

src/mountinfo/mountinfo.c

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -49,40 +49,22 @@
4949
const char *applet = NULL;
5050
const char *procmounts = "/proc/mounts";
5151
const char *extraopts = "[mount1] [mount2] ...";
52-
const char getoptstring[] = "f:F:n:N:o:O:p:P:iste:E:" getoptstring_COMMON;
53-
const struct option longopts[] = {
54-
{ "fstype-regex", 1, NULL, 'f'},
55-
{ "skip-fstype-regex", 1, NULL, 'F'},
56-
{ "node-regex", 1, NULL, 'n'},
57-
{ "skip-node-regex", 1, NULL, 'N'},
58-
{ "options-regex", 1, NULL, 'o'},
59-
{ "skip-options-regex", 1, NULL, 'O'},
60-
{ "point-regex", 1, NULL, 'p'},
61-
{ "skip-point-regex", 1, NULL, 'P'},
62-
{ "options", 0, NULL, 'i'},
63-
{ "fstype", 0, NULL, 's'},
64-
{ "node", 0, NULL, 't'},
65-
{ "netdev", 0, NULL, 'e'},
66-
{ "nonetdev", 0, NULL, 'E'},
67-
longopts_COMMON
68-
};
69-
const char * const longopts_help[] = {
70-
"fstype regex to find",
71-
"fstype regex to skip",
72-
"node regex to find",
73-
"node regex to skip",
74-
"options regex to find",
75-
"options regex to skip",
76-
"point regex to find",
77-
"point regex to skip",
78-
"print options",
79-
"print fstype",
80-
"print node",
81-
"is it a network device",
82-
"is it not a network device",
83-
longopts_help_COMMON
84-
};
8552
const char *usagestring = NULL;
53+
#define opts(opt, opt_long) \
54+
opt(f, fstype-regex, required_argument, "fstype regex to find") \
55+
opt(F, skip-fstype-regex, required_argument, "fstype regex to skip") \
56+
opt(n, node-regex, required_argument, "node regex to find") \
57+
opt(N, skip-node-regex, required_argument, "node regex to skip") \
58+
opt(o, options-regex, required_argument, "options regex to find") \
59+
opt(O, skip-options-regex, required_argument, "options regex to skip") \
60+
opt(p, point-regex, required_argument, "point regex to find") \
61+
opt(P, skip-point-regex, required_argument, "point regex to skip") \
62+
opt(i, options, no_argument, "print options") \
63+
opt(s, fstype, no_argument, "print fstype") \
64+
opt(t, node, no_argument, "print node") \
65+
opt(e, netdev, no_argument, "is it a network device") \
66+
opt(E, nonetdev, no_argument, "is it not a network device")
67+
cmdline_opts(opts)
8668

8769
typedef enum {
8870
mount_from,

src/openrc-run/openrc-run.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,16 @@
6161
#define WARN_TIMEOUT 10 /* warn about this every N seconds */
6262

6363
const char *applet = NULL;
64-
const char *extraopts = "stop | start | restart | status | describe | zap";
65-
const char getoptstring[] = "dDsSvl:Z" getoptstring_COMMON;
66-
const struct option longopts[] = {
67-
{ "debug", 0, NULL, 'd'},
68-
{ "dry-run", 0, NULL, 'Z'},
69-
{ "ifstarted", 0, NULL, 's'},
70-
{ "ifstopped", 0, NULL, 'S'},
71-
{ "nodeps", 0, NULL, 'D'},
72-
{ "lockfd", 1, NULL, 'l'},
73-
longopts_COMMON
74-
};
75-
const char *const longopts_help[] = {
76-
"set xtrace when running the script",
77-
"show what would be done",
78-
"only run commands when started",
79-
"only run commands when stopped",
80-
"ignore dependencies",
81-
"fd of the exclusive lock from rc",
82-
longopts_help_COMMON
83-
};
8464
const char *usagestring = NULL;
65+
const char *extraopts = "stop | start | restart | status | describe | zap";
66+
#define opts(opt, opt_long) \
67+
opt(d, debug, no_argument, "set xtrace when running the script") \
68+
opt(Z, dry-run, no_argument, "show what would be done") \
69+
opt(s, ifstarted, no_argument, "only run commands when started") \
70+
opt(S, ifstopped, no_argument, "only run commands when stopped") \
71+
opt(D, nodeps, no_argument, "ignore dependencies") \
72+
opt(l, lockfd, required_argument, "fd of the exclusive lock from rc")
73+
cmdline_opts(opts)
8574

8675
static char *service, *runlevel, *ibsave, *prefix;
8776
static RC_DEPTREE *deptree;

src/openrc-shutdown/openrc-shutdown.c

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -44,33 +44,19 @@
4444

4545
const char *applet = NULL;
4646
const char *extraopts = NULL;
47-
const char getoptstring[] = "cdDfFHKpRrsw" getoptstring_COMMON;
48-
const struct option longopts[] = {
49-
{ "cancel", no_argument, NULL, 'c'},
50-
{ "no-write", no_argument, NULL, 'd'},
51-
{ "dry-run", no_argument, NULL, 'D'},
52-
{ "halt", no_argument, NULL, 'H'},
53-
{ "kexec", no_argument, NULL, 'K'},
54-
{ "poweroff", no_argument, NULL, 'p'},
55-
{ "reexec", no_argument, NULL, 'R'},
56-
{ "reboot", no_argument, NULL, 'r'},
57-
{ "single", no_argument, NULL, 's'},
58-
{ "write-only", no_argument, NULL, 'w'},
59-
longopts_COMMON
60-
};
61-
const char * const longopts_help[] = {
62-
"cancel a pending shutdown",
63-
"do not write wtmp record",
64-
"print actions instead of executing them",
65-
"halt the system",
66-
"reboot the system using kexec",
67-
"power off the system",
68-
"re-execute init (use after upgrading)",
69-
"reboot the system",
70-
"single user mode",
71-
"write wtmp boot record and exit",
72-
longopts_help_COMMON
73-
};
47+
#define opts(opt, opt_long) \
48+
opt(c, cancel, no_argument, "cancel a pending shutdown") \
49+
opt(d, no-write, no_argument, "do not write wtmp record") \
50+
opt(D, dry-run, no_argument, "print actions instead of executing them") \
51+
opt(H, halt, no_argument, "halt the system") \
52+
opt(K, kexec, no_argument, "reboot the system using kexec") \
53+
opt(p, poweroff, no_argument, "power off the system") \
54+
opt(R, reexec, no_argument, "re-execute init (use after upgrading)") \
55+
opt(r, reboot, no_argument, "reboot the system") \
56+
opt(s, single, no_argument, "single user mode") \
57+
opt(w, write-only, no_argument, "write wtmp boot record and exit")
58+
cmdline_opts(opts)
59+
7460
const char *usagestring = ""
7561
"Usage: openrc-shutdown -c | --cancel\n"
7662
" or: openrc-shutdown -R | --reexec\n"

src/openrc/rc.c

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,16 @@
4949
#include "_usage.h"
5050
#include "helpers.h"
5151

52+
#define opts(opt, opt_long) \
53+
opt(n, no-stop, no_argument, "do not stop any services") \
54+
opt(o, override, required_argument, "override the next runlevel to change into\n" \
55+
"when leaving single user or boot runlevels") \
56+
opt(s, service, required_argument, "runs the service specified with the rest of the arguments") \
57+
opt(S, sys, no_argument, "output the RC system type, if any")
58+
cmdline_opts(opts)
59+
5260
const char *extraopts = NULL;
53-
const char getoptstring[] = "a:no:s:S" getoptstring_COMMON;
54-
const struct option longopts[] = {
55-
{ "no-stop", 0, NULL, 'n' },
56-
{ "override", 1, NULL, 'o' },
57-
{ "service", 1, NULL, 's' },
58-
{ "sys", 0, NULL, 'S' },
59-
longopts_COMMON
60-
};
61-
const char * const longopts_help[] = {
62-
"do not stop any services",
63-
"override the next runlevel to change into\nwhen leaving single user or boot runlevels",
64-
"runs the service specified with the rest\nof the arguments",
65-
"output the RC system type, if any",
66-
longopts_help_COMMON
67-
};
68-
const char *usagestring = ""
69-
"Usage: openrc [options] [<runlevel>]";
61+
const char *usagestring = "Usage: openrc [options] [<runlevel>]";
7062

7163
#define INITSH RC_LIBEXECDIR "/sh/init.sh"
7264
#define INITEARLYSH RC_LIBEXECDIR "/sh/init-early.sh"

src/rc-depend/rc-depend.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,15 @@
3232

3333
const char *applet = NULL;
3434
const char *extraopts = NULL;
35-
const char getoptstring[] = "aot:suTF:" getoptstring_COMMON;
36-
const struct option longopts[] = {
37-
{ "starting", 0, NULL, 'a'},
38-
{ "stopping", 0, NULL, 'o'},
39-
{ "type", 1, NULL, 't'},
40-
{ "notrace", 0, NULL, 'T'},
41-
{ "strict", 0, NULL, 's'},
42-
{ "update", 0, NULL, 'u'},
43-
{ "deptree-file", 1, NULL, 'F'},
44-
longopts_COMMON
45-
};
46-
const char * const longopts_help[] = {
47-
"Order services as if runlevel is starting",
48-
"Order services as if runlevel is stopping",
49-
"Type(s) of dependency to list",
50-
"Don't trace service dependencies",
51-
"Only use what is in the runlevels",
52-
"Force an update of the dependency tree",
53-
"File to load cached deptree from",
54-
longopts_help_COMMON
55-
};
35+
#define opts(opt, opt_long) \
36+
opt(a, starting, no_argument, "Order services as if runlevel is starting") \
37+
opt(o, stopping, no_argument, "Order services as if runlevel is stopping") \
38+
opt(t, type, required_argument, "Type(s) of dependency to list") \
39+
opt(T, notrace, no_argument, "Don't trace service dependencies") \
40+
opt(s, strict, no_argument, "Only use what is in the runlevels") \
41+
opt(u, update, no_argument, "Force an update of the dependency tree") \
42+
opt(F, deptree-file, required_argument, "File to load cached deptree from")
43+
cmdline_opts(opts)
5644
const char *usagestring = NULL;
5745

5846
int main(int argc, char **argv)

src/rc-service/rc-service.c

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,21 @@
3131

3232
const char *applet = NULL;
3333
const char *extraopts = NULL;
34-
const char getoptstring[] = "cdDe:ilr:INsSZ" getoptstring_COMMON;
35-
const struct option longopts[] = {
36-
{ "debug", 0, NULL, 'd' },
37-
{ "nodeps", 0, NULL, 'D' },
38-
{ "exists", 1, NULL, 'e' },
39-
{ "ifcrashed", 0, NULL, 'c' },
40-
{ "ifexists", 0, NULL, 'i' },
41-
{ "ifinactive", 0, NULL, 'I' },
42-
{ "ifnotstarted", 0, NULL, 'N' },
43-
{ "ifstarted", 0, NULL, 's' },
44-
{ "ifstopped", 0, NULL, 'S' },
45-
{ "list", 0, NULL, 'l' },
46-
{ "resolve", 1, NULL, 'r' },
47-
{ "dry-run", 0, NULL, 'Z' },
48-
longopts_COMMON
49-
};
50-
const char * const longopts_help[] = {
51-
"set xtrace when running the command",
52-
"ignore dependencies",
53-
"tests if the service exists or not",
54-
"if the service is crashed run the command",
55-
"if the service exists run the command",
56-
"if the service is inactive run the command",
57-
"if the service is not started run the command",
58-
"if the service is started run the command",
59-
"if the service is stopped run the command",
60-
"list all available services",
61-
"resolve the service name to an init script",
62-
"dry run (show what would happen)",
63-
longopts_help_COMMON
64-
};
34+
#define opts(opt, opt_long) \
35+
opt(d, debug, no_argument, "set xtrace when running the command") \
36+
opt(D, nodeps, no_argument, "ignore dependencies") \
37+
opt(e, exists, required_argument, "tests if the service exists or not") \
38+
opt(c, ifcrashed, no_argument, "if the service is crashed run the command") \
39+
opt(i, ifexists, no_argument, "if the service exists run the command") \
40+
opt(I, ifinactive, no_argument, "if the service is inactive run the command") \
41+
opt(N, ifnotstarted, no_argument, "if the service is not started run the command") \
42+
opt(s, ifstarted, no_argument, "if the service is started run the command") \
43+
opt(S, ifstopped, no_argument, "if the service is stopped run the command") \
44+
opt(l, list, no_argument, "list all available services") \
45+
opt(r, resolve, required_argument, "resolve the service name to an init script") \
46+
opt(Z, dry-run, no_argument, "dry run (show what would happen)")
47+
cmdline_opts(opts)
48+
6549
const char *usagestring = ""
6650
"Usage: rc-service [options] [-i] <service> <cmd>...\n"
6751
" or: rc-service [options] -e <service>\n"

0 commit comments

Comments
 (0)