Rizin
unix-like reverse engineering framework and cli tools
args.h File Reference

Argument parsing. More...

Go to the source code of this file.

Classes

struct  args_info
 

Functions

void args_parse (args_info *args, int argc, char **argv)
 
void args_free (void)
 

Variables

bool opt_stdout
 
bool opt_force
 
bool opt_keep_original
 
bool opt_robot
 
bool opt_ignore_check
 
const char stdin_filename []
 

Detailed Description

Argument parsing.

Definition in file args.h.

Function Documentation

◆ args_free()

void args_free ( void  )

Definition at line 695 of file args.c.

696 {
698  return;
699 }
uint64_t * opt_block_list
Definition: coder.c:29
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and opt_block_list.

Referenced by main().

◆ args_parse()

void args_parse ( args_info args,
int  argc,
char **  argv 
)

Definition at line 592 of file args.c.

593 {
594  // Initialize those parts of *args that we need later.
595  args->files_name = NULL;
596  args->files_file = NULL;
597  args->files_delim = '\0';
598 
599  // Check how we were called.
600  {
601  // Remove the leading path name, if any.
602  const char *name = strrchr(argv[0], '/');
603  if (name == NULL)
604  name = argv[0];
605  else
606  ++name;
607 
608  // NOTE: It's possible that name[0] is now '\0' if argv[0]
609  // is weird, but it doesn't matter here.
610 
611  // Look for full command names instead of substrings like
612  // "un", "cat", and "lz" to reduce possibility of false
613  // positives when the programs have been renamed.
614  if (strstr(name, "xzcat") != NULL) {
616  opt_stdout = true;
617  } else if (strstr(name, "unxz") != NULL) {
619  } else if (strstr(name, "lzcat") != NULL) {
622  opt_stdout = true;
623  } else if (strstr(name, "unlzma") != NULL) {
626  } else if (strstr(name, "lzma") != NULL) {
628  }
629  }
630 
631  // First the flags from the environment
632  parse_environment(args, argv[0], "XZ_DEFAULTS");
633  parse_environment(args, argv[0], "XZ_OPT");
634 
635  // Then from the command line
636  parse_real(args, argc, argv);
637 
638  // If encoder or decoder support was omitted at build time,
639  // show an error now so that the rest of the code can rely on
640  // that whatever is in opt_mode is also supported.
641 #ifndef HAVE_ENCODERS
642  if (opt_mode == MODE_COMPRESS)
643  message_fatal(_("Compression support was disabled "
644  "at build time"));
645 #endif
646 #ifndef HAVE_DECODERS
647  // Even MODE_LIST cannot work without decoder support so MODE_COMPRESS
648  // is the only valid choice.
649  if (opt_mode != MODE_COMPRESS)
650  message_fatal(_("Decompression support was disabled "
651  "at build time"));
652 #endif
653 
654  // Never remove the source file when the destination is not on disk.
655  // In test mode the data is written nowhere, but setting opt_stdout
656  // will make the rest of the code behave well.
657  if (opt_stdout || opt_mode == MODE_TEST) {
658  opt_keep_original = true;
659  opt_stdout = true;
660  }
661 
662  // When compressing, if no --format flag was used, or it
663  // was --format=auto, we compress to the .xz format.
666 
667  // Compression settings need to be validated (options themselves and
668  // their memory usage) when compressing to any file format. It has to
669  // be done also when uncompressing raw data, since for raw decoding
670  // the options given on the command line are used to know what kind
671  // of raw data we are supposed to decode.
674 
675  // If no filenames are given, use stdin.
676  if (argv[optind] == NULL && args->files_name == NULL) {
677  // We don't modify or free() the "-" constant. The caller
678  // modifies this so don't make the struct itself const.
679  static char *names_stdin[2] = { (char *)"-", NULL };
680  args->arg_names = names_stdin;
681  args->arg_count = 1;
682  } else {
683  // We got at least one filename from the command line, or
684  // --files or --files0 was specified.
685  args->arg_names = argv + optind;
686  args->arg_count = (unsigned int)(argc - optind);
687  }
688 
689  return;
690 }
bool opt_keep_original
Definition: args.c:23
static void parse_real(args_info *args, int argc, char **argv)
Definition: args.c:120
bool opt_stdout
Definition: args.c:21
static void parse_environment(args_info *args, char *argv0, const char *varname)
Definition: args.c:511
int optind
Definition: getopt.h:6
void coder_set_compression_settings(void)
Definition: coder.c:137
enum format_type opt_format
Definition: coder.c:25
enum operation_mode opt_mode
Definition: coder.c:24
@ MODE_COMPRESS
Definition: coder.h:14
@ MODE_TEST
Definition: coder.h:16
@ MODE_DECOMPRESS
Definition: coder.h:15
@ FORMAT_RAW
Definition: coder.h:27
@ FORMAT_AUTO
Definition: coder.h:23
@ FORMAT_XZ
Definition: coder.h:24
@ FORMAT_LZMA
Definition: coder.h:25
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
void message_fatal(const char *fmt,...)
Definition: message.c:777
int args
Definition: mipsasm.c:18
const char * name
Definition: op.c:541
#define _(String)
Definition: opintl.h:53
static int
Definition: sfsocketcall.h:114
Definition: z80asm.h:102

References _, args, argv, coder_set_compression_settings(), FORMAT_AUTO, FORMAT_LZMA, FORMAT_RAW, FORMAT_XZ, int, message_fatal(), MODE_COMPRESS, MODE_DECOMPRESS, MODE_TEST, name, NULL, opt_format, opt_keep_original, opt_mode, opt_stdout, optind, parse_environment(), and parse_real().

Referenced by main().

Variable Documentation

◆ opt_force

bool opt_force
extern

Definition at line 22 of file args.c.

Referenced by io_open_dest_real(), io_open_src_real(), io_unlink(), list_file(), and parse_real().

◆ opt_ignore_check

bool opt_ignore_check
extern

Definition at line 25 of file args.c.

Referenced by parse_real().

◆ opt_keep_original

bool opt_keep_original
extern

Definition at line 23 of file args.c.

Referenced by args_parse(), io_close_src(), and parse_real().

◆ opt_robot

◆ opt_stdout

bool opt_stdout
extern

Definition at line 21 of file args.c.

Referenced by args_parse(), io_open_dest_real(), io_open_src_real(), list_file(), main(), and parse_real().

◆ stdin_filename

const char stdin_filename[]
extern

Definition at line 29 of file args.c.

Referenced by io_open_src_real(), list_file(), main(), parse_real(), and print_filename().