ptypes.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Implementation of standard PTYPEs
  3. */
  4. #include <assert.h>
  5. #include <stdio.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <errno.h>
  10. #include <faux/str.h>
  11. #include <faux/list.h>
  12. #include <faux/conv.h>
  13. #include <faux/argv.h>
  14. #include <klish/kcontext.h>
  15. #include <klish/kentry.h>
  16. /** @brief PTYPE: Consider ENTRY's name (or "value" field) as a command
  17. */
  18. int klish_ptype_COMMAND(kcontext_t *context)
  19. {
  20. const kentry_t *entry = NULL;
  21. const char *value = NULL;
  22. const char *command_name = NULL;
  23. entry = kcontext_candidate_entry(context);
  24. value = kcontext_candidate_value(context);
  25. command_name = kentry_value(entry);
  26. if (!command_name)
  27. command_name = kentry_name(entry);
  28. if (!command_name)
  29. return -1;
  30. return faux_str_casecmp(value, command_name);
  31. }
  32. /** @brief COMPLETION: Consider ENTRY's name (or "value" field) as a command
  33. *
  34. * This completion function has main ENTRY that is a child of COMMAND ptype
  35. * ENTRY. The PTYPE entry has specific ENTRY (with name and possible value)
  36. * as a parent.
  37. *
  38. * command (COMMON ENTRY) with name or value
  39. * ptype (PTYPE ENTRY)
  40. * completion (COMPLETION ENTRY) - start point
  41. */
  42. int klish_completion_COMMAND(kcontext_t *context)
  43. {
  44. const kentry_t *entry = NULL;
  45. const char *command_name = NULL;
  46. entry = kcontext_candidate_entry(context);
  47. command_name = kentry_value(entry);
  48. if (!command_name)
  49. command_name = kentry_name(entry);
  50. if (!command_name)
  51. return 0;
  52. printf("%s\n", command_name);
  53. return 0;
  54. }
  55. /** @brief PTYPE: ENTRY's name (or "value" field) as a case sensitive command
  56. */
  57. int klish_ptype_COMMAND_CASE(kcontext_t *context)
  58. {
  59. const kentry_t *entry = NULL;
  60. const char *value = NULL;
  61. const char *command_name = NULL;
  62. entry = kcontext_candidate_entry(context);
  63. value = kcontext_candidate_value(context);
  64. command_name = kentry_value(entry);
  65. if (!command_name)
  66. command_name = kentry_name(entry);
  67. if (!command_name)
  68. return -1;
  69. return strcmp(value, command_name);
  70. }
  71. /** @brief PTYPE: Signed int with optional range
  72. *
  73. * Use long long int for conversion from text.
  74. *
  75. * <ACTION sym="INT">-30 80</ACTION>
  76. * Means range from "-30" to "80"
  77. */
  78. int klish_ptype_INT(kcontext_t *context)
  79. {
  80. const char *script = NULL;
  81. const char *value_str = NULL;
  82. long long int value = 0;
  83. script = kcontext_script(context);
  84. value_str = kcontext_candidate_value(context);
  85. if (!faux_conv_atoll(value_str, &value, 0))
  86. return -1;
  87. // Range is specified
  88. if (!faux_str_is_empty(script)) {
  89. faux_argv_t *argv = faux_argv_new();
  90. const char *str = NULL;
  91. faux_argv_parse(argv, script);
  92. // Min
  93. str = faux_argv_index(argv, 0);
  94. if (str) {
  95. long long int min = 0;
  96. if (!faux_conv_atoll(str, &min, 0) || (value < min)) {
  97. faux_argv_free(argv);
  98. return -1;
  99. }
  100. }
  101. // Max
  102. str = faux_argv_index(argv, 1);
  103. if (str) {
  104. long long int max = 0;
  105. if (!faux_conv_atoll(str, &max, 0) || (value > max)) {
  106. faux_argv_free(argv);
  107. return -1;
  108. }
  109. }
  110. faux_argv_free(argv);
  111. }
  112. return 0;
  113. }
  114. /** @brief PTYPE: Unsigned int with optional range
  115. *
  116. * Use unsigned long long int for conversion from text.
  117. *
  118. * <ACTION sym="UINT">30 80</ACTION>
  119. * Means range from "30" to "80"
  120. */
  121. int klish_ptype_UINT(kcontext_t *context)
  122. {
  123. const char *script = NULL;
  124. const char *value_str = NULL;
  125. unsigned long long int value = 0;
  126. script = kcontext_script(context);
  127. value_str = kcontext_candidate_value(context);
  128. if (!faux_conv_atoull(value_str, &value, 0))
  129. return -1;
  130. // Range is specified
  131. if (!faux_str_is_empty(script)) {
  132. faux_argv_t *argv = faux_argv_new();
  133. const char *str = NULL;
  134. faux_argv_parse(argv, script);
  135. // Min
  136. str = faux_argv_index(argv, 0);
  137. if (str) {
  138. unsigned long long int min = 0;
  139. if (!faux_conv_atoull(str, &min, 0) || (value < min)) {
  140. faux_argv_free(argv);
  141. return -1;
  142. }
  143. }
  144. // Max
  145. str = faux_argv_index(argv, 1);
  146. if (str) {
  147. unsigned long long int max = 0;
  148. if (!faux_conv_atoull(str, &max, 0) || (value > max)) {
  149. faux_argv_free(argv);
  150. return -1;
  151. }
  152. }
  153. faux_argv_free(argv);
  154. }
  155. return 0;
  156. }
  157. /** @brief PTYPE: Arbitrary string
  158. */
  159. int klish_ptype_STRING(kcontext_t *context)
  160. {
  161. // Really any string is a ... (surprise!) string
  162. context = context; // Happy compiler
  163. return 0;
  164. }