Annotation of coherent/g/usr/bin/vi/shell.c, revision 1.1

1.1     ! root        1: /* shell.c */
        !             2: 
        !             3: /* Author:
        !             4:  *     Guntram Blohm
        !             5:  *     Buchenstrasse 19
        !             6:  *     7904 Erbach, West Germany
        !             7:  *     Tel. ++49-7305-6997
        !             8:  *     sorry - no regular network connection
        !             9:  */
        !            10: 
        !            11: /*
        !            12:  * This file contains a minimal version of a shell for TOS. It allows the
        !            13:  * setting of an environment, calling programs, and exiting.
        !            14:  * If you don't have another one, this might be sufficient, but you should 
        !            15:  * prefer *any* other shell.
        !            16:  * You may, however, want to set your SHELL environment variable to this
        !            17:  * shell: it implements the -c switch, which is required by Elvis, and
        !            18:  * not supported by most other atari shells.
        !            19:  */
        !            20:  
        !            21: #include <stdio.h>
        !            22: #include <string.h>
        !            23: #include <osbind.h>
        !            24: extern char *getenv(), *malloc();
        !            25: extern char **environ;
        !            26: long _stksize=16384;
        !            27: 
        !            28: #define        MAXENV  50
        !            29: 
        !            30: struct
        !            31: {
        !            32:        char *name;
        !            33:        char *value;
        !            34: } myenv[MAXENV];
        !            35: 
        !            36: int cmd_set(), cmd_exit();
        !            37: 
        !            38: struct buildins
        !            39: {
        !            40:        char *name;
        !            41:        int (*func)();
        !            42: } buildins[]=
        !            43: {      "exit", cmd_exit,
        !            44:        "set", cmd_set,
        !            45:        0,
        !            46: };
        !            47: 
        !            48: main(argc, argv)
        !            49:        int argc;
        !            50:        char **argv;
        !            51: {
        !            52:        char buf[128];
        !            53:        int i;
        !            54: 
        !            55:        for (i=0; environ[i] && strncmp(environ[i],"ARGV=",5); i++)
        !            56:                cmd_set(environ[i]);
        !            57:        script("profile.sh");
        !            58: 
        !            59:        if (argc>1 && !strcmp(argv[1], "-c"))
        !            60:        {
        !            61:                buf[0]='\0';
        !            62:                for (i=2; i<argc; i++)
        !            63:                {       if (i>2)
        !            64:                                strcat(buf, " ");
        !            65:                        strcat(buf, argv[i]);
        !            66:                }
        !            67:                execute(buf);
        !            68:        }
        !            69:        else
        !            70:                while (fputs("$ ", stdout), gets(buf))
        !            71:                        execute(buf);
        !            72:        exit(0);
        !            73: }
        !            74: 
        !            75: execute(buf)
        !            76:        char *buf;
        !            77: {
        !            78:        char *scan=buf;
        !            79:        char cmd[80];
        !            80:        char line[128];
        !            81:        char env[4096], *ep=env;
        !            82:        int i;
        !            83: 
        !            84:        while (*scan==' ')
        !            85:                scan++;
        !            86:        if (!*scan)
        !            87:                return 0;
        !            88:        while (*scan && *scan!=' ')
        !            89:                scan++;
        !            90:        if (*scan)
        !            91:                *scan++='\0';
        !            92: 
        !            93:        for (i=0; buildins[i].name; i++)
        !            94:                if (!strcmp(buf, buildins[i].name))
        !            95:                        return (*buildins[i].func)(scan);
        !            96: 
        !            97:        if (!searchpath(buf, cmd))
        !            98:        {       printf("%s: not found\n", buf);
        !            99:                return -1;
        !           100:        }
        !           101: 
        !           102:        strcpy(line+1, scan);
        !           103:        line[0]=strlen(scan);
        !           104:        for (i=0; i<MAXENV && myenv[i].name; i++)
        !           105:        {       strcpy(ep, myenv[i].name);
        !           106:                strcat(ep, "=");
        !           107:                strcat(ep, myenv[i].value);
        !           108:                ep+=strlen(ep)+1;
        !           109:        }
        !           110:        
        !           111:        *ep='\0';
        !           112: 
        !           113:        return Pexec(0, cmd, line, env);
        !           114: }
        !           115: 
        !           116: searchpath(from, to)
        !           117:        char *from, *to;
        !           118: {
        !           119:        char *path="";
        !           120:        char *scan;
        !           121:        char *end;
        !           122:        char *q;
        !           123:        int i;
        !           124: 
        !           125:        for (i=0; i<MAXENV && myenv[i].name; i++)
        !           126:                if (!strcmp(myenv[i].name,"PATH"))
        !           127:                        path=myenv[i].value;
        !           128:        for (scan=from; *scan; scan++)
        !           129:                if (*scan==':' || *scan=='\\')
        !           130:                {       path=0;
        !           131:                        break;
        !           132:                }
        !           133:        if (!path)
        !           134:        {       strcpy(to, from);
        !           135:                end=to+strlen(to);
        !           136:                strcpy(end, ".prg"); if (try(to)) return 1;
        !           137:                strcpy(end, ".ttp"); if (try(to)) return 1;
        !           138:                strcpy(end, ".tos"); if (try(to)) return 1;
        !           139:                *to='\0'; return 0;
        !           140:        }
        !           141:        for (scan=path; *scan; )
        !           142:        {
        !           143:                for (q=to; *scan && *scan!=';' && *scan!=','; scan++)
        !           144:                        *q++=*scan;
        !           145:                if (*scan==';' || *scan==',')
        !           146:                        scan++;
        !           147:                *q++='\\';
        !           148:                *q='\0';
        !           149:                strcpy(q, from);
        !           150:                end=q+strlen(q);
        !           151:                strcpy(end, ".prg"); if (try(to)) return 1;
        !           152:                strcpy(end, ".ttp"); if (try(to)) return 1;
        !           153:                strcpy(end, ".tos"); if (try(to)) return 1;
        !           154:        }
        !           155:        *to='\0';
        !           156:        return 0;
        !           157: }
        !           158: 
        !           159: try(name)
        !           160:        char *name;
        !           161: {
        !           162:        if (Fattrib(name, 0, 0) < 0)
        !           163:                return 0;
        !           164:        return 1;
        !           165: }
        !           166: 
        !           167: cmd_exit()
        !           168: {
        !           169:        exit(0);
        !           170: }
        !           171: 
        !           172: cmd_set(line)
        !           173:        char *line;
        !           174: {
        !           175:        char *value;
        !           176:        int i;
        !           177: 
        !           178:        if (!*line)
        !           179:        {
        !           180:                for (i=0; i<MAXENV && myenv[i].name; i++)
        !           181:                        printf("%s=%s\n", myenv[i].name, myenv[i].value);
        !           182:                return 0;
        !           183:        }
        !           184: 
        !           185:        for (value=line; *value && *value!='='; value++)
        !           186:                ;
        !           187:        if (!*value)
        !           188:        {       printf("Usage: set name=var\n");
        !           189:                return -1;
        !           190:        }
        !           191:        *value++='\0';
        !           192:        return doset(line, value);
        !           193: }
        !           194: 
        !           195: doset(line, value)
        !           196:        char *line, *value;
        !           197: {
        !           198:        int i;
        !           199: 
        !           200:        for (i=0; i<MAXENV && myenv[i].name && strcmp(myenv[i].name, line); i++)
        !           201:                ;
        !           202:        if (i==MAXENV)
        !           203:        {       printf("No Space\n");
        !           204:                return -1;
        !           205:        }
        !           206:        if (!myenv[i].name)
        !           207:        {       myenv[i].name=malloc(strlen(line)+1);
        !           208:                strcpy(myenv[i].name, line);
        !           209:        }
        !           210:        if (myenv[i].value)
        !           211:                free(myenv[i].value);
        !           212:        myenv[i].value=malloc(strlen(value)+1);
        !           213:        strcpy(myenv[i].value, value);
        !           214:        return 0;
        !           215: }
        !           216: 
        !           217: script(name)
        !           218:        char *name;
        !           219: {
        !           220:        FILE *fp;
        !           221:        char buf[128], *p;
        !           222: 
        !           223:        if ((fp=fopen(name, "r"))==0)
        !           224:                return;
        !           225:        while (fgets(buf, sizeof buf, fp))
        !           226:        {
        !           227:                if ((p=strchr(buf, '\n'))!=0)
        !           228:                        *p='\0';
        !           229:                execute(buf);
        !           230:        }
        !           231:        fclose(fp);
        !           232: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.