Hello!

After seeing this patch from Justus Winter:

http://lists.gnu.org/archive/html/bug-hurd/2013-06/msg00120.html

I wrote a Coccinelle script that finds similar errors:

<smpl>
@find_struct@
identifier argp_s;
identifier argp_parser_func;
identifier options;
@@
(
struct argp argp_s = {
        .parser = argp_parser_func,
};
|
struct argp argp_s = { options, argp_parser_func, ... };
|
argp_s->parser = argp_parser_func;
|
argp_s.parser = argp_parser_func;
)

@depends on find_struct@
identifier find_struct.argp_parser_func;
identifier state;
expression E;
@@
argp_parser_func (..., struct argp_state *state)
{
<...
- error(...,
+ argp_error(state,
E);
...>
}
</smpl>

I found 4 programs that could be improved by gracefully handling errors using argp_error: notice (hurd-extras), mboxfs (hurd-extras), tarfs (incubator) and unionfs. The patches are attached.


Cyril Roelandt.
diff -u -p a/mboxfs/mboxfs.c b/mboxfs/mboxfs.c
--- a/mboxfs/mboxfs.c
+++ b/mboxfs/mboxfs.c
@@ -98,7 +98,7 @@ mboxfs_parse_opts (int key, char *arg, s
     case ARGP_KEY_ARG:
       mboxfs_options.file_name = strdup (arg);
       if (!mboxfs_options.file_name || !strlen (mboxfs_options.file_name))
-	error (1, 1, "No mailbox specified.");
+	argp_error(sate, "No mailbox specified.");
   }
 
   return 0;
diff -u -p a/notice/notice.c b/notice/notice.c
--- a/notice/notice.c
+++ b/notice/notice.c
@@ -137,14 +137,14 @@ parse_opt (int key, char *arg, struct ar
 	/* Open the file.  */
 	file = file_name_lookup (arg, 0, 0);
 	if (file == MACH_PORT_NULL)
-	  error (1, errno, "Could not open %s", arg);
+	  argp_error(state, arg);
 
 	/* Create the notification port.  */
 	err = ports_create_port (class, bucket,
 				 sizeof (struct notice_handle) + strlen (arg),
 				 &notice);
 	if (err)
-	  error (1, err, "Could not allocate port");
+	  argp_error(state, "Could not allocate port");
 	/* Record the file name and the changes we are interested in.  */
 	strcpy (notice->name, arg);
 	notice->changes = changes;
@@ -153,7 +153,7 @@ parse_opt (int key, char *arg, struct ar
 	err = file_notice_changes (file, ports_get_right (notice),
 				   MACH_MSG_TYPE_MAKE_SEND);
 	if (err)
-	  error (1, err, "Noticing changes not possible for %s", arg);
+	  argp_error(state, arg);
 	break;
       }
 
diff -u -p a/tarfs.c b/tarfs.c
--- a/tarfs.c
+++ b/tarfs.c
@@ -211,7 +211,7 @@ tarfs_parse_opts (int key, char *arg, st
     case ARGP_KEY_ARG:
       tarfs_options.file_name = strdup (arg);
       if (!tarfs_options.file_name || !strlen (tarfs_options.file_name))
-	error (1, 1, "No archive specified.");
+	argp_error(sate, "No archive specified.");
   }
 
   return 0;
diff -u -p a/options.c b/options.c
--- a/options.c
+++ b/options.c
@@ -116,7 +116,7 @@ argp_parse_common_options (int key, char
     case OPT_STOW:		/* --stow */
       err = stow_diradd (arg, ulfs_flags, &ulfs_patternlist, ulfs_priority);
       if (err)
-	error (EXIT_FAILURE, err, "stow_diradd");
+	argp_error(state, "stow_diradd");
       ulfs_modified = 1;
       ulfs_flags = ulfs_mode = ulfs_priority = 0;
       ulfs_match = 0;
@@ -136,7 +136,7 @@ argp_parse_common_options (int key, char
       else
 	err = ulfs_register (arg, ulfs_flags, ulfs_priority);
       if (err)
-	error (EXIT_FAILURE, err, "ulfs_register");
+	argp_error(state, "ulfs_register");
       ulfs_modified = 1;
       ulfs_flags = ulfs_mode = ulfs_priority = 0;
       ulfs_match = 0;

Reply via email to