1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/******************************************************************************
* Copyright (C) 2018 Alejandro Colomar Andrés *
******************************************************************************/
/******************************************************************************
******* headers **************************************************************
******************************************************************************/
/* Standard C ----------------------------------------------------------------*/
#include <getopt.h>
/* FILE & fopen() & snprintf() & FILENAME_MAX */
#include <stdio.h>
/* exit() */
#include <stdlib.h>
/* Project -------------------------------------------------------------------*/
/* print_share_...() */
#include "about.h"
/* user_iface_mode */
#include "user_iface.h"
/* saved_name */
#include "save.h"
/* Module --------------------------------------------------------------------*/
/* menu_iface_mode */
#include "menu_iface.h"
#include "parser.h"
/******************************************************************************
******* macros ***************************************************************
******************************************************************************/
# define OPT_LIST "hLuv""f:i:"
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
static void parse_file (char* argument);
static void parse_iface (char* argument);
/******************************************************************************
******* main *****************************************************************
******************************************************************************/
void parser (int argc, char *argv[])
{
int opt = 0;
int opt_index = 0;
struct option long_options[] = {
/* Standard */
{"help", no_argument, 0, 'h'},
{"license", no_argument, 0, 'L'},
{"usage", no_argument, 0, 'u'},
{"version", no_argument, 0, 'v'},
/* Non-standard */
{"file", required_argument, 0, 'f'},
{"iface", required_argument, 0, 'i'},
/* Null */
{0, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, OPT_LIST, long_options,
&opt_index)) != -1) {
switch (opt) {
/* Standard */
case 'h':
print_share_file(SHARE_HELP);
exit(EXIT_SUCCESS);
case 'L':
print_share_file(SHARE_LICENSE);
exit(EXIT_SUCCESS);
case 'u':
print_share_file(SHARE_USAGE);
exit(EXIT_SUCCESS);
case 'v':
print_version();
exit(EXIT_SUCCESS);
/* Non-standard */
case 'f':
parse_file(optarg);
break;
case 'i':
parse_iface(optarg);
break;
case '?':
/* getopt_long already printed an error message. */
default:
print_share_file(SHARE_USAGE);
exit(EXIT_FAILURE);
}
}
}
/******************************************************************************
******* static functions *****************************************************
******************************************************************************/
static void parse_file (char* argument)
{
// FIXME
FILE *fp;
fp = fopen(argument, "r");
if (!fp) {
printf("--file argument not valid\n");
printf("It must be a valid file name (relative to saved dir)\n");
exit(EXIT_FAILURE);
} else {
fclose(fp);
sprintf(saved_path, "");
snprintf(saved_name, FILENAME_MAX, "%s", argument);
}
}
static void parse_iface (char* argument)
{
menu_iface_mode = atoi(argument);
user_iface_mode = menu_iface_mode;
if (menu_iface_mode < MENU_IFACE_CLUI || menu_iface_mode > MENU_IFACE_TUI) {
printf("--iface argument not valid\n");
printf("It must be an integer [%i U %i]\n", MENU_IFACE_CLUI, MENU_IFACE_TUI);
exit(EXIT_FAILURE);
}
}
/******************************************************************************
******* end of file **********************************************************
******************************************************************************/
|