int main() {
    char *s, *buf, *cmd, **args;
    int i, pos;
    buf = malloc(STRING_SIZE * sizeof(char));
    cmd = malloc(STRING_SIZE * sizeof(char));
    if (buf == NULL || cmd == NULL) {
        printf("Impossivel alocar memoria.\n");
        exit(EXIT_FAILURE);
    }

    agenda ag = createAgenda();
    carregaContatos(ag);

    args = malloc(ARGS_SIZE * sizeof(char*));
    for (i = 0; i < ARGS_SIZE; i++) {
        args[i] = malloc(STRING_SIZE * sizeof(char));
        if (args[i] == NULL) {
            printf("Impossivel alocar memoria para argumentos.\n");
            exit(EXIT_FAILURE);
        }
    }

    printf("Agenda Topicos de Programacao 2016\n");
    while (1) {
        printf("> ");
        if (fgets(buf, STRING_SIZE, stdin) == NULL) normalexit(NULL, 0, NULL, ag);

        if (sscanf(buf, " %s %n", cmd, &pos) == EOF) {
            // evita que um comando seja chamado duas vezes caso uma string vazia seja passada
            cmd = "";
            pos = 0;
        }

        s = buf + pos;

        i = 0;
        while (sscanf(s, "%s %n", args[i], &pos) != EOF) {
            i++;
            s = s + pos;
        }

        int (*foo)(char*, int, char**, agenda) = findCommand(cmd);
        (*foo)(cmd, i, args, ag);
    }

    free(buf);
    free(cmd);

    return EXIT_SUCCESS;
}
Exemple #2
0
/*************************************************************************
* Programme principal
*************************************************************************/
int main(int argc, char *argv[]) {
    int end_Menu = 0;

    db_Used active_Db;
    memset(&active_Db, sizeof(active_Db), 0);

    while(!end_Menu) {
        switch(showMenu(active_Db)) {
            case SELECT_AGENDA: selectAgenda(&active_Db); break;
            case NEW_AGENDA: createAgenda(); break;
            case APPEND_ENTRY: newEntry(&active_Db); break;
            case IMPORT_ENTRIES: importEntries(&active_Db); break;
            case SEARCH_PART: searchPart(&active_Db); break;
            case SEARCH_NUM: searchNum(&active_Db); break;
            case READ_ALL: readAll(&active_Db); break;

            case QUIT: printf("Bye bye !\n\n"); end_Menu = 1; break;
            default: printf("Error !\nBad Entry, try again.\n\n"); break;
        }
    }

    return 0;
}