From 1ebc4fe42ecf74da14bd1ce8a7931a0820cf36e3 Mon Sep 17 00:00:00 2001 From: Sanjay Bhatnagar Date: Thu, 25 Sep 2025 20:33:22 -0600 Subject: [PATCH 1/4] ErrorObj is derived from std::exception --- code/ErrorObj.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/ErrorObj.h b/code/ErrorObj.h index 9dc7003..76bbadb 100644 --- a/code/ErrorObj.h +++ b/code/ErrorObj.h @@ -21,10 +21,11 @@ #define ERROROBJ_H #include #include +#include using namespace std; -class ErrorObj{ +class ErrorObj: public std::exception{ public: enum {Informational=100,Recoverable,Severe,Fatal}; ErrorObj():Id(), Msg(), Src(), Message(),Level(0) From f7320c76af608327cdfea93a0715b9b6c7c8a812 Mon Sep 17 00:00:00 2001 From: Sanjay Bhatnagar Date: Sat, 27 Sep 2025 21:33:24 -0600 Subject: [PATCH 2/4] Emit clNumParsingError exception on error in parsing numerical values for keywords of type int, float. To be consistent, change the name of boolError to clBoolParsingError. --- code/SetVar.cc | 11 ++++++++++- code/boolError.h | 8 ++++---- code/calc/calc.y | 3 ++- code/calc/lex.l | 24 ++++++++++++------------ code/clError.h | 10 ++++++++++ code/clparseVal.cc | 7 +++++-- code/isBool.cc | 4 ++-- code/tstcpp.cc | 2 +- 8 files changed, 46 insertions(+), 23 deletions(-) diff --git a/code/SetVar.cc b/code/SetVar.cc index 2eaaf12..f0b577b 100644 --- a/code/SetVar.cc +++ b/code/SetVar.cc @@ -161,7 +161,7 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) if (clIsTrue(newval)==1) {Matched=1;} else if (clIsFalse(newval)==1) {Matched=1;} } - catch (boolError& x) + catch (clBoolParsingError& x) { throw(x); } @@ -171,6 +171,15 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) // os << b; newval=os.str(); } + else if (ISSET(S->Attributes, CL_INTEGERTYPE)) + { + double d; + if ((n=calc((char *)v,&d)) < 0) + { + std::string mesg("Error in parsing value of \""+std::string(S->Name)+"\""); + throw(clNumParsingError(mesg.c_str(),"VerifyVal",CL_SEVERE)); + } + } else if (n > 0) { Matched=0; diff --git a/code/boolError.h b/code/boolError.h index c914bd4..2d455a0 100644 --- a/code/boolError.h +++ b/code/boolError.h @@ -30,12 +30,12 @@ --------------------------------------------------------------------*/ #include -class boolError: public clError +class clBoolParsingError: public clError { public: - boolError(): clError() {}; - boolError(const char *m, const char *i, int l=0): clError(m,i,l) {}; - boolError(const boolError& that) + clBoolParsingError(): clError() {}; + clBoolParsingError(const char *m, const char *i, int l=0): clError(m,i,l) {}; + clBoolParsingError(const clBoolParsingError& that) {this->Msg = that.Msg; this->Id = that.Id; this->Level = that.Level;} }; #endif /* __cplusplus */ diff --git a/code/calc/calc.y b/code/calc/calc.y index 2a52c89..8d93a51 100644 --- a/code/calc/calc.y +++ b/code/calc/calc.y @@ -22,7 +22,7 @@ #include #include #include - +#include static double fooUserFunc(char* s) { return 0.0; } @@ -67,6 +67,7 @@ expr: NUMBER { $$ = $1; } $$ = (*($1->ufunc))($1->Name); if ($1->Name) free($1->Name); $1->Name = NULL; + return -2; } else { diff --git a/code/calc/lex.l b/code/calc/lex.l index 1dbb4e3..d9a94bd 100644 --- a/code/calc/lex.l +++ b/code/calc/lex.l @@ -31,8 +31,8 @@ int Calc_index; /*----------------------------------------------------------------------*/ int calc_error(char *s) { - /* ywarn(s, (char *) 0);*/ - return 1; + ywarn(s, (char *) 0); + return 1; } /*----------------------------------------------------------------------*/ int ywarn(char *s, char *t) @@ -83,7 +83,7 @@ int Calc_index=0; #define unput(C) {Calc_line[Calc_index==0?Calc_index:--Calc_index]=C;} #define output(C) {fprintf(stderr,"%c",C);} #define ECHON /* {fprintf(stderr,"\"\\n\"\n");}*/ -#define ECHO /*{fprintf(stderr,"\"%s\"\n",yytext);}*/ +#define ECHOSS(S) /* {fprintf(stderr,"%s: \"%s\"\n",S,yytext);}*/ /*yy_scan_buffer(Calc_line,strlen(Calc_line));*/ @@ -156,12 +156,13 @@ DANG ({DDEG}?({SPACE}?{DMIN})?({SPACE}?{DSEC})?) } {HEXD} { int i; - ECHO; + ECHOSS("HEXD"); sscanf(yytext+2,"%x",&i);calc_lval.val = i; INCR_INDEX; return NUMBER; } {HANG} {/* Numer in the hour angle format */ + ECHOSS("HANG"); float a[3]; split3(yytext,yyleng,"hms",a); calc_lval.val = (a[0]*60.0+a[1])*60.0+a[2]; @@ -170,6 +171,7 @@ DANG ({DDEG}?({SPACE}?{DMIN})?({SPACE}?{DSEC})?) return NUMBER; } {DANG} {/* Number in the angular format */ + ECHOSS("DANG"); float a[3]; split3(yytext,yyleng,"d\'\"",a); calc_lval.val = (a[0]*60.0+a[1])*60.0+a[2]; @@ -178,36 +180,37 @@ DANG ({DDEG}?({SPACE}?{DMIN})?({SPACE}?{DSEC})?) return NUMBER; } {CREAL} { - ECHO; + ECHOSS("CREAL"); sscanf(yytext,"%lf",&calc_lval.val); INCR_INDEX; return NUMBER; } {FREAL} { float m,e; char c; - ECHO; + ECHOSS("FREAL"); sscanf(yytext, "%f%c%f", &m,&c,&e); calc_lval.val = m*pow(10.0,e); INCR_INDEX; return NUMBER; } atan2 { Calc_Symbol *s; /* A patch for atan2...*/ - ECHO; + ECHOSS("atan2"); INCR_INDEX; if ((s=calcget(yytext))!=0) { calc_lval.symb=s; return s->type;} } [a-zA-Z]+[a-zA-Z0-9]* { Calc_Symbol *s; - ECHO; INCR_INDEX; if ((s=calcget(yytext))!=0) { + ECHOSS("A-Z"+s->type); calc_lval.symb=s; return s->type; } else { + ECHOSS("A-Z UserFunc"); s2.ufunc = UserFunc; s2.Name = (char *)malloc(strlen(yytext)+1); strcpy(s2.Name,yytext); @@ -216,7 +219,4 @@ atan2 { Calc_Symbol *s; /* A patch for atan2...*/ } } \n {ECHON;return '\n';} -. {ECHO;return yytext[0];} - - - +. {ECHOSS(".");return yytext[0];} diff --git a/code/clError.h b/code/clError.h index 36daa56..8c2bbec 100644 --- a/code/clError.h +++ b/code/clError.h @@ -44,8 +44,18 @@ class clError: public ErrorObj this->Id = that.Id; this->Level = that.Level; } + ~clError() {}; }; +class clNumParsingError: public clError +{ +public: + clNumParsingError():clError() {}; + clNumParsingError(const char *m, const char *i, int l=0): clError(m,i,l) {}; + clNumParsingError(const string& m, const string& i, int l=0): clError(m,i,l) {}; + clNumParsingError(const clNumParsingError& that) : clError(that) {}; + ~clNumParsingError() {}; +}; /*--------------------------------------------------------------------- In the entire library, the following routine is used always when an exception is to be thrown. diff --git a/code/clparseVal.cc b/code/clparseVal.cc index b03b81c..8ba8562 100644 --- a/code/clparseVal.cc +++ b/code/clparseVal.cc @@ -77,8 +77,11 @@ HANDLE_EXCEPTIONS( if (retVal == CL_UNKNOWNBOOL ) reportParseError(*S, N-1); } - else if ((n=calc((char *)S->Val[N-1].c_str(),d))) - reportParseError(*S, N-1); + else + { + n=calc((char *)S->Val[N-1].c_str(),d); + if (n < 0) reportParseError(*S, N-1); + } return 1; } else return CL_FAIL; diff --git a/code/isBool.cc b/code/isBool.cc index 026a342..aebd1bb 100644 --- a/code/isBool.cc +++ b/code/isBool.cc @@ -78,7 +78,7 @@ int clIsTrue(const string& val) int retVal=clBoolCmp(val, true); if (retVal == CL_UNKNOWNBOOL) { - boolError bl("Unrecognized value for Bool", "###Error", CL_INFORMATIONAL); + clBoolParsingError bl("Unrecognized value for Bool", "###Error", CL_INFORMATIONAL); throw(bl); } return retVal; @@ -88,7 +88,7 @@ int clIsFalse(const string& val) int retVal=clBoolCmp(val, false); if (retVal == CL_UNKNOWNBOOL) { - boolError bl("Unrecognized value for Bool", "###Error", CL_INFORMATIONAL); + clBoolParsingError bl("Unrecognized value for Bool", "###Error", CL_INFORMATIONAL); throw(bl); } return retVal; diff --git a/code/tstcpp.cc b/code/tstcpp.cc index 49a9d62..688b1d8 100644 --- a/code/tstcpp.cc +++ b/code/tstcpp.cc @@ -114,7 +114,7 @@ void UI(bool restart, int argc, char **argv) } EndCL(); } - catch (clError x) + catch (clError& x) { x << x << endl; clRetry(); From b3a4348b2d5baf74a47739f6acf41b3d1507fd06 Mon Sep 17 00:00:00 2001 From: Sanjay Bhatnagar Date: Mon, 29 Sep 2025 11:31:42 -0600 Subject: [PATCH 3/4] Intercept number parsing error from the parser and throw a C++ exception. CMakeLists.txt: Complied Bison and flex files with C++ SetVar.cc: VerifyVal() now intercepts number parsing errors also. clError.h: Moved clBoolParsingError type here, and removed boolError.h callback.cc, clParseVal.cc, isBool.cc: Removed inclusion of boolError.h --- code/CMakeLists.txt | 6 ++++-- code/SetVar.cc | 8 +++----- code/boolError.h | 43 ------------------------------------------- code/callbacks.cc | 2 +- code/cl.h | 2 +- code/clError.h | 15 ++++++++++++++- code/clparseVal.cc | 21 +++++++++++++++------ code/isBool.cc | 2 +- 8 files changed, 39 insertions(+), 60 deletions(-) delete mode 100644 code/boolError.h diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index b709acf..f4a62a0 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -49,7 +49,6 @@ target_include_directories(parafeed PUBLIC $) # install target set(header_list - boolError.h clbool.h clconvert.h clError.h @@ -96,7 +95,7 @@ bison_target(ShellParser ${CMAKE_CURRENT_BINARY_DIR}/shell.tab.cc COMPILE_FLAGS "-p sh_ -o shell.tab.cc" DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/shell.tab.h) -# set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/shell.tab.c PROPERTY LANGUAGE CXX)) + set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/shell.tab.c PROPERTY LANGUAGE CXX) flex_target(ShellScanner shell.l ${CMAKE_CURRENT_BINARY_DIR}/shell.cc @@ -112,10 +111,12 @@ bison_target(CalcParser ${CMAKE_CURRENT_BINARY_DIR}/y.tab.c COMPILE_FLAGS "-p calc_" DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/y.tab.h) + set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/shell.tab.c PROPERTY LANGUAGE CXX) flex_target(CalcScanner calc/lex.l ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c COMPILE_FLAGS "-Pcalc_") + #set_property(SOURCE ${CMAKE_CURRENT_BINARY_DIR}/lex.yy.c PROPERTY LANGUAGE CXX) add_flex_bison_dependency(CalcScanner CalcParser) target_sources(parafeed PRIVATE ${BISON_CalcParser_OUTPUTS} ${FLEX_CalcScanner_OUTPUTS}) @@ -148,6 +149,7 @@ set(sh_sources yyerror.cc minmatch.cc isBool.cc) + foreach(source IN LISTS sh_sources) set_property(SOURCE ${source} PROPERTY LANGUAGE CXX) endforeach() diff --git a/code/SetVar.cc b/code/SetVar.cc index f0b577b..19123bc 100644 --- a/code/SetVar.cc +++ b/code/SetVar.cc @@ -23,7 +23,7 @@ #include #include #include -#include +#include #ifdef __cplusplus #include #include @@ -154,7 +154,6 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) } else { - // Matched=(clIsTrue(newval) || clIsFalse(newval)); Matched=0; try { @@ -168,7 +167,6 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) os << newval; } - // os << b; newval=os.str(); } else if (ISSET(S->Attributes, CL_INTEGERTYPE)) @@ -177,7 +175,7 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) if ((n=calc((char *)v,&d)) < 0) { std::string mesg("Error in parsing value of \""+std::string(S->Name)+"\""); - throw(clNumParsingError(mesg.c_str(),"VerifyVal",CL_SEVERE)); + throw(clNumParsingError(mesg.c_str(),"###Error",CL_SEVERE)); } } else if (n > 0) @@ -208,7 +206,7 @@ void SetVal(const char *v, Symbol *S, int i) { string vv; string trimmed=trim(string(v)); - // stripwhite(v); + try { VerifyVal(trimmed.c_str(),S,vv); diff --git a/code/boolError.h b/code/boolError.h deleted file mode 100644 index 2d455a0..0000000 --- a/code/boolError.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2000-2012, 2013 S. Bhatnagar (bhatnagar dot sanjay at gmail dot com) - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - * - */ -//-*-C++-*- -// -// $Id: clError.h,v 2.1 1999/09/15 12:00:51 sanjay Exp sanjay $ -// -#if !defined(BOOLERROROBJ_H) -#define BOOLERROROBJ_H -#ifdef __cplusplus - -/*-------------------------------------------------------------------- - The C++ object thrown in case of error. It's derived from ErrorObj - class, which is the baseclass for all error objects. ---------------------------------------------------------------------*/ - -#include -class clBoolParsingError: public clError -{ -public: - clBoolParsingError(): clError() {}; - clBoolParsingError(const char *m, const char *i, int l=0): clError(m,i,l) {}; - clBoolParsingError(const clBoolParsingError& that) - {this->Msg = that.Msg; this->Id = that.Id; this->Level = that.Level;} -}; -#endif /* __cplusplus */ - -#endif /* CLERROR_H */ diff --git a/code/callbacks.cc b/code/callbacks.cc index 0888951..a9012bf 100644 --- a/code/callbacks.cc +++ b/code/callbacks.cc @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include //#include diff --git a/code/cl.h b/code/cl.h index f99aa54..3447ae4 100644 --- a/code/cl.h +++ b/code/cl.h @@ -230,7 +230,7 @@ int dowarranty(char *); // #endif //int clparseVal(Symbol *, int *, double *); #ifdef __cplusplus -void reportParseError(const Symbol& S, const int& N); +void reportParseError(clError& excp,const Symbol& S, const int& N); int PrintVals(FILE *f,Symbol *S,unsigned int newline=1); #endif int PrintKey(FILE *f, Symbol *S); diff --git a/code/clError.h b/code/clError.h index 8c2bbec..2a7a174 100644 --- a/code/clError.h +++ b/code/clError.h @@ -43,10 +43,23 @@ class clError: public ErrorObj this->Msg = that.Msg; this->Id = that.Id; this->Level = that.Level; - } + }; + void set(const string& m, const string& i, int l=0) + { + Msg=m; Id=i; Level=l; + }; ~clError() {}; }; +class clBoolParsingError: public clError +{ +public: + clBoolParsingError(): clError() {}; + clBoolParsingError(const char *m, const char *i, int l=0): clError(m,i,l) {}; + clBoolParsingError(const clBoolParsingError& that) + {this->Msg = that.Msg; this->Id = that.Id; this->Level = that.Level;} +}; + class clNumParsingError: public clError { public: diff --git a/code/clparseVal.cc b/code/clparseVal.cc index 8ba8562..1643684 100644 --- a/code/clparseVal.cc +++ b/code/clparseVal.cc @@ -24,6 +24,7 @@ #include #include #include +#include #ifdef __cplusplus extern "C" { #endif @@ -31,11 +32,12 @@ extern "C" { Convert the n th value of symbol S into a double ------------------------------------------------------------------------*/ -void reportParseError(const Symbol& S, const int& N) + void reportParseError(clError& excp, const Symbol& S, const int& N) { ostringstream os; os << "In conversion of " << S.Name << "[" << N << "]=" << S.Val[N]; - clThrowUp(os.str().c_str(), "###Error", CL_FAIL); + excp.set(os.str().c_str(), "###Error", CL_FAIL); + throw(excp); } #ifdef __cplusplus } @@ -62,7 +64,7 @@ int clparseVal(Symbol *S, int *Which, string& val) int clparseVal(Symbol *S, int *Which, double *d) { - unsigned int N = _ABS(*Which),n; + unsigned int N = _ABS(*Which); HANDLE_EXCEPTIONS( if (S != NULL) { @@ -75,12 +77,19 @@ HANDLE_EXCEPTIONS( else if ((retVal=clIsFalse(val))==1) *d=0; //*d = clIsTrue(val); if (retVal == CL_UNKNOWNBOOL ) - reportParseError(*S, N-1); + { + clBoolParsingError clExcp; + reportParseError(clExcp, *S, N-1); + } } else { - n=calc((char *)S->Val[N-1].c_str(),d); - if (n < 0) reportParseError(*S, N-1); + int n=calc((char *)S->Val[N-1].c_str(),d); + if (n < 0) + { + clNumParsingError clExcp; + reportParseError(clExcp,*S, N-1); + } } return 1; } diff --git a/code/isBool.cc b/code/isBool.cc index aebd1bb..2abba05 100644 --- a/code/isBool.cc +++ b/code/isBool.cc @@ -25,7 +25,7 @@ #include #include #include -#include +#include //#include #ifdef __cplusplus From 123ae88719ce135709730e71726307909d0f47c9 Mon Sep 17 00:00:00 2001 From: Sanjay Bhatnagar Date: Mon, 29 Sep 2025 20:36:21 -0600 Subject: [PATCH 4/4] Parsing error reportings for CL_FLOATTYPE was missed. Now added. --- code/SetVar.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/code/SetVar.cc b/code/SetVar.cc index 19123bc..79302e2 100644 --- a/code/SetVar.cc +++ b/code/SetVar.cc @@ -169,7 +169,8 @@ int UnsetVar(Symbol *S, int setFactoryDefaults) } newval=os.str(); } - else if (ISSET(S->Attributes, CL_INTEGERTYPE)) + else if (ISSET(S->Attributes, CL_INTEGERTYPE)|| + ISSET(S->Attributes, CL_FLOATTYPE)) { double d; if ((n=calc((char *)v,&d)) < 0)