io.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. In and output, error messages, etc.
  3. */
  4. /* $Id$ */
  5. #include <stdio.h>
  6. #if __STDC__
  7. #include <stdarg.h>
  8. extern fatal(char *, ...);
  9. #else
  10. #include <varargs.h>
  11. #endif
  12. #include "logging.h"
  13. #include "global.h"
  14. #include "mem.h"
  15. #include "linfil.h"
  16. extern int running; /* from main.c */
  17. extern char *prog_name; /* from main.c */
  18. extern char *load_name; /* from init.c */
  19. /******** The message file ********/
  20. extern char mess_file[64]; /* from main.c */
  21. long mess_id; /* Id, to determine unique mess file */
  22. FILE *mess_fp; /* Filepointer of message file */
  23. PRIVATE do_fatal();
  24. incr_mess_id()
  25. { /* for a new child */
  26. mess_id++;
  27. }
  28. #ifdef LOGGING
  29. extern long inr; /* from log.c */
  30. #endif /* LOGGING */
  31. /******** General file handling ********/
  32. PRIVATE int highestfd();
  33. int fd_limit = 100; /* first non-available file descriptor */
  34. FILE *fcreat_high(fn)
  35. char *fn;
  36. {
  37. /* Creates an unbuffered FILE with name fn on the highest
  38. possible file descriptor.
  39. */
  40. register int fd;
  41. register FILE *fp;
  42. if ((fd = creat(fn, 0644)) == -1)
  43. return NULL;
  44. fd = highestfd(fd);
  45. if ((fp = fdopen(fd, "w")) == NULL)
  46. return NULL;
  47. setbuf(fp, (char *) 0); /* unbuffered! */
  48. fd_limit = fd;
  49. return fp;
  50. }
  51. PRIVATE int highestfd(fd)
  52. int fd;
  53. {
  54. /* Moves the (open) file descriptor fd to the highest available
  55. position and returns the new fd. Does this without knowing
  56. how many fd-s are available.
  57. */
  58. register int newfd, higherfd;
  59. /* try to get a better fd */
  60. newfd = dup(fd);
  61. if (newfd < 0) {
  62. return fd;
  63. }
  64. if (newfd > 99) {
  65. /* for systems with an unlimited supply of file descriptors */
  66. close(newfd);
  67. return fd;
  68. }
  69. /* occupying the new fd, try to do even better */
  70. higherfd = highestfd(newfd);
  71. close(fd);
  72. return higherfd; /* this is a deep one */
  73. }
  74. init_ofiles(firsttime)
  75. int firsttime;
  76. {
  77. if (!firsttime) {
  78. fclose(mess_fp); /* old message file */
  79. mess_fp = 0;
  80. sprintf(mess_file, "%s_%ld", mess_file, mess_id);
  81. }
  82. /* Create messagefile */
  83. if ((mess_fp = fcreat_high(mess_file)) == NULL)
  84. fatal("Cannot create messagefile '%s'", mess_file);
  85. init_wmsg();
  86. mess_id = 1; /* ID of next child */
  87. #ifdef LOGGING
  88. open_log(firsttime);
  89. #endif /* LOGGING */
  90. }
  91. #if __STDC__
  92. /*VARARGS0*/
  93. fatal(char *fmt, ...)
  94. {
  95. va_list ap;
  96. fprintf(stderr, "%s: ", prog_name);
  97. va_start(ap, fmt);
  98. {
  99. do_fatal(stderr, fmt, ap);
  100. }
  101. va_end(ap);
  102. if (mess_fp) {
  103. va_start(ap, fmt);
  104. {
  105. do_fatal(mess_fp, fmt, ap);
  106. }
  107. va_end(ap);
  108. }
  109. if (running)
  110. core_dump();
  111. close_down(1);
  112. }
  113. #else
  114. /*VARARGS0*/
  115. fatal(va_alist)
  116. va_dcl
  117. {
  118. va_list ap;
  119. fprintf(stderr, "%s: ", prog_name);
  120. va_start(ap);
  121. {
  122. register char *fmt = va_arg(ap, char *);
  123. do_fatal(stderr, fmt, ap);
  124. }
  125. va_end(ap);
  126. if (mess_fp) {
  127. va_start(ap);
  128. {
  129. register char *fmt = va_arg(ap, char *);
  130. do_fatal(mess_fp, fmt, ap);
  131. }
  132. va_end(ap);
  133. }
  134. if (running)
  135. core_dump();
  136. close_down(1);
  137. }
  138. #endif
  139. close_down(rc)
  140. int rc;
  141. {
  142. /* all exits should go through here */
  143. if (mess_fp) {
  144. fclose(mess_fp);
  145. mess_fp = 0;
  146. }
  147. #ifdef LOGGING
  148. close_log();
  149. #endif /* LOGGING */
  150. exit(rc);
  151. }
  152. PRIVATE do_fatal(fp, fmt, ap)
  153. FILE *fp;
  154. char *fmt;
  155. va_list ap;
  156. {
  157. fprintf(fp, "(Fatal error) ");
  158. if (load_name)
  159. fprintf(fp, "%s: ", load_name);
  160. vfprintf(fp, fmt, ap);
  161. fputc('\n', fp);
  162. }
  163. #if __STDC__
  164. /*VARARGS0*/
  165. message(char *fmt, ...)
  166. {
  167. va_list ap;
  168. fprintf(mess_fp, "(Message): ");
  169. va_start(ap, fmt);
  170. {
  171. vfprintf(mess_fp, fmt, ap);
  172. }
  173. va_end(ap);
  174. fprintf(mess_fp, " at %s\n", position());
  175. }
  176. #else
  177. /*VARARGS0*/
  178. message(va_alist)
  179. va_dcl
  180. {
  181. va_list ap;
  182. fprintf(mess_fp, "(Message): ");
  183. va_start(ap);
  184. {
  185. register char *fmt = va_arg(ap, char *);
  186. vfprintf(mess_fp, fmt, ap);
  187. }
  188. va_end(ap);
  189. fprintf(mess_fp, " at %s\n", position());
  190. }
  191. #endif
  192. char *position() /* transient */
  193. {
  194. static char buff[300];
  195. register char *fn = dt_fname(getFIL());
  196. #ifdef LOGGING
  197. sprintf(buff, "\"%s\", line %ld, INR = %ld", fn, getLIN(), inr);
  198. #else /* LOGGING */
  199. sprintf(buff, "\"%s\", line %ld", fn, getLIN());
  200. #endif /* LOGGING */
  201. return buff;
  202. }
  203. char *dt_fname(p)
  204. ptr p;
  205. {
  206. return (p ? &data_loc(p) : "<unknown>");
  207. }