sys.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /* sys.h Copyright (C) 1992-2002 Codemist Ltd */
  2. /*
  3. * This file should contain a list of all the functions in CSL that have
  4. * to be provided on a per-host basis.
  5. */
  6. /*
  7. * This code may be used and modified, and redistributed in binary
  8. * or source form, subject to the "CCL Public License", which should
  9. * accompany it. This license is a variant on the BSD license, and thus
  10. * permits use of code derived from this in either open and commercial
  11. * projects: but it does require that updates to this code be made
  12. * available back to the originators of the package.
  13. * Before merging other code in with this or linking this code
  14. * with other packages or libraries please check that the license terms
  15. * of the other material are compatible with those of this.
  16. */
  17. /* Signature: 5a249156 08-Apr-2002 */
  18. #ifndef header_sys_h
  19. #define header_sys_h 1
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*
  24. * I provide a hook so that memory allocation can be passed not to the
  25. * usual C library malloc() function but to some other system-specific code.
  26. * This is achieved by putting the actual function that is to be called in
  27. * a variable. Ditto for free(). I have static initialisation that causes
  28. * use of the ordinary C library and so anybody who needs an alternative
  29. * must reset the pointers VERY early on.
  30. */
  31. typedef void *malloc_function(size_t);
  32. extern malloc_function *malloc_hook;
  33. typedef void *realloc_function(void *, size_t);
  34. extern realloc_function *realloc_hook;
  35. typedef void free_function(void *);
  36. extern free_function *free_hook;
  37. /*
  38. * find_image_directory is handed the information that main() sees when
  39. * the application is started up, and it returns a string (in freshly
  40. * malloc'd space) that is the default name for the checkpoint image file
  41. * to be used for this run.
  42. */
  43. extern char *find_image_directory(int argc, char *argv[]);
  44. /*
  45. * open_file accepts a vector of characters (old) and looks at the first
  46. * (n) of them. This is taken as a CSL filename - it is subject (possibly)
  47. * to adjustments, the expanded name is copied to the array (filename) and an
  48. * attempt is made to open a file. (mode) is a string (like "r", "w" or "r+b")
  49. * suitable for handing to the C "fopen" function to specify a mode, and
  50. * if (old_file) is non-NULL we should do an freopen() rather than fopen().
  51. * The main intent in filename conversion as supported here is to allow
  52. * users on all machines to use Unix-like file-names to at least some
  53. * minimal extent, so names like "subdir/file.lsp" should be allowed, even
  54. * if in native mode the computer used uses some quite different way of
  55. * specifying sub-directories and file-extensions.
  56. */
  57. extern FILE *open_file(char *filename, char *old, size_t n,
  58. char *mode, FILE *old_file);
  59. /*
  60. * find if a file exists, and if it does return (as 24 chars) the
  61. * change time for it. See open_file re args.
  62. */
  63. extern CSLbool file_exists(char *filename, char *old, size_t n, char *tt);
  64. /*
  65. * The interfaces to create_directory and delete_file are similar
  66. * to that for open_file. They do what their names suggest! They return
  67. * zero on success, and non-zero on failure. Each does file-name
  68. * conversion so that Unix-style names can be used even with Windows.
  69. */
  70. extern int create_directory(char *filename, char *old, size_t n);
  71. extern int delete_file(char *filename, char *old, size_t n);
  72. extern int rename_file(char *from_name, char *from_old, size_t from_size,
  73. char *to_name, char *to_old, size_t to_size);
  74. /*
  75. * The interfaces to file_readable and file_writable are also similar
  76. * to that for open_file. They return 1 if their argument can be opened
  77. * for reading or writing respectively, and 0 otherwise. directoryp tests
  78. * whether its argument is a directory.
  79. */
  80. extern int file_readable(char *filename, char *old, size_t n);
  81. extern int file_writeable(char *filename, char *old, size_t n);
  82. extern int directoryp(char *filename, char *old, size_t n);
  83. /*
  84. * file_length returns the length of its argument (a file) in bytes.
  85. */
  86. extern long file_length(char *filename, char *old, size_t n);
  87. /*
  88. * current_directory() places the name of the current directory in the buffer
  89. * which has the indicated size, and returns 0 for failure or otherwise
  90. * the length of data written.
  91. */
  92. extern int current_directory(char *name, int len);
  93. /*
  94. * The next three are much-like the same... On some operating systems
  95. * they will be pretty meaningless!
  96. */
  97. extern int get_current_directory(char *name, int len);
  98. extern int get_home_directory(char *name, int len);
  99. extern int get_users_home_directory(char *name, int len);
  100. extern int change_directory(char *filename, char *old, size_t n);
  101. /*
  102. * get_truename attempts to get a canonical name for a file or directory.
  103. * It returns a string. If unable to do anything useful it can just
  104. * return a copy of its input, but the result is expected to be a
  105. * freshly allocated block of memory and should be handed to free() after
  106. * it has been used. This interface using malloc() should be changed at
  107. * some stage so that the caller passes down a buffer for the result to
  108. * be placed in (what is arg1 for anyway).
  109. * This comment also needs to be expanded to explain in a little more detail
  110. * what a "canonical" name for a file or directory is - eg whether the
  111. * intent is to allow wildcard inputs or whether this function is
  112. * expected to convert from a relative file-name into a fully rooted one.
  113. */
  114. extern char *get_truename(char *filename, char *old, size_t n);
  115. #ifdef NAG_VERSION
  116. /*
  117. * list_directory_members allocates (using malloc) both a vector
  118. * (of type char **) and a load of strings to go in there, and updates
  119. * filelist to point at it. The caller must free() the space at some
  120. * later stage.
  121. */
  122. extern int list_directory_members(char *filename, char *old, char **filelist[],
  123. size_t n);
  124. #else
  125. /*
  126. * list_directory_members calls the given callback function handing it
  127. * the name of each file in given directory.
  128. */
  129. typedef void directory_callback(char *, int, long int);
  130. extern void list_directory_members(char *filename, char *old,
  131. size_t n, directory_callback *fn);
  132. #endif
  133. /*
  134. * (f) is an open file - truncate it at position (where).
  135. */
  136. extern int truncate_file(FILE *f, long int where);
  137. /*
  138. * If I am to process directories I need a set of routines that will
  139. * scan sub-directories for me. The specification I want is:
  140. * int scan_directory(char *dir,
  141. * void (*proc)(char *name, int why, int32 size));
  142. *
  143. * This is called with a file- or directory-name as its first argument
  144. * and a function as its second.
  145. * It calls the function for every directory and every file that can be found
  146. * rooted from the given place. If the file to scan is specified as NULL
  147. * the current directory is processed.
  148. * When a simple file is found the procedure is called with the name of the
  149. * file, why=0, and the length (in bytes) of the file. For a directory
  150. * the function is called with why=1, then the contents of the directory are
  151. * processed. For directories the size information will be 0. There is no
  152. * guarantee of useful behaviour if some of the files to be scanned are
  153. * flagged as "invisible" or "not readable" or if they are otherwise special.
  154. * The value returned is the number of characters that should be removed
  155. * the start of file-names returned to get rid of any initial directory
  156. * specified. If dir is passed as NULL this will be zero and names will
  157. * come back plain, otherwise it will be 1+strlen(dir)
  158. */
  159. #define SCAN_FILE 0
  160. #define SCAN_STARTDIR 1
  161. #define SCAN_ENDDIR 2
  162. extern void scan_directory(char *dir,
  163. void (*proc)(char *name, int why, long int size));
  164. /*
  165. * When scan_directory calls the procedure it has been passed, it will have
  166. * set scan_leafstart to the offset in the passed filename where the
  167. * original directory ended and the new information starts. Thus if the
  168. * input string was (say) "/usr/users/acn/xxx" and some particular sub-file
  169. * was reported as "/usr/users/acn/xxx/subdir/subfile.ext" then
  170. * (name+scan_leafstart) gives the relative name "subdir/subfile.ext".
  171. */
  172. extern int scan_leafstart;
  173. /*
  174. * scan_files() is just like scan_directory() excepr that it does not
  175. * recurse into sub-directories.
  176. */
  177. extern void scan_files(char *dir,
  178. void (*proc)(char *name, int why, long int size));
  179. extern void unpack_date(unsigned long int r,
  180. int *year, int *mon, int *day,
  181. int *hour, int *min, int *sec);
  182. extern unsigned long int pack_date(int year, int mon, int day,
  183. int hour, int min, int sec);
  184. typedef struct date_and_type
  185. {
  186. unsigned long int date;
  187. unsigned long int type;
  188. } date_and_type;
  189. /* Reinstate date and filetype... */
  190. extern void set_filedate(char *name, unsigned long int datestamp,
  191. unsigned long int ftype);
  192. extern void put_fileinfo(date_and_type *p, char *name);
  193. /*
  194. * my_getenv() is much like the ANSI getenv(), but exists because
  195. * it may be useful to perform mappings on the character string given
  196. * (e.g. to fold case) before calling the built-in getenv().
  197. */
  198. extern char *my_getenv(char *s);
  199. /*
  200. * my_system is just like the ANSI function system() - done this way to
  201. * allow for machines where this is not available or where more work is
  202. * needed.
  203. */
  204. extern int my_system(char *s);
  205. #ifdef PIPES
  206. /*
  207. * my_popen() and my_pclose() are intended to be just like the Unix
  208. * popen() and pclose functions.
  209. */
  210. extern FILE *my_popen(char *command_name, char *direction);
  211. extern void my_pclose(FILE *stream);
  212. #endif
  213. #ifdef SIMULATED_PIPES
  214. /*
  215. * For RISCOS (at least) I send characters to a pipe (possibly
  216. * only used to support the gnuplot package) through a separate
  217. * special function as documented here.
  218. */
  219. extern int my_pipe_putc(int c, FILE *f);
  220. extern int my_pipe_flush(FILE *f);
  221. #else
  222. # define my_pipe_putc(c, f) putc(c, f)
  223. # define my_pipe_flush(f) fflush(f)
  224. #endif
  225. #ifdef PIPES_SOMETIMES
  226. /*
  227. * If I have an operating system where the presence of pipes is
  228. * conditional, this variable is used to keep track.
  229. */
  230. extern int pipes_today;
  231. #endif
  232. /*
  233. * batchp() should return true if stdin is NOT from an interactive
  234. * terminal.
  235. */
  236. extern int batchp(void);
  237. #ifdef UNIX_TIMES
  238. /*
  239. * The intent here is that if UNIX_TIMES is set then the CPU times
  240. * reported to the user will be "user time" and will not include
  241. * "system time", as in using the Unix "times" facility. If UNIX_TIMES
  242. * is not defined no special code in sysxxx.c is needed.
  243. */
  244. extern clock_t read_clock(void);
  245. #else
  246. # define read_clock() clock()
  247. #endif
  248. #ifdef SHOW_COUNTS_AVAILABLE
  249. /*
  250. * show_counts() can be called via mapstore(), and would be expected to
  251. * display some information relating to how many times each chunk of
  252. * C code has been executed - possibly only when the C coded parts of CSL have
  253. * been compiled with some special profile option. write_profile is
  254. * similar in style, but sends output to a file. If you do not have
  255. * statistics gathering facilities you do not need these implemented.
  256. */
  257. extern void show_counts(void);
  258. extern void write_profile(char *filename);
  259. #endif
  260. /*
  261. * Imultiply and Idivide are things that you may want to re-implement in
  262. * machine code - if so here are their signatures, and you should #define
  263. * IMULTIPLY/IDIVIDE in machine.h to say what you have done. See arithXX.c
  264. * for the portable versions.
  265. */
  266. #ifdef _MSC_VER
  267. /*
  268. * Apologies here: For Microsoft VC++ I need to define these as __stdcall
  269. * (and certainly NOT as __fastcall) because Microsoft potentially change
  270. * which registers are used with __fastcall from release to release of
  271. * their compiler.
  272. */
  273. #ifdef IMULTIPLY
  274. extern unsigned32 __stdcall Imultiply(unsigned32 *rlow, unsigned32 a,
  275. unsigned32 b, unsigned32 c);
  276. #endif
  277. #ifdef IDIVIDE
  278. extern unsigned32 __stdcall Idivide(unsigned32 *qp, unsigned32 a,
  279. unsigned32 b, unsigned32 c);
  280. extern unsigned32 __stdcall Idiv10_9(unsigned32 *qp, unsigned32 a, unsigned32 b);
  281. #endif
  282. #else
  283. #ifdef IMULTIPLY
  284. extern unsigned32 Imultiply(unsigned32 *rlow, unsigned32 a,
  285. unsigned32 b, unsigned32 c);
  286. #endif
  287. #ifdef IDIVIDE
  288. extern unsigned32 Idivide(unsigned32 *qp, unsigned32 a,
  289. unsigned32 b, unsigned32 c);
  290. extern unsigned32 Idiv10_9(unsigned32 *qp, unsigned32 a, unsigned32 b);
  291. #endif
  292. #endif
  293. #ifdef TICK_STREAM
  294. /*
  295. * add_ticker() starts a steady(-ish) stream of clock pulses going,
  296. * and causes (indirectly) accept_tick() to be called in a regular basis.
  297. * These are used to help me with interfaces to window systems, and to
  298. * make responses to keyboard interrupts more friendly and reliable.
  299. */
  300. extern void add_ticker(void);
  301. /*
  302. * remove_ticker() switches tick-events off. Best if it is legal to
  303. * call remove_ticker even if ticks are not active.
  304. */
  305. extern void MS_CDECL remove_ticker(void);
  306. extern void accept_tick(void);
  307. #endif
  308. #ifdef POLL_FOR_ATTN
  309. /*
  310. * Used by MSDOS system to check if ^C has been pressed - see gc.c
  311. * and read.c for the only calls. Arg decides if it waits until some
  312. * input is ready.
  313. */
  314. extern void poll_for_attn(void);
  315. #endif
  316. /*
  317. * When the garbage collector observes that memory is tight it can attempt
  318. * to allocate more by going back to malloc(). This function is called with
  319. * an argument that indicates how many pages are in use at present, and it
  320. * is expected to return an indication of how many more might be worth
  321. * allocating. See the code in gc.c for details of usage.
  322. */
  323. int32 ok_to_grab_memory(int32 current_pages);
  324. #ifdef WINDOW_SYSTEM
  325. /*
  326. * start_up_window_manager is called after command-line options have been
  327. * decoded - and hence after any flags that might influence the character
  328. * of windowed use of the system have been set.
  329. */
  330. extern void start_up_window_manager(int use_wimp);
  331. /*
  332. * The next represents the only way in which characters will be
  333. * sent to the screen - in the case of WINDOW_SYSTEM it write to the
  334. * screen. Possibly this is more complicated than just writing to stdout.
  335. */
  336. extern void putc_stdout(int c);
  337. /*
  338. * flush_screen() tries to make sure that the display is up to date.
  339. */
  340. extern void flush_screen(void);
  341. /*
  342. * I may want to redirect stdout to a file, in which case the following
  343. * is the handle that I will use. See "--" decoding in csl.c
  344. */
  345. extern FILE *alternative_stdout;
  346. /*
  347. * Ditto reading from stdin. Reads chars into buffer, returns count.
  348. */
  349. extern int wimpget(char *buf);
  350. /*
  351. * The next two call-backs tell the window manager how much time has
  352. * been spent and how many garbage collections have been done.
  353. */
  354. extern void report_time(int32 t, int32 gct);
  355. extern void report_space(int gccount, double percent);
  356. /*
  357. * pause_for_user() gets called right at the end to give a chance for the
  358. * system to delay before closing the main output window.
  359. */
  360. extern void pause_for_user(void);
  361. #endif
  362. #ifdef WINDOWS_NT
  363. /*
  364. * Am I in Win32S rather than full Win32?
  365. * The values I will use will be
  366. * 0 Windows NT
  367. * 1 win32s on top of Windows 3.x
  368. * 2 Windows 95
  369. */
  370. extern int win32s;
  371. #endif
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif /* header_sys_h */
  376. /* end of sys.h */