void init_megahal() { megahal_setnoprompt(); megahal_setnobanner(); megahal_setdirectory((char*)"/Users/ras0219/projects/zinc"); megahal_initialize(); }
static PyObject *mhsetdirectory(PyObject *self, PyObject *args) { char *input; if (!PyArg_ParseTuple(args, "s", &input)) return NULL; megahal_setdirectory(input); return Py_BuildValue(""); }
int main(int argc, char **argv) { char *port = "8080"; char *mhdir = NULL; int c; while(1) { static struct option long_options[] = { { "help", no_argument, 0, '?' }, { "port", optional_argument, 0, 'p' }, { "dir", optional_argument, 0, 'd' }, { 0, 0, 0, 0} }; int option_index = 0; c = getopt_long(argc, argv, "?p:", long_options, &option_index); if (c == -1) break; switch (c) { case 'p': port = optarg; break; case 'd': mhdir = optarg; break; case '?': printf("usage: megahal-server --port=<port> --dir=<megahal-brain-directory>\n"); exit(1); } } printf("megahal-server: port set to %s and dir set to %s\n",port,mhdir); struct mg_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; const char *options[] = {"listening_ports", port, NULL}; pthread_mutex_init(&mhlock, NULL); if (mhdir) { megahal_setdirectory(mhdir); } megahal_initialize(); signal(SIGINT, cleanup); signal(SIGTERM, cleanup); char *mhout = megahal_initial_greeting(); printf("MegaHal initial greeting: %s\n", mhout); struct mg_context *ctx; ctx = mg_start(&callbacks, 0, options); /* while(1) { char *i = megahal_input("> "); if (megahal_command(i) != 0) continue; mhout = megahal_do_reply(i, 1); megahal_output(mhout); } */ while(keep_running) { sleep(1); } printf("Stopping and cleaning up.\n"); megahal_cleanup(); mg_stop(ctx); return 0; }
/* * 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); }