Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pidmon.h"
- #include <stdio.h>
- #include <string.h>
- #include <err.h>
- #include <errno.h>
- pm_task *pm_task_init() {
- pm_task *new = malloc(sizeof(pm_task));
- // assert(pm_task);
- new->name = NULL;
- new->user = NULL;
- new->pid = (size_t)0;
- new->autostart = true;
- new->restart = false;
- new->delay = 0;
- new->next = NULL;
- return new;
- }
- pm_task* read_configured_tasks(char *configfile) {
- char *task = "task";
- char *task_options[3] = {"user", "command", NULL};
- char *cur_key = NULL;
- char cur_match[256];
- bzero(cur_match, sizeof(cur_match));
- pm_task *head = pm_task_init();
- pm_task *cur = head;
- int c, i, p = 0;
- int context = 0; // 0 = global , 1 = task..
- int key_or_value = 0; // 0 = key, 1 = label..
- int ignore_rest_of_line = 0; // 0 = read, 1 = ignore
- FILE *config = NULL;
- if((config = fopen(configfile, "r")) == NULL) errx(errno, "fopen('%s', 'r'): %s", configfile, strerror(errno));
- while(!feof(config) && (c = fgetc(config)) != EOF) {
- if (c == 32 || c == 9) {
- // got space
- continue;
- }
- if (c == 35) ignore_rest_of_line = 1; // ignore comments
- if (c == 10) {
- // got enter, reset ignore..
- if (ignore_rest_of_line) ignore_rest_of_line = 0;
- if (key_or_value == 1) {
- // STORE VALUE
- printf("should store value for key: %s, value: %s\n", cur_key, cur_match);
- if (strcmp(cur_key, "user") == 0) {
- cur->user = cur_match;
- key_or_value = 0;
- p = 0;
- cur_key = NULL;
- bzero(cur_match, sizeof(cur_match));
- } else if (strcmp(cur_key, "command") == 0) {
- cur->command = cur_match;
- key_or_value = 0;
- cur_key = NULL;
- bzero(cur_match, sizeof(cur_match));
- }
- }
- continue;
- }
- if (ignore_rest_of_line) continue;
- printf("context = %s, type = %s, p = %d, char: '%c'\n", context == 0 ? "global" : "task", key_or_value == 0 ? "key" : "value", p, c);
- if(context == 0) { // get task, in global context
- cur_match[p++] = c;
- if (!key_or_value) { // get key
- if(strncmp(task, cur_match, p) != 0) {
- printf("Unexpected char '%c', at end of '%s'\n", c, cur_match);
- cur_match[--p] = 0;
- } else {
- if (strlen(task) == strlen(cur_match)) {
- // got key, move on..
- cur_key = task;
- key_or_value++;
- bzero(cur_match, sizeof(cur_match));
- p = 0;
- continue;
- }
- }
- printf("cur_match:%s\n", cur_match);
- } else {
- // get value, until '{'
- if(c == 123) {
- cur_match[--p] = 0;
- printf("got task name: %s\n", cur_match);
- key_or_value = 0;
- cur_key = NULL;
- context = 1;
- p = 0;
- cur->name = cur_match;
- bzero(cur_match, sizeof(cur_match));
- }
- }
- } else {
- // task context
- cur_match[p++] = c;
- if(!key_or_value) {
- if(!cur_key) {
- for(i = 0; task_options[i]; i++) {
- if (strlen(cur_match) < strlen(task_options[i])) continue;
- if (strncmp(task_options[i], cur_match, p) == 0) {
- printf("match on %s->%s\n", cur_match, task_options[i]);
- cur_key = task_options[i];
- bzero(cur_match, sizeof(cur_match));
- key_or_value++;
- p = 0;
- }
- }
- }
- // get key
- //printf("storing key: %c\n", c);
- }
- }
- // implicit continue
- continue;
- }
- return head;
- }
Add Comment
Please, Sign In to add comment