zones2indications.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2006, Digium, Inc.
  5. *
  6. * Tzafrir Cohen <tzafrir.cohen@xorcom.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. * \brief print libtonozone data as Asterisk indications.conf
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <dahdi/tonezone.h>
  24. #include <unistd.h>
  25. #define PROGRAM "zones2indication"
  26. void print_tone_zone_sound(struct ind_tone_zone *zone_data, const char* name,
  27. int toneid) {
  28. int i;
  29. for (i=0; i<DAHDI_TONE_MAX; i++) {
  30. if (zone_data->tones[i].toneid == toneid){
  31. printf("%s = %s\n", name, zone_data->tones[i].data);
  32. break;
  33. }
  34. }
  35. }
  36. void print_indications(struct ind_tone_zone *zone_data) {
  37. int i;
  38. printf (
  39. "[%s]\n"
  40. "; Source: libtonezone.\n"
  41. "description = %s\n"
  42. "\n",
  43. zone_data->country, zone_data->description
  44. );
  45. printf(
  46. "ringcadence = "
  47. );
  48. for(i=0; ; i++) {
  49. if (zone_data->ringcadence[i] == 0)
  50. break;
  51. if (i != 0)
  52. putchar(',');
  53. printf("%d",zone_data->ringcadence[i]);
  54. }
  55. putchar('\n');
  56. print_tone_zone_sound(zone_data, "dial", DAHDI_TONE_DIALTONE);
  57. print_tone_zone_sound(zone_data, "busy", DAHDI_TONE_BUSY);
  58. print_tone_zone_sound(zone_data, "ring", DAHDI_TONE_RINGTONE);
  59. print_tone_zone_sound(zone_data, "congestion", DAHDI_TONE_CONGESTION);
  60. print_tone_zone_sound(zone_data, "callwaiting", DAHDI_TONE_CALLWAIT);
  61. print_tone_zone_sound(zone_data, "dialrecall", DAHDI_TONE_DIALRECALL);
  62. print_tone_zone_sound(zone_data, "record", DAHDI_TONE_RECORDTONE);
  63. print_tone_zone_sound(zone_data, "info", DAHDI_TONE_INFO);
  64. print_tone_zone_sound(zone_data, "stutter", DAHDI_TONE_STUTTER);
  65. printf("\n\n");
  66. }
  67. int print_zone_by_id(int zone_num) {
  68. struct tone_zone *zone_data = tone_zone_find_by_num(zone_num);
  69. if (zone_data == NULL)
  70. return 1;
  71. print_indications(zone_data);
  72. return 0;
  73. }
  74. int print_zone_by_country(char* country) {
  75. struct tone_zone *zone_data = tone_zone_find(country);
  76. if (zone_data == NULL)
  77. return 1;
  78. print_indications(zone_data);
  79. return 0;
  80. }
  81. int print_all() {
  82. int i;
  83. /* loop over all possible zones */
  84. for (i=0; ; i++) {
  85. if (print_zone_by_id(i))
  86. break;
  87. }
  88. return 0;
  89. }
  90. void usage() {
  91. fprintf(stderr,
  92. PROGRAM ": print libtonozone data as Asterisk indications.conf\n"
  93. "\n"
  94. "Usage:\n"
  95. " " PROGRAM " -a Print all countries\n"
  96. " " PROGRAM " -c <code> Select country by two-letter country code\n"
  97. " " PROGRAM " -n <num> Select country by its internal libtonezone number\n"
  98. " " PROGRAM " -h Print this text.\n"
  99. );
  100. }
  101. int main(int argc, char* argv[]){
  102. int country_code = -1;
  103. int opt_print_all = 0;
  104. int opt;
  105. char* endptr = NULL;
  106. while((opt = getopt(argc, argv, "ac:hn:")) != -1) {
  107. switch(opt) {
  108. case 'a':
  109. return print_all();
  110. case 'c':
  111. return print_zone_by_country(optarg);
  112. case 'h':
  113. usage();
  114. return 0;
  115. case 'n':
  116. printf("number is %s.\n", optarg);
  117. country_code = strtol(optarg, &endptr, 10);
  118. return print_zone_by_id(country_code);
  119. /* FIXME: what if this is not a number?
  120. if (endptr != NULL) {
  121. fprintf(stderr, "Error: Invalid country code %s, %d.\n",optarg, country_code);
  122. usage();
  123. exit(1);
  124. }
  125. */
  126. break;
  127. }
  128. }
  129. /* If we got here, the user selected no option */
  130. usage();
  131. return 2;
  132. }