Refactor commandline handling

- instead of having the list of commandline arguments, their handlers
   and their help strings in different places, unify them in a single
   array
 - this allows to only change one piece of code when adding new
   arguments
This commit is contained in:
josch
2014-09-16 12:40:52 +02:00
parent e8f3164374
commit 08b808f893
20 changed files with 199 additions and 177 deletions

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -34,13 +35,18 @@ static char*
_get_passwd_from_user(void);
int
mfshell_cmd_auth(mfshell_t *mfshell)
mfshell_cmd_auth(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
if(mfshell == NULL) return -1;
if(mfshell->server == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
// free and invalidate existing user name
if(mfshell->user != NULL)
{

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,14 +28,22 @@
#include "command.h"
int
mfshell_cmd_chdir(mfshell_t *mfshell,const char *folderkey)
mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv)
{
folder_t *folder_new;
const char *folder_curr;
const char *folder_parent;
const char *folderkey;
int retval;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folderkey = argv[1];
if(folderkey == NULL) return -1;
// change to root

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,8 +28,13 @@
#include "command.h"
int
mfshell_cmd_debug(mfshell_t *mfshell)
mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv)
{
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
printf(" %-15.15s %s\n\r",
"server:",
mfshell->server);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -25,14 +26,22 @@
#include "private.h"
int
mfshell_cmd_file(mfshell_t *mfshell,const char *quickkey)
mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
const char *quickkey;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -29,15 +30,23 @@
#include "download.h"
int
mfshell_cmd_get(mfshell_t *mfshell,const char *quickkey)
mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
ssize_t bytes_read;
const char *quickkey;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -26,8 +27,8 @@
#include "private.h"
#include "command.h"
void
mfshell_cmd_help(void)
int
mfshell_cmd_help(mfshell_t *mfshell, int argc, char **argv)
{
printf(
" arguments:\n\r"
@@ -36,26 +37,22 @@ mfshell_cmd_help(void)
printf("\n\r");
printf(
" help show this help\n\r"
" debug show debug information\n\r"
int column1_width = 0;
int column2_width = 0;
" host <server> change target server\n\r"
" auth authenticate with active server\n\r"
" whoami show basic user info\n\r"
_cmd_t* curr_cmd;
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
if (strlen(curr_cmd->name) > column1_width)
column1_width = strlen(curr_cmd->name);
if (strlen(curr_cmd->argstring) > column2_width)
column2_width = strlen(curr_cmd->argstring);
}
" ls show contents of active folder\n\r"
" cd [folderkey] change active folder\n\r"
" pwd show the active folder\n\r"
" lpwd show the local working directory\n\r"
" lcd [dir] change the local working directory\n\r"
" mkdir [folder name] create a new folder\n\r"
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
printf("%*s %*s %s\n", column1_width, curr_cmd->name, column2_width, curr_cmd->argstring, curr_cmd->help);
}
" file [quickkey] show file information\n\r"
" links [quickkey] show access urls for the file\n\r"
" get [quickkey] download a file\n\r");
return;
return 0;
}

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -30,16 +31,22 @@ static char*
_get_host_from_user(void);
int
mfshell_cmd_host(mfshell_t *mfshell,const char *host)
mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv)
{
char *alt_host = NULL;
char *host;
if(mfshell == NULL) return -1;
if(host == NULL)
{
alt_host = _get_host_from_user();
host = alt_host;
switch (argc) {
case 1:
alt_host = _get_host_from_user();
host = alt_host;
break;
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
if(mfshell->server != NULL)

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,11 +28,19 @@
#include "mfshell.h"
int
mfshell_cmd_lcd(mfshell_t *mfshell,const char *dir)
mfshell_cmd_lcd(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *dir;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
dir = argv[1];
if(dir == NULL) return -1;
if(strlen(dir) < 1) return -1;

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -25,14 +26,22 @@
#include "private.h"
int
mfshell_cmd_links(mfshell_t *mfshell,const char *quickkey)
mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
file_t *file;
int len;
int retval;
const char *quickkey;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
quickkey = argv[1];
if(quickkey == NULL) return -1;
len = strlen(quickkey);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,13 +28,18 @@
#include "command.h"
int
mfshell_cmd_list(mfshell_t *mfshell)
mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *folder_curr;
if(mfshell == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folder_curr = folder_get_key(mfshell->folder_curr);
// safety check... this should never happen

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,13 +28,18 @@
#include "private.h"
int
mfshell_cmd_lpwd(mfshell_t *mfshell)
mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv)
{
extern int term_width;
int trim_size;
if(mfshell == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of argumens\n");
return -1;
}
if(mfshell->local_working_dir == NULL)
{
mfshell->local_working_dir = (char*)calloc(PATH_MAX + 1,sizeof(char));

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,13 +28,22 @@
#include "command.h"
int
mfshell_cmd_mkdir(mfshell_t *mfshell,const char *name)
mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
const char *folder_curr;
const char *name;
if(mfshell == NULL) return -1;
if (argc != 2) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
name = argv[1];
if (name == NULL) return -1;
folder_curr = folder_get_key(mfshell->folder_curr);
// safety check... this should never happen

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,7 +28,7 @@
#include "command.h"
int
mfshell_cmd_pwd(mfshell_t *mfshell)
mfshell_cmd_pwd(mfshell_t *mfshell, int argc, char **argv)
{
const char *folder_name;
char *folder_name_tmp = NULL;
@@ -35,6 +36,11 @@ mfshell_cmd_pwd(mfshell_t *mfshell)
if(mfshell == NULL) return -1;
if(mfshell->folder_curr == NULL) return -1;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
folder_name = folder_get_name(mfshell->folder_curr);
if(folder_name[0] == '\0') return -1;

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -27,10 +28,15 @@
#include "command.h"
int
mfshell_cmd_whoami(mfshell_t *mfshell)
mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv)
{
int retval;
if (argc != 1) {
fprintf(stderr, "Invalid number of arguments\n");
return -1;
}
retval = mfshell->user_get_info(mfshell);
mfshell->update_secret_key(mfshell);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -22,32 +23,34 @@
#include "mfshell.h"
void mfshell_cmd_help(void);
int mfshell_cmd_help(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_debug(mfshell_t *mfshell);
int mfshell_cmd_debug(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_host(mfshell_t *mfshell,const char *host);
int mfshell_cmd_host(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_auth(mfshell_t *mfshell);
int mfshell_cmd_auth(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_whomai(mfshell_t *mfshell);
int mfshell_cmd_whomai(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_list(mfshell_t *mfshell);
int mfshell_cmd_list(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_chdir(mfshell_t *mfshell,const char *folderkey);
int mfshell_cmd_chdir(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_pwd(mfshell_t *mfshell);
int mfshell_cmd_pwd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_lpwd(mfshell_t *mfshell);
int mfshell_cmd_lpwd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_lcd(mfshell_t *mfshell,const char *dir);
int mfshell_cmd_lcd(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_file(mfshell_t *mfshell,const char *quickkey);
int mfshell_cmd_file(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_links(mfshell_t *mfshell,const char *quickkey);
int mfshell_cmd_links(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_mkdir(mfshell_t *mfshell,const char *name);
int mfshell_cmd_mkdir(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_get(mfshell_t *mfshell,const char *quickkey);
int mfshell_cmd_get(mfshell_t *mfshell, int argc, char **argv);
int mfshell_cmd_whoami(mfshell_t *mfshell, int argc, char **argv);
#endif

100
console.c
View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -124,99 +125,12 @@ _execute_shell_command(mfshell_t *mfshell,char *command)
argc = stringv_len(argv);
if(strcmp(argv[0],"whoami") == 0)
{
retval = mfshell->user_get_info(mfshell);
mfshell->update_secret_key(mfshell);
}
if(strcmp(argv[0],"help") == 0)
{
mfshell->help();
}
if(strcmp(argv[0],"host") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->host(mfshell,argv[1]);
}
else
{
retval = mfshell->host(mfshell,NULL);
}
}
if(strcmp(argv[0],"debug") == 0)
{
retval = mfshell->debug(mfshell);
}
if((strcmp(argv[0],"ls") == 0) || strcmp(argv[0],"list") == 0)
{
retval = mfshell->list(mfshell);
}
if((strcmp(argv[0],"cd") == 0) || strcmp(argv[0],"chdir") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->chdir(mfshell,argv[1]);
}
}
if(strcmp(argv[0],"pwd") == 0)
{
retval = mfshell->pwd(mfshell);
}
if(strcmp(argv[0],"lpwd") == 0)
{
retval = mfshell->lpwd(mfshell);
}
if(strcmp(argv[0],"lcd") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->lcd(mfshell,argv[1]);
}
}
if(strcmp(argv[0],"file") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->file(mfshell,argv[1]);
}
}
if(strcmp(argv[0],"links") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->links(mfshell,argv[1]);
}
}
if(strcmp(argv[0],"get") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->get(mfshell,argv[1]);
}
}
if(strcmp(argv[0],"auth") == 0)
{
retval = mfshell->auth(mfshell);
}
if(strcmp(argv[0],"mkdir") == 0)
{
if(argc > 1 && argv[1] != '\0')
{
retval = mfshell->mkdir(mfshell,argv[1]);
_cmd_t* curr_cmd;
for (curr_cmd = mfshell->commands; curr_cmd->name != NULL; curr_cmd++) {
if (strcmp(argv[0], curr_cmd->name) == 0) {
// TODO: handle retval
retval = curr_cmd->handler(mfshell, argc, argv);
break;
}
}

3
main.c
View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -67,7 +68,7 @@ int main(int argc,char **argv)
"2c6dq0gb2sr8rgsue5a347lzpjnaay46yjazjcjg",server);
printf("\n\r");
mfshell->auth(mfshell);
mfshell->exec(mfshell, "auth");
// begin shell mode
mfshell_run(mfshell);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -37,6 +38,28 @@
#include "file_links.h"
#include "folder_create.h"
struct _cmd_s commands[] = {
{"help", "", "show this help", mfshell_cmd_help},
{"debug", "", "show debug information", mfshell_cmd_debug},
{"host", "<server>", "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},
{NULL, NULL, NULL, NULL}
};
mfshell_t*
mfshell_create(int app_id,char *app_key,char *server)
@@ -68,21 +91,6 @@ mfshell_create(int app_id,char *app_key,char *server)
mfshell->exec = _execute_shell_command;
// console commands
mfshell->debug = mfshell_cmd_debug;
mfshell->list = mfshell_cmd_list;
mfshell->chdir = mfshell_cmd_chdir;
mfshell->pwd = mfshell_cmd_pwd;
mfshell->help = mfshell_cmd_help;
mfshell->file = mfshell_cmd_file;
mfshell->links = mfshell_cmd_links;
mfshell->host = mfshell_cmd_host;
mfshell->auth = mfshell_cmd_auth;
mfshell->lpwd = mfshell_cmd_lpwd;
mfshell->lcd = mfshell_cmd_lcd;
mfshell->mkdir = mfshell_cmd_mkdir;
mfshell->get = mfshell_cmd_get;
// configure REST API callbacks
mfshell->get_session_token = _get_session_token;
mfshell->user_get_info = _user_get_info;
@@ -97,6 +105,9 @@ mfshell_create(int app_id,char *app_key,char *server)
mfshell->folder_curr = folder_alloc();
folder_set_key(mfshell->folder_curr,"myfiles");
// shell commands
mfshell->commands = commands;
return mfshell;
}

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -23,6 +24,7 @@
typedef struct _mfshell_s mfshell_t;
typedef struct _folder_s folder_t;
typedef struct _file_s file_t;
typedef struct _cmd_s cmd_t;
mfshell_t* mfshell_create(int app_id,char *app_key,char *server);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Bryan Christ <bryan.christ@mediafire.com>
* 2014 Johannes Schauer <j.schauer@email.de>
*
* 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
@@ -25,6 +26,15 @@
typedef struct _mfshell_s _mfshell_t;
typedef struct _folder_s _folder_t;
typedef struct _file_s _file_t;
typedef struct _cmd_s _cmd_t;
struct _cmd_s
{
char *name;
char *argstring;
char *help;
int (*handler) (_mfshell_t *mfshell, int argc, char **argv);
};
struct _folder_s
{
@@ -76,22 +86,6 @@ struct _mfshell_s
int (*exec) (_mfshell_t*,char*);
/* console commands */
void (*help) (void);
int (*debug) (_mfshell_t*);
int (*whoami) (_mfshell_t*);
int (*list) (_mfshell_t*);
int (*chdir) (_mfshell_t*,const char*);
int (*pwd) (_mfshell_t*);
int (*file) (_mfshell_t*,const char*);
int (*links) (_mfshell_t*,const char*);
int (*host) (_mfshell_t*,const char*);
int (*auth) (_mfshell_t*);
int (*get) (_mfshell_t*,const char*);
int (*lpwd) (_mfshell_t*);
int (*lcd) (_mfshell_t*,const char*);
int (*mkdir) (_mfshell_t*,const char*);
/* REST API calls */
int (*get_session_token) (_mfshell_t*);
@@ -110,6 +104,8 @@ struct _mfshell_s
/* Local tracking */
char *local_working_dir;
/* shell commands */
_cmd_t *commands;
};
void