2 Unix SMB/CIFS implementation.
5 Copyright (C) Simo Sorce 2002
6 Copyright (C) Eric Lorimer 2002
8 Most of this code was ripped off of rpcclient.
9 Copyright (C) Tim Potter 2000-2001
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 /* List to hold groups of commands */
30 static struct cmd_list {
31 struct cmd_list *prev, *next;
32 struct cmd_set *cmd_set;
35 TALLOC_CTX *global_ctx;
37 /****************************************************************************
38 handle completion of commands for readline
39 ****************************************************************************/
40 static char **completion_fn(char *text, int start, int end)
42 #define MAX_COMPLETIONS 100
45 struct cmd_list *commands = cmd_list;
48 /* FIXME!!! -- what to do when completing argument? */
49 /* for words not at the start of the line fallback
50 to filename completion */
55 /* make sure we have a list of valid commands */
59 matches = (char **)malloc(sizeof(matches[0])*MAX_COMPLETIONS);
60 if (!matches) return NULL;
62 matches[count++] = strdup(text);
63 if (!matches[0]) return NULL;
65 while (commands && count < MAX_COMPLETIONS-1)
67 if (!commands->cmd_set)
70 for (i=0; commands->cmd_set[i].name; i++)
72 if ((strncmp(text, commands->cmd_set[i].name, strlen(text)) == 0) &&
73 commands->cmd_set[i].fn)
75 matches[count] = strdup(commands->cmd_set[i].name);
82 commands = commands->next;
87 SAFE_FREE(matches[0]);
88 matches[0] = strdup(matches[1]);
90 matches[count] = NULL;
94 static char* next_command (char** cmdstr)
96 static pstring command;
99 if (!cmdstr || !(*cmdstr))
102 p = strchr_m(*cmdstr, ';');
105 pstrcpy(command, *cmdstr);
112 /* Display help on commands */
113 static NTSTATUS cmd_help(struct vfs_state *vfs, TALLOC_CTX *mem_ctx,
114 int argc, char **argv)
116 struct cmd_list *tmp;
117 struct cmd_set *tmp_set;
121 printf("Usage: %s [command]\n", argv[0]);
125 /* Help on one command */
128 for (tmp = cmd_list; tmp; tmp = tmp->next) {
130 tmp_set = tmp->cmd_set;
132 while(tmp_set->name) {
133 if (strequal(argv[1], tmp_set->name)) {
134 if (tmp_set->usage &&
136 printf("%s\n", tmp_set->usage);
138 printf("No help for %s\n", tmp_set->name);
147 printf("No such command: %s\n", argv[1]);
151 /* List all commands */
153 for (tmp = cmd_list; tmp; tmp = tmp->next) {
155 tmp_set = tmp->cmd_set;
157 while(tmp_set->name) {
159 printf("%15s\t\t%s\n", tmp_set->name,
160 tmp_set->description ? tmp_set->description:
170 /* Change the debug level */
171 static NTSTATUS cmd_debuglevel(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
174 printf("Usage: %s [debuglevel]\n", argv[0]);
179 DEBUGLEVEL = atoi(argv[1]);
182 printf("debuglevel is %d\n", DEBUGLEVEL);
187 static NTSTATUS cmd_freemem(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
190 talloc_destroy(global_ctx);
193 vfs->data_size = NULL;
196 static NTSTATUS cmd_quit(struct vfs_state *vfs, TALLOC_CTX *mem_ctx, int argc, char **argv)
199 talloc_destroy(global_ctx);
202 return NT_STATUS_OK; /* NOTREACHED */
205 static struct cmd_set vfstest_commands[] = {
207 { "GENERAL OPTIONS" },
209 { "help", cmd_help, "Get help on commands", "[command]" },
210 { "?", cmd_help, "Get help on commands", "[command]" },
211 { "debuglevel", cmd_debuglevel, "Set debug level", "level" },
212 { "freemem", cmd_freemem, "Free currently allocated buffers", "freemem" },
213 { "exit", cmd_quit, "Exit program", "" },
214 { "quit", cmd_quit, "Exit program", "" },
219 static struct cmd_set separator_command[] = {
220 { "---------------", NULL, "----------------------" },
225 extern struct cmd_set vfs_commands[];
226 static struct cmd_set *vfstest_command_list[] = {
232 static void add_command_set(struct cmd_set *cmd_set)
234 struct cmd_list *entry;
236 if (!(entry = (struct cmd_list *)malloc(sizeof(struct cmd_list)))) {
237 DEBUG(0, ("out of memory\n"));
243 entry->cmd_set = cmd_set;
244 DLIST_ADD(cmd_list, entry);
247 static NTSTATUS do_cmd(struct vfs_state *vfs, struct cmd_set *cmd_entry, char *cmd)
249 char *p = cmd, **argv = NULL;
250 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
254 /* Count number of arguments first time through the loop then
255 allocate memory and strdup them. */
258 while(next_token(&p, buf, " ", sizeof(buf))) {
260 argv[argc] = strdup(buf);
268 /* Create argument list */
270 argv = (char **)malloc(sizeof(char *) * argc);
271 memset(argv, 0, sizeof(char *) * argc);
274 fprintf(stderr, "out of memory\n");
275 result = NT_STATUS_NO_MEMORY;
285 /* Call the function */
289 if (global_ctx == NULL) {
291 if (!(global_ctx = talloc_init())) {
292 DEBUG(0, ("talloc_init() failed\n"));
298 result = cmd_entry->fn(vfs, global_ctx, argc, argv);
301 fprintf (stderr, "Invalid command\n");
310 for (i = 0; i < argc; i++)
319 /* Process a command entered at the prompt or as part of -c */
320 static NTSTATUS process_cmd(struct vfs_state *vfs, char *cmd)
322 struct cmd_list *temp_list;
326 NTSTATUS result = NT_STATUS_OK;
329 if (cmd[strlen(cmd) - 1] == '\n')
330 cmd[strlen(cmd) - 1] = '\0';
332 if (!next_token(&p, buf, " ", sizeof(buf))) {
336 /* strip the trainly \n if it exsists */
338 if (buf[len-1] == '\n')
341 /* Search for matching commands */
343 for (temp_list = cmd_list; temp_list; temp_list = temp_list->next) {
344 struct cmd_set *temp_set = temp_list->cmd_set;
346 while(temp_set->name) {
347 if (strequal(buf, temp_set->name)) {
349 result = do_cmd(vfs, temp_set, cmd);
358 if (!found && buf[0]) {
359 printf("command not found: %s\n", buf);
363 if (!NT_STATUS_IS_OK(result)) {
364 printf("result was %s\n", nt_errstr(result));
370 void exit_server(char *reason)
372 DEBUG(3,("Server exit (%s)\n", (reason ? reason : "")));
376 static int server_fd = -1;
377 int last_message = -1;
379 int smbd_server_fd(void)
384 BOOL reload_services(BOOL test)
389 /* Print usage information */
390 static void usage(void)
392 printf("Usage: vfstest [options]\n");
394 printf("\t-c or --command \"command string\" execute semicolon separated cmds\n");
395 printf("\t-d or --debug debuglevel set the debuglevel\n");
396 printf("\t-l or --logfile logfile logfile to use instead of stdout\n");
397 printf("\t-h or --help Print this help message.\n");
403 int main(int argc, char *argv[])
405 extern pstring global_myname;
406 static int got_pass = 0;
407 BOOL interactive = True;
410 static char *cmdstr = "";
412 struct cli_state *cli;
416 static char *opt_authfile=NULL,
419 *opt_configfile=NULL,
421 static int opt_debuglevel;
423 struct cmd_set **cmd_set;
424 struct in_addr server_ip;
426 extern BOOL AllowDebugChange;
427 static struct vfs_state vfs;
431 /* make sure the vars that get altered (4th field) are in
432 a fixed location or certain compilers complain */
434 struct poptOption long_options[] = {
435 /* {"conf", 's', POPT_ARG_STRING, &opt_configfile, 's'},*/
436 {"debug", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
437 {"debuglevel", 'd', POPT_ARG_INT, &opt_debuglevel, 'd'},
438 /* {"user", 'U', POPT_ARG_STRING, &opt_username, 'U'},*/
439 {"command", 'c', POPT_ARG_STRING, &cmdstr},
440 {"logfile", 'l', POPT_ARG_STRING, &opt_logfile, 'l'},
441 {"help", 'h', POPT_ARG_NONE, 0, 'h'},
449 AllowDebugChange = False;
451 pc = poptGetContext("vfstest", argc, (const char **) argv,
454 while((opt = poptGetNextOpt(pc)) != -1) {
457 slprintf(logfile, sizeof(logfile) - 1, "%s.client",
459 lp_set_logfile(logfile);
464 DEBUGLEVEL = opt_debuglevel;
471 pstrcpy(username,opt_username);
473 if ((lp=strchr_m(username,'%'))) {
475 pstrcpy(password,lp+1);
477 memset(strchr_m(opt_username,'%') + 1, 'X',
494 /* the following functions are part of the Samba debugging
495 facilities. See lib/debug.c */
496 setup_logging("vfstest", interactive);
500 /* Load command lists */
502 cmd_set = vfstest_command_list;
505 add_command_set(*cmd_set);
506 add_command_set(separator_command);
510 /* some basic initialization stuff */
511 vfs.conn = (struct connection_struct *)malloc(sizeof(struct connection_struct));
512 vfs.conn->user = "vfstest";
513 for (i=0; i < 1024; i++)
516 /* Do anything specified with -c */
521 while((cmd=next_command(&p)) != NULL) {
522 process_cmd(&vfs, cmd);
529 vfs.conn->vfs_private = NULL;
530 smbd_vfs_init(vfs.conn);
532 /* Loop around accepting commands */
538 slprintf(prompt, sizeof(prompt) - 1, "vfstest $> ");
540 line = smb_readline(prompt, NULL, completion_fn);
546 process_cmd(&vfs, line);