@@ -51,13 +51,18 @@ main(int argc, char **argv)
51
51
{
52
52
int tab_size = 8 ,
53
53
min_spaces = 2 ,
54
+ only_comment_periods = FALSE,
54
55
protect_quotes = FALSE,
56
+ protect_leading_whitespace = FALSE,
55
57
del_tabs = FALSE,
56
58
clip_lines = FALSE,
59
+ in_comment = FALSE,
60
+ was_period = FALSE,
57
61
prv_spaces ,
58
62
col_in_tab ,
59
63
escaped ,
60
- nxt_spaces ;
64
+ nxt_spaces ,
65
+ in_leading_whitespace ;
61
66
char in_line [BUFSIZ ],
62
67
out_line [BUFSIZ ],
63
68
* src ,
@@ -74,7 +79,7 @@ main(int argc, char **argv)
74
79
if (strcmp (cp , "detab" ) == 0 )
75
80
del_tabs = 1 ;
76
81
77
- while ((ch = getopt (argc , argv , "cdhqs :t:" )) != -1 )
82
+ while ((ch = getopt (argc , argv , "cdhlmqs :t:" )) != -1 )
78
83
switch (ch )
79
84
{
80
85
case 'c' :
@@ -83,6 +88,13 @@ main(int argc, char **argv)
83
88
case 'd' :
84
89
del_tabs = TRUE;
85
90
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 ;
86
98
case 'q' :
87
99
protect_quotes = TRUE;
88
100
break ;
@@ -97,6 +109,8 @@ main(int argc, char **argv)
97
109
fprintf (stderr , "USAGE: %s [ -cdqst ] [file ...]\n\
98
110
-c (clip trailing whitespace)\n\
99
111
-d (delete tabs)\n\
112
+ -l (protect leading whitespace)\n\
113
+ -m (only C comment periods)\n\
100
114
-q (protect quotes)\n\
101
115
-s minimum_spaces\n\
102
116
-t tab_width\n" ,
@@ -134,13 +148,24 @@ main(int argc, char **argv)
134
148
if (escaped == FALSE)
135
149
quote_char = ' ' ;
136
150
escaped = FALSE;
151
+ in_leading_whitespace = TRUE;
137
152
138
153
/* process line */
139
154
while (* src != NUL )
140
155
{
141
156
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
+
142
165
/* 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' ))
144
169
{
145
170
if (* src == '\t' )
146
171
{
@@ -192,6 +217,11 @@ main(int argc, char **argv)
192
217
/* Not a potential space/tab replacement */
193
218
else
194
219
{
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 == '.' );
195
225
/* output accumulated spaces */
196
226
output_accumulated_spaces (& prv_spaces , & dst );
197
227
/* This can only happen in a quote. */
0 commit comments