Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5ff47ac

Browse files
committed
entab: add new options
Add new entab options to process only C comment whitespace after periods, and to protect leading whitespace.
1 parent e9afdf2 commit 5ff47ac

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/tools/entab/entab.c

+33-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ main(int argc, char **argv)
5151
{
5252
int tab_size = 8,
5353
min_spaces = 2,
54+
only_comment_periods = FALSE,
5455
protect_quotes = FALSE,
56+
protect_leading_whitespace = FALSE,
5557
del_tabs = FALSE,
5658
clip_lines = FALSE,
59+
in_comment = FALSE,
60+
was_period = FALSE,
5761
prv_spaces,
5862
col_in_tab,
5963
escaped,
60-
nxt_spaces;
64+
nxt_spaces,
65+
in_leading_whitespace;
6166
char in_line[BUFSIZ],
6267
out_line[BUFSIZ],
6368
*src,
@@ -74,7 +79,7 @@ main(int argc, char **argv)
7479
if (strcmp(cp, "detab") == 0)
7580
del_tabs = 1;
7681

77-
while ((ch = getopt(argc, argv, "cdhqs:t:")) != -1)
82+
while ((ch = getopt(argc, argv, "cdhlmqs:t:")) != -1)
7883
switch (ch)
7984
{
8085
case 'c':
@@ -83,6 +88,13 @@ main(int argc, char **argv)
8388
case 'd':
8489
del_tabs = TRUE;
8590
break;
91+
case 'l':
92+
protect_leading_whitespace = TRUE;
93+
break;
94+
case 'm':
95+
/* only process text followed by periods in C comments */
96+
only_comment_periods = TRUE;
97+
break;
8698
case 'q':
8799
protect_quotes = TRUE;
88100
break;
@@ -97,6 +109,8 @@ main(int argc, char **argv)
97109
fprintf(stderr, "USAGE: %s [ -cdqst ] [file ...]\n\
98110
-c (clip trailing whitespace)\n\
99111
-d (delete tabs)\n\
112+
-l (protect leading whitespace)\n\
113+
-m (only C comment periods)\n\
100114
-q (protect quotes)\n\
101115
-s minimum_spaces\n\
102116
-t tab_width\n",
@@ -134,13 +148,24 @@ main(int argc, char **argv)
134148
if (escaped == FALSE)
135149
quote_char = ' ';
136150
escaped = FALSE;
151+
in_leading_whitespace = TRUE;
137152

138153
/* process line */
139154
while (*src != NUL)
140155
{
141156
col_in_tab++;
157+
158+
/* look backward so we handle slash-star-slash properly */
159+
if (!in_comment && src > in_line &&
160+
*(src - 1) == '/' && *src == '*')
161+
in_comment = TRUE;
162+
else if (in_comment && *src == '*' && *(src + 1) == '/')
163+
in_comment = FALSE;
164+
142165
/* Is this a potential space/tab replacement? */
143-
if (quote_char == ' ' && (*src == ' ' || *src == '\t'))
166+
if ((!only_comment_periods || (in_comment && was_period)) &&
167+
(!protect_leading_whitespace || !in_leading_whitespace) &&
168+
quote_char == ' ' && (*src == ' ' || *src == '\t'))
144169
{
145170
if (*src == '\t')
146171
{
@@ -192,6 +217,11 @@ main(int argc, char **argv)
192217
/* Not a potential space/tab replacement */
193218
else
194219
{
220+
/* allow leading stars in comments */
221+
if (in_leading_whitespace && *src != ' ' && *src != '\t' &&
222+
(!in_comment || *src != '*'))
223+
in_leading_whitespace = FALSE;
224+
was_period = (*src == '.');
195225
/* output accumulated spaces */
196226
output_accumulated_spaces(&prv_spaces, &dst);
197227
/* This can only happen in a quote. */

0 commit comments

Comments
 (0)