diff --git a/src/main.c b/src/main.c index 97921ed..1891324 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,7 @@ static_assert((sizeof(long long) >= 8), "Size of long long is less than 8, cannot compile"); void usage() { - fprintf(stderr, "Usage: " PROGRAM_NAME " [-h | -i | -s] [-n] [-v data_path] [--] .torrent_file...\n"); + fprintf(stderr, "Usage: " PROGRAM_NAME " [-h | -i | -s | -f CHAR] [-n] [-v data_path] [--] .torrent_file...\n"); exit(EXIT_FAILURE); } @@ -30,6 +30,8 @@ void help() { " -v PATH verify the torrent file, pass in the path of the files\n" " -s don't write any output\n" " -n Don't use torrent name as a folder when verifying\n" +" -f CHAR Show info from the .torrent file, as an input for a script\n" +" Valid CHARs are: i - Info hash\n" "\n" "EXIT CODE\n" " If no error, exit code is 0. In verify mode exit code is 0 if it's\n" @@ -56,12 +58,6 @@ int main(int argc, char** argv) { fprintf(stderr, "Provide at least one torrent file\n"); usage(); } - /* - if (data_path && optind != argc - 1) { - fprintf(stderr, "If used with -v, only input 1 bittorrent file\n"); - usage(); - } - */ int exit_code = EXIT_SUCCESS; for (int i = optind; i < argc; i++) { @@ -74,6 +70,10 @@ int main(int argc, char** argv) { showinfo(&m); } + if (opt_scriptformat_info != OPT_SCRIPTFORMAT_NONE) { + showinfo_script(&m); + } + if (opt_data_path) { /* Verify */ int verify_result = verify(&m, opt_data_path, !opt_no_use_dir); if (verify_result != 0) { diff --git a/src/opts.c b/src/opts.c index eb88021..d1bdc3f 100644 --- a/src/opts.c +++ b/src/opts.c @@ -1,17 +1,19 @@ #include "opts.h" #include +#include int opt_silent = 0; int opt_showinfo = 0; int opt_help = 0; int opt_no_use_dir = 0; int opt_pretty_progress = 0; +int opt_scriptformat_info = OPT_SCRIPTFORMAT_NONE; char* opt_data_path = NULL; int opts_parse(int argc, char** argv) { int opt; - while ((opt = getopt(argc, argv, "pnihsv:")) != -1) { + while ((opt = getopt(argc, argv, "pnihsv:f:")) != -1) { switch (opt) { case 'i': opt_showinfo = 1; @@ -31,6 +33,21 @@ int opts_parse(int argc, char** argv) { case 'v': opt_data_path = optarg; break; + case 'f': + if (strlen(optarg) != 1) + return -1; + char info_char = optarg[0]; + opt_scriptformat_info = OPT_SCRIPTFORMAT_INVALID; + for (int i = 0; i < OPT_SCRIPTFORMAT_MAPPING_LEN; i++) { + const opt_scriptformat_mapping_t* curr = &OPT_SCRIPTFORMAT_MAPPING[i]; + if (info_char == curr->info_char) { + opt_scriptformat_info = curr->info; + break; + } + } + if (opt_scriptformat_info == OPT_SCRIPTFORMAT_INVALID) + return -1; + break; default: return -1; } diff --git a/src/opts.h b/src/opts.h index 3d67302..0121d68 100644 --- a/src/opts.h +++ b/src/opts.h @@ -1,11 +1,29 @@ #ifndef OPTS_H #define OPTS_H +enum OPT_SCRIPTFORMAT { + OPT_SCRIPTFORMAT_INVALID = -1, + OPT_SCRIPTFORMAT_NONE, + OPT_SCRIPTFORMAT_INFOHASH, +}; + +typedef struct { + char info_char; + enum OPT_SCRIPTFORMAT info; +} opt_scriptformat_mapping_t; + +const static opt_scriptformat_mapping_t OPT_SCRIPTFORMAT_MAPPING[] = { + { .info_char = 'i', .info = OPT_SCRIPTFORMAT_INFOHASH }, +}; +#define OPT_SCRIPTFORMAT_MAPPING_LEN sizeof(OPT_SCRIPTFORMAT_MAPPING)/sizeof(OPT_SCRIPTFORMAT_MAPPING[0]) + + extern int opt_silent; extern int opt_showinfo; extern int opt_help; extern int opt_no_use_dir; extern int opt_pretty_progress; +extern int opt_scriptformat_info; extern char* opt_data_path; /* Parse the given arguments. Return -1 if error */ diff --git a/src/showinfo.c b/src/showinfo.c index 6a8fe00..f6313c9 100644 --- a/src/showinfo.c +++ b/src/showinfo.c @@ -3,6 +3,7 @@ #include #include "showinfo.h" #include "util.h" +#include "opts.h" void showinfo(metainfo_t* m) { const char* s; @@ -95,3 +96,20 @@ void showinfo(metainfo_t* m) { strncpy(str_buff, "err", sizeof(str_buff)); printf("Total size: %s\n", str_buff); } + +void showinfo_script(metainfo_t* m) { + switch (opt_scriptformat_info) { + case OPT_SCRIPTFORMAT_INFOHASH: + { + char hex_str[sizeof(sha1sum_t)*2+1]; + const sha1sum_t* infohash = metainfo_infohash(m); + util_byte2hex((const unsigned char*)infohash, sizeof(sha1sum_t), 0, hex_str); + printf("%s\n", hex_str); + break; + } + default: + printf("Unknown?? [>.<]\n"); + break; + } +} + diff --git a/src/showinfo.h b/src/showinfo.h index a521a8d..a93172c 100644 --- a/src/showinfo.h +++ b/src/showinfo.h @@ -7,4 +7,10 @@ */ void showinfo(metainfo_t* m); +/* + * Print the selected info from the metainfo file + * to be readable by a script + */ +void showinfo_script(metainfo_t* m); + #endif