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.
29 #include "common/util.h"
30 #include "common/tokenize.h"
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 /** Get a position type from a string.
61 * \param pos The position.
62 * \param len The string length, -1 if unknown.
66 position_fromstr(const char *pos
, ssize_t len
)
68 switch(a_tokenize(pos
, len
))
81 /** Convert a position type to a string.
82 * \param p The position.
83 * \return A position string.
86 position_tostr(position_t p
)
90 case Top
: return "top";
91 case Bottom
: return "bottom";
92 case Right
: return "right";
93 case Left
: return "left";
98 /** Get a orientation type from a string.
99 * \param pos The orientation.
100 * \param len The string length, -1 if unknown.
101 * \return A orientation.
104 orientation_fromstr(const char *pos
, ssize_t len
)
106 switch(a_tokenize(pos
, len
))
117 /** Convert a orientation type to a string.
118 * \param p The orientation.
119 * \return A orientation string.
122 orientation_tostr(orientation_t p
)
126 case North
: return "north";
127 case South
: return "south";
128 case East
: return "east";
129 default: return NULL
;
133 /** \brief safe limited strcpy.
135 * Copies at most min(<tt>n-1</tt>, \c l) characters from \c src into \c dst,
136 * always adding a final \c \\0 in \c dst.
138 * \param[in] dst destination buffer.
139 * \param[in] n size of the buffer. Negative sizes are allowed.
140 * \param[in] src source string.
141 * \param[in] l maximum number of chars to copy.
143 * \return minimum of \c src \e length and \c l.
146 a_strncpy(char *dst
, ssize_t n
, const char *src
, ssize_t l
)
148 ssize_t len
= MIN(a_strlen(src
), l
);
152 ssize_t dlen
= MIN(n
- 1, len
);
153 memcpy(dst
, src
, dlen
);
160 /** \brief safe strcpy.
162 * Copies at most <tt>n-1</tt> characters from \c src into \c dst, always
163 * adding a final \c \\0 in \c dst.
165 * \param[in] dst destination buffer.
166 * \param[in] n size of the buffer. Negative sizes are allowed.
167 * \param[in] src source string.
168 * \return \c src \e length. If this value is \>= \c n then the copy was
172 a_strcpy(char *dst
, ssize_t n
, const char *src
)
174 ssize_t len
= a_strlen(src
);
178 ssize_t dlen
= MIN(n
- 1, len
);
179 memcpy(dst
, src
, dlen
);
186 /** Execute a command and replace the current process.
187 * \param cmd The command to execute.
190 a_exec(const char *cmd
)
192 static const char *shell
= NULL
;
194 if(!shell
&& !(shell
= getenv("SHELL")))
197 execl(shell
, shell
, "-c", cmd
, NULL
);
200 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80