Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <kdbease.h>
10 : #include <kdbhelper.h>
11 : #include <kdbopts.h>
12 :
13 : #include <stdio.h>
14 : #include <stdlib.h>
15 :
16 : extern char ** environ;
17 :
18 : #define BASE_KEY "/sw/org/erm/#0/current"
19 : #define SPEC_BASE_KEY "spec" BASE_KEY
20 :
21 0 : static KeySet * createSpec (void)
22 : {
23 0 : return ksNew (
24 : 10,
25 : keyNew (SPEC_BASE_KEY "/emptydirs", KEY_META, "description", "remove empty directories", KEY_META, "opt", "d", KEY_META,
26 : "opt/arg", "none", KEY_META, "opt/long", "dir", KEY_END),
27 : keyNew (SPEC_BASE_KEY "/force", KEY_META, "description", "ignore nonexistent files and arguments, never prompt", KEY_META,
28 : "opt", "f", KEY_META, "opt/arg", "none", KEY_META, "opt/long", "force", KEY_END),
29 : keyNew (SPEC_BASE_KEY "/interactive", KEY_META, "description",
30 : "prompt according to WHEN: never, once (-I), or always (-i), without WHEN, prompt always", KEY_META, "opt", "#1",
31 : KEY_META, "opt/#0", "i", KEY_META, "opt/#0/arg", "optional", KEY_META, "opt/#0/flagvalue", "always", KEY_META,
32 : "opt/#0/long", "interactive", KEY_META, "opt/#1", "I", KEY_META, "opt/#1/arg", "none", KEY_META, "opt/#1/flagvalue",
33 : "once", KEY_META, "opt/arg/name", "WHEN", KEY_END),
34 : keyNew (SPEC_BASE_KEY "/nopreserve", KEY_META, "description", "do not treat '/' specially", KEY_META, "opt/arg", "none",
35 : KEY_META, "opt/long", "no-preserve-root", KEY_END),
36 : keyNew (SPEC_BASE_KEY "/preserve", KEY_META, "description",
37 : "do not remove '/' (default), with 'all', reject any command line argument on a separate device from its parent",
38 : KEY_META, "opt/arg", "optional", KEY_META, "opt/arg/name", "all", KEY_META, "opt/flagvalue", "root", KEY_META,
39 : "opt/long", "preserve-root", KEY_END),
40 : keyNew (SPEC_BASE_KEY "/recursive", KEY_META, "description", "remove directories and their contents recursively", KEY_META,
41 : "opt", "#1", KEY_META, "opt/#0", "r", KEY_META, "opt/#0/arg", "none", KEY_META, "opt/#0/long", "recursive",
42 : KEY_META, "opt/#1", "R", KEY_META, "opt/#1/arg", "none", KEY_END),
43 : keyNew (SPEC_BASE_KEY "/showversion", KEY_META, "description", "output version information and exit", KEY_META, "opt/arg",
44 : "none", KEY_META, "opt/long", "version", KEY_END),
45 : keyNew (SPEC_BASE_KEY "/singlefs", KEY_META, "description",
46 : "when removing a hierarchy recursively, skip any directory that is on a file system different from that of the "
47 : "corresponding line argument",
48 : KEY_META, "opt/arg", "none", KEY_META, "opt/long", "one-file-system", KEY_END),
49 : keyNew (SPEC_BASE_KEY "/verbose", KEY_META, "description", "explain what is being done", KEY_META, "opt", "v", KEY_META,
50 : "opt/arg", "none", KEY_META, "opt/long", "verbose", KEY_META, "env", "VERBOSE", KEY_END),
51 : keyNew (SPEC_BASE_KEY "/files/#", KEY_META, "description", "the files that shall be deleted", KEY_META, "args", "remaining",
52 : KEY_META, "env", "FILES", KEY_END),
53 : KS_END);
54 : }
55 :
56 0 : int main (int argc, const char ** argv)
57 : {
58 0 : KeySet * ks = createSpec ();
59 0 : Key * errorKey = keyNew (BASE_KEY, KEY_END);
60 :
61 0 : int result = elektraGetOpts (ks, argc, argv, (const char **) environ, errorKey);
62 0 : if (result == -1)
63 : {
64 0 : fprintf (stderr, "ERROR: %s\n", keyString (keyGetMeta (errorKey, "error/reason")));
65 0 : keyDel (errorKey);
66 0 : ksDel (ks);
67 0 : return EXIT_FAILURE;
68 : }
69 :
70 0 : if (result == 1)
71 : {
72 0 : char * help = elektraGetOptsHelpMessage (errorKey, NULL, NULL);
73 0 : fprintf (stderr, "%s\n", help);
74 0 : elektraFree (help);
75 0 : keyDel (errorKey);
76 0 : ksDel (ks);
77 0 : return EXIT_SUCCESS;
78 : }
79 :
80 0 : printf ("When called with the same arguments 'rm' \n");
81 :
82 0 : Key * lookup = ksLookupByName (ks, BASE_KEY "/emptydirs", 0);
83 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
84 : {
85 0 : printf ("will delete empty directories\n");
86 : }
87 :
88 0 : lookup = ksLookupByName (ks, BASE_KEY "/force", 0);
89 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
90 : {
91 0 : printf ("will use force mode\n");
92 : }
93 :
94 0 : lookup = ksLookupByName (ks, BASE_KEY "/interactive", 0);
95 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "never") == 0)
96 : {
97 0 : printf ("will not use interactive mode\n");
98 : }
99 0 : else if (lookup != NULL && elektraStrCmp (keyString (lookup), "once") == 0)
100 : {
101 0 : printf ("will use interactive mode; ask once\n");
102 : }
103 0 : else if (lookup != NULL && elektraStrCmp (keyString (lookup), "always") == 0)
104 : {
105 0 : printf ("will use interactive mode; always ask\n");
106 : }
107 :
108 0 : lookup = ksLookupByName (ks, BASE_KEY "/nopreserve", 0);
109 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
110 : {
111 0 : printf ("will not treat '/' specially\n");
112 : }
113 :
114 0 : lookup = ksLookupByName (ks, BASE_KEY "/preserve", 0);
115 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "root") == 0)
116 : {
117 0 : printf ("will never remove '/'");
118 : }
119 0 : else if (lookup != NULL && elektraStrCmp (keyString (lookup), "all") == 0)
120 : {
121 0 : printf ("will reject any argument on separate device from its parent\n");
122 : }
123 :
124 0 : lookup = ksLookupByName (ks, BASE_KEY "/recursive", 0);
125 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
126 : {
127 0 : printf ("will delete recursively\n");
128 : }
129 :
130 0 : lookup = ksLookupByName (ks, BASE_KEY "/showversion", 0);
131 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
132 : {
133 0 : printf ("will show version and exit\n");
134 : }
135 :
136 0 : lookup = ksLookupByName (ks, BASE_KEY "/singlefs", 0);
137 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
138 : {
139 0 : printf ("will skip directories on different file systems\n");
140 : }
141 :
142 0 : lookup = ksLookupByName (ks, BASE_KEY "/verbose", 0);
143 0 : if (lookup != NULL && elektraStrCmp (keyString (lookup), "1") == 0)
144 : {
145 0 : printf ("will explain what is being done\n");
146 : }
147 :
148 0 : printf ("will remove the following files:\n");
149 :
150 0 : Key * arrayParent = ksLookupByName (ks, BASE_KEY "/files", 0);
151 0 : KeySet * files = elektraArrayGet (arrayParent, ks);
152 :
153 0 : ksRewind (files);
154 0 : Key * cur = NULL;
155 0 : while ((cur = ksNext (files)) != NULL)
156 : {
157 0 : printf (" %s\n", keyString (cur));
158 : }
159 0 : printf ("\n");
160 :
161 0 : keyDel (errorKey);
162 0 : ksDel (ks);
163 :
164 0 : return EXIT_SUCCESS;
165 : }
|