// File: /workspace/github_c_makefile_star2_O0/Openvario[P]sensord/cmdline_parser.c
// Function: cmdline_parser
void cmdline_parser(int argc, char **argv, t_io_mode *io_mode){

	// locale variables
	int c;
	char datalog_filename[50];
	char sensordata_filename[50];

	const char* Usage = "\n"\
	"  -v              print version information\n"\
	"  -f              don't daemonize, stay in foreground\n"\
	"  -i              inetd mode\n"\
	"  -c [filename]   use config file [filename]\n"\
	"  -d[n]           set debug level. n can be [1..2]. default=1\n"\
   	"  -j              turn on timing jitter... for testing only!\n"\
	"  -r [filename]   record measurement values to file\n"\
	"  -s              second order temperature compensation for MS5611 enable\n"\
	"  -p [filename]   use values from file instead of measuring\n"\
	"\n";

	// check commandline arguments
	while ((c = getopt (argc, argv, "vd::fijlhr:p:c:s")) != -1)
	{
		switch (c) {
			case 'v':
				//sprintf(s, "sensord V0.1 %s %s", __DATE__
				printf("sensord V%c.%c RELEASE %c build: %s %s %s\n", VERSION_MAJOR, VERSION_MINOR, VERSION_RELEASE, VERSION_GIT, __DATE__, __TIME__);
				printf("sensord  Copyright (C) 2014  see AUTHORS on www.openvario.org\n");
				printf("This program comes with ABSOLUTELY NO WARRANTY;\n");
				printf("This is free software, and you are welcome to redistribute it under certain conditions;\n");
				break;

			case 'c':
				// use config file
				// record sensordata for replay
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -c\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}
				else
				{
					strcpy(config_filename, optarg);
					fprintf(stderr, "!! Using config file %s !!\n", config_filename);

					// Open the fp to config file
					fp_config = fopen(config_filename,"r");

					//check if config file opened ok ...
					if( fp_config == NULL)
					{
						fprintf(stderr, "Error opening config file: %s\n", config_filename);
						fprintf(stderr, "Exiting ...\n");
						exit(EXIT_FAILURE);
					}
				}
				break;

			case 'd':
				if (optarg == NULL)
				{
					g_debug = 1;
				}
				else
					g_debug = atoi(optarg);

				fprintf(stderr, "!! DEBUG LEVEL %d !!\n",g_debug);
				break;

			case 'f':
				// don't daemonize
				fprintf(stderr, "!! STAY in g_foreground !!\n");
				g_foreground = true;
				break;

			case 'i':
				// inetd mode
				g_foreground = true;
				g_inetd = true;
				break;

			case 'j':
				// Cause deliberate timing jitter
				fprintf(stderr, "!! Timing jitter on !!\n");
				tj = true;
				break;

			case 's':
				// enable second order compensation
				fprintf(stderr, "Second order compensation ENABLE\n");
				g_secordcomp = true;
				break;

			case 'r':
				// record sensordata for replay
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -r\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}

				io_mode->sensordata_to_file = true;
				strcpy(datalog_filename, optarg);
				fprintf(stderr, "!! RECORD DATA TO %s !!\n", datalog_filename);

				// Open the fp to record file
				fp_datalog = fopen(datalog_filename,"w+");
				break;

			case 'p':
				// replay sensordata instead of measuring
				if (optarg == NULL)
				{
					fprintf(stderr, "Missing option for -p\n");
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}

				io_mode->sensordata_from_file = true;
				strcpy(sensordata_filename, optarg);
				fprintf(stderr, "!! REPLAY DATA FROM %s !!\n", sensordata_filename);
				// Open the fp to replay file
				fp_sensordata = fopen(sensordata_filename,"r");
				if (fp_sensordata == NULL)
				{
					fprintf(stderr, "Error opening file %s for replay !!\n", sensordata_filename);
					fprintf(stderr, "Exiting ...\n");
					exit(EXIT_FAILURE);
				}
				break;

			case '?':
				fprintf(stderr, "Unknown option %c\n", optopt);
				fprintf(stderr, "Usage: sensord [OPTION]\n%s",Usage);
				fprintf(stderr, "Exiting ...\n");
				exit(EXIT_FAILURE);
				break;
		}
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq 0x57e3(%rip), %rax      # 0xc180
movq %rax, -0x98(%rbp)
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
leaq 0x59b2(%rip), %rdx      # 0xc364
callq 0x2260
movl %eax, -0x1c(%rbp)
cmpl $-0x1, %eax
je 0x6e05
movl -0x1c(%rbp), %eax
addl $-0x3f, %eax
movl %eax, %ecx
movq %rcx, -0xa0(%rbp)
subl $0x37, %eax
ja 0x6e00
movq -0xa0(%rbp), %rax
leaq 0x56b7(%rip), %rcx      # 0xc0a0
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq 0x597c(%rip), %rdi      # 0xc375
movl $0x30, %esi
movl $0x33, %edx
movl $0x35, %ecx
leaq 0x5991(%rip), %r8       # 0xc3a0
leaq 0x599c(%rip), %r9       # 0xc3b2
leaq 0x59a1(%rip), %rax      # 0xc3be
movq %rax, (%rsp)
movb $0x0, %al
callq 0x2110
leaq 0x5998(%rip), %rdi      # 0xc3c7
movb $0x0, %al
callq 0x2110
leaq 0x59c9(%rip), %rdi      # 0xc406
movb $0x0, %al
callq 0x2110
leaq 0x59ec(%rip), %rdi      # 0xc437
movb $0x0, %al
callq 0x2110
jmp 0x6e00
movq 0x8572(%rip), %rax      # 0xefd0
cmpq $0x0, (%rax)
jne 0x6a9e
movq 0x858d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5a1b(%rip), %rsi      # 0xc490
movb $0x0, %al
callq 0x21d0
movq 0x8575(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5418(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
movq 0x852b(%rip), %rax      # 0xefd0
movq (%rax), %rsi
leaq 0x8991(%rip), %rdi      # 0xf440
callq 0x2050
movq 0x853d(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x59e2(%rip), %rsi      # 0xc4a7
leaq 0x8974(%rip), %rdx      # 0xf440
movb $0x0, %al
callq 0x21d0
leaq 0x8966(%rip), %rdi      # 0xf440
leaq 0x5cb7(%rip), %rsi      # 0xc798
callq 0x2250
movq %rax, 0x894b(%rip)      # 0xf438
cmpq $0x0, 0x8943(%rip)      # 0xf438
jne 0x6b38
movq 0x84fa(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x59bb(%rip), %rsi      # 0xc4c3
leaq 0x8931(%rip), %rdx      # 0xf440
movb $0x0, %al
callq 0x21d0
movq 0x84db(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x537e(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
jmp 0x6b3a
jmp 0x6e00
movq 0x848a(%rip), %rax      # 0xefd0
cmpq $0x0, (%rax)
jne 0x6b5b
leaq 0x8921(%rip), %rax      # 0xf474
movl $0x1, (%rax)
jmp 0x6b75
movq 0x846e(%rip), %rax      # 0xefd0
movq (%rax), %rdi
callq 0x2270
movl %eax, %ecx
leaq 0x8901(%rip), %rax      # 0xf474
movl %ecx, (%rax)
movq 0x847c(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x88ee(%rip), %rax      # 0xf474
movl (%rax), %edx
leaq 0x5953(%rip), %rsi      # 0xc4e2
movb $0x0, %al
callq 0x21d0
jmp 0x6e00
movq 0x8456(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x594c(%rip), %rsi      # 0xc4f8
movb $0x0, %al
callq 0x21d0
movb $0x1, 0x8866(%rip)      # 0xf420
jmp 0x6e00
movb $0x1, 0x885a(%rip)      # 0xf420
movb $0x1, 0x8854(%rip)      # 0xf421
jmp 0x6e00
movq 0x841f(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5931(%rip), %rsi      # 0xc514
movb $0x0, %al
callq 0x21d0
movb $0x1, 0x8832(%rip)      # 0xf423
jmp 0x6e00
movq 0x83fb(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5925(%rip), %rsi      # 0xc52c
movb $0x0, %al
callq 0x21d0
movb $0x1, 0x880d(%rip)      # 0xf422
jmp 0x6e00
movq 0x83af(%rip), %rax      # 0xefd0
cmpq $0x0, (%rax)
jne 0x6c61
movq 0x83ca(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5916(%rip), %rsi      # 0xc54e
movb $0x0, %al
callq 0x21d0
movq 0x83b2(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x5255(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
movq -0x18(%rbp), %rax
movb $0x1, (%rax)
leaq -0x50(%rbp), %rdi
movq 0x835d(%rip), %rax      # 0xefd0
movq (%rax), %rsi
callq 0x2050
movq 0x8376(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x50(%rbp), %rdx
leaq 0x58d5(%rip), %rsi      # 0xc565
movb $0x0, %al
callq 0x21d0
leaq -0x50(%rbp), %rdi
leaq 0x58dc(%rip), %rsi      # 0xc57e
callq 0x2250
movq %rax, 0x8782(%rip)      # 0xf430
jmp 0x6e00
movq 0x8316(%rip), %rax      # 0xefd0
cmpq $0x0, (%rax)
jne 0x6cfa
movq 0x8331(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x58b0(%rip), %rsi      # 0xc581
movb $0x0, %al
callq 0x21d0
movq 0x8319(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x51bc(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
movq -0x18(%rbp), %rax
movb $0x1, 0x1(%rax)
leaq -0x90(%rbp), %rdi
movq 0x82c0(%rip), %rax      # 0xefd0
movq (%rax), %rsi
callq 0x2050
movq 0x82d9(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x90(%rbp), %rdx
leaq 0x5868(%rip), %rsi      # 0xc598
movb $0x0, %al
callq 0x21d0
leaq -0x90(%rbp), %rdi
leaq 0x5a53(%rip), %rsi      # 0xc798
callq 0x2250
movq %rax, 0x86d7(%rip)      # 0xf428
cmpq $0x0, 0x86cf(%rip)      # 0xf428
jne 0x6d9c
movq 0x8296(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq -0x90(%rbp), %rdx
leaq 0x5840(%rip), %rsi      # 0xc5b3
movb $0x0, %al
callq 0x21d0
movq 0x8277(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x511a(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
jmp 0x6e00
movq 0x8253(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq 0x8231(%rip), %rax      # 0xefe0
movl (%rax), %edx
leaq 0x5820(%rip), %rsi      # 0xc5d8
movb $0x0, %al
callq 0x21d0
movq 0x8232(%rip), %rax      # 0xeff8
movq (%rax), %rdi
movq -0x98(%rbp), %rdx
leaq 0x5814(%rip), %rsi      # 0xc5eb
movb $0x0, %al
callq 0x21d0
movq 0x8213(%rip), %rax      # 0xeff8
movq (%rax), %rdi
leaq 0x50b6(%rip), %rsi      # 0xbea5
movb $0x0, %al
callq 0x21d0
movl $0x1, %edi
callq 0x2290
jmp 0x69a4
addq $0xb0, %rsp
popq %rbp
retq
nop