X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=test%2Fcardfile.c;h=bf03755f658acc879e4da5aa82ba057f54298486;hp=08d33ed43cc0d8014e4a8f3a894215e8e95f943f;hb=9b4c4abadc0a29999c5ddad5aa8d769fee28d687;hpb=a8987e73ec254703634802b4f7ee30d3a485524d;ds=inline diff --git a/test/cardfile.c b/test/cardfile.c index 08d33ed4..bf03755f 100644 --- a/test/cardfile.c +++ b/test/cardfile.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 1999-2002,2003 Free Software Foundation, Inc. * + * Copyright 2019,2020 Thomas E. Dickey * + * Copyright 1999-2016,2017 Free Software Foundation, Inc. * * * * Permission is hereby granted, free of charge, to any person obtaining a * * copy of this software and associated documentation files (the * @@ -27,9 +28,9 @@ ****************************************************************************/ /* - * Author: Thomas E. Dickey 1999 + * Author: Thomas E. Dickey * - * $Id: cardfile.c,v 1.23 2003/04/26 16:43:56 tom Exp $ + * $Id: cardfile.c,v 1.47 2020/02/02 23:34:34 tom Exp $ * * File format: text beginning in column 1 is a title; other text is content. */ @@ -43,6 +44,10 @@ #define VISIBLE_CARDS 10 #define OFFSET_CARD 2 +#define pair_1 1 +#define pair_2 2 + +#define isVisible(cardp) ((cardp)->panel != 0) enum { MY_CTRL_x = MAX_FORM_COMMAND @@ -61,19 +66,16 @@ typedef struct _card { } CARD; static CARD *all_cards; +static bool try_color = FALSE; static char default_name[] = "cardfile.dat"; -#if !HAVE_STRDUP -#define strdup my_strdup -static char * -strdup(char *s) +static void +failed(const char *s) { - char *p = (char *) malloc(strlen(s) + 1); - if (p) - strcpy(p, s); - return (p); + perror(s); + endwin(); + ExitProgram(EXIT_FAILURE); } -#endif /* not HAVE_STRDUP */ static const char * skip(const char *buffer) @@ -86,7 +88,7 @@ skip(const char *buffer) static void trim(char *buffer) { - unsigned n = strlen(buffer); + size_t n = strlen(buffer); while (n-- && isspace(UChar(buffer[n]))) buffer[n] = 0; } @@ -106,7 +108,7 @@ add_title(const char *title) break; } - card = (CARD *) calloc(1, sizeof(CARD)); + card = typeCalloc(CARD, (size_t) 1); card->title = strdup(title); card->content = strdup(""); @@ -124,20 +126,29 @@ add_title(const char *title) static void add_content(CARD * card, const char *content) { - unsigned total, offset; + size_t total; content = skip(content); if ((total = strlen(content)) != 0) { - if ((offset = strlen(card->content)) != 0) { + size_t offset; + + if (card->content != 0 && (offset = strlen(card->content)) != 0) { total += 1 + offset; - card->content = (char *) realloc(card->content, total + 1); - strcpy(card->content + offset++, " "); + card->content = typeRealloc(char, total + 1, card->content); + if (card->content) { + _nc_STRCPY(card->content + offset, " ", total + 1 - offset); + offset++; + } } else { + offset = 0; if (card->content != 0) free(card->content); - card->content = (char *) malloc(total + 1); + card->content = typeMalloc(char, total + 1); } - strcpy(card->content + offset, content); + if (card->content) + _nc_STRCPY(card->content + offset, content, total + 1 - offset); + else + failed("add_content"); } } @@ -165,10 +176,11 @@ static void read_data(char *fname) { FILE *fp; - CARD *card = 0; - char buffer[BUFSIZ]; if ((fp = fopen(fname, "r")) != 0) { + CARD *card = 0; + char buffer[BUFSIZ]; + while (fgets(buffer, sizeof(buffer), fp)) { trim(buffer); if (isspace(UChar(*buffer))) { @@ -189,15 +201,17 @@ static void write_data(const char *fname) { FILE *fp; - CARD *p = 0; - int n; if (!strcmp(fname, default_name)) fname = "cardfile.out"; if ((fp = fopen(fname, "w")) != 0) { + CARD *p = 0; + for (p = all_cards; p != 0; p = p->link) { FIELD **f = form_fields(p->form); + int n; + for (n = 0; f[n] != 0; n++) { char *s = field_buffer(f[n], 0); if (s != 0 @@ -238,7 +252,8 @@ order_cards(CARD * first, int depth) if (first) { if (depth && first->link) order_cards(first->link, depth - 1); - top_panel(first->panel); + if (isVisible(first)) + top_panel(first->panel); } } @@ -248,8 +263,13 @@ order_cards(CARD * first, int depth) static CARD * next_card(CARD * now) { - if (now->link) - now = now->link; + if (now->link != 0) { + CARD *tst = now->link; + if (isVisible(tst)) + now = tst; + else + (void) next_card(tst); + } return now; } @@ -260,9 +280,24 @@ static CARD * prev_card(CARD * now) { CARD *p; - for (p = all_cards; p != 0; p = p->link) - if (p->link == now) + for (p = all_cards; p != 0; p = p->link) { + if (p->link == now) { + if (!isVisible(p)) + p = prev_card(p); return p; + } + } + return now; +} + +/* + * Returns the first card in the list that we will display. + */ +static CARD * +first_card(CARD * now) +{ + if (!isVisible(now)) + now = next_card(now); return now; } @@ -280,8 +315,8 @@ form_virtualize(WINDOW *w) return (MY_CTRL_N); case CTRL('P'): return (MY_CTRL_P); - case CTRL('Q'): - case 033: + case QUIT: + case ESCAPE: return (MY_CTRL_Q); case KEY_BACKSPACE: @@ -308,7 +343,7 @@ form_virtualize(WINDOW *w) static FIELD ** make_fields(CARD * p, int form_high, int form_wide) { - FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *)); + FIELD **f = typeCalloc(FIELD *, (size_t) 3); f[0] = new_field(1, form_wide, 0, 0, 0, 0); set_field_back(f[0], A_REVERSE); @@ -336,7 +371,7 @@ show_legend(void) #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS static void -free_form_fields(FIELD ** f) +free_form_fields(FIELD **f) { int n; @@ -355,23 +390,36 @@ cardfile(char *fname) WINDOW *win; CARD *p; CARD *top_card; - int visible_cards = count_cards(); - int panel_wide = COLS - (visible_cards * OFFSET_CARD); - int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5; - int form_wide = panel_wide - 2; - int form_high = panel_high - 2; - int y = (visible_cards - 1) * OFFSET_CARD; - int x = 0; - int ch = ERR; - int last_ch; + int visible_cards; + int panel_wide; + int panel_high; + int form_wide; + int form_high; + int y; + int x; int finished = FALSE; show_legend(); + /* decide how many cards we can display */ + visible_cards = count_cards(); + while ( + (panel_wide = COLS - (visible_cards * OFFSET_CARD)) < 10 || + (panel_high = LINES - (visible_cards * OFFSET_CARD) - 5) < 5) { + --visible_cards; + } + form_wide = panel_wide - 2; + form_high = panel_high - 2; + y = (visible_cards - 1) * OFFSET_CARD; + x = 0; + /* make a panel for each CARD */ for (p = all_cards; p != 0; p = p->link) { - win = newwin(panel_high, panel_wide, y, x); + if ((win = newwin(panel_high, panel_wide, y, x)) == 0) + break; + + wbkgd(win, (chtype) COLOR_PAIR(pair_2)); keypad(win, TRUE); p->panel = new_panel(win); box(win, 0, 0); @@ -385,13 +433,15 @@ cardfile(char *fname) x += OFFSET_CARD; } - order_cards(top_card = all_cards, visible_cards); + top_card = first_card(all_cards); + order_cards(top_card, visible_cards); while (!finished) { + int ch = ERR; + update_panels(); doupdate(); - last_ch = ch; ch = form_virtualize(panel_window(top_card->panel)); switch (form_driver(top_card->form, ch)) { case E_OK: @@ -432,6 +482,8 @@ cardfile(char *fname) FIELD **oldf = form_fields(p->form); WINDOW *olds = form_sub(p->form); + if (!isVisible(p)) + continue; win = form_win(p->form); /* move and resize the card as needed @@ -477,21 +529,19 @@ cardfile(char *fname) } #if NO_LEAKS while (all_cards != 0) { - FIELD **f; - int count; - p = all_cards; all_cards = all_cards->link; - f = form_fields(p->form); - count = field_count(p->form); + if (isVisible(p)) { + FIELD **f = form_fields(p->form); - unpost_form(p->form); /* ...so we can free it */ - free_form(p->form); /* this also disconnects the fields */ + unpost_form(p->form); /* ...so we can free it */ + free_form(p->form); /* this also disconnects the fields */ - free_form_fields(f); + free_form_fields(f); - del_panel(p->panel); + del_panel(p->panel); + } free(p->title); free(p->content); free(p); @@ -499,6 +549,22 @@ cardfile(char *fname) #endif } +static void +usage(void) +{ + static const char *msg[] = + { + "Usage: cardfile [options] file" + ,"" + ,"Options:" + ," -c use color if terminal supports it" + }; + size_t n; + for (n = 0; n < SIZEOF(msg); n++) + fprintf(stderr, "%s\n", msg[n]); + ExitProgram(EXIT_FAILURE); +} + /*******************************************************************************/ int @@ -508,11 +574,32 @@ main(int argc, char *argv[]) setlocale(LC_ALL, ""); + while ((n = getopt(argc, argv, "c")) != -1) { + switch (n) { + case 'c': + try_color = TRUE; + break; + default: + usage(); + } + } + initscr(); cbreak(); noecho(); - if (argc > 1) { + if (try_color) { + if (has_colors()) { + start_color(); + init_pair(pair_1, COLOR_WHITE, COLOR_BLUE); + init_pair(pair_2, COLOR_WHITE, COLOR_CYAN); + bkgd((chtype) COLOR_PAIR(pair_1)); + } else { + try_color = FALSE; + } + } + + if (optind + 1 == argc) { for (n = 1; n < argc; n++) read_data(argv[n]); if (count_cards() == 0)