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];
40 int using_web_installer = 0;
44 * Set an entry in inittab to the desired state
46 void set_init_entry(char *which_entry, char *new_state) {
56 if (inittab == NULL) return;
58 fp = fopen("/etc/inittab", "r");
59 if (fp == NULL) return;
61 while(fgets(buf, sizeof buf, fp) != NULL) {
63 if (num_tokens(buf, ':') == 4) {
64 extract_token(entry, buf, 0, ':');
65 extract_token(levels, buf, 1, ':');
66 extract_token(state, buf, 2, ':');
67 extract_token(prog, buf, 3, ':'); /* includes 0x0a LF */
69 if (!strcmp(entry, which_entry)) {
70 strcpy(state, new_state);
71 sprintf(buf, "%s:%s:%s:%s",
72 entry, levels, state, prog);
76 inittab = realloc(inittab, strlen(inittab) + strlen(buf) + 2);
77 if (inittab == NULL) {
85 fp = fopen("/etc/inittab", "w");
87 fwrite(inittab, strlen(inittab), 1, fp);
89 kill(1, SIGHUP); /* Tell init to re-read /etc/inittab */
98 * Shut down the Citadel service if necessary, during setup.
100 void shutdown_service(void) {
103 char looking_for[SIZ];
108 strcpy(init_entry, "");
110 /* Determine the fully qualified path name of webserver */
111 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
113 /* Pound through /etc/inittab line by line. Set have_entry to 1 if
114 * an entry is found which we believe starts webserver.
116 infp = fopen("/etc/inittab", "r");
120 while (fgets(buf, sizeof buf, infp) != NULL) {
121 buf[strlen(buf) - 1] = 0;
122 extract_token(entry, buf, 0, ':');
123 extract_token(prog, buf, 3, ':');
124 if (!strncasecmp(prog, looking_for,
125 strlen(looking_for))) {
127 strcpy(init_entry, entry);
133 /* Bail out if there's nothing to do. */
134 if (!have_entry) return;
136 set_init_entry(init_entry, "off");
141 * Start the Citadel service.
143 void start_the_service(void) {
144 if (strlen(init_entry) > 0) {
145 set_init_entry(init_entry, "respawn");
151 void cleanup(int exitcode)
163 void title(char *text)
165 if (setup_type == UI_TEXT) {
166 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n<%s>\n", text);
172 int yesno(char *question)
175 newtComponent form = NULL;
176 newtComponent yesbutton = NULL;
177 newtComponent nobutton = NULL;
183 switch (setup_type) {
187 printf("%s\nYes/No --> ", question);
188 fgets(buf, sizeof buf, stdin);
189 answer = tolower(buf[0]);
192 else if (answer == 'n')
194 } while ((answer < 0) || (answer > 1));
199 newtCenteredWindow(76, 10, "Question");
200 form = newtForm(NULL, NULL, 0);
201 for (i=0; i<num_tokens(question, '\n'); ++i) {
202 extract_token(buf, question, i, '\n');
203 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
205 yesbutton = newtButton(10, 5, "Yes");
206 nobutton = newtButton(60, 5, "No");
207 newtFormAddComponent(form, yesbutton);
208 newtFormAddComponent(form, nobutton);
209 if (newtRunForm(form) == yesbutton) {
216 newtFormDestroy(form);
225 void set_value(char *prompt, char str[])
235 strcpy(setupmsg, "");
237 switch (setup_type) {
239 title("WebCit setup");
240 printf("\n%s\n", prompt);
241 printf("This is currently set to:\n%s\n", str);
242 printf("Enter new value or press return to leave unchanged:\n");
243 fgets(buf, sizeof buf, stdin);
244 buf[strlen(buf) - 1] = 0;
245 if (strlen(buf) != 0)
251 newtCenteredWindow(76, 10, "WebCit setup");
252 form = newtForm(NULL, NULL, 0);
253 for (i=0; i<num_tokens(prompt, '\n'); ++i) {
254 extract_token(buf, prompt, i, '\n');
255 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
257 newtFormAddComponent(form, newtEntry(1, 8, str, 74, &result,
258 NEWT_FLAG_RETURNEXIT));
263 newtFormDestroy(form);
270 void important_message(char *title, char *msgtext)
273 newtComponent form = NULL;
278 switch (setup_type) {
281 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");
282 printf(" %s \n\n%s\n\n", title, msgtext);
283 printf("Press return to continue...");
284 fgets(buf, sizeof buf, stdin);
289 newtCenteredWindow(76, 10, title);
290 form = newtForm(NULL, NULL, 0);
291 for (i=0; i<num_tokens(msgtext, '\n'); ++i) {
292 extract_token(buf, msgtext, i, '\n');
293 newtFormAddComponent(form, newtLabel(1, 1+i, buf));
295 newtFormAddComponent(form, newtButton(35, 5, "OK"));
298 newtFormDestroy(form);
306 void display_error(char *error_message)
308 important_message("Error", error_message);
311 void progress(char *text, long int curr, long int cmax)
315 /* These variables are static because progress() gets called
316 * multiple times during the course of whatever operation is
317 * being performed. This makes setup non-threadsafe, but who
320 static newtComponent form = NULL;
321 static newtComponent scale = NULL;
323 static long dots_printed = 0L;
326 switch (setup_type) {
330 printf("%s\n", text);
331 printf("..........................");
332 printf("..........................");
333 printf("..........................\r");
336 } else if (curr == cmax) {
337 printf("\r%79s\n", "");
339 a = (curr * 100) / cmax;
342 while (dots_printed < a) {
353 newtCenteredWindow(76, 8, text);
354 form = newtForm(NULL, NULL, 0);
355 scale = newtScale(1, 3, 74, cmax);
356 newtFormAddComponent(form, scale);
360 if ((curr > 0) && (curr <= cmax)) {
361 newtScaleSet(scale, curr);
365 newtFormDestroy(form);
379 * check_inittab_entry() -- Make sure "webserver" is in /etc/inittab
382 void check_inittab_entry(void)
386 char looking_for[SIZ];
389 char listenport[128];
393 /* Determine the fully qualified path name of webserver */
394 snprintf(looking_for, sizeof looking_for, "%s/webserver ", WEBCITDIR);
396 /* If there's already an entry, then we have nothing left to do. */
397 if (strlen(init_entry) > 0) {
401 /* Otherwise, prompt the user to create an entry. */
402 snprintf(question, sizeof question,
403 "There is no '%s' entry in /etc/inittab.\n"
404 "Would you like to add one?",
406 if (yesno(question) == 0)
409 snprintf(question, sizeof question,
410 "On which port do you want WebCit to listen for HTTP "
411 "requests?\n\nYou can use the standard port (80) if you are "
412 "not running another\nweb server (such as Apache), otherwise "
413 "select another port.");
414 sprintf(listenport, "2000");
415 set_value(question, listenport);
417 /* Find out where Citadel is. */
418 if ( (using_web_installer) && (getenv("CITADEL") != NULL) ) {
419 strcpy(hostname, "uds");
420 strcpy(portname, getenv("CITADEL"));
423 snprintf(question, sizeof question,
424 "Is the Citadel service running on the same host as WebCit?");
425 if (yesno(question)) {
426 sprintf(hostname, "uds");
427 sprintf(portname, "/usr/local/citadel");
428 set_value("In what directory is Citadel installed?", portname);
431 sprintf(hostname, "127.0.0.1");
432 sprintf(portname, "504");
433 set_value("Enter the host name or IP address of your "
434 "Citadel server.", hostname);
435 set_value("Enter the port number on which Citadel is "
436 "running (usually 504)", portname);
440 /* Generate a unique entry name for /etc/inittab */
441 snprintf(entryname, sizeof entryname, "c0");
444 if (entryname[1] > '9') {
447 if (entryname[0] > 'z') {
449 "Can't generate a unique entry name");
453 snprintf(buf, sizeof buf,
454 "grep %s: /etc/inittab >/dev/null 2>&1", entryname);
455 } while (system(buf) == 0);
457 /* Now write it out to /etc/inittab */
458 infp = fopen("/etc/inittab", "a");
460 display_error(strerror(errno));
462 fprintf(infp, "# Start the WebCit server...\n");
463 fprintf(infp, "%s:2345:respawn:%s -p%s %s %s\n",
464 entryname, looking_for,
465 listenport, hostname, portname);
467 strcpy(init_entry, entryname);
475 * Figure out what type of user interface we're going to use
477 int discover_ui(void)
483 newtDrawRootText(0, 0, "WebCit Setup");
493 int main(int argc, char *argv[])
499 /* set an invalid setup type */
502 /* Check to see if we're running the web installer */
503 if (getenv("CITADEL_INSTALLER") != NULL) {
504 using_web_installer = 1;
507 /* parse command line args */
508 for (a = 0; a < argc; ++a) {
509 if (!strncmp(argv[a], "-u", 2)) {
510 strcpy(aaa, argv[a]);
511 strcpy(aaa, &aaa[2]);
512 setup_type = atoi(aaa);
514 if (!strcmp(argv[a], "-i")) {
517 if (!strcmp(argv[a], "-q")) {
518 setup_type = UI_SILENT;
523 /* If a setup type was not specified, try to determine automatically
524 * the best one to use out of all available types.
526 if (setup_type < 0) {
527 setup_type = discover_ui();
529 if (info_only == 1) {
530 important_message("WebCit Setup", "Welcome to WebCit setup");
534 /* Get started in a valid setup directory. */
535 strcpy(setup_directory, WEBCITDIR);
536 if ( (using_web_installer) && (getenv("WEBCIT") != NULL) ) {
537 strcpy(setup_directory, getenv("WEBCIT"));
540 set_value("In what directory is WebCit installed?",
543 if (chdir(setup_directory) != 0) {
544 important_message("WebCit Setup",
545 "The directory you specified does not exist.");
549 /* See if we need to shut down the WebCit service. */
550 for (a=0; a<=3; ++a) {
551 progress("Shutting down the WebCit service...", a, 3);
552 if (a == 0) shutdown_service();
557 switch (setup_type) {
561 " *** WebCit setup program ***\n\n");
566 check_inittab_entry(); /* Check /etc/inittab */
568 /* See if we can start the WebCit service. */
569 if (strlen(init_entry) > 0) {
570 for (a=0; a<=3; ++a) {
571 progress("Starting the WebCit service...", a, 3);
572 if (a == 0) start_the_service();
575 important_message("Setup finished",
576 "Setup is finished. You may now log in.");
579 important_message("Setup finished",
580 "Setup is finished. You may now start the server.");