nissy-nx

A Rubik's cube optimal solver
git clone https://git.tronto.net/nissy-nx
Download | Log | Files | Refs | README | LICENSE

shell.c (2392B)


      1 #define SHELL_C
      2 
      3 #include "shell.h"
      4 
      5 static void             cleanwhitespaces(char *line);
      6 static int              parseline(char *line, char **v);
      7 
      8 static void
      9 cleanwhitespaces(char *line)
     10 {
     11 	char *i;
     12 
     13 	for (i = line; *i != 0; i++)
     14 		if (*i == '\t' || *i == '\n')
     15 			*i = ' ';
     16 }
     17 
     18 /* This function assumes that **v is large enough */
     19 static int
     20 parseline(char *line, char **v)
     21 {
     22 	char *t;
     23 	int n = 0;
     24 	
     25 	cleanwhitespaces(line);
     26 
     27 	for (t = strtok(line, " "); t != NULL; t = strtok(NULL, " "))
     28 		strcpy(v[n++], t);
     29 
     30 	return n;
     31 }
     32 
     33 void
     34 exec_args(int c, char **v)
     35 {
     36 	int i;
     37 	char line[MAXLINELEN];
     38 	Command *cmd = NULL;
     39 	CommandArgs *args;
     40 	Alg *scramble;
     41 
     42 	for (i = 0; commands[i] != NULL; i++)
     43 		if (!strcmp(v[0], commands[i]->name))
     44 			cmd = commands[i];
     45 
     46 	if (cmd == NULL) {
     47 		fprintf(stderr, "%s: command not found\n", v[0]);
     48 		return;
     49 	}
     50 
     51 	args = cmd->parse_args(c-1, &v[1]);
     52 	if (!args->success) {
     53 		fprintf(stderr, "usage: %s\n", cmd->usage);
     54 		return;
     55 	}
     56 
     57 	if (args->scrstdin) {
     58 		while (true) {
     59 			if (fgets(line, MAXLINELEN, stdin) == NULL) {
     60 				clearerr(stdin);
     61 				break;
     62 			}
     63 			
     64 			scramble = new_alg(line);
     65 
     66 			printf(">>> Line: %s", line);
     67 
     68 			if (scramble != NULL && scramble->len > 0) {
     69 				args->scramble = scramble;
     70 				cmd->exec(args);
     71 				free_alg(scramble);
     72 				args->scramble = NULL;
     73 			} 
     74 		}
     75 	} else {
     76 		cmd->exec(args);
     77 	}
     78 	free_args(args);
     79 }
     80 
     81 void
     82 launch(bool batchmode)
     83 {
     84 	int i, shell_argc;
     85 	char line[MAXLINELEN], **shell_argv;
     86 
     87 	shell_argv = malloc(MAXNTOKENS * sizeof(char *));
     88 	for (i = 0; i < MAXNTOKENS; i++)
     89 		shell_argv[i] = malloc((MAXTOKENLEN+1) * sizeof(char));
     90 
     91 	if (!batchmode) {
     92 		fprintf(stderr, "Welcome to Nissy "VERSION".\n"
     93 				"Type \"commands\" for a list of commands.\n");
     94 	}
     95 
     96 	while (true) {
     97 		if (!batchmode) {
     98 			fprintf(stdout, "nissy-# ");
     99 		}
    100 
    101 		if (fgets(line, MAXLINELEN, stdin) == NULL)
    102 			break;
    103 
    104 		if (batchmode) {
    105 			printf(">>>\n"
    106 			       ">>> Executing command: %s"
    107 			       ">>>\n", line);
    108 		}
    109 
    110 		shell_argc = parseline(line, shell_argv);
    111 
    112 		if (shell_argc > 0)
    113 			exec_args(shell_argc, shell_argv);
    114 	}
    115 
    116 	for (i = 0; i < MAXNTOKENS; i++)
    117 		free(shell_argv[i]);
    118 	free(shell_argv);
    119 }
    120 
    121 #ifndef TEST
    122 int
    123 main(int argc, char *argv[])
    124 {
    125 	init_env();
    126 	init_trans();
    127 
    128 	if (argc > 1) {
    129 		if (!strcmp(argv[1], "-b")) {
    130 			launch(true);
    131 		} else {
    132 			exec_args(argc-1, &argv[1]);
    133 		}
    134 	} else {
    135 		launch(false);
    136 	}
    137 
    138 	return 0;
    139 }
    140 #endif