cef8f8
autofs-5.1.3 - refactor amd_parse.c
cef8f8
cef8f8
From: Ian Kent <raven@themaw.net>
cef8f8
cef8f8
Refactor some blocks of code into functions to allow later reuse.
cef8f8
cef8f8
Signed-off-by: Ian Kent <raven@themaw.net>
cef8f8
---
cef8f8
 CHANGELOG           |    1 
cef8f8
 modules/amd_parse.y |  293 ++++++++++++++++++++++++++++++++++------------------
cef8f8
 2 files changed, 197 insertions(+), 97 deletions(-)
cef8f8
cef8f8
--- autofs-5.0.7.orig/CHANGELOG
cef8f8
+++ autofs-5.0.7/CHANGELOG
cef8f8
@@ -273,6 +273,7 @@
cef8f8
 - fix symlink false negative in umount_multi().
cef8f8
 - remove expand_selectors() on amd parser entry.
cef8f8
 - fix amd defaults map entry handling.
cef8f8
+- refactor amd_parse.c.
cef8f8
 
cef8f8
 25/07/2012 autofs-5.0.7
cef8f8
 =======================
cef8f8
--- autofs-5.0.7.orig/modules/amd_parse.y
cef8f8
+++ autofs-5.0.7/modules/amd_parse.y
cef8f8
@@ -55,6 +55,11 @@ static int make_selector(char *name,
cef8f8
 			 unsigned int compare);
cef8f8
 static void add_selector(struct selector *selector);
cef8f8
 
cef8f8
+static int match_map_option_fs_type(char *map_option, char *type);
cef8f8
+static int match_map_option_map_type(char *map_option, char *type);
cef8f8
+static int match_map_option_cache_option(char *type);
cef8f8
+static int match_mnt_option_options(char *mnt_option, char *options);
cef8f8
+
cef8f8
 static struct amd_entry entry;
cef8f8
 static struct list_head *entries;
cef8f8
 static struct autofs_point *pap;
cef8f8
@@ -253,86 +258,13 @@ selection: SELECTOR IS_EQUAL SELECTOR_VA
cef8f8
 
cef8f8
 option_assignment: MAP_OPTION OPTION_ASSIGN FS_TYPE
cef8f8
 	{
cef8f8
-		if (!strcmp($3, "auto")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_AUTO;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "nfs") ||
cef8f8
-			   !strcmp($3, "nfs4")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_NFS;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "nfsl")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_NFSL;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "link")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_LINK;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "linkx")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_LINKX;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "host")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_HOST;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "lofs")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_LOFS;
cef8f8
-			entry.type = amd_strdup("bind");
cef8f8
-		} else if (!strcmp($3, "xfs")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_XFS;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "ext2") ||
cef8f8
-			   !strcmp($3, "ext3") ||
cef8f8
-			   !strcmp($3, "ext4")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_EXT;
cef8f8
-			entry.type = amd_strdup($3);
cef8f8
-		} else if (!strcmp($3, "ufs")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_UFS;
cef8f8
-			entry.type = conf_amd_get_linux_ufs_mount_type();
cef8f8
-		} else if (!strcmp($3, "cdfs")) {
cef8f8
-			entry.flags |= AMD_MOUNT_TYPE_CDFS;
cef8f8
-			entry.type = amd_strdup("iso9660");
cef8f8
-		} else if (!strcmp($3, "jfs") ||
cef8f8
-			   !strcmp($3, "nfsx") ||
cef8f8
-			   !strcmp($3, "program") ||
cef8f8
-			   !strcmp($3, "lustre") ||
cef8f8
-			   !strcmp($3, "direct")) {
cef8f8
-			sprintf(msg_buf, "file system type %s is "
cef8f8
-					 "not yet implemented", $3);
cef8f8
-			amd_msg(msg_buf);
cef8f8
-			YYABORT;
cef8f8
-		} else if (!strcmp($3, "cachefs")) {
cef8f8
-			sprintf(msg_buf, "file system %s is not "
cef8f8
-					 "supported by autofs, ignored", $3);
cef8f8
-			amd_msg(msg_buf);
cef8f8
-		} else {
cef8f8
-			amd_notify($1);
cef8f8
+		if (!match_map_option_fs_type($1, $3))
cef8f8
 			YYABORT;
cef8f8
-		}
cef8f8
 	}
