]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/tic.c
ncurses 5.9 - patch 20120204
[ncurses.git] / progs / tic.c
index 8e89095fc67637076289c15ec71b385d1adafd31..aa38840e7d9030a82f55070f5f7534b4b0be8c28 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2011,2012 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            *
@@ -35,6 +35,7 @@
 /*
  *     tic.c --- Main program for terminfo compiler
  *                     by Eric S. Raymond
+ *                     and Thomas E Dickey
  *
  */
 
 #include <sys/stat.h>
 
 #include <dump_entry.h>
+#include <hashed_db.h>
 #include <transform.h>
 
-MODULE_ID("$Id: tic.c,v 1.147 2011/02/12 18:39:08 tom Exp $")
+MODULE_ID("$Id: tic.c,v 1.160 2012/02/05 01:39:39 tom Exp $")
 
 const char *_nc_progname = "tic";
 
@@ -69,11 +71,13 @@ static const char usage_string[] = "\
 1\
 a\
 C\
+D\
 c\
 f\
 G\
 g\
 I\
+K\
 L\
 N\
 r\
@@ -133,7 +137,9 @@ usage(void)
 #if NCURSES_XNAMES
        "  -a         retain commented-out capabilities (sets -x also)",
 #endif
+       "  -K         translate entries to termcap source form with BSD syntax",
        "  -C         translate entries to termcap source form",
+       "  -D         print list of tic's database locations (first must be writable)",
        "  -c         check only, validate input without compiling or translating",
        "  -e<names>  translate/compile only entries named by comma-separated list",
        "  -f         format complex strings for readability",
@@ -173,7 +179,7 @@ usage(void)
 
 #define L_BRACE '{'
 #define R_BRACE '}'
-#define S_QUOTE '\'';
+#define S_QUOTE '\''
 
 static void
 write_it(ENTRY * ep)
@@ -221,7 +227,7 @@ write_it(ENTRY * ep)
     }
 
     _nc_set_type(_nc_first_name(ep->tterm.term_names));
-    _nc_curr_line = ep->startline;
+    _nc_curr_line = (int) ep->startline;
     _nc_write_entry(&ep->tterm);
 }
 
@@ -339,23 +345,23 @@ put_translate(int c)
 static char *
 stripped(char *src)
 {
+    char *dst = 0;
+
     while (isspace(UChar(*src)))
        src++;
+
     if (*src != '\0') {
-       char *dst;
        size_t len;
 
-       if ((dst = strdup(src)) == NULL)
+       if ((dst = strdup(src)) == NULL) {
            failed("strdup");
-
-       assert(dst != 0);
-
-       len = strlen(dst);
-       while (--len != 0 && isspace(UChar(dst[len])))
-           dst[len] = '\0';
-       return dst;
+       } else {
+           len = strlen(dst);
+           while (--len != 0 && isspace(UChar(dst[len])))
+               dst[len] = '\0';
+       }
     }
-    return 0;
+    return dst;
 }
 
 static FILE *
@@ -471,11 +477,113 @@ open_tempfile(char *name)
     return result;
 }
 
