TPCCLIB
Loading...
Searching...
No Matches
tacnames.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 <ctype.h>
14#include <math.h>
15/*****************************************************************************/
16#include "tpcextensions.h"
17#include "tpcift.h"
18#include "tpctac.h"
19/*****************************************************************************/
20
21/*****************************************************************************/
22static char *info[] = {
23 "List TAC names inside a TAC files.",
24 " ",
25 "Usage: @P [options] files",
26 " ",
27 "Options:",
28 " -missing",
29 " List only files that contain TAC(s) with missing name.",
30 " -errorifmissing",
31 " Return error code if TAC(s) with missing name.",
32 " -stdoptions", // List standard options like --help, -v, etc
33 " ",
34 "See also: taclist, tacren, tacadd, tacformat, iftren",
35 " ",
36 "Keywords: TAC, tool, rename, simulation",
37 0};
38/*****************************************************************************/
39
40/*****************************************************************************/
41/* Turn on the globbing of the command line, since it is disabled by default in
42 mingw-w64 (_dowildcard=0); in MinGW32 define _CRT_glob instead, if necessary;
43 In Unix&Linux wildcard command line processing is enabled by default. */
44/*
45#undef _CRT_glob
46#define _CRT_glob -1
47*/
48int _dowildcard = -1;
49/*****************************************************************************/
50
51/*****************************************************************************/
55int main(int argc, char **argv)
56{
57 int ai, help=0, version=0, verbose=1;
58 int ret, listMissing=0, errorIfMissing=0;
59 char *cptr;
60
61
62 /*
63 * Get arguments
64 */
65 if(argc==1) {tpcPrintUsage(argv[0], info, stderr); return(1);}
66 /* Options */
67 for(ai=1; ai<argc; ai++) if(*argv[ai]=='-') {
68 if(tpcProcessStdOptions(argv[ai], &help, &version, &verbose)==0) continue;
69 cptr=argv[ai]+1; if(*cptr=='-') cptr++; if(!*cptr) continue;
70 if(strncasecmp(cptr, "MISSING", 3)==0) {
71 listMissing=1; continue;
72 } else if(strncasecmp(cptr, "ERRORIFMISSING", 3)==0) {
73 errorIfMissing=1; continue;
74 }
75 fprintf(stderr, "Error: invalid option '%s'\n", argv[ai]);
76 return(1);
77 } else break; // tac name argument may start with '-'
78
79 TPCSTATUS status; statusInit(&status);
80 statusSet(&status, __func__, __FILE__, __LINE__, TPCERROR_OK);
81 status.verbose=verbose-1;
82
83 /* Print help or version? */
84 if(help==2) {tpcHtmlUsage(argv[0], info, ""); return(0);}
85 if(help) {tpcPrintUsage(argv[0], info, stdout); return(0);}
86 if(version) {tpcPrintBuild(argv[0], stdout); return(0);}
87
88 /* Store file names */
89 if(ai>=argc) {fprintf(stderr, "Error: missing filenames.\n"); return(1);}
90 IFT fnames; iftInit(&fnames);
91 for(; ai<argc; ai++) iftPut(&fnames, "", argv[ai], 0, &status);
92
93
94 /* In verbose mode print arguments and options */
95 if(verbose>1) {
96 printf("listMissing := %d\n", listMissing);
97 printf("errorIfMissing := %d\n", errorIfMissing);
98 printf("fileNr := %d\n", fnames.keyNr);
99 }
100
101
102 /* Collect TAC names */
103 int missingNr=0;
104 IFT tnames; iftInit(&tnames);
105 {
106 TAC tac; tacInit(&tac);
107 char tacfile[FILENAME_MAX], tacname[256];
108 for(int fi=0; fi<fnames.keyNr; fi++) {
109 strlcpy(tacfile, fnames.item[fi].value, FILENAME_MAX);
110 if(verbose>2) printf("reading %s\n", tacfile);
111 ret=tacRead(&tac, tacfile, &status);
112 if(ret!=TPCERROR_OK) {
113 fprintf(stderr, "Error: cannot read %s\n", tacfile);
114 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
115 iftFree(&fnames); iftFree(&tnames);
116 return(1);
117 }
118 for(int ti=0; ti<tac.tacNr; ti++) {
119 strlcpy(tacname, tac.c[ti].name, 256);
120 if(verbose>3) printf("\t%d\t%s\n", 1+ti, tacname);
121 if(!tacname[0] || strcmp(tacname, ".")==0) {missingNr++; continue;}
122 /* Check that TAC name is not already listed */
123 if(iftFindKey(&tnames, tacname, 0)>=0) continue;
124 iftPut(&tnames, tacname, "", 0, NULL);
125 }
126 tacFree(&tac);
127 }
128 }
129 if(verbose>2) {
130 printf("\n%d missing TAC names.\n", missingNr);
131 printf("\nTAC names:\n==========\n");
132 for(int ti=0; ti<tnames.keyNr; ti++) printf("%s\n", tnames.item[ti].key);
133 }
134 if(listMissing && missingNr==0) {
135 fprintf(stdout, "Note: no missing TAC names.\n");
136 iftFree(&fnames); iftFree(&tnames);
137 return(0);
138 }
139
140 /* List TAC names, and files that contain each name */
141 {
142 TAC tac; tacInit(&tac);
143 char tacfile[FILENAME_MAX], tacname[256];
144
145 if(listMissing==0) for(int ti=0; ti<tnames.keyNr; ti++) {
146 strlcpy(tacname, tnames.item[ti].key, 256);
147 printf("%s\n", tacname);
148 for(int fi=0; fi<fnames.keyNr; fi++) {
149 strlcpy(tacfile, fnames.item[fi].value, FILENAME_MAX);
150 if(verbose>10) printf("reading %s\n", tacfile);
151 ret=tacRead(&tac, tacfile, &status);
152 if(ret!=TPCERROR_OK) {
153 fprintf(stderr, "Error: cannot read %s\n", tacfile);
154 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
155 iftFree(&fnames); iftFree(&tnames);
156 return(1);
157 }
158 for(int ti=0; ti<tac.tacNr; ti++) {
159 if(strcasecmp(tacname, tac.c[ti].name)==0) {
160 printf("\t%s\n", tacfile);
161 break;
162 }
163 }
164 tacFree(&tac);
165 }
166 }
167
168 if(missingNr>0) {
169 printf("Missing TAC name\n");
170 for(int fi=0; fi<fnames.keyNr; fi++) {
171 strlcpy(tacfile, fnames.item[fi].value, FILENAME_MAX);
172 if(verbose>10) printf("reading %s\n", tacfile);
173 ret=tacRead(&tac, tacfile, &status);
174 if(ret!=TPCERROR_OK) {
175 fprintf(stderr, "Error: cannot read %s\n", tacfile);
176 fprintf(stderr, "Error: %s\n", errorMsg(status.error));
177 iftFree(&fnames); iftFree(&tnames);
178 return(1);
179 }
180 for(int ti=0; ti<tac.tacNr; ti++) {
181 strlcpy(tacname, tac.c[ti].name, 256);
182 if(verbose>11) printf("\t%d\t%s\n", 1+ti, tacname);
183 if(!tacname[0] || strcmp(tacname, ".")==0) {
184 printf("\t%s\n", tacfile);
185 break;
186 }
187 }
188 tacFree(&tac);
189 }
190 }
191 }
192
193 iftFree(&fnames); iftFree(&tnames);
194
195 if(errorIfMissing!=0 && missingNr>0) {
196 fprintf(stderr, "Error: missing TAC name(s).\n");
197 return(9);
198 }
199 return(0);
200}
201/*****************************************************************************/
202
203/*****************************************************************************/
void iftFree(IFT *ift)
Definition ift.c:37
int iftPut(IFT *ift, const char *key, const char *value, char comment, TPCSTATUS *status)
Definition ift.c:63
void iftInit(IFT *ift)
Definition ift.c:21
int iftFindKey(IFT *ift, const char *key, int start_index)
Definition iftfind.c:30
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
char * key
Definition tpcift.h:32
Definition tpcift.h:43
IFT_ITEM * item
Definition tpcift.h:57
int keyNr
Definition tpcift.h:47
char name[MAX_TACNAME_LEN+1]
Definition tpctac.h:81
Definition tpctac.h:87
TACC * c
Definition tpctac.h:117
int tacNr
Definition tpctac.h:91
int verbose
Verbose level, used by statusPrint() etc.
tpcerror error
Error code.
void tacFree(TAC *tac)
Definition tac.c:106
void tacInit(TAC *tac)
Definition tac.c:24
int tacRead(TAC *d, const char *fname, TPCSTATUS *status)
Definition tacio.c:413
Header file for library libtpcextensions.
@ TPCERROR_OK
No error.
Header file for library libtpcift.
Header file for library libtpctac.