• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 #define DBUS_COMPILATION
4 #include <dbus/dbus-internals.h>
5 #include <dbus/dbus-list.h>
6 #include <dbus/dbus-memory.h>
7 #include <dbus/dbus-shell.h>
8 #include <dbus/dbus-string.h>
9 #include <dbus/dbus-sysdeps.h>
10 
11 static dbus_bool_t
test_command_line(const char * arg1,...)12 test_command_line (const char *arg1, ...)
13 {
14   int i, original_argc, shell_argc;
15   char **shell_argv;
16   char **original_argv;
17   char *command_line, *tmp;
18   DBusString str;
19   DBusList *list = NULL, *node;
20   va_list var_args;
21   DBusError error;
22 
23   va_start (var_args, arg1);
24   _dbus_list_append (&list, (char *)arg1);
25   do
26     {
27       tmp = va_arg (var_args, char *);
28       if (!tmp)
29         break;
30       _dbus_list_append (&list, tmp);
31     } while (tmp);
32   va_end (var_args);
33 
34   original_argc = _dbus_list_get_length (&list);
35   original_argv = dbus_new (char *, original_argc);
36   _dbus_string_init (&str);
37   for (i = 0, node = _dbus_list_get_first_link (&list); i < original_argc && node;
38        i++, node = _dbus_list_get_next_link (&list, node))
39     {
40       original_argv[i] = node->data;
41       if (i > 0)
42         _dbus_string_append_byte (&str, ' ');
43       _dbus_string_append (&str, original_argv[i]);
44     }
45 
46   _dbus_list_clear (&list);
47   command_line = _dbus_string_get_data (&str);
48   printf ("\n\nTesting command line '%s'\n", command_line);
49 
50   dbus_error_init (&error);
51   if (!_dbus_shell_parse_argv (command_line, &shell_argc, &shell_argv, &error))
52     {
53       fprintf (stderr, "Error parsing command line: %s\n", error.message ? error.message : "");
54       return FALSE;
55     }
56   else
57     {
58       if (shell_argc != original_argc)
59         {
60           printf ("Number of arguments returned (%d) don't match original (%d)\n",
61                   shell_argc, original_argc);
62           return FALSE;
63         }
64       printf ("Number of arguments: %d\n", shell_argc);
65       for (i = 0; i < shell_argc; i++)
66         {
67           char *unquoted;
68 
69           unquoted = _dbus_shell_unquote (original_argv[i]);
70           if (strcmp (unquoted ? unquoted : "",
71                       shell_argv[i] ? shell_argv[i] : ""))
72             {
73               printf ("Position %d, returned argument (%s) does not match original (%s)\n",
74                       i, shell_argv[i], unquoted);
75               dbus_free (unquoted);
76               return FALSE;
77             }
78           dbus_free (unquoted);
79           if (shell_argv[i])
80             printf ("Argument %d = %s\n", i, shell_argv[i]);
81         }
82 
83       dbus_free_string_array (shell_argv);
84     }
85 
86   _dbus_string_free (&str);
87 
88   return TRUE;
89 }
90 
91 int
main(int argc,char ** argv)92 main (int argc, char **argv)
93 {
94   if (!test_command_line ("command", "-s", "--force-shutdown", "\"a string\"", "123", NULL)
95       || !test_command_line ("command", "-s", NULL)
96       || !test_command_line ("/opt/gnome/bin/service-start", NULL)
97       || !test_command_line ("grep", "-l", "-r", "-i", "'whatever'", "files*.c", NULL)
98       || !test_command_line ("/home/boston/johnp/devel-local/dbus/test/test-segfault", NULL)
99       || !test_command_line ("ls", "-l", "-a", "--colors", _dbus_get_tmpdir(), NULL)
100       || !test_command_line ("rsync-to-server", NULL)
101       || !test_command_line ("test-segfault", "--no-segfault", NULL)
102       || !test_command_line ("evolution", "mailto:pepe@cuco.com", NULL)
103       || !test_command_line ("run", "\"a \n multiline\"", NULL)
104       || test_command_line ("ls", "\"a wrong string'", NULL) /* invalid command line */ )
105     return -1;
106 
107   return 0;
108 }
109