PageRenderTime 59ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/soslim/cmdline.c

https://github.com/NameLessJedi/android_build
C | 141 lines | 122 code | 14 blank | 5 comment | 9 complexity | 353af50939b39467e3846376824956b6 MD5 | raw file
  1. #include <debug.h>
  2. #include <cmdline.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <getopt.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. extern char *optarg;
  9. extern int optind, opterr, optopt;
  10. static struct option long_options[] =
  11. {
  12. {"verbose", no_argument, 0, 'V'},
  13. {"quiet", no_argument, 0, 'Q'},
  14. {"shady", no_argument, 0, 'S'},
  15. {"print", no_argument, 0, 'p'},
  16. {"help", no_argument, 0, 'h'},
  17. {"outfile", required_argument, 0, 'o'},
  18. {"filter", required_argument, 0, 'f'},
  19. {"dry", no_argument, 0, 'n'},
  20. {"strip", no_argument, 0, 's'},
  21. {0, 0, 0, 0},
  22. };
  23. /* This array must parallel long_options[] */
  24. static
  25. const char *descriptions[sizeof(long_options)/sizeof(long_options[0])] = {
  26. "print verbose output",
  27. "suppress errors and warnings",
  28. "patch ABS symbols whose values coincide with section starts and ends",
  29. "print the symbol table (if specified, only -V is allowed)",
  30. "this help screen",
  31. "specify an output file (if not provided, input file is modified)",
  32. "specify a symbol-filter file",
  33. "dry run (perform all calculations but do not modify the ELF file)",
  34. "strip debug sections, if they are present"
  35. };
  36. void print_help(void)
  37. {
  38. fprintf(stdout,
  39. "invokation:\n"
  40. "\tsoslim file1 [file2 file3 ... fileN] [-Ldir1 -Ldir2 ... -LdirN] "
  41. "[-Vpn]\n"
  42. "or\n"
  43. "\tsoslim -h\n\n");
  44. fprintf(stdout, "options:\n");
  45. struct option *opt = long_options;
  46. const char **desc = descriptions;
  47. while (opt->name) {
  48. fprintf(stdout, "\t-%c/--%-15s %s\n",
  49. opt->val,
  50. opt->name,
  51. *desc);
  52. opt++;
  53. desc++;
  54. }
  55. }
  56. int get_options(int argc, char **argv,
  57. char **outfile,
  58. char **symsfile,
  59. int *print_symtab,
  60. int *verbose,
  61. int *quiet,
  62. int *shady,
  63. int *dry_run,
  64. int *strip_debug)
  65. {
  66. int c;
  67. ASSERT(outfile);
  68. *outfile = NULL;
  69. ASSERT(symsfile);
  70. *symsfile = NULL;
  71. ASSERT(print_symtab);
  72. *print_symtab = 0;
  73. ASSERT(verbose);
  74. *verbose = 0;
  75. ASSERT(quiet);
  76. *quiet = 0;
  77. ASSERT(shady);
  78. *shady = 0;
  79. ASSERT(dry_run);
  80. *dry_run = 0;
  81. ASSERT(strip_debug);
  82. *strip_debug = 0;
  83. while (1) {
  84. /* getopt_long stores the option index here. */
  85. int option_index = 0;
  86. c = getopt_long (argc, argv,
  87. "QVSphi:o:y:Y:f:ns",
  88. long_options,
  89. &option_index);
  90. /* Detect the end of the options. */
  91. if (c == -1) break;
  92. if (isgraph(c)) {
  93. INFO ("option -%c with value `%s'\n", c, (optarg ?: "(null)"));
  94. }
  95. #define SET_STRING_OPTION(name) do { \
  96. ASSERT(optarg); \
  97. *name = strdup(optarg); \
  98. } while(0)
  99. switch (c) {
  100. case 0:
  101. /* If this option set a flag, do nothing else now. */
  102. if (long_options[option_index].flag != 0)
  103. break;
  104. INFO ("option %s", long_options[option_index].name);
  105. if (optarg)
  106. INFO (" with arg %s", optarg);
  107. INFO ("\n");
  108. break;
  109. case 'p': *print_symtab = 1; break;
  110. case 'h': print_help(); exit(1); break;
  111. case 'V': *verbose = 1; break;
  112. case 'Q': *quiet = 1; break;
  113. case 'S': *shady = 1; break;
  114. case 'n': *dry_run = 1; break;
  115. case 's': *strip_debug = 1; break;
  116. case 'o': SET_STRING_OPTION(outfile); break;
  117. case 'f': SET_STRING_OPTION(symsfile); break;
  118. case '?':
  119. /* getopt_long already printed an error message. */
  120. break;
  121. #undef SET_STRING_OPTION
  122. default:
  123. FAILIF(1, "Unknown option");
  124. }
  125. }
  126. return optind;
  127. }