]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
config.c: retain leading whitespace before comments
authorAllan Nathanson <42244061+Allan-N@users.noreply.github.com>
Wed, 30 Oct 2024 20:52:41 +0000 (16:52 -0400)
committerAsterisk Development Team <asteriskteam@digium.com>
Thu, 23 Jan 2025 18:39:41 +0000 (18:39 +0000)
Configurations loaded with the ast_config_load2() API and later written
out with ast_config_text_file_save2() will have any leading whitespace
stripped away.  The APIs should make reasonable efforts to maintain the
content and formatting of the configuration files.

This change retains any leading whitespace from comment lines that start
with a ";".

Resolves: https://github.com/asterisk/asterisk/issues/970
(cherry picked from commit 4528f5f25a5f4445eec7ebe0a1d9e925b005873b)

main/config.c

index 3d7bcbe62fc4ee1f8ab79a1c390e653a2e65cd6f..1fa567950e47a98f2f2b2ee0d343e2a06491b15e 100644 (file)
@@ -2469,6 +2469,18 @@ static struct ast_config *config_text_file_load(const char *database, const char
                                                                if (!comment) {
                                                                        /* If ; is found, and we are not nested in a comment,
                                                                           we immediately stop all comment processing */
+                                                                       while ((comment_p > new_buf) && isspace(*(comment_p - 1))) {
+                                                                               /* To preserve comment formatting we want to include any
+                                                                                  whitespace before the ";" */
+                                                                               comment_p--;
+                                                                       }
+                                                                       if ((comment_p != new_buf) && (*comment_p == ' ') && (*(comment_p + 1) == ' ')) {
+                                                                               /* If the comment (w/whitespace) does not start in column 1
+                                                                                  then, when written out, the preceeding variable/value will
+                                                                                  be separated from the comment by two spaces. To preserve
+                                                                                  comment formatting we should also take this into account. */
+                                                                               comment_p += 2;
+                                                                       }
                                                                        if (ast_test_flag(&flags, CONFIG_FLAG_WITHCOMMENTS)) {
                                                                                CB_ADD(&lline_buffer, comment_p);
                                                                        }