more simplifications and header moves
[apps/madmutt.git] / lib.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1996-2000 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 /*
12  * This file used to contain some more functions, namely those
13  * which are now in muttlib.c.  They have been removed, so we have
14  * some of our "standard" functions in external programs, too.
15  */
16
17 #if HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include <string.h>
22 #include <ctype.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <sys/wait.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <pwd.h>
30
31 #ifdef HAVE_SYSEXITS_H
32 #include <sysexits.h>
33 #else /* Make sure EX_OK is defined <philiph@pobox.com> */
34 #define EX_OK 0
35 #endif
36
37 #include <lib-lib/macros.h>
38 #include <lib-lib/mem.h>
39 #include <lib-lib/str.h>
40
41 #include "lib.h"
42
43 #include "lib/debug.h"
44
45 static struct sysexits
46 {
47   int v;
48   const char *str;
49
50 sysexits_h[] = 
51 {
52 #ifdef EX_USAGE
53   { 0xff & EX_USAGE, "Bad usage." },
54 #endif
55 #ifdef EX_DATAERR
56   { 0xff & EX_DATAERR, "Data format error." },
57 #endif
58 #ifdef EX_NOINPUT
59   { 0xff & EX_NOINPUT, "Cannot open input." },
60 #endif
61 #ifdef EX_NOUSER
62   { 0xff & EX_NOUSER, "User unknown." },
63 #endif
64 #ifdef EX_NOHOST
65   { 0xff & EX_NOHOST, "Host unknown." },
66 #endif
67 #ifdef EX_UNAVAILABLE
68   { 0xff & EX_UNAVAILABLE, "Service unavailable." },
69 #endif
70 #ifdef EX_SOFTWARE
71   { 0xff & EX_SOFTWARE, "Internal error." },
72 #endif
73 #ifdef EX_OSERR
74   { 0xff & EX_OSERR, "Operating system error." },
75 #endif
76 #ifdef EX_OSFILE
77   { 0xff & EX_OSFILE, "System file missing." },
78 #endif
79 #ifdef EX_CANTCREAT
80   { 0xff & EX_CANTCREAT, "Can't create output." },
81 #endif
82 #ifdef EX_IOERR
83   { 0xff & EX_IOERR, "I/O error." },
84 #endif
85 #ifdef EX_TEMPFAIL
86   { 0xff & EX_TEMPFAIL, "Deferred." },
87 #endif
88 #ifdef EX_PROTOCOL
89   { 0xff & EX_PROTOCOL, "Remote protocol error." },
90 #endif
91 #ifdef EX_NOPERM
92   { 0xff & EX_NOPERM, "Insufficient permission." },
93 #endif
94 #ifdef EX_CONFIG
95   { 0xff & EX_NOPERM, "Local configuration error." },
96 #endif
97   { S_ERR, "Exec error." },
98   { -1, NULL}
99 };
100
101 void mutt_nocurses_error (const char *fmt, ...)
102 {
103   va_list ap;
104
105   va_start (ap, fmt);
106   vfprintf (stderr, fmt, ap);
107   va_end (ap);
108   fputc ('\n', stderr);
109 }
110
111
112 /* these characters must be escaped in regular expressions */
113
114 static char rx_special_chars[] = "^.[$()|*+?{\\";
115
116 int mutt_rx_sanitize_string (char *dest, size_t destlen, const char *src)
117 {
118   while (*src && --destlen > 2) {
119     if (strchr (rx_special_chars, *src)) {
120       *dest++ = '\\';
121       destlen--;
122     }
123     *dest++ = *src++;
124   }
125
126   *dest = '\0';
127
128   if (*src)
129     return -1;
130   else
131     return 0;
132 }
133
134 const char *
135 mutt_strsysexit(int e)
136 {
137   int i;
138   
139   for(i = 0; sysexits_h[i].str; i++)
140   {
141     if(e == sysexits_h[i].v)
142       break;
143   }
144   
145   return sysexits_h[i].str;
146 }