File: cmdline.c

package info (click to toggle)
barcode 0.99-3
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 4,648 kB
  • sloc: ansic: 21,507; sh: 11,815; makefile: 24; sed: 16
file content (172 lines) | stat: -rw-r--r-- 4,985 bytes parent folder | download
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
 * cmdline.c -- parse a command line using getopt, environment and defaults
 *
 * Copyright (c) 1999 Alessandro Rubini (rubini@gnu.org)
 * Copyright (c) 1999 Prosa Srl. (prosa@prosa.it)
 * Copyright (c) 2010, 2011 Giuseppe Scrivano (gscrivano@gnu.org)
 * 
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   Changes:
 *      Michele Comitini (mcm@glisco.it): better handling of numeric type args.
 */

#include "barcode.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "cmdline.h"

/* In case of error, print help string and return error (-1) */
int commandline_errormsg(FILE *f, struct commandline *args,
				       char *prgname, char *messagehead)
{
    char *buffer;
    char *buffer2;
    int len;
    struct commandline *ptr;

    if (!messagehead) {
  fprintf(f, _("%s: Error in cmdline\nPossible options:\n"), prgname);
    } else {
  fprintf(f,"%s: %s",prgname, messagehead);
    }

    for (ptr = args; ptr->option; ptr++) {
    if (_(ptr->descrip)) {
	    len = strlen(_(ptr->descrip));
	    len +=  ptr->default_v ? strlen(ptr->default_v) : 6;
	    len += ptr->env ? strlen(ptr->env) : 6;
	    buffer2 = malloc(len+3); /* 3 to be safe :) */
	    len += 32;
	    if (buffer2) {
		buffer = malloc(len);
		if (buffer) {
          sprintf(buffer2, _(ptr->descrip),
			    ptr->default_v ? ptr->default_v : "(none)",
			    ptr->env ? ptr->env : "(none)");
		    sprintf(buffer, "   -%c %s     %s", ptr->option,
			    ptr->type ? "<arg>" : "     ", buffer2);
		    fprintf(f, "%s\n",buffer);
		    free(buffer);
		}
		free(buffer2);
	    }
	}
    }
    return -1;
}

/*
 * Associate formats to type.
 */
struct type_fmt {
    enum option_type type;
    char *fmt;
};
static struct type_fmt formats[] = {
    {CMDLINE_I, "%i%s"},   /* the trailing "%s" is used for error check */
    {CMDLINE_D, "%d%s"},
    {CMDLINE_X, "%x%s"},
    {CMDLINE_O, "%o%s"},
    {CMDLINE_F, "%lf%s"},
    {CMDLINE_P, "%p%s"},
    {0, NULL}  /* If we get here, it's CMDLINE_S: no sscanf buf strcpy() */
};


/* Parse one argument (default or provided) */
static int commandline_oneopt(struct commandline *ptr, char *value)
{
    struct type_fmt *tptr;
    char *trash = value ? strdup(value) : NULL;

    /* convert it, if needed */
    if ((ptr->type != CMDLINE_NONE) && ptr->result) {
	for (tptr = formats; tptr->type; tptr++)
	    if (tptr->type == ptr->type)
		break;
	if (!tptr->type) /* not found: it is a string */
	    *(char **)(ptr->result) = value;
	else
	    if (sscanf(value, tptr->fmt, ptr->result, trash)!=1) {
		free(trash);
		return -1;
	    }
    }
    if (trash)
	free(trash);
    
    if ((ptr->type == CMDLINE_NONE) && ptr->result) /* no type, just count */
	(*(int *)(ptr->result))++;

    /* call the function, if needed */
    if (ptr->fun) {
	if (ptr->type) {
	    if (ptr->result) return ptr->fun(ptr->result); /* converted */
	    return ptr->fun(value); /* type, but not converted */
	}
	return ptr->fun(NULL); /* no type: just call it */
    }
    return 0;
}

/* The main function */
int commandline(struct commandline *args, int argc, char **argv,
		char *errorhead)
{
    struct commandline *ptr;
    char *getopt_desc = (char *)calloc(512, 1);
    int desc_offset = 0;
    int opt, retval;
    char *value;

    /* Build getopt string and process defaults values */
    for (ptr = args; ptr->option; ptr++) {
	getopt_desc[desc_offset++] = ptr->option;
	if (ptr->type) getopt_desc[desc_offset++] = ':';

	value = NULL;
	if (ptr->env)
	    value = getenv(ptr->env);
	if (!value)
	    value = ptr->default_v;
	if (value && (retval = commandline_oneopt(ptr, value))) {
	    /*
	     * if the function returns a specific (not -1) value, it already
	     * printed its message, so avoid the generic help
	     */
	    if (retval == -1)
		commandline_errormsg(stderr, args, argv[0], errorhead);
	    return retval;
	}
    }

    /* Run getopt and process material */
    while ((opt = getopt(argc, argv, getopt_desc)) != -1) {
	for (ptr = args; ptr->option; ptr++)
	    if (opt == ptr->option)
		break;
	if (!ptr->option) /* unknown option */
	    return commandline_errormsg(stderr, args, argv[0], errorhead);
	if ( (retval = commandline_oneopt(ptr, optarg)) ) { /*  wrong arg */
	    if (retval == -1)
		commandline_errormsg(stderr, args, argv[0], errorhead);
	    return retval;
	}
    }
    return 0;
}