예제 #1
0
static int cmd_line_callback(
    void * instance,
    int argc,
    const char * argv[],
    int arg,
    int * num_to_skip,
    int * ret,
    void * context
    )
{
    fc_solve_display_information_context_t * dc;
    *num_to_skip = 0;

    dc = (fc_solve_display_information_context_t * )context;

    if (!strcmp(argv[arg], "--version"))
    {
        printf(
            "fc-solve\nlibfreecell-solver version %s\n",
            freecell_solver_user_get_lib_version(instance)
        );
        *ret = EXIT_AND_RETURN_0;
        return FCS_CMD_LINE_STOP;
    }
    else if ((!strcmp(argv[arg], "-h")) || (!strcmp(argv[arg], "--help")))
    {
        char * help_key;

        help_key = getenv("FREECELL_SOLVER_DEFAULT_HELP");
        if (help_key == NULL)
        {
            help_key = "summary";
        }
        print_help_string(help_key);
        *ret = EXIT_AND_RETURN_0;
        return FCS_CMD_LINE_STOP;
    }
    else if (!strncmp(argv[arg], "--help-", 7))
    {
        print_help_string(argv[arg]+7);
        *ret = EXIT_AND_RETURN_0;
        return FCS_CMD_LINE_STOP;
    }
    else if ((!strcmp(argv[arg], "-i")) || (!strcmp(argv[arg], "--iter-output")))
    {
#define set_iter_handler() \
        freecell_solver_user_set_iter_handler(   \
            instance,   \
            my_iter_handler,   \
            dc    \
            );        \
        dc->debug_iter_output_on = TRUE;

        set_iter_handler();
    }
    else if ((!strcmp(argv[arg], "-s")) || (!strcmp(argv[arg], "--state-output")))
    {
        set_iter_handler();
        dc->debug_iter_state_output = TRUE;
#undef set_iter_handler
    }
    else if ((!strcmp(argv[arg], "-p")) || (!strcmp(argv[arg], "--parseable-output")))
    {
        dc->parseable_output = TRUE;
    }
    else if ((!strcmp(argv[arg], "-c")) || (!strcmp(argv[arg], "--canonized-order-output")))
    {
        dc->canonized_order_output = TRUE;
    }
    else if ((!strcmp(argv[arg], "-o")) || (!strcmp(argv[arg], "--output")))
    {
        arg++;
        if (arg == argc)
        {
            return FCS_CMD_LINE_STOP;
        }
        *num_to_skip = 2;
        dc->output_filename = (const char *)argv[arg];
        return FCS_CMD_LINE_SKIP;
    }
    else if ((!strcmp(argv[arg], "-t")) || (!strcmp(argv[arg], "--display-10-as-t")))
    {
        dc->display_10_as_t = TRUE;
    }
    else if ((!strcmp(argv[arg], "-m")) || (!strcmp(argv[arg], "--display-moves")))
    {
        dc->display_moves = TRUE;
        dc->display_states = FALSE;
    }
    else if ((!strcmp(argv[arg], "-sn")) || (!strcmp(argv[arg], "--standard-notation")))
    {
        dc->standard_notation = STANDARD_NOTATION_REGULAR;

    }
    else if ((!strcmp(argv[arg], "-snx")) || (!strcmp(argv[arg], "--standard-notation-extended")))
    {
        dc->standard_notation = STANDARD_NOTATION_EXTENDED;
    }
    else if ((!strcmp(argv[arg], "-sam")) || (!strcmp(argv[arg], "--display-states-and-moves")))
    {
        dc->display_moves = TRUE;
        dc->display_states = TRUE;
    }
    else if ((!strcmp(argv[arg], "-pi")) || (!strcmp(argv[arg], "--display-parent-iter")))
    {
        dc->display_parent_iter_num = TRUE;
    }
    else if ((!strcmp(argv[arg], "--reset")))
    {
        init_debug_context(dc);
        freecell_solver_user_set_iter_handler(
            instance,
            NULL,
            NULL
            );
        *num_to_skip = 0;
        return FCS_CMD_LINE_OK;
    }
    else
    {
        printf("Unimplemented option - \"%s\"!", argv[arg]);
        exit(-1);
    }
    *num_to_skip = 1;
    return FCS_CMD_LINE_SKIP;
}
예제 #2
0
파일: tags2db.c 프로젝트: xuhdev/tags2db
    int
