Skip to content

Commit

Permalink
Fix filename extension detection on relative paths
Browse files Browse the repository at this point in the history
Added "sir" (reverse string index search) function to eval.c which
enables the emacs.rc script to fetch the filename extension correctly even
when loading the file using a relative path.

Signed-off-by: Luiz A. Bühnemann <[email protected]>
  • Loading branch information
la3280 committed May 11, 2022
1 parent 1cdcf9d commit 4a7c6ca
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
1 change: 1 addition & 0 deletions efunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ extern char *mklower(char *str);
extern int abs(int x);
extern int ernd(void);
extern int sindex(char *source, char *pattern);
extern int rsindex(char *source, char *pattern);
extern char *xlat(char *source, char *lookup, char *trans);

/* crypt.c */
Expand Down
2 changes: 1 addition & 1 deletion emacs.rc
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ bind-to-key execute-macro-40 M-?
add-mode "wrap"
!return
!endif
set %rctmp &sin $cfname "."
set %rctmp &sir $cfname "."
!if &equ %rctmp 0
!return
!endif
Expand Down
44 changes: 44 additions & 0 deletions eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ char *gtfun(char *fname)
return itoa(~atoi(arg1));
case UFXLATE:
return xlat(arg1, arg2, arg3);
case UFRSINDEX:
return itoa(rsindex(arg1, arg2));
}

exit(-11); /* never should get here */
Expand Down Expand Up @@ -972,6 +974,48 @@ int sindex(char *source, char *pattern)
return 0;
}

/*
* reverse find pattern within source
*
* char *source; source string to search
* char *pattern; string to look for
*/
int rsindex(char *source, char *pattern)
{
char *sp; /* ptr to current position to scan */
char *csp; /* ptr to source string during comparison */
char *cp; /* ptr to place to check for equality */
int lp;
int ls;

ls = strlen(source);
lp = strlen(pattern);
if (ls == 0 || lp == 0 || ls < lp)
return 0;

/* scanning through the source string */
sp = source + ls - lp;
while (sp >= source) {
/* scan through the pattern */
cp = pattern;
csp = sp;
while (*cp) {
if (!eq(*cp, *csp))
break;
++cp;
++csp;
}

/* was it a match? */
if (*cp == 0)
return (int) (sp - source) + 1;
--sp;
}

/* no match at all.. */
return 0;
}

/*
* Filter a string through a translation table
*
Expand Down
2 changes: 2 additions & 0 deletions evar.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ static struct user_function funcs[] = {
{ "bxo", DYNAMIC }, /* bitwise xor 9-10-87 jwm */
{ "bno", MONAMIC }, /* bitwise not */
{ "xla", TRINAMIC }, /* XLATE character string translation */
{ "sir", DYNAMIC } /* reverse find the index of one string in another */
};

/* And its preprocesor definitions. */
Expand Down Expand Up @@ -205,5 +206,6 @@ static struct user_function funcs[] = {
#define UFBXOR 36
#define UFBNOT 37
#define UFXLATE 38
#define UFRSINDEX 39

#endif /* EVAR_H_ */

0 comments on commit 4a7c6ca

Please sign in to comment.