entab: add new options
Add new entab options to process only C comment whitespace after periods, and to protect leading whitespace.
This commit is contained in:
parent
e9afdf2f4b
commit
5ff47acf8f
@ -51,13 +51,18 @@ main(int argc, char **argv)
|
|||||||
{
|
{
|
||||||
int tab_size = 8,
|
int tab_size = 8,
|
||||||
min_spaces = 2,
|
min_spaces = 2,
|
||||||
|
only_comment_periods = FALSE,
|
||||||
protect_quotes = FALSE,
|
protect_quotes = FALSE,
|
||||||
|
protect_leading_whitespace = FALSE,
|
||||||
del_tabs = FALSE,
|
del_tabs = FALSE,
|
||||||
clip_lines = FALSE,
|
clip_lines = FALSE,
|
||||||
|
in_comment = FALSE,
|
||||||
|
was_period = FALSE,
|
||||||
prv_spaces,
|
prv_spaces,
|
||||||
col_in_tab,
|
col_in_tab,
|
||||||
escaped,
|
escaped,
|
||||||
nxt_spaces;
|
nxt_spaces,
|
||||||
|
in_leading_whitespace;
|
||||||
char in_line[BUFSIZ],
|
char in_line[BUFSIZ],
|
||||||
out_line[BUFSIZ],
|
out_line[BUFSIZ],
|
||||||
*src,
|
*src,
|
||||||
@ -74,7 +79,7 @@ main(int argc, char **argv)
|
|||||||
if (strcmp(cp, "detab") == 0)
|
if (strcmp(cp, "detab") == 0)
|
||||||
del_tabs = 1;
|
del_tabs = 1;
|
||||||
|
|
||||||
while ((ch = getopt(argc, argv, "cdhqs:t:")) != -1)
|
while ((ch = getopt(argc, argv, "cdhlmqs:t:")) != -1)
|
||||||
switch (ch)
|
switch (ch)
|
||||||
{
|
{
|
||||||
case 'c':
|
case 'c':
|
||||||
@ -83,6 +88,13 @@ main(int argc, char **argv)
|
|||||||
case 'd':
|
case 'd':
|
||||||
del_tabs = TRUE;
|
del_tabs = TRUE;
|
||||||
break;
|
break;
|
||||||
|
case 'l':
|
||||||
|
protect_leading_whitespace = TRUE;
|
||||||
|
break;
|
||||||
|
case 'm':
|
||||||
|
/* only process text followed by periods in C comments */
|
||||||
|
only_comment_periods = TRUE;
|
||||||
|
break;
|
||||||
case 'q':
|
case 'q':
|
||||||
protect_quotes = TRUE;
|
protect_quotes = TRUE;
|
||||||
break;
|
break;
|
||||||
@ -97,6 +109,8 @@ main(int argc, char **argv)
|
|||||||
fprintf(stderr, "USAGE: %s [ -cdqst ] [file ...]\n\
|
fprintf(stderr, "USAGE: %s [ -cdqst ] [file ...]\n\
|
||||||
-c (clip trailing whitespace)\n\
|
-c (clip trailing whitespace)\n\
|
||||||
-d (delete tabs)\n\
|
-d (delete tabs)\n\
|
||||||
|
-l (protect leading whitespace)\n\
|
||||||
|
-m (only C comment periods)\n\
|
||||||
-q (protect quotes)\n\
|
-q (protect quotes)\n\
|
||||||
-s minimum_spaces\n\
|
-s minimum_spaces\n\
|
||||||
-t tab_width\n",
|
-t tab_width\n",
|
||||||
@ -134,13 +148,24 @@ main(int argc, char **argv)
|
|||||||
if (escaped == FALSE)
|
if (escaped == FALSE)
|
||||||
quote_char = ' ';
|
quote_char = ' ';
|
||||||
escaped = FALSE;
|
escaped = FALSE;
|
||||||
|
in_leading_whitespace = TRUE;
|
||||||
|
|
||||||
/* process line */
|
/* process line */
|
||||||
while (*src != NUL)
|
while (*src != NUL)
|
||||||
{
|
{
|
||||||
col_in_tab++;
|
col_in_tab++;
|
||||||
|
|
||||||
|
/* look backward so we handle slash-star-slash properly */
|
||||||
|
if (!in_comment && src > in_line &&
|
||||||
|
*(src - 1) == '/' && *src == '*')
|
||||||
|
in_comment = TRUE;
|
||||||
|
else if (in_comment && *src == '*' && *(src + 1) == '/')
|
||||||
|
in_comment = FALSE;
|
||||||
|
|
||||||
/* Is this a potential space/tab replacement? */
|
/* Is this a potential space/tab replacement? */
|
||||||
if (quote_char == ' ' && (*src == ' ' || *src == '\t'))
|
if ((!only_comment_periods || (in_comment && was_period)) &&
|
||||||
|
(!protect_leading_whitespace || !in_leading_whitespace) &&
|
||||||
|
quote_char == ' ' && (*src == ' ' || *src == '\t'))
|
||||||
{
|
{
|
||||||
if (*src == '\t')
|
if (*src == '\t')
|
||||||
{
|
{
|
||||||
@ -192,6 +217,11 @@ main(int argc, char **argv)
|
|||||||
/* Not a potential space/tab replacement */
|
/* Not a potential space/tab replacement */
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
/* allow leading stars in comments */
|
||||||
|
if (in_leading_whitespace && *src != ' ' && *src != '\t' &&
|
||||||
|
(!in_comment || *src != '*'))
|
||||||
|
in_leading_whitespace = FALSE;
|
||||||
|
was_period = (*src == '.');
|
||||||
/* output accumulated spaces */
|
/* output accumulated spaces */
|
||||||
output_accumulated_spaces(&prv_spaces, &dst);
|
output_accumulated_spaces(&prv_spaces, &dst);
|
||||||
/* This can only happen in a quote. */
|
/* This can only happen in a quote. */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user