home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD2.bin / bbs / comm / amitcp-3.0ß2.lha / AmiTCP / src / amitcp / kern / subr_prf.c < prev    next >
C/C++ Source or Header  |  1994-02-16  |  18KB  |  697 lines

  1. RCS_ID_C="$Id: subr_prf.c,v 1.33 1994/02/16 06:07:33 jraja Exp $";
  2. /*
  3.  * Copyright (c) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>,
  4.  *                    Helsinki University of Technology, Finland.
  5.  *                    All rights reserved.
  6.  *
  7.  * HISTORY
  8.  * $Log: subr_prf.c,v $
  9.  * Revision 1.33  1994/02/16  06:07:33  jraja
  10.  * Replaced RawDoFmt() calls with vcsprintf(), which now does the formatting
  11.  * itself. Changed stuffchar() to cs_putchar(), added csprintn() to print
  12.  * out numbers.
  13.  * Changed vlog() to return the number of characters written, changed
  14.  * printf() to use vlog() (wheir code was identical).
  15.  *
  16.  * Revision 1.32  1994/01/05  10:27:30  jraja
  17.  * Added IntuitionBase opening and closing to the panic().
  18.  * Note: using a local library base variable might not work in GCC?
  19.  * 
  20.  * Revision 1.31  1993/12/21  22:06:48  jraja
  21.  * Changed implicit structure member (s_sec) to explicit (tv_secs) on timeval.
  22.  *
  23.  * Revision 1.30  1993/11/06  23:51:22  ppessi
  24.  * Added csprintf(), sprintf function using CSource.
  25.  * Added vsprintf() and vcsprintf() as well.
  26.  *
  27.  * Revision 1.29  1993/10/07  22:41:34  ppessi
  28.  * Added time to the log message.
  29.  *
  30.  * Revision 1.28  1993/05/16  00:11:12  jraja
  31.  * Removed redundant volatile keyword.
  32.  *
  33.  * Revision 1.27  93/05/14  15:54:17  15:54:17  ppessi (Pekka Pessi)
  34.  * Minor prototype fixes.
  35.  * 
  36.  * Revision 1.26  93/05/05  16:10:15  16:10:15  puhuri (Markus Peuhkuri)
  37.  * Fixes for final demo.
  38.  * 
  39.  * Revision 1.25  93/05/04  12:40:07  12:40:07  puhuri (Markus Peuhkuri)
  40.  * Fixed PANICBUFFER (SASC didnt like implicit alloca())
  41.  * Now log takes care of keeping level on right range.
  42.  * 
  43.  * Revision 1.24  93/04/29  22:02:44  22:02:44  puhuri (Markus Peuhkuri)
  44.  * fixed variable names to use configuration structure.
  45.  * 
  46.  * Revision 1.23  93/04/28  12:59:05  12:59:05  puhuri (Markus Peuhkuri)
  47.  * Fixed stdargs.h to stdarg.h
  48.  * 
  49.  * Revision 1.22  93/04/27  10:23:19  10:23:19  puhuri (Markus Peuhkuri)
  50.  * Add vlog-function for calling log from API. log() calls now vlog().
  51.  * 
  52.  * Revision 1.21  93/04/26  18:55:08  18:55:08  puhuri (Markus Peuhkuri)
  53.  * Moved contents of closelog to log_deinit() and removed closelog.
  54.  * 
  55.  * Revision 1.20  93/04/26  11:54:46  11:54:46  too (Tomi Ollila)
  56.  * Changed include paths of amiga_api.h, amiga_libcallentry.h and amiga_raf.h
  57.  * from kern to api
  58.  * 
  59.  * Revision 1.19  93/04/23  02:28:38  02:28:38  ppessi (Pekka Pessi)
  60.  * Number and length of logging messages made configureable.
  61.  * 
  62.  * Revision 1.18  93/04/21  19:08:46  19:08:46  puhuri (Markus Peuhkuri)
  63.  * Now checks if process run into panic() was AmiTCP.
  64.  * Now uses new structure to log_msg. (Printing of priority is in
  65.  * NETTRACE)
  66.  * 
  67.  * Revision 1.17  93/04/06  15:15:55  15:15:55  jraja (Jarno Tapio Rajahalme)
  68.  * Changed spl function return value storage to spl_t,
  69.  * changed bcopys and bzeros to aligned and/or const when possible,
  70.  * added inclusion of conf.h to every .c file.
  71.  * 
  72.  * Revision 1.16  93/03/20  07:06:38  07:06:38  ppessi (Pekka Pessi)
  73.  * Fixed memory leak caused by task_remove()
  74.  * 
  75.  * Revision 1.15  93/03/19  14:14:54  14:14:54  too (Tomi Ollila)
  76.  * Code changes at night 17-18 March 1993
  77.  * 
  78.  * Revision 1.14  93/03/15  09:12:42  09:12:42  jraja (Jarno Tapio Rajahalme)
  79.  * Checkin' mods made by ppessi.
  80.  * 
  81.  * Revision 1.13  93/03/13  00:05:31  00:05:31  ppessi (Pekka Pessi)
  82.  * Quick'n'dirty fix for PANICBUFFER; some prototypes fixed. 
  83.  * 
  84.  * Revision 1.12  93/03/10  17:14:12  17:14:12  puhuri (Markus Peuhkuri)
  85.  * Fixed documentation. Now (s)printf returns number of printed chars.
  86.  * 
  87.  * Revision 1.11  93/03/09  13:33:11  13:33:11  puhuri (Markus Peuhkuri)
  88.  * See messages for amiga_log.c/1.10.
  89.  * Now uses new function to get log message.
  90.  * Fixed a bug in closing of log (caused a read from low memory, 0x14)
  91.  * 
  92.  * Revision 1.10  93/03/05  21:11:17  21:11:17  jraja (Jarno Tapio Rajahalme)
  93.  * Fixed includes (again).
  94.  * 
  95.  * Revision 1.9  93/03/05  12:32:33  12:32:33  jraja (Jarno Tapio Rajahalme)
  96.  * Removed #include <kern/amiga_api_protos.h>.
  97.  * 
  98.  * Revision 1.8  93/03/05  03:26:17  03:26:17  ppessi (Pekka Pessi)
  99.  * Compiles with SASC. Initial test version.
  100.  * 
  101.  * Revision 1.7  93/03/02  18:25:12  18:25:12  puhuri (Markus Peuhkuri)
  102.  * Add sprintf() and fixed documentation
  103.  * 
  104.  * Revision 1.6  93/03/02  16:10:35  16:10:35  puhuri (Markus Peuhkuri)
  105.  * Removed amiga.lib functions, fixed memory managment
  106.  * 
  107.  * Revision 1.5  93/02/26  19:44:35  19:44:35  puhuri (Markus Peuhkuri)
  108.  * Modified to use MsgPort as list of free messages,
  109.  * made log() and printf() re-entrant, removed kprintf (replaced w/
  110.  * exec/RawDoFmt()), kernputchar, ksprintn() and logpri.
  111.  * As every BSD/Mach code was removed so was copyright.
  112.  * 
  113.  * Revision 1.4  93/02/25  19:39:25  19:39:25  puhuri (Markus Peuhkuri)
  114.  * Includes fixed, compatible with both GCC and SAS, 
  115.  * function declarations fixed, calls from panic() fixed,
  116.  * checks for various places.
  117.  * 
  118.  * Revision 1.3  93/02/24  17:32:17  17:32:17  puhuri (Markus Peuhkuri)
  119.  * Add printf (for printing to log)
  120.  * 
  121.  * Revision 1.2  93/02/24  17:22:23  17:22:23  puhuri (Markus Peuhkuri)
  122.  * Made program to work much cleanier, still uses Amiga.lib
  123.  * 
  124.  * Revision 1.1  92/12/17  18:19:42  puhuri
  125.  * Initial revision
  126.  *
  127.  */
  128.  
  129. #include <conf.h>
  130.  
  131. #include <sys/param.h>
  132. #include <sys/systm.h>
  133. #include <sys/syslog.h>
  134. #include <sys/time.h>
  135.  
  136. #include <kern/amiga_includes.h>
  137. #include <api/amiga_api.h>
  138. #include <api/amiga_libcallentry.h>
  139. #include <kern/amiga_log.h>
  140. #include <stdarg.h>
  141. #include <intuition/intuition.h>
  142.  
  143. #if __GNUC__
  144. #include <inline/intuition.h>
  145. #elif __SASC
  146. #include <proto/intuition.h>
  147. #endif
  148.  
  149. #include <kern/amiga_main_protos.h>
  150. #include <dos/rdargs.h>        /* CSource */
  151.  
  152. extern void exit(int);
  153.  
  154. void
  155. cs_putchar(unsigned char ch, struct CSource * cs)
  156. {
  157.   if (cs->CS_CurChr < cs->CS_Length 
  158.       && (cs->CS_Buffer[cs->CS_CurChr] = ch))
  159.     cs->CS_CurChr++;
  160. }
  161.  
  162. /****i* bsdsocket.library/panic ******************************************
  163. *
  164. *   NAME    
  165. *    panic -- Inform user from serious failure.
  166. *
  167. *   SYNOPSIS
  168. *    panic(Message, Arguments...)
  169. *
  170. *    void panic( STRPTR, ... )
  171. *
  172. *   FUNCTION
  173. *    Calls api_setfunctions() with no arguments to stop programs using
  174. *    AmiTCP. Writes message to log file. Sets up User Requester to
  175. *    inform user about situation. Avoids self-loops.
  176. *
  177. *
  178. *   INPUTS
  179. *        Messagestring - A pointer to string containing message to show
  180. *        to user and to write to log. It should describe problem so
  181. *        that user can take correcting action if it is failure with
  182. *        his configuration or is able to write bug report if it is
  183. *        a bug withn program.
  184. *
  185. *       Arguments - as in c-library printf()
  186. *
  187. *   RESULT
  188. *        This function does not return.
  189. *
  190. *   EXAMPLE
  191. *        if(Everything==WRONG)
  192. *        panic("Everything is wrong\nGoto sleep");
  193. *
  194. *   NOTES
  195. *        As panic does not return, it should be used only in extreme
  196. *    cases
  197. *
  198. *   BUGS
  199. *
  200. *   SEE ALSO
  201. *    log()
  202. *
  203. ******************************************************************************
  204. *
  205. */
  206.  
  207. #define PANICBUFFERSIZE 512
  208.  
  209. void
  210. panic(const char *fmt,...)
  211. {
  212.   struct EasyStruct panicES = {
  213.     sizeof( struct EasyStruct),
  214.     NULL,
  215.     "AmiTCP PANIC",
  216.     "panic: %s" ,
  217.     "Exit AmiTCP"
  218.   };
  219.   static in_panic = 0;
  220.   struct CSource cs;
  221.   char buffer[PANICBUFFERSIZE];
  222.   va_list ap;
  223.   struct Library *IntuitionBase = NULL; /* local intuitionbase */
  224.   extern struct Task *AmiTCP_Task;
  225.  
  226.   if (!in_panic){
  227.                 /* If we're called previously.. */
  228.     in_panic++;            /* We're in panic now */
  229.     api_setfunctions();        /* Set libraries to return error code */
  230.  
  231.     cs.CS_Buffer = buffer;
  232.     cs.CS_CurChr = 0;
  233.     cs.CS_Length = PANICBUFFERSIZE;
  234.  
  235.     va_start(ap, fmt);
  236.     vcsprintf(&cs, fmt, ap);
  237.     va_end(ap);
  238.     
  239.     log(LOG_EMERG, "panic: %s", buffer); /* Write to log */
  240.   }
  241.   in_panic--;
  242.     
  243.   /*
  244.    * Inform user (if log system has failed...)
  245.    * by opening a requester to the default public screen.
  246.    *
  247.    * Open a local IntuitionBase for the EasyRequestArgs()
  248.    */
  249.   if ((IntuitionBase = OpenLibrary("intuition.library", 37L)) != NULL) {
  250.     EasyRequestArgs(NULL, &panicES, NULL, (char *)&buffer);
  251.     CloseLibrary(IntuitionBase);
  252.     IntuitionBase = NULL;
  253.   }
  254.     
  255.   /*
  256.    * If the caller