summaryrefslogtreecommitdiffstats
path: root/src/parse.c
blob: e80bc94db214020ddac7a4b870646776f908649c (plain) (blame)
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
141
/******************************************************************************
 *	Copyright (C) 2018	Alejandro Colomar Andrés		      *
 *	SPDX-License-Identifier:	GPL-2.0-only			      *
 ******************************************************************************/


/******************************************************************************
 ******* headers **************************************************************
 ******************************************************************************/
#include "coins/parse.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

#include <getopt.h>
#include <sys/stat.h>

#include "libalx/base/errno/error.h"
#include "libalx/base/stdio/printf/snprintfs.h"


/******************************************************************************
 ******* macros ***************************************************************
 ******************************************************************************/
#define PROG_NAME		"coins"
#define PROG_YEAR		"2018"

#define OPT_LIST		"hLuv""f:"

#define SHARE_DIR		"" INSTALL_SHARE_DIR "/coins/"
#define SHARE_COPYRIGHT_FILE	"" SHARE_DIR "/COPYRIGHT.txt"
#define SHARE_HELP_FILE		"" SHARE_DIR "/HELP.txt"
#define SHARE_LICENSE_FILE	"" SHARE_DIR "/LICENSE.txt"
#define SHARE_USAGE_FILE	"" SHARE_DIR "/USAGE.txt"


/******************************************************************************
 ******* enum *****************************************************************
 ******************************************************************************/


/******************************************************************************
 ******* struct / union *******************************************************
 ******************************************************************************/


/******************************************************************************
 ******* static functions (prototypes) ****************************************
 ******************************************************************************/
static
void	parse_file	(char fname[static restrict FILENAME_MAX], char *arg);
static
void	print_version	(void);


/******************************************************************************
 ******* global functions *****************************************************
 ******************************************************************************/
void	parse	(char fname[static restrict FILENAME_MAX],
		 int argc, char *argv[])
{
	static bool	f = 0;
	int	opt		= 0;
	int	opt_index	= 0;

	const	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'},
		/* Null */
		{0,			0,			0,	0}
	};

	while ((opt = getopt_long(argc, argv, OPT_LIST, long_options,
						&opt_index)) != -1) {
		switch (opt) {
		/* Standard */
		case 'h':
			system("cat "SHARE_HELP_FILE"");
			exit(EXIT_SUCCESS);
		case 'L':
			system("cat "SHARE_LICENSE_FILE"");
			exit(EXIT_SUCCESS);
		case 'u':
			system("cat "SHARE_USAGE_FILE"");
			exit(EXIT_SUCCESS);
		case 'v':
			print_version();
			exit(EXIT_SUCCESS);
		/* Non-standard */
		case 'f':
			parse_file(fname, optarg);
			f	= true;
			break;

		case '?':
			/* getopt_long already printed an error message. */
		default:
			goto usage;
		}
	}

	if (!f)
		goto usage;
	return;
usage:
	system("cat "SHARE_USAGE_FILE"");
	exit(EXIT_FAILURE);
}


/******************************************************************************
 ******* static functions (definitions) ***************************************
 ******************************************************************************/
static
void	parse_file	(char fname[static restrict FILENAME_MAX], char *arg)
{
	struct stat	sb;

	if (stat(arg, &sb)  ||  !S_ISREG(sb.st_mode))
		alx_error(EXIT_FAILURE, arg);
	if (alx_snprintfs(fname, NULL, FILENAME_MAX, "%s", arg))
		alx_error(EXIT_FAILURE, arg);
	return;
}

static
void	print_version	(void)
{
	printf("%s %s\n\n", PROG_NAME, PROG_VERSION);
}


/******************************************************************************
 ******* end of file **********************************************************
 ******************************************************************************/