From 5e100e18f15038da4ffb2dd0eb320e8057ce17df Mon Sep 17 00:00:00 2001
From: "Christopher J. Morrone" <morrone2@llnl.gov>
Date: Fri, 18 Aug 2006 00:12:54 +0000
Subject: [PATCH] Better way to handle comments under #SBATCH

---
 src/sbatch/opt.c | 34 +++++-----------------------------
 1 file changed, 5 insertions(+), 29 deletions(-)

diff --git a/src/sbatch/opt.c b/src/sbatch/opt.c
index 01be42a6607..fb7e494a525 100644
--- a/src/sbatch/opt.c
+++ b/src/sbatch/opt.c
@@ -1115,35 +1115,11 @@ static char *_next_line(const void *buf, int size, void **state)
 	return line;
 }
 
-/* Strip comments from a line by terminating the string
- * where the comment begins.
- * Everything after a non-escaped "#" is a comment.
- */
-static void _strip_comments(char *line)
-{
-	int i;
-	int len = strlen(line);
-	int bs_count = 0;
-
-	for (i = 0; i < len; i++) {
-		/* if # character is preceded by an even number of
-		 * escape characters '\' */
-		if (line[i] == '#' && (bs_count%2) == 0) {
-			line[i] = '\0';
- 			break;
-		} else if (line[i] == '\\') {
-			bs_count++;
-		} else {
-			bs_count = 0;
-		}
-	}
-}
-
 /*
  * _get_argument - scans a line for something that looks like a command line
  *	argument, and return an xmalloc'ed string containing the argument.
  *	Quotes can be used to group characters, including whitespace.
- *	Quotes can be included in an argument be escaping the quotes;
+ *	Quotes can be included in an argument be escaping the quotes,
  *	preceding the quote with a backslash (\").
  *
  * IN - line
@@ -1170,8 +1146,6 @@ static char *_get_argument(const char *line, int *skipped)
 	if (*ptr == '\0')
 		return NULL;
 
-	_strip_comments(ptr);
-
 	/* copy argument into "argument" buffer, */
 	i = 0;
 	while ((no_isspace_check || !isspace(*ptr))
@@ -1190,6 +1164,9 @@ static char *_get_argument(const char *line, int *skipped)
 			/* toggle the no_isspace_check flag */
 			no_isspace_check = no_isspace_check? false : true;
 			ptr++;
+		} else if (*ptr == '#') {
+			/* found an un-escaped #, rest of line is a comment */
+			break;
 		} else {
 			argument[i] = *ptr;
 			ptr++;
@@ -1237,8 +1214,7 @@ static void _opt_batch_script(const void *body, int size)
 		/* this line starts with the magic word */
 		ptr = line + strlen(magic_word);
 		while ((option = _get_argument(ptr, &skipped)) != NULL) {
-			debug2("\tFound argument \"%s\" skipped = %d",
-			       option, skipped);
+			debug2("Found in script, argument \"%s\"", option);
 			argc += 1;
 			xrealloc(argv, sizeof(char*) * argc);
 			argv[argc-1] = option;
-- 
GitLab