static int red_cmd(char *cmd) { char *arg = cmd+1; SKIPSPACES(arg); switch(*cmd) { case 'q': return 0; case ';': case '#': break; // comment case '>': return cmd_dump(arg); break; case '<': return cmd_load(arg); break; case '.': return red_interpret(arg); break; case 's': return cmd_seek(arg); break; case 'b': return cmd_bsize(arg); break; case '/': return cmd_search(arg); break; case 'd': return cmd_system ("echo X | ired -n $BLOCK | rasm2 -o $OFFSET -D - |head -n $(($LINES-1))"); case 'p': return cmd_print(arg); break; case 'r': return cmd_resize(arg); break; case 'x': return cmd_hexdump(arg); break; case 'X': return cmd_bytedump(arg); break; case 'w': return cmd_write(arg); break; case '!': return cmd_system(arg); break; case 'V': return cmd_system("vired $FILE"); break; case '?': return cmd_help(arg); break; default: fprintf(stderr, "? %s\n", cmd); } return 1; }
int main(int argc, char *argv[]) { int ch; while ((ch = getopt(argc, argv, "V")) != -1) { switch (ch) { case 'V': cmd_version(NULL); exit(EX_OK); case '?': default: usage(); } } if (argc > 2) usage(); g_log_set_default_handler(log_handler, NULL); smf = smf_new(); if (smf == NULL) { g_critical("Cannot initialize smf_t."); return (-1); } if (argc == 2) cmd_load(argv[1]); else cmd_trackadd(NULL); #ifdef HAVE_LIBREADLINE rl_readline_name = "smfsh"; rl_attempted_completion_function = smfsh_completion; #endif for (;;) read_and_execute_command(); return (0); }
int parse (char *cmd) { char p1[2048], p2[2048]; int v; if (!strncasecmp (cmd, "exit", strlen ("exit")) || cmd[0] == 'q') { printf ("Exiting...\n"); return 0; } else if (!strncasecmp (cmd, "auto", strlen ("auto"))) { auto_eval ^= 1; printf ("Auto evaluation: %s\n", auto_eval ? "ON" : "OFF"); } else if (!strncasecmp (cmd, "list", strlen ("list"))) { jones_kb_dump_objects (_kb); jones_kb_dump_rules (_kb); } else if (!strncasecmp (cmd, "load", strlen ("load"))) { char *str; str = cmd; str += strlen("load") + 1; str[strlen(str) - 1] = 0; cmd_load (str); } /* --------------------------------------------------*/ /* XXX: New functions to migrate to the KB interface */ else if (!strncasecmp (cmd, "lena", strlen ("lena"))) { LENA_EXPR* e; cmd[strlen(cmd) - 1] = 0; if ((e = jones_kb_add_rule (_kb, cmd + strlen("lena ")))) { printf ("Rule %s created\n", OBJ_ID(e)); } } else if (!strncasecmp (cmd, "set", strlen ("set"))) { sscanf (cmd + strlen ("set "), "%s %s", p1, p2); if (p2[0] == 'T' || p2[0] == 't') v = FACT_TRUE; else if (p2[0] == 'F' || p2[0] == 'f') v = FACT_FALSE; else v = FACT_UNKNOWN; jones_kb_add_fact (_kb, p1, v, NULL); } else if (!strncasecmp (cmd, "run", strlen ("run"))) { jones_kb_run (_kb); } else if (cmd[strlen(cmd) - 2] == '?') // FACT Query { jones_kb_fact_query (_kb, cmd); } return 1; }
int main ( int argc , char** argv ) { /* * Do initialization steps here. */ /* * Set up list of player id's. */ char* players [2] = { "r" , "b" }; /* * Attempt to initialize the game state to the default. * * Add more error checking? */ State* state = build_state ( ); FILE* init_file = fopen ( INIT_FILENAME , "r" ); bool initialized = init_file && state_load ( state , init_file ); fclose ( init_file ); if ( ! initialized ) return 1; /* * The top level of program logic resides nested within this loop, * which runs until one of the players sends the termination signal. * * Note that this is an infinite loop. The actual control logic for * this loop is a single goto statment in the verb interpreter, set * to jump outside the loop if and only if the VERB_TERM is * encountered. DO NOT BE ALARMED. THIS IS A LEGITIMATE USE OF THE * GOTO STATEMENT! */ while ( true ) { /* * The logic inside this loop effectively handles a single * round, from the starting position to reseting the game. */ /* * Initialize the player indexer and player. */ unsigned short indexer = 0; char* player = players[0]; /* * While a game is still in progress (i.e. while both players * have legal moves available), this loop alternates between * the red and black players, starting with the red player. * * The control statement for this loop determines whether the * player has any vaild moves. If the player does not have any * valid moves, the control logic of the interpreter loop is * modified to force the losing player to fix the situation. * This could change in the future, so with minor changes, * control could just be made to fall through to a reset * after this loop, it just depends on what happens during * development. */ while ( state_has_moves ( state , player ) ) { /* * The logic inside this loop effectively handles a single * turn, allowing an unlimited number of commands which are * not moves to be performed, followed by a single move. * * Notice that both players must be trusted for this system * to work as-is, otherwise any player could modify the * state of the game to their advantage. This is a standing * security issue, but one which can be ignored at the * moment for the academic purposes of the project. */ /* * Initialize the keep_turn flag. */ bool keep_turn = true; /* * While the player has not yet made a move, it is still * the player's turn. Thus, this loop terminates only after * a move has been made, processing any other commands * the player gives during the current turn. Control stays * with the player anyways if he has no moves, as he has * lost and the losing player is responsible for resetting * the board. */ while ( keep_turn || ( ! state_has_moves ( state , player ) ) ) { /* * This logic effectively handles a single command from * the current player. This involves reading the command * from the player's input pipe, interpreting and * executing the command, and returning the result of * the command to the player as appropriate. */ /* * Initialize the command buffer. */ //char* cmd = malloc ( sizeof ( char ) * CMD_BUFFER_SIZE ); char* cmd = NULL; size_t cmdlen = 0; /* * Read the next command from stdin. * * Might try and modify this to avoid the buffer. */ getline ( &cmd , &cmdlen , stdin ); /* * Extract the verb of the command, this is always the * first word of the command, in a HTTP-like format. */ char* verb = strtok ( cmd , " \n" ); /* * Use an if-else chain statement to select the proper * code to execute for each verb. */ if ( ! verb ) { /* * If the entry was just a newline, ignore it. */ } else if ( ! strcmp ( verb , VERB_TERM ) ) { /* * If the term signal is given, then jump to the * term_exit branch. */ goto term_exit; } else if ( ! strcmp ( verb , VERB_LOAD ) ) { cmd_load ( cmd , state ); } else if ( ! strcmp ( verb , VERB_SAVE ) ) { cmd_save ( cmd , state ); } else if ( ! strcmp ( verb , VERB_TGET ) ) { cmd_tget ( cmd , state ); } else if ( ! strcmp ( verb , VERB_TSET ) ) { cmd_tset ( cmd , state ); } else if ( ! strcmp ( verb , VERB_MOVE ) ) { /* * This code executes if the verb is the move command. * It then parses the remainder of the command to * determine its validity. If the command is well- * structured and the move is legal, the move is made * and the moved flag set to true. Otherwise, a 400 * BAD SYNTAX error will be returned. If the move * requested is well-formed but illegal, a 403 MOVE * FORBIDDEN error is returned. Assuming no errors * occur, a 200 OK message is returned. */ cmd_move ( cmd , state ); keep_turn = false; } else { /* * If we don't recognize the given verb, we do the * safe thing and return the verb nonexistant error. */ //puts ( "PCIP/1.0 404 NOTVERB" ); puts ( "NULL" ); } /* * Deallocate the command buffer. */ free ( cmd ); } /* * Increment the player indexer with wraparound. Set the * pipes pointing to the current player's input and * output. * * Trust me, I have a reason for putting it here. */ indexer = ( indexer + 1 ) % PLAYER_COUNT; player = players[indexer]; state->player = *player; /* * Announce the turn change. */ printf ( "PCIP/1.0 201 NEW TURN\n%c\n" , *player ); } /* * This is the interpreter loop's aforementioned implicit fall * through case for processing end of game conditions. * * Given the termination condition of the previous loop, the * player input and output pipes now point to the loosing * player, who is now responsible for resetting the game. */ } /* * If the term command is given, the program jumps to this point, * just before deallocation of top-level dynamic resources occurs. * Note that breaking out of a switch statement and a triple nested * do-while loop *is* a legitimate use of goto. */ term_exit: /* * Clean up. */ /* * Deallocate the game state. */ free_state ( state ); /* * If we got this far without an incident, the program should have * run successfully, so we return the corresponding status code. */ return EXIT_SUCCESS; };
int main(int argc, char**argv) { char *savedgame = NULL; int usesignal = 1; int i; setupfilenames(); for (i=1;i<argc;i++) { if (!strcmp(argv[i], "-load")) savedgame = argv[++i]; else if (!strcmp(argv[i], "-xsignal")) usesignal = 0; else if (!strcmp(argv[i], "-book")) bookfile = argv[++i]; else if (!strcmp(argv[i], "-binbook")) { binary_book = 1; bookfile = "book.bin"; } else if (!strcmp(argv[i], "-weights")) weightfile = argv[++i]; else if (!strcmp(argv[i], "-xbook")) book_mode = 0; else if (!strcmp(argv[i], "-xweights")) weight_mode = 0; else if (!strcmp(argv[i], "-seed")) { seed = atol(argv[++i]); seed_with_time = 0; } else printf("unknown command line switch: %s\n", argv[i]); } printf("Book: %s\n", bookfile); printf("Weight: %s\n", weightfile); if (usesignal) signal(SIGINT, myhandler); atexit(exit_engine); /* turn off buffering */ setbuf(stdout, NULL); setbuf(stderr, NULL); if (!weight_mode||!load_weights(weightfile)) { if (weight_mode) fprintf(stderr,"main: could not load weights, using defaults\n"); else printf("ignoreing weight file, using defaults\n"); setup_default_weights(); } board = malloc(sizeof(chessboard)); /* default seed for binary book */ seed_rng(DEFAULT_RANDOM_SEED); compute_randoms(); /* randomize for random book moves */ seed_randoms(); for (;;) { if (!savedgame) initialize(); else cmd_load(savedgame-1); playchess(); } return 0; }
/* execute command */ EXPORT INT exec_cmd(B *cmd) { INT ac; B *av[N_ARGS]; ac = setup_param(cmd, av); if (ac < 1) return 0; if (strcmp(av[0], "date") == 0) { cmd_date(ac, av); } else if (strcmp(av[0], "attach") == 0) { cmd_attach(ac, av); } else if (strcmp(av[0], "detach") == 0) { cmd_detach(ac, av); } else if (strcmp(av[0], "mkdir") == 0) { cmd_mkdir(ac, av); } else if (strcmp(av[0], "rmdir") == 0) { cmd_rmdir(ac, av); } else if (strcmp(av[0], "pwd") == 0) { cmd_pwd(ac, av); } else if (strcmp(av[0], "cd") == 0) { cmd_cd(ac, av); } else if (strcmp(av[0], "rm") == 0) { cmd_rm(ac, av); } else if (strcmp(av[0], "mv") == 0) { cmd_mv(ac, av); } else if (strcmp(av[0], "ls") == 0) { cmd_ls(ac, av); } else if (strcmp(av[0], "tp") == 0 || strcmp(av[0], "tpx") == 0) { cmd_tp(ac, av); } else if (strcmp(av[0], "cp") == 0) { cmd_cp(ac, av); } else if (strcmp(av[0], "trunc") == 0) { cmd_trunc(ac, av); } else if (strcmp(av[0], "df") == 0) { cmd_df(ac, av); } else if (strcmp(av[0], "sync") == 0) { cmd_sync(ac, av); } else if (strcmp(av[0], "chmod") == 0) { cmd_chmod(ac, av); } else if (strcmp(av[0], "ref") == 0) { cmd_ref(ac, av); } else if (strcmp(av[0], "load") == 0) { cmd_load(ac, av); } else if (strcmp(av[0], "loadspg") == 0) { cmd_loadspg(ac, av); } else if (strcmp(av[0], "unload") == 0) { cmd_unload(ac, av); } else if (strcmp(av[0], "call") == 0) { cmd_call(ac, av); } else if (strncmp(av[0], "?", 1) == 0) { P("date [y m d [h m s]]\n"); P("attach devnm connm\n"); P("detach connm\n"); P("cd dir\n"); P("pwd \n"); P("ls [-t][-l][dir]\n"); P("mkdir dir [mode]\n"); P("rmdir dir\n"); P("rm path\n"); P("mv o-path n-path\n"); P("trunc path len\n"); P("df path\n"); P("sync [path [d]]\n"); P("chmod path mode\n"); P("tp path [ofs len]\n"); P("tpx path [ofs len]\n"); P("cp s-path d-path/dir [wofs [wlen]]\n"); P("ref [item]\n"); P("call addr [p1 p2 p3]\n"); P("load path\n"); P("loadspg path [arg ...]\n"); P("unload progid\n"); #ifdef NET_SAMPLE P("net execute network sample\n"); } else if (strcmp(av[0], "net") == 0) { IMPORT void net_test(void); net_test(); #endif } else { return 0; } return 1; }