awful.widget: fix tags removal
[awesome.git] / common / util.c
blob1833e8b294da464176fcf4e1e3a9ce559091e3a2
1 /*
2 * util.c - useful functions
4 * Copyright © 2007-2008 Julien Danjou <julien@danjou.info>
5 * Copyright © 2006 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <stdarg.h>
24 #include <sys/wait.h>
25 #include <unistd.h>
26 #include <limits.h>
28 #include "util.h"
29 #include "tokenize.h"
31 /** Print error and exit with EXIT_FAILURE code.
33 void
34 _fatal(int line, const char *fct, const char *fmt, ...)
36 va_list ap;
38 va_start(ap, fmt);
39 fprintf(stderr, "E: awesome: %s:%d: ", fct, line);
40 vfprintf(stderr, fmt, ap);
41 va_end(ap);
42 fprintf(stderr, "\n");
43 exit(EXIT_FAILURE);
46 /** Print error message on stderr.
48 void
49 _warn(int line, const char *fct, const char *fmt, ...)
51 va_list ap;
52 va_start(ap, fmt);
53 fprintf(stderr, "W: awesome: %s:%d: ", fct, line);
54 vfprintf(stderr, fmt, ap);
55 va_end(ap);
56 fprintf(stderr, "\n");
59 /** Lookup for a function pointer from its name
60 * in the given name_func_link_t list.
61 * \param funcname Function name.
62 * \param list Function and name link list.
63 * \return Function pointer.
65 void *
66 name_func_lookup(const char *funcname, const name_func_link_t *list)
68 int i;
70 if(funcname && list)
71 for(i = 0; list[i].name; i++)
72 if(!a_strcmp(funcname, list[i].name))
73 return list[i].func;
75 return NULL;
78 /** Lookup for a function name from its pointer
79 * in the given name_func_link_t list.
80 * \param funcp Function pointer.
81 * \param list Function and name link list.
82 * \return Name of the function.
84 const char *
85 name_func_rlookup(void * funcp, const name_func_link_t *list)
87 int i;
89 if(funcp && list)
90 for(i = 0; list[i].name; i++)
91 if(funcp == list[i].func)
92 return list[i].name;
94 return NULL;
97 /** Get a position type from a string.
98 * \param pos The position.
99 * \param len The string length, -1 if unknown.
100 * \return A position.
102 position_t
103 position_fromstr(const char *pos, ssize_t len)
105 switch(a_tokenize(pos, len))
107 default:
108 return Top;
109 case A_TK_BOTTOM:
110 return Bottom;
111 case A_TK_RIGHT:
112 return Right;
113 case A_TK_LEFT:
114 return Left;
115 case A_TK_FLOATING:
116 return Floating;
120 /** Convert a position type to a string.
121 * \param p The position.
122 * \return A position string.
124 const char *
125 position_tostr(position_t p)
127 switch(p)
129 case Top: return "top";
130 case Bottom: return "bottom";
131 case Right: return "right";
132 case Left: return "left";
133 case Floating: return "floating";
134 default: return NULL;
138 /** Get a orientation type from a string.
139 * \param pos The orientation.
140 * \param len The string length, -1 if unknown.
141 * \return A orientation.
143 orientation_t
144 orientation_fromstr(const char *pos, ssize_t len)
146 switch(a_tokenize(pos, len))
148 default:
149 return North;
150 case A_TK_SOUTH:
151 return South;
152 case A_TK_EAST:
153 return East;
157 /** Convert a orientation type to a string.
158 * \param p The orientation.
159 * \return A orientation string.
161 const char *
162 orientation_tostr(orientation_t p)
164 switch(p)
166 case North: return "north";
167 case South: return "south";
168 case East: return "east";
169 default: return NULL;
173 /** \brief safe limited strcpy.
175 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
176 * always adding a final \c \\0 in \c dst.
178 * \param[in] dst destination buffer.
179 * \param[in] n size of the buffer. Negative sizes are allowed.
180 * \param[in] src source string.
181 * \param[in] l maximum number of chars to copy.
183 * \return minimum of \c src \e length and \c l.
185 ssize_t
186 a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
188 ssize_t len = MIN(a_strlen(src), l);
190 if (n > 0)
192 ssize_t dlen = MIN(n - 1, len);
193 memcpy(dst, src, dlen);
194 dst[dlen] = '\0';
197 return len;
200 /** \brief safe strcpy.
202 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
203 * adding a final \c \\0 in \c dst.
205 * \param[in] dst destination buffer.
206 * \param[in] n size of the buffer. Negative sizes are allowed.
207 * \param[in] src source string.
208 * \return \c src \e length. If this value is \>= \c n then the copy was
209 * truncated.
211 ssize_t
212 a_strcpy(char *dst, ssize_t n, const char *src)
214 ssize_t len = a_strlen(src);
216 if (n > 0)
218 ssize_t dlen = MIN(n - 1, len);
219 memcpy(dst, src, dlen);
220 dst[dlen] = '\0';
223 return len;
226 /** Execute a command and replace the current process.
227 * \param cmd The command to execute.
229 void
230 a_exec(const char *cmd)
232 static const char *shell = NULL;
234 if(!shell && !(shell = getenv("SHELL")))
235 shell = "/bin/sh";
237 execl(shell, shell, "-c", cmd, NULL);
240 /** Split a string in substring.
241 * \param str The string to split.
242 * \param len The string length.
243 * \param delim The string delimiter.
244 * \return A list of string NULL terminted.
246 char **
247 a_strsplit(const char *str, ssize_t len, char delim)
249 char **retval;
250 int comp, i, j = 0, n = 1;
252 /* count components */
253 for(i = 0; i < len; i++)
254 if(str[i] == delim)
255 n++;
257 retval = p_new(char *, n + 1);
259 for(i = 0, comp = 0; comp < n; comp++)
261 if(str[i] == delim)
262 i++;
264 for(j = i; str[j] != delim && j < len; j++);
266 retval[comp] = p_dup(&str[i], j - i + 1);
267 retval[comp][j - i] = '\0';
269 i = j;
272 return retval;
275 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80