Annotation of researchv10dc/ipc/bin/qns.c, revision 1.1

1.1     ! root        1: #include <fio.h>
        !             2: #include <libc.h>
        !             3: #include "ipc.h"
        !             4: 
        !             5: #define NULL 0
        !             6: 
        !             7: extern char *optarg;
        !             8: extern int optind;
        !             9: 
        !            10: int debug;
        !            11: 
        !            12: main(ac, av)
        !            13:        int ac;
        !            14:        char *av[];
        !            15: {
        !            16:        int c;
        !            17:        char buf[256];
        !            18:        char *ns = "/cs/ns";
        !            19: 
        !            20:        while((c = getopt(ac, av, "n:d")) != -1){
        !            21:                switch(c){
        !            22:                case 'n':
        !            23:                        ns = optarg;
        !            24:                        break;
        !            25:                case 'd':
        !            26:                        debug++;
        !            27:                        break;
        !            28:                }
        !            29:        }
        !            30:        if(optind<ac) {
        !            31:                /*
        !            32:                 *  do a single command
        !            33:                 */
        !            34:                if (command(ns, optind, ac, av)<0)
        !            35:                        exit(1);
        !            36:        } else {
        !            37:                /*
        !            38:                 *  make a connection and just shuttle bytes
        !            39:                 */
        !            40:                call(ns);
        !            41:        }
        !            42:        _exit(0);
        !            43: }
        !            44: 
        !            45: /*
        !            46:  *  do the command from the command line
        !            47:  */
        !            48: command(ns, optind, ac, av)
        !            49:        char *ns;
        !            50:        int optind, ac;
        !            51:        char *av[];
        !            52: {
        !            53:        char *cp;
        !            54:        char *types;
        !            55:        Qset *sp;
        !            56:        Qtuple *tp;
        !            57:        char buf[1024];
        !            58:        int fd;
        !            59:        extern char *qvalue();
        !            60:        extern Qset *qset();
        !            61: 
        !            62:        if(strcmp(av[optind], "value")==0 && ac-optind>=3){
        !            63:                /*
        !            64:                 *  just return the value
        !            65:                 */
        !            66:                types = av[++optind];
        !            67:                for(++optind; optind<ac; optind++){
        !            68:                        strcat(buf, av[optind]);
        !            69:                        strcat(buf, " ");
        !            70:                }
        !            71:                cp = qvalue(types, buf, ns);
        !            72:                if(cp!=NULL){
        !            73:                        fprint(1, "%s\n", cp);
        !            74:                } else if(errno!=0){
        !            75:                        fprint(2, "error: %s\n", errstr);
        !            76:                        exit(1);
        !            77:                }
        !            78:                return 0;
        !            79:        } else if(strcmp(av[optind], "values")==0 && ac-optind>=3){
        !            80:                /*
        !            81:                 *  return a set of tuples (one tuple per line)
        !            82:                 */
        !            83:                types = av[++optind];
        !            84:                for(++optind; optind<ac; optind++){
        !            85:                        strcat(buf, av[optind]);
        !            86:                        strcat(buf, " ");
        !            87:                }
        !            88:                sp = qset(buf, ns);
        !            89:                if(sp==0 && errno!=0){
        !            90:                        fprint(2, "error: %s\n", errstr);
        !            91:                        exit(1);
        !            92:                }
        !            93:                for(; sp; sp = sp->next){
        !            94:                        if(sp->this) for(tp=sp->this; tp; tp=tp->next){
        !            95:                                if((tp->type && strcmp(tp->type, types)==0)
        !            96:                                || (tp->type==0 && strcmp("name", types)==0))
        !            97:                                        fprint(1, "%s\n", tp->value);
        !            98:                        }
        !            99:                }
        !           100:                return 0;
        !           101:        } else if(strcmp(av[optind], "set")==0 && ac-optind>=2){
        !           102:                /*
        !           103:                 *  return a set of tuples (one tuple per line)
        !           104:                 */
        !           105:                for(++optind; optind<ac; optind++){
        !           106:                        strcat(buf, av[optind]);
        !           107:                        strcat(buf, " ");
        !           108:                }
        !           109:                sp = qset(buf, ns);
        !           110:                if(sp==0 && errno!=0){
        !           111:                        fprint(2, "error: %s\n", errstr);
        !           112:                        exit(1);
        !           113:                }
        !           114:                for(; sp; sp = sp->next){
        !           115:                        if(sp->this) for(tp=sp->this; tp; tp=tp->next){
        !           116:                                if(tp->type && *(tp->type))
        !           117:                                        fprint(1, "%s,%s ",tp->value,tp->type);
        !           118:                                else
        !           119:                                        fprint(1, "%s ", tp->value);
        !           120:                        }
        !           121:                        fprint(1, "\n");
        !           122:                }
        !           123:                return 0;
        !           124:        }
        !           125: 
        !           126:        /*
        !           127:         *  something other than set or value, do it the hard way
        !           128:         */
        !           129:        fd = ipcopen(ns, "");
        !           130:        if(fd<0) {
        !           131:                fprint(2, "can't reach name server\n");
        !           132:                exit(1);
        !           133:        }
        !           134:        Finit(fd, (char *)0);
        !           135:        buf[0] = 0;
        !           136:        for(; optind<ac; optind++){
        !           137:                strcat(buf, av[optind]);
        !           138:                strcat(buf, " ");
        !           139:        }
        !           140:        fprint(fd, "%s\n", buf);
        !           141:        return getreply(fd);
        !           142: }
        !           143: 
        !           144: /*
        !           145:  *  just send user strings to the name server and vice versa
        !           146:  */
        !           147: call(ns)
        !           148:        char *ns;
        !           149: {
        !           150:        int fd;
        !           151:        char buf[256];
        !           152:        char *cp;
        !           153: 
        !           154:        fd = ipcopen(ns, "");
        !           155:        if(fd<0) {
        !           156:                fprint(2, "can't reach name server\n");
        !           157:                exit(1);
        !           158:        }
        !           159:        Finit(fd, (char *)0);
        !           160:        fprint(1, ">");
        !           161:        while((cp=Frdline(0))!=NULL){
        !           162:                if (*cp != NULL) {
        !           163:                        fprint(fd, "%s\n", cp);
        !           164:                        if(getreply(fd)<0)
        !           165:                                return;
        !           166:                }
        !           167:                fprint(1, ">");
        !           168:        }
        !           169: }
        !           170: 
        !           171: getreply(fd)
        !           172:        int fd;
        !           173: {
        !           174:        char *cp;
        !           175: 
        !           176:        while((cp=Frdline(fd))!=NULL){
        !           177:                if(strncmp("OK", cp, 2)==0) {
        !           178:                        continue;
        !           179:                } else if(strncmp("ILL", cp, 3)==0 || strncmp("BUSY", cp, 4)==0) {
        !           180:                        fprint(1, "%s\n", cp);
        !           181:                        return 0;
        !           182:                } else if(strncmp("DONE", cp, 4)==0) {
        !           183:                        return 0;
        !           184:                }
        !           185:                fprint(1, "%s\n", *cp=='\t'?cp+1:cp);
        !           186:        }
        !           187:        return -1;
        !           188: }

unix.superglobalmegacorp.com

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