Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions directive.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ void directive( void)
ret:
in_directive = FALSE;
keep_comments = option_flags.c && compiling && !no_output;
keep_spaces = option_flags.k && compiling;
/* keep_spaces is on for #define line even if no_output is TRUE */
if (! wrong_line)
newlines++;
Expand Down
2 changes: 2 additions & 0 deletions internal.H
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ extern struct std_limits_ {
/* The boolean flags specified by the execution options. */
extern struct option_flags_ {
int c; /* -C option (keep comments) */
int k; /* -k option (keep white spaces)*/
} option_flags;

extern int stdc_val; /* Value of __STDC__ */
Expand All @@ -335,6 +336,7 @@ extern long src_line; /* Current source line number */
extern int wrong_line; /* Force #line to compiler */
extern int newlines; /* Count of blank lines */
extern int keep_comments; /* Don't remove comments */
extern int keep_spaces; /* Don't remove white spaces */
extern int include_nest; /* Nesting level of #include */
extern const char * null; /* "" string for convenience */
extern const char ** inc_dirp; /* Directory of #includer */
Expand Down
4 changes: 3 additions & 1 deletion mcpp_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
/* The boolean flags specified by the execution options. */
struct option_flags_ option_flags = {
FALSE, /* c: -C (keep comments) */
FALSE, /* k: -k (keep white spaces) */
};

int mcpp_mode = STD; /* Mode of preprocessing */
Expand Down Expand Up @@ -263,7 +264,7 @@ static void init_main( void)
std_limits.str_len = NBUFF;
std_limits.id_len = IDMAX;
std_limits.n_mac_pars = NMACPARS;
option_flags.c = FALSE;
option_flags.c = option_flags.k = FALSE;
sh_file = NULL;
sh_line = 0;
}
Expand Down Expand Up @@ -455,6 +456,7 @@ static void mcpp_main( void)
LINE_COL line_col; /* Location of macro call in source */

keep_comments = option_flags.c && !no_output;
keep_spaces = option_flags.k; /* Will be turned off if !compiling */
Comment thread
externl marked this conversation as resolved.
Resolved
line_col.col = line_col.line = 0L;

/*
Expand Down
30 changes: 29 additions & 1 deletion support.c
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,21 @@ static char * parse_line( void)
free( temp); /* End of file with un- */
return NULL; /* terminated comment */
}
if (keep_spaces) {
/*
* Truncate a comment too long for the rest of the buffer.
* The buffer may already be full: an earlier comment of
* this line was truncated to end at 'limit - 1', and a
* token after it moved 'tp' to 'limit'. 'limit - tp - 1'
* is then negative, and would become a huge 'com_size',
* so the comment gets no padding instead.
*/
if (tp + com_size >= limit - 1) /* Too long comment */
com_size = (tp < limit - 1) ? limit - tp - 1 : 0;
while (com_size--)
*tp++ = ' '; /* Spaces of the comment length */
break;
}
if (temp == tp ||
! (char_type[ *(tp - 1) & UCHARMAX] & HSP))
*tp++ = ' '; /* Squeeze white spaces */
Expand Down Expand Up @@ -1141,7 +1156,12 @@ static char * parse_line( void)
, NULL, (long) c, NULL);
case '\t': /* Horizontal space */
case ' ':
if (! (char_type[ *(tp - 1) & UCHARMAX] & HSP)) {
if (keep_spaces) {
if (c == '\t')
*tp++ = '\t';
else
*tp++ = ' '; /* Convert to ' ' */
} else if (! (char_type[ *(tp - 1) & UCHARMAX] & HSP)) {
*tp++ = ' '; /* Squeeze white spaces */
}
break;
Expand Down Expand Up @@ -1206,6 +1226,10 @@ static char * read_a_comment(
char * saved_sp;
int cat_line = 0; /* Number of catenated lines */

if (keep_spaces) {
saved_sp = sp - 2; /* '-2' for beginning / and * */
*sizp = 0;
}
if (keep_comments) /* If writing comments */
mcpp_fputs( "/*", OUT); /* Write the initializer*/
c = *sp++;
Expand All @@ -1232,6 +1256,8 @@ static char * read_a_comment(
/* trouble on some other tools such as rpcgen. */
wrong_line = TRUE;
}
if (keep_spaces) /* Save the length */
*sizp = *sizp + (sp - saved_sp);
if ((mcpp_debug & MACRO_CALL) && compiling) {
if (cat_line) {
cat_line++;
Expand All @@ -1244,6 +1270,8 @@ static char * read_a_comment(
}
return sp; /* End of comment */
case '\n': /* Line-crossing comment*/
if (keep_spaces) /* Save the length */
*sizp = *sizp + (sp - saved_sp) - 1; /* '-1' for '\n' */
Comment thread
externl marked this conversation as resolved.
Resolved
if ((mcpp_debug & MACRO_CALL) && compiling) {
/* Save location informations */
if (cat_line == 0) /* First line of catenation */
Expand Down
7 changes: 6 additions & 1 deletion system.c
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,9 @@ opt_search: ;
}
break;

case 'k': /* Keep white spaces */
option_flags.k = TRUE;
break;

case 'M': /* Output source file dependency line */
if (argv[ mcpp_optind - 1] == mcpp_optarg) { /* -M */
Expand Down Expand Up @@ -488,6 +491,8 @@ static void usage(

"-I- Unset system or site specific include directories.\n",

"-k Keep white spaces of input lines as they are.\n",

"-M file\n",
" Output source file dependency line for makefile.\n",
"-U <macro> Undefine <macro>.\n",
Expand Down Expand Up @@ -532,7 +537,7 @@ static void set_opt_list(

const char * const * lp = & list[ 0];

strcpy( optlist, "e:CD:I:M:U:");
strcpy( optlist, "e:kCD:I:M:U:");
/* Default options */
while (*lp)
strcat( optlist, *lp++);
Expand Down
Loading