added missing case in for ENC_ISO_8859_15 in src/util.c:read_font_info
[enscript.git] / src / util.c
index 99831bc..cb8c082 100644 (file)
@@ -6,25 +6,24 @@
  */
 
 /*
- * This file is part of GNU enscript.
+ * This file is part of GNU Enscript.
  *
- * This program is free software; you can redistribute it and/or modify
+ * Enscript is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
  *
- * This program is distributed in the hope that it will be useful,
+ * Enscript is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; see the file COPYING.  If not, write to
- * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
- * Boston, MA 02110-1301, USA.
+ * along with Enscript.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "gsint.h"
+#include<langinfo.h>
 
 /*
  * Types and definitions.
@@ -32,7 +31,7 @@
 
 #define CFG_FATAL(body)                                                \
   do {                                                         \
-    fprintf (stderr, "%s:%s:%d: ", program, fname, line);      \
+    fprintf (stderr, "%s:%s:%d: ", program, buffer_ptr(&fname), line); \
     fprintf body;                                              \
     fprintf (stderr, "\n");                                    \
     fflush (stderr);                                           \
@@ -94,6 +93,19 @@ static struct
   if (token2 == NULL)                                                  \
     CFG_FATAL ((stderr, _("missing argument: %s"), token));
 
+void search_and_replace(char *str, char *search, char *replace) {
+    char *pos;
+    int search_len = strlen(search);
+    int replace_len = strlen(replace);
+
+    while ((pos = strstr(str, search)) != NULL) {
+        char tmp[strlen(str) + 1];
+        strcpy(tmp, pos + search_len);
+        strcpy(pos, replace);
+        strcpy(pos + replace_len, tmp);
+        str = pos + replace_len;
+    }
+}
 int
 read_config (char *path, char *file)
 {
@@ -110,10 +122,13 @@ read_config (char *path, char *file)
 
   fp = fopen (buffer_ptr (&fname), "r");
 
-  buffer_uninit (&fname);
+  /* We wait to uninit the buffer so that CFG_FATAL can use it. */
 
   if (fp == NULL)
-    return 0;
+    {
+      buffer_uninit (&fname);
+      return 0;
+    }
 
   while (fgets (buf, sizeof (buf), fp))
     {
@@ -137,6 +152,7 @@ read_config (char *path, char *file)
        {
          token2 = GET_TOKEN (NULL);
          CHECK_TOKEN ();
+         search_and_replace(token2,"$HOME",getenv("HOME"));
          xfree (afm_path);
          afm_path = xstrdup (token2);
        }
@@ -170,6 +186,16 @@ read_config (char *path, char *file)
        {
          token2 = GET_TOKEN (NULL);
          CHECK_TOKEN ();
+#ifdef LC_PAPER
+         if (!strcasecmp("LC_PAPER", token2))
+           {
+             unsigned int paperheight = (unsigned int)nl_langinfo(_NL_PAPER_HEIGHT);
+             if (paperheight && paperheight == 279)
+               token2 = "letter";
+             else
+               token2 = "a4";
+           }
+#endif
          xfree (media_name);
          media_name = xstrdup (token2);
        }
@@ -438,6 +464,9 @@ read_config (char *path, char *file)
       else
        CFG_FATAL ((stderr, _("illegal option: %s"), token));
     }
+
+  fclose (fp);
+  buffer_uninit (&fname);
   return 1;
 }
 
@@ -573,6 +602,7 @@ paste_file (char *name, char *suffix)
                                  (void **) &cp2))
                  {
                    /* Not it is not,  we must include this resource. */
+#include<langinfo.h>
                    fprintf (ofp, "%%%%IncludeResource: font %s\n", cp);
 
                    /*
@@ -625,7 +655,7 @@ paste_file (char *name, char *suffix)
 
          if (j >= sizeof (name) - 1)
            FATAL ((stderr, _("%s:%d: %%Format: too long name, maxlen=%d"),
-                   buffer_ptr (ctx.fullname), line, sizeof (name) - 1));
+                   buffer_ptr (ctx.fullname), line, (int)(sizeof (name) - 1)));
 
          /* Find the start of the format string. */
          for (; buf[i] && isspace (buf[i]); i++)
@@ -948,6 +978,11 @@ read_font_info (void)
                                        enc_flags);
              break;
 
