util.h 9.7 KB

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