4
* Command line argument parser.
6
* Copyright 1996-2003 Glyph & Cog, LLC
9
/*========================================================================
11
Modified under the Poppler project - http://poppler.freedesktop.org
13
Poppler project changes to this file are under the GPLv2 or later license
15
All changes made under the Poppler project to this file are licensed
16
under GPL version 2 or later
18
Copyright (C) 2008, 2009 Albert Astals Cid <aacid@kde.org>
20
To see a description of the changes please see the Changelog file that
21
came with your tarball or type make ChangeLog if you are building from git
23
========================================================================*/
30
#include "parseargs.h"
32
#include "goo/gstrtod.h"
34
static const ArgDesc *findArg(const ArgDesc *args, char *arg);
35
static GBool grabArg(const ArgDesc *arg, int i, int *argc, char *argv[]);
37
GBool parseArgs(const ArgDesc *args, int *argc, char *argv[]) {
45
if (!strcmp(argv[i], "--")) {
47
for (j = i; j < *argc; ++j)
50
} else if ((arg = findArg(args, argv[i]))) {
51
if (!grabArg(arg, i, argc, argv))
60
void printUsage(char *program, char *otherArgs, const ArgDesc *args) {
66
for (arg = args; arg->arg; ++arg) {
67
if ((w1 = strlen(arg->arg)) > w)
71
fprintf(stderr, "Usage: %s [options]", program);
73
fprintf(stderr, " %s", otherArgs);
74
fprintf(stderr, "\n");
76
for (arg = args; arg->arg; ++arg) {
77
fprintf(stderr, " %s", arg->arg);
78
w1 = 9 + w - strlen(arg->arg);
98
fprintf(stderr, "%-*s", w1, typ);
100
fprintf(stderr, ": %s", arg->usage);
101
fprintf(stderr, "\n");
105
static const ArgDesc *findArg(const ArgDesc *args, char *arg) {
108
for (p = args; p->arg; ++p) {
109
if (p->kind < argFlagDummy && !strcmp(p->arg, arg))
115
static GBool grabArg(const ArgDesc *arg, int i, int *argc, char *argv[]) {
124
*(GBool *)arg->val = gTrue;
128
if (i + 1 < *argc && isInt(argv[i+1])) {
129
*(int *)arg->val = atoi(argv[i+1]);
137
if (i + 1 < *argc && isFP(argv[i+1])) {
138
*(double *)arg->val = gatof(argv[i+1]);
147
strncpy((char *)arg->val, argv[i+1], arg->size - 1);
148
((char *)arg->val)[arg->size - 1] = '\0';
156
fprintf(stderr, "Internal error in arg table\n");
162
for (j = i; j < *argc; ++j)
168
GBool isInt(char *s) {
169
if (*s == '-' || *s == '+')
178
GBool isFP(char *s) {
181
if (*s == '-' || *s == '+')
184
while (isdigit(*s)) {
190
while (isdigit(*s)) {
194
if (n > 0 && (*s == 'e' || *s == 'E')) {
196
if (*s == '-' || *s == '+')
203
} while (isdigit(*s));