main(int argc, const char *argv[])
{
    int             i;
    Record*         rec;
    int             code_return;
    int             tags_type = 0;
    int             db_type = 0;

    /* zero global */
    memset(&global, 0, sizeof(global));

    i = 0;
    global.tags_type_infos[i].name = "ctags";
    global.tags_type_infos[i].func_read_one_record =
        tags_ctags_read_one_record;
    ++ i;
#ifdef ENABLED_GCCXML
    global.tags_type_infos[i].name = "gccxml";
    global.tags_type_infos[i].func_read_one_record =
        tags_gccxml_read_one_record;
#endif

    global.db_type_infos[0].name = "sqlite3";
    global.db_type_infos[0].func_initialize = db_sqlite3_initialize;
    global.db_type_infos[0].func_write_one_record =
        db_sqlite3_write_one_record;
    global.db_type_infos[0].func_finalize = db_sqlite3_finalize;

    /* default field name prefix is "tags2db_" */
    global.output_db_object.field_prefix = "tags2db_";

    /* parse argument */
    {
        bool     tags_file_name_flag = false; /* indicates it's "-f" switch */
        /* indicates tags file name has already been specified */
        bool     tags_file_name_already_specified_flag = false;
        bool     tags_type_flag = false;
        bool     db_type_flag = false;
        bool     db_field_prefix_flag = false; /* -p or --prefix */

        for(i = 1; i < argc; ++i)
        {
            if(tags_file_name_flag)
            {
                tags_file_name_flag = false;

                if(!strcmp(argv[i], "-"))
                    global.input_tag_object.input_tag_file = stdin;
                else
                {
                    global.input_tag_object.input_tag_file = fopen(
                            argv[i], "r");
                    
                    if(!global.input_tag_object.input_tag_file)
                    {
                        perror("Unable to open the tag file");
                        exit(1);
                    }
                }

                tags_file_name_already_specified_flag = true;

            }
            else if(tags_type_flag)
            {
                int     j;

                tags_type_flag = false;

                /* 
                 * Check whether the specified tags type is available. If not
                 * available, print an error message and exit.
                 */
                tags_type = -1;

                for(j = 0; j < TAGS_TYPE_INFO_COUNT; ++ j)
                {
                    if(!strcmp(global.tags_type_infos[j].name, argv[i]))
                    {
                        tags_type = j;
                        break;
                    }
                }

                if(tags_type == -1)
                {
                    fprintf(stderr, "Tags type \"");
                    fprintf(stderr, argv[i]);
                    fprintf(stderr, "\" is not available.\n");
                    exit(5);
                }
            }
            else if(db_type_flag)
            {
                int     j;

                db_type_flag = false;

                /* 
                 * Check whether the specified tags type is available. If not
                 * available, print an error message and exit.
                 */
                db_type = -1;

                for(j = 0; j < DB_TYPE_INFO_COUNT; ++ j)
                {
                    if(!strcmp(global.db_type_infos[j].name, argv[i]))
                    {
                        db_type = j;
                        break;
                    }
                }

                if(db_type == -1)
                {
                    fprintf(stderr, "Database type \"");
                    fprintf(stderr, argv[i]);
                    fprintf(stderr, "\" is not available.\n");
                    exit(6);
                }
            }
            else if(db_field_prefix_flag)
            {
                db_field_prefix_flag = false;

                global.output_db_object.field_prefix = argv[i];
            }
            else if(!strcmp(argv[i], "-t"))
                tags_type_flag = true;
            else if(!strcmp(argv[i], "-d"))
                db_type_flag = true;
            else if(!strcmp(argv[i], "-f"))
                tags_file_name_flag = true;
            else if((!strcmp(argv[i], "-p")) ||
                    (!strcmp(argv[i], "--prefix")))
            {
                db_field_prefix_flag = true;
            }
            else if((!strcmp(argv[i], "-h")) || (!strcmp(argv[i], "--help")))
            {
                print_help_string();
                exit(0);
            }
            else if(!strcmp(argv[i], "--version"))
            {
                fprintf(stdout, get_version_string());
                exit(0);
            }
            else if(strlen(argv[i]) > 0 && argv[i][0] == '-')
            {
                fprintf(stderr, "Uknown option \"");
                fprintf(stderr, argv[i]);
                fprintf(stderr, "\"\n");
                exit(7);
            }
            else /* when no "-" siwtch is specified */
            {
                if(tags_file_name_already_specified_flag)
                    global.output_db_object.connection_string = strdup(
                            argv[i]);
                else
                {
                    -- i;
                    tags_file_name_flag = true;
                }
            }
        }
    }

    if(!global.input_tag_object.input_tag_file)
    {
        fprintf(stderr, "Input tag file is not specified.\n");
        exit(2);
    }

    if(!global.output_db_object.connection_string)
    {
        fprintf(stderr, "Out file path is not specified.\n");
        exit(3);
    }

    if((code_return = (*global.db_type_infos[db_type].func_initialize) (
                    &global.output_db_object)) != 0)
    {
        fprintf(stderr, "Failed to initialize sqlite3: %d\n", code_return);
        exit(4);
    }

    rec = (*global.tags_type_infos[tags_type].func_read_one_record) (
            &global.input_tag_object);

    while(rec)
    {
        if((code_return =
                    (*global.db_type_infos[db_type].func_write_one_record) (
                        &global.output_db_object, rec)) != 0)
        {
            fprintf(stderr, "Failed to write to sqlite3 database: %d\n",
                    code_return);
            break;
        }
        record_free(rec);
        rec = (*global.tags_type_infos[tags_type].func_read_one_record) (
                &global.input_tag_object);
    }

    if((code_return = (*global.db_type_infos[db_type].func_finalize) (
                    &global.output_db_object)) != 0)
    {
        fprintf(stderr, "Failed to finalize the sqlite3 database: %d\n",
                code_return);
    }
    /* close the tag file */
    fclose(global.input_tag_object.input_tag_file);

    return 0;
}
예제 #3
0
/*
 * Parse arguments
 */
