Patchwork [6,of,7,c-hglib] examples: rewrite merge example

login
register
mail settings
Submitter Giovanni Gherdovich
Date Nov. 23, 2014, 5:54 p.m.
Message ID <2fcddadba4b91e1f5fb7.1416765290@tosh>
Download mbox | patch
Permalink /patch/6835/
State Accepted
Delegated to: Pierre-Yves David
Headers show

Comments

Giovanni Gherdovich - Nov. 23, 2014, 5:54 p.m.
# HG changeset patch
# User Giovanni Gherdovich <g.gherdovich@gmail.com>
# Date 1416754299 -3600
#      Sun Nov 23 15:51:39 2014 +0100
# Branch refactor examples
# Node ID 2fcddadba4b91e1f5fb75a1aa42981b48582732d
# Parent  c0b9754029daf740bbf4ee09cb6183f46d7f344f
examples: rewrite merge example

Rewrite the merge example to account for the change
in hg_read_header() that returns NULL if no header on pipe
and to use a switch statement to make the channel-based
behaviour more explicit.

Patch

diff -r c0b9754029da -r 2fcddadba4b9 examples/merge.c
--- a/examples/merge.c	Thu Nov 20 11:41:35 2014 +0100
+++ b/examples/merge.c	Sun Nov 23 15:51:39 2014 +0100
@@ -41,34 +41,39 @@ 
  * */
 int hg_merge_by_hand(hg_handle *handle, char *(*prompt)(char *buff, int size))
 {
-	char *comm[] = {"merge", "--tool=internal:prompt", NULL};
+	char *cmd[] = {"merge", "--tool=internal:prompt", NULL};
 	char buff[BUFF_SIZE];
 	char *prompt_msg;
 	int exitcode = 0;
-	int ns;
+	int exit = 0;
+	int n;
+	hg_header *header;
 
-	hg_rawcommand(handle, comm);
+	hg_rawcommand(handle, cmd);
 
-	hg_header *head;
-	while (head = hg_read_header(handle), head != NULL &&
-	       head->channel != r) {
-		if (head->channel == o) {
-			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
+	while (!exit) {
+		header = hg_read_header(handle);
+		switch (header->channel) {
+		case o:
+		case e:
+			while (n = hg_rawread(handle, buff, BUFF_SIZE), n > 0)
 				printf("%s", buff);
-			}
-		} else if (head->channel == e) {
-			if (ns = hg_rawread(handle, buff, BUFF_SIZE), ns > 0) {
-				printf("error data: %s", buff);
-			}
-		} else if (head->channel == L || head->channel == I) {
+			break;
+		case I:
+		case L:
 			prompt_msg = prompt(buff, strlen(buff));
 			hg_rawwrite(handle, prompt_msg, strlen(prompt_msg));
+			break;
+		case r:
+			exitcode = hg_exitcode(handle);
+			printf("exitcode = %d \n", exitcode);
+			exit = 1;
+			break;
+		case unknown_channel:
+			break;
 		}
 	}
 
-	exitcode = hg_exitcode(handle);
-	printf("exitcode = %d\n", exitcode);
-
 	return exitcode;
 }