home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / libhook / src / hk_file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.4 KB  |  206 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "xp_core.h"
  19. #include "xp_mcom.h"
  20. #include "jsapi.h"
  21. #include "prefapi.h"
  22. #include "fe_proto.h"
  23. #include "hk_funcs.h"
  24.  
  25. #include "hk_private.h"
  26.  
  27.  
  28. /**************************************************
  29.  * This is a special function just to read in the
  30.  * users special hook.js file and interpret it
  31.  * into the special javascript hook filter object.
  32.  **************************************************/
  33.  
  34.  
  35. void
  36. HK_JSErrorReporter(JSContext *cx, const char *message, JSErrorReport *report);
  37.  
  38.  
  39. void
  40. HK_ReadHookFile(char *filename)
  41. {
  42.     JSErrorReporter err_reporter;
  43.     JSContext *j_context;
  44.     JSObject *hook_obj;
  45.     JSBool ret;
  46.     jsval result;
  47.     XP_File file_ptr;
  48.     XP_StatStruct stat_info;
  49.     int32 file_len;
  50.     int32 read_len;
  51.     char *read_buf;
  52.  
  53.     /*
  54.      * If we can't get a valid javascript context, just return.
  55.      */
  56.     if (!PREF_GetConfigContext(&j_context))
  57.     {
  58.         return;
  59.     }
  60.     if (j_context == NULL)
  61.     {
  62.         return;
  63.     }
  64.  
  65.     /*
  66.      * If we don't have a javascript hook object, try to create one,
  67.      * if we fail, then return.
  68.      */
  69.     hook_obj = hk_GetHookObject();
  70.     if (hook_obj == NULL)
  71.     {
  72.         if (HK_Init() == 0)
  73.         {
  74.             return;
  75.         }
  76.         else
  77.         {
  78.             hook_obj = hk_GetHookObject();
  79.         }
  80.  
  81.         if (hook_obj == NULL)
  82.         {
  83.             return;
  84.         }
  85.     }
  86.  
  87.     /*
  88.      * If the hook.js file doesn't exist or has
  89.      * zero size, return.
  90.      */
  91.     if (XP_Stat(filename, &stat_info, xpJSHTMLFilters) < 0)
  92.     {
  93.         return;
  94.     }
  95.     file_len = stat_info.st_size;
  96.     if (file_len <= 1)
  97.     {
  98.         return;
  99.     }
  100.  
  101.     /*
  102.      * If the hook.js cannot be opened for reading, return.
  103.      */
  104.     file_ptr = XP_FileOpen(filename, xpJSHTMLFilters, XP_FILE_READ);
  105.     if (file_ptr == NULL)
  106.     {
  107.         return;
  108.     }
  109.  
  110.     /*
  111.      * If a buffer to read hook.js cannot be allocated, return.
  112.      */
  113.     read_buf = XP_ALLOC(file_len * sizeof(char));
  114.     if (read_buf == NULL)
  115.     {
  116.         return;
  117.     }
  118.  
  119.     read_len = XP_FileRead(read_buf, file_len, file_ptr);
  120.  
  121.     (void)XP_FileClose(file_ptr);
  122.  
  123.     /*
  124.      * Evaluate the file in the javascript hook object.
  125.      */
  126.     err_reporter = JS_SetErrorReporter(j_context, HK_JSErrorReporter);
  127.     ret = JS_EvaluateScript(j_context, hook_obj, read_buf, read_len,
  128.             filename, 0, &result);
  129.     JS_SetErrorReporter(j_context, err_reporter);
  130.  
  131.     XP_FREE(read_buf);
  132. }
  133.  
  134.  
  135. /* copied from libpref */
  136. /* Platform specific alert messages */
  137. static void
  138. hk_Alert(char* msg)
  139. {
  140. #if defined(XP_MAC) || defined(XP_UNIX)
  141. #if defined(XP_UNIX)
  142.     if (getenv("NO_PREF_SPAM") == NULL)
  143. #endif
  144.     FE_Alert(NULL, msg);
  145. #endif
  146. #if defined (XP_WIN)
  147.     MessageBox (NULL, msg, "Netscape -- JS Preference Warning", MB_OK);
  148. #endif
  149. }
  150.  
  151.  
  152. /*
  153.  * Mostly copied from elsewhere, reports errors in the
  154.  * hook.js file as it is read.
  155.  */
  156. void
  157. HK_JSErrorReporter(JSContext *cx, const char *message, JSErrorReport *report)
  158. {
  159.     char *last;
  160.     int i, j, k, n;
  161.     const char *s, *t;
  162.  
  163.     last = PR_sprintf_append(0, "An error occurred reading the HTML hook file.");
  164.     last = PR_sprintf_append(last, "\n\n");
  165.     if (!report)
  166.     {
  167.         last = PR_sprintf_append(last, "%s\n", message);
  168.     }
  169.     else
  170.     {
  171.         if (report->filename)
  172.         {
  173.             last = PR_sprintf_append(last, "%s, ",
  174.                 report->filename);
  175.         }
  176.         if (report->lineno)
  177.         {
  178.             last = PR_sprintf_append(last, "line %u: ",
  179.                 report->lineno);
  180.         }
  181.         last = PR_sprintf_append(last, "%s. ", message);
  182.         if (report->linebuf)
  183.         {
  184.             for (s = t = report->linebuf; *s != '\0'; s = t)
  185.             {
  186.                 for (; t != report->tokenptr && *t != '<' && *t != '\0'; t++)
  187.                     ;
  188.                 last = PR_sprintf_append(last, "%.*s", t - s, s);
  189.                 if (*t == '\0')
  190.                 {
  191.                     break;
  192.                 }
  193.                 last = PR_sprintf_append(last, (*t == '<') ? "" : "%c", *t);
  194.                 t++;
  195.             }
  196.         }
  197.     }
  198.  
  199.     if (last)
  200.     {
  201.         hk_Alert(last);
  202.         XP_FREE(last);
  203.     }
  204. }
  205.  
  206.