[BACK]Return to sm_ext.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / oxc

Diff for /OpenXM/src/oxc/sm_ext.c between version 1.5 and 1.11

version 1.5, 2000/11/28 18:11:42 version 1.11, 2016/04/01 18:12:39
Line 1 
Line 1 
 /* -*- mode: C; coding: euc-japan -*- */  /* -*- mode: C; coding: euc-japan -*- */
 /* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.4 2000/11/28 04:52:05 ohara Exp $ */  /* $OpenXM: OpenXM/src/oxc/sm_ext.c,v 1.10 2003/05/07 04:00:30 ohara Exp $ */
   
 #include <stdio.h>  #include <stdio.h>
 #include <stdlib.h>  #include <stdlib.h>
Line 10 
Line 10 
 #include <ox_toolkit.h>  #include <ox_toolkit.h>
 #include "sm.h"  #include "sm.h"
   
   static int  sm_control_spawn();
   static int  sm_control_terminate();
   static int  sm_control_kill();
   static int  sm_control_reset_pid();
   
   static void pid_extend();
   static int  pid_lookup(pid_t pid);
   static int  pid_registed(pid_t pid);
   static void pid_regist(pid_t pid);
   static void pid_delete(pid_t pid);
   static int  pid_reset(pid_t pid);
   static int  pid_kill(pid_t pid);
   static void pid_kill_all();
   
 /* ultra loose data base. */  /* ultra loose data base. */
 static db db_localfunc[] = {  static struct { int (*func_ptr)(); char *key; } tbl_lfunc[] = {
       {lf_oxc_open, "spawn"},
     {lf_oxc_open, "oxc_open"},      {lf_oxc_open, "oxc_open"},
     {NULL, NULL}      {NULL, NULL}
 };  };
   
 static db db_sm[] = {  static struct { int (*func_ptr)(); int key; } tbl_smcmd[] = {
       {sm_control_spawn,     SM_control_spawn_server},
       {sm_control_terminate, SM_control_terminate_server},
     {sm_executeFunction, SM_executeFunction},      {sm_executeFunction, SM_executeFunction},
     {sm_mathcap,         SM_mathcap},      {sm_mathcap,         SM_mathcap},
     {sm_set_mathcap,     SM_setMathCap},      {sm_set_mathcap,     SM_setMathCap},
     {sm_popCMO,          SM_popCMO},      {sm_popCMO,          SM_popCMO},
     {sm_pops,            SM_pops},      {sm_pops,            SM_pops},
         {sm_control_reset_pid, SM_control_reset_connection_pid},      {sm_control_reset_pid, SM_control_reset_connection_server},
         {sm_control_kill_pid, SM_control_kill_pid},      {sm_control_kill, SM_control_kill},
         {sm_control_kill, SM_control_kill},      {NULL, 0}
     {NULL, NULL}  
 };  };
   
 __inline__  extern OXFILE *stack_oxfp;
 static int (*db_search(void *key, db *dbs, int (*cmp)(void *, void *)))()  
   int (*sm_search_f(int code))()
 {  {
     while (dbs->key != NULL) {      int i;
         if (cmp(key, dbs->key) == 0) {      for (i=0; tbl_smcmd[i].key != 0; i++) {
             return dbs->func_ptr;          if (code == tbl_smcmd[i].key) {
               return tbl_smcmd[i].func_ptr;
         }          }
         dbs++;  
     }      }
     return NULL;      return NULL;
 }  }
   
 static int (*lookup_localfunction(char *name))()  static int (*lookup_localfunction(char *name))()
 {  {
     return db_search(name, db_localfunc, strcmp);      int i;
       for (i=0; tbl_lfunc[i].key != NULL; i++) {
           if (strcmp(name, tbl_lfunc[i].key) == 0) {
               return tbl_lfunc[i].func_ptr;
           }
       }
       return NULL;
 }  }
   
 /*  /*
Line 50  Normally local functions push a return value to the st
Line 73  Normally local functions push a return value to the st
 but, if error occurs, then these return non-positive numbers and  but, if error occurs, then these return non-positive numbers and
 the sm_executeFunction push an error object.  the sm_executeFunction push an error object.
 */  */
 void sm_executeFunction(OXFILE *oxfp)  int sm_executeFunction()
 {  {
     int (*func)(OXFILE *);      int (*func)();
     int retcode = 0;      int retcode = 0;
     cmo *ob = pop();      cmo *ob = pop();
     if (ob->tag == CMO_STRING) {      if (ob->tag == CMO_STRING) {
         func = lookup_localfunction(((cmo_string *)ob)->s);          func = lookup_localfunction(((cmo_string *)ob)->s);
         if (func != NULL) {          if (func != NULL) {
             if ((retcode = func(oxfp)) > 0) {              if ((retcode = func()) > 0) {
                 return;                  return 0;
             }              }
         }          }
     }      }
     push_error(retcode, ob);      push_error(retcode, ob);
       return 0;
 }  }
   
 /* getargs() set number of popped objects to argc. */  /* getargs() set number of popped objects to argc. */