cef8f8
 	| MAP_OPTION OPTION_ASSIGN MAP_TYPE
cef8f8
 	{
cef8f8
-		if (!strcmp($3, "file") ||
cef8f8
-		    !strcmp($3, "nis") ||
cef8f8
-		    !strcmp($3, "nisplus") ||
cef8f8
-		    !strcmp($3, "ldap") ||
cef8f8
-		    !strcmp($3, "hesiod"))
cef8f8
-			entry.map_type = amd_strdup($3);
cef8f8
-		else if (!strcmp($3, "exec"))
cef8f8
-			/* autofs uses "program" for "exec" map type */
cef8f8
-			entry.map_type = amd_strdup("program");
cef8f8
-		else if (!strcmp($3, "passwd")) {
cef8f8
-			sprintf(msg_buf, "map type %s is "
cef8f8
-					 "not yet implemented", $3);
cef8f8
-			amd_msg(msg_buf);
cef8f8
-			YYABORT;
cef8f8
-		} else if (!strcmp($3, "ndbm") ||
cef8f8
-			   !strcmp($3, "union")) {
cef8f8
-			sprintf(msg_buf, "map type %s is not "
cef8f8
-					 "supported by autofs", $3);
cef8f8
-			amd_msg(msg_buf);
cef8f8
+		if (!match_map_option_map_type($1, $3))
cef8f8
 			YYABORT;
cef8f8
-		} else {
cef8f8
-			amd_notify($1);
cef8f8
-			YYABORT;
cef8f8
-		}
cef8f8
 	}
cef8f8
 	| MAP_OPTION OPTION_ASSIGN FS_OPT_VALUE
cef8f8
 	{
cef8f8
@@ -397,13 +329,7 @@ option_assignment: MAP_OPTION OPTION_ASS
cef8f8
 	}
cef8f8
 	| MNT_OPTION OPTION_ASSIGN options
