]> www.hackdaworld.org Git - my-code/ivac.git/commitdiff
added usage function, added parsing of argv, some display tests ..
authorhackbard <hackbard>
Wed, 12 May 2004 07:53:27 +0000 (07:53 +0000)
committerhackbard <hackbard>
Wed, 12 May 2004 07:53:27 +0000 (07:53 +0000)
src/ivac.c
src/ivac.h

index 0a29f21aaa9e415e9078f211f04f9ae0c91562fd..3aad5998c56104ef3a7a598d26ba7c54d61ab3e7 100644 (file)
 
 #include "ivac.h"
 
 
 #include "ivac.h"
 
+int usage(void) {
+
+  puts("");
+  puts("usage: ivac <options>");
+  puts("");
+  puts("options:");
+  puts("-h \t\t show this help");
+  puts("-n <name> \t specify your name");
+  puts("-p <port> \t specify port to listen for incoming connections");
+  puts("");
+
+  return SUCCESS;
+}
+
 int main(int argc,char **argv) {
 
   /* TESTING BY NOW */
 int main(int argc,char **argv) {
 
   /* TESTING BY NOW */
@@ -32,13 +46,31 @@ int main(int argc,char **argv) {
   t_ivac ivac;
   int i;
 
   t_ivac ivac;
   int i;
 
-  if(argc!=2) {
-    printf("\nusage: %s <username>\n\n",argv[0]);
-    return ERROR;
+  /* default values */
+  strcpy(ivac.username,"ivac");
+  ivac.net.l_port=IVAC_LISTEN_PORT;
+  /* parse argv and change default values */
+  for(i=1;i<argc;i++) {
+    if(argv[i][0]=='-') {
+      switch(argv[i][1]) {
+        case 'h':
+          usage();
+          break;
+        case 'n':
+          strncpy(ivac.username,argv[i+1],CHAR_USERNAME);
+          break;
+        case 'p':
+          ivac.net.l_port=atoi(argv[i+1]);
+          break;
+        default:
+          usage();
+          break;
+      }
+    }
+    else usage();
   }
 
   }
 
-  /* set username */
-  strncpy(ivac.username,argv[1],CHAR_USERNAME);
   /* clear challenger struct */
   for(i=0;i<MAX_CONNECTIONS;i++)
     memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
   /* clear challenger struct */
   for(i=0;i<MAX_CONNECTIONS;i++)
     memset(&(ivac.challenger[i]),0,sizeof(t_challenger));
@@ -60,8 +92,6 @@ int main(int argc,char **argv) {
   ivac.input.mode=CONTENT_BUFFER;
   input_init(&(ivac.input));
 
   ivac.input.mode=CONTENT_BUFFER;
   input_init(&(ivac.input));
 
-  /* set listen port (futur: read from config or entered later) */
-  network_set_listen_port(&(ivac.net),IVAC_LISTEN_PORT);
   /* network init */
   if(network_init(&(ivac.net))==N_ERROR) {
     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
   /* network init */
   if(network_init(&(ivac.net))==N_ERROR) {
     printf("[ivac] use 'fuser -n tcp %d' to determine the process to kill!\n",
@@ -74,8 +104,10 @@ int main(int argc,char **argv) {
   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
   event_math(0,&(ivac.event),READ,ADD);
 
   event_math(ivac.net.l_fd,&(ivac.event),READ,ADD);
   event_math(0,&(ivac.event),READ,ADD);
 
+#ifdef USE_NCURSES
   /* display init */
   /* display init */
-  // display_init(&(ivac.display));
+  display_init(&(ivac.display));
+#endif
 
   /* display */
   ivac_display(&(ivac.display));
 
   /* display */
   ivac_display(&(ivac.display));
@@ -83,8 +115,6 @@ int main(int argc,char **argv) {
   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
 
   /* start event system - callbacks used: ivac_event_cb + ivac_regular_cb */
   event_start(&(ivac.event),(void *)&ivac,ivac_event_cb,ivac_regular_cb);
 
-  // ivac_shutdown(&ivac);
-
   return SUCCESS;
 }
 
   return SUCCESS;
 }
 
@@ -93,7 +123,9 @@ int ivac_shutdown(t_ivac *ivac) {
   network_shutdown(&(ivac->net));
   input_shutdown(&(ivac->input));
   event_stop(&(ivac->event));
   network_shutdown(&(ivac->net));
   input_shutdown(&(ivac->input));
   event_stop(&(ivac->event));
-  // display_shutdown(&(ivac->display));
+#ifdef USE_NCURSES
+  display_shutdown(&(ivac->display));
+#endif
 
   return SUCCESS;
 }
 
   return SUCCESS;
 }
@@ -131,7 +163,7 @@ int ivac_send_info(int channel,t_ivac *ivac) {
 
 int ivac_send_quit(int channel,t_ivac *ivac) {
 
 
 int ivac_send_quit(int channel,t_ivac *ivac) {
 
-  char data[6];
+  char data[7]; /* one more for \0 */
 
   data[0]=IVAC_SEND_QUIT;
   data[1]=4;
 
   data[0]=IVAC_SEND_QUIT;
   data[1]=4;
@@ -182,7 +214,8 @@ int ivac_receive_info(int channel,t_ivac *ivac) {
           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
           count+=6;
       default:
           memset(&(ivac->challenger[channel]),0,sizeof(t_challenger));
           count+=6;
       default:
-        puts("[ivac] ivac_receive_info, unknown character");
+        printf("[ivac] ivac_receive_info, unknown character: (%c,%02x\n",
+               data[count]);
         return ERROR;
         break;
     }
         return ERROR;
         break;
     }
@@ -223,7 +256,7 @@ int ivac_event_cb(t_event *event,void *ptr) {
       /* remote is sending info */
       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
         ivac_receive_info(channel,ivac);
       /* remote is sending info */
       if(FD_ISSET(ivac->net.connection[channel].fd,&(event->rfds)))
         ivac_receive_info(channel,ivac);
-        // ivac_display_content(ivac);
+        ivac_display_content(ivac);
     }
   } 
 
     }
   } 
 
@@ -286,6 +319,12 @@ int ivac_parse_command(t_input *input,void *ptr) {
     if(!(strncmp(arg[0],"quit",4))) {
       valid=1;
       sprintf(c_str,"ivac shutdown ...");
     if(!(strncmp(arg[0],"quit",4))) {
       valid=1;
       sprintf(c_str,"ivac shutdown ...");
+      for(i=0;i<MAX_CONNECTIONS;i++) {
+        if(ivac->net.connection[i].status&C_ESTABL) {
+          ivac_send_quit(i,ivac);
+          network_close(&(ivac->net),i);
+        }
+      }
       ivac_shutdown(ivac);
     }
     if(!(strncmp(arg[0],"set",3))) {
       ivac_shutdown(ivac);
     }
     if(!(strncmp(arg[0],"set",3))) {
index b635160f12115b37f233c7fcc6aca2ac8b3bb4ed..af64d04c8592b51f018558a3a821fd86ad7e3346 100644 (file)
@@ -65,8 +65,10 @@ typedef struct s_ivac {
 } t_ivac;
 
 /* function prototypes */
 } t_ivac;
 
 /* function prototypes */
+int usage(void);
 int ivac_shutdown(t_ivac *ivac);
 int ivac_send_info(int channel,t_ivac *ivac);
 int ivac_shutdown(t_ivac *ivac);
 int ivac_send_info(int channel,t_ivac *ivac);
+int ivac_send_quit(int channel,t_ivac *ivac);
 int ivac_receive_info(int channel,t_ivac *ivac);
 int ivac_event_cb(t_event *event,void *ptr);
 int ivac_regular_cb(t_event *event,void *ptr);
 int ivac_receive_info(int channel,t_ivac *ivac);
 int ivac_event_cb(t_event *event,void *ptr);
 int ivac_regular_cb(t_event *event,void *ptr);