TPCCLIB
Loading...
Searching...
No Matches
execlong.c
Go to the documentation of this file.
1
7/*****************************************************************************/
8#include "tpcclibConfig.h"
9/*****************************************************************************/
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <math.h>
14/*****************************************************************************/
15#include "tpcextensions.h"
16#include "tpcift.h"
17/*****************************************************************************/
18
19/*****************************************************************************/
20static char *info[] = {
21 "Execute shell command based on the contents of given IFT file.",
22 "IFT file may or may not contain key names; any key names are ignored.",
23 "First line must contain the program/command name.",
24 "Comment lines are ignored.",
25 " ",
26 "Usage: @P [options] filename",
27 " ",
28 "Options:",
29 " -dry",
30 " Just print the command line in stdout.",
31 " -stdoptions", // List standard options like --help, -v, etc
32 " ",
33 "Example:",
34 " @P command.ift",
35 " ",
36 "Program uses system() function; its return value is not standardized,",
37 "therefore do not trust the return value of this function either.",
38 " ",
39 "See also: iftadd, iftdel, iftisval, iftvalc",
40 " ",
41 "Keywords: header, IFT, tool",
42 0};
43/*****************************************************************************/
44
45/*****************************************************************************/
46/* Turn on the globbing of the command line, since it is disabled by default in
47 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
48 In Unix&Linux wildcard command line processing is enabled by default. */
49/*
50#undef _CRT_glob
51#define _CRT_glob -1
52*/
53int _dowildcard = -1;
54/*****************************************************************************/
55
56/*****************************************************************************/
60int main(int argc, char **argv)
61{
62 int ai, help=0, version=0, verbose=1;
63 int ret;
64 char *cptr, iftfile[FILENAME_MAX];
65 int dryrun=0;
66
67
68 /*
69 * Get arguments
70 */
71 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
72 iftfile[0]=(char)0;
73 /* Options */
74 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
75 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
76 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
77 if(strcasecmp(cptr, "DRY")==0) {
78 dryrun=1; continue;
79 }
80 fprintf(stderr, "Error: invalid option '%s'.\n", argv[ai]);
81 return(1);
82 } else break;
83
84 TPCSTATUS status; statusInit(&status);
85 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
86 status.verbose=verbose-1;
87
88 /* Print help or version? */
89 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
90 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
91 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
92
93 /* Process other arguments, starting from the first non-option */
94 if(ai<argc) strlcpy(iftfile, argv[ai++], FILENAME_MAX);
95 if(ai<argc) {
96 fprintf(stderr, "Error: too many arguments.\n");
97 return(1);
98 }
99
100 /* Is something missing? */
101 if(!iftfile[0]) {
102 fprintf(stderr, "Error: missing command parameter file; use option --help\n");
103 return(1);
104 }
105
106 /* In verbose mode print arguments and options */
107 if(verbose>1) {
108 printf("iftfile := %s\n", iftfile);
109 printf("dryrun := %d\n", dryrun);
110 }
111
112 /* Check that command processor is available, if not a dry run */
113 if(dryrun==0 && system(NULL)==0) {
114 fprintf(stderr, "Error: no command processor available.\n");
115 return(10);
116 }
117
118
119 /*
120 * Read IFT file
121 */
122 if(verbose>1) printf("reading %s\n", iftfile);
123 IFT ift;
124 iftInit(&ift);
125 FILE *fp=fopen(iftfile, "r"); if(fp==NULL) {
126 fprintf(stderr, "Error: cannot open file %s\n", iftfile);
127 return(2);
128 }
129 ret=iftRead(&ift, fp, 0, 0, &status); fclose(fp);
130 if(ret) {
131 fprintf(stderr, "Error (%d): %s\n", ret, errorMsg(status.error));
132 iftFree(&ift); return(2);
133 }
134 if(verbose>2) printf("list size: %d item(s)\n", ift.keyNr);
135
136 /* Calculate the length of command-line */
137 int i, len=0;
138 for(i=0; i<ift.keyNr; i++) {
139 if(i>0) len++;
140 len+=strlen(ift.item[i].value);
141 }
142 if(i<1) {
143 fprintf(stderr, "Error: invalid file contents.\n");
144 iftFree(&ift); return(3);
145 }
146
147
148 /* Join the values */
149 char cmdline[len+1]; cmdline[0]=(char)0;
150 for(i=0; i<ift.keyNr; i++) {
151 if(i>0) strcat(cmdline, " ");
152 strcat(cmdline, ift.item[i].value);
153 }
154 iftFree(&ift);
155
156 /* If dry run was requested, then print it and exit */
157 if(dryrun!=0) {
158 fflush(stdout);
159 fprintf(stdout, "%s\n", cmdline);
160 fflush(stdout);
161 return(0);
162 }
163
164 /* In verbose mode print it too */
165 if(verbose>1) {
166 fflush(stdout);
167 fprintf(stdout, "command := %s\n", cmdline);
168 fflush(stdout);
169 }
170
171 /* Otherwise try to execute the command */
172 ret=system(cmdline); // return value is not standardized, so do not trust it too much
173 // in case of error it may even be that we never get this far
174 if(ret!=0 || verbose>2) printf(" %d returned.\n", ret);
175 fflush(stdout);
176 return(ret);
177}
178/*****************************************************************************/
179
180/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
void iftInit(IFT *ift)
Definition ift.c:21
int iftRead(IFT *ift, FILE *fp, int is_key_required, int is_comment_accepted, TPCSTATUS *status)
Definition iftio.c:130
int tpcProcessStdOptions(const char *s, int *print_usage, int *print_version, int *verbose_level)
Definition proginfo.c:47
int tpcHtmlUsage(const char *program, char *text[], const char *path)
Definition proginfo.c:169
void tpcPrintBuild(const char *program, FILE *fp)
Definition proginfo.c:339
void tpcPrintUsage(const char *program, char *text[], FILE *fp)
Definition proginfo.c:114
void statusInit(TPCSTATUS *s)
Definition statusmsg.c:104
char * errorMsg(tpcerror e)
Definition statusmsg.c:68
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Definition stringext.c:632
char * value
Definition tpcift.h:37
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.