Skip to content

Commit

Permalink
improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
dongyx committed Oct 15, 2023
1 parent 3543286 commit fa0cc40
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 41 deletions.
1 change: 1 addition & 0 deletions cases/compile2/echo.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 3
1 change: 1 addition & 0 deletions cases/compile2/echo.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%="$*"%>
6 changes: 6 additions & 0 deletions cases/compile2/run
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

set -e
shsub -c -s /bin/sh echo.tpl >echo.sh.tmp
sh echo.sh.tmp 1 2 3 >echo.tmp
diff -u echo.out echo.tmp
91 changes: 50 additions & 41 deletions shsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct iframe {
enum token lookahead;
} istack[MAXINCL], *isp = istack;

char *progname, *tmplname, script[] = "/tmp/shsub.XXXXXX";
char *progname, *tmplname, *script;
enum token lookahead;
int cstack[3], *csp = cstack, lineno = 1;
char literal[MAXLITR];
Expand All @@ -53,9 +53,10 @@ void syserr(void);

int main(int argc, char **argv)
{
char *sh = "/bin/sh", tmp[] = "/tmp/shsub.XXXXXX";
int fd, op, exe = 1;
FILE *in, *out;
char *sh = "/bin/sh", *target = NULL;
int fd, op, noexe = 0;
struct stat st;

progname = argv[0];
if (argc > 1) {
Expand All @@ -66,53 +67,61 @@ int main(int argc, char **argv)
}
while ((op = getopt(argc, argv, "s:co:")) != -1)
switch (op) {
case 's': sh = optarg; break;
case 'c': noexe = 1; break;
case 'o': target = optarg; break;
case '?': err("Call with `--help` for usage");
case 's':
sh = optarg;
break;
case 'c':
exe = 0;
break;
case 'o':
script = optarg;
break;
default:
err("Call with `--help` for usage");
}
argc -= optind;
argv += optind;
in = stdin;
if (argc > 0) {
tmplname = argv[0];
if (!(in = fopen(tmplname = argv[0], "r")))
err("%s: %s", tmplname, strerror(errno));
}
lookahead = gettoken(in);
if (noexe) {
if (target) {
if ((fd = open(
target,
O_CREAT|O_WRONLY|O_TRUNC,
0755
)) == -1)
syserr();
if (!(out = fdopen(fd, "w")))
syserr();
} else
out = stdout;
fprintf(out, "#!%s\n", sh);
tmpl(in, out);
if (fclose(out) == EOF)
} else
in = stdin;
if (exe) {
if ((fd = mkstemp(script = tmp)) == -1)
syserr();
return 0;
}
if ((fd = mkstemp(script)) == -1 || atexit(rmscr))
syserr();
if (!(out = fdopen(fd, "w")))
syserr();
fprintf(out, "#!%s\nrm %s\n", sh, script);
if (atexit(rmscr) == -1)
syserr();
} else if (script) {
if ((fd = open(script, O_CREAT|O_WRONLY|O_TRUNC, 0744))
== -1
)
syserr();
} else
fd = 1;
if (fd != 1) {
if (fstat(fd, &st) == -1)
syserr();
if (fchmod(fd, st.st_mode|0111) == -1)
syserr();
if (!(out = fdopen(fd, "w")))
syserr();
} else
out = stdout;
fprintf(out, "#!%s\n", sh);
if (exe)
fprintf(out, "rm %s\n", script);
lookahead = gettoken(in);
tmpl(in, out);
if (fchmod(fd, S_IRWXU) == -1)
syserr();
if (fclose(out))
syserr();
if (argc > 0)
execv(script, argv);
else
execl(script, "-", NULL);
syserr();
if (exe) {
if (argc > 0)
execv(script, argv);
else
execl(script, "-", NULL);
syserr();
}
return 0;
}

Expand Down Expand Up @@ -292,14 +301,14 @@ void text(int esc, FILE *in, FILE *ou)
void match(enum token tok, FILE *fp)
{
if (lookahead != tok)
parserr("Lack of expected token");
parserr("Unexpected token");
lookahead = gettoken(fp);
}

void ipush(FILE *in)
{
if (isp == istack + MAXINCL)
err("Including too deep");
err("Too deep including");
isp->lookahead = lookahead;
memcpy(isp->cstack, cstack, sizeof cstack);
isp->csp = csp;
Expand Down

0 comments on commit fa0cc40

Please sign in to comment.