c7c635cda1e1208e4511b362072a3164ccbad61d
[oweals/cde.git] / cde / programs / dtappbuilder / src / libAButil / util.h
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23
24 /*
25  *      $XConsortium: util.h /main/4 1995/11/06 18:52:47 rswiston $
26  *
27  * @(#)util.h   1.39 14 Feb 1994        cde_app_builder/src/libAButil
28  *
29  *      RESTRICTED CONFIDENTIAL INFORMATION:
30  *      
31  *      The information in this document is subject to special
32  *      restrictions in a confidential disclosure agreement between
33  *      HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
34  *      document outside HP, IBM, Sun, USL, SCO, or Univel without
35  *      Sun's specific written approval.  This document and all copies
36  *      and derivative works thereof must be returned or destroyed at
37  *      Sun's request.
38  *
39  *      Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
40  *
41  */
42
43 /*
44  * util.h - general utilities
45  */
46 #ifndef _AB_ABUTIL_H_
47 #define _AB_ABUTIL_H_
48
49 #ifndef _POSIX_SOURCE
50 #define _POSIX_SOURCE 1         /* we want to be POSIX-compliant */
51 #endif
52
53 #include <sys/types.h>
54 #include <unistd.h>
55 #include <stdlib.h>
56 #include <stdio.h>
57 #include <ab_private/AB.h>
58 #include <ab/util_types.h>
59 #include <ab_private/util_err.h>
60
61 /*
62  * <string.h> seems to have a problem cc -Xc is continually complaining
63  * about strdup().
64  */
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #if !defined(linux)
70 extern char     *strdup(const char *);
71 #endif
72
73 #ifdef __cplusplus
74 } /*extern "C" */
75 #endif
76
77
78 typedef enum
79 {
80     AB_OS_UNDEF = 0,
81     AB_OS_HPUX,
82     AB_OS_AIX,
83     AB_OS_SUNOS,
84     AB_OS_OSF1,
85     AB_OS_UNIXWARE,
86     AB_OS_UXP,
87     AB_OS_LNX,
88     AB_OS_FBSD,
89     AB_OS_NBSD,
90     AB_OS_OBSD,
91     AB_OS_TYPE_NUM_VALUES       /* must be last */
92 } AB_OS_TYPE;
93
94
95 typedef int UTIL_OUTPUT_HANDLER(STRING error_message);
96 typedef UTIL_OUTPUT_HANDLER *UtilOutputHandler;
97
98 extern const STRING     Util_null_string;       /* "(nil)" */
99 extern const STRING     Util_empty_string;      /* "" */
100 extern int              utilP_verbosityPriv3602759317;  /* private! */
101
102 /*
103  * Initialization
104  */
105 int     util_init(int *argc_in_out, STRING **argv_in_out);
106
107 /*
108  *  Memory management
109  */
110 #define util_free(ptr) {if ((ptr) != NULL) {free(ptr); (ptr)= NULL;}}
111 #define util_malloc(size) (malloc(size))
112
113
114 /*
115  * General
116  */
117 int     util_putenv(STRING string);
118 #define util_min(a,b) (((a) < (b))? (a):(b))
119 #define util_max(a,b) (((a) > (b))? (a):(b))
120 #define util_xor(a,b) ( (((a)?1:0) + ((b)?1:0)) == 1 ) /* logical xor */
121 int     util_set_program_name(STRING this_prog_name);
122 STRING  util_get_program_name(void);
123 int     util_set_program_name_from_argv0(STRING argv0);
124 AB_OS_TYPE      util_get_os_type(void);
125 STRING          util_os_type_to_string(AB_OS_TYPE);
126 STRING          util_os_type_to_ident(AB_OS_TYPE);
127
128
129 /*
130  * Verbosity
131  *
132  * If verbosity is >= 3, debugging is turned on.
133  *      verbosity < 3 => debugging level 0
134  *      verbosity 3 => debugging level 1
135  *      verbosity 4 => debugging level 2
136  *      et cetera...
137  */
138 int     util_set_verbosity(int verbosity);
139 int     util_get_verbosity(void);
140 BOOL    util_be_silent(void);
141 BOOL    util_be_verbose(void);
142 int     util_get_debug_level(void);
143 int     debug_level(void);      /* these are special-case shortcuts that */
144 BOOL    debugging();            /* don't conform to the naming convention */
145
146
147 /*
148  * Input/Output
149  *
150  * util_puts_err() and util_printf_err() should be used for messages
151  * that absolutely must be seen by the user (this should cause a popup
152  * to appear, when ab is running).
153  *
154  * util_puts() and util_printf() should be used for informative messages,
155  * and may or may not actually be presented to the user.
156  *
157  * util_dputs() and util_dprintf() should be used for debugging messages.
158  * Their first parameter is the debugging level at which the message should
159  * be printed. (verbosity 3 = debug level 1). If debugging is disabled
160  * (e.g., verbosity < 3), these functions never generate output.
161  * 
162  * To redirect the error output, use util_set_err_output_handler. ALL error 
163  * output will then be sent to that error handler. A value of NULL sends 
164  * error messages to stderr (which is the startup default). This is normally 
165  * used to add an error handler that will pop up an error dialog when
166  * running in a windowed application.
167  *
168  * util_set_output_handler() works similarly to the error handler.
169  */
170 int     util_set_err_output_handler(UtilOutputHandler);
171 int     util_set_output_handler(UtilOutputHandler);
172 int     util_puts(STRING msg);
173 int     util_puts_err(STRING msg);
174 int     util_printf(STRING fmt, ...);
175 int     util_printf_err(STRING fmt, ...);
176 void    util_set_help_data(STRING help, STRING vol, STRING locID);
177 int     util_get_help_data(STRING *help, STRING *vol, STRING *locID);
178
179         /* print output if debugging level >= specified */
180 int     util_dputs(int debug_level, STRING msg);
181 int     util_dprintf(int debug_level, STRING fmt, ...);
182
183 /*
184  * files
185  */
186 #define util_fopen fopen                /* for consistency */
187 int     util_unbuffer_file(FILE *fp);   /* for debugging - removes all */
188                                         /* buffer from the stream */
189 BOOL    util_file_is_regular_file(STRING filename);
190 BOOL    util_file_is_directory(STRING filename);
191 BOOL    util_directory_exists(STRING dirName);
192 BOOL    util_file_exists(STRING fileName);
193 long    util_file_size(STRING fileName); /* error if file don't exist */
194 BOOL    util_paths_are_same_file(STRING path1, STRING path2);
195 int     util_fdtruncate(int fildes, off_t length);      /* truncate open file*/
196 int     util_ftruncate(FILE *file, off_t length, const char *accessType);
197 int     util_flock(
198                         FILE    *file,
199                         BOOL    wait, 
200                         int     lockType, 
201                         off_t   offset, 
202                         off_t   length
203         );
204 int     util_funlock(
205                         FILE    *file,
206                         off_t   offset, 
207                         off_t   length
208         );
209 FILE    *util_fopen_locked(const char *filename, const char *type);
210
211 BOOL    util_file_name_has_extension(STRING file_name, STRING ext);
212 BOOL    util_file_name_has_ab_extension(STRING file_name);
213 BOOL    util_file_name_is_bil_encapsulated(STRING file_name);
214 BOOL    util_file_name_is_bil_module(STRING file_name);
215 BOOL    util_file_name_is_bil_proj(STRING file_name);
216 FILE*   util_create_tmp_file(char *data);
217
218 int     util_cvt_path_to_relative(              /* NULL from = cwd */
219                         STRING  path,
220                         STRING  from,
221                         char    *buf,
222                         int     buf_size);
223
224 /* 
225  * strings
226  *
227  * THE UTIL STRING FUNCTIONS *ALWAYS* NULL-TERMINATE ANY RETURNED STRINGS!
228  *
229  * Note: len = length of string withOUT terminating 0
230  *       size = size of string including terminating 0 ( = len+1 )
231  */
232 BOOL    util_streq(STRING s1, STRING s2);               /* True if strings = */
233 BOOL    util_strcmp(STRING s1, STRING s2);      /* allows NULL strings */
234 STRING  util_strsafe(STRING s);         /*returns "(nil)" for NULL strs*/
235 BOOL    util_strempty(STRING s);                /* looks for NULL AND "" */
236 int     util_strncpy(STRING to, STRING from, int to_size);
237 int     util_strcvt_to_lower(STRING to, STRING from, int to_size);
238 int     util_strcasestr(STRING str, STRING substr);
239 int     util_strncasecmp(STRING s1, STRING s2, int max_chars);
240 STRING  util_strip_white_space(STRING string);
241
242
243 STRING          util_get_file_name_from_path(
244                         STRING  path,
245                         STRING  fileNameBuf,
246                         int     fileNameBufSize
247                 );
248
249 STRING          util_get_dir_name_from_path(
250                         STRING  path,
251                         STRING  dirNameBuf,
252                         int     dirNameBufSize
253                 );
254
255 int             util_mkdir_hier(
256                         STRING path
257                 );
258
259 BOOL            util_path_is_absolute(
260                         STRING      dir 
261                 );
262
263 int             util_derive_name_from_path(
264                     char    *fullpath,
265                     char    *objname
266                 );
267
268 int             util_check_name(
269                     STRING      name,
270                     STRING      new_name
271                 );
272
273 /*
274  * Process control
275  */
276 pid_t   util_vfork(void);               /* lightweight fork() */
277
278
279 /* 
280  * conversions
281  */
282 STRING  util_cvt_bool_to_string(BOOL bool_value, STRING buf, int buf_size);
283
284
285
286 /*************************************************************************
287 **                                                                      **
288 **              Inline implementation                                   **
289 **                                                                      **
290 *************************************************************************/
291
292 /* check 1st chars before calling strcmp - avoids a lot of calls */
293 #define util_streq(s1,s2) \
294    (   ((s1) == (s2)) \
295     || (((s1) != NULL) && ((s2) != NULL) && (((s1)[0] == (s2)[0]) && (strcmp(s1,s2) == 0))) \
296    )
297
298 /*
299  * allows compare of null strings (NULL < "")
300  */
301 #define util_strcmp(s1,s2)              \
302     (((s1) == (s2))?                    \
303         0                               \
304     :                                   \
305         ((s1 == NULL)?                  \
306             (-1)                        \
307         :                               \
308             ((s2) == NULL?              \
309                 (1)                     \
310             :                           \
311                 strcmp((s1),(s2))       \
312             )                           \
313         )                               \
314     )
315
316 #define util_strlen(s)  ((s) == NULL? 0:strlen(s))
317
318 #define util_strsafe(s) ((s) == NULL? Util_null_string:(s))
319
320 #define util_strempty(s) (s == NULL || (strcmp(s, "") == 0))
321
322 /*
323  * Verbosity
324  */
325 #define util_get_verbosity()    (utilP_verbosityPriv3602759317)
326 #define util_be_silent()        (util_get_verbosity() < 1)
327 #define util_be_verbose()       (util_get_verbosity() > 1)
328 #define util_get_debug_level()  (util_max(0, util_get_verbosity() - 2))
329 #define debug_level()           ((int)(util_get_debug_level()))
330 #define debugging()             ((BOOL)(debug_level() > 0))
331
332 /*
333  * We key on DEBUG here, so that if DEBUG is turned off, these macros
334  * evaluate to constants. That should allow the compiler to remove
335  * sections of code that are executed conditionally based on these
336  * macros.
337  */
338 #ifndef DEBUG
339 #undef  util_get_debug_level
340 #define util_get_debug_level()  (0)
341 #undef  debug_level
342 #define debug_level()           (0)
343 #undef  debugging
344 #define debugging()             (FALSE)
345 #endif /* DEBUG */
346
347 #define util_fclose(fp) ((fp) == NULL? 0:(util_funlock(fp, 0, 0), fclose(fp), (fp)= NULL, 0))
348
349 /*
350  * These functions are obsolete.  Here for backwards compatibility
351  */
352 #define util_error(s) util_puts_err(s)
353
354 #endif /* _AB_ABUTIL_H_ */
355