Ejemplo n.º 1
0
/*  copy_field2buf

    copies a buffer to another...

    Will copy for bufsize - 1 to leave space for '\0'.
*/
void copy_field2buf(char *buf, char *fieldbuf, size_t bufsize)
{
    size_t i = 0;

    /* safety */
    if (!buf || !fieldbuf)
        abort();

    /* copy while:
        1. we are inside the target buffers size
        2. we are inside the sources buffer size
    */
    for (i = 0;
            fieldbuf[i] != ' ' && i < bufsize - 1 && i < StrMemLen(fieldbuf);
            i++) {
        buf[i] = fieldbuf[i];
    }
    buf[i] = '\0';
}
Ejemplo n.º 2
0
/*  create_newwin

    creates a window, sets it background and sets a title.

    Returns a pointer to the window or NULL in case of failure.
*/
WINDOW *create_newwin(int height, int width, int starty, int startx,
        const char *title, chtype ch)
{
    WINDOW *local_win = NULL;
    size_t memsize = 0, screensize = 0;
    char *title_ptr = NULL;

    vrmr_fatal_if(height <= 0 || width <= 0 || starty < 0 || startx < 0);

    /* create the window */
    if (!(local_win = newwin(height, width, starty, startx)))
        return (NULL);

    /* box and background */
    (void)box(local_win, 0, 0);
    (void)wbkgd(local_win, ch);

    /* draw title if we have one */
    if (title != NULL) {
        memsize = StrMemLen(title);
        screensize = StrLen(title);

        if ((int)screensize + 4 <= width) {
            title_ptr = malloc(memsize + 3);
            if (title_ptr == NULL) {

            } else {
                snprintf(title_ptr, memsize + 3, " %s ", title);
                mvwprintw(local_win, 0, (int)(((size_t)width - screensize) / 2),
                        title_ptr);
                free(title_ptr);
            }
        } else {
            vrmr_warning(gettext("Warning"),
                    gettext("title '%s' too long, window will be drawn without "
                            "a title."),
                    title);
        }
    }

    return (local_win);
}
Ejemplo n.º 3
0
/*  strip the buf src from the spaces before the text. Leave other
    spaces alone.
*/
void
strip_buf(char *src, char *dst, size_t dstsize)
{
    size_t  i = 0,
            k = 0;
    char    copy_space = 0;

    for(i = 0; i < dstsize && i < StrMemLen(src); i++)
    {
        if(src[i] != ' ')
            copy_space = 1;

        if(src[i] != ' ' || copy_space == 1)
        {
            dst[k] = src[i];
            k++;
        }
    }
    dst[k] = '\0';
}
Ejemplo n.º 4
0
/* parse a line from the helpfile */
int
read_helpline(const int debuglvl, struct vrmr_list *help_list, char *line)
{
    char        oneword[512] = "";
    size_t      i = 0;
    size_t      k = 0;
    helpword    *hw = NULL;

    for(i = 0, k = 0; i < StrMemLen(line); i++)
    {
        if(line[i] == ' ' || line[i] == '\n')
        {
            oneword[k] = '\0';
            k = 0;

            /* only add a word to the list if it really contains characters */
            if(StrLen(oneword) > 0)
            {
                /* get some mem for the word struct */
                if(!(hw = malloc(sizeof(helpword))))
                {
                    vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__);
                    return(-1);
                }
                hw->word = NULL;
                hw->newline = 0;
                hw->line_num = 0;

                if(!(hw->word = malloc(StrMemLen(oneword) + 1)))
                {
                    vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__);
                    return(-1);
                }
                (void)strlcpy(hw->word, oneword,
                        StrMemLen(oneword) + 1);

                if(vrmr_list_append(debuglvl, help_list, hw) == NULL)
                {
                    vrmr_error(-1, VR_INTERR, "append to list failed (in: %s:%d).", __FUNC__, __LINE__);
                    return(-1);
                }
            }

            /* the newline is a special word */
            if( (i == 0 && line[i] == '\n') ||

                (i > 0  && line[i] == '\n' &&
                (line[i-1] == '.' ||
                 line[i-1] == '?' ||
                 line[i-1] == '!' ||
                 line[i-1] == ':' ||
                (line[i-1] == ',' && i == (StrMemLen(line) - 1) ))
                )
            )
            {
                /* get some mem for the word struct */
                if(!(hw = malloc(sizeof(helpword))))
                {
                    vrmr_error(-1, VR_ERR, gettext("malloc failed: %s (in: %s:%d)."), strerror(errno), __FUNC__, __LINE__);
                    return(-1);
                }
                hw->word = NULL;
                hw->newline = 1;
                hw->line_num = 0;

                if(vrmr_list_append(debuglvl, help_list, hw) == NULL)
                {
                    vrmr_error(-1, VR_INTERR, "append to list (in: %s:%d).", __FUNC__, __LINE__);
                    return(-1);
                }
            }
        }
        else
        {
            oneword[k] = line[i];
            k++;
        }
    }

    return(0);
}
Ejemplo n.º 5
0
int
read_helpfile(const int debuglvl, struct vrmr_list *help_list, char *part)
{
    char    line[128] = "";
    FILE    *fp = NULL;
    char    inrange = 0;
    char    helpfile[256] = "";

    /* safety */
    if(!help_list || !part)
    {
        vrmr_error(-1, VR_INTERR, "parameter problem "
                "(in: %s:%d).", __FUNC__, __LINE__);
        return(-1);
    }

    /* setup the list */
    if(vrmr_list_setup(debuglvl, help_list, free_helpword) < 0)
    {
        vrmr_error(-1, VR_INTERR, "vrmr_list_setup failed "
                "(in: %s:%d).", __FUNC__, __LINE__);
        return(-1);
    }

    /* TRANSLATORS: translate this to you language code: so for
       'ru' use 'vuurmuur-ru.hlp', for 'pt_BR' use
       'vuurmuur-pt_BR.hlp'
     */
    if(snprintf(helpfile, sizeof(helpfile), "%s/%s",
            vccnf.helpfile_location,
            gettext("vuurmuur.hlp")) >= (int)sizeof(helpfile))
    {
        vrmr_error(-1, "Error", "buffer too small for "
                    "helpfile supplied at compile-time "
                    "(in: %s:%d).", __FUNC__, __LINE__);
        return(-1);
    }
    vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile));

    /* open the file */
    fp = fopen(helpfile, "r");
    if(fp == NULL)
    {
        vrmr_debug(__FUNC__, "opening '%s' failed: %s, "
                "falling back to default.",
                helpfile, strerror(errno));

        /* language helpfile does not exist, try to fall back to default */
        if(snprintf(helpfile, sizeof(helpfile), "%s/vuurmuur.hlp",
                vccnf.helpfile_location) >= (int)sizeof(helpfile))
        {
            vrmr_error(-1, "Error", "buffer too small for "
                    "helpfile supplied at compile-time "
                    "(in: %s:%d).", __FUNC__, __LINE__);
            return(-1);
        }
        vrmr_sanitize_path(debuglvl, helpfile, sizeof(helpfile));

        if(!(fp = fopen(helpfile, "r")))
        {
            vrmr_error(-1, VR_ERR, "%s %s: %s",
                    STR_OPENING_FILE_FAILED,
                    helpfile, strerror(errno));
            return(-1);
        }
    }

    while(fgets(line, (int)sizeof(line), fp) != NULL)
    {
        if(inrange)
        {
            if(strcmp(line, ":[END]:\n") == 0)
            {
                /* implied inrange = 0; */
                break;
            }
        }

        if(inrange)
        {
            if(read_helpline(debuglvl, help_list, line) < 0)
                return(-1);
        }
        else
        {
            if(strncmp(line, part, StrMemLen(part)) == 0)
                inrange = 1;
        }
    }
    
    fclose(fp);
    return(0);
}
Ejemplo n.º 6
0
/*

    returns 1 if yes, 0 if no
*/
int confirm(const char *title, const char *text, chtype forecolor,
        chtype backcolor, int def)
{
    int retval = 0;
    ITEM **menu_items;
    MENU *confirm_menu;
    PANEL *my_panels[1];
    WINDOW *confirm_win, *dw;
    ITEM *cur;

    int height = 7, width = 25, startx = 5, starty = 5, max_x = 0, max_y = 0;
    const char *choices[] = {STR_YES, STR_NO};

    size_t n_choices = 2, i = 0;

    int ch, quit = 0;

    char *print_title;

    /* safety */
    vrmr_fatal_if_null(title);
    vrmr_fatal_if_null(text);

    if (width - 4 < (int)StrLen(text))
        width = (int)StrLen(text) + 4;
    if (width - 6 < (int)StrLen(title))
        width = (int)StrLen(title) + 6;
    getmaxyx(stdscr, max_y, max_x);
    startx = (max_x - width) / 2;
    starty = (max_y - height) / 2;

    print_title = malloc(StrMemLen(title) + 3);
    vrmr_fatal_alloc("malloc", print_title);
    snprintf(print_title, StrMemLen(title) + 3, " %s ", title);

    menu_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *));
    vrmr_fatal_alloc("calloc", menu_items);
    for (i = 0; i < n_choices; ++i) {
        menu_items[i] = new_item(choices[i], NULL);
    }
    menu_items[n_choices] = (ITEM *)NULL;
    confirm_menu = new_menu((ITEM **)menu_items);
    vrmr_fatal_if_null(confirm_menu);
    confirm_win = newwin(height, width, starty, startx);
    wbkgd(confirm_win, backcolor);
    keypad(confirm_win, TRUE);
    wrefresh(confirm_win);
    my_panels[0] = new_panel(confirm_win);
    set_menu_win(confirm_menu, confirm_win);
    dw = derwin(confirm_win, height - 4, 10, 4, (width) / 2 - 5);
    set_menu_sub(confirm_menu, dw);
    set_menu_format(confirm_menu, height - 4, 2);
    box(confirm_win, 0, 0);
    print_in_middle(confirm_win, 0, 0, width, print_title, backcolor);
    print_in_middle(confirm_win, 2, 0, width, text, backcolor);
    set_menu_back(confirm_menu, backcolor);
    set_menu_fore(confirm_menu, forecolor);
    post_menu(confirm_menu);

    /* set the cursor to the 'no' position */
    if (!def) {
        menu_driver(confirm_menu, REQ_RIGHT_ITEM);
    }
    update_panels();
    doupdate();

    while (quit == 0) {
        ch = wgetch(confirm_win);
        switch (ch) {
            case KEY_DOWN:
                menu_driver(confirm_menu, REQ_LEFT_ITEM);
                break;
            case KEY_UP:
                menu_driver(confirm_menu, REQ_RIGHT_ITEM);
                break;
            case KEY_LEFT:
                menu_driver(confirm_menu, REQ_LEFT_ITEM);
                break;
            case KEY_RIGHT:
                menu_driver(confirm_menu, REQ_RIGHT_ITEM);
                break;

            case 10: // enter
            {
                cur = current_item(confirm_menu);
                vrmr_fatal_if_null(cur);
                if (strcmp((char *)item_name(cur), STR_YES) == 0) {
                    retval = 1;
                }
                quit = 1;
                break;
            }

            case 'y':
            case 'Y':
                retval = 1;
                quit = 1;
                break;

            case 'n':
            case 'N':
                retval = 0;
                quit = 1;
                break;

            case 27:
            case KEY_F(10):
            case 'q':
            case 'Q':
                quit = 1;
                break;
        }
    }

    unpost_menu(confirm_menu);
    free_menu(confirm_menu);
    for (i = 0; i < n_choices; ++i)
        free_item(menu_items[i]);
    free(menu_items);
    destroy_win(dw);
    del_panel(my_panels[0]);
    destroy_win(confirm_win);
    free(print_title);
    update_panels();
    doupdate();
    return (retval);
}