]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/make_hash.c
ncurses 6.1 - patch 20181215
[ncurses.git] / ncurses / tinfo / make_hash.c
index ffebe70705814d0807d3b048b264e3cc975a4410..c8bf911f826089da93f4da1e5e164fa6d0f1b439 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2017,2018 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            *
 
 /*
  *     make_hash.c --- build-time program for constructing comp_captab.c
- *
  */
 
-#include <curses.priv.h>
+#include <build.priv.h>
 
 #include <tic.h>
 #include <hashsize.h>
 
 #include <ctype.h>
 
-MODULE_ID("$Id: make_hash.c,v 1.2 2009/08/08 17:42:41 tom Exp $")
+MODULE_ID("$Id: make_hash.c,v 1.17 2018/05/12 15:58:31 tom Exp $")
 
 /*
  *     _nc_make_hash_table()
@@ -59,6 +58,24 @@ MODULE_ID("$Id: make_hash.c,v 1.2 2009/08/08 17:42:41 tom Exp $")
 #define MODULE_ID(id)          /*nothing */
 #include <tinfo/doalloc.c>
 
+static void
+failed(const char *s)
+{
+    perror(s);
+    exit(EXIT_FAILURE);
+}
+
+static char *
+strmalloc(char *s)
+{
+    size_t need = strlen(s) + 1;
+    char *result = malloc(need);
+    if (result == 0)
+       failed("strmalloc");
+    _nc_STRCPY(result, s, need);
+    return result;
+}
+
 /*
  *     int hash_function(string)
  *
@@ -119,6 +136,18 @@ _nc_make_hash_table(struct name_table_entry *table,
 
 #define MAX_COLUMNS BUFSIZ     /* this _has_ to be worst-case */
 
+static int
+count_columns(char **list)
+{
+    int result = 0;
+    if (list != 0) {
+       while (*list++) {
+           ++result;
+       }
+    }
+    return result;
+}
+
 static char **
 parse_columns(char *buffer)
 {
@@ -126,10 +155,12 @@ parse_columns(char *buffer)
 
     int col = 0;
 
-    if (list == 0 && (list = typeCalloc(char *, MAX_COLUMNS)) == 0)
-         return (0);
-
     if (*buffer != '#') {
+       if (list == 0) {
+           list = typeCalloc(char *, (MAX_COLUMNS + 1));
+           if (list == 0)
+               return (0);
+       }
        while (*buffer != '\0') {
            char *s;
            for (s = buffer; (*s != '\0') && !isspace(UChar(*s)); s++)
@@ -195,14 +226,24 @@ main(int argc, char **argv)
      * Read the table into our arrays.
      */
     for (n = 0; (n < CAPTABSIZE) && fgets(buffer, BUFSIZ, stdin);) {
-       char **list, *nlp = strchr(buffer, '\n');
+       char **list;
+       char *nlp = strchr(buffer, '\n');
        if (nlp)
            *nlp = '\0';
+       else
+           buffer[sizeof(buffer) - 2] = '\0';
        list = parse_columns(buffer);
        if (list == 0)          /* blank or comment */
            continue;
+       if (column < 0 || column > count_columns(list)) {
+           fprintf(stderr, "expected %d columns, have %d:\n%s\n",
+                   column,
+                   count_columns(list),
+                   buffer);
+           exit(EXIT_FAILURE);
+       }
        name_table[n].nte_link = -1;    /* end-of-hash */
-       name_table[n].nte_name = strdup(list[column]);
+       name_table[n].nte_name = strmalloc(list[column]);
        if (!strcmp(list[2], "bool")) {
            name_table[n].nte_type = BOOLEAN;
            name_table[n].nte_index = BoolCount++;
@@ -256,13 +297,12 @@ main(int argc, char **argv)
        printf("static struct name_table_entry *_nc_%s_table = 0;\n\n", root_name);
     } else {
 
-       printf("static struct name_table_entry %s _nc_%s_table[] =\n",
-              bigstring ? "" : "const",
+       printf("static struct name_table_entry const _nc_%s_table[] =\n",
               root_name);
        printf("{\n");
        for (n = 0; n < CAPTABSIZE; n++) {
-           sprintf(buffer, "\"%s\"",
-                   name_table[n].nte_name);
+           _nc_SPRINTF(buffer, _nc_SLIMIT(sizeof(buffer)) "\"%s\"",
+                       name_table[n].nte_name);
            printf("\t{ %15s,\t%10s,\t%3d, %3d }%c\n",
                   buffer,
                   typenames[name_table[n].nte_type],
@@ -290,5 +330,11 @@ main(int argc, char **argv)
     printf("#endif\n\n");
 
     free(hash_table);
+#if NO_LEAKS
+    for (n = 0; (n < CAPTABSIZE); ++n) {
+       free((void *) name_table[n].nte_name);
+    }
+    free(name_table);
+#endif
     return EXIT_SUCCESS;
 }