]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/cardfile.c
ncurses 6.4 - patch 20240420
[ncurses.git] / test / cardfile.c
index 0bcffbb1ec6a0cbd198b8888273082977d7deafa..092483b02c00fed29a0b7c797893dab5c9fcbef0 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1999-2010,2012 Free Software Foundation, Inc.              *
+ * Copyright 2019-2021,2022 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            *
@@ -29,7 +30,7 @@
 /*
  * Author: Thomas E. Dickey
  *
- * $Id: cardfile.c,v 1.40 2012/10/27 19:37:56 tom Exp $
+ * $Id: cardfile.c,v 1.51 2022/12/04 00:40:11 tom Exp $
  *
  * File format: text beginning in column 1 is a title; other text is content.
  */
@@ -76,20 +77,6 @@ failed(const char *s)
     ExitProgram(EXIT_FAILURE);
 }
 
-#if !HAVE_STRDUP
-#define strdup my_strdup
-static char *
-strdup(const char *s)
-{
-    char *p = typeMalloc(char, strlen(s) + 1);
-    if (p)
-       strcpy(p, s);
-    else
-       failed("strdup");
-    return (p);
-}
-#endif /* not HAVE_STRDUP */
-
 static const char *
 skip(const char *buffer)
 {
@@ -121,7 +108,7 @@ add_title(const char *title)
            break;
     }
 
-    card = typeCalloc(CARD, 1);
+    card = typeCalloc(CARD, (size_t) 1);
     card->title = strdup(title);
     card->content = strdup("");
 
@@ -139,15 +126,19 @@ add_title(const char *title)
 static void
 add_content(CARD * card, const char *content)
 {
-    size_t total, offset;
+    size_t total;
 
     content = skip(content);
     if ((total = strlen(content)) != 0) {
+       size_t offset;
+
        if (card->content != 0 && (offset = strlen(card->content)) != 0) {
            total += 1 + offset;
            card->content = typeRealloc(char, total + 1, card->content);
-           if (card->content)
-               strcpy(card->content + offset++, " ");
+           if (card->content) {
+               _nc_STRCPY(card->content + offset, " ", total + 1 - offset);
+               offset++;
+           }
        } else {
            offset = 0;
            if (card->content != 0)
@@ -155,7 +146,7 @@ add_content(CARD * card, const char *content)
            card->content = typeMalloc(char, total + 1);
        }
        if (card->content)
-           strcpy(card->content + offset, content);
+           _nc_STRCPY(card->content + offset, content, total + 1 - offset);
        else
            failed("add_content");
     }
@@ -185,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))) {
@@ -209,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
@@ -302,7 +296,7 @@ prev_card(CARD * now)
 static CARD *
 first_card(CARD * now)
 {
-    if (!isVisible(now))
+    if (now != NULL && !isVisible(now))
        now = next_card(now);
     return now;
 }
@@ -349,7 +343,7 @@ form_virtualize(WINDOW *w)
 static FIELD **
 make_fields(CARD * p, int form_high, int form_wide)
 {
-    FIELD **f = typeCalloc(FIELD *, 3);
+    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);
@@ -377,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;
 
@@ -403,7 +397,6 @@ cardfile(char *fname)
     int form_high;
     int y;
     int x;
-    int ch = ERR;
     int finished = FALSE;
 
     show_legend();
@@ -444,6 +437,8 @@ cardfile(char *fname)
     order_cards(top_card, visible_cards);
 
     while (!finished) {
+       int ch = ERR;
+
        update_panels();
        doupdate();
 
@@ -534,13 +529,11 @@ cardfile(char *fname)
     }
 #if NO_LEAKS
     while (all_cards != 0) {
-       FIELD **f;
-
        p = all_cards;
        all_cards = all_cards->link;
 
        if (isVisible(p)) {
-           f = form_fields(p->form);
+           FIELD **f = form_fields(p->form);
 
            unpost_form(p->form);       /* ...so we can free it */
            free_form(p->form); /* this also disconnects the fields */
@@ -557,37 +550,45 @@ cardfile(char *fname)
 }
 
 static void
-usage(void)
+usage(int ok)
 {
     static const char *msg[] =
     {
-       "Usage: view [options] file"
+       "Usage: cardfile [options] file"
        ,""
+       ,USAGE_COMMON
        ,"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);
+    ExitProgram(ok ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 /*******************************************************************************/
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
 
 int
 main(int argc, char *argv[])
 {
-    int n;
+    int ch;
 
     setlocale(LC_ALL, "");
 
-    while ((n = getopt(argc, argv, "c")) != -1) {
-       switch (n) {
+    while ((ch = getopt(argc, argv, OPTS_COMMON "c")) != -1) {
+       switch (ch) {
        case 'c':
            try_color = TRUE;
            break;
+       case OPTS_VERSION:
+           show_version(argv);
+           ExitProgram(EXIT_SUCCESS);
        default:
-           usage();
+           usage(ch == OPTS_USAGE);
+           /* NOTREACHED */
        }
     }
 
@@ -607,6 +608,7 @@ main(int argc, char *argv[])
     }
 
     if (optind + 1 == argc) {
+       int n;
        for (n = 1; n < argc; n++)
            read_data(argv[n]);
        if (count_cards() == 0)