+           case ENC_ISO_8859_15:
+             (void) afm_font_encoding (font, AFM_ENCODING_ISO_8859_15,
+                                       enc_flags);
+             break;
+
            case ENC_ASCII:
              (void) afm_font_encoding (font, AFM_ENCODING_ASCII, enc_flags);
              break;
@@ -1239,6 +1274,8 @@ escape_string (char *string)
 
   /* Create result. */
   cp = xmalloc (len + 1);
+  if (cp == NULL)
+      return NULL;
   for (i = 0, j = 0; string[i]; i++)
     switch (string[i])
       {
@@ -1354,7 +1391,8 @@ format_user_string (char *context_name, char *str)
                  break;
 
                case 'c':       /* `%c' trailing component of pwd. */
-                 getcwd (buf, sizeof (buf));
+                 if (!getcwd (buf, sizeof (buf)))
+                   perror("getcwd");
                  cp = strrchr (buf, '/');
                  if (cp)
                    cp++;
@@ -1370,7 +1408,8 @@ format_user_string (char *context_name, char *str)
                  break;
 
                case 'd':       /* `%d' current working directory */
-                 getcwd (buf, sizeof (buf));
+                 if (!getcwd (buf, sizeof (buf)))
+                   perror("getcwd");
                  APPEND_STR (buf);
                  break;
 
@@ -1838,7 +1877,7 @@ parse_float (char *string, int units, int horizontal)
 
        case 'l':
          if (horizontal)
-           val *= CHAR_WIDTH ('m');
+           val *= FNT_CHAR_WIDTH ('m');
          else
            val *= LINESKIP;
          break;
@@ -1879,6 +1918,7 @@ is_open (InputStream *is, FILE *fp, char *fname, char *input_filter)
       char *cmd = NULL;
       int cmdlen;
       int i, pos;
+      char *cp;
 
       is->is_pipe = 1;
 
@@ -1902,12 +1942,16 @@ is_open (InputStream *is, FILE *fp, char *fname, char *input_filter)
                {
                case 's':
                  /* Expand cmd-buffer. */
-                 cmdlen += strlen (fname);
-                 cmd = xrealloc (cmd, cmdlen);
+                 if ((cp = shell_escape (fname)) != NULL)
+                   {
+                     cmdlen += strlen (cp);
+                     cmd = xrealloc (cmd, cmdlen);
 
-                 /* Paste filename. */
-                 strcpy (cmd + pos, fname);
-                 pos += strlen (fname);
+                     /* Paste filename. */
+                     strcpy (cmd + pos, cp);
+                     pos += strlen (cp);
+                     free (cp);
+                   }
 
                  i++;
                  break;
@@ -2116,3 +2160,36 @@ buffer_len (Buffer *buffer)
 {
   return buffer->len;
 }
+
+/*
+ * Escapes the name of a file so that the shell groks it in 'single'
+ * quotation marks.  The resulting pointer has to be free()ed when not
+ * longer used.
+*/
+char *
+shell_escape(const char *fn)
+{
+  size_t len = 0;
+  const char *inp;
+  char *retval, *outp;
+
+  for(inp = fn; *inp; ++inp)
+    switch(*inp)
+    {
+      case '\'': len += 4; break;
+      default:   len += 1; break;
+    }
+
+  outp = retval = malloc(len + 1);
+  if(!outp)
+    return NULL; /* perhaps one should do better error handling here */
+  for(inp = fn; *inp; ++inp)
+    switch(*inp)
+    {
+      case '\'': *outp++ = '\''; *outp++ = '\\'; *outp++ = '\'', *outp++ = '\''; break;
+      default:   *outp++ = *inp; break;
+    }
+  *outp = 0;
+
+  return retval;
+}