Skip to content

Commit

Permalink
Fix a bug of EXEC SQL INCLUDE (opensourcecobol#23, opensourcecobol#24,
Browse files Browse the repository at this point in the history
  • Loading branch information
yutaro-sakamoto committed Aug 16, 2022
1 parent 0271e01 commit 9d1d258
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 146 deletions.
7 changes: 7 additions & 0 deletions ocesql/ocesql.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ enum oc_usage{
#define INC_START_MARK ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"
#define INC__END__MARK "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"


#define PERIOD_DEFAULT 0
#define PERIOD_FORCE_ON 1
#define PERIOD_FORCE_OFF 2

struct filename {
char *source;
char *translate;
Expand Down Expand Up @@ -205,6 +210,8 @@ extern char *errorfilename;
extern int flag_external;
extern char *filenameID;

extern int config_period;

extern struct cb_sql_list *
cb_add_text_list (struct cb_sql_list *list, struct cb_sql_list *targetlist);
extern struct cb_sql_list *
Expand Down
6 changes: 5 additions & 1 deletion ocesql/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -2350,7 +2350,11 @@ put_exec_list()

l->startLine = startlineno;
l->endLine = endlineno;
l->period = period;
if(config_period == PERIOD_FORCE_ON || (config_period == PERIOD_DEFAULT && period != 0)) {
l->period = 1;
} else {
l->period = 0;
}
l->host_list = host_reference_list;
l->hostreferenceCount =hostreferenceCount;
l->res_host_list = res_host_reference_list;
Expand Down
6 changes: 5 additions & 1 deletion ocesql/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,11 @@ put_exec_list()

l->startLine = startlineno;
l->endLine = endlineno;
l->period = period;
if(config_period == PERIOD_FORCE_ON || (config_period == PERIOD_DEFAULT && period != 0)) {
l->period = 1;
} else {
l->period = 0;
}
l->host_list = host_reference_list;
l->hostreferenceCount =hostreferenceCount;
l->res_host_list = res_host_reference_list;
Expand Down
51 changes: 25 additions & 26 deletions ocesql/ppout.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,11 @@ void ppoutputendcall(struct cb_exec_list *list){
return ;

memset(buff, 0, sizeof(buff));
if( list->period)
if(config_period == PERIOD_FORCE_ON || (config_period == PERIOD_DEFAULT && list->period)) {
com_sprintf(buff,sizeof(buff), "OCESQL%5sEND-CALL.\n", " ");
else
} else {
com_sprintf(buff,sizeof(buff), "OCESQL%5sEND-CALL\n", " ");
}
fputs(buff, outfile);
return ;

Expand Down Expand Up @@ -1880,37 +1881,30 @@ void ppbuff_incfile(struct cb_exec_list *list){

incf = fopen_or_die(filename, "r");

memset(incmsg, 0, 256);
sprintf(incmsg, "%s incfile start:%s", INC_START_MARK, filename);
com_strcpy(out,sizeof(out),incmsg);
outwrite();

while(1){
memset(incf_buff, 0, BUFFSIZE + 1);
fgets(incf_buff, BUFFSIZE, incf);
if(feof(incf)) break;
char* result = fgets(incf_buff, BUFFSIZE, incf);
if(result == NULL) break;

if(strlen(incf_buff) > MAX_LINESIZE){
memset(buff, 0, sizeof(buff));
com_sprintf(buff,sizeof(buff), "E%03d",ERR_EXCEED_LIMIT_LINE_LENGTH);
printerrormsg("", lineNUM, buff);
}

int len = strlen(incf_buff);
if(len > 0 && incf_buff[len - 1] == '\n') {
if(len > 1 && incf_buff[len - 2] == '\r') {
incf_buff[len - 2] = '\0';
} else {
incf_buff[len - 1] = '\0';
}
}
com_strcpy(out,sizeof(out),"OCESQL");
com_strcat(out,sizeof(out), incf_buff + strlen("OCESQL"));
retcode = strlen(incf_buff);
len2 = strlen("OCESQL");
if(retcode > len2){
out[retcode-1] = '\0';
}
outwrite();
}

memset(incmsg, 0, 256);
sprintf(incmsg, "%s incfile end:%s",INC__END__MARK , filename);
com_strcpy(out,sizeof(out),incmsg);
outwrite();

return;
}
return;
Expand Down Expand Up @@ -1939,8 +1933,11 @@ void ppoutput(char *ppin,char *ppout,struct cb_exec_list *head){

EOFFLG = 0;
if (readfile && outfile){
for(;EOFflg != 1;){
while(1){
com_readline(readfile, inbuff, &lineNUM, &EOFflg);
if(EOFflg) {
break;
}
if(strstr(inbuff, INC_START_MARK) != NULL ||
strstr(inbuff, INC__END__MARK) != NULL){
continue;
Expand Down Expand Up @@ -2026,8 +2023,13 @@ void ppoutput_incfile(char *ppin,char *ppout,struct cb_exec_list *head){

EOFFLG = 0;
if (readfile && outfile){
for(;EOFflg != 1;){
int after_first_read = 0;
while(EOFflg != 1){
if(after_first_read) {
fwrite (outbuff ,len, 1 , outfile);
}
com_readline(readfile, inbuff, &lineNUM, &EOFflg);
after_first_read = 1;
if(head){
if (l->startLine<= lineNUM && l->endLine>=lineNUM){
if (strcmp(l->commandName, "INCFILE") == 0){
Expand All @@ -2042,7 +2044,6 @@ void ppoutput_incfile(char *ppin,char *ppout,struct cb_exec_list *head){

outbuff = inbuff;
len = strlen(outbuff);
fwrite (outbuff ,len, 1 , outfile );

if (EOFflg == 1){
fputc('\n',outfile);
Expand All @@ -2051,7 +2052,9 @@ void ppoutput_incfile(char *ppin,char *ppout,struct cb_exec_list *head){
else{
if(lineNUM - l->endLine == 1){
if(strcmp(l->commandName,"INCFILE")==0){
fprintf(outfile, "ocesql* start_include period=%s\n", l->period ? "true" : "false");
ppbuff_incfile(l);
fprintf(outfile, "ocesql* end_include\n");
}
if (l->next != NULL)
l = l->next;
Expand All @@ -2068,18 +2071,14 @@ void ppoutput_incfile(char *ppin,char *ppout,struct cb_exec_list *head){
}
outbuff = inbuff;
len = strlen(outbuff);
fwrite (outbuff ,len, 1 , outfile );
}else{
outbuff = inbuff;
len = strlen(outbuff);
fwrite (outbuff ,len, 1 , outfile );

}
}
}else{
outbuff = inbuff;
len = strlen(outbuff);
fwrite (outbuff ,len, 1 , outfile);
}
}
}
Expand Down
Loading

0 comments on commit 9d1d258

Please sign in to comment.