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

Introduce 'function' built-in. #77

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
faab6cc
Introduce 'function' built-in
Krush206 Jul 7, 2023
b683769
sh.c: Sgoal shouldn't be assigned funcmain
Krush206 Jul 7, 2023
4d90ff3
sh.c: cleanup_push() raises SIGSEGV
Krush206 Jul 8, 2023
715a882
fargv should be freed from dofunction()
Krush206 Jul 8, 2023
5e37bfc
Move function tests to srcunit()
Krush206 Aug 5, 2023
235b358
Better function parsing
Krush206 Nov 21, 2023
4a54e05
Create tests for 'function' built-in
Krush206 Nov 21, 2023
5bc9252
sh.c: call cleanup_push() properly
Krush206 Nov 21, 2023
b1e4135
Documentation for 'function' built-in
Krush206 Nov 21, 2023
ede350d
Rename ERR_DOLFUNC to ERR_FUNC
Krush206 Nov 21, 2023
fc7dbd3
sh.func.c: prefer dolzero over ffile
Krush206 Nov 21, 2023
d762fba
sh.func.c: allocate memory properly
Krush206 Nov 21, 2023
951264d
sh.func.c: assign NULL after deallocating fargv
Krush206 Nov 22, 2023
7d6eef8
Prefer srcfile() over dosource()
Krush206 Dec 3, 2023
c60a4c2
Functions should work for sourced scripts.
Krush206 Feb 14, 2024
954cfd6
Rethought on functions
Krush206 May 18, 2024
15d4e30
Do not fork & fix interrupts
Krush206 Jun 2, 2024
042e84b
Refactor
Krush206 Jun 3, 2024
2fa7967
tcsh.man.in: correction & additional information
Krush206 Jun 3, 2024
b950b85
sh.err.c: recursion/nest
Krush206 Jun 3, 2024
d840e89
Create aliases for functions
Krush206 Jun 3, 2024
22d3150
sh.func.c: replace setexit with cleanup_push
Krush206 Jun 4, 2024
adea1b6
Avoid SIGPIPE on doexit
Krush206 Jun 4, 2024
d0167d4
sh.func.c: don't catch OLDSTD
Krush206 Jun 4, 2024
911ed6e
22d3150: move remaining parts to st_restore
Krush206 Jun 6, 2024
56e5e7d
911ed6e: more remaining parts
Krush206 Jun 7, 2024
c8b8f51
Prefer dozip over doreturn
Krush206 Jun 13, 2024
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
Prev Previous commit
Next Next commit
Prefer srcfile() over dosource()
  • Loading branch information
Krush206 committed Jun 1, 2024
commit 7d6eef8e715724e93a680d97beffbdd6a1dc3751
15 changes: 3 additions & 12 deletions sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ struct saved_state {
};

static int srccat (Char *, Char *);
#ifndef WINNT_NATIVE
static int srcfile (const char *, int, int, Char **);
#else
int srcfile (const char *, int, int, Char **);
#endif /*WINNT_NATIVE*/
static void srcunit (int, int, int, Char **);
static void mailchk (void);
#ifndef _PATH_DEFPATH
Expand Down Expand Up @@ -1544,11 +1539,7 @@ srccat(Char *cp, Char *dp)
/*
* Source to a file putting the file descriptor in a safe place (> 2).
*/
#ifndef WINNT_NATIVE
static int
#else
int
#endif /*WINNT_NATIVE*/
srcfile(const char *f, int onlyown, int flag, Char **av)
{
int unit;
Expand Down Expand Up @@ -1779,7 +1770,7 @@ srcunit(int unit, int onlyown, int hflg, Char **av)
Char funcexit[] = { 'e', 'x', 'i', 't', 0 },
funcmain[] = { 'm', 'a', 'i', 'n', 0 };
struct Strbuf aword = Strbuf_INIT;
Sgoal = fargv->v[2];
Sgoal = fargv->v[0];
Stype = TC_GOTO;
fargv->eof = 0;

Expand Down Expand Up @@ -1817,8 +1808,8 @@ srcunit(int unit, int onlyown, int hflg, Char **av)
(void) getword(NULL);
}

setq(STRargv, &fargv->v[3], &shvhed, VAR_READWRITE);
dogoto(&fargv->v[1], fargv->t);
setq(STRargv, &fargv->v[1], &shvhed, VAR_READWRITE);
gotolab(fargv->v[0]);

{
struct Ain a;
Expand Down
62 changes: 28 additions & 34 deletions sh.func.c
Original file line number Diff line number Diff line change
Expand Up @@ -2732,44 +2732,38 @@ dofunction(Char **v, struct command *t)
if (!dolzero)
stderror(ERR_FUNC);

{
int i, j;
Char **vh;

for (i = 0; v[i]; i++)
;
if (fargv) {
fargv->next = malloc(sizeof *fargv);
fargv->next->prev = fargv;
fargv = fargv->next;
} else {
fargv = malloc(sizeof *fargv);
fargv->prev = NULL;
}

vh = xmalloc(sizeof(Char [i + 2]));
vh[i + 1] = NULL;
{
int i = 0;
Char **vh = NULL;

for (j = i--; i; i--, j--) {
vh[j] = xmalloc(sizeof(Char [Strlen(v[i]) + 1]));
Strcpy(vh[j], v[i]);
}
vh[1] = xmalloc(sizeof (Char [Strlen(ffile) + 1]));
Strcpy(vh[1], ffile);
*vh = xmalloc(sizeof (Char [Strlen(*v) + 1]));
Strcpy(*vh, *v);

if (fargv) {
fargv->next = malloc(sizeof *fargv);
fargv->next->prev = fargv;
fargv = fargv->next;
} else {
fargv = malloc(sizeof *fargv);
fargv->prev = NULL;
for (v++; *v; v++, i++) {
vh = xrealloc(vh, sizeof(Char *[i + 2]));
vh[i] = xmalloc(sizeof(Char [Strlen(*v) + 1]));
Strcpy(vh[i], *v);
}

dosource(fargv->v = vh, fargv->t = t);
/* Reset STRargv on function exit. */
setv(STRargv, NULL, VAR_READWRITE);
vh[i] = NULL;
fargv->v = vh;
}

if (fargv->prev) {
fargv = fargv->prev;
free(fargv->next);
} else {
free(fargv);
fargv = NULL;
}
srcfile(short2str(ffile), 0, 0, NULL);
/* Reset STRargv on function exit. */
setv(STRargv, NULL, VAR_READWRITE);

if (fargv->prev) {
fargv = fargv->prev;
free(fargv->next);
} else {
free(fargv);
fargv = NULL;
}
}
4 changes: 2 additions & 2 deletions sh.h
Original file line number Diff line number Diff line change
Expand Up @@ -1310,11 +1310,11 @@ extern Char *Sgoal;
extern int Stype;
extern struct funcargs {
Char **v;
struct command *t;
int eof;
struct funcargs *prev,
*next;
int eof;
} *fargv;
extern int getword(struct Strbuf *);
extern int srcfile(const char *, int, int, Char **);

#endif /* _h_sh */