reimplement mutt_gecos_name.
authorPierre Habouzit <madcoder@debian.org>
Sun, 12 Nov 2006 13:25:41 +0000 (14:25 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 12 Nov 2006 13:25:41 +0000 (14:25 +0100)
Signed-off-by: Pierre Habouzit <madcoder@debian.org>
alias.c
init.c
lib-sys/Makefile.am
lib-sys/mutt_tunnel.c
lib-sys/unix.c [new file with mode: 0644]
lib-sys/unix.h [new file with mode: 0644]
muttlib.c
protos.h

diff --git a/alias.c b/alias.c
index cefe0ec..fa83c6f 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -23,6 +23,8 @@
 #include <lib-lib/debug.h>
 #include <lib-lib/rx.h>
 
 #include <lib-lib/debug.h>
 #include <lib-lib/rx.h>
 
+#include <lib-sys/unix.h>
+
 #include <lib-ui/curses.h>
 #include <lib-ui/enter.h>
 #include <lib-ui/menu.h>
 #include <lib-ui/curses.h>
 #include <lib-ui/enter.h>
 #include <lib-ui/menu.h>
@@ -100,8 +102,8 @@ static address_t *mutt_expand_aliases_r (address_t * a, LIST ** expn)
         if (pw) {
           char namebuf[STRING];
 
         if (pw) {
           char namebuf[STRING];
 
-          mutt_gecos_name (namebuf, sizeof (namebuf), pw);
-          m_strreplace (&a->personal, namebuf);
+          mutt_gecos_name(namebuf, sizeof (namebuf), pw, GecosMask.rx);
+          m_strreplace(&a->personal, namebuf);
         }
       }
     }
         }
       }
     }
diff --git a/init.c b/init.c
index bb8bedb..5151f5a 100644 (file)
--- a/init.c
+++ b/init.c
@@ -2608,7 +2608,8 @@ void mutt_init (int skip_sys_rc, LIST * commands)
     if (!Homedir)
       Homedir = m_strdup(pw->pw_dir);
 
     if (!Homedir)
       Homedir = m_strdup(pw->pw_dir);
 
-    Realname = m_strdup(mutt_gecos_name (rnbuf, sizeof(rnbuf), pw));
+    mutt_gecos_name(rnbuf, sizeof(rnbuf), pw, GecosMask.rx);
+    Realname = m_strdup(rnbuf);
     Shell = m_strdup(pw->pw_shell);
     endpwent ();
   }
     Shell = m_strdup(pw->pw_shell);
     endpwent ();
   }
index 5e896e5..18d56c7 100644 (file)
@@ -1,11 +1,12 @@
 noinst_LIBRARIES = libsys.a
 
 noinst_LIBRARIES = libsys.a
 
-libsys_a_SOURCES = exit.h \
-                  exit.c \
+libsys_a_SOURCES = exit.h unix.h \
+                  exit.c unix.c \
                   $(___networking_part____) \
                   mutt_socket.h mutt_tunnel.c mutt_ssl.h \
                   mutt_socket.c mutt_tunnel.h mutt_ssl.c mutt_ssl_gnutls.c
 
                   $(___networking_part____) \
                   mutt_socket.h mutt_tunnel.c mutt_ssl.h \
                   mutt_socket.c mutt_tunnel.h mutt_ssl.c mutt_ssl_gnutls.c
 
-noinst_HEADERS   = mutt_socket.h mutt_tunnel.c mutt_ssl.h
+noinst_HEADERS   = exit.h unix.h \
+                  mutt_socket.h mutt_tunnel.c mutt_ssl.h
 
 -include ../cflags.mk
 
 -include ../cflags.mk
index d82ec63..b74c5c5 100644 (file)
 # include "config.h"
 #endif
 
 # include "config.h"
 #endif
 
+#include <netinet/in.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <errno.h>
+
 #include <lib-lib/mem.h>
 #include <lib-lib/str.h>
 #include <lib-lib/macros.h>
 
 #include "mutt.h"
 #include <lib-lib/mem.h>
 #include <lib-lib/str.h>
 #include <lib-lib/macros.h>
 
 #include "mutt.h"
+
+#include "exit.h"
 #include "mutt_socket.h"
 #include "mutt_tunnel.h"
 
 #include "mutt_socket.h"
 #include "mutt_tunnel.h"
 
