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.
31 /** Print error and exit with EXIT_FAILURE code.
34 _fatal(int line
, const char *fct
, const char *fmt
, ...)
39 fprintf(stderr
, "E: awesome: %s:%d: ", fct
, line
);
40 vfprintf(stderr
, fmt
, ap
);
42 fprintf(stderr
, "\n");
46 /** Print error message on stderr.
49 _warn(int line
, const char *fct
, const char *fmt
, ...)
53 fprintf(stderr
, "W: awesome: %s:%d: ", fct
, line
);
54 vfprintf(stderr
, fmt
, 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.
66 name_func_lookup(const char *funcname
, const name_func_link_t
*list
)
71 for(i
= 0; list
[i
].name
; i
++)
72 if(!a_strcmp(funcname
, list
[i
].name
))
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.
85 name_func_rlookup(void * funcp
, const name_func_link_t
*list
)
90 for(i
= 0; list
[i
].name
; i
++)
91 if(funcp
== list
[i
].func
)
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.
103 position_fromstr(const char *pos
, ssize_t len
)
105 switch(a_tokenize(pos
, len
))
120 /** Convert a position type to a string.
121 * \param p The position.
122 * \return A position string.
125 position_tostr(position_t 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.
144 orientation_fromstr(const char *pos
, ssize_t len
)
146 switch(a_tokenize(pos
, len
))
157 /** Convert a orientation type to a string.
158 * \param p The orientation.
159 * \return A orientation string.
162 orientation_tostr(orientation_t 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.
186 a_strncpy(char *dst
, ssize_t n
, const char *src
, ssize_t l
)
188 ssize_t len
= MIN(a_strlen(src
), l
);
192 ssize_t dlen
= MIN(n
- 1, len
);
193 memcpy(dst
, src
, dlen
);
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
212 a_strcpy(char *dst
, ssize_t n
, const char *src
)
214 ssize_t len
= a_strlen(src
);
218 ssize_t dlen
= MIN(n
- 1, len
);
219 memcpy(dst
, src
, dlen
);
226 /** Execute a command and replace the current process.
227 * \param cmd The command to execute.
230 a_exec(const char *cmd
)
232 static const char *shell
= NULL
;
234 if(!shell
&& !(shell
= getenv("SHELL")))
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.
247 a_strsplit(const char *str
, ssize_t len
, char delim
)
250 int comp
, i
, j
= 0, n
= 1;
252 /* count components */
253 for(i
= 0; i
< len
; i
++)
257 retval
= p_new(char *, n
+ 1);
259 for(i
= 0, comp
= 0; comp
< n
; comp
++)
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';
275 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80