From e51d7f3c6d44ad0ac1e0acd9a2054f6e8a04542a Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Thu, 20 Jan 2022 19:30:10 -0500 Subject: [PATCH] Add "DO file" command which is equivalent to "INCLUDE [filedir()]/file" --- src/calendar.c | 5 ++++- src/files.c | 36 +++++++++++++++++++++++++++++++-- src/main.c | 3 ++- src/protos.h | 2 +- src/token.c | 1 + src/types.h | 2 +- tests/include_dir/02.rem | 2 ++ tests/include_dir/subdir/04.rem | 1 + 8 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 tests/include_dir/subdir/04.rem diff --git a/src/calendar.c b/src/calendar.c index 78eb3406..a7284a4a 100644 --- a/src/calendar.c +++ b/src/calendar.c @@ -1238,7 +1238,10 @@ static void GenerateCalEntries(int col) case T_IfTrig: r=DoIfTrig(&p); break; case T_Else: r=DoElse(&p); break; case T_EndIf: r=DoEndif(&p); break; - case T_Include: r=DoInclude(&p); break; + + case T_Include: + case T_IncludeR: r=DoInclude(&p, tok.type); break; + case T_IncludeCmd: r=DoIncludeCmd(&p); break; case T_Exit: DoExit(&p); break; case T_Set: r=DoSet(&p); break; diff --git a/src/files.c b/src/files.c index 2d890096..7c78d884 100644 --- a/src/files.c +++ b/src/files.c @@ -557,20 +557,52 @@ static int PopFile(void) /* The INCLUDE command. */ /* */ /***************************************************************/ -int DoInclude(ParsePtr p) +int DoInclude(ParsePtr p, enum TokTypes tok) { DynamicBuffer buf; + DynamicBuffer fullname; + DynamicBuffer path; int r, e; + char const *s; DBufInit(&buf); + DBufInit(&fullname); + DBufInit(&path); if ( (r=ParseToken(p, &buf)) ) return r; e = VerifyEoln(p); if (e) Eprint("%s", ErrMsg[e]); - if ( (r=IncludeFile(DBufValue(&buf))) ) { + + if (tok == T_IncludeR) { + /* Relative include: Include relative to dir + containing current file */ + if (DBufPuts(&path, FileName) != OK) return E_NO_MEM; + if (DBufLen(&path) == 0) { + s = DBufValue(&buf); + } else { + char *t = DBufValue(&path) + DBufLen(&path) - 1; + while (t > DBufValue(&path) && *t != '/') t--; + if (*t == '/') { + *t = 0; + if (DBufPuts(&fullname, DBufValue(&path)) != OK) return E_NO_MEM; + if (DBufPuts(&fullname, "/") != OK) return E_NO_MEM; + if (DBufPuts(&fullname, DBufValue(&buf)) != OK) return E_NO_MEM; + s = DBufValue(&fullname); + } else { + s = DBufValue(&buf); + } + } + } else { + s = DBufValue(&buf); + } + if ( (r=IncludeFile(s)) ) { DBufFree(&buf); + DBufFree(&path); + DBufFree(&fullname); return r; } DBufFree(&buf); + DBufFree(&path); + DBufFree(&fullname); NumIfs = 0; IfFlags = 0; return OK; diff --git a/src/main.c b/src/main.c index 453acf45..728a9e09 100644 --- a/src/main.c +++ b/src/main.c @@ -235,12 +235,13 @@ static void DoReminders(void) case T_Else: r=DoElse(&p); break; case T_EndIf: r=DoEndif(&p); break; case T_Include: + case T_IncludeR: /* In purge mode, include closes file, so we need to echo it here! */ if (PurgeMode) { PurgeEchoLine("%s\n", CurLine); } - r=DoInclude(&p); + r=DoInclude(&p, tok.type); purge_handled = 1; break; case T_IncludeCmd: diff --git a/src/protos.h b/src/protos.h index 67aab259..54ba3bc9 100644 --- a/src/protos.h +++ b/src/protos.h @@ -44,7 +44,7 @@ void PrintValue (Value *v, FILE *fp); int CopyValue (Value *dest, const Value *src); int ReadLine (void); int OpenFile (char const *fname); -int DoInclude (ParsePtr p); +int DoInclude (ParsePtr p, enum TokTypes tok); int DoIncludeCmd (ParsePtr p); int IncludeFile (char const *fname); int GetAccessDate (char const *file); diff --git a/src/token.c b/src/token.c index e93311ce..1ab0d2c1 100644 --- a/src/token.c +++ b/src/token.c @@ -48,6 +48,7 @@ Token TokArray[] = { { "clear-omit-context", 5, T_Clr, 0 }, { "debug", 5, T_Debug, 0 }, { "december", 3, T_Month, 11 }, + { "do", 2, T_IncludeR, 0 }, { "dumpvars", 4, T_Dumpvars, 0 }, { "duration", 3, T_Duration, 0 }, { "else", 4, T_Else, 0 }, diff --git a/src/types.h b/src/types.h index 20fd639b..90b904c4 100644 --- a/src/types.h +++ b/src/types.h @@ -151,7 +151,7 @@ typedef Parser *ParsePtr; /* Pointer to parser structure */ enum TokTypes { T_Illegal, /* Commands first */ - T_Rem, T_Push, T_Pop, T_Preserve, T_Include, T_IncludeCmd, T_If, T_Else, T_EndIf, + T_Rem, T_Push, T_Pop, T_Preserve, T_Include, T_IncludeR, T_IncludeCmd, T_If, T_Else, T_EndIf, T_IfTrig, T_ErrMsg, T_Set, T_UnSet, T_Fset, T_Omit, T_Banner, T_Exit, T_AddOmit, diff --git a/tests/include_dir/02.rem b/tests/include_dir/02.rem index ef53d19b..daff774d 100644 --- a/tests/include_dir/02.rem +++ b/tests/include_dir/02.rem @@ -1 +1,3 @@ REM 15 MSG 02 +DO subdir/04.rem +INCLUDE subdir/04.rem diff --git a/tests/include_dir/subdir/04.rem b/tests/include_dir/subdir/04.rem new file mode 100644 index 00000000..a5d35e07 --- /dev/null +++ b/tests/include_dir/subdir/04.rem @@ -0,0 +1 @@ +REM 16 MSG Should be included by 02.rem