void argparser_parse(argparser* ap) {
    int i, j;

    int help_passed = 0;

    for (i = 0; i < ap->argc; i++) {
        for (j = 0; j < (int)ap->size; j++) {
            argstruct* as = &ap->args[j];

            int shortmatch = as->shortarg ? strcmp(ap->argv[i], as->shortarg) == 0 : 0;
            int longmatch  = as->longarg  ? strcmp(ap->argv[i], as->longarg)  == 0 : 0;

            if (shortmatch || longmatch) {
                /* Assign arg */
                switch (as->type) {
                    case ARGTYPE_INT:
                        if (i+1 < ap->argc) {
                            *(int*)as->arg = atoi(ap->argv[++i]);
                            as->parsed = 1;
                        }
                        break;
                    case ARGTYPE_DOUBLE:
                        if (i+1 < ap->argc) {
                            *(double*)as->arg = atof(ap->argv[++i]);
                            as->parsed = 1;
                        }
                        break;
                    case ARGTYPE_STRING:
                        if (i+1 < ap->argc) {
                            strcpy((char*)as->arg, ap->argv[++i]);
                            as->parsed = 1;
                        }
                        break;
                    case ARGTYPE_BOOL:
                        *(int*)as->arg = 1;
                        as->parsed = 1;
                        break;
                }
            }
        }
    }

    /* Check if -h, --help was passed as only arg */
    if (ap->argc == 2) {
        char* arg = ap->argv[1];

        int shorthelp = strcmp(arg, "-h") == 0;
        int longhelp  = strcmp(arg, "--help") == 0;

        if (shorthelp || longhelp) {
            print_help_string(ap);
            help_passed = 1;
        }
    }

    if (help_passed) {
        argparser_destroy(ap);
        exit(EXIT_SUCCESS);
    }

    /* If strict, make sure all args were passed */
    if (!help_passed && ap->mode == PARSEMODE_STRICT) {
        int failed = 0;
        for (i = 0; i < (int)ap->size; i++) {
            argstruct as = ap->args[i];
            if (!as.parsed) {
                const char* sa = as.shortarg;
                const char* la = as.longarg;
                if (sa && la)
                    fprintf(stderr, "Failed to provide arg %s, %s\n", sa, la);
                else
                    fprintf(stderr, "Failed to provide arg %s\n", sa ? sa : la);

                failed = 1;
            }
        }
        if (failed) {
            argparser_abort(ap, "Failed to provide mandatory argument(s)");
        }
    }

    /* Cleanup */
    argparser_destroy(ap);
}
예제 #4
0
int main(void)
{
	entry* entry_head = calloc(sizeof(entry), 1);
	snapshot* snapshot_head = calloc(sizeof(snapshot), 1);

	int latest_snapshotID = 1;

	char buffer[MAX_LINE_LENGTH];

	printf("> ");

	while(fgets(buffer, sizeof(buffer), stdin))
	{
		struct command_struct *command = get_command_struct(buffer);
		if(!command) continue;


		if(strcmp(command->args_malloc_ptr[0], "bye") == 0)
		{
			bye_command(snapshot_head, entry_head);
			free_command(command);
			printf("bye");
			return 0;
		}
		else if(strcmp(command->args_malloc_ptr[0], "help") == 0)
		{
			print_help_string();
		}
		else if(strcmp(command->args_malloc_ptr[0], "list") == 0)
		{
			list_command(command, entry_head, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "get") == 0)
		{
			get_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "del") == 0)
		{
			del_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "purge") == 0)
		{
			purge_command(command, entry_head, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "set") == 0)
		{
			set_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "push") == 0)
		{
			push_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "append") == 0)
		{
			append_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pick") == 0)
		{
			pick_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pluck") == 0)
		{
			pluck_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "pop") == 0)
		{
			pop_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "drop") == 0)
		{
			drop_command(command, snapshot_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "rollback") == 0)
		{
			rollback_command(command, snapshot_head, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "checkout") == 0)
		{
			checkout_command(command, snapshot_head, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "snapshot") == 0)
		{
			snapshot_command(snapshot_head, entry_head, &latest_snapshotID);
		}
		else if(strcmp(command->args_malloc_ptr[0], "min") == 0)
		{
			min_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "max") == 0)
		{
			max_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "sum") == 0)
		{
			sum_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "len") == 0)
		{
			len_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "rev") == 0)
		{
			rev_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "uniq") == 0)
		{
			uniq_command(command, entry_head);
		}
		else if(strcmp(command->args_malloc_ptr[0], "sort") == 0)
		{
			sort_command(command, entry_head);
		}
		printf("\n> ");
		free_command(command);
	}
		bye_command(snapshot_head, entry_head);
	return 0;
}