1 /****************************************************************************
2 * Copyright (c) 1999-2008,2010 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
30 * Author: Thomas E. Dickey
32 * $Id: cardfile.c,v 1.38 2010/11/14 00:58:45 tom Exp $
34 * File format: text beginning in column 1 is a title; other text is content.
37 #include <test.priv.h>
39 #if USE_LIBFORM && USE_LIBPANEL
44 #define VISIBLE_CARDS 10
49 #define isVisible(cardp) ((cardp)->panel != 0)
52 MY_CTRL_x
= MAX_FORM_COMMAND
59 typedef struct _card
{
67 static CARD
*all_cards
;
68 static bool try_color
= FALSE
;
69 static char default_name
[] = "cardfile.dat";
72 #define strdup my_strdup
76 char *p
= typeMalloc(char, strlen(s
) + 1);
81 #endif /* not HAVE_STRDUP */
84 skip(const char *buffer
)
86 while (isspace(UChar(*buffer
)))
94 size_t n
= strlen(buffer
);
95 while (n
-- && isspace(UChar(buffer
[n
])))
99 /*******************************************************************************/
102 add_title(const char *title
)
106 for (p
= all_cards
, q
= 0; p
!= 0; q
= p
, p
= p
->link
) {
107 int cmp
= strcmp(p
->title
, title
);
114 card
= typeCalloc(CARD
, 1);
115 card
->title
= strdup(title
);
116 card
->content
= strdup("");
119 card
->link
= all_cards
;
122 card
->link
= q
->link
;
130 add_content(CARD
* card
, const char *content
)
132 size_t total
, offset
;
134 content
= skip(content
);
135 if ((total
= strlen(content
)) != 0) {
136 if (card
->content
!= 0 && (offset
= strlen(card
->content
)) != 0) {
138 card
->content
= typeRealloc(char, total
+ 1, card
->content
);
140 strcpy(card
->content
+ offset
++, " ");
143 if (card
->content
!= 0)
145 card
->content
= typeMalloc(char, total
+ 1);
148 strcpy(card
->content
+ offset
, content
);
155 CARD
*card
= add_title("");
156 add_content(card
, "");
161 find_card(char *title
)
165 for (card
= all_cards
; card
!= 0; card
= card
->link
)
166 if (!strcmp(card
->title
, title
))
173 read_data(char *fname
)
179 if ((fp
= fopen(fname
, "r")) != 0) {
180 while (fgets(buffer
, sizeof(buffer
), fp
)) {
182 if (isspace(UChar(*buffer
))) {
184 card
= add_title("");
185 add_content(card
, buffer
);
186 } else if ((card
= find_card(buffer
)) == 0) {
187 card
= add_title(buffer
);
194 /*******************************************************************************/
197 write_data(const char *fname
)
203 if (!strcmp(fname
, default_name
))
204 fname
= "cardfile.out";
206 if ((fp
= fopen(fname
, "w")) != 0) {
207 for (p
= all_cards
; p
!= 0; p
= p
->link
) {
208 FIELD
**f
= form_fields(p
->form
);
209 for (n
= 0; f
[n
] != 0; n
++) {
210 char *s
= field_buffer(f
[n
], 0);
212 && (s
= strdup(s
)) != 0) {
214 fprintf(fp
, "%s%s\n", n
? "\t" : "", s
);
223 /*******************************************************************************/
234 for (p
= all_cards
; p
!= 0; p
= p
->link
)
241 * Shuffle the panels to keep them in a natural hierarchy.
244 order_cards(CARD
* first
, int depth
)
247 if (depth
&& first
->link
)
248 order_cards(first
->link
, depth
- 1);
249 if (isVisible(first
))
250 top_panel(first
->panel
);
255 * Return the next card in the list
258 next_card(CARD
* now
)
260 if (now
->link
!= 0) {
261 CARD
*tst
= now
->link
;
265 (void) next_card(tst
);
271 * Return the previous card in the list
274 prev_card(CARD
* now
)
277 for (p
= all_cards
; p
!= 0; p
= p
->link
) {
278 if (p
->link
== now
) {
288 * Returns the first card in the list that we will display.
291 first_card(CARD
* now
)
294 now
= next_card(now
);
298 /*******************************************************************************/
301 form_virtualize(WINDOW
*w
)
317 return (REQ_DEL_PREV
);
319 return (REQ_DEL_CHAR
);
321 return (REQ_LEFT_CHAR
);
323 return (REQ_RIGHT_CHAR
);
327 return (REQ_NEXT_FIELD
);
330 return (REQ_PREV_FIELD
);
338 make_fields(CARD
* p
, int form_high
, int form_wide
)
340 FIELD
**f
= typeCalloc(FIELD
*, 3);
342 f
[0] = new_field(1, form_wide
, 0, 0, 0, 0);
343 set_field_back(f
[0], A_REVERSE
);
344 set_field_buffer(f
[0], 0, p
->title
);
345 field_opts_off(f
[0], O_BLANK
);
347 f
[1] = new_field(form_high
- 1, form_wide
, 1, 0, 0, 0);
348 set_field_buffer(f
[1], 0, p
->content
);
349 set_field_just(f
[1], JUSTIFY_LEFT
);
350 field_opts_off(f
[1], O_BLANK
);
361 addstr("^Q/ESC -- exit form ^W -- writes data to file\n");
362 addstr("^N -- go to next card ^P -- go to previous card\n");
363 addstr("Arrow keys move left/right within a field, up/down between fields");
366 #if (defined(KEY_RESIZE) && HAVE_WRESIZE) || NO_LEAKS
368 free_form_fields(FIELD
** f
)
372 for (n
= 0; f
[n
] != 0; ++n
) {
379 /*******************************************************************************/
382 cardfile(char *fname
)
395 int finished
= FALSE
;
399 /* decide how many cards we can display */
400 visible_cards
= count_cards();
402 (panel_wide
= COLS
- (visible_cards
* OFFSET_CARD
)) < 10 ||
403 (panel_high
= LINES
- (visible_cards
* OFFSET_CARD
) - 5) < 5) {
406 form_wide
= panel_wide
- 2;
407 form_high
= panel_high
- 2;
408 y
= (visible_cards
- 1) * OFFSET_CARD
;
411 /* make a panel for each CARD */
412 for (p
= all_cards
; p
!= 0; p
= p
->link
) {
414 if ((win
= newwin(panel_high
, panel_wide
, y
, x
)) == 0)
417 wbkgd(win
, COLOR_PAIR(pair_2
));
419 p
->panel
= new_panel(win
);
422 p
->form
= new_form(make_fields(p
, form_high
, form_wide
));
423 set_form_win(p
->form
, win
);
424 set_form_sub(p
->form
, derwin(win
, form_high
, form_wide
, 1, 1));
431 top_card
= first_card(all_cards
);
432 order_cards(top_card
, visible_cards
);
438 ch
= form_virtualize(panel_window(top_card
->panel
));
439 switch (form_driver(top_card
->form
, ch
)) {
442 case E_UNKNOWN_COMMAND
:
448 top_card
= prev_card(top_card
);
449 order_cards(top_card
, visible_cards
);
452 top_card
= next_card(top_card
);
453 order_cards(top_card
, visible_cards
);
456 form_driver(top_card
->form
, REQ_VALIDATION
);
459 #if defined(KEY_RESIZE) && HAVE_WRESIZE
461 /* resizeterm already did "something" reasonable, but it cannot
462 * know much about layout. So let's make it nicer.
464 panel_wide
= COLS
- (visible_cards
* OFFSET_CARD
);
465 panel_high
= LINES
- (visible_cards
* OFFSET_CARD
) - 5;
467 form_wide
= panel_wide
- 2;
468 form_high
= panel_high
- 2;
470 y
= (visible_cards
- 1) * OFFSET_CARD
;
474 for (p
= all_cards
; p
!= 0; p
= p
->link
) {
475 FIELD
**oldf
= form_fields(p
->form
);
476 WINDOW
*olds
= form_sub(p
->form
);
480 win
= form_win(p
->form
);
482 /* move and resize the card as needed
483 * FIXME: if the windows are shrunk too much, this won't do
486 wresize(win
, panel_high
, panel_wide
);
488 /* reconstruct each form. Forms are not resizable, and
489 * there appears to be no good way to reload the text in
494 unpost_form(p
->form
);
497 p
->form
= new_form(make_fields(p
, form_high
, form_wide
));
498 set_form_win(p
->form
, win
);
499 set_form_sub(p
->form
, derwin(win
, form_high
, form_wide
,
503 free_form_fields(oldf
);
524 while (all_cards
!= 0) {
528 all_cards
= all_cards
->link
;
531 f
= form_fields(p
->form
);
533 unpost_form(p
->form
); /* ...so we can free it */
534 free_form(p
->form
); /* this also disconnects the fields */
550 static const char *msg
[] =
552 "Usage: view [options] file"
555 ," -c use color if terminal supports it"
558 for (n
= 0; n
< SIZEOF(msg
); n
++)
559 fprintf(stderr
, "%s\n", msg
[n
]);
560 ExitProgram(EXIT_FAILURE
);
563 /*******************************************************************************/
566 main(int argc
, char *argv
[])
570 setlocale(LC_ALL
, "");
572 while ((n
= getopt(argc
, argv
, "c")) != -1) {
589 init_pair(pair_1
, COLOR_WHITE
, COLOR_BLUE
);
590 init_pair(pair_2
, COLOR_WHITE
, COLOR_CYAN
);
591 bkgd(COLOR_PAIR(pair_1
));
597 if (optind
+ 1 == argc
) {
598 for (n
= 1; n
< argc
; n
++)
600 if (count_cards() == 0)
604 read_data(default_name
);
605 if (count_cards() == 0)
607 cardfile(default_name
);
612 ExitProgram(EXIT_SUCCESS
);
618 printf("This program requires the curses form and panel libraries\n");
619 ExitProgram(EXIT_FAILURE
);