Added getopt, and some things got based on parameters.
authorviric@llimona
Fri, 21 Sep 2007 00:21:20 +0200
changeset 29 91286c3ecebc
parent 28 b73712b8370d
child 30 7cf0e2193984
Added getopt, and some things got based on parameters.
app_term.c
client.c
main.c
main.h
server.c
--- a/app_term.c	Wed Sep 19 23:54:59 2007 +0200
+++ b/app_term.c	Fri Sep 21 00:21:20 2007 +0200
@@ -85,7 +85,7 @@
     int pid;
     int res;
 
-    if (1)
+    if (command_line.s_param.run_in_subterminal)
         give_terminal(p_parent, p_child);
     else
         give_pipes(p_parent, p_child);
--- a/client.c	Wed Sep 19 23:54:59 2007 +0200
+++ b/client.c	Fri Sep 21 00:21:20 2007 +0200
@@ -55,9 +55,8 @@
     Net_c_process_read_fdset net_process_read_fdset;
     Net_c_send net_send;
 
-    command_line.is_server = 0;
-
-    if (1) /* Will be 'tcp', 'ether', ... */
+    if (command_line.c_param.transport == UNIX)
+        /* Will be 'tcp', 'ether', ... */
     {
         c_unix_connect_socket();
         net_prepare_read_fdset = c_unix_prepare_read_fdset;
@@ -65,11 +64,13 @@
         net_send = c_unix_send;
     }
 
-    prepare_user_terminal();
+    if (command_line.c_param.raw_mode)
+        prepare_user_terminal();
 
     loop(net_prepare_read_fdset, net_process_read_fdset, net_send);
 
-    restore_user_terminal();
+    if (command_line.c_param.raw_mode)
+        restore_user_terminal();
 
     return 0;
 }
--- a/main.c	Wed Sep 19 23:54:59 2007 +0200
+++ b/main.c	Fri Sep 21 00:21:20 2007 +0200
@@ -25,7 +25,7 @@
 
 struct Command_line command_line;
 
