testc.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif /* HAVE_CONFIG_H */
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <string.h>
  8. #if WITH_INTERNAL_GETOPT
  9. #include "libc/getopt.h"
  10. #else
  11. #ifdef HAVE_GETOPT_H
  12. #include <getopt.h>
  13. #endif
  14. #endif
  15. #if HAVE_LOCALE_H
  16. #include <locale.h>
  17. #endif
  18. #if HAVE_LANGINFO_CODESET
  19. #include <langinfo.h>
  20. #endif
  21. #include "faux/faux.h"
  22. #include "faux/str.h"
  23. #include "faux/list.h"
  24. #ifndef VERSION
  25. #define VERSION 1.0.0
  26. #endif
  27. #define QUOTE(t) #t
  28. #define version(v) printf("%s\n", v)
  29. // Command line options */
  30. struct opts_s {
  31. int debug;
  32. faux_list_t *so_list;
  33. };
  34. typedef struct opts_s opts_t;
  35. static opts_t *opts_parse(int argc, char *argv[]);
  36. static void opts_free(opts_t *opts);
  37. static void help(int status, const char *argv0);
  38. int main(int argc, char *argv[]) {
  39. opts_t *opts = NULL;
  40. #if HAVE_LOCALE_H
  41. // Set current locale
  42. setlocale(LC_ALL, "");
  43. #endif
  44. opts = opts_parse(argc, argv);
  45. if (!opts) {
  46. fprintf(stderr, "Error: Can't parse command line options\n");
  47. return -1;
  48. }
  49. opts_free(opts);
  50. return 0;
  51. }
  52. static void opts_free(opts_t *opts) {
  53. assert(opts);
  54. if (!opts)
  55. return;
  56. faux_list_free(opts->so_list);
  57. faux_free(opts);
  58. }
  59. static opts_t *opts_new(void) {
  60. opts_t *opts = NULL;
  61. opts = faux_zmalloc(sizeof(*opts));
  62. assert(opts);
  63. if (!opts)
  64. return NULL;
  65. opts->debug = BOOL_FALSE;
  66. // Members of list are static strings from argv so don't free() it
  67. opts->so_list = faux_list_new(NULL, NULL);
  68. if (!opts->so_list) {
  69. opts_free(opts);
  70. return NULL;
  71. }
  72. return opts;
  73. }
  74. static opts_t *opts_parse(int argc, char *argv[]) {
  75. opts_t *opts = NULL;
  76. static const char *shortopts = "hvd";
  77. #ifdef HAVE_GETOPT_LONG
  78. static const struct option longopts[] = {
  79. {"help", 0, NULL, 'h'},
  80. {"version", 0, NULL, 'v'},
  81. {"debug", 0, NULL, 'd'},
  82. {NULL, 0, NULL, 0}
  83. };
  84. #endif
  85. opts = opts_new();
  86. if (!opts)
  87. return NULL;
  88. optind = 1;
  89. while (1) {
  90. int opt;
  91. #ifdef HAVE_GETOPT_LONG
  92. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  93. #else
  94. opt = getopt(argc, argv, shortopts);
  95. #endif
  96. if (-1 == opt)
  97. break;
  98. switch (opt) {
  99. case 'd':
  100. opts->debug = BOOL_TRUE;
  101. break;
  102. case 'h':
  103. help(0, argv[0]);
  104. exit(0);
  105. break;
  106. case 'v':
  107. version(VERSION);
  108. exit(0);
  109. break;
  110. default:
  111. help(-1, argv[0]);
  112. exit(-1);
  113. break;
  114. }
  115. }
  116. if (optind < argc) {
  117. int i;
  118. for (i = argc - 1; i >= optind; i--)
  119. faux_list_add_uniq(opts->so_list, argv[i]);
  120. } else {
  121. help(-1, argv[0]);
  122. exit(-1);
  123. }
  124. return opts;
  125. }
  126. static void help(int status, const char *argv0) {
  127. const char *name = NULL;
  128. if (!argv0)
  129. return;
  130. /* Find the basename */
  131. name = strrchr(argv0, '/');
  132. if (name)
  133. name++;
  134. else
  135. name = argv0;
  136. if (status != 0) {
  137. fprintf(stderr, "Try `%s -h' for more information.\n",
  138. name);
  139. } else {
  140. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  141. printf("Unit test helper for C code.\n");
  142. printf("Options:\n");
  143. printf("\t-v, --version\tPrint version.\n");
  144. printf("\t-h, --help\tPrint this help.\n");
  145. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  146. }
  147. }