Commits

Bryan O'Sullivan committed 537d0a8

Added getopt bits. Now try to parse real config file.

Comments (0)

Files changed (3)

 }
 
 
+int
+save_pattern(char *name)
+{
+    int len = strlen(name);
+
+    if (len == 0) {
+	return 0;
+    }
+
+    int x = fnmatch(name, "eth0", 0);
+
+    if (x != 0 && x != FNM_NOMATCH) {
+	return -1;
+    }
+	
+    struct if_pat *pat = xmalloc(sizeof(*pat));
+
+    pat->pat = xmalloc(len + 1);
+    memcpy(pat->pat, name, len + 1);
+    pat->next = pats;
+    pats = pat->next;
+
+    return 0;
+}
+
+
 void
 read_config(char *filename)
 {
 	fp = stdin;
     } else if ((fp = fopen(filename, "r")) == NULL) {
 	perror(filename);
-	exit(1);
+	return;
     }
 
     char buf[8192];
 	    *h = '\0';
 	}
 
-	int len = strlen(l);
-
-	if (len == 0) {
-	    continue;
-	}
-
-	int x = fnmatch(l, "eth0", 0);
-
-	if (x != 0 && x != FNM_NOMATCH) {
-	    fprintf(stderr, "%s:%d:bad pattern: %s\n", filename, line, l);
+	if (save_pattern(l) == -1) {
+	    fprintf(stderr, "%s, line %d: bad pattern: %s\n",
+		    filename, line, l);
 	    exit(1);
 	}
-	
-	struct if_pat *pat = xmalloc(sizeof(*pat));
-
-	pat->pat = xmalloc(len + 1);
-	memcpy(pat->pat, l, len + 1);
-	pat->next = pats;
-	pats = pat->next;
     }
     
     if (ferror(fp)) {
 #include <stdio.h>
 #include <stdlib.h>
+#include <unistd.h>
 
 #include "netplug.h"
 
 
-int handle_interface(struct nlmsghdr *hdr, void *arg)
+static int
+handle_interface(struct nlmsghdr *hdr, void *arg)
 {
     if (hdr->nlmsg_type != RTM_NEWLINK && hdr->nlmsg_type != RTM_DELLINK) {
 	return 0;
 }
 
 
+static void
+usage(int exitcode)
+{
+    fprintf(stderr, "Usage: netplug [-c config_file]\n");
+    exit(exitcode);
+}
+
+
 int
 main(int argc, char *argv[])
 {
-    read_config("-");
+    int c;
+    char *cfg_file = "/etc/netplug/netplug.conf";
+
+    while ((c = getopt(argc, argv, "c:hi:")) != EOF) {
+	switch (c) {
+	case 'c':
+	    cfg_file = optarg;
+	    break;
+	case 'h':
+	    usage(0);
+	case 'i':
+	    if (save_pattern(optarg) == -1) {
+		fprintf(stderr, "Bad pattern for '-i %s'\n", optarg);
+		exit(1);
+	    }
+	    break;
+	case '?':
+	    usage(1);
+	}
+    }
+    
+    read_config(cfg_file);
     
     int fd = netlink_open();
 
 /* configuration */
 
 void read_config(char *filename);
+int save_pattern(char *pat);
 int if_match(char *name);