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.
32 /** Print error and exit with EXIT_FAILURE code.
35 _fatal(int line
, const char *fct
, const char *fmt
, ...)
40 fprintf(stderr
, "E: awesome: %s:%d: ", fct
, line
);
41 vfprintf(stderr
, fmt
, ap
);
43 fprintf(stderr
, "\n");
47 /** Print error message on stderr.
50 _warn(int line
, const char *fct
, const char *fmt
, ...)
54 fprintf(stderr
, "W: awesome: %s:%d: ", fct
, line
);
55 vfprintf(stderr
, fmt
, 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.
67 name_func_lookup(const char *funcname
, const name_func_link_t
*list
)
72 for(i
= 0; list
[i
].name
; i
++)
73 if(!a_strcmp(funcname
, list
[i
].name
))
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.
86 name_func_rlookup(void * funcp
, const name_func_link_t
*list
)
91 for(i
= 0; list
[i
].name
; i
++)
92 if(funcp
== list
[i
].func
)
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.
104 position_fromstr(const char *pos
, ssize_t len
)
106 switch(a_tokenize(pos
, len
))
121 /** Convert a position type to a string.
122 * \param p The position.
123 * \return A position string.
126 position_tostr(position_t 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.
145 orientation_fromstr(const char *pos
, ssize_t len
)
147 switch(a_tokenize(pos
, len
))
158 /** Convert a orientation type to a string.
159 * \param p The orientation.
160 * \return A orientation string.
163 orientation_tostr(orientation_t 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.
187 a_strncpy(char *dst
, ssize_t n
, const char *src
, ssize_t l
)
189 ssize_t len
= MIN(a_strlen(src
), l
);
193 ssize_t dlen
= MIN(n
- 1, len
);
194 memcpy(dst
, src
, dlen
);
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
213 a_strcpy(char *dst
, ssize_t n
, const char *src
)
215 ssize_t len
= a_strlen(src
);
219 ssize_t dlen
= MIN(n
- 1, len
);
220 memcpy(dst
, src
, dlen
);
227 /** Execute a command and replace the current process.
228 * \param cmd The command to execute.
231 a_exec(const char *cmd
)
233 static const char *shell
= NULL
;
235 if(!shell
&& !(shell
= getenv("SHELL")))
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.
248 a_strsplit(const char *str
, ssize_t len
, char delim
)
251 int comp
, i
, j
= 0, n
= 1;
253 /* count components */
254 for(i
= 0; i
< len
; i
++)
258 retval
= p_new(char *, n
+ 1);
260 for(i
= 0, comp
= 0; comp
< n
; comp
++)
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';
276 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80