]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ncurses/test/cardfile.c
Add more compatibility junk.
[FreeBSD/FreeBSD.git] / contrib / ncurses / test / cardfile.c
1 /****************************************************************************
2  * Copyright (c) 1999-2001,2002 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /*
30  * Author: Thomas E. Dickey <dickey@clark.net> 1999
31  *
32  * $Id: cardfile.c,v 1.11 2002/04/06 23:12:50 tom Exp $
33  *
34  * File format: text beginning in column 1 is a title; other text forms the content.
35  */
36
37 #include <test.priv.h>
38
39 #if HAVE_FORM_H && HAVE_PANEL_H && HAVE_LIBFORM && HAVE_LIBPANEL
40
41 #include <form.h>
42 #include <panel.h>
43
44 #include <ctype.h>
45
46 #define VISIBLE_CARDS 10
47 #define OFFSET_CARD 2
48
49 #ifndef CTRL
50 #define CTRL(x)         ((x) & 0x1f)
51 #endif
52
53 typedef struct _card {
54     struct _card *link;
55     PANEL *panel;
56     FORM *form;
57     char *title;
58     char *content;
59 } CARD;
60
61 static CARD *all_cards;
62 static char default_name[] = "cardfile.dat";
63
64 #if !HAVE_STRDUP
65 #define strdup my_strdup
66 static char *
67 strdup(char *s)
68 {
69     char *p = (char *) malloc(strlen(s) + 1);
70     if (p)
71         strcpy(p, s);
72     return (p);
73 }
74 #endif /* not HAVE_STRDUP */
75
76 static const char *
77 skip(const char *buffer)
78 {
79     while (isspace(UChar(*buffer)))
80         buffer++;
81     return buffer;
82 }
83
84 static void
85 trim(char *buffer)
86 {
87     unsigned n = strlen(buffer);
88     while (n-- && isspace(UChar(buffer[n])))
89         buffer[n] = 0;
90 }
91
92 /*******************************************************************************/
93
94 static CARD *
95 add_title(const char *title)
96 {
97     CARD *card, *p, *q;
98
99     for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
100         int cmp = strcmp(p->title, title);
101         if (cmp == 0)
102             return p;
103         if (cmp > 0)
104             break;
105     }
106
107     card = (CARD *) calloc(1, sizeof(CARD));
108     card->title = strdup(title);
109     card->content = strdup("");
110
111     if (q == 0) {
112         card->link = all_cards;
113         all_cards = card;
114     } else {
115         card->link = q->link;
116         q->link = card;
117     }
118
119     return card;
120 }
121
122 static void
123 add_content(CARD * card, const char *content)
124 {
125     unsigned total, offset;
126
127     content = skip(content);
128     if ((total = strlen(content)) != 0) {
129         if ((offset = strlen(card->content)) != 0) {
130             total += 1 + offset;
131             card->content = (char *) realloc(card->content, total + 1);
132             strcpy(card->content + offset++, " ");
133         } else {
134             card->content = (char *) malloc(total + 1);
135         }
136         strcpy(card->content + offset, content);
137     }
138 }
139
140 static CARD *
141 new_card(void)
142 {
143     CARD *card = add_title("");
144     add_content(card, "");
145     return card;
146 }
147
148 static CARD *
149 find_card(char *title)
150 {
151     CARD *card;
152
153     for (card = all_cards; card != 0; card = card->link)
154         if (!strcmp(card->title, title))
155             break;
156
157     return card;
158 }
159
160 static void
161 read_data(char *fname)
162 {
163     FILE *fp;
164     CARD *card = 0;
165     char buffer[BUFSIZ];
166
167     if ((fp = fopen(fname, "r")) != 0) {
168         while (fgets(buffer, sizeof(buffer), fp)) {
169             trim(buffer);
170             if (isspace(UChar(*buffer))) {
171                 if (card == 0)
172                     card = add_title("");
173                 add_content(card, buffer);
174             } else if ((card = find_card(buffer)) == 0) {
175                 card = add_title(buffer);
176             }
177         }
178         fclose(fp);
179     }
180 }
181
182 /*******************************************************************************/
183
184 static void
185 write_data(const char *fname)
186 {
187     FILE *fp;
188     CARD *p = 0;
189     int n;
190
191     if (!strcmp(fname, default_name))
192         fname = "cardfile.out";
193
194     if ((fp = fopen(fname, "w")) != 0) {
195         for (p = all_cards; p != 0; p = p->link) {
196             FIELD **f = form_fields(p->form);
197             for (n = 0; f[n] != 0; n++) {
198                 char *s = field_buffer(f[n], 0);
199                 if (s != 0
200                     && (s = strdup(s)) != 0) {
201                     trim(s);
202                     fprintf(fp, "%s%s\n", n ? "\t" : "", s);
203                     free(s);
204                 }
205             }
206         }
207         fclose(fp);
208     }
209 }
210
211 /*******************************************************************************/
212
213 /*
214  * Count the cards
215  */
216 static int
217 count_cards(void)
218 {
219     CARD *p;
220     int count = 0;
221
222     for (p = all_cards; p != 0; p = p->link)
223         count++;
224
225     return count;
226 }
227
228 /*
229  * Shuffle the panels to keep them in a natural hierarchy.
230  */
231 static void
232 order_cards(CARD * first, int depth)
233 {
234     if (first) {
235         if (depth && first->link)
236             order_cards(first->link, depth - 1);
237         top_panel(first->panel);
238     }
239 }
240
241 /*
242  * Return the next card in the list
243  */
244 static CARD *
245 next_card(CARD * now)
246 {
247     if (now->link)
248         now = now->link;
249     return now;
250 }
251
252 /*
253  * Return the previous card in the list
254  */
255 static CARD *
256 prev_card(CARD * now)
257 {
258     CARD *p;
259     for (p = all_cards; p != 0; p = p->link)
260         if (p->link == now)
261             return p;
262     return now;
263 }
264
265 /*******************************************************************************/
266
267 static int
268 form_virtualize(WINDOW *w)
269 {
270     int c = wgetch(w);
271
272     switch (c) {
273     case CTRL('W'):
274         return (MAX_FORM_COMMAND + 4);
275     case CTRL('N'):
276         return (MAX_FORM_COMMAND + 3);
277     case CTRL('P'):
278         return (MAX_FORM_COMMAND + 2);
279     case CTRL('Q'):
280     case 033:
281         return (MAX_FORM_COMMAND + 1);
282
283     case KEY_BACKSPACE:
284         return (REQ_DEL_PREV);
285     case KEY_DC:
286         return (REQ_DEL_CHAR);
287     case KEY_LEFT:
288         return (REQ_LEFT_CHAR);
289     case KEY_RIGHT:
290         return (REQ_RIGHT_CHAR);
291
292     case KEY_DOWN:
293     case KEY_NEXT:
294         return (REQ_NEXT_FIELD);
295     case KEY_UP:
296     case KEY_PREVIOUS:
297         return (REQ_PREV_FIELD);
298
299     default:
300         return (c);
301     }
302 }
303
304 /*******************************************************************************/
305
306 static void
307 cardfile(char *fname)
308 {
309     WINDOW *win;
310     CARD *p;
311     CARD *top_card;
312     int visible_cards = count_cards();
313     int panel_wide = COLS - (visible_cards * OFFSET_CARD);
314     int panel_high = LINES - (visible_cards * OFFSET_CARD) - 5;
315     int form_wide = panel_wide - 2;
316     int form_high = panel_high - 2;
317     int x = (visible_cards - 1) * OFFSET_CARD;
318     int y = 0;
319     int ch;
320     int finished = FALSE;
321
322     move(LINES - 3, 0);
323     addstr("^Q/ESC -- exit form            ^W   -- writes data to file\n");
324     addstr("^N   -- go to next card        ^P   -- go to previous card\n");
325     addstr("Arrow keys move left/right within a field, up/down between fields");
326
327     /* make a panel for each CARD */
328     for (p = all_cards; p != 0; p = p->link) {
329         FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
330
331         win = newwin(panel_high, panel_wide, x, y);
332         keypad(win, TRUE);
333         p->panel = new_panel(win);
334         box(win, 0, 0);
335
336         /* ...and a form in each panel */
337         f[0] = new_field(1, form_wide, 0, 0, 0, 0);
338         set_field_back(f[0], A_REVERSE);
339         set_field_buffer(f[0], 0, p->title);
340
341         f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
342         set_field_buffer(f[1], 0, p->content);
343         set_field_just(f[1], JUSTIFY_LEFT);
344
345         f[2] = 0;
346
347         p->form = new_form(f);
348         set_form_win(p->form, win);
349         set_form_sub(p->form, derwin(win, form_high, form_wide, 1, 1));
350         post_form(p->form);
351
352         x -= OFFSET_CARD;
353         y += OFFSET_CARD;
354     }
355
356     order_cards(top_card = all_cards, visible_cards);
357
358     while (!finished) {
359         update_panels();
360         doupdate();
361
362         switch (form_driver(top_card->form, ch =
363                             form_virtualize(panel_window(top_card->panel)))) {
364         case E_OK:
365             break;
366         case E_UNKNOWN_COMMAND:
367             switch (ch) {
368             case MAX_FORM_COMMAND + 1:
369                 finished = TRUE;
370                 break;
371             case MAX_FORM_COMMAND + 2:
372                 top_card = prev_card(top_card);
373                 order_cards(top_card, visible_cards);
374                 break;
375             case MAX_FORM_COMMAND + 3:
376                 top_card = next_card(top_card);
377                 order_cards(top_card, visible_cards);
378                 break;
379             case MAX_FORM_COMMAND + 4:
380                 write_data(fname);
381                 break;
382 #ifdef KEY_RESIZE
383             case KEY_RESIZE:
384                 flash();
385                 break;
386 #endif
387             default:
388                 beep();
389                 break;
390             }
391             break;
392         default:
393             flash();
394             break;
395         }
396     }
397 }
398
399 /*******************************************************************************/
400
401 int
402 main(int argc, char *argv[])
403 {
404     int n;
405
406     initscr();
407     cbreak();
408     noecho();
409
410     if (argc > 1) {
411         for (n = 1; n < argc; n++)
412             read_data(argv[n]);
413         if (count_cards() == 0)
414             new_card();
415         cardfile(argv[1]);
416     } else {
417         read_data(default_name);
418         if (count_cards() == 0)
419             new_card();
420         cardfile(default_name);
421     }
422
423     endwin();
424
425     ExitProgram(EXIT_SUCCESS);
426 }
427 #else
428 int
429 main(void)
430 {
431     printf("This program requires the curses form and panel libraries\n");
432     ExitProgram(EXIT_FAILURE);
433 }
434 #endif