Guest User

Untitled

a guest
Jun 24th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. #include "pidmon.h"
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <err.h>
  5. #include <errno.h>
  6.  
  7. pm_task *pm_task_init() {
  8. pm_task *new = malloc(sizeof(pm_task));
  9. // assert(pm_task);
  10. new->name = NULL;
  11. new->user = NULL;
  12. new->pid = (size_t)0;
  13. new->autostart = true;
  14. new->restart = false;
  15. new->delay = 0;
  16. new->next = NULL;
  17.  
  18. return new;
  19. }
  20. pm_task* read_configured_tasks(char *configfile) {
  21. char *task = "task";
  22. char *task_options[3] = {"user", "command", NULL};
  23. char *cur_key = NULL;
  24. char cur_match[256];
  25. bzero(cur_match, sizeof(cur_match));
  26.  
  27. pm_task *head = pm_task_init();
  28. pm_task *cur = head;
  29.  
  30. int c, i, p = 0;
  31. int context = 0; // 0 = global , 1 = task..
  32. int key_or_value = 0; // 0 = key, 1 = label..
  33. int ignore_rest_of_line = 0; // 0 = read, 1 = ignore
  34.  
  35. FILE *config = NULL;
  36.  
  37. if((config = fopen(configfile, "r")) == NULL) errx(errno, "fopen('%s', 'r'): %s", configfile, strerror(errno));
  38. while(!feof(config) && (c = fgetc(config)) != EOF) {
  39. if (c == 32 || c == 9) {
  40. // got space
  41. continue;
  42. }
  43. if (c == 35) ignore_rest_of_line = 1; // ignore comments
  44. if (c == 10) {
  45. // got enter, reset ignore..
  46. if (ignore_rest_of_line) ignore_rest_of_line = 0;
  47. if (key_or_value == 1) {
  48. // STORE VALUE
  49. printf("should store value for key: %s, value: %s\n", cur_key, cur_match);
  50. if (strcmp(cur_key, "user") == 0) {
  51. cur->user = cur_match;
  52. key_or_value = 0;
  53. p = 0;
  54. cur_key = NULL;
  55. bzero(cur_match, sizeof(cur_match));
  56. } else if (strcmp(cur_key, "command") == 0) {
  57. cur->command = cur_match;
  58. key_or_value = 0;
  59. cur_key = NULL;
  60. bzero(cur_match, sizeof(cur_match));
  61. }
  62. }
  63. continue;
  64. }
  65. if (ignore_rest_of_line) continue;
  66. printf("context = %s, type = %s, p = %d, char: '%c'\n", context == 0 ? "global" : "task", key_or_value == 0 ? "key" : "value", p, c);
  67.  
  68. if(context == 0) { // get task, in global context
  69. cur_match[p++] = c;
  70. if (!key_or_value) { // get key
  71. if(strncmp(task, cur_match, p) != 0) {
  72. printf("Unexpected char '%c', at end of '%s'\n", c, cur_match);
  73. cur_match[--p] = 0;
  74. } else {
  75. if (strlen(task) == strlen(cur_match)) {
  76. // got key, move on..
  77. cur_key = task;
  78. key_or_value++;
  79. bzero(cur_match, sizeof(cur_match));
  80. p = 0;
  81. continue;
  82. }
  83. }
  84. printf("cur_match:%s\n", cur_match);
  85. } else {
  86. // get value, until '{'
  87. if(c == 123) {
  88. cur_match[--p] = 0;
  89. printf("got task name: %s\n", cur_match);
  90. key_or_value = 0;
  91. cur_key = NULL;
  92. context = 1;
  93. p = 0;
  94. cur->name = cur_match;
  95. bzero(cur_match, sizeof(cur_match));
  96. }
  97. }
  98. } else {
  99. // task context
  100. cur_match[p++] = c;
  101.  
  102. if(!key_or_value) {
  103. if(!cur_key) {
  104. for(i = 0; task_options[i]; i++) {
  105. if (strlen(cur_match) < strlen(task_options[i])) continue;
  106. if (strncmp(task_options[i], cur_match, p) == 0) {
  107. printf("match on %s->%s\n", cur_match, task_options[i]);
  108. cur_key = task_options[i];
  109. bzero(cur_match, sizeof(cur_match));
  110. key_or_value++;
  111. p = 0;
  112. }
  113. }
  114. }
  115. // get key
  116. //printf("storing key: %c\n", c);
  117. }
  118. }
  119.  
  120.  
  121.  
  122.  
  123. // implicit continue
  124. continue;
  125. }
  126. return head;
  127. }
Add Comment
Please, Sign In to add comment