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[])
234 strcpy(setupmsg, "");
236 switch (setup_type) {
238 title("WebCit setup");
239 printf("\n%s\n", prompt);
240 printf("This is currently set to:\n%s\n", str);
241 printf("Enter new value or press return to leave unchanged:\n");
242 fgets(buf, sizeof buf, stdin);
243 buf[strlen(buf) - 1] = 0;
244 if (strlen(buf) != 0)
250 newtCenteredWindow(76, 10, "WebCit setup");
251 form = newtForm(NULL, NULL, 0);
252 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
253 extract_token(buf, prompt, i, '\n');
254 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
256 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
257 NEWT_FLAG_RETURNEXIT));
262 newtFormDestroy(form);
269 void important_message(char *title, char *msgtext)
272 newtComponent form = NULL;
277 switch (setup_type) {
280 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");
281 printf(" %s \n\n%s\n\n", title, msgtext);
282 printf("Press return to continue...");
283 fgets(buf, sizeof buf, stdin);
288 newtCenteredWindow(76, 10, title);
289 form = newtForm(NULL, NULL, 0);
290 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
291 extract_token(buf, msgtext, i, '\n');
292 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
294 newtFormAddComponent(form, newtButton(35, 5, "OK"));
297 newtFormDestroy(form);
305 void display_error(char *error_message)
307 important_message("Error", error_message);
310 void progress(char *text, long int curr, long int cmax)
314 /* These variables are static because progress() gets called
315 * multiple times during the course of whatever operation is
316 * being performed. This makes setup non-threadsafe, but who
319 static newtComponent form = NULL;
320 static newtComponent scale = NULL;
322 static long dots_printed = 0L;
325 switch (setup_type) {
329 printf("%s\n", text);
330 printf("..........................");
331 printf("..........................");
332 printf("..........................\r");
335 } else if (curr == cmax) {
336 printf("\r%79s\n", "");
338 a = (curr * 100) / cmax;
341 while (dots_printed < a) {
352 newtCenteredWindow(76, 8, text);
353 form = newtForm(NULL, NULL, 0);
354 scale = newtScale(1, 3, 74, cmax);
355 newtFormAddComponent(form, scale);
359 if ((curr > 0) && (curr <= cmax)) {
360 newtScaleSet(scale, curr);
364 newtFormDestroy(form);
378 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
381 void check_inittab_entry(void)
385 char looking_for[SIZ];
388 char listenport[128];
392 /* Determine the fully qualified path name of webserver */
393 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
395 /* If there's already an entry, then we have nothing left to do. */
396 if (strlen(init_entry) > 0) {
400 /* Otherwise, prompt the user to create an entry. */
401 snprintf(question, sizeof question,
402 "There is no '%s' entry in /etc/inittab.\n"
403 "Would you like to add one?",
405 if (yesno(question) == 0)
408 snprintf(question, sizeof question,
409 "On which port do you want WebCit to listen for HTTP "
410 "requests?\n\nYou can use the standard port (80) if you are "
411 "not running another\nweb server (such as Apache), otherwise "
412 "select another port.");
413 sprintf(listenport, "2000");
414 set_value(question, listenport);
416 /* Find out where Citadel is. */
417 snprintf(question, sizeof question,
418 "Is the Citadel service running on the same host as WebCit?");
419 if (yesno(question)) {
420 sprintf(hostname, "uds");
421 sprintf(portname, "/usr/local/citadel");
422 set_value("In what directory is Citadel installed?", portname);
425 sprintf(hostname, "127.0.0.1");
426 sprintf(portname, "504");
427 set_value("Enter the host name or IP address of your "
428 "Citadel server.", hostname);
429 set_value("Enter the port number on which Citadel is "
430 "running (usually 504)", portname);
433 /* Generate a unique entry name for /etc/inittab */
434 snprintf(entryname, sizeof entryname, "c0");
437 if (entryname[1] > '9') {
440 if (entryname[0] > 'z') {
442 "Can't generate a unique entry name");
446 snprintf(buf, sizeof buf,
447 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
448 } while (system(buf) == 0);
450 /* Now write it out to /etc/inittab */
451 infp = fopen("/etc/inittab", "a");
453 display_error(strerror(errno));
455 fprintf(infp, "# Start the WebCit server...\n");
456 fprintf(infp, "%s:2345:respawn:%s -p%s %s %s\n",
457 entryname, looking_for,
458 listenport, hostname, portname);
460 strcpy(init_entry, entryname);
468 * Figure out what type of user interface we're going to use
470 int discover_ui(void)
476 newtDrawRootText(0, 0, "WebCit Setup");
486 int main(int argc, char *argv[])
492 /* set an invalid setup type */
495 /* parse command line args */
496 for (a = 0; a < argc; ++a) {
497 if (!strncmp(argv[a], "-u", 2)) {
498 strcpy(aaa, argv[a]);
499 strcpy(aaa, &aaa[2]);
500 setup_type = atoi(aaa);
502 if (!strcmp(argv[a], "-i")) {
505 if (!strcmp(argv[a], "-q")) {
506 setup_type = UI_SILENT;
511 /* If a setup type was not specified, try to determine automatically
512 * the best one to use out of all available types.
514 if (setup_type < 0) {
515 setup_type = discover_ui();
517 if (info_only == 1) {
518 important_message("WebCit Setup", "Welcome to WebCit setup");
522 /* Get started in a valid setup directory. */
523 strcpy(setup_directory, WEBCITDIR);
524 set_value("In what directory is WebCit installed?",
526 if (chdir(setup_directory) != 0) {
527 important_message("WebCit Setup",
528 "The directory you specified does not exist.");
532 /* See if we need to shut down the WebCit service. */
533 for (a=0; a<=3; ++a) {
534 progress("Shutting down the WebCit service...", a, 3);
535 if (a == 0) shutdown_service();
540 switch (setup_type) {
544 " *** WebCit setup program ***\n\n");
549 check_inittab_entry(); /* Check /etc/inittab */
551 /* See if we can start the WebCit service. */
552 if (strlen(init_entry) > 0) {
553 for (a=0; a<=3; ++a) {
554 progress("Starting the WebCit service...", a, 3);
555 if (a == 0) start_the_service();
558 important_message("Setup finished",
559 "Setup is finished. You may now log in.");
562 important_message("Setup finished",
563 "Setup is finished. You may now start the server.");