Beispiel #1
0
int Command_depends(apr_pool_t *p, const char *path)
{
    FILE *in = NULL;
    bstring line = NULL;

    in = fopen(path, "r");

    check(in != NULL, "Failed to open downloaded depends: %s", path);

    for(line = bgets((bNgetc)fgetc, in, '\n'); line != NULL;
            line = bgets((bNgetc)fgetc, in, '\n'))
    {
        btrimws(line);
        log_info("Processing depends: %s", bdata(line));
        int rc = Command_install(p, bdata(line), NULL, NULL, NULL);
        check(rc == 0, "Failed to install: %s", bdata(line));
        bdestroy(line);
    }

    fclose(in);
    return 0;

error:
    if(line) bdestroy(line);
    if(in) fclose(in);
    return -1;
}
Beispiel #2
0
TSTree *load_routes(const char *file)
{
    TSTree *routes = NULL;
    bstring line = NULL;
    FILE *routes_map = NULL;

    routes_map = fopen(file, "r");
    check(routes_map != NULL, "Failed to open routes %s", file);

    while((line = bgets((bNgetc)fgetc, routes_map, '\n')) != NULL){
        check(btrimws(line) == BSTR_OK, "Failed to trim line.");
        routes = add_route_data(routes, line);
        check(routes != NULL, "Failed to add route.");
        bdestroy(line);
    }

    fclose(routes_map);
    return routes;

error:
    if(routes_map) fclose(routes_map);
    if(line) bdestroy(line);

    return NULL;
}
Beispiel #3
0
TSTree *load_routes(const char *file)
/* open file , read line by line  the pairs of key & data
 * use add route data to store them in the tree
 */
{
	TSTree *routes = NULL;
	bstring line = NULL;
	FILE *routes_map = NULL;
	
	routes_map = fopen(file , "r");
	check(routes_map != NULL , "failed to open routes: %s " , file );
	
	while((line = bgets((bNgetc)fgetc , routes_map ,'\n')) != NULL )
	{
		check(btrimws(line) == BSTR_OK , "failed to trim line");
		routes = add_route_data(routes , line);
		check(routes != NULL , "failed to add route");
		bdestroy(line);
	}

	fclose(routes_map);
	return routes;
error:
	if(routes_map) fclose(routes_map);
	if(line) bdestroy(line);
	
	return NULL;

}
Beispiel #4
0
void rparse_do(char* yytext, int* out_line, bstring* out_filename)
{
    bstring str = bfromcstr(yytext);
    bstring space = bfromcstr(" ");

    // Determine the space positions.
    int firstSpace = binstr(str, 0, space);
    int secondSpace = binstr(str, firstSpace + 1, space);

    // Create substrings.
    bstring bline = bmidstr(str, firstSpace + 1, secondSpace - firstSpace);
    bstring file = bmidstr(str, secondSpace + 1, blength(str) - secondSpace);

    // Convert the line number and trim file name.
    int line = strtoul((const char*)bline->data, NULL, 10);
    btrimws(file);

    // Set variables.
    if (*out_filename == NULL)
        *out_filename = bfromcstr("");
    bassign(*out_filename, file);
    *out_line = line;

    // Free resources.
    bdestroy(file);
    bdestroy(bline);
    bdestroy(space);
    bdestroy(str);
}
Beispiel #5
0
int Command_depends(apr_pool_t *p , const char* path){
/* read from the depend file of some package line by line(which is downloaded from
 * network or be a local file generated by teh command_fetch
 * for each depends , install it ,  use the apr functions in command_install
 * to install
 */
	FILE *in = NULL;
	bstring line = NULL;

	in = fopen(path , "r");
	check(in != NULL , "faled to open download depends: %s" , path);

	for( line = bgets((bNgetc)fgetc , in , '\n');line != NULL;
		line = bgets((bNgetc)fgetc , in , '\n'))
	{
		btrimws(line);
	log_info("process depends: %s" , bdata(line));
	int rc = Command_install(p , bdata(line),NULL ,NULL ,NULL);
	check(rc == 0 , "failed to install: %s" , bdata(line));
	bdestroy(line);
	}

	fclose(in);
	return 0;

error:
	if(line) bdestroy(line);
	if(in) fclose(in);
	return -1;
}
Beispiel #6
0
/* Accepts a filepath, and installs all dependencies.
 *    does not fetch the file, that's done by Command_fetch. */
