@@ -43,13 +43,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
43
43
#define MINI 0
44
44
#define MEDIUM 1
45
45
46
- static gboolean flag_help = FALSE;
47
46
48
- static gchar sock_path [ 1024 ] ;
47
+ static gchar * sock_path ;
49
48
static gchar line [65536 ];
50
49
static gboolean flag_color = FALSE;
51
- static gchar format [ 256 ] ;
52
-
50
+ static gchar * format ;
51
+ static gboolean flag_version = FALSE;
53
52
#define BOOL (i ) (i?1:0)
54
53
55
54
struct dump_as_is_arg_s {
@@ -117,6 +116,18 @@ static struct keyword_set_s KEYWORDS_COLOR = {
117
116
"[32mUP[0m "
118
117
};
119
118
119
+ static GOptionEntry entries [] = {
120
+ {"color" , 'c' , 0 , G_OPTION_ARG_NONE , & flag_color ,
121
+ "coloured display \n" , NULL },
122
+ {"sock-path" , 'S' , 0 , G_OPTION_ARG_FILENAME , & sock_path ,
123
+ "explicit unix socket path\n" , "SOCKET" },
124
+ {"format" , 'f' , 0 , G_OPTION_ARG_STRING , & format ,
125
+ "output result by given FORMAT" ,"FORMAT" },
126
+ {"version" , 'v' , 0 , G_OPTION_ARG_NONE , & flag_version ,
127
+ "Display the version of gridinit_cmd" , NULL },
128
+ {NULL }
129
+ };
130
+
120
131
static gint
121
132
compare_child_info (gconstpointer p1 , gconstpointer p2 )
122
133
{
@@ -553,7 +564,7 @@ command_kill(int argc, char **args)
553
564
{
554
565
struct dump_as_is_arg_s dump_args = {};
555
566
556
- int rc = send_commandv (dump_as , & dump_args , "stop" , argc , args );
567
+ int rc = send_commandv (dump_as , & dump_args , "stop" , argc , args );
557
568
return !rc
558
569
|| dump_args .count_errors != 0
559
570
|| dump_args .count_success == 0 ;
@@ -641,75 +652,73 @@ struct command_s {
641
652
{ NULL , NULL }
642
653
};
643
654
644
- static int
645
- main_options (int argc , char * * args )
646
- {
647
- int opt ;
648
-
649
- g_strlcpy (sock_path , GRIDINIT_SOCK_PATH , sizeof (sock_path ));
650
-
651
- while ((opt = getopt (argc , args , "chf:S:" )) != -1 ) {
652
- switch (opt ) {
653
- case 'c' :
654
- flag_color = TRUE;
655
- break ;
656
- case 'S' :
657
- if (optarg )
658
- g_strlcpy (sock_path , optarg , sizeof (sock_path ));
659
- break ;
660
- case 'h' :
661
- flag_help = TRUE;
662
- break ;
663
- case 'f' :
664
- if (optarg )
665
- g_strlcpy (format , optarg , sizeof (format ));
666
- break ;
667
- }
668
- }
669
-
670
- return optind ;
671
- }
672
-
673
655
static void
674
- help (char * * args )
656
+ help (void )
675
657
{
676
658
close (2 );
677
- g_print ("Usage: %s [-h|-c|-f FORMAT|-S SOCK]... (status{,2,3}|start|stop|reload|repair) [ID...]\n" , args [0 ]);
678
- g_print ("\n OPTIONS:\n" );
679
- g_print (" -c : coloured display\n" );
680
- g_print (" -h : displays a little help section\n" );
681
- g_print (" -S SOCK : explicit unix socket path\n" );
682
- g_print (" -f FORMAT : output result by json\n" );
683
659
g_print ("\n COMMANDS:\n" );
684
- g_print (" status* : Displays the status of the given processes or groups\n" );
685
- g_print (" start : Starts the given processes or groups, even if broken\n" );
686
- g_print (" kill : Stops the given processes or groups, they won't be automatically\n" );
687
- g_print (" restarted even after a configuration reload\n" );
688
- g_print (" stop : Calls 'kill' until the children exit\n" );
689
- g_print (" restart : Restarts the given processes or groups\n" );
690
- g_print (" reload : Reloads the configuration, stopping obsolete processes, starting\n" );
691
- g_print (" the newly discovered. Broken or stopped processes are not restarted\n" );
692
- g_print (" repair : Removes the broken flag set on a process. Start must be called to\n" );
693
- g_print (" restart the process.\n" );
660
+ g_print (" status* : Displays the status of the given processes or groups\n" );
661
+ g_print (" start : Starts the given processes or groups, even if broken\n" );
662
+ g_print (" kill : Stops the given processes or groups, they won't be automatically\n" );
663
+ g_print (" restarted even after a configuration reload\n" );
664
+ g_print (" stop : Calls 'kill' until the children exit\n" );
665
+ g_print (" restart : Restarts the given processes or groups\n" );
666
+ g_print (" reload : Reloads the configuration, stopping obsolete processes, starting\n" );
667
+ g_print (" the newly discovered. Broken or stopped processes are not restarted\n" );
668
+ g_print (" repair : Removes the broken flag set on a process. Start must be called to\n" );
669
+ g_print (" restart the process.\n" );
694
670
g_print ("with ID the key of a process, or '@GROUP', with GROUP the name of a process\n" );
695
671
g_print ("group\n" );
696
672
close (1 );
697
673
exit (0 );
698
674
}
699
675
676
+ static int
677
+ main_options (int argc , char * * args )
678
+ {
679
+ GError * error = NULL ;
680
+ GOptionContext * context ;
681
+
682
+ context = g_option_context_new ("(status{,2,3}|start|stop|reload|repair) [ID...]\n" );
683
+ g_option_context_add_main_entries (context , entries , NULL );
684
+ if (!g_option_context_parse (context , & argc , & args , & error )) {
685
+ g_print ("option parsing failed: %s\n" , error -> message );
686
+ gchar * usage = g_option_context_get_help (context , TRUE, NULL );
687
+ g_print ("%s" , usage );
688
+ help ();
689
+ }
690
+
691
+ return argc ;
692
+ }
693
+
694
+ static void
695
+ usage (void )
696
+ {
697
+ GOptionContext * context ;
698
+ context = g_option_context_new ("(status{,2,3}|start|stop|reload|repair) [ID...]\n" );
699
+ gchar * usage = g_option_context_get_help (context , TRUE, NULL );
700
+ g_print ("%s" , usage );
701
+ help ();
702
+ }
700
703
int
701
704
main (int argc , char * * args )
702
705
{
703
706
struct command_s * cmd ;
704
- int opt_index ;
707
+ int opt_index = 1 ;
708
+
709
+ if (argc == 1 )
710
+ usage ();
705
711
706
712
close (0 );
707
- opt_index = main_options (argc , args );
708
713
709
- if (flag_help )
710
- help (args );
711
- if (opt_index >= argc )
712
- help (args );
714
+ argc = main_options (argc , args );
715
+
716
+ if (flag_version ) {
717
+ fprintf (stdout , "gridinit_cmd version: %s\n" , API_VERSION );
718
+ close (1 );
719
+ close (2 );
720
+ return 0 ;
721
+ }
713
722
714
723
for (cmd = COMMANDS ; cmd -> name ;cmd ++ ) {
715
724
if (0 == g_ascii_strcasecmp (cmd -> name , args [opt_index ])) {
@@ -720,9 +729,6 @@ main(int argc, char ** args)
720
729
}
721
730
}
722
731
723
- fprintf (stderr , "\n*** Invalid command %s ***\n\n" , args [opt_index ]);
724
- help (args );
725
-
726
732
close (1 );
727
733
close (2 );
728
734
return 1 ;
0 commit comments