cef8f8
 	{
cef8f8
-		if (!strcmp($1, "opts"))
cef8f8
-			entry.opts = amd_strdup(opts);
cef8f8
-		else if (!strcmp($1, "addopts"))
cef8f8
-			entry.addopts = amd_strdup(opts);
cef8f8
-		else if (!strcmp($1, "remopts"))
cef8f8
-			entry.remopts = amd_strdup(opts);
cef8f8
-		else {
cef8f8
+		if (!match_mnt_option_options($1, $3)) {
cef8f8
 			amd_notify($1);
cef8f8
 			YYABORT;
cef8f8
 		}
cef8f8
@@ -412,27 +338,20 @@ option_assignment: MAP_OPTION OPTION_ASS
cef8f8
 	| MNT_OPTION OPTION_ASSIGN
cef8f8
 	{
cef8f8
 		memset(opts, 0, sizeof(opts));
cef8f8
-		if (!strcmp($1, "opts"))
cef8f8
-			entry.opts = amd_strdup("");
cef8f8
-		else if (!strcmp($1, "addopts"))
cef8f8
-			entry.addopts = amd_strdup("");
cef8f8
-		else if (!strcmp($1, "remopts"))
cef8f8
-			entry.remopts = amd_strdup("");
cef8f8
-		else {
cef8f8
+		if (!match_mnt_option_options($1, "")) {
cef8f8
 			amd_notify($1);
cef8f8
 			YYABORT;
cef8f8
 		}
cef8f8
 	}
cef8f8
 	| MAP_OPTION OPTION_ASSIGN CACHE_OPTION
cef8f8
 	{
cef8f8
-		if (strncmp($3, "inc", 3))
cef8f8
-			entry.cache_opts = AMD_CACHE_OPTION_INC;
cef8f8
-		else if (strncmp($3, "all", 3))
cef8f8
-			entry.cache_opts = AMD_CACHE_OPTION_ALL;
cef8f8
-		else if (strncmp($3, "re", 2))
cef8f8
-			entry.cache_opts = AMD_CACHE_OPTION_REGEXP;
cef8f8
-		if (strstr($3, "sync"))
cef8f8
-			entry.cache_opts |= AMD_CACHE_OPTION_SYNC;
cef8f8
+		if (!strcmp($1, "cache")) {
cef8f8
+			if (!match_map_option_cache_option($3))
cef8f8
+				YYABORT;
cef8f8
+		} else {
cef8f8
+			amd_notify($1);
cef8f8
+			YYABORT;
cef8f8
+		}
cef8f8
 	}
cef8f8
 	;
cef8f8
 
cef8f8
@@ -478,6 +397,186 @@ options: OPTION
cef8f8
 
cef8f8
 %%
cef8f8
 
cef8f8
+static int match_map_option_fs_type(char *map_option, char *type)
cef8f8
+{
cef8f8
+	char *fs_type;
cef8f8
+
cef8f8
+	fs_type = amd_strdup(type);
cef8f8
+	if (!fs_type) {
cef8f8
+		amd_notify(type);
cef8f8
+		return 0;
cef8f8
+	}
cef8f8
+
cef8f8
+	if (!strcmp(fs_type, "auto")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_AUTO;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "nfs") ||
cef8f8
+		   !strcmp(fs_type, "nfs4")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_NFS;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "nfsl")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_NFSL;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "link")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_LINK;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "linkx")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_LINKX;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "host")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_HOST;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "lofs")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_LOFS;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "xfs")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_XFS;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "ext2") ||
cef8f8
+		   !strcmp(fs_type, "ext3") ||
cef8f8
+		   !strcmp(fs_type, "ext4")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_EXT;
cef8f8
+		entry.type = fs_type;
cef8f8
+	} else if (!strcmp(fs_type, "ufs")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_UFS;
cef8f8
+		entry.type = conf_amd_get_linux_ufs_mount_type();
cef8f8
+		if (!entry.type) {
cef8f8
+			amd_msg("memory allocation error");
cef8f8
+			amd_notify(type);
cef8f8
+			free(fs_type);
cef8f8
+			return 0;
cef8f8
+		}
cef8f8
+		free(fs_type);
cef8f8
+	} else if (!strcmp(fs_type, "cdfs")) {
cef8f8
+		entry.flags |= AMD_MOUNT_TYPE_CDFS;
cef8f8
+		entry.type = amd_strdup("iso9660");
cef8f8
+		if (!entry.type) {
cef8f8
+			amd_msg("memory allocation error");
cef8f8
+			amd_notify(map_option);
cef8f8
+			free(fs_type);
cef8f8
+			return 0;
cef8f8
+		}
cef8f8
+		free(fs_type);
cef8f8
+	} else if (!strcmp(fs_type, "jfs") ||
cef8f8
+		   !strcmp(fs_type, "nfsx") ||
cef8f8
+		   !strcmp(fs_type, "program") ||
cef8f8
+		   !strcmp(fs_type, "lustre") ||
cef8f8
+		   !strcmp(fs_type, "direct")) {
cef8f8
+		sprintf(msg_buf, "file system type %s is "
cef8f8
+				 "not yet implemented", fs_type);
cef8f8
+		amd_msg(msg_buf);
cef8f8
+		free(fs_type);
cef8f8
+		return 0;
cef8f8
+	} else if (!strcmp(fs_type, "cachefs")) {
cef8f8
+		sprintf(msg_buf, "file system %s is not "
cef8f8
+				 "supported by autofs, ignored",
cef8f8
+				 fs_type);
cef8f8
+		amd_msg(msg_buf);
cef8f8
+		free(fs_type);
cef8f8
+	} else {
cef8f8
+		amd_notify(fs_type);
cef8f8
+		free(fs_type);
cef8f8
+		return 0;
cef8f8
+	}
cef8f8
+
cef8f8
+	return 1;
cef8f8
+}
cef8f8
+
cef8f8
+static int match_map_option_map_type(char *map_option, char *type)
cef8f8
+{
cef8f8
+	char *map_type;
cef8f8
+
cef8f8
+	map_type = amd_strdup(type);
cef8f8
+	if (!map_type) {
cef8f8
+		amd_notify(type);
cef8f8
+		return 0;
cef8f8
+	}
cef8f8
+
cef8f8
+	if (!strcmp(map_type, "file") ||
cef8f8
+	    !strcmp(map_type, "nis") ||
cef8f8
+	    !strcmp(map_type, "nisplus") ||
cef8f8
+	    !strcmp(map_type, "ldap") ||
cef8f8
+	    !strcmp(map_type, "hesiod")) {
cef8f8
+		entry.map_type = map_type;
cef8f8
+	} else if (!strcmp(map_type, "exec")) {
cef8f8
+		/* autofs uses "program" for "exec" map type */
cef8f8
+		entry.map_type = amd_strdup("program");
cef8f8
+		if (!entry.map_type) {
cef8f8
+			amd_notify(type);
cef8f8
+			free(map_type);
cef8f8
+			return 0;
cef8f8
+		}
cef8f8
+		free(map_type);
cef8f8
+	} else if (!strcmp(map_type, "passwd")) {
cef8f8
+		sprintf(msg_buf, "map type %s is "
cef8f8
+				 "not yet implemented", map_type);
cef8f8
+		amd_msg(msg_buf);
cef8f8
+		free(map_type);
cef8f8
+		return 0;
cef8f8
+	} else if (!strcmp(map_type, "ndbm") ||
cef8f8
+		   !strcmp(map_type, "union")) {
cef8f8
+		sprintf(msg_buf, "map type %s is not "
cef8f8
+				 "supported by autofs", map_type);
cef8f8
+		amd_msg(msg_buf);
cef8f8
+		free(map_type);
cef8f8
+		return 0;
cef8f8
+	} else {
cef8f8
+		amd_notify(type);
cef8f8
+		free(map_type);
cef8f8
+		return 0;
cef8f8
+	}
cef8f8
+
cef8f8
+	return 1;
cef8f8
+}
cef8f8
+
cef8f8
+static int match_map_option_cache_option(char *type)
cef8f8
+{
cef8f8
+	char *cache_opt;
cef8f8
+
cef8f8
+	cache_opt = amd_strdup(type);
cef8f8
+	if (!cache_opt) {
cef8f8
+		amd_notify(type);
cef8f8
+		return 0;
cef8f8
+	}
cef8f8
+
cef8f8
+	if (strncmp(cache_opt, "inc", 3))
cef8f8
+		entry.cache_opts = AMD_CACHE_OPTION_INC;
cef8f8
+	else if (strncmp(cache_opt, "all", 3))
cef8f8
+		entry.cache_opts = AMD_CACHE_OPTION_ALL;
cef8f8
+	else if (strncmp(cache_opt, "re", 2))
cef8f8
+		entry.cache_opts = AMD_CACHE_OPTION_REGEXP;
cef8f8
+	if (strstr(cache_opt, "sync"))
cef8f8
+		entry.cache_opts |= AMD_CACHE_OPTION_SYNC;
cef8f8
+	free(cache_opt);
cef8f8
+
cef8f8
+	return 1;
cef8f8
+}
cef8f8
+
cef8f8
+static int match_mnt_option_options(char *mnt_option, char *options)
cef8f8
+{
cef8f8
+	char *tmp;
cef8f8
+
cef8f8
+	if (!strcmp(mnt_option, "opts")) {
cef8f8
+		tmp = amd_strdup(options);
cef8f8
+		if (!tmp)
cef8f8
+			return 0;
cef8f8
+		entry.opts = tmp;
cef8f8
+	} else if (!strcmp(mnt_option, "addopts")) {
cef8f8
+		tmp = amd_strdup(options);
cef8f8
+		if (!tmp)
cef8f8
+			return 0;
cef8f8
+		entry.addopts = tmp;
cef8f8
+	} else if (!strcmp(mnt_option, "remopts")) {
cef8f8
+		tmp = amd_strdup(options);
cef8f8
+		if (!tmp)
cef8f8
+			return 0;
cef8f8
+		entry.remopts = tmp;
cef8f8
+	} else
cef8f8
+		return 0;
cef8f8
+
cef8f8
+	return 1;
cef8f8
+}
cef8f8
+
cef8f8
 static void prepend_opt(char *dest, char *opt)
cef8f8
 {
cef8f8
 	char new[MAX_OPTS_LEN];