|
DataMuseum.dkPresents historical artifacts from the history of: Commodore CBM-900 |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about Commodore CBM-900 Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - download
Length: 5372 (0x14fc) Types: TextFile Notes: UNIX file Names: »main.c«
└─⟦f27320a65⟧ Bits:30001972 Commodore 900 hard disk image with partial source code └─⟦f4b8d8c84⟧ UNIX Filesystem └─ ⟦this⟧ »cmd/rsh/main.c« └─ ⟦this⟧ »cmd/sh/main.c«
/* * The Bourne shell. * Main programme, initialisation and miscellaneous routines. */ #include <param.h> #include "sh.h" main(argc, argv, envp) char *argv[]; char *envp[]; { sarg0 = argc>0 ? argv[0] : ""; if (argc>0 && argv[0][0]=='-') { lgnflag++; umask(ufmask=022); } else { umask(ufmask=umask(ufmask)); } if (setjmp(restart) != 0) { /* reentry for shell command file execution */ argc = nargc; argv = nargv; envp = nenvp; cmdflag++; nllflag = 0; } shpid = getpid(); initvar(envp); cleanup(1, NULL); if (set(argc, argv, 1)) { exit(1); NOTREACHED; } if (cflag) { if (sargp[0]==NULL) { printe("No string for -c?"); exit(1); NOTREACHED; } --sargc; session(SARGS, *sargp++); } else if (!sflag && !iflag && sargc!=0) { sarg0 = *sargp++; --sargc; session(SFILE, sarg0); } else { session(SSTR, stdin); } cleanup(2, NULL); return (slret); } /* * Loop on input. */ session(t, p) register char *p; { SES s; register int rcode; s.s_next = sesp; sesp = &s; s.s_bpp = savebuf(); switch (s.s_type = t) { case SARGS: s.s_strp = p; s.s_flag = 0; break; case SARGV: s.s_argv = (char **) p; if ((s.s_strp = s.s_argv[0]) == NULL) return (0); s.s_flag = 0; break; case SFILE: s.s_strp = p; if ((s.s_ifp = fopen(s.s_strp, "r")) == NULL) { ecantopen(s.s_strp); return (1); } s.s_flag = isatty(fileno(s.s_ifp)) && isatty(2); break; case SSTR: s.s_strp = NULL; s.s_ifp = (FILE *) p; s.s_flag = isatty(fileno(s.s_ifp)) && isatty(2); break; } if (s.s_next == NULL) { /* Initial entry */ if (iflag) s.s_flag = iflag; else iflag = s.s_flag; dflttrp(IRDY); } /* Loop on input */ for (;;) { switch (rcode = setjmp(s.s_envl)) { case RSET: /* initial setjmp call */ if (lgnflag) { lgnflag = 0; if (ffind("/etc", "profile", 4)) session(SFILE, duplstr(strt, 0)); if (*vhome && ffind(vhome, ".profile", 4)) session(SFILE, duplstr(strt, 0)); } checkmail(); comflag = 1; errflag = 0; recover(IRDY); freebuf(s.s_bpp); s.s_bpp = savebuf(); yyparse(); case REOF: recover(IRDY); break; case RCMD: recover(IRDY); s.s_con = NULL; command(s.s_node); if ((tflag && tflag++ >= 2)) break; continue; case RERR: recover(IRDY); if ( ! errflag) syntax(); if ( ! iflag || (tflag && tflag++ >= 2)) break; continue; case RINT: if (s.s_next != NULL) { sesp = s.s_next; reset(RINT); NOTREACHED; } prpflag = 2; if ( ! iflag || (tflag && tflag++ >= 2)) break; continue; case RUEXITS: case RUABORT: if (s.s_next != NULL) { sesp = s.s_next; reset(rcode); NOTREACHED; } if ( ! iflag || (tflag && tflag++ >= 2)) break; continue; case RNOSBRK: case RSYSER: case RBRKCON: case RNOWAY: default: if (s.s_next!=NULL) break; if ( ! iflag || (tflag && tflag++ >= 2)) break; continue; } break; } freebuf(s.s_bpp); if (s.s_type == SFILE) fclose(s.s_ifp); if (s.s_next == NULL) { sigintr(0); recover(IRDY); } sesp = s.s_next; return (slret); } reset(f) { longjmp(sesp->s_envl, f); NOTREACHED; } /* * Kludge cleanup. */ cleanup(flag, file) char *file; { static char *files[8]; static int nfiles = 0; register char **pp; if (flag) { for (pp=files; pp<files+8; pp+=1) if (*pp != NULL) { if (flag==2) unlink(*pp); sfree(*pp); *pp = NULL; } nfiles = 0; } else { pp = files + nfiles; if (*pp != NULL) { unlink(*pp); sfree(*pp); } *pp = duplstr(file, 1); nfiles = (nfiles + 1) & 7; } } /* * Make a temp file name. */ char * shtmp() { static char tmpfile[] = "/tmp/shXXXXXX"; static int tmpflag = 0; sprintf(tmpfile+6, "%05d%c", shpid, (tmpflag++%26) + 'a'); return (tmpfile); } /* * Print formatted. */ /* printv(av) register char **av; { while (*av) prints("\t%s\n", *av++); } */ prints(a1) char *a1; { fprintf(stderr, "%r", &a1); } /* * Make a core dump in /tmp and longjmp back to session - * there's a possibility we'll die horribly. */ panic() { #ifdef PARANOID register int f; if ((f=fork())==0) { abort(); NOTREACHED; } waitc(f); #endif printe("Internal shell assertion failed"); reset(RNOWAY); NOTREACHED; } /* * Print out an error message. */ printe(a1) char *a1; { errflag += 1; if (! noeflag) { fprintf(stderr, "%r", &a1); fprintf(stderr, "\n"); } } /* * Some familiar errors. */ ecantopen(s) char *s; { printe("Can't open %s", s); } ecantfind(s) char *s; { printe("Can't find %s", s); } ecantmake(s) char *s; { printe("Can't create %s", s); } emisschar(c) { printe("Missing `%c'", c); } ecantfdop() { printe("Fdopen failed"); } enotdef(s) char *s; { printe("Can't find variable %s", s); } eillvar(s) char *s; { printe("Illegal variable name: %s", s); } eredir() { printe("Illegal redirection"); } etoolong() { printe("Argument too long: %.*s", STRSIZE, strt); } /* * Don't print out an error message. */ yyerror() { } /* * print out the prompt given the prompt to write */ prompt(vps) char *vps; { prints("%s", vps); #if RSX fflush(stdout); #endif } /* * Syntax error message - print line number and file if * not interactive. */ syntax() { if (sesp->s_type == SFILE) printe("%s: Syntax error in line %d", sesp->s_strp, yyline); else printe("Syntax error"); }