mirror of
https://github.com/aaru-dps/libaaruformat.git
synced 2025-12-16 19:24:40 +00:00
[feature] Add command handling and usage functions for aaruformattool using argtable3
This commit is contained in:
237
tool/main.c
237
tool/main.c
@@ -17,108 +17,14 @@
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <aaruformat.h>
|
||||
|
||||
#include "aaruformattool.h"
|
||||
#include "main.h"
|
||||
|
||||
void usage()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool <verb> [arguments]\n");
|
||||
printf("\n");
|
||||
printf("Available verbs:\n");
|
||||
printf("\tidentify\tIdentifies if the indicated file is a supported AaruFormat image.\n");
|
||||
printf("\tinfo\tPrints information about a given AaruFormat image.\n");
|
||||
printf("\tread\tReads a sector and prints it out on screen.\n");
|
||||
printf("\tread_long\tReads a sector with all its prefixes and suffixes and prints it out on screen.\n");
|
||||
printf("\tverify\tVerifies the integrity of all blocks in a AaruFormat image.\n");
|
||||
printf("\tverify_sectors\tVerifies the integrity of all sectors in a AaruFormat image.\n");
|
||||
printf("\n");
|
||||
printf("For help on the verb invoke the tool with the verb and no arguments.\n");
|
||||
}
|
||||
|
||||
void usage_identify()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool identify <filename>\n");
|
||||
printf("Identifies if the indicated file is a support AaruFormat image.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<filename>\tPath to file to identify if it is a supported AaruFormat image.\n");
|
||||
}
|
||||
|
||||
void usage_info()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool info <filename>\n");
|
||||
printf("Prints information about a given AaruFormat image.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<filename>\tPath to AaruFormat image to print information from.\n");
|
||||
}
|
||||
|
||||
void usage_read()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool read <sector_number> <filename>\n");
|
||||
printf("Reads a sector and prints it out on screen.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<sector_number>\tSector number to read and print out.\n");
|
||||
printf("\t<filename>\tPath to AaruFormat image to print information from.\n");
|
||||
}
|
||||
|
||||
void usage_read_long()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool read_long <sector_number> <filename>\n");
|
||||
printf("Reads a sector with all its prefixes and suffixes and prints it out on screen.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<sector_number>\tSector number to read and print out.\n");
|
||||
printf("\t<filename>\tPath to AaruFormat image to print information from.\n");
|
||||
}
|
||||
|
||||
void usage_verify()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool verify <filename>\n");
|
||||
printf("Verifies the integrity of all blocks in a AaruFormat image.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<filename>\tPath to AaruFormat image to verify.\n");
|
||||
}
|
||||
|
||||
void usage_verify_sectors()
|
||||
{
|
||||
printf("\n");
|
||||
printf("Usage:\n");
|
||||
printf("aaruformattool verify_sectors <filename>\n");
|
||||
printf("Verifies the integrity of all sectors in a AaruFormat image.\n");
|
||||
printf("\n");
|
||||
printf("Arguments:\n");
|
||||
printf("\t<filename>\tPath to AaruFormat image to verify.\n");
|
||||
}
|
||||
#include <string.h>
|
||||
#include "commands.h"
|
||||
#include "usage.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint64_t sector_no = 0;
|
||||
|
||||
printf("AaruFormat Tool version %d.%d\n", AARUFORMAT_TOOL_MAJOR_VERSION, AARUFORMAT_TOOL_MINOR_VERSION);
|
||||
printf("Copyright (C) 2019-2022 Natalia Portillo\n");
|
||||
printf("libaaruformat version %d.%d\n", LIBAARUFORMAT_MAJOR_VERSION, LIBAARUFORMAT_MINOR_VERSION);
|
||||
printf("\n");
|
||||
print_banner();
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
@@ -126,133 +32,16 @@ int main(int argc, char *argv[])
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(strncmp(argv[1], "identify", strlen("identify")) == 0)
|
||||
const char *verb = argv[1];
|
||||
argc--;
|
||||
argv++; // Shift to pass only args to verb handlers
|
||||
|
||||
for(size_t i = 0; i < num_commands; ++i)
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
usage_identify();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 3)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_identify();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return identify(argv[2]);
|
||||
if(strcmp(commands[i].verb, verb) == 0) { return commands[i].handler(argc, argv); }
|
||||
}
|
||||
|
||||
if(strncmp(argv[1], "info", strlen("info")) == 0)
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
usage_info();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 3)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_info();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return info(argv[2]);
|
||||
}
|
||||
|
||||
if(strncmp(argv[1], "read_long", strlen("read_long")) == 0)
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
usage_read_long();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 5)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_read_long();
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
|
||||
sector_no = strtoll(argv[2], NULL, 10);
|
||||
|
||||
if(errno != 0)
|
||||
{
|
||||
fprintf(stderr, "Invalid sector number\n");
|
||||
usage_read_long();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return read_long(sector_no, argv[3]);
|
||||
}
|
||||
else if(strncmp(argv[1], "read", strlen("read")) == 0)
|
||||
{
|
||||
if(argc < 4)
|
||||
{
|
||||
usage_read();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 5)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_read();
|
||||
return -1;
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
|
||||
sector_no = strtoll(argv[2], NULL, 10);
|
||||
|
||||
if(errno != 0)
|
||||
{
|
||||
fprintf(stderr, "Invalid sector number\n");
|
||||
usage_read();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return read(sector_no, argv[3]);
|
||||
}
|
||||
|
||||
if(strncmp(argv[1], "verify_sectors", strlen("verify_sectors")) == 0)
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
usage_verify_sectors();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 3)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_verify_sectors();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return verify_sectors(argv[2]);
|
||||
}
|
||||
else if(strncmp(argv[1], "verify", strlen("verify")) == 0)
|
||||
{
|
||||
if(argc == 2)
|
||||
{
|
||||
usage_verify();
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(argc > 3)
|
||||
{
|
||||
fprintf(stderr, "Invalid number of arguments\n");
|
||||
usage_verify();
|
||||
return -1;
|
||||
}
|
||||
|
||||
return verify(argv[2]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
fprintf(stderr, "Unknown verb: %s\n", verb);
|
||||
usage();
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user