-static const char version[] = "0.9.1";
+static const char version[] = "0.1";
 
 int max(int a, int b)
 {
@@ -36,37 +36,132 @@
 
 static int showhelp(const char *pname)
 {
-    printf("sdtdin-mix v%s - Copyright (C) 2007  Lluis Batlle i Rossell\n",
+    printf("st v%s - Swiss Terminal,  Copyright (C) 2007  "
+            "Lluis Batlle i Rossell\n",
             version);
-    printf("usage: %s [appcommand] [param1] [param2] ...\n", pname);
+    printf("usage: %s [opts] [appcommand] [param1] [param2] ...\n", pname);
     printf(" If you give _appcommand_, it starts the application and\n");
     printf(" a stdin server on $SM_SOCKET or /tmp/socket-sm.UID.\n");
     printf(" If not given, it starts a stdin client for the same socket.\n");
+    printf("options: \n");
+    printf(" -h     Show help\n");
+    printf(" -P     Run the child as connected to a pipe (default)\n");
+    printf(" -t     Run the child as connected to a terminal (raw mode in "
+        "client)\n");
+    printf(" -n MAX Serve at most MAX sockets for each transport\n");
     return 0;
 }
 
+static int my_getopt(int argc, char * argv[], const char *optstring)
+{
+    char *old_getopt_env;
+    static char getopt_env[100] = "POSIXLY_CORRECT=YES";
+    int res;
+
+    old_getopt_env = getenv("POSIXLY_CORRECT");
+    putenv(getopt_env);
+
+    res = getopt(argc, argv, optstring);
+
+    if (old_getopt_env == 0)
+    {
+        putenv("POSIXLY_CORRECT");
+    }
+    else
+        snprintf(getopt_env, sizeof getopt_env, "POSIXLY_CORRECT=%s",
+                old_getopt_env);
+
+    return res;
+}
+
 static void default_command_line()
 {
-    command_line.buffer_size = 1000;
-    get_unix_path();
+    command_line.is_server = 0;
+    command_line.s_param.use_blocking_sockets = 1;
+    command_line.s_param.run_in_subterminal = 0;
+    command_line.s_param.max_served = 1;
+    command_line.s_param.serve_unix = 1;
+    command_line.s_param.serve_tcp = 0;
+    command_line.s_param.send_xterm_resize = 1;
+    command_line.s_param.client_may_close_app_stdin = 0;
+    command_line.s_param.detach = 0;
+
+    command_line.tcp_port = 40000; /* Arbitrary */
+    command_line.buffer_size = 4096; /* Arbitrary */
+    get_unix_path(); /* for command_line.unix_path */
+
+    command_line.c_param.transport = UNIX;
+    command_line.c_param.wait_until_char = -1;
+    command_line.c_param.raw_mode = 0;
+    command_line.c_param.server_address = 0; /* TODO: free it */
 }
 
-static int parse_opts(int argn, char * const * argv)
+static int parse_opts(int argc, char * argv[])
 {
+    int c;
+    extern char *optarg;
+    extern int optind, opterr, optopt;
+
+    while(1) {
+        c = my_getopt(argc, argv, "tp:NrdPn:c:h");
+
+        if (c == -1)
+            break;
+
+        switch(c)
+        {
+            case 't':
+                command_line.s_param.run_in_subterminal = 1;
+                command_line.c_param.raw_mode = 1;
+                break;
+            case 'P':
+                command_line.s_param.run_in_subterminal = 0;
+                break;
+            case 'N':
+                command_line.s_param.use_blocking_sockets = 0;
+                break;
+            case 'd':
+                command_line.s_param.detach = 1;
+                break;
+            case 'p':
+                command_line.tcp_port = atoi(optarg);
+                break;
+            case 'n':
+                command_line.s_param.max_served = atoi(optarg);
+                break;
+            case 'c':
+                command_line.c_param.server_address = strdup(optarg);
+                break;
+            case 'h':
+                showhelp(argv[0]);
+                exit(0);
+                break;
+            case '?':
+                error("Wrong option %c.\n", optopt);
+        }
+    }
+
+    if (command_line.s_param.use_blocking_sockets == 0)
+        not_implemented("Not using blocking sockets.");
+
+    if (optind < argc)
+    {
+        command_line.is_server = 1;
+        command_line.s_param.command = &argv[optind];
+    }
     return 0;
 }
 
-int main(int argn, char * const * argv)
+int main(int argc, char * argv[])
 {
     int res;
 
     default_command_line();
+    parse_opts(argc, argv);
     init_stream_buffer();
 
-    if (argn > 1 && strcmp(argv[1], "-h") == 0)
-        res = showhelp(argv[0]);
-    else if (argn > 1)
-        res = server(argn, argv);
+    if (command_line.is_server)
+        res = server();
     else
         res = client();
     return res;
--- a/main.h	Wed Sep 19 23:54:59 2007 +0200
+++ b/main.h	Fri Sep 21 00:21:20 2007 +0200
@@ -1,18 +1,30 @@
+enum Transport
+{
+    UNIX,
+    TCP,
+    ETHERNET
+};
+
 struct Command_line {
     int is_server;
     char *unix_path; /* path or 0 if not to be used */
+    int tcp_port;
     struct {
-        int serve_tcp; /* how many */
-        int serve_unix; /* how many */
+        int serve_tcp; /* yes/no */
+        int serve_unix; /* yes/no */
+        int max_served; /* how many */
         char run_in_subterminal; /* use opentty */
         char use_blocking_sockets;
         char send_xterm_resize;
         char client_may_close_app_stdin;
-        char *command[]; /* ordono por exec */
+        int detach; /* detach from terminal as nohup */
+        char **command; /* ordono por exec */
     } s_param;
     struct {
+        enum Transport transport;
         unsigned int wait_until_char; /* -1, don't wait. */
         char raw_mode; /* bool. else cooked */
+        char * server_address;
     } c_param;
     int buffer_size;
 };
@@ -39,7 +51,7 @@
 void init_stream_buffer();
 
 /* server.c */
-int server(int argn, char * const argv[]);
+int server();
 
 /* client.c */ 
 int client();
--- a/server.c	Wed Sep 19 23:54:59 2007 +0200
+++ b/server.c	Fri Sep 21 00:21:20 2007 +0200
@@ -59,23 +59,37 @@
     } while(1);
 }
 
-int server(int argn, char * const argv[])
+int server()
 {
     int child;
 
     command_line.is_server = 1;
 
-    child = fork_app(&argv[1]);
-    prepare_user_terminal();
+    child = fork_app(command_line.s_param.command);
+
+    if (command_line.s_param.run_in_subterminal)
+        prepare_user_terminal();
+
+    install_signal_forwarders(child);
 
-    /*install_signal_forwarders(child);*/
+    /* in raw mode, the signals for Control-C, ... will be generated by the
+     * slave pty. The master will receive the key codes. */
+    if (command_line.s_param.serve_unix)
+        s_unix_update_served(command_line.s_param.max_served);
 
-    s_unix_update_served(1);
+    if (command_line.s_param.serve_tcp)
+        not_implemented("Serve tcp.");
 
     loop();
 
-    s_unix_shutdown();
-    restore_user_terminal();
+    if (command_line.s_param.serve_unix)
+        s_unix_shutdown();
+
+    if (command_line.s_param.serve_tcp)
+        not_implemented("Serve tcp.");
+
+    if (command_line.s_param.run_in_subterminal)
+        restore_user_terminal();
 
     return 0;
 }