client: Clean up variable declarations.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 20 Dec 2008 17:06:11 +0000 (17:06 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Sat, 20 Dec 2008 17:06:11 +0000 (17:06 +0000)
The current code for main is somewhat messy, with inner blocks for
declaring variables in.  We actually need some of these variables to
have wider scopes now, so it makes sense to just lift them out to
toplevel.

In particular, we promote the main sel_state to a static, and the
sel_files to top-level in main.

client/tripectl.c

index 6a2f82a..e2411af 100644 (file)
@@ -74,6 +74,7 @@
 
 /*----- Static variables --------------------------------------------------*/
 
+static sel_state sel;
 static const char *pidfile = 0;
 static const char *logname = 0;
 static FILE *logfp = 0;
@@ -342,8 +343,18 @@ int main(int argc, char *argv[])
   string_v spawnopts = DA_INIT;
   char *p;
   FILE *pidfp = 0;
+  int i;
+  size_t sz;
   uid_t u = -1;
   gid_t g = -1;
+  int pfd[2];
+  pid_t kid;
+  struct sigaction sa;
+  sigset_t newmask, oldmask;
+  struct sockaddr_un sun;
+  selbuf bu, bs;
+  dstr d = DSTR_INIT;
+  sig hup;
 
   ego(argv[0]);
 
@@ -376,7 +387,7 @@ int main(int argc, char *argv[])
       { 0,             0,              0,      0 }
     };
 
-    int i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts, 0, 0, 0);
+    i = mdwopt(argc, argv, "+hvuDU:G:d:a:sp:S:lwf:nP:", opts, 0, 0, 0);
     if (i < 0)
       break;
     switch (i) {
@@ -453,6 +464,8 @@ int main(int argc, char *argv[])
     die(EXIT_FAILURE, "couldn't open `%s' for writing: %s",
        pidfile, strerror(errno));
   }
+  sel_init(&sel);
+  sig_init(&sel);
   signal(SIGINT, sigdie);
   signal(SIGQUIT, sigdie);
   signal(SIGTERM, sigdie);
@@ -461,11 +474,6 @@ int main(int argc, char *argv[])
   /* --- Connect to the server --- */
 
   if (f & f_spawn) {
-    int pfd[2];
-    pid_t kid;
-    struct sigaction sa;
-    sigset_t newmask, oldmask;
-
     sa.sa_handler = reap;
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = SA_NOCLDSTOP;
@@ -504,8 +512,7 @@ int main(int argc, char *argv[])
     fd = pfd[0];
     close(pfd[1]);
   } else {
-    struct sockaddr_un sun;
-    size_t sz = strlen(sock) + 1;
+    sz = strlen(sock) + 1;
     if (sz > sizeof(sun.sun_path))
       die(EXIT_FAILURE, "socket name `%s' too long", sock);
     memset(&sun, 0, sizeof(sun));
@@ -536,10 +543,6 @@ int main(int argc, char *argv[])
   if (optind == argc)
     setup("WATCH -A+tw");
   if (!(f & f_noinput) && optind == argc) {
-    sel_state sel;
-    selbuf bu, bs;
-
-    sel_init(&sel);
     selbuf_init(&bu, &sel, STDIN_FILENO, uline, &bu);
     selbuf_init(&bs, &sel, fd, sline, &bs);
     for (;;) {
@@ -551,7 +554,6 @@ int main(int argc, char *argv[])
   /* --- If there's a command, submit it --- */
 
   if (optind < argc) {
-    dstr d = DSTR_INIT;
     setup((f & f_warn) ? "WATCH -A+w" : "WATCH -A");
     while (optind < argc)
       u_quotify(&d, argv[optind++]);
@@ -565,24 +567,15 @@ int main(int argc, char *argv[])
 
   /* --- Pull everything else out of the box --- */
 
-  {
-    sel_state sel;
-    selbuf b;
-    sig hup;
+  selbuf_init(&bs, &sel, fd, cline, 0);
 
-    sel_init(&sel);
-    selbuf_init(&b, &sel, fd, cline, 0);
-
-    if (f & f_syslog)
-      openlog(QUIS, 0, LOG_DAEMON);
-    if (logfp) {
-      sig_init(&sel);
-      sig_add(&hup, SIGHUP, sighup, 0);
-    }
-    for (;;) {
-      if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
-       die(EXIT_FAILURE, "select failed: %s", strerror(errno));
-    }
+  if (f & f_syslog)
+    openlog(QUIS, 0, LOG_DAEMON);
+  if (logfp)
+    sig_add(&hup, SIGHUP, sighup, 0);
+  for (;;) {
+    if (sel_select(&sel) && errno != EINTR && errno != EAGAIN)
+      die(EXIT_FAILURE, "select failed: %s", strerror(errno));
   }
 
   return (0);