aboutsummaryrefslogtreecommitdiff
path: root/contrib/ncurses/test/cardfile.c
diff options
context:
space:
mode:
authorPeter Wemm <peter@FreeBSD.org>2000-07-03 09:24:12 +0000
committerPeter Wemm <peter@FreeBSD.org>2000-07-03 09:24:12 +0000
commit15589c42fa2774d2f8ee650f4f31eb8d3a861316 (patch)
tree27e79485df3c9195e6fe58960df47f675f41f1e9 /contrib/ncurses/test/cardfile.c
parent6b9085fd58b7602b14be0cd2059285baa764a7ee (diff)
downloadsrc-15589c42fa2774d2f8ee650f4f31eb8d3a861316.tar.gz
src-15589c42fa2774d2f8ee650f4f31eb8d3a861316.zip
Import the most recent ncurses 5.1 prerelease (20000701).
Mostly this is intended to resolve the trace() badness once and for all. Obtained from: ftp://dickey.his.com/ncurses/
Notes
Notes: svn path=/vendor/ncurses/dist/; revision=62449
Diffstat (limited to 'contrib/ncurses/test/cardfile.c')
-rw-r--r--contrib/ncurses/test/cardfile.c196
1 files changed, 98 insertions, 98 deletions
diff --git a/contrib/ncurses/test/cardfile.c b/contrib/ncurses/test/cardfile.c
index bc2e1adb14cc..980ffbeba94c 100644
--- a/contrib/ncurses/test/cardfile.c
+++ b/contrib/ncurses/test/cardfile.c
@@ -1,5 +1,5 @@
/****************************************************************************
- * Copyright (c) 1999 Free Software Foundation, Inc. *
+ * Copyright (c) 1999,2000 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 *
@@ -29,7 +29,7 @@
/*
* Author: Thomas E. Dickey <dickey@clark.net> 1999
*
- * $Id: cardfile.c,v 1.2 1999/06/16 00:41:57 tom Exp $
+ * $Id: cardfile.c,v 1.4 2000/03/19 01:34:00 tom Exp $
*
* File format: text beginning in column 1 is a title; other text forms the content.
*/
@@ -50,11 +50,11 @@
#endif
typedef struct _card {
- struct _card *link;
- PANEL *panel;
- FORM *form;
- char *title;
- char *content;
+ struct _card *link;
+ PANEL *panel;
+ FORM *form;
+ char *title;
+ char *content;
} CARD;
static CARD *all_cards;
@@ -62,37 +62,40 @@ static char default_name[] = "cardfile.dat";
#if !HAVE_STRDUP
#define strdup my_strdup
-static char *strdup (char *s)
+static char *
+strdup(char *s)
{
- char *p = (char *)malloc(strlen(s)+1);
+ char *p = (char *) malloc(strlen(s) + 1);
if (p)
strcpy(p, s);
- return(p);
+ return (p);
}
#endif /* not HAVE_STRDUP */
-static char *skip(char *buffer)
+static char *
+skip(char *buffer)
{
while (isspace(*buffer))
buffer++;
return buffer;
}
-static void trim(char *buffer)
+static void
+trim(char *buffer)
{
unsigned n = strlen(buffer);
while (n-- && isspace(buffer[n]))
- buffer[n] = 0;
+ buffer[n] = 0;
}
/*******************************************************************************/
-static CARD *add_title(const char *title)
+static CARD *
+add_title(const char *title)
{
CARD *card, *p, *q;
- for (p = all_cards, q = 0; p != 0; q = p, p = p->link)
- {
+ for (p = all_cards, q = 0; p != 0; q = p, p = p->link) {
int cmp = strcmp(p->title, title);
if (cmp == 0)
return p;
@@ -100,46 +103,49 @@ static CARD *add_title(const char *title)
break;
}
- card = (CARD *)calloc(1, sizeof(CARD));
+ card = (CARD *) calloc(1, sizeof(CARD));
card->title = strdup(title);
card->content = strdup("");
- if (q == 0)
- {
+ if (q == 0) {
card->link = all_cards;
all_cards = card;
- }
- else
- {
- card->link = q->link;
+ } else {
+ card->link = q->link;
q->link = card;
}
return card;
}
-static void add_content(CARD *card, char *content)
+static void
+add_content(CARD * card, char *content)
{
unsigned total, offset;
content = skip(content);
- if ((total = strlen(content)) != 0)
- {
- if ((offset = strlen(card->content)) != 0)
- {
+ if ((total = strlen(content)) != 0) {
+ if ((offset = strlen(card->content)) != 0) {
total += 1 + offset;
- card->content = (char *)realloc(card->content, total + 1);
+ card->content = (char *) realloc(card->content, total + 1);
strcpy(card->content + offset++, " ");
- }
- else
- {
- card->content = (char *)malloc(total + 1);
+ } else {
+ card->content = (char *) malloc(total + 1);
}
strcpy(card->content + offset, content);
}
}
-static CARD *find_card(char *title)
+static CARD *
+new_card(void)
+{
+ CARD *card = add_title("");
+ add_content(card, "");
+ return card;
+}
+
+static CARD *
+find_card(char *title)
{
CARD *card;
@@ -150,25 +156,21 @@ static CARD *find_card(char *title)
return card;
}
-static void read_data(char *fname)
+static void
+read_data(char *fname)
{
FILE *fp;
CARD *card = 0;
char buffer[BUFSIZ];
- if ((fp = fopen(fname, "r")) != 0)
- {
- while (fgets(buffer, sizeof(buffer), fp))
- {
+ if ((fp = fopen(fname, "r")) != 0) {
+ while (fgets(buffer, sizeof(buffer), fp)) {
trim(buffer);
- if (isspace(*buffer))
- {
+ if (isspace(*buffer)) {
if (card == 0)
card = add_title("");
add_content(card, buffer);
- }
- else if ((card = find_card(buffer)) == 0)
- {
+ } else if ((card = find_card(buffer)) == 0) {
card = add_title(buffer);
}
}
@@ -178,7 +180,8 @@ static void read_data(char *fname)
/*******************************************************************************/
-static void write_data(const char *fname)
+static void
+write_data(const char *fname)
{
FILE *fp;
CARD *p = 0;
@@ -187,17 +190,13 @@ static void write_data(const char *fname)
if (!strcmp(fname, default_name))
fname = "cardfile.out";
- if ((fp = fopen(fname, "w")) != 0)
- {
- for (p = all_cards; p != 0; p = p->link)
- {
+ if ((fp = fopen(fname, "w")) != 0) {
+ for (p = all_cards; p != 0; p = p->link) {
FIELD **f = form_fields(p->form);
- for (n = 0; f[n] != 0; n++)
- {
+ for (n = 0; f[n] != 0; n++) {
char *s = field_buffer(f[n], 0);
if (s != 0
- && (s = strdup(s)) != 0)
- {
+ && (s = strdup(s)) != 0) {
trim(s);
fprintf(fp, "%s%s\n", n ? "\t" : "", s);
free(s);
@@ -213,7 +212,8 @@ static void write_data(const char *fname)
/*
* Count the cards
*/
-static int count_cards(void)
+static int
+count_cards(void)
{
CARD *p;
int count = 0;
@@ -227,12 +227,12 @@ static int count_cards(void)
/*
* Shuffle the panels to keep them in a natural hierarchy.
*/
-static void order_cards(CARD *first, int depth)
+static void
+order_cards(CARD * first, int depth)
{
- if (first)
- {
+ if (first) {
if (depth && first->link)
- order_cards(first->link, depth-1);
+ order_cards(first->link, depth - 1);
top_panel(first->panel);
}
}
@@ -240,17 +240,19 @@ static void order_cards(CARD *first, int depth)
/*
* Return the next card in the list
*/
-static CARD *next_card(CARD *now)
+static CARD *
+next_card(CARD * now)
{
if (now->link)
- now = now->link;
+ now = now->link;
return now;
}
/*
* Return the previous card in the list
*/
-static CARD *prev_card(CARD *now)
+static CARD *
+prev_card(CARD * now)
{
CARD *p;
for (p = all_cards; p != 0; p = p->link)
@@ -259,49 +261,49 @@ static CARD *prev_card(CARD *now)
return now;
}
-
/*******************************************************************************/
-static int form_virtualize(WINDOW *w)
+static int
+form_virtualize(WINDOW *w)
{
- int c = wgetch(w);
+ int c = wgetch(w);
- switch(c)
- {
+ switch (c) {
case CTRL('W'):
- return(MAX_FORM_COMMAND + 4);
+ return (MAX_FORM_COMMAND + 4);
case CTRL('N'):
- return(MAX_FORM_COMMAND + 3);
+ return (MAX_FORM_COMMAND + 3);
case CTRL('P'):
- return(MAX_FORM_COMMAND + 2);
+ return (MAX_FORM_COMMAND + 2);
case CTRL('Q'):
case 033:
- return(MAX_FORM_COMMAND + 1);
+ return (MAX_FORM_COMMAND + 1);
case KEY_BACKSPACE:
- return(REQ_DEL_PREV);
+ return (REQ_DEL_PREV);
case KEY_DC:
- return(REQ_DEL_CHAR);
+ return (REQ_DEL_CHAR);
case KEY_LEFT:
- return(REQ_LEFT_CHAR);
+ return (REQ_LEFT_CHAR);
case KEY_RIGHT:
- return(REQ_RIGHT_CHAR);
+ return (REQ_RIGHT_CHAR);
case KEY_DOWN:
case KEY_NEXT:
- return(REQ_NEXT_FIELD);
+ return (REQ_NEXT_FIELD);
case KEY_UP:
case KEY_PREVIOUS:
- return(REQ_PREV_FIELD);
+ return (REQ_PREV_FIELD);
default:
- return(c);
+ return (c);
}
}
/*******************************************************************************/
-static void cardfile(char *fname)
+static void
+cardfile(char *fname)
{
WINDOW *win;
CARD *p;
@@ -322,9 +324,8 @@ static void cardfile(char *fname)
addstr("Arrow keys move left/right within a field, up/down between fields");
/* make a panel for each CARD */
- for (p = all_cards; p != 0; p = p->link)
- {
- FIELD **f = (FIELD **)calloc(3, sizeof(FIELD *));
+ for (p = all_cards; p != 0; p = p->link) {
+ FIELD **f = (FIELD **) calloc(3, sizeof(FIELD *));
win = newwin(panel_high, panel_wide, x, y);
keypad(win, TRUE);
@@ -336,7 +337,7 @@ static void cardfile(char *fname)
set_field_back(f[0], A_REVERSE);
set_field_buffer(f[0], 0, p->title);
- f[1] = new_field(form_high-1, form_wide, 1, 0, 0, 0);
+ f[1] = new_field(form_high - 1, form_wide, 1, 0, 0, 0);
set_field_buffer(f[1], 0, p->content);
set_field_just(f[1], JUSTIFY_LEFT);
@@ -353,31 +354,28 @@ static void cardfile(char *fname)
order_cards(top_card = all_cards, visible_cards);
- update_panels();
-
- while (!finished)
- {
+ while (!finished) {
update_panels();
doupdate();
- switch(form_driver(top_card->form, ch = form_virtualize(panel_window(top_card->panel))))
- {
+ switch (form_driver(top_card->form, ch =
+ form_virtualize(panel_window(top_card->panel)))) {
case E_OK:
break;
case E_UNKNOWN_COMMAND:
switch (ch) {
- case MAX_FORM_COMMAND+1:
+ case MAX_FORM_COMMAND + 1:
finished = TRUE;
break;
- case MAX_FORM_COMMAND+2:
+ case MAX_FORM_COMMAND + 2:
top_card = prev_card(top_card);
order_cards(top_card, visible_cards);
break;
- case MAX_FORM_COMMAND+3:
+ case MAX_FORM_COMMAND + 3:
top_card = next_card(top_card);
order_cards(top_card, visible_cards);
break;
- case MAX_FORM_COMMAND+4:
+ case MAX_FORM_COMMAND + 4:
write_data(fname);
break;
default:
@@ -394,7 +392,8 @@ static void cardfile(char *fname)
/*******************************************************************************/
-int main(int argc, char *argv[])
+int
+main(int argc, char *argv[])
{
int n;
@@ -402,15 +401,16 @@ int main(int argc, char *argv[])
cbreak();
noecho();
- if (argc > 1)
- {
+ if (argc > 1) {
for (n = 1; n < argc; n++)
read_data(argv[n]);
+ if (count_cards() == 0)
+ new_card();
cardfile(argv[1]);
- }
- else
- {
+ } else {
read_data(default_name);
+ if (count_cards() == 0)
+ new_card();
cardfile(default_name);
}