int Command_depends(apr_pool_t *p, const char *path) {
    FILE *in = NULL;
    bstring line = NULL;

    in = fopen(path, "r");
    check(in != NULL, "Failed to open downloaded depends: %s", path);

    // bgets is like fgets. See db.c for a deeper discussion of what's
    // going on with the cast -- we are casting fgetc to a type bstringlib
    // likes, then using it to do a gets op. The cast is probably illegal in
    // ANSI C, in which case we would need to define a wrapper, but gcc
    // apparently lets us get away with it.
    for (line = bgets((bNgetc)fgetc, in, '\n');
         line != NULL;
         line = bgets((bNgetc)fgetc, in, '\n')) {
        // trim whitewspace, the bstrlib equiv of python's strip
        btrimws(line);
        log_info("Processing depends: %s", bdata(line));
        // note tha dependencies are assumed not to need options
        int rc = Command_install(p, bdata(line), NULL, NULL, NULL);
        check(rc == 0, "Failed to install: %s", bdata(line));
        bdestroy(line);
    }
    fclose(in);
    return 0;
error:
    if (line) bdestroy(line);
    if (in) fclose(in);
    return -1;
}
Beispiel #7
0
Object*
String_to_object(bstring string)
{
  Object *obj = NULL;

  if (bchar(string, 0) == '"') {
    int len = blength(string) - 2;
    obj = Object_create_string(bmidstr(string, 1, len));
  } else if (bchar(string, 0) == '[') {
    int strlen = blength(string) - 2;
    bstring inner_str = bmidstr(string, 1, strlen);
    struct bstrList *elements = bsplit(inner_str, ',');

    int len = elements->qty;
    int i = 0;

    DArray *array = DArray_create(sizeof(Object*), len);
    bstring *ptr = elements->entry;
    for(i = 0; i < len; i++) {
      btrimws(*ptr);
      DArray_push(array, String_to_object(*ptr));
      ptr++;
    }
    obj = Object_create_array(array);
  } else {
    int value = atoi(bdata(string));
    if (value != 0) {
      obj = Object_create_integer(atoi(bdata(string)));
    } else {
      return NULL;
    }
  }
  return obj;
}
Beispiel #8
0
bstring read_line(const char *prompt)
{
	printf("%s" , prompt);
	
	bstring result = bgets((bNgetc)fgetc , stdin , '\n');
	check_debug(result != NULL , "stdin closed");
	
	check(btrimws(result) == BSTR_OK , "failed to trim.");

	return result;
error:
	return NULL;
}
Beispiel #9
0
static void macro_handle(state_t* state, match_t* match, bool* reprocess)
{
    bstring name;
    bstring temp;
    list_t parameters;
    list_t arguments;
    bool getting_name = true;
    bool getting_parameters = false;
    bool getting_arguments = false;
    struct replace_info* info = match->userdata;
    int i = 0;
    int argument_brackets = 0;
    char c;
    char* start_loc;
    match_t* new_match;
    struct replace_info* new_info;

    // Parse the parameters out of the name.
    list_init(&parameters);
    temp = bfromcstr("");
    for (i = 0; i < blength(info->full); i++)
    {
        c = info->full->data[i];
        if (getting_name)
        {
            if (c == '(')
            {
                getting_name = false;
                getting_parameters = true;
                name = bstrcpy(temp);
                bassigncstr(temp, "");
            }
            else
                bconchar(temp, c);
        }
        else if (getting_parameters)
        {
            if (c == ',' || c == ')')
            {
                btrimws(temp);
                list_append(&parameters, bstrcpy(temp));
                bassigncstr(temp, "");
                if (c == ')')
                {
                    getting_parameters = false;
                    break;
                }
            }
            else
                bconchar(temp, c);
        }
    }

    // Attempt to accept an open bracket.
    c = ppimpl_get_input(state);
    while (c == '\1')
    {
        // Consume macro termination.
        i = 0;
        while (i < strlen("\1MACROTERMINATE\1"))
        {
            if (c != "\1MACROTERMINATE\1"[i++])
                dhalt(ERR_PP_EXPECTED_OPEN_BRACKET, ppimpl_get_location(state));
            c = ppimpl_get_input(state);
        }
        ppimpl_pop_scope(state);
    }
    if (c != '(')
        dhalt(ERR_PP_EXPECTED_OPEN_BRACKET, ppimpl_get_location(state));
    
    // Read arguments.
    getting_arguments = true;
    list_init(&arguments);
    start_loc = ppimpl_get_location(state);
    bassigncstr(temp, "");
    while (ppimpl_has_input(state) && getting_arguments)
    {
        c = ppimpl_get_input(state);

        if (c == '(')
        {
            argument_brackets++;
            bconchar(temp, c);
        }
        else if (c == ')' && argument_brackets != 0)
        {
            argument_brackets--;
            bconchar(temp, c);
        }
        else if (c == ')' && argument_brackets == 0)
        {
            list_append(&arguments, bstrcpy(temp));
            bassigncstr(temp, "");
            getting_arguments = false;
            break;
        }
        else if (c == ',' && argument_brackets == 0)
        {
            list_append(&arguments, bstrcpy(temp));
            bassigncstr(temp, "");
        }
        else
            bconchar(temp, c);
    }
    if (getting_arguments)
        dhalt(ERR_PP_NO_TERMINATING_BRACKET, start_loc);

    // Check to see if the argument count is correct.
    if (list_size(&arguments) > list_size(&parameters))
        dhalt(ERR_PP_TOO_MANY_PARAMETERS, start_loc);
    else if (list_size(&arguments) < list_size(&parameters))
        dhalt(ERR_PP_NOT_ENOUGH_PARAMETERS, start_loc);
    free(start_loc);

    // Create a new scope for macro evaluation.
    ppimpl_push_scope(state, true);

    // Define the new handlers.
    for (i = 0; i < list_size(&parameters); i++)
    {
        new_info = malloc(sizeof(struct replace_info));
        new_info->full = list_get_at(&parameters, i);
        new_info->replacement = list_get_at(&arguments, i);
        if (biseq(new_info->full, new_info->replacement))
        {
            free(new_info);
            continue;
        }
        new_match = malloc(sizeof(match_t));
        new_match->text = bautofree(list_get_at(&parameters, i));
        new_match->handler = replace_handle;
        new_match->line_start_only = false;
        new_match->identifier_only = true;
        new_match->userdata = new_info;
        new_match->case_insensitive = false;
        ppimpl_register(state, new_match);
    }
    
    // Print out the macro evaluation and terminator.
    ppimpl_printf(state, "%s\1MACROTERMINATE\1", info->replacement->data);
}
Beispiel #10
0
static void define_handle(state_t* state, match_t* match, bool* reprocess)
{
    // We need to parse this manually because we're interested in getting
    // the first word and then all of the content until a line that doesn't end
    // with "\".
    bstring name = bfromcstr("");
    bstring word = bfromcstr("");
    bstring definition = bfromcstr("");
    bool getting_word = true;
    bool getting_definition = true;
    bool is_macro = false;
    match_t* new_match;
    struct replace_info* info;

    // Get the first word.
    while (getting_word)
    {
        char c = ppimpl_get_input(state);
        bconchar(word, c);
        if (!is_macro && c != '(')
            bconchar(name, c);
        bltrimws(word);

        // Handle termination.
        if (blength(word) > 0 && (c == ' ' || c == '\t') && !is_macro)
        {
            // End of word.
            btrimws(word);
            btrimws(name);
            getting_word = false;
        }
        else if (blength(word) > 0 && c == '(' && !is_macro)
        {
            // Start of macro.
            is_macro = true;
        }
        else if (blength(word) > 0 && c == '(' && is_macro)
        {
            // Second ( in a macro; error.
            dhalt(ERR_PP_MACRO_MALFORMED, ppimpl_get_location(state));
        }
        else if (blength(word) > 0 && c == ')' && is_macro)
        {
            // End of macro name.
            btrimws(word);
            btrimws(name);
            getting_word = false;
        }
        else if (blength(word) == 0 && c == '\n')
            dhalt(ERR_PP_C_DEFINE_PARAMETERS_INCORRECT, ppimpl_get_location(state));
        else if (blength(word) > 0 && c == '\n')
        {
            // End of word.
            btrimws(word);
            btrimws(name);
            getting_word = false;
            getting_definition = false;
            ppimpl_printf(state, "\n");
        }
    }

    // Get the definition.
    while (getting_definition)
    {
        char c = ppimpl_get_input(state);
        bconchar(definition, c);
        bltrimws(definition);
        
        if (c == '\n')
        {
            if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '\\')
            {
                // Remove the new slash.
                bdelete(definition, blength(definition) - 2, 1);
                ppimpl_oprintf(state, "\n");
            }
            else
            {
                btrimws(definition);
                getting_definition = false;
                ppimpl_printf(state, "\n");
            }
        }
        else if (c == '/' || c == '*')
        {
            if (blength(definition) > 1 && definition->data[blength(definition) - 2] == '/')
            {
                // a line or block comment
                ppimpl_iprintf(state, "/%c", c);
                // remove the slashes
                bdelete(definition, blength(definition) - 2, 2);
                btrimws(definition);
                getting_definition = false;
            }
        }
    }
    
    if (blength(definition) == 0 && !is_macro)
        bassigncstr(definition, "1");
        

    // Create the new replacement handler.
    info = malloc(sizeof(struct replace_info));
    info->full = word;
    info->replacement = definition;
    if (biseq(info->full, info->replacement))
    {
        free(info);
        return;
    }
    new_match = malloc(sizeof(match_t));
    new_match->text = bautofree(name);
    if (is_macro)
        new_match->handler = macro_handle;
    else
        new_match->handler = replace_handle;
    new_match->line_start_only = false;
    new_match->identifier_only = true;
    new_match->userdata = info;
    new_match->case_insensitive = false;
    ppimpl_register(state, new_match);
    *reprocess = true;
}
Beispiel #11
0
bool do_search(CURL* curl, bstring name, bool all)
{
    DIR* dir;
    bool printed;
    CURLcode res;
    FILE* fp;
    list_t installed;
    struct bStream* stream;
    long httpcode = 0;
    bstring buffer, fname, sstr;
    bstring ext = bfromcstr(".lua");
    bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/search?q=");
    bstring modpath = osutil_getmodulepath();
    struct dirent* entry;
    list_init(&installed);
    list_attributes_copy(&installed, list_meter_string, 1);
    list_attributes_comparator(&installed, list_comparator_string);

    // Attempt to open the modules directory.
    dir = opendir(modpath->data);
    if (dir == NULL)
    {
        printd(LEVEL_ERROR, "unable to query local repository.\n");
        return 1;
    }

    // Append the temporary search file name.
    bcatcstr(modpath, "/.search");
    bconcat(url, name);

    // Open the file and do the cURL transfer.
    printd(LEVEL_DEFAULT, "querying module repository...\n");
    fp = fopen(modpath->data, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url->data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
    if (res != 0 || httpcode != 200)
    {
        bdestroy(url);
        bdestroy(name);
        bdestroy(modpath);
        printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode);
        return 1;
    }
    fclose(fp);

    // Print the local results.
    if (all)
        printd(LEVEL_DEFAULT, "all modules:\n");
    else
        printd(LEVEL_DEFAULT, "search results for %s:\n", name->data);
    while ((entry = readdir(dir)) != NULL)
    {
        fname = bfromcstr(&entry->d_name[0]);
        if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (binstr(fname, 0, name) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (entry->d_type != DT_REG)
        {
            bdestroy(fname);
            continue;
        }
        sstr = bmidstr(fname, 0, blength(fname) - 4);
        printd(LEVEL_DEFAULT, "   %s (installed)\n", sstr->data);
        list_append(&installed, sstr->data);
        bdestroy(sstr);
        bdestroy(fname);
    }

    // Print the online results.
    fp = fopen(modpath->data, "r");
    stream = bsopen(&read_data, fp);
    buffer = bfromcstr("");
    printed = false;
    while (bsreadln(buffer, stream, '\n') != BSTR_ERR)
    {
        btrimws(buffer);
        sstr = bmidstr(buffer, 0, blength(buffer) - 4);
        if (!list_contains(&installed, sstr->data))
            printd(LEVEL_DEFAULT, "  %s\n", sstr->data);
        printed = true;
        bdestroy(sstr);
    }
    if (!printed)
        printd(LEVEL_DEFAULT, "   <no online results>\n");
    bsclose(stream);
    fclose(fp);

    // Clean up.
    curl_easy_cleanup(curl);
    return 0;
}
Beispiel #12
0
bool do_install_all(CURL* curl)
{
    // define used variables
    DIR* dir;
    FILE* fp;
    CURLcode res;
    bool printed;
    bool install_status;
    bool something_errored;
    bool if_something_was_installed;
    list_t installed;
    long httpcode = 0;
    struct dirent* entry;
    struct bStream* stream;
    bstring buffer, fname, sstr;
    bstring modpath = osutil_getmodulepath();
    bstring ext = bfromcstr(".lua");
    bstring url = bfromcstr("http://dms.dcputoolcha.in/modules/list");
    list_init(&installed);
    list_attributes_copy(&installed, list_meter_string, 1);
    list_attributes_comparator(&installed, list_comparator_string);

    // Attempt to open the modules directory.
    dir = opendir(modpath->data);
    if (dir == NULL)
    {
        printd(LEVEL_ERROR, "unable to query local repository.\n");
        return 1;
    }

    // add the filename we wish to query to the modules folder path
    bcatcstr(modpath, "/.all_avail");

    // Open the file and do the cURL transfer.
    printd(LEVEL_DEFAULT, "loading a list of all the modules...\n");
    fp = fopen(modpath->data, "wb");
    curl_easy_setopt(curl, CURLOPT_URL, url->data);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
    res = curl_easy_perform(curl);
    curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpcode);
    if (res != 0 || httpcode != 200)
    {
        bdestroy(url);
        bdestroy(modpath);
        printd(LEVEL_ERROR, "curl failed with error code %i, HTTP error code %i.\n", res, httpcode);
        return 1;
    }
    fclose(fp);


    // create a list of already installed modules
    while ((entry = readdir(dir)) != NULL)
    {
        fname = bfromcstr(&entry->d_name[0]);
        if (binstr(fname, blength(fname) - 4, ext) == BSTR_ERR)
        {
            bdestroy(fname);
            continue;
        }
        if (entry->d_type != DT_REG)
        {
            bdestroy(fname);
            continue;
        }
        sstr = bmidstr(fname, 0, blength(fname) - 4);
        list_append(&installed, sstr->data);
        bdestroy(sstr);
        bdestroy(fname);
    }

    printd(LEVEL_DEFAULT, "\n");

    // Print the names of the modules, and install them through the do_install function
    fp = fopen(modpath->data, "r");
    stream = bsopen(&read_data, fp);
    buffer = bfromcstr("");
    printed = false;
    if_something_was_installed = false;
    something_errored = false;
    while (bsreadln(buffer, stream, '\n') != BSTR_ERR)
    {
        btrimws(buffer);
        sstr = bmidstr(buffer, 0, blength(buffer) - 4);

        // if the module is not already installed
        if (!list_contains(&installed, sstr->data))
        {
            install_status = do_install(curl, bfromcstr(sstr->data));
            if_something_was_installed = true;
            // check whether the installation was successful
            if (install_status != 0)
            {
                printd(LEVEL_DEFAULT, "  %s failed to install.\n", sstr->data);
                something_errored = true;
            }
            printd(LEVEL_DEFAULT, "\n");
        }

        printed = true;
        bdestroy(sstr);
    }
    if (!printed)
        printd(LEVEL_DEFAULT, "  <no modules available>\n");
    if (something_errored)
        printd(LEVEL_DEFAULT, "errors occured\n");
    if (!if_something_was_installed)
        printd(LEVEL_DEFAULT, "no changes were made\n");
    bsclose(stream);
    fclose(fp);

    // Clean up.
    curl_easy_cleanup(curl);

    return 0;
}
Beispiel #13
0
void
asciiBoxes_print(BoxContainer* container)
{
    int width;
    int boxwidth=0; /* box width is inner width of box */

    /* determine maximum label width */
    for ( int i=0; i < container->numLines; i++ )
    {
        for ( int j=0; j < container->numColumns; j++ )
        {
            btrimws(container->boxes[i][j].label);
            boxwidth = MAX(boxwidth,blength(container->boxes[i][j].label));

            /* if box is joined increase counter */
            if ( container->boxes[i][j].width > 1 )
            {
                j +=  container->boxes[i][j].width;
            }
        }
    }
    boxwidth += 2;  /* add one space each side */

    /* top line */
    printf("+");

    for ( int i=0; i < (container->numColumns * (boxwidth+2) +
                (container->numColumns+1));  /* one space between boxes */
            i++ )
    {
        printf("-");
    }
    printf("+\n");

    for ( int i=0; i < container->numLines; i++ )
    {
        /* Box top line */
        printf("| ");

        for ( int j=0; j < container->numColumns; j++ )
        {
            printf("+");

            if ( container->boxes[i][j].width == 1 )
            {
                for ( int k=0; k < boxwidth; k++ )
                {
                    printf("-");
                }
            }
            else 
            {
                for ( int k=0; k < (container->boxes[i][j].width * boxwidth +
                            (container->boxes[i][j].width-1)*3);
                        k++)
                {
                    printf("-");
                }
                j += container->boxes[i][j].width-1;
            }
            printf("+ ");
        }
        printf("|\n");
        printf("| ");

        /* Box label line */
        for ( int j=0; j < container->numColumns; j++ )
        {
            int offset=0;

            /* center label */
            if ( container->boxes[i][j].width == 1 )
            {
                width = (boxwidth - blength(container->boxes[i][j].label))/2;
                offset = (boxwidth - blength(container->boxes[i][j].label))%2;
            }
            else
            {
                width = (container->boxes[i][j].width * boxwidth +
                        ((container->boxes[i][j].width-1)*3) -
                        blength(container->boxes[i][j].label))/2;

                offset = (container->boxes[i][j].width * boxwidth +
                        ((container->boxes[i][j].width-1)*3) -
                        blength(container->boxes[i][j].label))%2;
            }
            printf("|");

            for ( int k=0; k < (width+offset); k++ )
            {
                printf(" ");
            }

            printf("%s",container->boxes[i][j].label->data);

            for ( int k=0; k < width; k++ )
            {
                printf(" ");
            }
            printf("| ");

            if ( container->boxes[i][j].width != 1 )
            {
                j+= container->boxes[i][j].width-1;
            }
        }
        printf("|\n");
        printf("| ");

        /* Box bottom line */
        for ( int j=0; j < container->numColumns; j++ )
        {
            printf("+");

            if ( container->boxes[i][j].width == 1 )
            {
                for ( int k=0; k < boxwidth; k++ )
                {
                    printf("-");
                }
            }
            else 
            {
                for ( int k=0; k < (container->boxes[i][j].width * boxwidth +
                            (container->boxes[i][j].width-1)*3);
                        k++ )
                {
                    printf("-");
                }
                j+= container->boxes[i][j].width-1;
            }
            printf("+ ");
        }
        printf("|\n");
    }

    /* bottom line */
    printf("+");
    for ( int i=0; i < (container->numColumns * (boxwidth+2) + 
                container->numColumns+1); i++ )
    {
        printf("-");
    }
    printf("+\n");
}