testc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. faux_list_node_t *iter = NULL;
  41. char *so = NULL;
  42. #if HAVE_LOCALE_H
  43. // Set current locale
  44. setlocale(LC_ALL, "");
  45. #endif
  46. // Parse command line options
  47. opts = opts_parse(argc, argv);
  48. if (!opts) {
  49. fprintf(stderr, "Error: Can't parse command line options\n");
  50. return -1;
  51. }
  52. iter = faux_list_head(opts->so_list);
  53. while ((so = faux_list_each(&iter))) {
  54. printf("%s\n", so);
  55. }
  56. opts_free(opts);
  57. return 0;
  58. }
  59. static void opts_free(opts_t *opts) {
  60. assert(opts);
  61. if (!opts)
  62. return;
  63. faux_list_free(opts->so_list);
  64. faux_free(opts);
  65. }
  66. static opts_t *opts_new(void) {
  67. opts_t *opts = NULL;
  68. opts = faux_zmalloc(sizeof(*opts));
  69. assert(opts);
  70. if (!opts)
  71. return NULL;
  72. opts->debug = BOOL_FALSE;
  73. // Members of list are static strings from argv so don't free() it
  74. opts->so_list = faux_list_new(BOOL_FALSE, BOOL_TRUE,
  75. (faux_list_cmp_fn)strcmp, NULL, NULL);
  76. if (!opts->so_list) {
  77. opts_free(opts);
  78. return NULL;
  79. }
  80. return opts;
  81. }
  82. static opts_t *opts_parse(int argc, char *argv[]) {
  83. opts_t *opts = NULL;
  84. static const char *shortopts = "hvd";
  85. #ifdef HAVE_GETOPT_LONG
  86. static const struct option longopts[] = {
  87. {"help", 0, NULL, 'h'},
  88. {"version", 0, NULL, 'v'},
  89. {"debug", 0, NULL, 'd'},
  90. {NULL, 0, NULL, 0}
  91. };
  92. #endif
  93. opts = opts_new();
  94. if (!opts)
  95. return NULL;
  96. optind = 1;
  97. while (1) {
  98. int opt;
  99. #ifdef HAVE_GETOPT_LONG
  100. opt = getopt_long(argc, argv, shortopts, longopts, NULL);
  101. #else
  102. opt = getopt(argc, argv, shortopts);
  103. #endif
  104. if (-1 == opt)
  105. break;
  106. switch (opt) {
  107. case 'd':
  108. opts->debug = BOOL_TRUE;
  109. break;
  110. case 'h':
  111. help(0, argv[0]);
  112. exit(0);
  113. break;
  114. case 'v':
  115. version(VERSION);
  116. exit(0);
  117. break;
  118. default:
  119. help(-1, argv[0]);
  120. exit(-1);
  121. break;
  122. }
  123. }
  124. if (optind < argc) {
  125. int i = 0;
  126. for (i = optind; i < argc; i++)
  127. faux_list_add(opts->so_list, argv[i]);
  128. } else {
  129. help(-1, argv[0]);
  130. exit(-1);
  131. }
  132. return opts;
  133. }
  134. static void help(int status, const char *argv0) {
  135. const char *name = NULL;
  136. if (!argv0)
  137. return;
  138. /* Find the basename */
  139. name = strrchr(argv0, '/');
  140. if (name)
  141. name++;
  142. else
  143. name = argv0;
  144. if (status != 0) {
  145. fprintf(stderr, "Try `%s -h' for more information.\n",
  146. name);
  147. } else {
  148. printf("Usage: %s [options] <so_object> [so_object] ...\n", name);
  149. printf("Unit test helper for C code.\n");
  150. printf("Options:\n");
  151. printf("\t-v, --version\tPrint version.\n");
  152. printf("\t-h, --help\tPrint this help.\n");
  153. printf("\t-d, --debug\tDebug mode. Don't daemonize.\n");
  154. }
  155. }