aboutsummaryrefslogtreecommitdiff
path: root/vendor/voclient/voapps/generic.c
blob: ddb86adfdaff6db6eb4081a02d1f3df247782f5f (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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 *  GENERIC -- Template file for VOApps task
 *
 *    Usage:
 *		generic [<otps>] <votable>
 *
 *  @file       generic.c
 *  @author     Mike Fitzpatrick
 *  @date       6/03/12
 *
 *  @brief      Template file for VOApps task.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "votParse.h"			/* keep these in order!		*/
#include "voApps.h"


/*  All tasks should support a "--return" flag if they can return a pointer
 *  to a result as part of the programmatic invocation.  As a cmdline task
 *  'reslen' and 'result' are ignored (actually, they're thrown away), but
 *  when called from an API the 'result' is a pointer to an arbitrary memory
 *  location that is passed back to the caller.  
 *
 *  The result object is defined by the task and can be anything.  The 
 *  '--return' flag can be defined to take an optional argument to specify
 *  which of multiple possible objects are returned (e.g. "--return=fits"
 *  "--return=votable") but the task is responsible for creating the object. 
 */
static int  do_return   = 0;		/* return result?		*/

/*  Global task declarations.  These should all be defined as 'static' to
 *  avoid namespace collisions.
 */
static int foo		= 0;


/*  A result buffer should be defined to point to the result object if it is
 *  created dynamically, e.g. a list of votable columns.  The task is
 *  responsible for initially allocating this pointer and then resizing as
 *  needed.
 */
#define	SZ_RESBUF	8192

static char *resbuf;


/*  Task specific option declarations.  Task options are declared using the
 *  getopt_long(3) syntax.
 */
static Task  self       = {  "generic",  generic,  0,  0,  0  };

static char  *opts 	= "%hno:r";
static struct option long_opts[] = {
        { "test",         2, 0,   '%'},		/* --test is std	*/
        { "help",         2, 0,   'h'},		/* --help is std	*/
        { "number",       2, 0,   'n'},		/* opt w/ no arg	*/
        { "output",       1, 0,   'o'},		/* opt w/ required arg	*/
        { "return",       2, 0,   'r'},		/* --return is std	*/
        { NULL,           0, 0,    0 }
};


/*  All tasks should declare a static Usage() method to print the help 
 *  text in response to a '-h' or '--help' flag.  The help text should 
 *  include a usage summary, a description of options, and some examples.
 */
static void Usage (void);
static void Tests (char *input);


/**
 *  Application entry point.  All VOApps tasks MUST contain this 
 *  method signature.
 */
int
generic (int argc, char **argv, size_t *reslen, void **result)
{
    /*  These declarations are required for the VOApps param interface.
     */
    char **pargv, optval[SZ_FNAME];

    /*  These declarations are specific to the task.
     */
    char  *iname, *oname;
    int    ch = 0, status = OK, number = 0, pos = 0;
    FILE  *fd = (FILE *) NULL;


    /* Initialize result object	whether we return an object or not.
     */
    *reslen = 0;	
    *result = NULL;

    /*  Initialize local task values.
     */
    iname  = NULL;
    oname  = NULL;


    /*  Parse the argument list.  The use of vo_paramInit() is required to
     *  rewrite the argv[] strings in a way vo_paramNext() can be used to
     *  parse them.  The programmatic interface allows "param=value" to
     *  be passed in, but the getopt_long() interface requires these to
     *  be written as "--param=value" so they are not confused with 
     *  positional parameters (i.e. any param w/out a leading '-').
     */
    pargv = vo_paramInit (argc, argv, opts, long_opts);
    while ((ch = vo_paramNext(opts,long_opts,argc,pargv,optval,&pos)) != 0) {
        if (ch > 0) {
	    /*  If the 'ch' value is > 0 we are parsing a single letter
	     *  flag as defined in the 'opts string.
	     */
	    switch (ch) {
	    case '%':  Tests ();			return (self.nfail);
	    case 'h':  Usage ();			return (OK);
	    case 'n':  number++;			break;
	    case 'o':  oname = strdup (optval);		break;
	    case 'r':  do_return=1;	    	    	break;
	    default:
		fprintf (stderr, "Invalid option '%s'\n", optval);
		return (1);
	    }

	} else if (ch == PARG_ERR) {
	    return (ERR);

	} else {
	    /*  This code processes the positional arguments.  The 'optval'
	     *  string contains the value but since this string is
	     *  overwritten w/ each arch we need to make a copy (and must
	     *  remember to free it later.
	     */
	    iname = strdup (optval);
	    break;
	}
    }


    /*  Sanity checks.  Tasks should validate input and accept stdin/stdout
     *  where it makes sense.
     */
    if (iname == NULL) iname = strdup ("stdin");
    if (oname == NULL) oname = strdup ("stdout");
    if (strcmp (iname, "-") == 0) { free (iname), iname = strdup ("stdin");  }
    if (strcmp (oname, "-") == 0) { free (oname), oname = strdup ("stdout"); }



   /********
    ********
    ********
    ********
    ********		Main body of task
    ********
    ********
    ********
    *******/




    /*  Clean up.  Rememebr to free whatever pointers were created when
     *  parsing arguments.
     */
    if (iname)
	free (iname);
    if (oname)
	free (oname);

    vo_paramFree (argc, pargv);

    return (status);	/* status must be OK or ERR (i.e. 0 or 1)     	*/
}


/**
 *  USAGE -- Print task help summary.
 */
static void
Usage (void)
{
    fprintf (stderr, "\n  Usage:\n\t"
        "generic [<opts>] votable.xml\n\n"
        "  where\n"
        "       -%%,--test		run unit tests\n"
        "       -h,--help		this message\n"
        "       -n,--number		number output\n"
        "       -o,--output=<file>	output file\n"
        "       -r,--return		return result from method\n"
	"\n"
 	"  Examples:\n\n"
	"    1)  First example\n\n"
	"	    %% generic test.xml\n"
	"	    %% generic -n test.xml\n"
	"	    %% cat test.xml | generic\n"
	"\n"
	"    2)  Second example\n\n"
	"	    %% generic -o pos.txt test.xml\n"
	"\n"
    );
}


/**
 *  Tests -- Task unit tests.
 */
static void
Tests (char *input)
{
   /*  First argument must always be the 'self' variable, the last must 
    *  always be a NULL to terminate the cmd args.
    */
   vo_taskTest (self, "--help", NULL);
}