Line 75  static int getargs(cmo ***args)
Line 99  static int getargs(cmo ***args)
     cmo_int32 *m = (cmo_int32 *)pop();      cmo_int32 *m = (cmo_int32 *)pop();
   
     if (m->tag != CMO_INT32 || (argc = m->i) < 0) {      if (m->tag != CMO_INT32 || (argc = m->i) < 0) {
         fprintf(stderr, "oxc: invalid arguments\n");          ox_printf("oxc: invalid arguments\n");
     }else {      }else {
         argv = (cmo **)malloc(sizeof(cmo *)*argc);          argv = (cmo **)malloc(sizeof(cmo *)*argc);
         for(i=0; i<argc; i++) {          for(i=0; i<argc; i++) {
Line 88  static int getargs(cmo ***args)
Line 112  static int getargs(cmo ***args)
   
 #define MAX_PROCESS 1024  #define MAX_PROCESS 1024
   
   /* Process Table */
 static pid_t *pids = NULL;  static pid_t *pids = NULL;
 static int pid_ptr = 0;  static int pid_ptr = 0;
 static int pid_size = 0;  static int pid_size = 0;
   
 void pids_extend()  static void pids_extend()
 {  {
     int size2 = pid_size + MAX_PROCESS;      int size2 = pid_size + MAX_PROCESS;
     pid_t *pids2 = (pid_t *)malloc(sizeof(pid_t)*size2);      pid_t *pids2 = (pid_t *)malloc(sizeof(pid_t)*size2);
Line 104  void pids_extend()
Line 129  void pids_extend()
     pids = pids2;      pids = pids2;
 }  }
   
 int pid_lookup(pid_t pid)  static int pid_lookup(pid_t pid)
 {  {
     int i;      int i;
     for(i=0; i<pid_ptr; i++) {      for(i=0; i<pid_ptr; i++) {
Line 115  int pid_lookup(pid_t pid)
Line 140  int pid_lookup(pid_t pid)
     return -1;      return -1;
 }  }
   
 int pid_registed(pid_t pid)  static int pid_registed(pid_t pid)
 {  {
     return pid_lookup(pid)+1;      return pid_lookup(pid)+1;
 }  }
   
 void pid_regist(pid_t pid)  static void pid_regist(pid_t pid)
 {  {
     if (pid_ptr >= pid_size) {      if (pid_ptr >= pid_size) {
                 pids_extend();          pids_extend();
     }      }
         pids[pid_ptr++] = pid;      pids[pid_ptr++] = pid;
 }  }
   
 void pid_delete(pid_t pid)  static void pid_delete(pid_t pid)
 {  {
     int i = pid_lookup(pid);      int i = pid_lookup(pid);
     if (i >= 0 && i != --pid_ptr) {      if (i >= 0 && i != --pid_ptr) {
Line 136  void pid_delete(pid_t pid)
Line 161  void pid_delete(pid_t pid)
     }      }
 }  }
   
 int pid_reset(pid_t pid)  static int pid_reset(pid_t pid)
 {  {
         if (pid_registed(pid)) {      if (pid_registed(pid)) {
                 kill(pid, SIGUSR1);          kill(pid, SIGUSR1);
                 return 1;          return 1;
         }      }
         return 0;      return 0;
 }  }
   
 int pid_kill(pid_t pid)  static int pid_kill(pid_t pid)
 {  {
         if (pid_registed(pid)) {      if (pid_registed(pid)) {
                 kill(pid, SIGKILL);          kill(pid, SIGKILL);
                 pid_delete(pid);          pid_delete(pid);
                 return 1;          return 1;
         }      }
         return 0;      return 0;
 }  }
   
 /* Killing all child processes */  /* Killing all child processes */
 void pid_kill_all()  static void pid_kill_all()
 {  {
         while(pid_ptr > 0) {      while(pid_ptr > 0) {
         kill(pids[--pid_ptr], SIGKILL);          kill(pids[--pid_ptr], SIGKILL);
         }      }
 }  }
   
   cmo_error2 *type_checker(cmo *ob, int type)
   {
   /*  cmo_error2 *err_ob; */
       if (ob->tag != type) {
           /* push and return an error object */
       }
       return NULL;
   }
   
 int lf_oxc_open()  int lf_oxc_open()
 {  {
     cmo **argv;      cmo_int32 *argc = (cmo_int32 *)pop();
     char *cmd;          if (argc->tag == CMO_INT32 && argc->i == 1) {
     int port;                  return sm_control_spawn();
     pid_t pid;          }
           push_error(-1, (cmo *)argc);
           return -1;
   }
   
     if (getargs(&argv) != 2 || argv[0]->tag != CMO_STRING  int sm_control_spawn_typecheck(cmo_list *args, cmo_list *ports, cmo_int32 *port, cmo_string *sname)
         || argv[1]->tag != CMO_INT32) {  {
         fprintf(stderr, "oxc: invalid arguments\n");          char *cmd = sname->s;
         return -1;  
     }  
   
     cmd  = ((cmo_string *)argv[0])->s;          return args->tag == CMO_LIST
     port = ((cmo_int32 *)argv[1])->i;                  && list_length(args) > 1
     pid = lf_oxc_open_main(cmd, port);                  && ports->tag == CMO_LIST
     if (pid > 0) {                  && list_length(ports) > 0
         push(new_cmo_int32(pid));                  && port->tag == CMO_INT32
         pid_regist(pid);                  && sname->tag == CMO_STRING
     }                  && cmd != NULL
     return pid;                  && which(cmd, getenv("PATH")) != NULL;
 }  }
   
 void sm_mathcap(OXFILE *oxfp)  static int sm_control_spawn()
 {  {
     push((cmo *)oxf_cmo_mathcap(oxfp));          cmo_list *args    = (cmo_list *)pop();
           cmo_list *ports   = (cmo_list *)list_first_cmo(args);
           cmo_int32 *port   = (cmo_int32 *)list_first_cmo(ports);
           cmo_string *sname = (cmo_string *)list_nth(args, 1);
           pid_t pid;
   
           if (sm_control_spawn_typecheck(args, ports, port, sname)) {
                   pid = lf_oxc_open_main(sname->s, (short)port->i);
                   if (pid > 0) {
                           push((cmo *)new_cmo_int32(pid));
                           pid_regist(pid);
                           ox_printf("oxc: spawns %s\n", sname->s);
                           return pid;
                   }
           }
           push_error(-1, (cmo *)args);
           return 0;
   
 }  }
   
 void sm_set_mathcap(OXFILE *oxfp)  int sm_mathcap()
 {  {
       push((cmo *)oxf_cmo_mathcap(stack_oxfp));
       return 0;
   }
   
   int sm_set_mathcap()
   {
     cmo_mathcap *m = (cmo_mathcap *)pop();      cmo_mathcap *m = (cmo_mathcap *)pop();
     if (m->tag == CMO_MATHCAP) {      if (m->tag == CMO_MATHCAP) {
         oxf_mathcap_update(oxfp, m);          oxf_mathcap_update(stack_oxfp, m);
     }else {      }else {
         push_error(-1, m);          push_error(-1, (cmo *)m);
         /* an error object must be pushed */          /* an error object must be pushed */
     }      }
       return 0;
 }  }
   
 void sm_control_kill(OXFILE *oxfp)  static int sm_control_kill()
 {  {
         pid_kill_all();      pid_kill_all();
       return 0;
 }  }
   
 void sm_control_kill_pid(OXFILE *oxfp)  static int sm_control_terminate()
 {  {
     cmo_int32 *m = (cmo_int32 *)pop();      cmo_int32 *m = (cmo_int32 *)pop();
     pid_t pid = m->i;      pid_t pid = m->i;
     if (m->tag != CMO_INT32 || !pid_kill(pid)) {      if (m->tag != CMO_INT32 || !pid_kill(pid)) {
         push_error(-1, m);          push_error(-1, (cmo *)m);
     }      }
       return 0;
 }  }
   
 void sm_control_reset_pid(OXFILE *oxfp)  static int sm_control_reset_pid()
 {  {
     cmo_int32 *m = (cmo_int32 *)pop();      cmo_int32 *m = (cmo_int32 *)pop();
     pid_t pid = m->i;      pid_t pid = m->i;
     if (m->tag != CMO_INT32 || !pid_reset(pid)) {      if (m->tag != CMO_INT32 || !pid_reset(pid)) {
         push_error(-1, m);          push_error(-1, (cmo *)m);
                 return;          return 0;
     }      }
         /* ... */      /* ... */
 }      return 0;
   
 void sm_control_spawn_server(OXFILE *oxfp);  
 void sm_control_terminate_server(OXFILE *oxfp);  
   
 static int intcmp(int key1, int key2)  
 {  
     return key1 != key2;  
 }  
   
   
 int (*sm_search_f(int code))()  
 {  
     return db_search(code, db_sm, intcmp);  
 }  }

Legend:
Removed from v.1.5  
changed lines
  Added in v.1.11

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>