int megahal_process(brain_t brain, const char *input, char **output, uint8_t flags) { list_t *words_in; int ret; if (input != NULL) { char *tmp; tmp = strdup(input); if (tmp == NULL) return -ENOMEM; ret = megahal_parse(tmp, &words_in); free(tmp); if (ret) return ret; } else { words_in = NULL; } if ((flags & MEGAHAL_F_LEARN) != 0) { WARN_IF(words_in == NULL); ret = megahal_learn(brain, words_in); if (ret) { list_free(&words_in); return ret; } } if (output != NULL) { list_t *words_out; if (words_in == NULL) BUG(); // TODO ret = megahal_reply(brain, words_in, &words_out); list_free(&words_in); if (ret) return ret; if (words_out == NULL) { *output = strdup("I don't know enough to answer you yet!"); if (*output == NULL) return -ENOMEM; } else { ret = megahal_output(words_out, output); list_free(&words_out); if (ret) return ret; } } else { list_free(&words_in); } return OK; }
/* * Function: Main * * Purpose: Initialise everything, and then do an infinite loop. In * the loop, we read the user's input and reply to it, and * do some housekeeping task such as responding to special * commands. */ int main(int argc, char **argv) { char *input=NULL; char *output=NULL; char *my_directory = NULL; int directory_set; int c, option_index = 0; directory_set = 0; while(1) { if((c = getopt_long(argc, argv, "hpwbd:", long_options, &option_index)) == -1) break; switch(c) { case 'p': megahal_setnoprompt(); break; case 'w': megahal_setnowrap(); break; case 'd': megahal_setdirectory (optarg); directory_set = 1; break; case 'b': megahal_setnobanner(); break; case 'h': usage(); return 0; } } if (directory_set == 0) { if ((my_directory = getenv("MEGAHAL_DIR"))) { megahal_setdirectory (my_directory); directory_set = 1; } else { struct stat dir_stat; my_directory = getenv ("HOME"); if (my_directory == NULL) { fprintf (stderr, "Cannot find your home directory.\n"); exit (1); } my_directory = malloc (12 + strlen (my_directory)); strcpy (my_directory, getenv ("HOME")); strcat (my_directory, "/.megahal"); if (stat (my_directory, &dir_stat)) { if (errno == ENOENT) { directory_set = mkdir (my_directory, S_IRWXU); if (directory_set != 0) { fprintf (stderr, "Could not create %s.\n", my_directory); exit (1); } megahal_setdirectory (my_directory); directory_set = 1; } } else { if (S_ISDIR(dir_stat.st_mode)) { megahal_setdirectory (my_directory); directory_set = 1; } else { fprintf (stderr, "Could not use megahal directory %s.\n", my_directory); exit (1); } } } } /* * Do some initialisation */ megahal_initialize(); /* * Create a dictionary which will be used to hold the segmented * version of the user's input. */ /* * Load the default MegaHAL personality. */ output = megahal_initial_greeting(); megahal_output(output); /* * Read input, formulate a reply and display it as output */ while(1) { input = megahal_input("> "); /* * If the input was a command, then execute it */ if (megahal_command(input) != 0) continue; output = megahal_do_reply(input, 1); megahal_output(output); } megahal_cleanup(); exit(0); }