-
-#include <netinet/in.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/wait.h>
-#include <fcntl.h>
-#include <errno.h>
-
 /* -- data types -- */
 typedef struct {
   pid_t pid;
 /* -- data types -- */
 typedef struct {
   pid_t pid;
diff --git a/lib-sys/unix.c b/lib-sys/unix.c
new file mode 100644 (file)
index 0000000..372ff06
--- /dev/null
@@ -0,0 +1,63 @@
+/*
+ * Copyright notice from original mutt:
+ * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
+ * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
+ *
+ * This file is part of mutt-ng, see http://www.muttng.org/.
+ * It's licensed under the GNU General Public License,
+ * please see the file GPL in the top level source directory.
+ */
+
+#include <stdlib.h>
+
+#include <lib-lib/macros.h>
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+
+#include "unix.h"
+
+/* Extract the real name from /etc/passwd's GECOS field.
+ * When set, honor the regular expression in rx,
+ * otherwise assume that the GECOS field is a comma-separated list.
+ * Replace "&" by a capitalized version of the user's login name.
+ */
+ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx)
+{
+    const char *p, *end;
+    ssize_t len;
+
+    *dst = '\0';
+    len  = 0;
+
+    if (!pw->pw_gecos)
+        return 0;
+
+    if (rx) {
+        regmatch_t pat_match[1];
+
+        if (regexec(rx->rx, pw->pw_gecos, 1, pat_match, 0)) {
+            return 0;
+        }
+
+        p   = pw->pw_gecos + pat_match[0].rm_so;
+        end = pw->pw_gecos + pat_match[0].rm_so;
+    } else {
+        p   = pw->pw_gecos;
+        end = m_strchrnul(pw->pw_gecos, ',');
+    }
+
+    for (;;) {
+        const char *q = MIN(end, m_strchrnul(p, '&'));
+
+        len += m_strncpy(dst + len, n - len, p, q - p);
+        p = q + 1;
+
+        if (!p[-1] || p >= end)
+            break;
+
+        /* p[0] == '&' */
+        len += m_strcpy(dst + len, n - len, pw->pw_name);
+    }
+
+    return len;
+}
diff --git a/lib-sys/unix.h b/lib-sys/unix.h
new file mode 100644 (file)
index 0000000..b43077a
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ *  This program 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 of the License, or (at
+ *  your option) any later version.
+ *
+ *  This program 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; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ *  MA 02110-1301, USA.
+ *
+ *  Copyright © 2006 Pierre Habouzit
+ */
+
+#ifndef MUTT_LIB_SYS_UNIX_H
+#define MUTT_LIB_SYS_UNIX_H
+
+#include <sys/types.h>
+#include <pwd.h>
+
+#include <lib-lib/rx.h>
+
+ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx);
+
+#endif /* MUTT_LIB_SYS_UNIX_H */
index 729c050..1c54b13 100644 (file)
--- a/muttlib.c
+++ b/muttlib.c
@@ -400,51 +400,6 @@ char *_mutt_expand_path (char *s, size_t slen, int rx)
   return (s);
 }
 
   return (s);
 }
 
-/* Extract the real name from /etc/passwd's GECOS field.
- * When set, honor the regular expression in GecosMask,
- * otherwise assume that the GECOS field is a 
- * comma-separated list.
- * Replace "&" by a capitalized version of the user's login
- * name.
- */
-
-char *mutt_gecos_name (char *dest, ssize_t destlen, struct passwd *pw)
-{
-  regmatch_t pat_match[1];
-  ssize_t pwnl;
-  int idx;
-  char *p;
-
-  if (!pw || !pw->pw_gecos)
-    return NULL;
-
-  p_clear(dest, destlen);
-
-  if (GecosMask.rx) {
-    if (regexec (GecosMask.rx, pw->pw_gecos, 1, pat_match, 0) == 0)
-      m_strcpy(dest, MIN(pat_match[0].rm_eo - pat_match[0].rm_so + 1, destlen),
-               pw->pw_gecos + pat_match[0].rm_so);
-  }
-  else if ((p = strchr (pw->pw_gecos, ',')))
-    m_strcpy(dest, MIN(destlen, p - pw->pw_gecos + 1), pw->pw_gecos);
-  else
-    m_strcpy(dest, destlen, pw->pw_gecos);
-
-  pwnl = m_strlen(pw->pw_name);
-
-  for (idx = 0; dest[idx]; idx++) {
-    if (dest[idx] == '&') {
-      memmove (&dest[idx + pwnl], &dest[idx + 1],
-               MAX (destlen - idx - pwnl - 1, 0));
-      memcpy (&dest[idx], pw->pw_name, MIN (destlen - idx - 1, pwnl));
-      dest[idx] = toupper ((unsigned char) dest[idx]);
-    }
-  }
-
-  return dest;
-}
-
-
 char *mutt_get_parameter (const char *s, PARAMETER * p)
 {
   for (; p; p = p->next)
 char *mutt_get_parameter (const char *s, PARAMETER * p)
 {
   for (; p; p = p->next)
index 8148476..d72450d 100644 (file)
--- a/protos.h
+++ b/protos.h
@@ -83,7 +83,6 @@ char *mutt_iconv_hook (const char *);
 char *mutt_expand_path (char *, size_t);
 char *_mutt_expand_path (char *, size_t, int);
 char *mutt_find_hook (int, const char *);
 char *mutt_expand_path (char *, size_t);
 char *_mutt_expand_path (char *, size_t, int);
 char *mutt_find_hook (int, const char *);
-char *mutt_gecos_name (char *, ssize_t, struct passwd *);
 char *mutt_gen_msgid (void);
 char *mutt_get_body_charset (char *, size_t, BODY *);
 const char *mutt_get_name (address_t *);
 char *mutt_gen_msgid (void);
 char *mutt_get_body_charset (char *, size_t, BODY *);
 const char *mutt_get_name (address_t *);