Esempio n. 1
0
/* Helper function for the redisAsyncCommand* family of functions. Writes a
 * formatted command to the output buffer and registers the provided callback
 * function with the context. */
static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) {
    redisContext *c = &(ac->c);
    redisCallback cb;
    int pvariant, hasnext;
    char *cstr, *astr;
    size_t clen, alen;
    char *p;
    sds sname;

    /* Don't accept new commands when the connection is about to be closed. */
    if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;

    /* Setup callback */
    cb.fn = fn;
    cb.privdata = privdata;

    /* Find out which command will be appended. */
    p = nextArgument(cmd,&cstr,&clen);
    assert(p != NULL);
    hasnext = (p[0] == '$');
    pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;
    cstr += pvariant;
    clen -= pvariant;

    if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) {
        c->flags |= REDIS_SUBSCRIBED;

        /* Add every channel/pattern to the list of subscription callbacks. */
        while ((p = nextArgument(p,&astr,&alen)) != NULL) {
            sname = sdsnewlen(astr,alen);
            if (pvariant)
                dictReplace(ac->sub.patterns,sname,&cb);
            else
                dictReplace(ac->sub.channels,sname,&cb);
        }
    } else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) {
        /* It is only useful to call (P)UNSUBSCRIBE when the context is
         * subscribed to one or more channels or patterns. */
        if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;

        /* (P)UNSUBSCRIBE does not have its own response: every channel or
         * pattern that is unsubscribed will receive a message. This means we
         * should not append a callback function for this command. */
    } else {
        if (c->flags & REDIS_SUBSCRIBED)
            /* This will likely result in an error reply, but it needs to be
             * received and passed to the callback. */
            __redisPushCallback(&ac->sub.invalid,&cb);
        else
            __redisPushCallback(&ac->replies,&cb);
    }

    __redisAppendCommand(c,cmd,len);

    /* Always schedule a write when the write buffer is non-empty */
    if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);

    return REDIS_OK;
}
Esempio n. 2
0
// the magical thingy that have to cut everything and make most of the logical preexecution things, hopefully right :D
void cutCommand(struct codeBreaking * code ){
	char * buf;
	int i ;
	i= nextArgument(&buf, code);
	if (i !=0 && i != 1) 
		exit (111); // magical error code
	if(i== 1) return;
	setCommand(code, &buf);
	while ((i = nextArgument(&buf, code)) == 0){
		addToList(code->argv, buf);
	}
	if (buf != NULL)
		addToList(code->argv, buf);
	if (i !=1) exit (112); //another magical error code
}
void TCPClient(const int& fd, const char* host) {
    std::string serverPath = ClientFunc::pwd(fd);
    WorkingDirectory wd;
    printInfo();
    while (true) {
        printf("%s:%s$ ", host, serverPath.c_str());
        char userInputCStr[maxn];
        if (!fgets(userInputCStr, maxn, stdin)) {
            break;
        }
        if (!strcmp(userInputCStr, "\n") || isAllSpace(userInputCStr)) {
            continue;
        }
        std::string userInput = userInputCStr;
        std::string command = nextArgument(userInput);
        if (command == "help") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: help\n");
                    printf("Print help information.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                printInfo();
            }
        }
        else if (command == "lpwd") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: lpwd\n");
                    printf("Print local current working directory.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                printf("%s\n", wd.getPath().c_str());
            }
        }
        else if (command == "lls") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: lpwd\n");
                    printf("List information about the files in the local current directory.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                DIR* dir = opendir(wd.getPath().c_str());
                if (!dir) {
                    fprintf(stderr, "%s: Cannot open the directory\n", wd.getPath().c_str());
                    continue;
                }
                else {
                    dirent *dirst;
                    std::vector<std::string> fileList;
                    while ((dirst = readdir(dir))) {
                        std::string name(dirst->d_name);
                        if (dirst->d_type == DT_DIR) {
                            name += "/";
                        }
                        fileList.push_back(name);
                    }
                    std::sort(fileList.begin(), fileList.end());
                    for (const auto& i : fileList) {
                        printf("%s\n", i.c_str());
                    }
                    closedir(dir);
                }
            }
        }
        else if (command == "exit") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: exit\n");
                    printf("Terminate the connection.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                ClientFunc::q(fd);
                printf("\nConnection Terminated\n\n");
                break;
            }
        }
        else if (command == "pwd") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: pwd\n");
                    printf("Print current working directory on Remote Server.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                printf("%s\n", ClientFunc::pwd(fd).c_str());
            }
        }
        else if (command == "ls") {
            std::string argu = nextArgument(userInput);
            if (argu != "") {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: ls\n");
                    printf("List information about the files in the current directory on Remote Server.\n");
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                printf("%s\n", ClientFunc::ls(fd).c_str());
            }
        }
        else if (command == "cd") {
            std::string argu = nextArgument(userInput);
            if (argu == "" || argu[0] == '-') {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: cd <path>\n");
                    printf("Change working directory to <path> on Remote Server.\n");
                    printf("ex:\n");
                    printf("    cd \"Network Programming\"\n");
                    printf("    cd ../Download\n");
                }
                else if (argu == "") {
                    continue;
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                ClientFunc::cd(fd, argu);
                serverPath = ClientFunc::pwd(fd);
            }
        }
        else if (command == "u") {
            std::string argu = nextArgument(userInput);
            if (argu == "" || argu[0] == '-') {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: u <file>\n");
                    printf("Upload file(path related to local working directory) to Remote Server.\n");
                    printf("ex:\n");
                    printf("    u hw1.tar\n");
                    printf("    u ../client.cpp\n");
                }
                else if (argu == "") {
                    printf("usage: u <file>\nu --help for more information\n");
                    continue;
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                ClientFunc::u(fd, argu);
            }
        }
        else if (command == "d") {
            std::string argu = nextArgument(userInput);
            if (argu == "" || argu[0] == '-') {
                if (argu == "-h" || argu == "-help" || argu == "--help") {
                    printf("usage: d <file>\n");
                    printf("Download file(path related to working directory on server) to Download.\n");
                    printf("ex:\n");
                    printf("    d hw1.tar\n");
                    printf("    d ../server.cpp\n");
                }
                else if (argu == "") {
                    printf("usage: d <file>\nd --help for more information\n");
                    continue;
                }
                else {
                    fprintf(stderr, "Unrecognized Argument %s\n", argu.c_str());
                }
            }
            else {
                ClientFunc::d(fd, argu, wd);
            }
        }
        else {
            fprintf(stderr, "%s: Command not found\n", command.c_str());
        }
    }
}