xref: /haiku/src/add-ons/kernel/file_systems/userlandfs/server/main.cpp (revision b028e77473189065f2baefc6f5e10d451cf591e2)
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
12 static const char* kServerSignature
13 	= "application/x-vnd.haiku.userlandfs-server";
14 
15 // usage
16 static 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 
33 static int kArgC;
34 static char** kArgV;
35 
36 // print_usage
37 void
38 print_usage(bool toStdErr = true)
39 {
40 	fprintf((toStdErr ? stderr : stdout), kUsage, kArgV[0], kArgV[0]);
41 }
42 
43 // main
44 int
45 main(int argc, char** argv)
46 {
47 	kArgC = argc;
48 	kArgV = argv;
49 
50 	// init debugging
51 	init_debugging();
52 	struct DebuggingExiter {
53 		DebuggingExiter()	{}
54 		~DebuggingExiter()	{ exit_debugging(); }
55 	} _;
56 
57 	// parse arguments
58 	int argi = 1;
59 	for (; argi < argc; argi++) {
60 		const char* arg = argv[argi];
61 		int32 argLen = strlen(arg);
62 		if (argLen == 0) {
63 			print_usage();
64 			return 1;
65 		}
66 		if (arg[0] != '-')
67 			break;
68 		if (strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
69 			print_usage(false);
70 			return 0;
71 		} else if (strcmp(arg, "--debug") == 0) {
72 			gServerSettings.SetEnterDebugger(true);
73 		}
74 	}
75 
76 	// get file system, if any
77 	bool dispatcher = true;
78 	const char* fileSystem = NULL;
79 	if (argi < argc) {
80 		fileSystem = argv[argi++];
81 		dispatcher = false;
82 	}
83 	if (argi < argc) {
84 		print_usage();
85 		return 1;
86 	}
87 
88 	// create and init the application
89 	BApplication* app = NULL;
90 	status_t error = B_OK;
91 	if (dispatcher) {
92 		UserlandFSDispatcher* dispatcher
93 			= new(nothrow) UserlandFSDispatcher(kServerSignature);
94 		if (!dispatcher) {
95 			fprintf(stderr, "Failed to create dispatcher.\n");
96 			return 1;
97 		}
98 		error = dispatcher->Init();
99 		app = dispatcher;
100 	} else {
101 		UserlandFSServer* server
102 			= new(nothrow) UserlandFSServer(kServerSignature);
103 		if (!server) {
104 			fprintf(stderr, "Failed to create server.\n");
105 			return 1;
106 		}
107 		error = server->Init(fileSystem);
108 		app = server;
109 	}
110 
111 	// run it, if everything went fine
112 	if (error == B_OK)
113 		app->Run();
114 	delete app;
115 	return 0;
116 }
117