| 1 |
<HTML> |
| 2 |
|
| 3 |
<HEAD> |
| 4 |
<TITLE> ACEDB source code documentation: the graph library </TITLE> |
| 5 |
|
| 6 |
<LINK rel=stylesheet type="text/css" |
| 7 |
href="./wdoc.css" |
| 8 |
title="Style sheet for wdoc developer documents"> |
| 9 |
|
| 10 |
</HEAD> |
| 11 |
|
| 12 |
<BODY> |
| 13 |
|
| 14 |
<H1>Application programmers' guide to the messubs utilities</H1> |
| 15 |
|
| 16 |
<PRE> |
| 17 |
|
| 18 |
There are several ancient and/or basic utility packages bundled into |
| 19 |
regular.h. |
| 20 |
|
| 21 |
--------------------------------------------------------------------------- |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
Message system |
| 26 |
-------------- |
| 27 |
|
| 28 |
messubs contains basic routines for message handling, crash control, |
| 29 |
memory management. One aim was to localise all the *printf() type |
| 30 |
functions in messubs. The more general form is in w1/messubs.c. |
| 31 |
|
| 32 |
|
| 33 |
Utility routines |
| 34 |
---------------- |
| 35 |
messubs.c contains a number of utility routines for use with the |
| 36 |
message routines: |
| 37 |
|
| 38 |
|
| 39 |
messSetMsgInfo(char *progname, char *progversion) |
| 40 |
|
| 41 |
Record the program name and version, the real and effective userids and |
| 42 |
the network id of the machine the program is being run on get automatically |
| 43 |
recorded as well. This information will automatically be incorporated into messages. |
| 44 |
|
| 45 |
|
| 46 |
char *messGetErrorProgram (void) |
| 47 |
|
| 48 |
Retrieve the name of the program being run. |
| 49 |
|
| 50 |
|
| 51 |
char *messprintf (char *format, ...) |
| 52 |
|
| 53 |
formats a message using sprintf into (static!) buffer - returns the buffer. |
| 54 |
(see hprintf for a better version). |
| 55 |
|
| 56 |
char *hprintf(STORE_HANDLE handle, char *format, ...) |
| 57 |
|
| 58 |
formats a message into a dynamically allocated buffer (allocated on the |
| 59 |
supplied handle) and returns that buffer, the buffer must be freed using |
| 60 |
either messfree() if handle=0, or handleDestroy() if a different handle |
| 61 |
was used. |
| 62 |
|
| 63 |
|
| 64 |
Callback routines |
| 65 |
----------------- |
| 66 |
So that the message routines can be used in both command line and graphical |
| 67 |
contexts, the message package uses a system of registered callback routines |
| 68 |
to allow applications to control how messages are output. |
| 69 |
|
| 70 |
By default (i.e. if the application does not register any callbacks), messages |
| 71 |
go to stdout and stderr. The application can however register the following |
| 72 |
callbacks: |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
Crash routines |
| 79 |
-------------- |
| 80 |
There are occasions where the application instead of exitting on a |
| 81 |
messcrash would like to regain control in some other routine. |
| 82 |
|
| 83 |
A good example would be during the dump of the database. The |
| 84 |
code may messcrash for a number of reasons, the application should not however |
| 85 |
terminate, but instead carry on to try and dump as much of the database as |
| 86 |
it can. To achieve this the application can register crash routines that will |
| 87 |
be called when application code elsewhere calls messcrash, the crashroutine |
| 88 |
can then jump back to some predefined point where dumping can resume as though |
| 89 |
nothing had happened. |
| 90 |
|
| 91 |
The message package uses the C setjmp/longjmp calls to achieve this. Note that |
| 92 |
this is a completely different mechanism from the callback routines mentioned |
| 93 |
above. All context between the setjmp routine and the messcrash routine is |
| 94 |
lost, this can be very useful for certain types of error situation, e.g. parsers. |
| 95 |
|
| 96 |
It is the APPLICATIONS RESPONSIBILITY to control the amount of recursion here, |
| 97 |
the message package just blindly calls the crashroutine and in NO WAY attempts |
| 98 |
to catch run away looping. (contrast this with callbacks above). |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
Message routines |
| 103 |
---------------- |
| 104 |
|
| 105 |
void messout (char *format,...) |
| 106 |
|
| 107 |
displays a blocking message on the screen using graphOut(). If there |
| 108 |
is no graph package it prints to stderr. If the dump file is already |
| 109 |
open it also writes the message to the dump file via messdump(). |
| 110 |
|
| 111 |
void messdump (char *format,...) |
| 112 |
|
| 113 |
appends the message to the dump file, which is by default "logbook.wrm". |
| 114 |
|
| 115 |
|
| 116 |
void messerror (char *format,...) |
| 117 |
|
| 118 |
related to perror(). Forces a crash via messcrash() after 50 calls. |
| 119 |
Use messerror when there has been an acedb error unrelated to any system |
| 120 |
call, use messSysError when a system call failed and errno will be |
| 121 |
used to return information about the reason for the system call failure. |
| 122 |
|
| 123 |
void messcrash (char *format,...) |
| 124 |
& void messSysCrash (char *format,...) |
| 125 |
|
| 126 |
writes out the user supplied message, then calls |
| 127 |
invokedebugger(), then tries to call any cleanup routine that has been |
| 128 |
registered via crashroutine(). This process is not reentrant. Any |
| 129 |
subsequent call to messcrash() leads to an immediate exit(). |
| 130 |
Use messcrash if there was an acedb error unrelated to any system call, |
| 131 |
use messSysCrash when a system call failed and errno will be |
| 132 |
used to return information about the reason for the system call failure. |
| 133 |
|
| 134 |
|
| 135 |
VoidRoutine crashroutine (VoidRoutine cleanup) |
| 136 |
|
| 137 |
registers cleanup() to be called during a crash by messcrash(). |
| 138 |
|
| 139 |
|
| 140 |
void messdumpset (char *cp) |
| 141 |
|
| 142 |
sets the dumpfile name if called before the first call to messdump(). |
| 143 |
|
| 144 |
void messbeep (void) - issues a beep |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
MessErrorCondContext messErrorCondCreate(MessErrorCondMsg message_policy, |
| 149 |
BOOL signal_fail_on_first_error, |
| 150 |
void *msg_func_data) ; |
| 151 |
BOOL messErrorCond(MessErrorCondContext mess_context, char *format, ...) ; |
| 152 |
|
| 153 |
These calls implement a conditional error/fail output routine. They are used to give the |
| 154 |
user control over whether none, just the first or all of the error messages from a certain |
| 155 |
operation should be output and whether the operation should stop at the first error or |
| 156 |
keep going. |
| 157 |
|
| 158 |
|
| 159 |
The application should call them like this: |
| 160 |
|
| 161 |
MessErrorCondContext mesg_cont ; |
| 162 |
BOOL result = FALSE ; |
| 163 |
|
| 164 |
/* this gets filled in according to how user wants to do things. */ |
| 165 |
mesg_cont = messErrorCondCreate(MESSERRORCONDMSGS_FIRST, TRUE, NULL) ; |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
...later in some complex, many stepped operation.... |
| 171 |
|
| 172 |
|
| 173 |
if (!(this part of the operation) |
| 174 |
&& (!(messErrorCond(mesg_cont, "operation failed with: %d", 999)))) |
| 175 |
{ |
| 176 |
abort whole operation |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
This saves having to keep a lot of state and the same old logic throughout |
| 181 |
the code of the operation. An example would be in the fMapConvert() function |
| 182 |
where there is much smapping of features and the user may or may not want |
| 183 |
to see all the error messages. |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
--------------------------------------------------------------------------- |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
Free Store system |
| 193 |
----------------- |
| 194 |
|
| 195 |
All dynamic memory allocation should be done via this package. Note |
| 196 |
that it is not permissable to call messfree() in the result of a call |
| 197 |
to malloc(), or free() on the results of a messalloc(). |
| 198 |
|
| 199 |
char *messalloc (int size) |
| 200 |
|
| 201 |
is analogous to malloc() with a messcrash() if it fails. However |
| 202 |
there is some extra overhead in both memory and time, so repeated |
| 203 |
messalloc/messfree calls on many small items should be avoided. |
| 204 |
|
| 205 |
BOOL messfree (char *cp) |
| 206 |
|
| 207 |
is a macro which does the equivalent of free() on cp if non-null, |
| 208 |
followed by setting cp = 0 and returning TRUE, else if cp == 0, |
| 209 |
returns FALSE. If the block pointed to by cp has a finalisation |
| 210 |
routine, it is called just before the memory is deallocated. (See |
| 211 |
below.) |
| 212 |
|
| 213 |
There is a handle-based free store package which allows easy and painless |
| 214 |
de-allocation of complex structures. |
| 215 |
|
| 216 |
|
| 217 |
STORE_HANDLE handleCreate (void) |
| 218 |
|
| 219 |
returns a new store handle, which can be passed to halloc() to |
| 220 |
register store on that handle. |
| 221 |
|
| 222 |
|
| 223 |
char *halloc (int size, STORE_HANDLE handle) |
| 224 |
|
| 225 |
Gets size bytes, as malloc, but registers the memory block on handle, |
| 226 |
The store can be deallocated by calling messfree(), as usual, or by |
| 227 |
calling handleDestroy() with its handle. Handle can be zero, in which |
| 228 |
case this call is identical to calling messfree. |
| 229 |
|
| 230 |
|
| 231 |
void handleDestroy (STORE_HANDLE handle) |
| 232 |
|
| 233 |
deallocates all the store registered on handle, calling all the |
| 234 |
finalisation routines for the blocks, if they exist. The order of |
| 235 |
deallocation is not specified. The handle stucture is also |
| 236 |
deallocated, and handle becomes invalid. |
| 237 |
|
| 238 |
|
| 239 |
void blockSetFinalise(char *block, void (*final)(char *) |
| 240 |
|
| 241 |
Registers a finalisation a finalisation routine on block, which must |
| 242 |
have been allocated by either messalloc or halloc. When the block gets |
| 243 |
freed, either by messfree or handleDestroy, the finalisation routine |
| 244 |
is called with the address of the block, just before it is freed. It |
| 245 |
is acceptable to call blockSetFinalize multiple times on the same |
| 246 |
block, later finalisation routines overwrite earlier ones. Zero is an |
| 247 |
acceptable argument and cancels finalisation. |
| 248 |
|
| 249 |
|
| 250 |
Void handleSetFinalise(STORE_HANDLE handle, |
| 251 |
void (*final)(char *), |
| 252 |
char *arg) |
| 253 |
|
| 254 |
Registers a finalisation routine on a handle, it gets called when |
| 255 |
handleDestroy is called, with argument arg, before any of the blocks |
| 256 |
on the handle are freed, or their finalisation routines are called. |
| 257 |
Again, may be called multiply, and with zero for final. |
| 258 |
|
| 259 |
|
| 260 |
Note that there are analogous stackHandleCreate and arrayHandleCreate |
| 261 |
routines which register the Arrays and Stacks they create on a handle, |
| 262 |
to be automatically destroyed by a call to handleDestroy(). It is |
| 263 |
permissable to mix memory blocks, Arrays, and Stacks on the same |
| 264 |
handle. |
| 265 |
|
| 266 |
|
| 267 |
void handleInfo (STORE_HANDLE handle, int *number, int *size) ; |
| 268 |
|
| 269 |
Will return (in number) the number of memory blocks allocated on a |
| 270 |
handle and (in size) their total size in bytes. Note that |
| 271 |
Arrays and Stacks allocated on a handle do not allocate all their |
| 272 |
memory on the handle, so the information returned by this function on |
| 273 |
handles which have Arrays and Stacks on them may be misleading. |
| 274 |
|
| 275 |
|
| 276 |
STORE_HANDLE handleHandleCreate(STORE_HANDLE parent_handle) ; |
| 277 |
|
| 278 |
ADVANCED USERS ONLY. handleHandleCreate does the same as handleCreate, |
| 279 |
except that the handle returned is itself registered on the handle |
| 280 |
passed as argument. All store allocated on the new handle can be |
| 281 |
freed as usual by calling handleDestroy on it; but in addition if |
| 282 |
parent_handle is freed by a call to handleDestroy, all the store |
| 283 |
registered on the new handle will be freed as a consequence of the freeing |
| 284 |
of the new handle. This facility is useful for grouping a set of data |
| 285 |
structures which may be deallocated either as a group, or as a consequence of |
| 286 |
the deallocation of their parent. Note that so far as calling handleInfo |
| 287 |
on parent_handle is concerned, the new handle represents a single object of |
| 288 |
a few tens of bytes. Any memory held on the new handle is not reported. |
| 289 |
|
| 290 |
|
| 291 |
char *strnew(char *old, STORE_HANDLE handle) |
| 292 |
|
| 293 |
Is a useful utility function, It allocates a suitably sized block on |
| 294 |
the handle, and copies the string pointed to by old into it, reurning |
| 295 |
the address of the copy. |
| 296 |
|
| 297 |
|
| 298 |
void handleAlloc(STORE_HANDLE handle, |
| 299 |
void (*final)(char *), |
| 300 |
int size) |
| 301 |
|
| 302 |
Is deprecated, (it was the original interface) simply calls halloc, |
| 303 |
followed by blockSetFinalize. |
| 304 |
------------------------------------------------------------------- |
| 305 |
|
| 306 |
filsubs |
| 307 |
------- |
| 308 |
|
| 309 |
char *filsetdir (char *s) |
| 310 |
|
| 311 |
Sets *s as the standard root path for all subsequent file commands. |
| 312 |
Used once in ACEDB to set $ACEDB. Returns the old value. If s == 0 |
| 313 |
then doesn't change the path, and returns the current value (the most |
| 314 |
common use of the function). |
| 315 |
|
| 316 |
FILE *filopen (char *name, char *ending, char *spec) |
| 317 |
|
| 318 |
Opens the file with given name. If the name does not start with '/' |
| 319 |
or '.' then the standard root path set in filsetdir() is prepended. |
| 320 |
If ending and *ending then '.' ending is postpended. Calls |
| 321 |
messerror() if the file can not be opened. |
| 322 |
|
| 323 |
FILE *filmail (char *address) |
| 324 |
|
| 325 |
Can be used in place of filopen() to open a file that will be mailed |
| 326 |
to the given address when it is closed (using filclose()). |
| 327 |
|
| 328 |
void filclose (FILE* fil) |
| 329 |
|
| 330 |
Closes a file. Mails it if it was opened with filmail(). Always use |
| 331 |
filclose(), since a file may have been opened using filmail(). |
| 332 |
|
| 333 |
FILE *filqueryopen (char *dirname, char *filname, |
| 334 |
char *ending, char *spec, char *title) ; |
| 335 |
|
| 336 |
This presents an interactive file chooser for the user to open a file. |
| 337 |
The user can always cancel, so you must check the return value. Only |
| 338 |
directories and files with the given "ending" will be presented to the |
| 339 |
user. title is the title of the file chooser window, which can help |
| 340 |
suggest what the file is for. dirname and filname should either be 0, |
| 341 |
in which case defaults are used, or static character arrays of 48 and |
| 342 |
24 characters respectively. In this case, the directory and name of |
| 343 |
the opened file will be left in the arrays, first allowing the |
| 344 |
programmer to get at the file names, and second establishing a default |
| 345 |
for the next time that the function is called. If spec is "w" one of |
| 346 |
the options from the chooser is allow the user to specify a mail |
| 347 |
address and open the file using filmail(), so you must be sure to |
| 348 |
close files opened by filqueryopen() with filclose(). |
| 349 |
|
| 350 |
BOOL filexists (char *name, char *ending) |
| 351 |
|
| 352 |
Tests for existence of a file, using the same rules for root and |
| 353 |
ending as filopen(). |
| 354 |
|
| 355 |
BOOL filremove (char *name, char *ending) |
| 356 |
|
| 357 |
Deletes a file from the operating system, using the same rules for |
| 358 |
root and ending as filopen(). |
| 359 |
|
| 360 |
FILE *filtmpopen (char **nameptr, char *spec) |
| 361 |
BOOL filtmpremove (char *name) |
| 362 |
void filtmpcleanup (void) |
| 363 |
|
| 364 |
filtmpopen() opens a temporary file. The name is returned to you in |
| 365 |
*nameptr. You can reopen the same file by leaving *nameptr and |
| 366 |
setting spec to "r". Use filtmpremove(*nameptr) to delete the file |
| 367 |
when finished. This will free memory for the file name -- DO NOT |
| 368 |
messfree (*nameptr). If you do not free explicitly filtmpremove() the |
| 369 |
temporary file, it will be removed automatically at the end of your |
| 370 |
ACEDB session (by filtmpcleanup()), unless of course you crash. |
| 371 |
|
| 372 |
<PRE> |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
</BODY> |
| 377 |
</HTML> |