aboutsummaryrefslogtreecommitdiff
path: root/src/rc-depend.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rc-depend.c')
-rw-r--r--src/rc-depend.c114
1 files changed, 79 insertions, 35 deletions
diff --git a/src/rc-depend.c b/src/rc-depend.c
index 5129e9b6..b86e17db 100644
--- a/src/rc-depend.c
+++ b/src/rc-depend.c
@@ -5,9 +5,13 @@
Released under the GPLv2
*/
+#define APPLET "rc-depend"
+
#include <sys/types.h>
#include <sys/stat.h>
+#include <getopt.h>
+#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -31,56 +35,97 @@ rc_depinfo_t *_rc_deptree_load (void) {
return (rc_deptree_load ());
}
+static char *applet = NULL;
+
+#include "_usage.h"
+#define getoptstring "t:suT" getoptstring_COMMON
+static struct option longopts[] = {
+ { "type", 0, NULL, 't'},
+ { "notrace", 0, NULL, 'T'},
+ { "strict", 0, NULL, 's'},
+ { "update", 0, NULL, 'u'},
+ longopts_COMMON
+ { NULL, 0, NULL, 0}
+};
+static const char * const longopts_help[] = {
+ "Type(s) of dependency to list",
+ "Don't trace service dependencies",
+ "Only use what is in the runlevels",
+ "Force an update of the dependency tree",
+ longopts_help_COMMON
+};
+#include "_usage.c"
+
int rc_depend (int argc, char **argv)
{
char **types = NULL;
char **services = NULL;
char **depends = NULL;
+ char **list;
rc_depinfo_t *deptree = NULL;
- rc_depinfo_t *di;
char *service;
int options = RC_DEP_TRACE;
bool first = true;
int i;
bool update = false;
char *runlevel = getenv ("RC_SOFTLEVEL");
-
- if (! runlevel)
- runlevel = rc_runlevel_get ();
-
- for (i = 1; i < argc; i++) {
- if (strcmp (argv[i], "--update") == 0) {
- if (! update) {
- ebegin ("Caching service dependencies");
- update = (rc_deptree_update () == 0);
- eend (update ? 0 : -1, "Failed to update the dependency tree");
- }
- continue;
- }
-
- if (strcmp (argv[i], "--strict") == 0) {
- options |= RC_DEP_STRICT;
- continue;
+ int opt;
+ char *token;
+
+ applet = argv[0];
+
+ while ((opt = getopt_long (argc, argv, getoptstring,
+ longopts, (int *) 0)) != -1)
+ {
+ switch (opt) {
+ case 's':
+ options |= RC_DEP_STRICT;
+ break;
+ case 't':
+ while ((token = strsep (&optarg, ",")))
+ rc_strlist_addu (&types, token);
+ break;
+ case 'u':
+ update = true;
+ break;
+ case 'T':
+ options &= RC_DEP_TRACE;
+ break;
+ case '?':
+ einfo ("hello");
+
+ case_RC_COMMON_GETOPT
}
+ }
- if (strcmp (argv[i], "--notrace") == 0) {
- options &= RC_DEP_TRACE;
- continue;
- }
+ if (update) {
+ bool u = false;
+ ebegin ("Caching service dependencies");
+ u = rc_deptree_update ();
+ eend (u ? 0 : -1, "%s: %s", applet, strerror (errno));
+ if (! u)
+ eerrorx ("Failed to update the dependency tree");
+ }
- if (argv[i][0] == '-') {
- argv[i]++;
- rc_strlist_add (&types, argv[i]);
- } else {
- if ((deptree = _rc_deptree_load ()) == NULL)
- eerrorx ("failed to load deptree");
+ if (! runlevel)
+ runlevel = rc_runlevel_get ();
- di = rc_deptree_depinfo (deptree, argv[i]);
- if (! di)
- eerror ("no dependency info for service `%s'", argv[i]);
- else
- rc_strlist_add (&services, argv[i]);
- }
+ if (! (deptree = _rc_deptree_load ()))
+ eerrorx ("failed to load deptree");
+
+ while (optind < argc) {
+ list = NULL;
+ rc_strlist_add (&list, argv[optind]);
+ errno = 0;
+ depends = rc_deptree_depends (deptree, NULL, list, runlevel, 0);
+ if (! depends && errno == ENOENT)
+ eerror ("no dependency info for service `%s'", argv[optind]);
+ else
+ rc_strlist_add (&services, argv[optind]);
+
+ rc_strlist_free (depends);
+ rc_strlist_free (list);
+ optind++;
}
if (! services) {
@@ -117,6 +162,5 @@ int rc_depend (int argc, char **argv)
rc_strlist_free (services);
rc_strlist_free (depends);
rc_deptree_free (deptree);
-
return (EXIT_SUCCESS);
}