Add an easy way to edit a configuration file.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 15 Jul 2012 18:37:38 +0000 (20:37 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 15 Jul 2012 18:37:38 +0000 (20:37 +0200)
doc/tinc.texi
doc/tincctl.8.in
src/tincctl.c

index 2dbfdfb..66300ea 100644 (file)
@@ -2056,6 +2056,10 @@ As above, but without removing any previously existing configuration variables.
 Remove configuration variables with the same name and @var{value}.
 If no @var{value} is given, all configuration variables with the same name will be removed.
 
 Remove configuration variables with the same name and @var{value}.
 If no @var{value} is given, all configuration variables with the same name will be removed.
 
+@item edit @var{filename}
+Start an editor for the given configuration file.
+You do not need to specify the full path to the file.
+
 @item start
 Start @samp{tincd}.
 
 @item start
 Start @samp{tincd}.
 
index 9f11e11..8a9bfcd 100644 (file)
@@ -68,6 +68,9 @@ Remove configuration variables with the same name and
 If no
 .Ar value
 is given, all configuration variables with the same name will be removed.
 If no
 .Ar value
 is given, all configuration variables with the same name will be removed.
+.It edit Ar filename
+Start an editor for the given configuration file.
+You do not need to specify the full path to the file.
 .It start
 Start
 .Xr tincd 8 .
 .It start
 Start
 .Xr tincd 8 .
index 523bbb4..6725b8b 100644 (file)
@@ -1269,6 +1269,59 @@ static int cmd_version(int argc, char *argv[]) {
        return 0;
 }
 
        return 0;
 }
 
+static const char *conffiles[] = {
+       "tinc.conf",
+       "tinc-up",
+       "tinc-down",
+       "subnet-up",
+       "subnet-down",
+       "host-up",
+       "host-down",
+       NULL,
+};
+
+static int cmd_edit(int argc, char *argv[]) {
+       if(argc != 2) {
+               fprintf(stderr, "Invalid number of arguments.\n");
+               return 1;
+       }
+
+       char *filename = NULL;
+
+       if(strncmp(argv[1], "hosts/", 6)) {
+               for(int i = 0; conffiles[i]; i++) {
+                       if(!strcmp(argv[1], conffiles[i])) {
+                               xasprintf(&filename, "%s/%s", confbase, argv[1]);
+                               break;
+                       }
+               }
+       } else {
+               argv[1] += 6;
+       }
+
+       if(!filename) {
+               xasprintf(&filename, "%s/%s", hosts_dir, argv[1]);
+               char *dash = strchr(argv[1], '-');
+               if(dash) {
+                       *dash++ = 0;
+                       if((strcmp(dash, "up") && strcmp(dash, "down")) || !check_id(argv[1])) {
+                               fprintf(stderr, "Invalid configuration filename.\n");
+                               return 1;
+                       }
+               }
+       }
+
+#ifndef HAVE_MINGW
+       char *editor = getenv("VISUAL") ?: getenv("EDITOR") ?: "vi";
+#else
+       char *editor = "edit"
+#endif
+
+       char *command;
+       xasprintf(&command, "\"%s\" \"%s\"", editor, filename);
+       return system(command);
+}
+
 static const struct {
        const char *command;
        int (*function)(int argc, char *argv[]);
 static const struct {
        const char *command;
        int (*function)(int argc, char *argv[]);
@@ -1294,6 +1347,7 @@ static const struct {
        {"generate-ecdsa-keys", cmd_generate_ecdsa_keys},
        {"help", cmd_help},
        {"version", cmd_version},
        {"generate-ecdsa-keys", cmd_generate_ecdsa_keys},
        {"help", cmd_help},
        {"version", cmd_version},
+       {"edit", cmd_edit},
        {NULL, NULL},
 };
 
        {NULL, NULL},
 };