ptypes.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 HELP: Consider ENTRY's name (or "value" field) as a command
  56. *
  57. * This help function has main ENTRY that is a child of COMMAND ptype
  58. * ENTRY. The PTYPE entry has specific ENTRY (with name and possible value)
  59. * as a parent.
  60. *
  61. * command (COMMON ENTRY) with name or value
  62. * ptype (PTYPE ENTRY)
  63. * help (HELP ENTRY) - start point
  64. */
  65. int klish_help_COMMAND(kcontext_t *context)
  66. {
  67. const kentry_t *entry = NULL;
  68. const char *command_name = NULL;
  69. const char *help_text = NULL;
  70. entry = kcontext_candidate_entry(context);
  71. command_name = kentry_value(entry);
  72. if (!command_name)
  73. command_name = kentry_name(entry);
  74. assert(command_name);
  75. help_text = kentry_help(entry);
  76. if (!help_text)
  77. help_text = kentry_value(entry);
  78. if (!help_text)
  79. help_text = kentry_name(entry);
  80. assert(help_text);
  81. printf("%s\n%s\n", command_name, help_text);
  82. return 0;
  83. }
  84. /** @brief PTYPE: ENTRY's name (or "value" field) as a case sensitive command
  85. */
  86. int klish_ptype_COMMAND_CASE(kcontext_t *context)
  87. {
  88. const kentry_t *entry = NULL;
  89. const char *value = NULL;
  90. const char *command_name = NULL;
  91. entry = kcontext_candidate_entry(context);
  92. value = kcontext_candidate_value(context);
  93. command_name = kentry_value(entry);
  94. if (!command_name)
  95. command_name = kentry_name(entry);
  96. if (!command_name)
  97. return -1;
  98. return strcmp(value, command_name);
  99. }
  100. /** @brief PTYPE: Signed int with optional range
  101. *
  102. * Use long long int for conversion from text.
  103. *
  104. * <ACTION sym="INT">-30 80</ACTION>
  105. * Means range from "-30" to "80"
  106. */
  107. int klish_ptype_INT(kcontext_t *context)
  108. {
  109. const char *script = NULL;
  110. const char *value_str = NULL;
  111. long long int value = 0;
  112. script = kcontext_script(context);
  113. value_str = kcontext_candidate_value(context);
  114. if (!faux_conv_atoll(value_str, &value, 0))
  115. return -1;
  116. // Range is specified
  117. if (!faux_str_is_empty(script)) {
  118. faux_argv_t *argv = faux_argv_new();
  119. const char *str = NULL;
  120. faux_argv_parse(argv, script);
  121. // Min
  122. str = faux_argv_index(argv, 0);
  123. if (str) {
  124. long long int min = 0;
  125. if (!faux_conv_atoll(str, &min, 0) || (value < min)) {
  126. faux_argv_free(argv);
  127. return -1;
  128. }
  129. }
  130. // Max
  131. str = faux_argv_index(argv, 1);
  132. if (str) {
  133. long long int max = 0;
  134. if (!faux_conv_atoll(str, &max, 0) || (value > max)) {
  135. faux_argv_free(argv);
  136. return -1;
  137. }
  138. }
  139. faux_argv_free(argv);
  140. }
  141. return 0;
  142. }
  143. /** @brief PTYPE: Unsigned int with optional range
  144. *
  145. * Use unsigned long long int for conversion from text.
  146. *
  147. * <ACTION sym="UINT">30 80</ACTION>
  148. * Means range from "30" to "80"
  149. */
  150. int klish_ptype_UINT(kcontext_t *context)
  151. {
  152. const char *script = NULL;
  153. const char *value_str = NULL;
  154. unsigned long long int value = 0;
  155. script = kcontext_script(context);
  156. value_str = kcontext_candidate_value(context);
  157. if (!faux_conv_atoull(value_str, &value, 0))
  158. return -1;
  159. // Range is specified
  160. if (!faux_str_is_empty(script)) {
  161. faux_argv_t *argv = faux_argv_new();
  162. const char *str = NULL;
  163. faux_argv_parse(argv, script);
  164. // Min
  165. str = faux_argv_index(argv, 0);
  166. if (str) {
  167. unsigned long long int min = 0;
  168. if (!faux_conv_atoull(str, &min, 0) || (value < min)) {
  169. faux_argv_free(argv);
  170. return -1;
  171. }
  172. }
  173. // Max
  174. str = faux_argv_index(argv, 1);
  175. if (str) {
  176. unsigned long long int max = 0;
  177. if (!faux_conv_atoull(str, &max, 0) || (value > max)) {
  178. faux_argv_free(argv);
  179. return -1;
  180. }
  181. }
  182. faux_argv_free(argv);
  183. }
  184. return 0;
  185. }
  186. /** @brief PTYPE: Arbitrary string
  187. */
  188. int klish_ptype_STRING(kcontext_t *context)
  189. {
  190. // Really any string is a ... (surprise!) string
  191. context = context; // Happy compiler
  192. return 0;
  193. }