shithub: pokecrystal

Download patch

ref: fe38946b0e2717c973839a4f8db647de0f1d9f3a
parent: 27ba16d827a4a50a1786c32691c2093bd05b1a2a
author: yenatch <yenatch@gmail.com>
date: Sat May 27 21:12:16 EDT 2017

scan_includes: add --strict so it doesn't have to be default

--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -2,12 +2,24 @@
 #include <stdlib.h>
 #include <string.h>
 #include <stdbool.h>
+#include <getopt.h>
 
 void usage(void) {
-	printf("Usage: scan_includes filename\n");
-	exit(1);
+	printf("Usage: scan_includes [-h] [-s] filename\n"
+	       "-h, --help\n"
+	       "    Print usage and exit\n"
+	       "-s, --strict\n"
+	       "    Fail if a file cannot be read\n");
 }
 
+struct Options {
+	bool help;
+	bool strict;
+};
+
+struct Options Options = {0};
+
+
 void scan_file(char* filename) {
 	FILE* f;
 	long size;
@@ -17,9 +29,13 @@
 	int length;
 
 	f = fopen(filename, "r");
-	if (f == NULL) {
-		fprintf(stderr, "Could not open file: '%s'\n", filename);
-		exit(1);
+	if (!f) {
+		if (Options.strict) {
+			fprintf(stderr, "Could not open file: '%s'\n", filename);
+			exit(1);
+		} else {
+			return;
+		}
 	}
 
 	fseek(f, 0, SEEK_END);
@@ -68,10 +84,43 @@
 	free(orig);
 }
 
+void get_args(int argc, char *argv[]) {
+	while (1) {
+		struct option long_options[] = {
+			{"strict", no_argument, 0, 's'},
+			{"help", no_argument, 0, 'h'},
+			{0}
+		};
+		int i = 0;
+		int opt = getopt_long(argc, argv, "sh", long_options, &i);
+
+		if (opt == -1) {
+			break;
+		}
+
+		switch (opt) {
+		case 's':
+			Options.strict = true;
+			break;
+		case 'h':
+			Options.help = true;
+			break;
+		}
+	}
+}
+
 int main(int argc, char* argv[]) {
-	if (argc < 2) {
+	get_args(argc, argv);
+	argc -= optind;
+	argv += optind;
+	if (Options.help) {
 		usage();
+		return 0;
 	}
-	scan_file(argv[1]);
+	if (argc < 1) {
+		usage();
+		exit(1);
+	}
+	scan_file(argv[0]);
 	return 0;
 }