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