]> rtime.felk.cvut.cz Git - sysless.git/blobdiff - libs4c/cmdproc/cmd_proc.c
Fix missing ':' in Emacs setup
[sysless.git] / libs4c / cmdproc / cmd_proc.c
index fbfe2bd4812a5eaba291cccb840404f5d1e3c7d2..0b255f5f4ed3b0ff67249467c1d66deff0c3a2d5 100644 (file)
@@ -7,8 +7,8 @@
               which can be received from more inputs and send reply
               to respective I/O stream output
  
-  Copyright (C) 2001 by Pavel Pisa pisa@cmp.felk.cvut.cz
-            (C) 2002 by PiKRON Ltd. http://www.pikron.com
+  Copyright (C) 2001-2009 by Pavel Pisa pisa@cmp.felk.cvut.cz
+            (C) 2002-2009 by PiKRON Ltd. http://www.pikron.com
             (C) 2007 by Michal Sojka <sojkam1@fel.cvut.cz>
 
   This file can be used and copied according to next
    - other license provided by project originators
  *******************************************************************/
 
-#include <types.h>
+#include <stdint.h>
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#include <i2str.h>
 #include <cmd_proc.h>
 #include "cmd_proc_priv.h"
 
@@ -34,6 +35,11 @@ char *skip_white(char *p)
 }
 
 /**
+ * Process a command line
+ *
+ * @param cmd_io I/O stack
+ * @param des_arr Pointer to the command descriptors.
+ * @param line Command line to process.
  *
  * @return Zero if no command was given in line, -CMDERR_BADCMD if
  * command is not known or has no function assigned to it. If a
@@ -89,26 +95,27 @@ int proc_cmd_line(cmd_io_t *cmd_io, cmd_des_t const **des_arr, char *line)
     var=NULL;
     while(*r){
       while((*p==*r)&&i){i--;r++;p++;};
-      if((i==0)&&!*r) break;    /* We've found the command */
-      if((*r=='?')&&i){
+      if((i==0)&&!*r) break;    /* We've found the command (full match) */
+      if((*r=='?')&&i){                /* '?' stands for an arbitrary character */
         if(!var) var=p;
        p++; r++; i--;
        continue;
       }
-      if((*r=='#')&&i&&isdigit((uint8_t)*p)){
+      if((*r=='#')&&i&&isdigit((uint8_t)*p)){ /* '#' stands for a digit */
        if(!var) var=p;
        p++; r++; i--;
        continue;
       }
-      if(*r=='*'){
+      if(*r=='*'){             /* '*' stands for any number of any characters */
        if(!var) var=p;
        i=0;
        break;
       }
-      i=1;
+      i=1;                     /* Signal no match */
       break;      
     }
     if(i!=0) continue; /* Try next command */
+    /* Setup parameters */
     if(des->mode&CDESM_OPCHR){
       if(!param[2])continue;
       if(!*param[2])continue;
@@ -121,6 +128,22 @@ int proc_cmd_line(cmd_io_t *cmd_io, cmd_des_t const **des_arr, char *line)
       if(param[parcnt])
         if(*param[parcnt]) parcnt++;
     }
+    if(des->mode&CDESM_SPACE_SEP){
+      while (*param[parcnt-1] && parcnt > (var ? 2 : 1) && parcnt < 9) {
+       p = param[parcnt-1];
+       while (*p && !isspace(*p)) p++;
+       if (*p) {
+         *p = '\0';
+         p = skip_white(p+1);
+         if (*p) {
+           parcnt++;
+           param[parcnt-1] = p;
+           continue;
+         }
+       }
+       break; /* No more parameters */
+      }
+    }
     param[parcnt]=0;
     if(!des->fnc) return -CMDERR_BADCMD;
     res=des->fnc(cmd_io,des,param);
@@ -153,6 +176,27 @@ int cmd_opchar_check(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
   return opchar;
 }
 
+int cmd_num_suffix(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[], unsigned *pval)
+{
+  unsigned val=0;
+  unsigned x;
+  char *p;
+  p=param[1];
+  if(!p) return -CMDERR_BADSUF;
+
+  do{
+    x=*p-'0';
+    if((unsigned)x>9)
+      return -CMDERR_BADSUF;
+    val*=10;
+    val+=x;
+    p++;
+  }while(*p && !strchr(" :?=",*p));
+  *pval=val;
+  return 0;  
+}
+
+
 int cmd_do_stamp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
 {
   if(*param[2]!=':') return -CMDERR_OPCHAR;
@@ -162,52 +206,6 @@ int cmd_do_stamp(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
   return 0; 
 }
 
-/**
- * Converts integer to string.
- * @param s Buffer to store the result.
- * @param val Value to convert.
- * @param len Minimal width of the converted strign (padded by ' ').
- * @param form Unused.
- * @return 0
- */
-int i2str(char *s,long val,int len,int form)
-{
-  int sig;
-  int dig=1;
-  int padd=0;
-  unsigned base=10;
-  unsigned long u;
-  unsigned long mag;
-  unsigned long num;
-  if((sig=val<0))num=-val;
-    else num=val;
-  mag=1;
-  u=base*mag;
-  while(num>=u){
-    dig++;
-    mag=u;
-    if(mag>~(unsigned long)0/base) break;
-    u*=base;
-  }
-  
-  if(len){
-    padd=len-dig;
-    if(sig) padd--;
-  }
-  if(padd<0) padd=0;
-  
-  while(padd){*(s++)=' ';padd--;}
-  if(sig) *(s++)='-';
-  
-  while(dig--){
-    *(s++)='0'+num/mag;
-    num=num%mag;
-    mag/=base;
-  }
-  *s=0;
-  return 0;
-}
-
 /**
  * Implementation of a command that reads or writes short pointed by des->info[0].
  */
@@ -321,6 +319,7 @@ int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
   cmd_des_t const **arr_stack[CMD_ARR_STACK_SIZE];
   int arr_stack_sp=0;
 
+  /* FIXME: This should not be there unconditional */
   if (cmd_io->priv.ed_line.io_stack)
     cmd_io = cmd_io->priv.ed_line.io_stack;
   
@@ -365,41 +364,6 @@ int cmd_do_help(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
   return 0;
 }
 
-/**
- * Executes command processor. This function is usually called from
- * application's main loop.
- */
-int cmd_processor_run(cmd_io_t *cmd_io, cmd_des_t const **commands)
-{
-  int val;
-
-  if(cmd_io_line_out(cmd_io))
-    return 1; /* Not all the output has been sent. */
-       
-  if(cmd_io_line_in(cmd_io)<=0)
-    return 0; /* Input line not finished or error. */
-
-  if(commands){
-    val=proc_cmd_line(cmd_io, commands, cmd_io->priv.ed_line.in->buf);
-  }else{
-    val=-CMDERR_BADCMD;
-  }
-
-  if(cmd_io->priv.ed_line.out->inbuf){
-    cmd_io_putc(cmd_io,'\r');
-    cmd_io_putc(cmd_io,'\n');
-  }else if(val<0){
-    char s[20];
-    cmd_io_puts(cmd_io,"ERROR ");
-    i2str(s,-val,0,0);
-    cmd_io_puts(cmd_io,s);
-    cmd_io_putc(cmd_io,'\r');
-    cmd_io_putc(cmd_io,'\n');
-  }
-  return 1; /* Command line processed */
-}
-
-
 /* Local Variables: */
 /* c-basic-offset: 2 */
-/* End */
+/* End: */