home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / ZMISC.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  8KB  |  293 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* zmisc.c */
  20. /* Miscellaneous operators */
  21. #include "errno_.h"
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "ghost.h"
  25. #include "gp.h"
  26. #include "errors.h"
  27. #include "oper.h"
  28. #include "ialloc.h"
  29. #include "idict.h"
  30. #include "dstack.h"            /* for name lookup in bind */
  31. #include "iname.h"
  32. #include "ipacked.h"
  33. #include "ivmspace.h"
  34. #include "store.h"
  35.  
  36. /* Import the C getenv function. */
  37. extern char *getenv(P1(const char *));
  38.  
  39. /* Import the serial number from iinit.c. */
  40. extern const long gs_serialnumber;
  41.  
  42. /* <proc> bind <proc> */
  43. int
  44. zbind(os_ptr op)
  45. {    uint depth = 1;
  46.     ref defn;
  47.     register os_ptr bsp;
  48.     switch ( r_type(op) )
  49.        {
  50.     case t_array:
  51.     case t_mixedarray:
  52.     case t_shortarray:
  53.         defn = *op;
  54.         break;
  55.     case t_oparray:
  56.         defn = op_array_table.value.refs[op_index(op) - op_def_count];
  57.         break;
  58.     default:
  59.         return_error(e_typecheck);
  60.        }
  61.     push(1);
  62.     *op = defn;
  63.     bsp = op;
  64.     /*
  65.      * We must not make the top-level procedure read-only,
  66.      * but we must bind it even if it is read-only already.
  67.      *
  68.      * Here are the invariants for the following loop:
  69.      *    `depth' elements have been pushed on the ostack;
  70.      *    For i < depth, p = ref_stack_index(&o_stack, i):
  71.      *      *p is an array (or packedarray) ref. */
  72. #define r_is_ex_oper(rp)\
  73.   ((r_btype(rp) == t_operator || r_type(rp) == t_oparray) &&\
  74.    r_has_attr(rp, a_executable))
  75.     while ( depth )
  76.        {    while ( r_size(bsp) )
  77.            {    ref *tp = bsp->value.refs;
  78.             r_dec_size(bsp, 1);
  79.             if ( r_is_packed(tp) )
  80.              { /* Check for a packed executable name */
  81.                ushort elt = *(ushort *)tp;
  82.                if ( r_packed_is_exec_name(&elt) )
  83.                 { ref nref;
  84.                   ref *pvalue;
  85.                   name_index_ref(packed_name_index(&elt),
  86.                          &nref);
  87.                   if ( (pvalue = dict_find_name(&nref)) != 0 &&
  88.                    r_is_ex_oper(pvalue)
  89.                  )
  90.                 /* Note: can't undo this by restore! */
  91.                 *(ushort *)tp =
  92.                   pt_tag(pt_executable_operator) +
  93.                   op_index(pvalue);
  94.                 }
  95.                bsp->value.refs = (ref *)((ref_packed *)tp + 1);
  96.              }
  97.             else
  98.               switch ( bsp->value.refs++, r_type(tp) )
  99.              {
  100.             case t_name:    /* bind the name if an operator */
  101.               if ( r_has_attr(tp, a_executable) )
  102.                {    ref *pvalue;
  103.                 if ( (pvalue = dict_find_name(tp)) != 0 &&
  104.                      r_is_ex_oper(pvalue)
  105.                    )
  106.                     ref_assign_old(bsp, tp, pvalue, "bind");
  107.                }
  108.               break;
  109.             case t_array:    /* push into array if procedure */
  110.               if ( !r_has_attr(tp, a_write) ) break;
  111.             case t_mixedarray:
  112.             case t_shortarray:
  113.               if ( r_has_attr(tp, a_executable) )
  114.                {    /* Make reference read-only */
  115.                 r_clear_attrs(tp, a_write);
  116.                 if ( bsp >= ostop )
  117.                   {    /* Push a new stack block. */
  118.                     ref temp;
  119.                     int code;
  120.                     temp = *tp;
  121.                     osp = bsp;
  122.                     code = ref_stack_push(&o_stack, 1);
  123.                     if ( code < 0 )
  124.                       {    ref_stack_pop(&o_stack, depth);
  125.                         return_error(code);
  126.                       }
  127.                     bsp = osp;
  128.                     *bsp = temp;
  129.                   }
  130.                 else
  131.                     *++bsp = *tp;
  132.                 depth++;
  133.                }
  134.              }
  135.            }
  136.         bsp--; depth--;
  137.         if ( bsp < osbot )
  138.           {    /* Pop back to the previous stack block. */
  139.             osp = bsp;
  140.             ref_stack_pop_block(&o_stack);
  141.             bsp = osp;
  142.           }
  143.        }
  144.     osp = bsp;
  145.     return 0;
  146. }
  147.  
  148. /* - .currenttime <int> */
  149. int
  150. zcurrenttime(register os_ptr op)
  151. {    long date_time[2];
  152.     gp_get_clock(date_time);
  153.     push(1);
  154.     make_real(op, date_time[0] * 1440.0 + date_time[1] / 60000.0);
  155.     return 0;
  156. }
  157.  
  158. /* <string> getenv <value_string> true */
  159. /* <string> getenv false */
  160. int
  161. zgetenv(register os_ptr op)
  162. {    char *str, *value;
  163.     int code;
  164.     check_read_type(*op, t_string);
  165.     str = ref_to_string(op, imemory, "getenv name");
  166.     if ( str == 0 )
  167.         return_error(e_VMerror);
  168.     value = getenv(str);
  169.     ifree_string((byte *)str, r_size(op) + 1, "getenv name");
  170.     if ( value == 0 )        /* not found */
  171.        {    make_bool(op, 0);
  172.         return 0;
  173.        }
  174.     code = string_to_ref(value, op, iimemory, "getenv value");
  175.     if ( code < 0 ) return code;
  176.     push(1);
  177.     make_bool(op, 1);
  178.     return 0;
  179. }
  180.  
  181. /* <name> <proc> .makeoperator <oper> */
  182. int
  183. zmakeoperator(register os_ptr op)
  184. {    check_type(op[-1], t_name);
  185.     check_proc(*op);
  186.     if ( r_is_local(op) && !r_is_local(&op_array_table) )
  187.         return_error(e_invalidaccess);
  188.     if ( op_array_count == r_size(&op_array_table) )
  189.         return_error(e_limitcheck);
  190.     ref_assign_old(&op_array_table,
  191.                &op_array_table.value.refs[op_array_count],
  192.                op, "makeoperator");
  193.     op_array_nx_table[op_array_count] = name_index(op - 1);
  194.     r_set_type_attrs(op - 1, t_oparray, a_executable);
  195.     r_set_size(op - 1, op_def_count + op_array_count);
  196.     op_array_count++;
  197.     pop(1);
  198.     return 0;
  199. }
  200.  
  201. /* - .oserrno <int> */
  202. int
  203. zoserrno(register os_ptr op)
  204. {    push(1);
  205.     make_int(op, errno);
  206.     return 0;
  207. }
  208.  
  209. /* <int> .setoserrno - */
  210. int
  211. zsetoserrno(register os_ptr op)
  212. {    check_type(*op, t_integer);
  213.     errno = op->value.intval;
  214.     pop(1);
  215.     return 0;
  216. }
  217.  
  218. /* <int> .oserrorstring <string> true */
  219. /* <int> .oserrorstring false */
  220. int
  221. zoserrorstring(register os_ptr op)
  222. {    const char *str;
  223.     int code;    
  224.     uint len;
  225.     byte ch;
  226.     check_type(*op, t_integer);
  227.     str = gp_strerror((int)op->value.intval);
  228.     if ( str == 0 || (len = strlen(str)) == 0 )
  229.     {    make_false(op);
  230.         return 0;
  231.     }
  232.     check_ostack(1);
  233.     code = string_to_ref(str, op, iimemory, ".oserrorstring");
  234.     if ( code < 0 )
  235.         return code;
  236.     /* Strip trailing end-of-line characters. */
  237.     while ( (len = r_size(op)) != 0 &&
  238.         ((ch = op->value.bytes[--len]) == '\r' || ch == '\n')
  239.           )
  240.         r_dec_size(op, 1);
  241.     push(1);
  242.     make_true(op);
  243.     return 0;
  244. }
  245.  
  246. /* - serialnumber <int> */
  247. int
  248. zserialnumber(register os_ptr op)
  249. {    push(1);
  250.     make_int(op, gs_serialnumber);
  251.     return 0;
  252. }
  253.  
  254. /* <string> <bool> .setdebug - */
  255. int
  256. zsetdebug(register os_ptr op)
  257. {    check_read_type(op[-1], t_string);
  258.     check_type(*op, t_boolean);
  259.        {    int i;
  260.         for ( i = 0; i < r_size(op - 1); i++ )
  261.             gs_debug[op[-1].value.bytes[i] & 127] =
  262.                 op->value.boolval;
  263.        }
  264.     pop(2);
  265.     return 0;
  266. }
  267.  
  268. /* - usertime <int> */
  269. int
  270. zusertime(register os_ptr op)
  271. {    long date_time[2];
  272.     gp_get_clock(date_time);
  273.     push(1);
  274.     make_int(op, date_time[0] * 86400000L + date_time[1]);
  275.     return 0;
  276. }
  277.  
  278. /* ------ Initialization procedure ------ */
  279.  
  280. op_def zmisc_op_defs[] = {
  281.     {"1bind", zbind},
  282.     {"0.currenttime", zcurrenttime},
  283.     {"1getenv", zgetenv},
  284.     {"2.makeoperator", zmakeoperator},
  285.     {"0.oserrno", zoserrno},
  286.     {"1.oserrorstring", zoserrorstring},
  287.     {"1serialnumber", zserialnumber},
  288.     {"2.setdebug", zsetdebug},
  289.     {"1.setoserrno", zsetoserrno},
  290.     {"0usertime", zusertime},
  291.     op_def_end(0)
  292. };
  293.