/* * Copyright (C) 2013 Bryan Christ * 2014 Johannes Schauer * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2, as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * */ #include #include #include #include #include "commands.h" #include "mfshell.h" #include "../mfapi/folder.h" struct mfcmd commands[] = { {"help", "", "show this help", mfshell_cmd_help}, {"debug", "", "show debug information", mfshell_cmd_debug}, {"host", "", "change target server", mfshell_cmd_host}, {"auth", ">", "authenticate with active server", mfshell_cmd_auth}, {"whoami", "", "show basic user info", mfshell_cmd_whoami}, {"ls", "", "show contents of active folder", mfshell_cmd_list}, {"cd", "[folderkey]", "change active folder", mfshell_cmd_chdir}, {"pwd", "", "show the active folder", mfshell_cmd_pwd}, {"lpwd", "", "show the local working directory", mfshell_cmd_lpwd}, {"lcd", "[dir]", "change the local working directory", mfshell_cmd_lcd}, {"mkdir", "[folder name]", "create a new folder", mfshell_cmd_mkdir}, {"file", "[quickkey]", "show file information", mfshell_cmd_file}, {"links", "[quickkey]", "show access urls for the file", mfshell_cmd_links}, {"get", "[quickkey]", "download a file", mfshell_cmd_get}, {"rmdir", "[folderkey]", "remove directory", mfshell_cmd_rmdir}, {NULL, NULL, NULL, NULL} }; mfshell *mfshell_create(int app_id, char *app_key, char *server) { mfshell *shell; if (app_id <= 0) return NULL; if (app_key == NULL) return NULL; if (server == NULL) return NULL; /* check to see if the server contains a forward-slash. if so, the caller did not understand the API and passed in the wrong type of server resource. */ if (strchr(server, '/') != NULL) return NULL; shell = (mfshell *) calloc(1, sizeof(mfshell)); shell->app_id = app_id; shell->app_key = strdup(app_key); shell->server = strdup(server); // object to track folder location shell->folder_curr = folder_alloc(); folder_set_key(shell->folder_curr, "myfiles"); // shell commands shell->commands = commands; return shell; } int mfshell_exec(mfshell * shell, int argc, char **argv) { mfcmd *curr_cmd; for (curr_cmd = shell->commands; curr_cmd->name != NULL; curr_cmd++) { if (strcmp(argv[0], curr_cmd->name) == 0) { return curr_cmd->handler(shell, argc, argv); } } return -1; } int mfshell_exec_shell_command(mfshell * shell, char *command) { wordexp_t p; int retval; if (shell == NULL) return -1; if (command == NULL) return -1; // FIXME: handle non-zero return value of wordexp retval = wordexp(command, &p, WRDE_SHOWERR | WRDE_UNDEF); if (retval != 0) { switch (retval) { case WRDE_BADCHAR: fprintf(stderr, "wordexp: WRDE_BADCHAR\n"); break; case WRDE_BADVAL: fprintf(stderr, "wordexp: WRDE_BADVAL\n"); break; case WRDE_CMDSUB: fprintf(stderr, "wordexp: WRDE_CMDSUB\n"); break; case WRDE_NOSPACE: fprintf(stderr, "wordexp: WRDE_NOSPACE\n"); break; case WRDE_SYNTAX: fprintf(stderr, "wordexp: WRDE_SYNTAX\n"); break; } } if (p.we_wordc < 1) return 0; if (p.we_wordv[0] == NULL) return 0; // TODO: handle retval retval = mfshell_exec(shell, p.we_wordc, p.we_wordv); wordfree(&p); return 0; } void mfshell_destroy(mfshell * shell) { free(shell->app_key); free(shell->server); free(shell->local_working_dir); folder_free(shell->folder_curr); if (shell->conn != NULL) mfconn_destroy(shell->conn); free(shell); }