Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals  

bltin.c

00001  /*
00002   * plint/prog/bltin.c : builtin plint program features
00003   * 
00004   * Time-stamp: <2002-12-21 14:42:32 gseba>
00005   * 
00006   * Copyright (C) Sebastian Glita, email: gseba@users.sourceforge.net
00007   * 
00008   * This file is part of plint.
00009   * 
00010   * plint is free software; you can  redistribute it and/or modify it under the
00011   * terms of the  GNU General Public License as published  by the Free Software
00012   * Foundation; either version 2, or (at your option) any later version.
00013   *
00014   * plint  is distributed  in the  hope  that it  will be  useful, but  WITHOUT
00015   * ANY  WARRANTY; without  even  the implied  warranty  of MERCHANTABILITY  or
00016   * FITNESS FOR A  PARTICULAR PURPOSE.  See the GNU  General Public License for
00017   * more details.
00018   * 
00019   * You should  have received a  copy of the  GNU General Public  License along
00020   * with  plint; see  the file  COPYING.  If  not, write  to the  Free Software
00021   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA USA.
00022   *
00023   */
00024 
00025 #include "plint.h"
00026 #include "share.h"
00027 #include "error.h"
00028 
00029 #include "data.h"
00030 #include "bltin.h"
00031 #include "stack.h"
00032 
00033 #include "parse/stack.h"
00034 
00035 #include "misc/dev.h"
00036 #include "misc/sys.h"
00037 #include "misc/file.h"
00038 
00039 plint_var_t plint_get_argo(int * n, char *** a)
00040 {
00041   plint_str_t s;
00042   if (1[*a] && 1[*a][0] == '\\')
00043     {
00044       ++*a, --*n;
00045       s = plint_str_new(1+**a, strlen(1+**a));
00046     }
00047   else
00048     PLINT_str_EMPTY(s);
00049   return plint_bar_new(plint_str_ptr(s));
00050 }
00051 plint_var_t plint_get_argv(int * n, char *** a)
00052 {
00053   plint_val_t v;
00054   plint_typ_t t;
00055   plint_var_t argv = plint_bar_new(PLINT_PTR_NEW(tab, PLINT_tab_INIT_NUL));
00056   char * s;
00057   size_t l;
00058 
00059   while (++*a, --*n)
00060     {
00061       s = (**a) + 1; l = strlen(s);
00062       switch ((**a)[0])
00063         {
00064           /* points */
00065         case '[' OR '(':
00066           --s;
00067         case '@':
00068           t = PLINT_pnt;
00069           if (2 != sscanf(s, "[%d,%d]", &v.pnt.x, &v.pnt.y) &&
00070               2 != sscanf(s, "(%d,%d)", &v.pnt.x, &v.pnt.y)
00071               )
00072             return argv;
00073           break;
00074 
00075           /* booleens */
00076         case 'y' OR 'n' OR 'Y' OR 'N' OR 't' OR 'f' OR 'T' OR 'F':
00077           --s;
00078         case '?':
00079           t = PLINT_bol;
00080           if (!strcasecmp("true", s) || !strcasecmp("yes", s) || !strcasecmp("y", s))
00081             v.bol = !0;
00082           else if (!strcasecmp("false", s) || !strcasecmp("no", s) || !strcasecmp("n", s))
00083             v.bol = 0;
00084           else
00085             return argv;
00086           break;
00087 
00088           /* strings */
00089         case '\'':
00090           if (s[--l] != '\'')
00091             return argv;
00092         case '"':
00093           t = PLINT_str;
00094           v.str = plint_str_new(s, l);
00095           break;
00096 
00097           /* numbers */
00098         case '.' OR '-' OR '+':
00099         case '0' OR '1' OR '2' OR '3' OR '4' OR '5' OR '6' OR '7' OR '8' OR '9':
00100           --s;
00101         case '$':
00102           t = PLINT_num_dbl;
00103           if (1 != sscanf(s, "%lf", &v.num_dbl))
00104             return argv;
00105           break;
00106 
00107         default:
00108           return argv;
00109         }
00110 
00111       {
00112         plint_var_t r = plint_bar_new(plint_val_new(t, v));
00113         plint_var_inc(plint_var_ref(argv));
00114         plint_blind_assign(plint_var_ref(plint_tab_a(plint_var_tab(argv))[plint_tab_n(plint_var_tab(argv)) - 1]), r, PLINT_ASG_REF);
00115 
00116         plint_var_singleton(plint_tab_a(plint_var_tab(argv))[plint_tab_n(plint_var_tab(argv)) - 1]) = 1;
00117         plint_var_readonly(plint_tab_a(plint_var_tab(argv))[plint_tab_n(plint_var_tab(argv)) - 1]) = 1;
00118 
00119         plint_var_fre(r);
00120       }
00121     }
00122 
00123   return argv;
00124 }
00125 
00126 int plint_prog_fork()
00127 {
00128   pid_t p;
00129 
00130   plint_dev_close();
00131 
00132   p = fork();
00133 
00134   if (p == 0)
00135     {
00136       // child
00137       plint_insert_t q;
00138       plint_fork_link(plint_code.root);
00139 
00140       q = plint_ins.hit;
00141       while ((q = q->down))
00142         plint_fork_link(q->point);
00143     }
00144   else if (p == -1)
00145     PLINT_ERROR(PLINT_ERR_FORK);
00146 
00147   plint_dev_open();
00148   return p;
00149 }
00150 
00151 
00152 int plint_num_level(int n)
00153 {
00154   plint_stack_t s = plint_stack.top;
00155   ++n;
00156   while (s && --n) s = s->proc->down;
00157   return s->block.src->level;
00158 }
00159 
00160 
00161 plint_tab_t plint_all_var(int b)
00162 {
00163   plint_stack_t s = plint_stack_block(b);
00164   plint_list_t l = s->head->next;
00165   size_t i, n = s->count - ((s->proc == s) ? s->block.src->proc.arrity : 0);
00166   plint_tab_t ret = PLINT_tab_INIT_NUL;
00167 
00168   if (n)
00169     PLINT_tab_NEW(ret, n);
00170 
00171   if (s->block.src->frm == PLINT_FRM_METH)
00172     l = l->next;
00173 
00174   for (i=0; i<n; ++i, l = l->next)
00175     {
00176       plint_str_t s;
00177       plint_hash_str(plint_hash.id, l->name, s);
00178       i[ret.a] = plint_bar_new(plint_str_ptr(s));
00179     }
00180   return ret;
00181 }
00182 
00183 plint_tab_t plint_all_arg(int b)
00184 {
00185   plint_stack_t s = plint_stack_block(b);
00186   plint_list_t l = s->head->next;
00187   size_t i = s->count, n = s->block.src->proc.arrity;
00188   plint_tab_t ret = PLINT_tab_INIT_NUL;
00189 
00190   if (n)
00191     PLINT_tab_NEW(ret, n);
00192 
00193   if (s->block.src->frm == PLINT_FRM_METH)
00194     l = l->next;
00195 
00196   for (i-=n; i; --i)
00197     l = l->next;
00198 
00199   for (; i<n; ++i, l = l->next)
00200     {
00201       plint_str_t s;
00202       plint_hash_str(plint_hash.id, l->name, s);
00203       i[ret.a] = plint_bar_new(plint_str_ptr(s));
00204     }
00205   return ret;
00206 }
00207 
00208 
00209 
00210 plint_bol_t plint_proc_last()
00211 {
00212   if (plint_stack.top->proc->down)
00213     return plint_stack.top->proc->block.src != plint_stack.top->proc->down->proc->block.src;
00214   return !0;
00215 }
00216 
00217 plint_str_t plint_proc_this(int b)
00218 {
00219   plint_str_t ret;
00220   plint_hash_str(plint_hash.id, plint_stack_block(b)->proc->block.src->proc.name, ret);
00221   return ret;
00222 }
00223 
00224 
00225 
00226 plint_tab_t plint_plugin_get(plint_str_t str)
00227 {
00228   plint_tab_t ret = PLINT_tab_INIT_NUL;
00229   plint_str_t ** pinfo = hash_getdata(hash_getindex(plint_hash.pi, plint_str_sn(str)));
00230   if (pinfo)
00231     {
00232       PLINT_tab_NEW(ret, 3);
00233       0[plint_tab_a(ret)] = plint_bar_new(plint_str_ptr(plint_str_cpy(0[*pinfo])));
00234       1[plint_tab_a(ret)] = plint_bar_new(plint_str_ptr(plint_str_cpy(1[*pinfo])));
00235       2[plint_tab_a(ret)] = plint_bar_new(plint_str_ptr(plint_str_cpy(2[*pinfo])));
00236     }
00237   return ret;
00238 }
00239 
00240 static int hsh_pia(hashindex_t hi, ns_key_t ns, plint_str_t ** pinfo, plint_hsh_t ret)
00241 {
00242   plint_str_t str;
00243   plint_tab_t tab = PLINT_tab_INIT(3);
00244   0[plint_tab_a(tab)] = plint_bar_new(plint_str_ptr(plint_str_cpy(0[*pinfo])));
00245   1[plint_tab_a(tab)] = plint_bar_new(plint_str_ptr(plint_str_cpy(1[*pinfo])));
00246   2[plint_tab_a(tab)] = plint_bar_new(plint_str_ptr(plint_str_cpy(2[*pinfo])));
00247   plint_hash_str(ns_hash(ns), hi, str);
00248   hash_putstr(NS_GLOBAL(ret), plint_str_sn(str), plint_bar_new(plint_tab_ptr(tab)));
00249   plint_str_fre(str);
00250 
00251   return !0;
00252 }
00253 
00254 plint_hsh_t plint_plugin_all()
00255 {
00256   plint_hsh_t ret = plint_hsh_new();
00257   hash_iterdata_all(plint_hash.pi, (hash_iterfunc_t)hsh_pia, ret);
00258   return ret;
00259 }

Generated on Thu Jan 9 19:02:37 2003 for plint by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002