release 1.3.4
[cygwin-run.git] / src / util.h
blob414aad79aaaab8fca1d45c599176dbf20118361b
1 /*
2 * Copyright (c) 2006,2009 Charles S. Wilson
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 #ifndef RUN2_UTIL_H
23 #define RUN2_UTIL_H
25 #include <stdarg.h>
26 #ifndef ORIGINAL_RUN
27 #include <ustr.h>
28 #endif
30 #if defined(__CYGWIN__)
31 # define PATH_SEP_CHAR_STR "/"
32 # define SEP_CHARS ":"
33 #else
34 # define PATH_SEP_CHAR_STR "\\"
35 # define SEP_CHARS ";"
36 #endif
38 #define RUN2_LOG_ALWAYS -1 /* used for --help, etc, even when --silent */
39 #define RUN2_LOG_FATAL 0 /* normal --silent behavior */
40 #define RUN2_LOG_ERROR 3 /* normal --no-silent behavior */
41 #define RUN2_LOG_WARN 4
42 #define RUN2_LOG_INFO 6 /* normal --verbose behavior */
43 #define RUN2_LOG_DEBUG 7 /* normal --debug[=1] behavior */
45 #define RUN2_DEFAULT_LOG_SILENT_LEVEL RUN2_LOG_FATAL
46 #define RUN2_DEFAULT_LOG_LEVEL RUN2_LOG_ERROR
47 #define RUN2_DEFAULT_LOG_VERBOSE_LEVEL RUN2_LOG_INFO
49 #if defined(PATH_MAX)
50 # define RUN2_PATHMAX PATH_MAX
51 #elif defined(MAXPATHLEN)
52 # define RUN2_PATHMAX MAXPATHLEN
53 #else
54 # define RUN2_PATHMAX 1024
55 #endif
57 #if __STDC_VERSION__ < 199901L
58 # if __GNUC__ >= 2
59 # define __func__ __FUNCTION__
60 # else
61 # define __func__ "<unknown>"
62 # endif
63 #endif
65 #ifndef ORIGINAL_RUN
66 #define USTR_CHECK(a) ustr_check (a, __FILE__, __LINE__)
67 Ustr* ustr_check (Ustr *s, const char *fn, int ln);
69 Ustr* run2_pfopen (const Ustr *name, const Ustr *dirs);
70 int run2_fileExists (Ustr **fullname, const Ustr* path, const Ustr* name);
71 #else
72 char* run2_pfopen (const char *name, const char *dirs);
73 int run2_fileExists (char **fullname, const char* path, const char* name);
74 #endif
75 void run2_message_ (int level, const char* fmt, ...);
76 void run2_vmessage_ (int level, const char* fmt, va_list args);
77 char** run2_dupargv (char **vector);
78 int run2_countargv (char **vector);
79 void run2_freeargv (char **vector);
81 const char *run2_get_program_name (void);
82 void run2_set_program_name (const char *p);
83 const char *run2_basename (const char *p);
84 int run2_ends_with(const char* s1, const char* s2);
85 void run2_strip_exe (char *s);
87 void run2_malloc_exit (void);
88 void run2_error (int status, int errnum, const char *message, ...);
89 void *run2_malloc (size_t);
90 void *run2_realloc (void *, size_t);
91 char *run2_strdup (const char *arg);
92 char *run2_quote_strdup (const char *arg, int quote);
93 char *run2_extend_str (const char *orig_value, const char *add, int to_end);
94 int run2_strtol(char *arg, long* value);
95 char *run2_get_homedir (const char *user);
97 /* modes */
98 int run2_gui_is_allowed(void);
99 int run2_tty_is_allowed(void);
100 int run2_get_verbose_level(void);
101 void run2_set_gui_mode(int mode); /* bool: true=enabled */
102 void run2_set_tty_mode(int mode); /* bool: true=enabled */
103 void run2_set_verbose_level(int level); /* int: higher turns on more messages */
106 #define realMsg(fmt, ...) run2_message_(RUN2_LOG_ALWAYS, fmt, ## __VA_ARGS__)
107 #define vrealMsg(fmt, args) run2_vmessage_(RUN2_LOG_ALWAYS, fmt, args)
109 #ifndef DEBUG
110 #define fatalMsg(fmt, ...) \
111 do { if (RUN2_LOG_FATAL <= run2_get_verbose_level()) { \
112 run2_message_(RUN2_LOG_FATAL, fmt, ## __VA_ARGS__); } \
113 } while(0)
114 #define errorMsg(fmt, ...) \
115 do { if (RUN2_LOG_ERROR <= run2_get_verbose_level()) { \
116 run2_message_(RUN2_LOG_ERROR, fmt, ## __VA_ARGS__); } \
117 } while(0)
118 #define warnMsg(fmt, ...) \
119 do { if (RUN2_LOG_WARN <= run2_get_verbose_level()) { \
120 run2_message_(RUN2_LOG_WARN, fmt, ## __VA_ARGS__); } \
121 } while(0)
122 #define infoMsg(fmt, ...) \
123 do { if (RUN2_LOG_INFO <= run2_get_verbose_level()) { \
124 run2_message_(RUN2_LOG_INFO, fmt, ## __VA_ARGS__); } \
125 } while(0)
126 #define debugMsg(level, fmt, ...) \
127 do { if (level + RUN2_LOG_INFO <= run2_get_verbose_level()) { \
128 run2_message_(level + RUN2_LOG_INFO, fmt, ## __VA_ARGS__); } \
129 } while(0)
131 #define vfatalMsg(fmt, ...) \
132 do { if (RUN2_LOG_FATAL <= run2_get_verbose_level()) { \
133 run2_vmessage_(RUN2_LOG_FATAL, fmt, ## __VA_ARGS__); } \
134 } while(0)
135 #define verrorMsg(fmt, ...) \
136 do { if (RUN2_LOG_ERROR <= run2_get_verbose_level()) { \
137 run2_vmessage_(RUN2_LOG_ERROR, fmt, ## __VA_ARGS__); } \
138 } while(0)
139 #define vwarnMsg(fmt, ...) \
140 do { if (RUN2_LOG_WARN <= run2_get_verbose_level()) { \
141 run2_vmessage_(RUN2_LOG_WARN, fmt, ## __VA_ARGS__); } \
142 } while(0)
143 #define vinfoMsg(fmt, ...) \
144 do { if (RUN2_LOG_INFO <= run2_get_verbose_level()) { \
145 run2_vmessage_(RUN2_LOG_INFO, fmt, ## __VA_ARGS__); } \
146 } while(0)
147 #define vdebugMsg(level, fmt, ...) \
148 do { if (level + RUN2_LOG_INFO <= run2_get_verbose_level()) { \
149 run2_vmessage_(level + RUN2_LOG_INFO, fmt, ## __VA_ARGS__); } \
150 } while(0)
152 #else
153 /* if DEBUG, then all messages are printed */
154 #define fatalMsg(fmt, ...) run2_message_ (RUN2_LOG_FATAL, fmt, ## __VA_ARGS__);
155 #define errorMsg(fmt, ...) run2_message_ (RUN2_LOG_ERROR, fmt, ## __VA_ARGS__);
156 #define warnMsg(fmt, ...) run2_message_ (RUN2_LOG_WARN, fmt, ## __VA_ARGS__);
157 #define infoMsg(fmt, ...) run2_message_ (RUN2_LOG_INFO, fmt, ## __VA_ARGS__);
158 #define debugMsg(level, fmt, ...) run2_message_ (level + RUN2_LOG_INFO, fmt, ## __VA_ARGS__)
159 #define vfatalMsg(fmt, args) run2_vmessage_(RUN2_LOG_FATAL, fmt, args);
160 #define verrorMsg(fmt, args) run2_vmessage_(RUN2_LOG_ERROR, fmt, args);
161 #define vwarnMsg(fmt, args) run2_vmessage_(RUN2_LOG_WARN, fmt, args);
162 #define vinfoMsg(fmt, args) run2_vmessage_(RUN2_LOG_INFO, fmt, args);
163 #define vdebugMsg(level, fmt, args) run2_vmessage_(level + RUN2_LOG_INFO, fmt, args)
164 #endif
166 #endif