+static const char *
+valid_db_path(const char *nominal)
+{
+    struct stat sb;
+#if USE_HASHED_DB
+    char suffix[] = DBM_SUFFIX;
+    size_t need = strlen(nominal) + sizeof(suffix);
+    char *result = malloc(need);
+
+    strcpy(result, nominal);
+    if (strcmp(result + need - sizeof(suffix), suffix)) {
+       strcat(result, suffix);
+    }
+#else
+    char *result = strdup(nominal);
+#endif
+
+    DEBUG(1, ("** stat(%s)", result));
+    if (stat(result, &sb) >= 0) {
+#if USE_HASHED_DB
+       if (!S_ISREG(sb.st_mode)
+           || access(result, R_OK | W_OK) != 0) {
+           DEBUG(1, ("...not a writable file"));
+           free(result);
+           result = 0;
+       }
+#else
+       if (!S_ISDIR(sb.st_mode)
+           || access(result, R_OK | W_OK | X_OK) != 0) {
+           DEBUG(1, ("...not a writable directory"));
+           free(result);
+           result = 0;
+       }
+#endif
+    } else {
+       /* check if parent is directory and is writable */
+       unsigned leaf = _nc_pathlast(result);
+
+       DEBUG(1, ("...not found"));
+       if (leaf) {
+           char save = result[leaf];
+           result[leaf] = 0;
+           if (stat(result, &sb) >= 0
+               && S_ISDIR(sb.st_mode)
+               && access(result, R_OK | W_OK | X_OK) == 0) {
+               result[leaf] = save;
+           } else {
+               DEBUG(1, ("...parent directory %s is not writable", result));
+               free(result);
+               result = 0;
+           }
+       } else {
+           DEBUG(1, ("... no parent directory"));
+           free(result);
+           result = 0;
+       }
+    }
+    return result;
+}
+
+/*
+ * Show the databases to which tic could write.  The location to which it
+ * writes is always the first one.  If none are writable, print an error
+ * message.
+ */
+static void
+show_databases(const char *outdir)
+{
+    bool specific = (outdir != 0) || getenv("TERMINFO") != 0;
+    const char *result;
+    const char *tried = 0;
+
+    if (outdir == 0) {
+       outdir = _nc_tic_dir(0);
+    }
+    if ((result = valid_db_path(outdir)) != 0) {
+       printf("%s\n", result);
+    } else {
+       tried = outdir;
+    }
+
+    if ((outdir = _nc_home_terminfo())) {
+       if ((result = valid_db_path(outdir)) != 0) {
+           printf("%s\n", result);
+       } else if (!specific) {
+           tried = outdir;
+       }
+    }
+
+    /*
+     * If we can write in neither location, give an error message.
+     */
+    if (tried) {
+       fflush(stdout);
+       fprintf(stderr, "%s: %s (no permission)\n", _nc_progname, tried);
+       ExitProgram(EXIT_FAILURE);
+    }
+}
+
+#define VtoTrace(opt) (unsigned) ((opt > 0) ? opt : (opt == 0))
+
 int
 main(int argc, char *argv[])
 {
     char my_tmpname[PATH_MAX];
-    int v_opt = -1, debug_level;
+    int v_opt = -1;
+    unsigned debug_level;
     int smart_defaults = TRUE;
     char *termcap;
     ENTRY *qp;
@@ -486,6 +594,7 @@ main(int argc, char *argv[])
     int sortmode = S_TERMINFO; /* sort_mode */
 
     int width = 60;
+    int height = 65535;
     bool formatted = FALSE;    /* reformat complex strings? */
     bool literal = FALSE;      /* suppress post-processing? */
     int numbers = 0;           /* format "%'char'" to/from "%{number}" */
@@ -513,6 +622,7 @@ main(int argc, char *argv[])
 #if NCURSES_XNAMES
     use_extended_names(FALSE);
 #endif
+    _nc_strict_bsd = 0;
 
     /*
      * Processing arguments is a little complicated, since someone made a
@@ -520,7 +630,7 @@ main(int argc, char *argv[])
      * be optional.
      */
     while ((this_opt = getopt(argc, argv,
-                             "0123456789CILNR:TUVace:fGgo:rstvwx")) != -1) {
+                             "0123456789CDIKLNR:TUVace:fGgo:rstvwx")) != -1) {
        if (isdigit(this_opt)) {
            switch (last_opt) {
            case 'v':
@@ -530,19 +640,40 @@ main(int argc, char *argv[])
                width = (width * 10) + (this_opt - '0');
                break;
            default:
-               if (this_opt != '1')
+               switch (this_opt) {
+               case '0':
+                   last_opt = this_opt;
+                   width = 65535;
+                   height = 1;
+                   break;
+               case '1':
+                   last_opt = this_opt;
+                   width = 0;
+                   break;
+               default:
                    usage();
-               last_opt = this_opt;
-               width = 0;
+               }
            }
            continue;
        }
        switch (this_opt) {
+       case 'K':
+           _nc_strict_bsd = 1;
+           /* the initial version of -K in 20110730 fell-thru here, but the
+            * same flag is useful when reading sources -TD
+            */
+           break;
        case 'C':
            capdump = TRUE;
            outform = F_TERMCAP;
            sortmode = S_TERMCAP;
            break;
+       case 'D':
+           debug_level = VtoTrace(v_opt);
+           set_trace_level(debug_level);
+           show_databases(outdir);
+           ExitProgram(EXIT_SUCCESS);
+           break;
        case 'I':
            infodump = TRUE;
            outform = F_TERMINFO;
@@ -618,7 +749,7 @@ main(int argc, char *argv[])
        last_opt = this_opt;
     }
 
-    debug_level = (v_opt > 0) ? v_opt : (v_opt == 0);
+    debug_level = VtoTrace(v_opt);
     set_trace_level(debug_level);
 
     if (_nc_tracing) {
@@ -638,7 +769,8 @@ main(int argc, char *argv[])
      */
     if (namelst && (!infodump && !capdump)) {
        (void) fprintf(stderr,
-                      "Sorry, -e can't be used without -I or -C\n");
+                      "%s: Sorry, -e can't be used without -I or -C\n",
+                      _nc_progname);
        cleanup(namelst);
        ExitProgram(EXIT_FAILURE);
     }
@@ -695,11 +827,11 @@ main(int argc, char *argv[])
                  smart_defaults
                  ? outform
                  : F_LITERAL,
-                 sortmode, width, debug_level, formatted);
+                 sortmode, width, height, debug_level, formatted);
     else if (capdump)
        dump_init(tversion,
                  outform,
-                 sortmode, width, debug_level, FALSE);
+                 sortmode, width, height, debug_level, FALSE);
 
     /* parse entries out of the source file */
     _nc_set_source(source_file);
