1// main.cpp
2
3#include <stdio.h>
4#include <string.h>
5
6#include "Debug.h"
7#include "ServerDefs.h"
8#include "UserlandFSDispatcher.h"
9#include "UserlandFSServer.h"
10
11// server signature
12static const char* kServerSignature
13	= "application/x-vnd.bonefish.userlandfs-server";
14
15// usage
16static const char* kUsage =
17"Usage: %s <options>\n"
18"       %s <options> <file system>\n"
19"\n"
20"The first version runs the server as the dispatcher, i.e. as the singleton\n"
21"app the kernel add-on contacts when it is looking for a file system.\n"
22"The dispatcher uses the second version to start a server for a specific file\n"
23"system.\n"
24"\n"
25"Options:\n"
26"  --debug     - the file system server enters the debugger after the\n"
27"                userland file system add-on has been loaded and is\n"
28"                ready to be used. If specified for the dispatcher, it\n"
29"                passes the flag to all file system servers it starts.\n"
30"  -h, --help  - print this text\n"
31;
32
33static int kArgC;
34static char** kArgV;
35
36// print_usage
37void
38print_usage(bool toStdErr = true)
39{
40	fprintf((toStdErr ? stderr : stdout), kUsage, kArgV[0], kArgV[0]);
41}
42
43// main
44int
45main(int argc, char** argv)
46{
47	kArgC = argc;
48	kArgV = argv;
49	// init debugging
50	init_debugging();
51	struct DebuggingExiter {
52		DebuggingExiter()	{}
53		~DebuggingExiter()	{ exit_debugging(); }
54	} _;
55	// parse arguments
56	int argi = 1;
57	// parse options
58	for (; argi < argc; argi++) {
59		const char* arg = argv[argi];
60		int32 argLen = strlen(arg);
61		if (argLen == 0) {
62			print_usage();
63			return 1;
64		}
65		if (arg[0] != '-')
66			break;
67		if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
68			print_usage(false);
69			return 0;
70		} else if (strcmp(arg, "--debug") == 0) {
71			gServerSettings.SetEnterDebugger(true);
72		}
73	}
74	// get file system, if any
75	bool dispatcher = true;
76	const char* fileSystem = NULL;
77	if (argi < argc) {
78		fileSystem = argv[argi++];
79		dispatcher = false;
80	}
81	if (argi < argc) {
82		print_usage();
83		return 1;
84	}
85	// create and init the application
86	BApplication* app = NULL;
87	status_t error = B_OK;
88	if (dispatcher) {
89		UserlandFSDispatcher* dispatcher
90			= new(nothrow) UserlandFSDispatcher(kServerSignature);
91		if (!dispatcher) {
92			fprintf(stderr, "Failed to create dispatcher.\n");
93			return 1;
94		}
95		error = dispatcher->Init();
96		app = dispatcher;
97	} else {
98		UserlandFSServer* server
99			= new(nothrow) UserlandFSServer(kServerSignature);
100		if (!server) {
101			fprintf(stderr, "Failed to create server.\n");
102			return 1;
103		}
104		error = server->Init(fileSystem);
105		app = server;
106	}
107	// run it, if everything went fine
108	if (error == B_OK)
109		app->Run();
110	delete app;
111	return 0;
112}
113
114