Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix COPY REPLACING and REPLACE #75

Closed
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
12 changes: 11 additions & 1 deletion NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ NEWS - user visible changes -*- outline -*-
callee modules that use BY VALUE must be compiled with the same version of
GnuCOBOL, either prior this release, or since.

** cobc now uses a two-pass preprocessing algorithm, where replacements for
COPY-REPLACING are done in a first pass, and the replacements for
REPLACE are done in a second pass. Note that, however, both
statements are parsed before the first replacement pass, so
COPY-REPLACING cannot impact a REPLACE statement itself.

* Changes to the COBOL compiler (cobc) options:

** new -fformat dialect option, and extended SOURCE FORMAT directives,
Expand Down Expand Up @@ -321,7 +327,9 @@ NEWS - user visible changes -*- outline -*-
and fatality `cobc --list-exceptions`

** new compiler command line option -ftcmd to enable printing of the command
line in the source listing
line in the source listing, -fno-timestamp to suppress printing of the time
and -ftittle to set a title instead of GnuCOBOL and version (_ chars are
replaced by spaces in the title)

** new compiler command line option --coverage to instrument binaries
for coverage checks
Expand Down Expand Up @@ -349,6 +357,8 @@ NEWS - user visible changes -*- outline -*-
the option -fdiagnostics-plain-output was added to request that diagnostic
output look as plain as possible and stay more stable over time

** the -P flag accepts - as argument for stdout

* Important Bugfixes:

** for dialects other than the GnuCOBOL default different reserved "alias" words
Expand Down
19 changes: 19 additions & 0 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@

2023-07-05 Fabrice Le Fessant <[email protected]>

* replace.c: rewrite the code for preprocessing with a two-phase
algorithm. The first phase performs COPY REPLACING on the stream
of tokens, while the second phase perform REPLACE on the resulting
stream of tokens. This rewriting is closer to the COBOL standard
and fixes bug #831 partially.
* cobc.c: flag -P now accepts - as argument to mean stdout

2023-07-02 Fabrice Le Fessant <[email protected]>

* pplex.l/replace.c: move the preprocessing code performing
COPY REPLACING and REPLACE from pplex.l to replace.c

2023-07-02 Fabrice Le Fessant <[email protected]>

* pplex.l (ppecho, ppecho_direct): replace alt_space by passing a
second equivalent token

2023-07-05 Fabrice Le Fessant <[email protected]>

* flag.def/cobc.c: new flags -fno-ttimestamp to suppress timestamp
Expand Down
3 changes: 2 additions & 1 deletion cobc/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
bin_PROGRAMS = cobc
cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.c parser.y scanner.c config.c \
reserved.c error.c tree.c tree.h field.c typeck.c codegen.c help.c \
config.def flag.def warning.def codeoptim.def ppparse.def codeoptim.c
config.def flag.def warning.def codeoptim.def ppparse.def \
codeoptim.c replace.c

#cobc_SOURCES = cobc.c cobc.h ppparse.y pplex.l parser.y scanner.l config.c

Expand Down
54 changes: 52 additions & 2 deletions cobc/cobc.c
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ static const char *const cob_csyns[] = {

#define COB_NUM_CSYNS sizeof(cob_csyns) / sizeof(cob_csyns[0])

static const char short_options[] = "hVivqECScbmxjdFOPgwo:t:T:I:L:l:D:K:k:";
static const char short_options[] = "hVivqECScbmxjdFOgwo:P:t:T:I:L:l:D:K:k:";

#define CB_NO_ARG no_argument
#define CB_RQ_ARG required_argument
Expand Down Expand Up @@ -1337,6 +1337,50 @@ cobc_plex_strdup (const char *dupstr)
return p;
}

/* Return a newly allocated zero-terminated string with only the first
* len chars of the first argument */
void *
cobc_plex_strsub (const char *s, const int len)
{
void *p;
int n;

n = strlen (s);

#ifdef COB_TREE_DEBUG
/* LCOV_EXCL_START */
if ( len>n ) {
cobc_err_msg ("call to %s with bad argument len=%d>%d=strlen(s)",
"cobc_plex_strsub", len, n);
cobc_abort_terminate (1);
}
/* LCOV_EXCL_STOP */
#endif

p = cobc_plex_malloc (len + 1);
memcpy (p, s, len);
return p;
}

/* Returns a newly allocated zero-terminated string containing the
* concatenation of str1 and str2. str1 and str2 may be freed
* afterwards.
*/
char *
lefessan marked this conversation as resolved.
Show resolved Hide resolved
cobc_plex_stradd (const char *str1, const char *str2)
{
char *p;
size_t m, n;

m = strlen (str1);
n = strlen (str2);
p = cobc_plex_malloc (m + n + 1);
memcpy (p, str1, m);
memcpy (p + m, str2, n);
return p;
}


/* variant of strcpy which copies max 'max_size' bytes from 'src' to 'dest',
if the size of 'src' is too long only its last/last bytes are copied and an
eliding "..." is placed in front or at end depending on 'elide_at_end' */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we move out all those memory-only related functions to a separate file?

Expand Down Expand Up @@ -9140,6 +9184,9 @@ main (int argc, char **argv)
memset (cb_listing_header, 0, sizeof (cb_listing_header));
/* If -P=file specified, all lists go to this file */
if (cobc_list_file) {
if (strcmp (cobc_list_file, COB_DASH) == 0) {
cb_listing_file = stdout;
} else
if (cb_unix_lf) {
cb_listing_file = fopen (cobc_list_file, "wb");
} else {
Expand Down Expand Up @@ -9225,7 +9272,10 @@ main (int argc, char **argv)
}

if (cobc_list_file) {
fclose (cb_listing_file);
if (cb_listing_file != stdout)
fclose (cb_listing_file);
else
fflush (stdout);
cb_listing_file = NULL;
}

Expand Down
2 changes: 2 additions & 0 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,8 @@ extern void cobc_parse_free (void *);

extern void *cobc_plex_malloc (const size_t);
extern void *cobc_plex_strdup (const char *);
extern void *cobc_plex_strsub (const char *, const int len);
extern char *cobc_plex_stradd (const char *str1, const char *str2);

extern void *cobc_check_string (const char *);
extern void cobc_err_msg (const char *, ...) COB_A_FORMAT12;
Expand Down
Loading