added command line parsing for options
This commit is contained in:
		
							parent
							
								
									e2946fc714
								
							
						
					
					
						commit
						dde3bda516
					
				
							
								
								
									
										76
									
								
								src/config.c
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								src/config.c
									
									
									
									
									
								
							@ -1,7 +1,25 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
 | 
					#include <string.h>
 | 
				
			||||||
 | 
					#include <unistd.h>
 | 
				
			||||||
#include "config.h"
 | 
					#include "config.h"
 | 
				
			||||||
#include "log.h"
 | 
					#include "log.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const struct option long_options[] = {
 | 
				
			||||||
 | 
					  {"framebuffer", required_argument, 0, 'F'},
 | 
				
			||||||
 | 
					  {"help",        no_argument,       0, 'h'},
 | 
				
			||||||
 | 
					  { NULL,         0,                 0,  0 }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const char *short_options = "F:h";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static const char *helptext =
 | 
				
			||||||
 | 
					  "UPIWIN - Micro Pi Windows server program\n\n"
 | 
				
			||||||
 | 
					  "Usage: upiwin [options] scriptname [scriptargs]\n\n"
 | 
				
			||||||
 | 
					  "Available options:\n"
 | 
				
			||||||
 | 
					  "  -F,--framebuffer [devname] - Specifies the framebuffer device name\n"
 | 
				
			||||||
 | 
					  "  -h,--help - Displays this help message.\n"
 | 
				
			||||||
 | 
					  "";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define EXITFUNCBLOCK_FUNCCOUNT 64
 | 
					#define EXITFUNCBLOCK_FUNCCOUNT 64
 | 
				
			||||||
 | 
					
 | 
				
			||||||
typedef struct tagEXITFUNCBLOCK
 | 
					typedef struct tagEXITFUNCBLOCK
 | 
				
			||||||
@ -38,14 +56,70 @@ static void init_defaults(void)
 | 
				
			|||||||
  Gconfig.sys_mq_length = 64;
 | 
					  Gconfig.sys_mq_length = 64;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
HRESULT Config_setup(void)
 | 
					static HRESULT parse_cmdline(int argc, char *argv[], GLOBAL_CONFIG *parsed)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  int c;
 | 
				
			||||||
 | 
					  PSTR pstr;
 | 
				
			||||||
 | 
					  BOOL help = FALSE;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  memset(parsed, 0, sizeof(GLOBAL_CONFIG));
 | 
				
			||||||
 | 
					  for (;;)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    c = getopt_long(argc, argv, short_options, long_options, NULL);
 | 
				
			||||||
 | 
					    if (c==-1)
 | 
				
			||||||
 | 
					      break;
 | 
				
			||||||
 | 
					    switch (c)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      case 'F':
 | 
				
			||||||
 | 
						pstr = strdup(optarg);
 | 
				
			||||||
 | 
						if (!pstr)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
						  Log(LERROR, "Out of memory in parse_cmdline");
 | 
				
			||||||
 | 
						  return E_OUTOFMEMORY;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if (parsed->framebuffer_device)
 | 
				
			||||||
 | 
						  free(parsed->framebuffer_device);
 | 
				
			||||||
 | 
						parsed->framebuffer_device = pstr;
 | 
				
			||||||
 | 
						break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      case 'h':
 | 
				
			||||||
 | 
						help = TRUE;
 | 
				
			||||||
 | 
						break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      default:
 | 
				
			||||||
 | 
						fprintf(stderr, "%s: unexpected option -%c\n", argv[0], c);
 | 
				
			||||||
 | 
						return E_UNEXPECTED;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  if (help)
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    fputs(helptext, stdout);
 | 
				
			||||||
 | 
					    return S_FALSE;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return S_OK;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					static void overlay_config(GLOBAL_CONFIG *p)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  if (p->framebuffer_device)
 | 
				
			||||||
 | 
					    Gconfig.framebuffer_device = p->framebuffer_device;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HRESULT Config_setup(int argc, char *argv[])
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					  HRESULT hr;
 | 
				
			||||||
 | 
					  GLOBAL_CONFIG from_commandline;
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
  if (atexit(run_exit_funcs))
 | 
					  if (atexit(run_exit_funcs))
 | 
				
			||||||
  {
 | 
					  {
 | 
				
			||||||
    Log(LFATAL, "Unable to set up exit function mechanism");
 | 
					    Log(LFATAL, "Unable to set up exit function mechanism");
 | 
				
			||||||
    return E_FAIL;
 | 
					    return E_FAIL;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  init_defaults();
 | 
					  init_defaults();
 | 
				
			||||||
 | 
					  hr = parse_cmdline(argc, argv, &from_commandline);
 | 
				
			||||||
 | 
					  if (hr != S_OK)
 | 
				
			||||||
 | 
					    return hr;
 | 
				
			||||||
 | 
					  overlay_config(&from_commandline)
 | 
				
			||||||
  return S_OK;
 | 
					  return S_OK;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,7 @@ typedef struct tagGLOBAL_CONFIG {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
extern GLOBAL_CONFIG Gconfig;
 | 
					extern GLOBAL_CONFIG Gconfig;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extern HRESULT Config_setup(void);
 | 
					extern HRESULT Config_setup(int argc, char *argv[]);
 | 
				
			||||||
extern HRESULT Config_exitfunc(PEXITFUNC pfn);
 | 
					extern HRESULT Config_exitfunc(PEXITFUNC pfn);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#endif /* __CONFIG_H_INCLUDED */
 | 
					#endif /* __CONFIG_H_INCLUDED */
 | 
				
			||||||
 | 
				
			|||||||
@ -27,13 +27,17 @@ static void do_draw(void)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int main(int argc, char *argv[])
 | 
					int main(int argc, char *argv[])
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
					  HRESULT hr;
 | 
				
			||||||
  int running = 1;
 | 
					  int running = 1;
 | 
				
			||||||
  MSG msg;
 | 
					  MSG msg;
 | 
				
			||||||
  char *tmp;
 | 
					  char *tmp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Time_init();
 | 
					  Time_init();
 | 
				
			||||||
  if (FAILED(Config_setup()))
 | 
					  hr = Config_setup(argc, argv);
 | 
				
			||||||
 | 
					  if (FAILED(hr))
 | 
				
			||||||
    return EXIT_FAILURE;
 | 
					    return EXIT_FAILURE;
 | 
				
			||||||
 | 
					  else if (hr != S_OK)
 | 
				
			||||||
 | 
					    return EXIT_SUCCESS;
 | 
				
			||||||
  if (FAILED(Fb_setup()))
 | 
					  if (FAILED(Fb_setup()))
 | 
				
			||||||
    return EXIT_FAILURE;
 | 
					    return EXIT_FAILURE;
 | 
				
			||||||
  if (FAILED(Gpio_setup()))
 | 
					  if (FAILED(Gpio_setup()))
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user