naughty: icon_size added to config and notify()
[awesome.git] / common / util.c
blobc49588a10978a658dc2704baa9bd1a2ffc183f18
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 <stdio.h>
25 #include <sys/wait.h>
26 #include <unistd.h>
27 #include <limits.h>
29 #include "util.h"
30 #include "tokenize.h"
32 /** Print error and exit with EXIT_FAILURE code.
34 void
35 _fatal(int line, const char *fct, const char *fmt, ...)
37 va_list ap;
39 va_start(ap, fmt);
40 fprintf(stderr, "E: awesome: %s:%d: ", fct, line);
41 vfprintf(stderr, fmt, ap);
42 va_end(ap);
43 fprintf(stderr, "\n");
44 exit(EXIT_FAILURE);
47 /** Print error message on stderr.
49 void
50 _warn(int line, const char *fct, const char *fmt, ...)
52 va_list ap;
53 va_start(ap, fmt);
54 fprintf(stderr, "W: awesome: %s:%d: ", fct, line);
55 vfprintf(stderr, fmt, ap);
56 va_end(ap);
57 fprintf(stderr, "\n");
60 /** Lookup for a function pointer from its name
61 * in the given name_func_link_t list.
62 * \param funcname Function name.
63 * \param list Function and name link list.
64 * \return Function pointer.
66 void *
67 name_func_lookup(const char *funcname, const name_func_link_t *list)
69 int i;
71 if(funcname && list)
72 for(i = 0; list[i].name; i++)
73 if(!a_strcmp(funcname, list[i].name))
74 return list[i].func;
76 return NULL;
79 /** Lookup for a function name from its pointer
80 * in the given name_func_link_t list.
81 * \param funcp Function pointer.
82 * \param list Function and name link list.
83 * \return Name of the function.
85 const char *
86 name_func_rlookup(void * funcp, const name_func_link_t *list)
88 int i;
90 if(funcp && list)
91 for(i = 0; list[i].name; i++)
92 if(funcp == list[i].func)
93 return list[i].name;
95 return NULL;
98 /** Get a position type from a string.
99 * \param pos The position.
100 * \param len The string length, -1 if unknown.
101 * \return A position.
103 position_t
104 position_fromstr(const char *pos, ssize_t len)
106 switch(a_tokenize(pos, len))
108 default:
109 return Top;
110 case A_TK_BOTTOM:
111 return Bottom;
112 case A_TK_RIGHT:
113 return Right;
114 case A_TK_LEFT:
115 return Left;
116 case A_TK_FLOATING:
117 return Floating;
121 /** Convert a position type to a string.
122 * \param p The position.
123 * \return A position string.
125 const char *
126 position_tostr(position_t p)
128 switch(p)
130 case Top: return "top";
131 case Bottom: return "bottom";
132 case Right: return "right";
133 case Left: return "left";
134 case Floating: return "floating";
135 default: return NULL;
139 /** Get a orientation type from a string.
140 * \param pos The orientation.
141 * \param len The string length, -1 if unknown.
142 * \return A orientation.
144 orientation_t
145 orientation_fromstr(const char *pos, ssize_t len)
147 switch(a_tokenize(pos, len))
149 default:
150 return North;
151 case A_TK_SOUTH:
152 return South;
153 case A_TK_EAST:
154 return East;
158 /** Convert a orientation type to a string.
159 * \param p The orientation.
160 * \return A orientation string.
162 const char *
163 orientation_tostr(orientation_t p)
165 switch(p)
167 case North: return "north";
168 case South: return "south";
169 case East: return "east";
170 default: return NULL;
174 /** \brief safe limited strcpy.
176 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
177 * always adding a final \c \\0 in \c dst.
179 * \param[in] dst destination buffer.
180 * \param[in] n size of the buffer. Negative sizes are allowed.
181 * \param[in] src source string.
182 * \param[in] l maximum number of chars to copy.
184 * \return minimum of \c src \e length and \c l.
186 ssize_t
187 a_strncpy(char *dst, ssize_t n, const char *src, ssize_t l)
189 ssize_t len = MIN(a_strlen(src), l);
191 if (n > 0)
193 ssize_t dlen = MIN(n - 1, len);
194 memcpy(dst, src, dlen);
195 dst[dlen] = '\0';
198 return len;
201 /** \brief safe strcpy.
203 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
204 * adding a final \c \\0 in \c dst.
206 * \param[in] dst destination buffer.
207 * \param[in] n size of the buffer. Negative sizes are allowed.
208 * \param[in] src source string.
209 * \return \c src \e length. If this value is \>= \c n then the copy was
210 * truncated.
212 ssize_t
213 a_strcpy(char *dst, ssize_t n, const char *src)
215 ssize_t len = a_strlen(src);
217 if (n > 0)
219 ssize_t dlen = MIN(n - 1, len);
220 memcpy(dst, src, dlen);
221 dst[dlen] = '\0';
224 return len;
227 /** Execute a command and replace the current process.
228 * \param cmd The command to execute.
230 void
231 a_exec(const char *cmd)
233 static const char *shell = NULL;
235 if(!shell && !(shell = getenv("SHELL")))
236 shell = "/bin/sh";
238 execl(shell, shell, "-c", cmd, NULL);
241 /** Split a string in substring.
242 * \param str The string to split.
243 * \param len The string length.
244 * \param delim The string delimiter.
245 * \return A list of string NULL terminted.
247 char **
248 a_strsplit(const char *str, ssize_t len, char delim)
250 char **retval;
251 int comp, i, j = 0, n = 1;
253 /* count components */
254 for(i = 0; i < len; i++)
255 if(str[i] == delim)
256 n++;
258 retval = p_new(char *, n + 1);
260 for(i = 0, comp = 0; comp < n; comp++)
262 if(str[i] == delim)
263 i++;
265 for(j = i; str[j] != delim && j < len; j++);
267 retval[comp] = p_dup(&str[i], j - i + 1);
268 retval[comp][j - i] = '\0';
270 i = j;
273 return retval;
276 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80