list.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. #include "proto.h"
  6. /* get list of running monitors */
  7. void list_mon(char ***list, int *n)
  8. {
  9. int i;
  10. const char *name;
  11. const char *env_prefix = "MONITOR_";
  12. int env_prefix_len;
  13. char **tokens;
  14. env_prefix_len = strlen(env_prefix);
  15. *list = NULL;
  16. *n = 0;
  17. tokens = NULL;
  18. for (i = 0; (name = G__env_name(i)); i++) {
  19. if (strncmp(env_prefix, name, env_prefix_len) == 0) {
  20. tokens = G_tokenize(name, "_");
  21. if (G_number_of_tokens(tokens) != 3 ||
  22. strcmp(tokens[2], "ENVFILE") != 0)
  23. continue;
  24. *list = G_realloc(*list, (*n + 1) * sizeof(char *));
  25. (*list)[*n] = G_store(tokens[1]);
  26. (*n)++;
  27. G_free_tokens(tokens);
  28. tokens = NULL;
  29. }
  30. }
  31. }
  32. /* print list of running monitors */
  33. void print_list(FILE *fd)
  34. {
  35. char **list;
  36. int i, n;
  37. list_mon(&list, &n);
  38. if (n > 0)
  39. G_message(_("List of running monitors:"));
  40. else {
  41. G_important_message(_("No monitors running"));
  42. return;
  43. }
  44. for (i = 0; i < n; i++)
  45. fprintf(fd, "%s\n", list[i]);
  46. }
  47. /* check if monitor is running */
  48. int check_mon(const char *name)
  49. {
  50. char *env_name;
  51. const char *str;
  52. env_name = NULL;
  53. G_asprintf(&env_name, "MONITOR_%s_ENVFILE", name);
  54. str = G__getenv(env_name);
  55. if (!str)
  56. return FALSE;
  57. return TRUE;
  58. }
  59. /* list related commands for given monitor */
  60. void list_cmd(const char *name, FILE *fd_out)
  61. {
  62. char buf[1024];
  63. char *cmd_name;
  64. const char *cmd_value;
  65. FILE *fd;
  66. cmd_name = NULL;
  67. G_asprintf(&cmd_name, "MONITOR_%s_CMDFILE", name);
  68. cmd_value = G__getenv(cmd_name);
  69. if (!cmd_value)
  70. G_fatal_error(_("Command file not found"));
  71. fd = fopen(cmd_value, "r");
  72. if (!fd)
  73. G_fatal_error(_("Unable to read command file"));
  74. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  75. fprintf(fd_out, "%s\n", buf);
  76. }
  77. fclose(fd);
  78. }