From 9c12e5df115402b553b63717c719a5feebc94386 Mon Sep 17 00:00:00 2001
From: nakst <>
Date: Tue, 14 Dec 2021 16:55:02 +0000
Subject: [PATCH] better support for automated port building

---
 util/build.c | 70 ++++++++++++++++++++++++++++++----------------------
 1 file changed, 40 insertions(+), 30 deletions(-)

diff --git a/util/build.c b/util/build.c
index c8d4aa0..522adb4 100644
--- a/util/build.c
+++ b/util/build.c
@@ -1466,38 +1466,48 @@ void DoCommand(const char *l) {
 		printf(ColorNormal);
 	} else if (0 == memcmp(l, "a2l ", 4)) {
 		AddressToLine(l + 3);
-	} else if (0 == strcmp(l, "build-port")) {
-		printf("\nAvailable ports:\n");
-		DIR *directory = opendir("ports");
-		struct dirent *entry;
-
-		while ((entry = readdir(directory))) {
-			char buffer[4096];
-			snprintf(buffer, sizeof(buffer), "ports/%s/port.sh", entry->d_name);
-			FILE *f = fopen(buffer, "rb");
-			
-			if (f) {
-				printf("\t%s\n", entry->d_name);
-				fclose(f);
-			}
-		}
-
-		closedir(directory);
-
-		LoadOptions();
-
-		if (!IsOptionEnabled("Flag.ENABLE_POSIX_SUBSYSTEM")) {
-			printf("\nMost ports require the POSIX subsystem to be enabled.\n");
-			printf("Run " ColorHighlight "config" ColorNormal " and select " ColorHighlight "Flag.ENABLE_POSIX_SUBSYSTEM" ColorNormal " to enable it.\n");
-		}
-
-		printf("\nEnter the port to be built: ");
+	} else if (0 == strcmp(l, "build-port") || 0 == memcmp(l, "build-port ", 11)) {
+		bool alreadyNamedPort = l[10] == ' ';
 		char *l2 = NULL;
-		size_t pos;
-		getline(&l2, &pos, stdin);
-		l2[strlen(l2) - 1] = 0;
+
+		if (!alreadyNamedPort) {
+			printf("\nAvailable ports:\n");
+			DIR *directory = opendir("ports");
+			struct dirent *entry;
+
+			while ((entry = readdir(directory))) {
+				char buffer[4096];
+				snprintf(buffer, sizeof(buffer), "ports/%s/port.sh", entry->d_name);
+				FILE *f = fopen(buffer, "rb");
+
+				if (f) {
+					printf("\t%s\n", entry->d_name);
+					fclose(f);
+				}
+			}
+
+			closedir(directory);
+
+			LoadOptions();
+
+			if (!IsOptionEnabled("Flag.ENABLE_POSIX_SUBSYSTEM")) {
+				printf("\nMost ports require the POSIX subsystem to be enabled.\n");
+				printf("Run " ColorHighlight "config" ColorNormal " and select " ColorHighlight "Flag.ENABLE_POSIX_SUBSYSTEM" ColorNormal " to enable it.\n");
+			}
+
+			printf("\nEnter the port to be built: ");
+			size_t pos;
+			getline(&l2, &pos, stdin);
+			l2[strlen(l2) - 1] = 0;
+		} else {
+			l2 = (char *) l + 11;
+		}
+
 		CallSystemF("ports/%s/port.sh", l2);
-		free(l2);
+
+		if (!alreadyNamedPort) {
+			free(l2);
+		}
 	} else if (0 == memcmp(l, "get-source ", 11)) {
 		if (CallSystem("mkdir -p bin/cache && rm -rf bin/source")) {
 			exit(1);