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-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);
|
|
|
|
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
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
2016-12-21 13:08:44 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* The beginning
|
|
|
|
*/
|
|
|
|
int main(int argc, char** argv) {
|
2017-08-03 16:16:58 +00:00
|
|
|
// for debugging
|
|
|
|
libp2p_logger_add_class("null");
|
|
|
|
libp2p_logger_add_class("bitswap");
|
|
|
|
libp2p_logger_add_class("secio");
|
|
|
|
libp2p_logger_add_class("peer_request_queue");
|
|
|
|
libp2p_logger_add_class("bitswap_engine");
|
|
|
|
libp2p_logger_add_class("peerstore");
|
2017-08-16 12:08:39 +00:00
|
|
|
libp2p_logger_add_class("dht_protocol");
|
|
|
|
libp2p_logger_add_class("peer");
|
2017-08-21 19:49:21 +00:00
|
|
|
libp2p_logger_add_class("lmdb_datastore");
|
2017-08-03 16:16:58 +00:00
|
|
|
|
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
|
|
|
|
int retVal = parse_arguments(argc, argv);
|
|
|
|
switch (retVal) {
|
|
|
|
case (INIT):
|
|
|
|
return ipfs_repo_init(argc, argv);
|
|
|
|
break;
|
|
|
|
case (ADD):
|
|
|
|
ipfs_import_files(argc, argv);
|
|
|
|
break;
|
|
|
|
case (OBJECT_GET):
|
|
|
|
ipfs_exporter_object_get(argc, argv);
|
|
|
|
break;
|
|
|
|
case(GET):
|
|
|
|
//ipfs_exporter_get(argc, argv);
|
|
|
|
//break;
|
|
|
|
case (CAT):
|
|
|
|
ipfs_exporter_object_cat(argc, argv);
|
|
|
|
break;
|
|
|
|
case (DNS):
|
|
|
|
ipfs_dns(argc, argv);
|
|
|
|
break;
|
|
|
|
case (DAEMON):
|
|
|
|
ipfs_daemon(argc, argv);
|
|
|
|
break;
|
|
|
|
case (PING):
|
|
|
|
ipfs_ping(argc, argv);
|
|
|
|
break;
|
|
|
|
case (NAME):
|
|
|
|
ipfs_name(args);
|
|
|
|
break;
|
|
|
|
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();
|
2016-12-21 13:08:44 +00:00
|
|
|
}
|