/* Update command line arguments passing to clang translation unit. Format
   of the coming CMDLINEARGS message is as follows:
   
       num_args: [#n_args#]
       arg1 arg2 ... (there should be n_args items here)
*/
void completion_doCmdlineArgs(completion_Session *session, FILE *fp)
{
    int i_arg = 0;
    char arg[LINE_MAX];

    /* destroy command line args, and we will rebuild it later */
    completion_freeCmdlineArgs(session);

    /* get number of arguments */
    fscanf(fp, "num_args:%d", &(session->num_args)); __skip_the_rest(fp);
    session->cmdline_args = (char**)calloc(sizeof(char*), session->num_args);

    /* rebuild command line arguments vector according to the message */
    for ( ; i_arg < session->num_args; i_arg++)
    {
        /* fetch an argument from message */
        fscanf(fp, "%s", arg);

        /* and add it to cmdline_args */
        session->cmdline_args[i_arg] = (char*)calloc(sizeof(char), strlen(arg) + 1);
        strcpy(session->cmdline_args[i_arg], arg);
    }

    /* we have to rebuild our translation units to make these cmdline args changes 
       take place */
    clang_disposeTranslationUnit(session->cx_tu);
    completion_parseTranslationUnit(session);
    completion_reparseTranslationUnit(session);  /* dump PCH for acceleration */
}
/* Initialize session object and launch the completion server, preparse the source file and 
   build the AST for furture code completion requests  
*/
void startup_completionSession(int argc, char *argv[], completion_Session *session)
{
    __initialize_completionSession(argc, argv, session);

    /* default parameters */
    session->ParseOptions      = DEFAULT_PARSE_OPTIONS;
    session->CompleteAtOptions = DEFAULT_COMPLETEAT_OPTIONS;

    session->cx_index = clang_createIndex(0, 0);
    completion_parseTranslationUnit(session);
    completion_reparseTranslationUnit(session);
}