6 * (This is basically just an install wizard. It's not required.)
16 #include <sys/types.h>
18 #include <sys/utsname.h>
33 #define UI_TEXT 0 /* Default setup type -- text only */
34 #define UI_SILENT 3 /* Silent running, for use in scripts */
35 #define UI_NEWT 4 /* Use the "newt" window library */
38 char setup_directory[SIZ];
43 * Set an entry in inittab to the desired state
45 void set_init_entry(char *which_entry, char *new_state) {
55 if (inittab == NULL) return;
57 fp = fopen("/etc/inittab", "r");
58 if (fp == NULL) return;
60 while(fgets(buf, sizeof buf, fp) != NULL) {
62 if (num_tokens(buf, ':') == 4) {
63 extract_token(entry, buf, 0, ':');
64 extract_token(levels, buf, 1, ':');
65 extract_token(state, buf, 2, ':');
66 extract_token(prog, buf, 3, ':'); /* includes 0x0a LF */
68 if (!strcmp(entry, which_entry)) {
69 strcpy(state, new_state);
70 sprintf(buf, "%s:%s:%s:%s",
71 entry, levels, state, prog);
75 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
76 if (inittab == NULL) {
84 fp = fopen("/etc/inittab", "w");
86 fwrite(inittab, strlen(inittab), 1, fp);
88 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
97 * Shut down the Citadel service if necessary, during setup.
99 void shutdown_service(void) {
102 char looking_for[SIZ];
107 strcpy(init_entry, "");
109 /* Determine the fully qualified path name of webserver */
110 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
112 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
113 * an entry is found which we believe starts webserver.
115 infp = fopen("/etc/inittab", "r");
119 while (fgets(buf, sizeof buf, infp) != NULL) {
120 buf[strlen(buf) - 1] = 0;
121 extract_token(entry, buf, 0, ':');
122 extract_token(prog, buf, 3, ':');
123 if (!strncasecmp(prog, looking_for,
124 strlen(looking_for))) {
126 strcpy(init_entry, entry);
132 /* Bail out if there's nothing to do. */
133 if (!have_entry) return;
135 set_init_entry(init_entry, "off");
140 * Start the Citadel service.
142 void start_the_service(void) {
143 if (strlen(init_entry) > 0) {
144 set_init_entry(init_entry, "respawn");
150 void cleanup(int exitcode)
162 void title(char *text)
164 if (setup_type == UI_TEXT) {
165 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
171 int yesno(char *question)
174 newtComponent form = NULL;
175 newtComponent yesbutton = NULL;
176 newtComponent nobutton = NULL;
182 switch (setup_type) {
186 printf("%s\nYes/No --> ", question);
187 fgets(buf, sizeof buf, stdin);
188 answer = tolower(buf[0]);
191 else if (answer == 'n')
193 } while ((answer < 0) || (answer > 1));
198 newtCenteredWindow(76, 10, "Question");
199 form = newtForm(NULL, NULL, 0);
200 for (i=0; i<num_tokens(question, '\n'); ++i) {
201 extract_token(buf, question, i, '\n');
202 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
204 yesbutton = newtButton(10, 5, "Yes");
205 nobutton = newtButton(60, 5, "No");
206 newtFormAddComponent(form, yesbutton);
207 newtFormAddComponent(form, nobutton);
208 if (newtRunForm(form) == yesbutton) {
215 newtFormDestroy(form);
224 void set_value(char *prompt, char str[])
232 char tempfile[PATH_MAX];
235 strcpy(tempfile, tmpnam(NULL));
236 strcpy(setupmsg, "");
238 switch (setup_type) {
240 title("WebCit setup");
241 printf("\n%s\n", prompt);
242 printf("This is currently set to:\n%s\n", str);
243 printf("Enter new value or press return to leave unchanged:\n");
244 fgets(buf, sizeof buf, stdin);
245 buf[strlen(buf) - 1] = 0;
246 if (strlen(buf) != 0)
252 newtCenteredWindow(76, 10, "WebCit setup");
253 form = newtForm(NULL, NULL, 0);
254 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
255 extract_token(buf, prompt, i, '\n');
256 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
258 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
259 NEWT_FLAG_RETURNEXIT));
264 newtFormDestroy(form);
271 void important_message(char *title, char *msgtext)
274 newtComponent form = NULL;
279 switch (setup_type) {
282 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
283 printf(" %s \n\n%s\n\n", title, msgtext);
284 printf("Press return to continue...");
285 fgets(buf, sizeof buf, stdin);
290 newtCenteredWindow(76, 10, title);
291 form = newtForm(NULL, NULL, 0);
292 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
293 extract_token(buf, msgtext, i, '\n');
294 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
296 newtFormAddComponent(form, newtButton(35, 5, "OK"));
299 newtFormDestroy(form);
307 void display_error(char *error_message)
309 important_message("Error", error_message);
312 void progress(char *text, long int curr, long int cmax)
316 /* These variables are static because progress() gets called
317 * multiple times during the course of whatever operation is
318 * being performed. This makes setup non-threadsafe, but who
321 static newtComponent form = NULL;
322 static newtComponent scale = NULL;
324 static long dots_printed = 0L;
327 switch (setup_type) {
331 printf("%s\n", text);
332 printf("..........................");
333 printf("..........................");
334 printf("..........................\r");
337 } else if (curr == cmax) {
338 printf("\r%79s\n", "");
340 a = (curr * 100) / cmax;
343 while (dots_printed < a) {
354 newtCenteredWindow(76, 8, text);
355 form = newtForm(NULL, NULL, 0);
356 scale = newtScale(1, 3, 74, cmax);
357 newtFormAddComponent(form, scale);
361 if ((curr > 0) && (curr <= cmax)) {
362 newtScaleSet(scale, curr);
366 newtFormDestroy(form);
380 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
383 void check_inittab_entry(void)
387 char looking_for[SIZ];
390 char listenport[128];
394 /* Determine the fully qualified path name of webserver */
395 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
397 /* If there's already an entry, then we have nothing left to do. */
398 if (strlen(init_entry) > 0) {
402 /* Otherwise, prompt the user to create an entry. */
403 snprintf(question, sizeof question,
404 "There is no '%s' entry in /etc/inittab.\n"
405 "Would you like to add one?",
407 if (yesno(question) == 0)
410 snprintf(question, sizeof question,
411 "On which port do you want WebCit to listen for HTTP "
412 "requests?\n\nYou can use the standard port (80) if you are "
413 "not running another\nweb server (such as Apache), otherwise "
414 "select another port.");
415 sprintf(listenport, "2000");
416 set_value(question, listenport);
418 /* Find out where Citadel is. */
419 snprintf(question, sizeof question,
420 "Is the Citadel service running on the same host as WebCit?");
421 if (yesno(question)) {
422 sprintf(hostname, "uds");
423 sprintf(portname, "/usr/local/citadel");
424 set_value("In what directory is Citadel installed?", portname);
427 sprintf(hostname, "127.0.0.1");
428 sprintf(portname, "504");
429 set_value("Enter the host name or IP address of your "
430 "Citadel server.", hostname);
431 set_value("Enter the port number on which Citadel is "
432 "running (usually 504)", portname);
435 /* Generate a unique entry name for /etc/inittab */
436 snprintf(entryname, sizeof entryname, "c0");
439 if (entryname[1] > '9') {
442 if (entryname[0] > 'z') {
444 "Can't generate a unique entry name");
448 snprintf(buf, sizeof buf,
449 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
450 } while (system(buf) == 0);
452 /* Now write it out to /etc/inittab */
453 infp = fopen("/etc/inittab", "a");
455 display_error(strerror(errno));
457 fprintf(infp, "# Start the WebCit server...\n");
458 fprintf(infp, "%s:2345:respawn:%s -p%s %s %s\n",
459 entryname, looking_for,
460 listenport, hostname, portname);
462 strcpy(init_entry, entryname);
470 * Figure out what type of user interface we're going to use
472 int discover_ui(void)
478 newtDrawRootText(0, 0, "WebCit Setup");
488 int main(int argc, char *argv[])
494 /* set an invalid setup type */
497 /* parse command line args */
498 for (a = 0; a < argc; ++a) {
499 if (!strncmp(argv[a], "-u", 2)) {
500 strcpy(aaa, argv[a]);
501 strcpy(aaa, &aaa[2]);
502 setup_type = atoi(aaa);
504 if (!strcmp(argv[a], "-i")) {
507 if (!strcmp(argv[a], "-q")) {
508 setup_type = UI_SILENT;
513 /* If a setup type was not specified, try to determine automatically
514 * the best one to use out of all available types.
516 if (setup_type < 0) {
517 setup_type = discover_ui();
519 if (info_only == 1) {
520 important_message("WebCit Setup", "Welcome to WebCit setup");
524 /* Get started in a valid setup directory. */
525 strcpy(setup_directory, WEBCITDIR);
526 set_value("In what directory is WebCit installed?",
528 if (chdir(setup_directory) != 0) {
529 important_message("WebCit Setup",
530 "The directory you specified does not exist.");
534 /* See if we need to shut down the WebCit service. */
535 for (a=0; a<=3; ++a) {
536 progress("Shutting down the WebCit service...", a, 3);
537 if (a == 0) shutdown_service();
542 switch (setup_type) {
546 " *** WebCit setup program ***\n\n");
551 check_inittab_entry(); /* Check /etc/inittab */
553 /* See if we can start the WebCit service. */
554 if (strlen(init_entry) > 0) {
555 for (a=0; a<=3; ++a) {
556 progress("Starting the WebCit service...", a, 3);
557 if (a == 0) start_the_service();
560 important_message("Setup finished",
561 "Setup is finished. You may now log in.");
564 important_message("Setup finished",
565 "Setup is finished. You may now start the server.");