changeset 33:e1004fcc93c9

flattened conditional nesting
author meillo@marmaro.de
date Thu, 06 May 2010 13:31:57 +0200 (2010-05-06)
parents 941f755e2965
children 8ea86ac25658
files src/online.c
diffstat 1 files changed, 76 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/src/online.c	Thu May 06 13:24:49 2010 +0200
+++ b/src/online.c	Thu May 06 13:31:57 2010 +0200
@@ -44,28 +44,31 @@
 	old_signal = signal(SIGCHLD, SIG_DFL);
 
 	in = peopen(pipe, "r", environ, &pid);
-	if (in != NULL) {
-		gchar output[256];
-		if (fgets(output, 255, in)) {
-			g_strchomp(g_strchug(output));
-			if (strlen(output) == 0) {
-				logwrite(LOG_ALERT, "only whitespace connection name\n");
-				name = NULL;
-			} else {
-				name = g_strdup(output);
-			}
+	if (in == NULL) {
+		logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
+		signal(SIGCHLD, old_signal);
+		return NULL;
+	}
+
+	gchar output[256];
+	if (fgets(output, 255, in)) {
+		g_strchomp(g_strchug(output));
+		if (strlen(output) == 0) {
+			logwrite(LOG_ALERT, "only whitespace connection name\n");
+			name = NULL;
 		} else {
-			logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
-			name = NULL;
+			name = g_strdup(output);
 		}
-		fclose(in);
-		waitpid(pid, &status, 0);
-		if (WEXITSTATUS(status) != EXIT_SUCCESS) {
-			g_free(name);
-			name = NULL;
-		}
-	} else
-		logwrite(LOG_ALERT, "could not open pipe '%s': %s\n", pipe, strerror(errno));
+	} else {
+		logwrite(LOG_ALERT, "nothing read from pipe %s\n", pipe);
+		name = NULL;
+	}
+	fclose(in);
+	waitpid(pid, &status, 0);
+	if (WEXITSTATUS(status) != EXIT_SUCCESS) {
+		g_free(name);
+		name = NULL;
+	}
 
 	signal(SIGCHLD, old_signal);
 
@@ -75,58 +78,63 @@
 gchar*
 detect_online()
 {
-	if (conf.online_detect != NULL) {
-		if (strcmp(conf.online_detect, "file") == 0) {
-			DEBUG(3) debugf("online detection method 'file'\n");
-			if (conf.online_file != NULL) {
-				struct stat st;
-				if (stat(conf.online_file, &st) == 0) {
-					FILE *fptr = fopen(conf.online_file, "r");
-					if (fptr) {
-						char buf[256];
-						if (fgets(buf, 256, fptr) == NULL) {
-							logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
-							fclose(fptr);
-							return NULL;
-						}
-						g_strchomp(g_strchug(buf));
-						fclose(fptr);
-						if (strlen(buf) == 0) {
-							logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
-							return NULL;
-						}
-						return g_strdup(buf);
-					} else {
-						logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
-						return NULL;
-					}
-				} else if (errno == ENOENT) {
-					logwrite(LOG_NOTICE, "not online.\n");
-					return NULL;
-				} else {
-					logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
-					return NULL;
-				}
-			} else
-				logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
-#ifdef ENABLE_MSERVER
-		} else if (strcmp(conf.online_detect, "mserver") == 0) {
-			DEBUG(3) debugf("connection method 'mserver'\n");
-			return mserver_detect_online(conf.mserver_iface);
-#endif
-		} else if (strcmp(conf.online_detect, "pipe") == 0) {
-			DEBUG(3) debugf("connection method 'pipe'\n");
-			if (conf.online_pipe)
-				return detect_online_pipe(conf.online_pipe);
-			else {
-				logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
+	if (conf.online_detect == NULL) {
+		return NULL;
+	}
+
+	if (strcmp(conf.online_detect, "file") == 0) {
+		DEBUG(3) debugf("online detection method 'file'\n");
+		if (conf.online_file != NULL) {
+			logwrite(LOG_ALERT, "online detection mode is 'file', but online_file is undefined\n");
+			return NULL;
+		}
+
+		struct stat st;
+		if (stat(conf.online_file, &st) == 0) {
+			FILE *fptr = fopen(conf.online_file, "r");
+			if (!fptr) {
+				logwrite(LOG_ALERT, "opening of %s failed: %s\n", conf.online_file, strerror(errno));
+				return NULL;
+			}
+			char buf[256];
+			if (fgets(buf, 256, fptr) == NULL) {
+				logwrite(LOG_ALERT, "empty online file %s\n", conf.online_file);
+				fclose(fptr);
+				return NULL;
+			}
+			g_strchomp(g_strchug(buf));
+			fclose(fptr);
+			if (strlen(buf) == 0) {
+				logwrite(LOG_ALERT, "only whitespace connection name in %s\n", conf.online_file);
 				return NULL;
 			}
-		} else if (strcmp(conf.online_detect, "argument") == 0) {
-			return connection_name;
+			return g_strdup(buf);
+		} else if (errno == ENOENT) {
+			logwrite(LOG_NOTICE, "not online.\n");
+			return NULL;
 		} else {
-			DEBUG(3) debugf("no connection method selected\n");
+			logwrite(LOG_ALERT, "stat of %s failed: %s", conf.online_file, strerror(errno));
+			return NULL;
 		}
+
+#ifdef ENABLE_MSERVER
+	} else if (strcmp(conf.online_detect, "mserver") == 0) {
+		DEBUG(3) debugf("connection method 'mserver'\n");
+		return mserver_detect_online(conf.mserver_iface);
+#endif
+	} else if (strcmp(conf.online_detect, "pipe") == 0) {
+		DEBUG(3) debugf("connection method 'pipe'\n");
+		if (conf.online_pipe)
+			return detect_online_pipe(conf.online_pipe);
+		else {
+			logwrite(LOG_ALERT, "online detection mode is 'pipe', but online_pipe is undefined\n");
+			return NULL;
+		}
+	} else if (strcmp(conf.online_detect, "argument") == 0) {
+		return connection_name;
+	} else {
+		DEBUG(3) debugf("no connection method selected\n");
 	}
+
 	return NULL;
 }