1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1999-2016,2017 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
31 * Author: Thomas E. Dickey
33 * $Id: cardfile.c,v 1.47 2020/02/02 23:34:34 tom Exp $
35 * File format: text beginning in column 1 is a title; other text is content.
38 #include <test.priv.h>
40 #if USE_LIBFORM && USE_LIBPANEL
45 #define VISIBLE_CARDS 10
50 #define isVisible(cardp) ((cardp)->panel != 0)
53 MY_CTRL_x = MAX_FORM_COMMAND
60 typedef struct _card {
68 static CARD *all_cards;
69 static bool try_color = FALSE;
70 static char default_name[] = "cardfile.dat";
77 ExitProgram(EXIT_FAILURE);
81 skip(const char *buffer)
83 while (isspace(UChar(*buffer)))
91 size_t n = strlen(buffer);
92 while (n-- && isspace(UChar(buffer[n])))
96 /*******************************************************************************/
99 add_title(const char *title)
103 for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
104 int cmp = strcmp(p->title, title);
111 card = typeCalloc(CARD, (size_t) 1);
112 card->title = strdup(title);
113 card->content = strdup("");
116 card->link = all_cards;
119 card->link = q->link;
127 add_content(CARD * card, const char *content)
131 content = skip(content);
132 if ((total = strlen(content)) != 0) {
135 if (card->content != 0 && (offset = strlen(card->content)) != 0) {
137 card->content = typeRealloc(char, total + 1, card->content);
139 _nc_STRCPY(card->content + offset, " ", total + 1 - offset);
144 if (card->content != 0)
146 card->content = typeMalloc(char, total + 1);
149 _nc_STRCPY(card->content + offset, content, total + 1 - offset);
151 failed("add_content");
158 CARD *card = add_title("");
159 add_content(card, "");
164 find_card(char *title)
168 for (card = all_cards; card != 0; card = card->link)
169 if (!strcmp(card->title, title))
176 read_data(char *fname)
180 if ((fp = fopen(fname, "r")) != 0) {
184 while (fgets(buffer, sizeof(buffer), fp)) {
186 if (isspace(UChar(*buffer))) {
188 card = add_title("");
189 add_content(card, buffer);
190 } else if ((card = find_card(buffer)) == 0) {
191 card = add_title(buffer);
198 /*******************************************************************************/
201 write_data(const char *fname)
205 if (!strcmp(fname, default_name))
206 fname = "cardfile.out";
208 if ((fp = fopen(fname, "w")) != 0) {
211 for (p = all_cards; p != 0; p = p->link) {
212 FIELD **f = form_fields(p->form);
215 for (n = 0; f[n] != 0; n++) {
216 char *s = field_buffer(f[n], 0);
218 && (s = strdup(s)) != 0) {
220 fprintf(fp, "%s%s\n", n ? "\t" : "", s);
229 /*******************************************************************************/
240 for (p = all_cards; p != 0; p = p->link)
247 * Shuffle the panels to keep them in a natural hierarchy.
250 order_cards(CARD * first, int depth)
253 if (depth && first->link)
254 order_cards(first->link, depth - 1);
255 if (isVisible(first))
256 top_panel(first->panel);
261 * Return the next card in the list
264 next_card(CARD * now)
266 if (now->link != 0) {
267 CARD *tst = now->link;
271 (void) next_card(tst);
277 * Return the previous card in the list
280 prev_card(CARD * now)
283 for (p = all_cards; p != 0; p = p->link) {
284 if (p->link == now) {
294 * Returns the first card in the list that we will display.
297 first_card(CARD * now)
300 now = next_card(now);
304 /*******************************************************************************/
307 form_virtualize(WINDOW *w)
323 return (REQ_DEL_PREV);
325 return (REQ_DEL_CHAR);
327 return (REQ_LEFT_CHAR);
329 return (REQ_RIGHT_CHAR);
333 return (REQ_NEXT_FIELD);
336 return (REQ_PREV_FIELD);
344 make_fields(CARD * p, int form_high, int form_wide)
346 FIELD **f = typeCalloc(FIELD *, (size_t) 3);
348 f[0] = new_field(1, form_wide, 0, 0, 0, 0);
349 set_field_back(f[0], A_REVERSE);
350 set_field_buffer(f[0], 0, p->title);
351 field_opts_off(f[0], O_BLANK);
353 f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
354 set_field_buffer(f[1], 0, p->content);
355 set_field_just(f[1], JUSTIFY_LEFT);
356 field_opts_off(f[1], O_BLANK);
367 addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
368 addstr("^N -- go to next card ^P -- go to previous card\n");
369 addstr("Arrow keys move left/right within a field, up/down between fields");
372 #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
374 free_form_fields(FIELD **f)
378 for (n = 0; f[n] != 0; ++n) {
385 /*******************************************************************************/
388 cardfile(char *fname)
400 int finished = FALSE;
404 /* decide how many cards we can display */
405 visible_cards = count_cards();
407 (panel_wide = COLS - (visible_cards * OFFSET_CARD)) < 10 ||
408 (panel_high = LINES - (visible_cards * OFFSET_CARD) - 5) < 5) {
411 form_wide = panel_wide - 2;
412 form_high = panel_high - 2;
413 y = (visible_cards - 1) * OFFSET_CARD;
416 /* make a panel for each CARD */
417 for (p = all_cards; p != 0; p = p->link) {
419 if ((win = newwin(panel_high, panel_wide, y, x)) == 0)
422 wbkgd(win, (chtype) COLOR_PAIR(pair_2));
424 p->panel = new_panel(win);
427 p->form = new_form(make_fields(p, form_high, form_wide));
428 set_form_win(p->form, win);
429 set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
436 top_card = first_card(all_cards);
437 order_cards(top_card, visible_cards);
445 ch = form_virtualize(panel_window(top_card->panel));
446 switch (form_driver(top_card->form, ch)) {
449 case E_UNKNOWN_COMMAND:
455 top_card = prev_card(top_card);
456 order_cards(top_card, visible_cards);
459 top_card = next_card(top_card);
460 order_cards(top_card, visible_cards);
463 form_driver(top_card->form, REQ_VALIDATION);
466 #if defined(KEY_RESIZE) && HAVE_WRESIZE
468 /* resizeterm already did "something" reasonable, but it cannot
469 * know much about layout. So let's make it nicer.
471 panel_wide = COLS - (visible_cards * OFFSET_CARD);
472 panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
474 form_wide = panel_wide - 2;
475 form_high = panel_high - 2;
477 y = (visible_cards - 1) * OFFSET_CARD;
481 for (p = all_cards; p != 0; p = p->link) {
482 FIELD **oldf = form_fields(p->form);
483 WINDOW *olds = form_sub(p->form);
487 win = form_win(p->form);
489 /* move and resize the card as needed
490 * FIXME: if the windows are shrunk too much, this won't do
493 wresize(win, panel_high, panel_wide);
495 /* reconstruct each form. Forms are not resizable, and
496 * there appears to be no good way to reload the text in
501 unpost_form(p->form);
504 p->form = new_form(make_fields(p, form_high, form_wide));
505 set_form_win(p->form, win);
506 set_form_sub(p->form, derwin(win, form_high, form_wide,
510 free_form_fields(oldf);
531 while (all_cards != 0) {
533 all_cards = all_cards->link;
536 FIELD **f = form_fields(p->form);
538 unpost_form(p->form); /* ...so we can free it */
539 free_form(p->form); /* this also disconnects the fields */
555 static const char *msg[] =
557 "Usage: cardfile [options] file"
560 ," -c use color if terminal supports it"
563 for (n = 0; n < SIZEOF(msg); n++)
564 fprintf(stderr, "%s\n", msg[n]);
565 ExitProgram(EXIT_FAILURE);
568 /*******************************************************************************/
571 main(int argc, char *argv[])
575 setlocale(LC_ALL, "");
577 while ((n = getopt(argc, argv, "c")) != -1) {
594 init_pair(pair_1, COLOR_WHITE, COLOR_BLUE);
595 init_pair(pair_2, COLOR_WHITE, COLOR_CYAN);
596 bkgd((chtype) COLOR_PAIR(pair_1));
602 if (optind + 1 == argc) {
603 for (n = 1; n < argc; n++)
605 if (count_cards() == 0)
609 read_data(default_name);
610 if (count_cards() == 0)
612 cardfile(default_name);
617 ExitProgram(EXIT_SUCCESS);
623 printf("This program requires the curses form and panel libraries\n");
624 ExitProgram(EXIT_FAILURE);