xref: /haiku/src/bin/mimeset.cpp (revision cfc3fa87da824bdf593eb8b817a83b6376e77935)
1 /*
2  * Copyright 2005-2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Application.h>
8 #include <Mime.h>
9 
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 
14 
15 extern const char *__progname;
16 static const char *sProgramName = __progname;
17 
18 // options
19 bool gFiles = true;
20 bool gApps = false;
21 int gForce = 0; // B_UPDATE_MIME_INFO_NO_FORCE;
22 
23 
24 void
25 usage(int status)
26 {
27 	printf("usage: %s [OPTION]... [PATH]...\n"
28 		"  -all\t\tcombine default action and the -apps option\n"
29 		"  -apps\t\tupdate 'app' and 'meta_mime' information\n"
30 		"  -f\t\tforce updating, even if previously updated\n"
31 		"  	\t  (will not overwrite the 'type' of a file)\n"
32 		"  -F\t\tforce updating, even if previously updated\n"
33 		"  	\t  (will overwrite the 'type' of a file)\n"
34 		"  --help\tdisplay this help information\n"
35 		"When PATH is @, file names are read from stdin\n\n",
36 		sProgramName);
37 
38 	exit(status);
39 }
40 
41 
42 status_t
43 process_file(const char *path)
44 {
45 	status_t status = B_OK;
46 
47 	BEntry entry(path);
48 	if (!entry.Exists())
49 		status = B_ENTRY_NOT_FOUND;
50 
51 	if (gFiles && status >= B_OK)
52 		status = update_mime_info(path, true, true, gForce);
53 	if (gApps && status >= B_OK)
54 		status = create_app_meta_mime(path, true, true, gForce);
55 
56 	if (status < B_OK) {
57 		fprintf(stderr, "%s: \"%s\": %s\n",
58 			sProgramName, path, strerror(status));
59 	}
60 	return status;
61 }
62 
63 
64 int
65 main(int argc, char **argv)
66 {
67 	// parse arguments
68 
69 	if (argc < 2)
70 		usage(1);
71 
72 	while (*++argv) {
73 		char *arg = *argv;
74 		if (*arg != '-')
75 			break;
76 
77 		if (!strcmp(arg, "-all"))
78 			gApps = true;
79 		else if (!strcmp(arg, "-apps")) {
80 			gApps = true;
81 			gFiles = false;
82 		} else if (!strcmp(arg, "-f"))
83 			gForce = 1; // B_UPDATE_MIME_INFO_FORCE_KEEP_TYPE;
84 		else if (!strcmp(arg, "-F"))
85 			gForce = 2; // B_UPDATE_MIME_INFO_FORCE_UPDATE_ALL;
86 		else if (!strcmp(arg, "--help"))
87 			usage(0);
88 		else {
89 			fprintf(stderr, "unknown  option \"%s\"\n", arg);
90 			usage(1);
91 		}
92 	}
93 
94 	// process files
95 
96 	BApplication app("application/x-vnd.haiku.mimeset");
97 
98 	while (*argv) {
99 		char *arg = *argv++;
100 
101 		if (!strcmp(arg, "@")) {
102 			// read file names from stdin
103 			char name[B_PATH_NAME_LENGTH];
104 			while (fgets(name, sizeof(name), stdin) != NULL) {
105 				name[strlen(name) - 1] = '\0';
106 					// remove trailing '\n'
107 				if (process_file(name) != B_OK)
108 					exit(1);
109 			}
110 		} else {
111 			if (process_file(arg) != B_OK)
112 				exit(1);
113 		}
114 	}
115 
116 	return 0;
117 }
118 
119