Esempio n. 1
0
static int x_issue_special_command(const char * line, Command_Options *copts, Dictionary dict)
{
	char *s, myline[1000], *x, *y;
	int i, count, j, k;
	Switch * as = default_switches;
	Parse_Options opts = copts->popts;

	strncpy(myline, line, sizeof(myline));
	myline[sizeof(myline)-1] = '\0';
	clean_up_string(myline);

	s = myline;
	j = k = -1;
	count = 0;

	/* Look for boolean flippers */
	for (i=0; as[i].string != NULL; i++)
	{
		if ((Bool == as[i].param_type) && 
		    strncasecmp(s, as[i].string, strlen(s)) == 0)
		{
			count++;
			j = i;
		}
	}

	/* Look for abbreviations */
	for (i=0; user_command[i].s != NULL; i++)
	{
		if (strncasecmp(s, user_command[i].s, strlen(s)) == 0)
		{
			count++;
			k = i;
		}
	}

	if (count > 1)
	{
		printf("Ambiguous command.  Type \"!help\" or \"!variables\"\n");
		return -1;
	}
	else if (count == 1)
	{
		/* flip boolean value */
		if (j >= 0)
		{
			setival(as[j], (0 == ival(as[j])));
			printf("%s turned %s.\n", as[j].description, (ival(as[j]))? "on" : "off");
			return 0;
		}
		else
		{
			/* Found an abbreviated command, but it wasn't a boolean */
			/* Replace the abbreviated command by the full one */
			/* Basically, this just fixes !v and !h for use below. */
			strcpy(s, user_command[k].s);
		}
	}

	if (strcmp(s, "variables") == 0)
	{
		printf(" Variable     Controls                                      Value\n");
		printf(" --------     --------                                      -----\n");
		for (i = 0; as[i].string != NULL; i++)
		{
			printf(" ");
			left_print_string(stdout, as[i].string, "             ");
			left_print_string(stdout, as[i].description,
			            "                                              ");
			if (Float == as[i].param_type)
			{
				/* Float point print! */
				printf("%5.2f", *((double *)as[i].ptr));
			}
			else
			if ((Bool == as[i].param_type) || Int == as[i].param_type)
			{
				printf("%5d", ival(as[i]));
			}
			else
			if (String == as[i].param_type)
			{
				printf("%s", *(char **)as[i].ptr);
			}
			if (Bool == as[i].param_type)
			{
				if (ival(as[i])) printf(" (On)"); else printf(" (Off)");
			}
			printf("\n");
		}
		printf("\n");
		printf("Toggle a boolean variable as in \"!batch\"; ");
		printf("set a variable as in \"!width=100\".\n");
		return 0;
	}

	if (strcmp(s, "help") == 0)
	{
		printf("Special commands always begin with \"!\".  Command and variable names\n");
		printf("can be abbreviated.  Here is a list of the commands:\n\n");
		for (i=0; user_command[i].s != NULL; i++) {
			printf(" !");
			left_print_string(stdout, user_command[i].s, "               ");
			left_print_string(stdout, user_command[i].str, "                                                    ");
			printf("\n");
		}
		printf(" !!<string>      Print all the dictionary words that matches <string>.\n");
		printf("                 A wildcard * may be used to find multiple matches.\n");
		printf("\n");
		printf(" !<var>          Toggle the specified boolean variable.\n");
		printf(" !<var>=<val>    Assign that value to that variable.\n");
		return 0;
	}

	if (s[0] == '!')
	{
		dict_display_word_info(dict, s+1, opts);
		dict_display_word_expr(dict, s+1, opts);
		return 0;
	}
#ifdef USE_REGEX_TOKENIZER
	if (s[0] == '/')
	{
		int rc = regex_tokenizer_test(dict, s+1);
		if (0 != rc) printf("regex_tokenizer_test: rc %d\n", rc);
		return 0;
	}
#endif

	/* Test here for an equation i.e. does the command line hold an equals sign? */
	for (x=s; (*x != '=') && (*x != '\0') ; x++)
	  ;
	if (*x == '=')
	{
		*x = '\0';
		y = x+1;
		x = s;
		/* now x is the first word and y is the rest */

		/* Figure out which command it is .. it'll be the j'th one */
		j = -1;
		for (i=0; as[i].string != NULL; i++)
		{
			if (strncasecmp(x, as[i].string, strlen(x)) == 0)
			{
				j = i;
				count ++;
			}
		}

		if (j<0)
		{
			printf("There is no user variable called \"%s\".\n", x);
			return -1;
		}

		if (count > 1)
		{
			printf("Ambiguous variable.  Type \"!help\" or \"!variables\"\n");
			return -1;
		}

		if ((as[j].param_type == Int) || (as[j].param_type == Bool))
		{
			int val = -1;
			if (is_numerical_rhs(y)) val = atoi(y);

			if ((0 == strcasecmp(y, "true")) || (0 == strcasecmp(y, "t"))) val = 1;
			if ((0 == strcasecmp(y, "false")) || (0 == strcasecmp(y, "f"))) val = 0;

			if (val < 0)
			{
				printf("Invalid value %s for variable %s Type \"!help\" or \"!variables\"\n", y, as[j].string);
				return -1;
			}

			setival(as[j], val);
			printf("%s set to %d\n", as[j].string, val);
			return 0;
		}
		else
		if (as[j].param_type == Float)
		{
			double val = -1.0;
			val = atof(y);
			if (val < 0.0)
			{
				printf("Invalid value %s for variable %s Type \"!help\" or \"!variables\"\n", y, as[j].string);
				return -1;
			}

			*((double *) as[j].ptr) = val;
			printf("%s set to %5.2f\n", as[j].string, val);
			return 0;
		}
		else
		if (as[j].param_type == String)
		{
			*((char **) as[j].ptr) = y;
			printf("%s set to %s\n", (char *)as[j].string, y);
			return 0;
		}
		else
		{
			printf("Internal error: Unknown variable type %d\n", as[j].param_type);
			return -1;
		}
	}

	/* Look for valid commands, but ones that needed an argument */
	j = -1;
	count = 0;
	for (i = 0; as[i].string != NULL; i++)
	{
		if ((Bool != as[i].param_type) && 
		    strncasecmp(s, as[i].string, strlen(s)) == 0)
		{
			j = i;
			count++;
		}
	}

	if (0 < count)
	{
		printf("Variable \"%s\" requires a value.  Try \"!help\".\n", as[j].string);
		return -1;
	}

	printf("I can't interpret \"%s\" as a command.  Try \"!help\".\n", myline);
	return -1;
}
Esempio n. 2
0
void x_issue_special_command(char * line, Parse_Options opts, Dictionary dict) {
    char *s, myline[1000], *x, *y;
    int i, count, j, k;
    Switch * as = default_switches;

    strncpy(myline, line, sizeof(myline));
    myline[sizeof(myline)-1] = '\0';
    clean_up_string(myline);

    s = myline;
    j = k = -1;
    count = 0;
    for (i=0; as[i].string != NULL; i++) {
	if (as[i].isboolean && strncasecmp(s, as[i].string, strlen(s)) == 0) {
	    count++;
	    j = i;
	}
    }
    for (i=0; user_command[i].s != NULL; i++) {
	if (strncasecmp(s, user_command[i].s, strlen(s)) == 0) {
	    count++;
	    k = i;
	}
    }

    if (count > 1) {
	printf("Ambiguous command.  Type \"!help\" or \"!variables\"\n");
	return;
    } 
    else if (count == 1) {
	if (j >= 0) {
	    *as[j].p = !(*as[j].p);
	    printf("%s turned %s.\n", as[j].description, (*as[j].p)? "on" : "off");
	    return;
	} 
	else {
	    /* replace the abbreviated command by the full one */
	    strcpy(s, user_command[k].s);  
	}
    }



    if (strcmp(s, "variables")==0) {
	printf(" Variable     Controls                                      Feature_value\n");
	printf(" --------     --------                                      -----\n");
	for (i=0; as[i].string != NULL; i++) {
	    printf(" ");
	    left_print_string(stdout, as[i].string, "             ");
	    left_print_string(stdout, as[i].description, "                                              ");
	    printf("%5d", *as[i].p);
	    if (as[i].isboolean) {
		if (*as[i].p) printf(" (On)"); else printf(" (Off)");
	    }
	    printf("\n");
	}
	printf("\n");
	printf("Toggle a boolean variable as in \"!batch\"; ");
	printf("set a variable as in \"!width=100\".\n");
	return;
    }
    if (strcmp(s, "help")==0) {
	printf("Special commands always begin with \"!\".  Command and variable names\n");
	printf("can be abbreviated.  Here is a list of the commands:\n\n");
	for (i=0; user_command[i].s != NULL; i++) {
	    printf(" !");
	    left_print_string(stdout, user_command[i].s, "                  ");
	    left_print_string(stdout, user_command[i].str, "                                                    ");
	    printf("\n");
	}
	printf(" !!<string>         Print all the dictionary words matching <string>.\n");
	printf("                    Also print the number of disjuncts of each.\n");
	printf("\n");
	printf(" !<var>             Toggle the specified boolean variable.\n");
	printf(" !<var>=<val>       Assign that value to that variable.\n");
	return;
    }

    if(s[0] == '!') {
	dict_display_word_info(dict, s+1);
	return;
    }
    
    /* test here for an equation */
    for (x=s; (*x != '=') && (*x != '\0') ; x++)
      ;
    if (*x == '=') {
	*x = '\0';
	y = x+1;
	x = s;
	/* now x is the first word and y is the rest */

	if (is_numerical_rhs(y)) {
	    for (i=0; as[i].string != NULL; i++) {
		if (strcmp(x, as[i].string) == 0) break;
	    }
	    if (as[i].string  == NULL) {
		printf("There is no user variable called \"%s\".\n", x);
	    } else {
		*(as[i].p) = atoi(y);
		printf("%s set to %d\n", x, atoi(y));
	    }
	    return;
	}
    }

    printf("I can't interpret \"%s\" as a command.  Try \"!help\".\n", myline);
}