2016-12-21 13:08:44 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2017-08-03 16:16:58 +00:00
|
|
|
#include "libp2p/utils/logger.h"
|
2016-12-21 13:08:44 +00:00
|
|
|
#include "ipfs/repo/init.h"
|
|
|
|
#include "ipfs/importer/importer.h"
|
2016-12-22 15:21:18 +00:00
|
|
|
#include "ipfs/importer/exporter.h"
|
2016-12-23 03:51:06 +00:00
|
|
|
#include "ipfs/dnslink/dnslink.h"
|
2017-02-10 02:53:58 +00:00
|
|
|
#include "ipfs/core/daemon.h"
|
2017-10-12 15:12:54 +00:00
|
|
|
#include "ipfs/core/swarm.h"
|
2017-09-21 12:51:18 +00:00
|
|
|
#include "ipfs/cmd/cli.h"
|
|
|
|
#include "ipfs/namesys/name.h"
|
2016-12-21 13:08:44 +00:00
|
|
|
|
2017-01-12 21:45:44 +00:00
|
|
|
#ifdef __MINGW32__
|
|
|
|
void bzero(void *s, size_t n)
|
|
|
|
{
|
|
|
|
memset (s, '\0', n);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *strtok_r(char *str, const char *delim, char **save)
|
|
|
|
{
|
|
|
|
char *res, *last;
|
|
|
|
|
|
|
|
if( !save )
|
|
|
|
return strtok(str, delim);
|
|
|
|
if( !str && !(str = *save) )
|
|
|
|
return NULL;
|
|
|
|
last = str + strlen(str);
|
|
|
|
if( (*save = res = strtok(str, delim)) )
|
|
|
|
{
|
|
|
|
*save += strlen(res);
|
|
|
|
if( *save < last )
|
|
|
|
(*save)++;
|
|
|
|
else
|
|
|
|
*save = NULL;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
#endif // MINGW
|
|
|
|
|
2016-12-21 13:08:44 +00:00
|
|
|
void stripit(int argc, char** argv) {
|
2017-09-21 14:47:16 +00:00
|
|
|
char* old_arg = argv[argc];
|
|
|
|
int full_length = strlen(old_arg);
|
|
|
|
char *tmp = (char*) malloc(full_length + 1);
|
2017-10-05 20:14:47 +00:00
|
|
|
if (tmp == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-21 14:47:16 +00:00
|
|
|
char* ptr1 = &old_arg[1];
|
|
|
|
strcpy(tmp, ptr1);
|
2016-12-21 13:08:44 +00:00
|
|
|
tmp[strlen(tmp)-1] = 0;
|
2017-09-21 14:47:16 +00:00
|
|
|
strcpy(old_arg, tmp);
|
|
|
|
free(tmp);
|
2016-12-21 13:08:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void strip_quotes(int argc, char** argv) {
|
|
|
|
for(int i = 0; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '\'' && argv[i][strlen(argv[i])-1] == '\'') {
|
|
|
|
stripit(i, argv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define INIT 1
|
|
|
|
#define ADD 2
|
2016-12-22 15:21:18 +00:00
|
|
|
#define OBJECT_GET 3
|
2016-12-23 03:51:06 +00:00
|
|
|
#define DNS 4
|
2016-12-23 17:53:03 +00:00
|
|
|
#define CAT 5
|
2017-02-10 01:10:21 +00:00
|
|
|
#define DAEMON 6
|
2017-02-10 02:53:58 +00:00
|
|
|
#define PING 7
|
2017-02-27 17:27:40 +00:00
|
|
|
#define GET 8
|
2017-09-21 12:51:18 +00:00
|
|
|
#define NAME 9
|
2017-10-12 15:12:54 +00:00
|
|
|
#define SWARM 10
|
2017-09-21 12:51:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find out if this command line argument is part of a switch
|
|
|
|
* @param argc the number of arguments
|
|
|
|
* @param argv the arguments
|
|
|
|
* @param index the argument to look at
|
|
|
|
* @returns 0 if not a switch, 1 if it is a regular switch, 2 if the next parameter is also part of the switch
|
|
|
|
*/
|
|
|
|
int is_switch(int argc, char** argv, int index) {
|
|
|
|
char* to_test = argv[index];
|
|
|
|
if (to_test[0] == '-') {
|
|
|
|
if (strcmp(to_test, "-c") == 0 || strcmp(to_test, "--config") == 0) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the command line piece that will actually do something
|
|
|
|
* @param argc the number of command line arguments
|
|
|
|
* @param argv the actual command line arguments
|
|
|
|
* @returns the index of the item that does something, or false(0)
|
|
|
|
*/
|
|
|
|
int get_cli_verb(int argc, char** argv) {
|
|
|
|
for(int i = 1; i < argc; i++) {
|
|
|
|
int advance_by_more_than_one = is_switch(argc, argv, i);
|
|
|
|
if (advance_by_more_than_one == 0) {
|
|
|
|
// this is the verb
|
|
|
|
return i;
|
|
|
|
} else {
|
|
|
|
if (advance_by_more_than_one == 2) {
|
|
|
|
// skip the next one
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2016-12-21 13:08:44 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Basic parsing of command line arguments to figure out where the user wants to go
|
|
|
|
*/
|
|
|
|
int parse_arguments(int argc, char** argv) {
|
2017-09-21 12:51:18 +00:00
|
|
|
int index = get_cli_verb(argc, argv);
|
|
|
|
if (argc == 1 || index == 0) {
|
|
|
|
libp2p_logger_error("main", "No parameters passed.\n");
|
2016-12-21 13:08:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("init", argv[index]) == 0) {
|
2016-12-21 13:08:44 +00:00
|
|
|
return INIT;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("add", argv[index]) == 0) {
|
2016-12-21 13:08:44 +00:00
|
|
|
return ADD;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("object", argv[index]) == 0 && argc > 2 && strcmp("get", argv[index+1]) == 0) {
|
2016-12-22 15:21:18 +00:00
|
|
|
return OBJECT_GET;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("cat", argv[index]) == 0) {
|
2016-12-23 17:53:03 +00:00
|
|
|
return CAT;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("dns", argv[index]) == 0) {
|
2016-12-23 03:51:06 +00:00
|
|
|
return DNS;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("daemon", argv[index]) == 0) {
|
2017-02-10 01:10:21 +00:00
|
|
|
return DAEMON;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("ping", argv[index]) == 0) {
|
2017-02-10 02:53:58 +00:00
|
|
|
return PING;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("get", argv[index]) == 0) {
|
2017-02-27 17:27:40 +00:00
|
|
|
return GET;
|
|
|
|
}
|
2017-09-21 12:51:18 +00:00
|
|
|
if (strcmp("name", argv[index]) == 0) {
|
|
|
|
return NAME;
|
|
|
|
}
|
2017-10-12 15:12:54 +00:00
|
|
|
if (strcmp("swarm", argv[index]) == 0) {
|
|
|
|
return SWARM;
|
|
|
|
}
|
2016-12-21 13:08:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* The beginning
|
|
|
|
*/
|
|
|
|
int main(int argc, char** argv) {
|
2017-09-27 12:02:00 +00:00
|
|
|
int retVal = 0;
|
2016-12-21 13:08:44 +00:00
|
|
|
strip_quotes(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
// CliArguments is the new way to do it. Eventually, all will use this structure
|
|
|
|
struct CliArguments* args = cli_arguments_new(argc, argv);
|
|
|
|
if (args != NULL) {
|
|
|
|
// until then, use the old way
|
2017-09-27 12:02:00 +00:00
|
|
|
int cmd = parse_arguments(argc, argv);
|
|
|
|
switch (cmd) {
|
2017-09-21 12:51:18 +00:00
|
|
|
case (INIT):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_repo_init(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (ADD):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_import_files(args);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (OBJECT_GET):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_exporter_object_get(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case(GET):
|
|
|
|
//ipfs_exporter_get(argc, argv);
|
|
|
|
//break;
|
|
|
|
case (CAT):
|
2017-10-05 18:08:36 +00:00
|
|
|
retVal = ipfs_exporter_object_cat(args, stdout);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (DNS):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_dns(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (DAEMON):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_daemon(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (PING):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_ping(argc, argv);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
|
|
|
case (NAME):
|
2017-09-27 12:02:00 +00:00
|
|
|
retVal = ipfs_name(args);
|
2017-09-21 12:51:18 +00:00
|
|
|
break;
|
2017-10-12 15:12:54 +00:00
|
|
|
case (SWARM):
|
|
|
|
retVal = ipfs_swarm(args);
|
|
|
|
break;
|
2017-09-21 12:51:18 +00:00
|
|
|
default:
|
|
|
|
libp2p_logger_error("main", "Invalid command line arguments.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cli_arguments_free(args);
|
2016-12-21 13:08:44 +00:00
|
|
|
}
|
2017-08-03 16:16:58 +00:00
|
|
|
libp2p_logger_free();
|
2017-09-27 12:02:00 +00:00
|
|
|
exit(retVal == 1 ? EXIT_SUCCESS : EXIT_FAILURE);
|
2016-12-21 13:08:44 +00:00
|
|
|
}
|