@@ -750,7 +882,7 @@ main(int argc, char *argv[])
 
            for_entry_list(qp) {
                if (matches(namelst, qp->tterm.term_names)) {
-                   int j = qp->cend - qp->cstart;
+                   long j = qp->cend - qp->cstart;
                    int len = 0;
 
                    /* this is in case infotocap() generates warnings */
@@ -767,7 +899,7 @@ main(int argc, char *argv[])
                    repair_acsc(&qp->tterm);
                    dump_entry(&qp->tterm, suppress_untranslatable,
                               limited, numbers, NULL);
-                   for (j = 0; j < (int) qp->nuses; j++)
+                   for (j = 0; j < (long) qp->nuses; j++)
                        dump_uses(qp->uses[j].name, !capdump);
                    len = show_entry();
                    if (debug_level != 0 && !limited)
@@ -915,18 +1047,18 @@ keypad_final(const char *string)
     return result;
 }
 
-static int
+static long
 keypad_index(const char *string)
 {
     char *test;
     const char *list = "PQRSwxymtuvlqrsPpn";   /* app-keypad except "Enter" */
     int ch;
-    int result = -1;
+    long result = -1;
 
     if ((ch = keypad_final(string)) != '\0') {
        test = strchr(list, ch);
        if (test != 0)
-           result = (test - list);
+           result = (long) (test - list);
     }
     return result;
 }
@@ -1097,11 +1229,11 @@ check_keypad(TERMTYPE *tp)
        VALID_STRING(key_c1) &&
        VALID_STRING(key_c3)) {
        char final[MAX_KP + 1];
-       int list[MAX_KP];
+       long list[MAX_KP];
        int increase = 0;
        int j, k, kk;
-       int last;
-       int test;
+       long last;
+       long test;
 
        final[0] = keypad_final(key_a1);
        final[1] = keypad_final(key_a3);
@@ -1592,7 +1724,7 @@ check_termtype(TERMTYPE *tp, bool literal)
     for (j = 0; j < NUM_STRINGS(tp); j++) {
        char *a = tp->Strings[j];
        if (VALID_STRING(a))
-           check_params(tp, ExtStrname(tp, j, strnames), a);
+           check_params(tp, ExtStrname(tp, (int) j, strnames), a);
     }
 
     check_acs(tp);