list.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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_get_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. /* GRASS variable names are upper case, but monitor names are lower
  26. * case. */
  27. (*list)[*n] = G_store_lower(tokens[1]);
  28. (*n)++;
  29. G_free_tokens(tokens);
  30. tokens = NULL;
  31. }
  32. }
  33. }
  34. /* print list of running monitors */
  35. void print_list(FILE *fd)
  36. {
  37. char **list;
  38. int i, n;
  39. list_mon(&list, &n);
  40. if (n > 0)
  41. G_message(_("List of running monitors:"));
  42. else {
  43. G_important_message(_("No monitors running"));
  44. return;
  45. }
  46. for (i = 0; i < n; i++)
  47. fprintf(fd, "%s\n", list[i]);
  48. }
  49. /* check if monitor is running */
  50. int check_mon(const char *name)
  51. {
  52. char *env_name;
  53. const char *str;
  54. env_name = NULL;
  55. G_asprintf(&env_name, "MONITOR_%s_ENVFILE", G_store_upper(name));
  56. str = G_getenv_nofatal(env_name);
  57. if (!str)
  58. return FALSE;
  59. return TRUE;
  60. }
  61. /* list related commands for given monitor */
  62. void list_cmd(const char *name, FILE *fd_out)
  63. {
  64. char buf[1024];
  65. char *cmd_name;
  66. const char *cmd_value;
  67. FILE *fd;
  68. cmd_name = NULL;
  69. G_asprintf(&cmd_name, "MONITOR_%s_CMDFILE", G_store_upper(name));
  70. cmd_value = G_getenv_nofatal(cmd_name);
  71. if (!cmd_value)
  72. G_fatal_error(_("Command file not found"));
  73. fd = fopen(cmd_value, "r");
  74. if (!fd)
  75. G_fatal_error(_("Unable to read command file"));
  76. while (G_getl2(buf, sizeof(buf) - 1, fd) != 0) {
  77. fprintf(fd_out, "%s\n", buf);
  78. }
  79. fclose(fd);
  80. }