xref: /haiku/src/bin/mimeset.cpp (revision 80d75f15dfa48ebea421c6b2c19a5296cc63d7eb)
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;
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 		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;
84 		else if (!strcmp(arg, "-F"))
85 			gForce = 2;
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 	int err = 0;
99 	while (*argv) {
100 		char *arg = *argv++;
101 
102 		if (!strcmp(arg, "@")) {
103 			// read file names from stdin
104 			char name[B_PATH_NAME_LENGTH];
105 			while (fgets(name, sizeof(name), stdin) != NULL) {
106 				name[strlen(name) - 1] = '\0';
107 					// remove trailing '\n'
108 				err = process_file(name);
109 				if (err)
110 					exit(err);
111 			}
112 		} else {
113 			err = process_file(arg);
114 			if (err)
115 				exit(err);
116 		}
117 	}
118 
119 	return 0;
120 }
121 
122