main.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include <pj/os.h>
  20. #include "test.h"
  21. /* Any tests that want to build a linked executable for RTEMS must include
  22. this header to get a default config for the network stack. */
  23. #if defined(PJ_RTEMS)
  24. # include <bsp.h>
  25. # include <rtems.h>
  26. # include <rtems/rtems_bsdnet.h>
  27. # include "../../../pjlib/include/rtems-network-config.h"
  28. #endif
  29. #if (PJ_LINUX || PJ_DARWINOS) && defined(PJ_HAS_EXECINFO_H) && PJ_HAS_EXECINFO_H != 0
  30. #include <execinfo.h>
  31. #include <signal.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <unistd.h>
  35. static void print_stack(int sig)
  36. {
  37. void *array[16];
  38. size_t size;
  39. size = backtrace(array, 16);
  40. fprintf(stderr, "Error: signal %d:\n", sig);
  41. backtrace_symbols_fd(array, size, STDERR_FILENO);
  42. exit(1);
  43. }
  44. static void init_signals(void)
  45. {
  46. signal(SIGSEGV, &print_stack);
  47. signal(SIGABRT, &print_stack);
  48. }
  49. #else
  50. #define init_signals()
  51. #endif
  52. static int main_func(int argc, char *argv[])
  53. {
  54. int rc;
  55. int interractive = 0;
  56. int no_trap = 0;
  57. while (argc > 1) {
  58. char *arg = argv[--argc];
  59. if (*arg=='-' && *(arg+1)=='i') {
  60. interractive = 1;
  61. } else if (*arg=='-' && *(arg+1)=='n') {
  62. no_trap = 1;
  63. }
  64. }
  65. if (!no_trap) {
  66. init_signals();
  67. }
  68. rc = test_main();
  69. if (interractive) {
  70. char s[10];
  71. puts("");
  72. puts("Press <ENTER> to exit");
  73. if (!fgets(s, sizeof(s), stdin))
  74. return rc;
  75. }
  76. return rc;
  77. }
  78. int main(int argc, char *argv[])
  79. {
  80. return pj_run_app(&main_func, argc, argv, 0);
  81. }