home *** CD-ROM | disk | FTP | other *** search
- /* H.Utils: general utilities (header file) */
-
- #ifndef __utils_h
- #define __utils_h
-
- /* Type definitions required for function prototypes */
-
- #ifndef __stdio_h
- /* If we've done <stdio.h> we've got everything we need already */
-
- /* Temporary #define - undone at the end */
- #define FILE struct __FILE_struct
-
- /* Set up size_t (as defined in standard headers) */
- #ifndef __size_t
- #define __size_t 1
- typedef unsigned int size_t; /* from <stddef.h> */
- #endif
-
- #endif
-
- /* ARM System time format */
-
- #ifndef __TIME_h
- #define __TIME_h
- typedef struct
- {
- unsigned char t[5]; /* Low byte first - ie. t[0] is low */
- }
- TIME;
- #endif
-
- /* Standard external function definitions */
-
- /* Error message handling */
- extern void panic (const char *format, ...);
- extern void message (const char *format, ...);
-
- /* Extra string handling */
- extern char *strdup (const char *str);
- extern char *strndup (const char *str, int n);
- extern char *strupper (char *str);
- extern char *strlower (char *str);
- extern char *strpcpy (char *s, const char *t);
- extern char *strnpcpy (char *s, const char *t, int n);
- extern int strlcmp (const char *s, const char *t);
- extern int strnlcmp (const char *s, const char *t, int n);
- extern int strucmp (const char *s, const char *t);
- extern int strnucmp (const char *s, const char *t, int n);
- extern int strcchr (const char *s, char c);
-
- /* Automatically reclaimed memory allocation */
- extern void *alloca (size_t size);
-
- /* Safe memory allocation */
- extern void *emalloc (size_t size);
- extern void *erealloc (void *ptr, size_t size);
- extern void *ecalloc (size_t count, size_t size);
-
- /* Safe file opening */
- extern FILE *efopen (const char *file, const char *mode);
- extern FILE *efreopen (const char *file, const char *mode, FILE *fp);
-
- /* Robust file copy (even across file systems */
- extern int frename (const char *old, const char *new);
-
- /* File pointer information */
- extern int isatty (FILE *fp);
-
- /* File system manipulation */
- extern int chdir (const char *dir);
- extern int touch (const char *file);
- extern char *getcwd (int preserve_pwd, char **pwd_ptr);
- #define getwd() getcwd (1, 0)
-
- /* Temporary file handling */
- extern char *temp_dir;
- extern char *mktemp (const char *file);
-
- /* Simulation of pipes */
- extern FILE *popen (char *command, char *mode);
- extern int pclose (FILE *fd);
-
- /* Random number generator */
- void srandom (unsigned seed);
- long random (void);
- char *initstate (unsigned seed, char *arg_state, int n);
- char *setstate (char *arg_state);
-
- /* Directory scan for wildcards */
- extern char *dirscan (const char *file);
-
- /* File information */
- extern int filetype (const char *file);
- extern TIME filetime (const char *file);
- extern unsigned filelen (const char *file);
-
- /* Results from filetype() */
- #define F_NONE (-1)
- #define F_DIR (-2)
- #define F_UNSTAMPED (-3)
- #define F_ERROR (-4)
-
- /* Debugging utilities */
- extern char *caller (int);
-
- /* Linker areas (names translated for C use) */
- extern void *_RO_Base;
- extern void *_RO_Limit;
- extern void *_RW_Base;
- extern void *_RW_Limit;
- extern void *_ZI_Base;
- extern void *_ZI_Limit;
-
- /* The following is basically a functional version of _RO_Limit. It is
- * retained for compatibility with older versions of this library only.
- */
- extern void *ProgLimit (void);
-
- /* Unexec for the Archimedes.
- *
- * Used as unexec(filename), which dumps a full core image of the
- * current program to file filename. This image is a proper unsqueezed
- * Archimedes application. You can squeeze the resulting executable just
- * like any other program.
- */
-
- extern void unexec (char *);
-
- /* Coroutines
- *
- * ----------------------------------------------------------------------
- *
- * Based on an article by David McQuillan in Archive, Vol 4 No 6.
- * The following notice is from the original article.
- *
- * No responsibility accepted if you come a cropper using the code.
- * In particular, I have not tried stack extension and interrupts with it.
- *
- * (c) put in Public Domain by David McQuillan, Jan 1991.
- *
- * ----------------------------------------------------------------------
- *
- * The coroutine mechanism must be initialised by calling co_initialise()
- * in the main program, passing it the address of a coroutine_t variable
- * in which the root coroutine data is stored. Coroutines are then created
- * by calling co_create, passing the address of the coroutine variable which
- * is to hold the coroutine data, the initial stack size, and the procedure
- * (a co_procedure_t) to be executed when the coroutine is started.
- *
- * Control is transferred to a coroutine via the co_resume() procedure.
- * The first parameter is the address of the coroutine to resume, and the
- * second parameter is a value to be passed to the coroutine. On the first
- * entry to the coroutine, this parameter is passed as the parameter to the
- * coroutine start procedure. On second and subsequent entries to the
- * coroutine, the parameter is returned as the result of the co_resume()
- * call which transferred control out of the coroutine.
- *
- * Note that a coroutine main procedure should never return. When it is
- * finished, it should co_resume() the main procedure. If a coroutine does
- * return, the coroutine package signals an error and terminates the program.
- *
- * When a coroutine is finished with, it can be deleted using co_delete().
- * This frees up the memory allocated to the coroutine stack and register set.
- */
-
- /* Coroutine type. The entries are
- *
- * env - a pointer to a jmp_buf holding the coroutine's register set.
- * The extra indirection is used to avoid the need to know the
- * size of a jmp_buf (which would require us to include
- * <setjmp.h>).
- * stack - the coroutine stack. The stack_chunk structure is declared
- * in "kernel.h".
- */
- typedef struct
- {
- void *env; /* Really (jmp_buf *) */
- struct stack_chunk *stack;
- }
- coroutine_t;
-
- /* Coroutine main procedure type */
- typedef void co_procedure_t (void *);
-
- /* Function declarations */
- void co_initialise (coroutine_t *);
- void co_create (coroutine_t *, size_t, co_procedure_t *);
- void co_delete (coroutine_t *);
- void *co_resume (coroutine_t *, void *);
-
- /* Copy data to the heap. This should only be used for l-values (including
- * array names).
- *
- * For example, with the following declarations:
- *
- * void *heap_ptr;
- * char a[100];
- * time_t t;
- *
- * Use:
- *
- * heap_ptr = VarToHeap(a); -- copies the whole array (100 bytes).
- * heap_ptr = VarToHeap(t); -- copies t, even if t is a structure type.
- *
- */
-
- #define VarToHeap(var) ( memcpy( emalloc(sizeof(var)), &(var), sizeof(var)) )
-
- /* Machine dependent data types */
-
- typedef unsigned char byte; /* 8-bit byte */
- typedef unsigned short halfword; /* 16-bit halfword */
- typedef unsigned int word; /* 32-bit word */
-
- /* Number of elements in array A */
-
- #define DIM(a) ( sizeof(a) / sizeof(*(a)) )
-
- /* Maximum and minimum */
-
- #define MAX(a,b) ( (a) > (b) ? (a) : (b) )
- #define MIN(a,b) ( (a) < (b) ? (a) : (b) )
-
- /* Scope control pseudo-keywords */
-
- #define public
- #define private static
-
- /* Flag a given variable (function argument) as used.
- *
- * Notes: No code is compiled (isn't optimisation wonderful).
- * Side effects in "var" are not evaluated.
- * Var must be an l-value.
- */
-
- #define USE(var) (void)( 0 ? (var)=(var) : 0 )
-
- /* Now undo the temporary definition of FILE */
-
- #ifndef __stdio_h
- #undef FILE
- #endif
-
- #endif
-