7 /* read the config file
9 * first mark all services as pending
11 * then for each line which is not a blank/comment
12 * strplit and find name/class
13 * if name already exists:
14 * if same class, set attributes
15 * if different class, rename old service and create new
17 * create new service if remainder parses ok
19 * if any remaining services are pending, disabled them unless there were errors
23 int read_config(void *services, char *file)
29 static char *lastfile = NULL;
30 if (file) lastfile = file;
33 for (serv = skip_first(services) ; serv ; serv = skip_next(serv))
39 error("cannot find config file %s", file);
42 while (fgets(linebuf, sizeof(linebuf), f)!= NULL)
44 int len = strlen(linebuf);
48 if (len > 1000 && linebuf[len-1] != '\n')
50 error("line too long in config file");
54 if (linebuf[len-1] == '\n') linebuf[--len] = 0;
56 words = strsplit(linebuf, " \t\n");
59 for (w=0 ;words[w] ; w++) if (words[w][0]=='#') words[w]=NULL;
60 if (words[0] && words[1])
63 service_t *svp = skip_search(services, words[0]);
65 class_t cl = find_class(words[1]);
68 if ((*svp)->class != cl)
70 /* different classes - rename old services */
71 skip_delete(services, words[0]);
72 strcpy(linebuf, words[0]);
73 do { strcat(linebuf, ".old"); } while (skip_search(services, linebuf)!= NULL);
74 free((*svp)->service);
75 (*svp)->service = strdup(linebuf);
76 skip_insert(services, *svp);
82 error("unknown class %s", words[1]);
86 sv = new_service(words[0], cl);
87 /* now process options */
88 for (w=2 ; words[w] && words[w][0] != '/' ; w++)
90 int ok = process_opt(sv, words[w]);
92 ok = sv->class->c_process_opt(sv, words[w]);
96 error("unknown option: %s", words[w]);
98 error("error in option: %s", words[w]);
105 if (sv == NULL) continue;
107 sv->program = strdup(words[w++]);
109 sv->args = strlistdup(words+w);
110 if (sv->program == NULL)
111 error("missing program name for service %s", words[0]), err++;
112 else if (sv->args == NULL)
113 error("missing program arguments for service %s", words[0]), err++;
119 service_t sv2 = *svp;
120 sv->enabled = sv2->enabled;
121 sv->proc_list = sv2->proc_list;
123 /* change service pointer to the new structure (for aidan) */
125 for (pp2 = skip_first(sv->proc_list) ; pp2 ; pp2=skip_next(pp2))
126 (*pp2)->service = sv;
128 sv2->proc_list = NULL;
129 sv->class->copy_state(sv2, sv);
130 skip_delete(services, sv2->service);
133 skip_insert(services, sv);
134 sv->class->register_service(sv);
137 if (sv) free_service(sv);
144 for (serv = skip_first(services) ; serv ; serv = skip_next(serv))
145 if ((*serv)->pending)
146 (*serv)->enabled = 0;