| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <unistd.h> |
|---|
| 4 | #include <string.h> |
|---|
| 5 | #include <malloc.h> |
|---|
| 6 | #include <ctype.h> |
|---|
| 7 | |
|---|
| 8 | #include "adlocal.h" |
|---|
| 9 | #include "arbdbt.h" |
|---|
| 10 | |
|---|
| 11 | /******************************************************************************************** |
|---|
| 12 | Parameter Functions |
|---|
| 13 | ********************************************************************************************/ |
|---|
| 14 | |
|---|
| 15 | struct gbl_param { |
|---|
| 16 | struct gbl_param *next; |
|---|
| 17 | GB_TYPES type; |
|---|
| 18 | char **addr; |
|---|
| 19 | const char *str; |
|---|
| 20 | const char *help_text; |
|---|
| 21 | }; |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | #define GBL_BEGIN_PARAMS struct gbl_param *params = 0 |
|---|
| 25 | |
|---|
| 26 | static int gbl_param_int(const char *str,int def, const char *help_text, struct gbl_param **pp, char **vaddr) |
|---|
| 27 | { |
|---|
| 28 | struct gbl_param *_gblp = (struct gbl_param *)GB_calloc(1,sizeof(struct gbl_param)); |
|---|
| 29 | _gblp->next = *pp; *pp = _gblp; _gblp->help_text = help_text; |
|---|
| 30 | _gblp->addr = vaddr; _gblp->type = GB_INT; _gblp->str = str; |
|---|
| 31 | _gblp->type = GB_INT; |
|---|
| 32 | return def; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | static const char *gbl_param_string(const char *str,const char *def, const char *help_text, struct gbl_param **pp, char **vaddr) { |
|---|
| 36 | struct gbl_param *_gblp = (struct gbl_param *)GB_calloc(1,sizeof(struct gbl_param)); |
|---|
| 37 | _gblp->next = *pp; *pp = _gblp; _gblp->help_text = help_text; |
|---|
| 38 | _gblp->addr = vaddr; _gblp->type = GB_INT; _gblp->str = str; |
|---|
| 39 | _gblp->type = GB_STRING; |
|---|
| 40 | return def; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /* |
|---|
| 44 | static float gbl_param_float(char *str,float def, char *help_text, struct gbl_param **pp, char **vaddr) { |
|---|
| 45 | struct gbl_param *_gblp = (struct gbl_param *)GB_calloc(1,sizeof(struct gbl_param)); |
|---|
| 46 | _gblp->next = *pp; *pp = _gblp; _gblp->help_text = help_text; |
|---|
| 47 | _gblp->addr = vaddr; _gblp->type = GB_INT; _gblp->str = str; |
|---|
| 48 | _gblp->type = GB_FLOAT; |
|---|
| 49 | return def; |
|---|
| 50 | } |
|---|
| 51 | */ |
|---|
| 52 | |
|---|
| 53 | static int gbl_param_bit(const char *str,int def, const char *help_text, struct gbl_param **pp, char **vaddr) { |
|---|
| 54 | struct gbl_param *_gblp = (struct gbl_param *)GB_calloc(1,sizeof(struct gbl_param)); |
|---|
| 55 | _gblp->next = *pp; *pp = _gblp; _gblp->help_text = help_text; |
|---|
| 56 | _gblp->addr = vaddr; _gblp->type = GB_INT; _gblp->str = str; |
|---|
| 57 | _gblp->type = GB_BIT; |
|---|
| 58 | return def; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | #define GBL_PARAM_INT(var,_str,_def,_help_text) int var = \ |
|---|
| 62 | gbl_param_int(_str,_def,_help_text, ¶ms, (char **)&var) |
|---|
| 63 | #define GBL_PARAM_STRING(var,_str,_def,_help_text) const char *var = \ |
|---|
| 64 | gbl_param_string(_str,_def,_help_text, ¶ms, (char **)&var) |
|---|
| 65 | #define GBL_PARAM_FLOAT(var,_str,_def,_help_text) float var = \ |
|---|
| 66 | gbl_param_float(_str,_def,_help_text, ¶ms, (char **)&var) |
|---|
| 67 | #define GBL_PARAM_BIT(var,_str,_def,_help_text) int var = \ |
|---|
| 68 | gbl_param_bit(_str,_def,_help_text, ¶ms, (char **)&var) |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | #define GBL_TRACE_PARAMS(argc,argv) { \ |
|---|
| 73 | GB_ERROR error = trace_params(argc,argv,params,com); \ |
|---|
| 74 | if (error) { GBL_END_PARAMS; return error; }; }; |
|---|
| 75 | |
|---|
| 76 | #define GBL_END_PARAMS { struct gbl_param *_gblp; while (params) { \ |
|---|
| 77 | _gblp = params; params = params->next; \ |
|---|
| 78 | free((char *)_gblp); } }; |
|---|
| 79 | |
|---|
| 80 | #define GBL_CHECK_FREE_PARAM(nr,cnt) if (nr+cnt >= GBL_MAX_ARGUMENTS) {\ |
|---|
| 81 | return "Max Parameters exceeded";} |
|---|
| 82 | |
|---|
| 83 | GB_ERROR trace_params(int argc, GBL *argv, struct gbl_param *ppara, char *com) { |
|---|
| 84 | int i; |
|---|
| 85 | int len; |
|---|
| 86 | struct gbl_param *para; |
|---|
| 87 | for (i=0;i<argc;i++) { |
|---|
| 88 | for (para = ppara; para; para = para->next) { |
|---|
| 89 | len = strlen(para->str); |
|---|
| 90 | if (!strncmp(argv[i].str,para->str,len)) { |
|---|
| 91 | switch(para->type) { |
|---|
| 92 | case GB_STRING: |
|---|
| 93 | *para->addr = argv[i].str+len; |
|---|
| 94 | break; |
|---|
| 95 | case GB_INT: |
|---|
| 96 | *((int *)para->addr) = atoi(argv[i].str+len); |
|---|
| 97 | break; |
|---|
| 98 | case GB_FLOAT: |
|---|
| 99 | *((float *)para->addr) = atof(argv[i].str+len); |
|---|
| 100 | break; |
|---|
| 101 | case GB_BIT: |
|---|
| 102 | *((float *)para->addr) += 1; |
|---|
| 103 | break; |
|---|
| 104 | default: |
|---|
| 105 | GB_internal_error("ACI: Unknown Parameter Type"); |
|---|
| 106 | break; |
|---|
| 107 | } |
|---|
| 108 | break; |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | if (!para) { /* error */ |
|---|
| 112 | int pcount = 0; |
|---|
| 113 | struct gbl_param **params; |
|---|
| 114 | int k; |
|---|
| 115 | char *res; |
|---|
| 116 | void *str = GBS_stropen(1000); |
|---|
| 117 | GB_ERROR err; |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | for (para = ppara; para; para = para->next) pcount++; |
|---|
| 121 | params = (struct gbl_param **)GB_calloc(sizeof(void *),pcount); |
|---|
| 122 | for (k = 0, para = ppara; para; para = para->next) params[k++] = para; |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | for (pcount--; pcount>=0; pcount--) { |
|---|
| 126 | para = params[pcount]; |
|---|
| 127 | GBS_strcat(str," "); |
|---|
| 128 | GBS_strcat(str,para->str); |
|---|
| 129 | switch(para->type) { |
|---|
| 130 | case GB_STRING: |
|---|
| 131 | GBS_strcat(str,"STRING"); |
|---|
| 132 | break; |
|---|
| 133 | case GB_INT: |
|---|
| 134 | GBS_strcat(str,"INT"); |
|---|
| 135 | break; |
|---|
| 136 | case GB_FLOAT: |
|---|
| 137 | GBS_strcat(str,"REAL"); |
|---|
| 138 | break; |
|---|
| 139 | default: |
|---|
| 140 | GBS_strcat(str," "); |
|---|
| 141 | break; |
|---|
| 142 | } |
|---|
| 143 | GBS_strcat(str,"\t\t;"); |
|---|
| 144 | GBS_strcat(str,para->help_text); |
|---|
| 145 | GBS_strcat(str,"\n"); |
|---|
| 146 | } |
|---|
| 147 | GB_DELETE(params); |
|---|
| 148 | res = GBS_strclose(str,0); |
|---|
| 149 | err = GB_export_error("Unknown Parameter '%s' in command '%s'\n\tPARAMETERS:\n%s",argv[i].str,com,res); |
|---|
| 150 | free(res); |
|---|
| 151 | return err; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | return 0; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | /******************************************************************************************** |
|---|
| 159 | String Functions |
|---|
| 160 | ********************************************************************************************/ |
|---|
| 161 | |
|---|
| 162 | GB_ERROR gbl_count(GBDATA *gb_species, char *com, |
|---|
| 163 | int argcinput, GBL *argvinput, |
|---|
| 164 | int argcparam,GBL *argvparam, |
|---|
| 165 | int *argcout, GBL **argvout) { |
|---|
| 166 | int i; |
|---|
| 167 | char tab[256]; /* if tab[char] count 'char' */ |
|---|
| 168 | char result[100]; |
|---|
| 169 | GBUSE(gb_species);GBUSE(com); |
|---|
| 170 | |
|---|
| 171 | if (argcparam!=1) return "count syntax: count(\"characters to count\")"; |
|---|
| 172 | |
|---|
| 173 | for (i=0;i<256;i++) { |
|---|
| 174 | if (strchr(argvparam[0].str,i)) tab[i] = 1; |
|---|
| 175 | else tab[i] = 0; |
|---|
| 176 | } |
|---|
| 177 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 178 | for (i=0;i<argcinput;i++) { /* go through all orig streams */ |
|---|
| 179 | char *p; |
|---|
| 180 | long sum = 0; /* count frequencies */ |
|---|
| 181 | p = argvinput[i].str; |
|---|
| 182 | while (*p){ |
|---|
| 183 | sum += tab[(unsigned int)*(p++)]; |
|---|
| 184 | } |
|---|
| 185 | sprintf(result,"%li ",sum); |
|---|
| 186 | (*argvout)[(*argcout)++].str = GB_STRDUP(result); /* export result string */ |
|---|
| 187 | } |
|---|
| 188 | return 0; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | GB_ERROR gbl_len(GBDATA *gb_species, char *com, |
|---|
| 192 | int argcinput, GBL *argvinput, |
|---|
| 193 | int argcparam,GBL *argvparam, |
|---|
| 194 | int *argcout, GBL **argvout) |
|---|
| 195 | { |
|---|
| 196 | int i; |
|---|
| 197 | char tab[256]; /* if tab[char] count 'char' */ |
|---|
| 198 | char result[100]; |
|---|
| 199 | const char *option; |
|---|
| 200 | |
|---|
| 201 | GBUSE(gb_species);GBUSE(com); |
|---|
| 202 | |
|---|
| 203 | if (argcparam == 0) option = ""; |
|---|
| 204 | else option = argvparam[0].str; |
|---|
| 205 | if (argcparam>=2) return "len syntax: len[(\"characters not to count\")]"; |
|---|
| 206 | |
|---|
| 207 | for (i=0;i<256;i++) { |
|---|
| 208 | if (strchr(option,i)) tab[i] = 0; |
|---|
| 209 | else tab[i] = 1; |
|---|
| 210 | } |
|---|
| 211 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 212 | for (i=0;i<argcinput;i++) { /* go through all orig streams */ |
|---|
| 213 | char *p; |
|---|
| 214 | long sum = 0; /* count frequencies */ |
|---|
| 215 | p = argvinput[i].str; |
|---|
| 216 | while (*p){ |
|---|
| 217 | sum += tab[(unsigned int)*(p++)]; |
|---|
| 218 | } |
|---|
| 219 | sprintf(result,"%li ",sum); |
|---|
| 220 | (*argvout)[(*argcout)++].str = GB_STRDUP(result); /* export result string */ |
|---|
| 221 | } |
|---|
| 222 | return 0; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | GB_ERROR gbl_remove(GBDATA *gb_species, char *com, |
|---|
| 226 | int argcinput, GBL *argvinput, |
|---|
| 227 | int argcparam,GBL *argvparam, |
|---|
| 228 | int *argcout, GBL **argvout) { |
|---|
| 229 | int i; |
|---|
| 230 | char tab[256]; /* if tab[char] count 'char' */ |
|---|
| 231 | GBUSE(gb_species);GBUSE(com); |
|---|
| 232 | |
|---|
| 233 | |
|---|
| 234 | if (argcparam!=1) return "remove syntax: remove(\"characters to remove\")"; |
|---|
| 235 | |
|---|
| 236 | for (i=0;i<256;i++) { |
|---|
| 237 | if (strchr(argvparam[0].str,i)) tab[i] = 1; |
|---|
| 238 | else tab[i] = 0; |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 242 | for (i=0;i<argcinput;i++) { /* go through all orig streams */ |
|---|
| 243 | void *strstruct; |
|---|
| 244 | char *p; |
|---|
| 245 | strstruct = GBS_stropen(1000); |
|---|
| 246 | for (p = argvinput[i].str;*p;p++){ |
|---|
| 247 | if (!tab[(unsigned int)*p]) { |
|---|
| 248 | GBS_chrcat(strstruct,*p); |
|---|
| 249 | } |
|---|
| 250 | } |
|---|
| 251 | (*argvout)[(*argcout)++].str = GBS_strclose(strstruct,1); |
|---|
| 252 | /* export result string */ |
|---|
| 253 | } |
|---|
| 254 | return 0; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | GB_ERROR gbl_keep(GBDATA *gb_species, char *com, |
|---|
| 258 | int argcinput, GBL *argvinput, |
|---|
| 259 | int argcparam,GBL *argvparam, |
|---|
| 260 | int *argcout, GBL **argvout) { |
|---|
| 261 | int i; |
|---|
| 262 | char tab[256]; /* if tab[char] count 'char' */ |
|---|
| 263 | GBUSE(gb_species);GBUSE(com); |
|---|
| 264 | |
|---|
| 265 | |
|---|
| 266 | if (argcparam!=1) return "keep syntax: keep(\"characters not to remove\")"; |
|---|
| 267 | |
|---|
| 268 | for (i=0;i<256;i++) { |
|---|
| 269 | if (strchr(argvparam[0].str,i)) tab[i] = 0; |
|---|
| 270 | else tab[i] = 1; |
|---|
| 271 | } |
|---|
| 272 | |
|---|
| 273 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 274 | for (i=0;i<argcinput;i++) { /* go through all orig streams */ |
|---|
| 275 | void *strstruct; |
|---|
| 276 | char *p; |
|---|
| 277 | strstruct = GBS_stropen(1000); |
|---|
| 278 | for (p = argvinput[i].str;*p;p++){ |
|---|
| 279 | if (!tab[(unsigned int)*p]) { |
|---|
| 280 | GBS_chrcat(strstruct,*p); |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | (*argvout)[(*argcout)++].str = GBS_strclose(strstruct,1); |
|---|
| 284 | /* export result string */ |
|---|
| 285 | } |
|---|
| 286 | return 0; |
|---|
| 287 | } |
|---|
| 288 | |
|---|
| 289 | GB_ERROR gbl_echo(GBDATA *gb_species, char *com, |
|---|
| 290 | int argcinput, GBL *argvinput, |
|---|
| 291 | int argcparam,GBL *argvparam, |
|---|
| 292 | int *argcout, GBL **argvout) { |
|---|
| 293 | int i; |
|---|
| 294 | argcinput = argcinput; |
|---|
| 295 | argvinput = argvinput; |
|---|
| 296 | GBUSE(gb_species);GBUSE(com); |
|---|
| 297 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 298 | for (i=0;i<argcparam;i++) { /* go through all in streams */ |
|---|
| 299 | char *p; |
|---|
| 300 | p = argvparam[i].str; |
|---|
| 301 | (*argvout)[(*argcout)++].str = GB_STRDUP(p); /* export result string */ |
|---|
| 302 | } |
|---|
| 303 | return 0; |
|---|
| 304 | } |
|---|
| 305 | |
|---|
| 306 | GB_ERROR _gbl_mid( int argcinput, GBL *argvinput, |
|---|
| 307 | int *argcout, GBL **argvout, |
|---|
| 308 | int start,int mstart,int end,int relend) { |
|---|
| 309 | int i; |
|---|
| 310 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 311 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 312 | char *p; |
|---|
| 313 | int c; |
|---|
| 314 | |
|---|
| 315 | int len; |
|---|
| 316 | int nstart = start; |
|---|
| 317 | int nend = end; |
|---|
| 318 | |
|---|
| 319 | p = argvinput[i].str; |
|---|
| 320 | len = strlen(p); |
|---|
| 321 | if (nstart<0) nstart = len - mstart; |
|---|
| 322 | if (nend<0) nend = len - relend; /* check rel len */ |
|---|
| 323 | |
|---|
| 324 | if (nstart>len ) nstart = len; /* check boundaries */ |
|---|
| 325 | if (nstart<0) nstart = 0; |
|---|
| 326 | if (nend>len) nend = len; |
|---|
| 327 | if (nend<nstart) nend = nstart; |
|---|
| 328 | |
|---|
| 329 | c = p[nend]; |
|---|
| 330 | p[nend] =0; |
|---|
| 331 | (*argvout)[(*argcout)++].str = GB_STRDUP(p+nstart); /* export result string */ |
|---|
| 332 | p[nend] = c; |
|---|
| 333 | } |
|---|
| 334 | return 0; |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | GB_ERROR gbl_dd(GBDATA *gb_species, char *com, |
|---|
| 338 | int argcinput, GBL *argvinput, |
|---|
| 339 | int argcparam,GBL *argvparam, |
|---|
| 340 | int *argcout, GBL **argvout) { |
|---|
| 341 | if (argcparam!=0) return "dd syntax: dd (no parameters)"; |
|---|
| 342 | argvparam = argvparam; |
|---|
| 343 | GBUSE(gb_species);GBUSE(com); |
|---|
| 344 | return _gbl_mid(argcinput, argvinput,argcout, argvout, |
|---|
| 345 | 0, 0, -1, 0); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | GB_ERROR gbl_head(GBDATA *gb_species, char *com, |
|---|
| 349 | int argcinput, GBL *argvinput, |
|---|
| 350 | int argcparam,GBL *argvparam, |
|---|
| 351 | int *argcout, GBL **argvout) { |
|---|
| 352 | int start; |
|---|
| 353 | GBUSE(gb_species);GBUSE(com); |
|---|
| 354 | if (argcparam!=1) return "head syntax: head(#start)"; |
|---|
| 355 | start = atoi(argvparam[0].str); |
|---|
| 356 | return _gbl_mid(argcinput, argvinput,argcout, argvout, |
|---|
| 357 | 0,0, start, -start); |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | GB_ERROR gbl_tail(GBDATA *gb_species, char *com, |
|---|
| 361 | int argcinput, GBL *argvinput, |
|---|
| 362 | int argcparam,GBL *argvparam, |
|---|
| 363 | int *argcout, GBL **argvout) { |
|---|
| 364 | int end; |
|---|
| 365 | GBUSE(gb_species);GBUSE(com); |
|---|
| 366 | if (argcparam!=1) return "tail syntax: tail(#length_of_tail)"; |
|---|
| 367 | end = atoi(argvparam[0].str); |
|---|
| 368 | return _gbl_mid(argcinput, argvinput,argcout, argvout, |
|---|
| 369 | -1, end, -1, 0); |
|---|
| 370 | } |
|---|
| 371 | |
|---|
| 372 | GB_ERROR gbl_mid0(GBDATA *gb_species, char *com, |
|---|
| 373 | int argcinput, GBL *argvinput, |
|---|
| 374 | int argcparam,GBL *argvparam, |
|---|
| 375 | int *argcout, GBL **argvout) { |
|---|
| 376 | int start; |
|---|
| 377 | int end; |
|---|
| 378 | if (argcparam!=2) return "mid syntax: mid(#start;#end)"; |
|---|
| 379 | GBUSE(gb_species);GBUSE(com); |
|---|
| 380 | start = atoi(argvparam[0].str); |
|---|
| 381 | end = atoi(argvparam[1].str); |
|---|
| 382 | return _gbl_mid(argcinput, argvinput, |
|---|
| 383 | argcout, argvout, |
|---|
| 384 | start, -start, end, -end); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | GB_ERROR gbl_mid(GBDATA *gb_species, char *com, |
|---|
| 388 | int argcinput, GBL *argvinput, |
|---|
| 389 | int argcparam,GBL *argvparam, |
|---|
| 390 | int *argcout, GBL **argvout) { |
|---|
| 391 | int start; |
|---|
| 392 | int end; |
|---|
| 393 | if (argcparam!=2) return "mid syntax: mid(#start;#end)"; |
|---|
| 394 | GBUSE(gb_species);GBUSE(com); |
|---|
| 395 | start = atoi(argvparam[0].str)-1; |
|---|
| 396 | end = atoi(argvparam[1].str)-1; |
|---|
| 397 | return _gbl_mid(argcinput, argvinput, |
|---|
| 398 | argcout, argvout, |
|---|
| 399 | start, -start, end, -end); |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | GB_ERROR gbl_tab(GBDATA *gb_species, char *com, |
|---|
| 403 | int argcinput, GBL *argvinput, |
|---|
| 404 | int argcparam,GBL *argvparam, |
|---|
| 405 | int *argcout, GBL **argvout) { |
|---|
| 406 | int i,j; |
|---|
| 407 | int tab; |
|---|
| 408 | if (argcparam!=1) return "tab syntax: tab(#tab)"; |
|---|
| 409 | GBUSE(gb_species);GBUSE(com); |
|---|
| 410 | tab = atoi(argvparam[0].str); |
|---|
| 411 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 412 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 413 | char *p; |
|---|
| 414 | if ((int)strlen(argvinput[i].str)>= tab) { |
|---|
| 415 | (*argvout)[(*argcout)++].str = GB_STRDUP(argvinput[i].str); |
|---|
| 416 | }else{ |
|---|
| 417 | p = (char *)GB_calloc(sizeof(char),tab+1); |
|---|
| 418 | strcpy(p,argvinput[i].str); |
|---|
| 419 | for (j= strlen(p);j<tab;j++){ |
|---|
| 420 | p[j] = ' '; |
|---|
| 421 | } |
|---|
| 422 | (*argvout)[(*argcout)++].str = p; |
|---|
| 423 | } |
|---|
| 424 | } |
|---|
| 425 | return 0; |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | GB_ERROR gbl_cut(GBDATA *gb_species, char *com, |
|---|
| 429 | int argcinput, GBL *argvinput, |
|---|
| 430 | int argcparam,GBL *argvparam, |
|---|
| 431 | int *argcout, GBL **argvout) { |
|---|
| 432 | int i; |
|---|
| 433 | GBUSE(gb_species);GBUSE(com); |
|---|
| 434 | |
|---|
| 435 | GBL_CHECK_FREE_PARAM(*argcout,argcparam); |
|---|
| 436 | for (i=0; i<argcparam;i++) { |
|---|
| 437 | int j = atoi(argvparam[i].str)-1; |
|---|
| 438 | if (j<0) continue; |
|---|
| 439 | if (j>= argcinput) continue; |
|---|
| 440 | (*argvout)[(*argcout)++].str = GB_STRDUP(argvinput[j].str);/* export result string */ |
|---|
| 441 | } |
|---|
| 442 | return 0; |
|---|
| 443 | } |
|---|
| 444 | |
|---|
| 445 | /******************************************************************************************** |
|---|
| 446 | Extended String Functions |
|---|
| 447 | ********************************************************************************************/ |
|---|
| 448 | |
|---|
| 449 | GB_ERROR gbl_extract_words(GBDATA *gb_species, char *com, |
|---|
| 450 | int argcinput, GBL *argvinput, |
|---|
| 451 | int argcparam,GBL *argvparam, |
|---|
| 452 | int *argcout, GBL **argvout) { |
|---|
| 453 | int i; |
|---|
| 454 | float len; |
|---|
| 455 | if (argcparam==0) return "extract_words needs two parameters:\nextract_words(\"Characters\",min_characters"; |
|---|
| 456 | if (argcparam>2) return "extract_words needs two parameters:\nextract_words(\"Characters\",min_characters"; |
|---|
| 457 | GBUSE(gb_species);GBUSE(com); |
|---|
| 458 | len = atof(argvparam[1].str); |
|---|
| 459 | |
|---|
| 460 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 461 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 462 | char *res; |
|---|
| 463 | res = GBS_extract_words(argvinput[i].str, argvparam[0].str, len, 1); |
|---|
| 464 | if (!res) return GB_get_error(); |
|---|
| 465 | (*argvout)[(*argcout)++].str = res; |
|---|
| 466 | } |
|---|
| 467 | return 0; |
|---|
| 468 | } |
|---|
| 469 | GB_ERROR gbl_extract_sequence(GBDATA *gb_species, char *com, |
|---|
| 470 | int argcinput, GBL *argvinput, |
|---|
| 471 | int argcparam,GBL *argvparam, |
|---|
| 472 | int *argcout, GBL **argvout) { |
|---|
| 473 | int i; |
|---|
| 474 | float len; |
|---|
| 475 | GB_ERROR errs = "extract_sequence needs two parameters:\nextract_words(\"Characters\",min_rel_characters [0.0-1.0]"; |
|---|
| 476 | if (argcparam==0) return errs; |
|---|
| 477 | if (argcparam>2) return errs; |
|---|
| 478 | GBUSE(gb_species);GBUSE(com); |
|---|
| 479 | len = atof(argvparam[1].str); |
|---|
| 480 | if (len <0.0 || len > 1.0) return errs; |
|---|
| 481 | |
|---|
| 482 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 483 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 484 | char *res; |
|---|
| 485 | res = GBS_extract_words(argvinput[i].str, argvparam[0].str, len, 0); |
|---|
| 486 | if (!res) return GB_get_error(); |
|---|
| 487 | (*argvout)[(*argcout)++].str = res; |
|---|
| 488 | } |
|---|
| 489 | return 0; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | GB_ERROR gbl_check(GBDATA *gb_species, char *com, |
|---|
| 493 | int argcinput, GBL *argvinput, |
|---|
| 494 | int argcparam,GBL *argvparam, |
|---|
| 495 | int *argcout, GBL **argvout) { |
|---|
| 496 | int i; |
|---|
| 497 | GBL_BEGIN_PARAMS; |
|---|
| 498 | GBL_PARAM_STRING(exclude,"exclude=","","Remove characters 'str' before calculating"); |
|---|
| 499 | GBL_PARAM_BIT(upper,"toupper",0,"Convert all characters to uppercase before calculating"); |
|---|
| 500 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 501 | GBL_END_PARAMS; |
|---|
| 502 | GBUSE(gb_species);GBUSE(com); |
|---|
| 503 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 504 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 505 | char buf[100]; |
|---|
| 506 | long id; |
|---|
| 507 | id = GBS_checksum(argvinput[i].str,upper,exclude); |
|---|
| 508 | sprintf(buf,"%lX",id); |
|---|
| 509 | (*argvout)[(*argcout)++].str = GB_STRDUP(buf); |
|---|
| 510 | } |
|---|
| 511 | return 0; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | GB_ERROR gbl_gcgcheck(GBDATA *gb_species, char *com, |
|---|
| 515 | int argcinput, GBL *argvinput, |
|---|
| 516 | int argcparam,GBL *argvparam, |
|---|
| 517 | int *argcout, GBL **argvout) { |
|---|
| 518 | int i; |
|---|
| 519 | GBL_BEGIN_PARAMS; |
|---|
| 520 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 521 | GBL_END_PARAMS; |
|---|
| 522 | GBUSE(gb_species);GBUSE(com); |
|---|
| 523 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 524 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 525 | char buf[100]; |
|---|
| 526 | long id; |
|---|
| 527 | id = GBS_gcgchecksum(argvinput[i].str); |
|---|
| 528 | sprintf(buf,"%li",id); |
|---|
| 529 | (*argvout)[(*argcout)++].str = GB_STRDUP(buf); |
|---|
| 530 | } |
|---|
| 531 | return 0; |
|---|
| 532 | } |
|---|
| 533 | |
|---|
| 534 | /******************************************************************************************** |
|---|
| 535 | SRT |
|---|
| 536 | ********************************************************************************************/ |
|---|
| 537 | |
|---|
| 538 | GB_ERROR gbl_srt(GBDATA *gb_species, char *com, |
|---|
| 539 | int argcinput, GBL *argvinput, |
|---|
| 540 | int argcparam,GBL *argvparam, |
|---|
| 541 | int *argcout, GBL **argvout) { |
|---|
| 542 | int i; |
|---|
| 543 | int j; |
|---|
| 544 | com = com; |
|---|
| 545 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 546 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 547 | char *source = argvinput[i].str; |
|---|
| 548 | for (j=0;j<argcparam;j++) { |
|---|
| 549 | char *hs = GBS_string_eval(source,argvparam[j].str,gb_species); |
|---|
| 550 | if (!hs) return GB_get_error(); |
|---|
| 551 | if (source != argvinput[i].str) free(source); |
|---|
| 552 | source = hs; |
|---|
| 553 | } |
|---|
| 554 | if (source != argvinput[i].str){ |
|---|
| 555 | (*argvout)[(*argcout)++].str = source; |
|---|
| 556 | }else{ |
|---|
| 557 | (*argvout)[(*argcout)++].str = GB_STRDUP(source); |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | return 0; |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | /******************************************************************************************** |
|---|
| 564 | Calculator Functions |
|---|
| 565 | ********************************************************************************************/ |
|---|
| 566 | |
|---|
| 567 | GB_ERROR gbl_calculator(GBDATA *gb_species, char *com, |
|---|
| 568 | int argcinput, GBL *argvinput, |
|---|
| 569 | int argcparam,GBL *argvparam, |
|---|
| 570 | int *argcout, GBL **argvout) { |
|---|
| 571 | int val1; |
|---|
| 572 | int val2; |
|---|
| 573 | int val =0; |
|---|
| 574 | char result[100]; |
|---|
| 575 | GBUSE(gb_species);GBUSE(com); |
|---|
| 576 | |
|---|
| 577 | if (argcparam>=2) return "calculating function syntax: plus[(#add)]"; |
|---|
| 578 | if (!argcinput) return "calculating function: missing input"; |
|---|
| 579 | |
|---|
| 580 | val1 = atoi(argvinput[0].str); |
|---|
| 581 | |
|---|
| 582 | if (argcparam==1){ |
|---|
| 583 | val2 = atoi(argvparam[0].str); |
|---|
| 584 | }else{ |
|---|
| 585 | if (argcinput<=1) return "calculating function: missing second input"; |
|---|
| 586 | val2 = atoi(argvinput[1].str); |
|---|
| 587 | } |
|---|
| 588 | if (!strcmp(com,"plus")) val = val1+val2; |
|---|
| 589 | else if (!strcmp(com,"minus")) val = val1-val2; |
|---|
| 590 | else if (!strcmp(com,"mult")) val = val1*val2; |
|---|
| 591 | else if (!strcmp(com,"div")) { if (val2) val = val1/val2;else val = 0; } |
|---|
| 592 | else if (!strcmp(com,"per_cent")) { if (val2) val = val1*100/val2; else val =0; } |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | sprintf(result,"%i",val); |
|---|
| 596 | GBL_CHECK_FREE_PARAM(*argcout,1); |
|---|
| 597 | (*argvout)[(*argcout)++].str = GB_STRDUP(result); /* export result string */ |
|---|
| 598 | return 0; |
|---|
| 599 | } |
|---|
| 600 | /******************************************************************************************** |
|---|
| 601 | Database Functions |
|---|
| 602 | ********************************************************************************************/ |
|---|
| 603 | GB_ERROR gbl_readdb(GBDATA *gb_species, char *com, |
|---|
| 604 | int argcinput, GBL *argvinput, |
|---|
| 605 | int argcparam,GBL *argvparam, |
|---|
| 606 | int *argcout, GBL **argvout) |
|---|
| 607 | { |
|---|
| 608 | int i; |
|---|
| 609 | void *strstr = GBS_stropen(1024); |
|---|
| 610 | |
|---|
| 611 | GBUSE(com); |
|---|
| 612 | GBUSE(argcparam); |
|---|
| 613 | GBUSE(argvparam); |
|---|
| 614 | GBUSE(argcinput); |
|---|
| 615 | GBUSE(argvinput); |
|---|
| 616 | |
|---|
| 617 | for (i=0;i<argcparam;i++){ |
|---|
| 618 | GBDATA *gb = GB_search(gb_species,argvparam[i].str,GB_FIND); |
|---|
| 619 | char *val; |
|---|
| 620 | if(!gb) continue; |
|---|
| 621 | val = GB_read_as_string(gb); |
|---|
| 622 | GBS_strcat(strstr,val); |
|---|
| 623 | free(val); |
|---|
| 624 | } |
|---|
| 625 | (*argvout)[(*argcout)++].str = GBS_strclose(strstr,0); /* export result string */ |
|---|
| 626 | return 0; |
|---|
| 627 | } |
|---|
| 628 | |
|---|
| 629 | |
|---|
| 630 | /******************************************************************************************** |
|---|
| 631 | Sequence Functions |
|---|
| 632 | ********************************************************************************************/ |
|---|
| 633 | |
|---|
| 634 | |
|---|
| 635 | GB_ERROR gbl_sequence(GBDATA *gb_species, char *com, |
|---|
| 636 | int argcinput, GBL *argvinput, |
|---|
| 637 | int argcparam,GBL *argvparam, |
|---|
| 638 | int *argcout, GBL **argvout) { |
|---|
| 639 | char *use; |
|---|
| 640 | GBDATA *gb_seq; |
|---|
| 641 | if (argcparam!=0) return "\"sequence\" syntax: \"sequence\" (no parameters)"; |
|---|
| 642 | argvparam = argvparam; |
|---|
| 643 | com = com;GBUSE(argvinput); |
|---|
| 644 | if (argcinput==0) return "No input stream"; |
|---|
| 645 | GBL_CHECK_FREE_PARAM(*argcout,1); |
|---|
| 646 | use = GBT_get_default_alignment(GB_get_root(gb_species)); |
|---|
| 647 | gb_seq = GBT_read_sequence(gb_species,use); |
|---|
| 648 | if (gb_seq) { |
|---|
| 649 | (*argvout)[(*argcout)++].str = GB_read_string(gb_seq); /* export result string */ |
|---|
| 650 | }else{ |
|---|
| 651 | (*argvout)[(*argcout)++].str = GB_STRDUP("no sequence"); /* export result string */ |
|---|
| 652 | } |
|---|
| 653 | free(use); |
|---|
| 654 | return 0; |
|---|
| 655 | } |
|---|
| 656 | |
|---|
| 657 | GB_ERROR gbl_sequence_type(GBDATA *gb_species, char *com, |
|---|
| 658 | int argcinput, GBL *argvinput, |
|---|
| 659 | int argcparam,GBL *argvparam, |
|---|
| 660 | int *argcout, GBL **argvout) { |
|---|
| 661 | char *use; |
|---|
| 662 | if (argcparam!=0) return "\"sequence_type\" syntax: \"sequence\" (no parameters)"; |
|---|
| 663 | argvparam = argvparam; |
|---|
| 664 | com = com;GBUSE(argvinput); |
|---|
| 665 | GBL_CHECK_FREE_PARAM(*argcout,1); |
|---|
| 666 | if (argcinput==0) return "No input stream"; |
|---|
| 667 | use = GBT_get_default_alignment(GB_get_root(gb_species)); |
|---|
| 668 | (*argvout)[(*argcout)++].str = GBT_get_alignment_type_string(GB_get_root(gb_species),use); |
|---|
| 669 | free(use); |
|---|
| 670 | return 0; |
|---|
| 671 | } |
|---|
| 672 | |
|---|
| 673 | GB_ERROR gbl_format_sequence(GBDATA *gb_species, char *com, |
|---|
| 674 | int argcinput, GBL *argvinput, |
|---|
| 675 | int argcparam,GBL *argvparam, |
|---|
| 676 | int *argcout, GBL **argvout) { |
|---|
| 677 | int i; |
|---|
| 678 | void *strstruct; |
|---|
| 679 | char *p; |
|---|
| 680 | char buffer[256]; |
|---|
| 681 | char spr[256]; |
|---|
| 682 | |
|---|
| 683 | GBL_BEGIN_PARAMS; |
|---|
| 684 | GBL_PARAM_INT(firsttab,"firsttab=",10,"Indent first line"); |
|---|
| 685 | GBL_PARAM_INT(tab,"tab=",10,"Indent not first line"); |
|---|
| 686 | GBL_PARAM_BIT(numleft,"numleft",0,"Numbers left of sequence"); |
|---|
| 687 | GBL_PARAM_INT(gap,"gap=",10,"Insert ' ' every n sequence characters"); |
|---|
| 688 | GBL_PARAM_INT(width,"width=",50,"Sequence width (bases only)"); |
|---|
| 689 | GBL_PARAM_STRING(nl,"nl="," ","Break line at characters 'str'"); |
|---|
| 690 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 691 | GBL_END_PARAMS; |
|---|
| 692 | GBUSE(gb_species); |
|---|
| 693 | if (argcinput==0) return "No input stream"; |
|---|
| 694 | GBL_CHECK_FREE_PARAM(*argcout,1); |
|---|
| 695 | |
|---|
| 696 | strstruct = GBS_stropen(5000); |
|---|
| 697 | |
|---|
| 698 | if (!strcmp(com,"format")) { |
|---|
| 699 | for (i=0,p= argvinput[0].str; *p; i++) { |
|---|
| 700 | int len = strlen(p); |
|---|
| 701 | if (i==0) { |
|---|
| 702 | int j; |
|---|
| 703 | for (j=0;j<firsttab;j++) buffer[j] = ' '; |
|---|
| 704 | buffer[j] = 0; |
|---|
| 705 | GBS_strcat(strstruct,buffer); |
|---|
| 706 | } |
|---|
| 707 | if (len>=width) { /* cut too long lines */ |
|---|
| 708 | int j; |
|---|
| 709 | for (j=width-1;j>=0;j--){ |
|---|
| 710 | if (strchr(nl,p[j])) break; |
|---|
| 711 | } |
|---|
| 712 | if (j<0) { |
|---|
| 713 | GBS_strncat(strstruct,p,width); |
|---|
| 714 | p+=width; |
|---|
| 715 | }else{ |
|---|
| 716 | GBS_strncat(strstruct,p,j); |
|---|
| 717 | p += j+1; |
|---|
| 718 | } |
|---|
| 719 | buffer[0] = '\n'; |
|---|
| 720 | for (j=1;j<=tab;j++) buffer[j] = ' '; |
|---|
| 721 | buffer[j] = 0; |
|---|
| 722 | GBS_strcat(strstruct,buffer); |
|---|
| 723 | }else{ |
|---|
| 724 | GBS_strcat(strstruct,p); |
|---|
| 725 | p+=len; |
|---|
| 726 | } |
|---|
| 727 | } |
|---|
| 728 | }else{ |
|---|
| 729 | for (i=0,p= argvinput[0].str; *p; i++) { |
|---|
| 730 | if (i==0) { |
|---|
| 731 | if (numleft) { |
|---|
| 732 | sprintf(spr,"%%-%ii ",firsttab-1); |
|---|
| 733 | sprintf(buffer,spr,p-argvinput[0].str); |
|---|
| 734 | }else{ |
|---|
| 735 | int j; |
|---|
| 736 | for (j=0;j<firsttab;j++) buffer[j] = ' '; |
|---|
| 737 | buffer[j] = 0; |
|---|
| 738 | } |
|---|
| 739 | GBS_strcat(strstruct,buffer); |
|---|
| 740 | }else if (i%width ==0) { |
|---|
| 741 | if (numleft) { |
|---|
| 742 | sprintf(spr,"\n%%-%ii ",tab-1); |
|---|
| 743 | sprintf(buffer,spr,p-argvinput[0].str); |
|---|
| 744 | }else{ |
|---|
| 745 | int j; |
|---|
| 746 | buffer[0] = '\n'; |
|---|
| 747 | for (j=1;j<=tab;j++) buffer[j] = ' '; |
|---|
| 748 | buffer[j] = 0; |
|---|
| 749 | } |
|---|
| 750 | GBS_strcat(strstruct,buffer); |
|---|
| 751 | }else if (gap && (i%gap==0)){ |
|---|
| 752 | GBS_strcat(strstruct," "); |
|---|
| 753 | }; |
|---|
| 754 | GBS_chrcat(strstruct,*(p++)); |
|---|
| 755 | |
|---|
| 756 | } |
|---|
| 757 | } |
|---|
| 758 | (*argvout)[(*argcout)++].str = GBS_strclose(strstruct,1); |
|---|
| 759 | return 0; |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | /******************************************************************************************** |
|---|
| 763 | Filter Functions |
|---|
| 764 | ********************************************************************************************/ |
|---|
| 765 | |
|---|
| 766 | GBDATA *gbl_search_sai_species(const char *species, const char *sai) { |
|---|
| 767 | GBDATA *gbd; |
|---|
| 768 | if (sai) { |
|---|
| 769 | GBDATA *gb_cont = GB_find(gb_local->gbl.gb_main,"extended_data",0,down_level); |
|---|
| 770 | if (!gb_cont) return 0; |
|---|
| 771 | gbd = GBT_find_SAI_rel_exdata(gb_cont,sai); |
|---|
| 772 | }else{ |
|---|
| 773 | GBDATA *gb_cont = GB_find(gb_local->gbl.gb_main,"species_data",0,down_level); |
|---|
| 774 | if (!gb_cont) return 0; |
|---|
| 775 | gbd = GBT_find_species_rel_species_data(gb_cont,species); |
|---|
| 776 | } |
|---|
| 777 | return gbd; |
|---|
| 778 | } |
|---|
| 779 | |
|---|
| 780 | |
|---|
| 781 | GB_ERROR gbl_filter(GBDATA *gb_spec, char *com, |
|---|
| 782 | int argcinput, GBL *argvinput, |
|---|
| 783 | int argcparam,GBL *argvparam, |
|---|
| 784 | int *argcout, GBL **argvout) { |
|---|
| 785 | int i; |
|---|
| 786 | GBDATA *gb_species; |
|---|
| 787 | GBDATA *gb_seq; |
|---|
| 788 | GBDATA *gb_ali; |
|---|
| 789 | char *filter = 0; |
|---|
| 790 | long flen = 0; |
|---|
| 791 | |
|---|
| 792 | GBL_BEGIN_PARAMS; |
|---|
| 793 | GBL_PARAM_STRING(sai,"SAI=",0,"Use default Sequence of SAI as a filter"); |
|---|
| 794 | GBL_PARAM_STRING(species,"species=",0,"Use default Sequence of species as a filter"); |
|---|
| 795 | GBL_PARAM_STRING(exclude,"exclude=",0,"Exclude colums"); |
|---|
| 796 | GBL_PARAM_STRING(include,"include=",0,"Use only those columns whose filter occur in include"); |
|---|
| 797 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 798 | GBL_END_PARAMS; |
|---|
| 799 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 800 | GBUSE(gb_spec); |
|---|
| 801 | if (argcinput==0) return "No input stream"; |
|---|
| 802 | if (!exclude && ! include) return "Missing parameter: 'exclude=' or 'include='"; |
|---|
| 803 | if (!sai && !species) return "Missing parameter: 'SAI=' or 'species='"; |
|---|
| 804 | gb_species = gbl_search_sai_species(species,sai); |
|---|
| 805 | if (!gb_species) return "SAI/Species not found"; |
|---|
| 806 | |
|---|
| 807 | { char *use = GBT_get_default_alignment(gb_local->gbl.gb_main); |
|---|
| 808 | gb_ali = GB_find(gb_species,use,0,down_level); |
|---|
| 809 | free(use); |
|---|
| 810 | if (!gb_ali) { |
|---|
| 811 | return GB_export_error( "Your filter has no alignment '%s'",use); |
|---|
| 812 | } |
|---|
| 813 | } |
|---|
| 814 | |
|---|
| 815 | for ( gb_seq = GB_find(gb_ali,0,0,down_level); |
|---|
| 816 | gb_seq; |
|---|
| 817 | gb_seq = GB_find(gb_seq,0,0,search_next|this_level)){ |
|---|
| 818 | long type = GB_read_type(gb_seq); |
|---|
| 819 | if (type == GB_BITS) { |
|---|
| 820 | flen = GB_read_bits_count(gb_seq); |
|---|
| 821 | filter = GB_read_bits(gb_seq,'-','+'); |
|---|
| 822 | break; |
|---|
| 823 | } |
|---|
| 824 | if (type == GB_STRING) { |
|---|
| 825 | flen = GB_read_string_count(gb_seq); |
|---|
| 826 | filter = GB_read_string(gb_seq); |
|---|
| 827 | break; |
|---|
| 828 | } |
|---|
| 829 | } |
|---|
| 830 | if (!filter) return "I cannot find any string in your filter SAI/species"; |
|---|
| 831 | |
|---|
| 832 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 833 | char *dest = GB_STRDUP(argvinput[i].str); |
|---|
| 834 | char *source = dest; |
|---|
| 835 | char *org = source; |
|---|
| 836 | int j; |
|---|
| 837 | int c = 0; |
|---|
| 838 | if (exclude) { |
|---|
| 839 | for (j = 0; j <flen && (c= *(source++));j++){ |
|---|
| 840 | if (strchr(exclude, filter[j])) continue; |
|---|
| 841 | *(dest++) = c; |
|---|
| 842 | } |
|---|
| 843 | }else{ |
|---|
| 844 | for (j = 0; j <flen && (c= *(source++));j++){ |
|---|
| 845 | if (!strchr(include, filter[j])) continue; |
|---|
| 846 | *(dest++) = c; |
|---|
| 847 | } |
|---|
| 848 | } |
|---|
| 849 | if (c) while ( (c= *(source++)) ) { *(dest++) = c; }; |
|---|
| 850 | *(dest) = 0; |
|---|
| 851 | |
|---|
| 852 | (*argvout)[(*argcout)++].str = org; |
|---|
| 853 | } |
|---|
| 854 | free(filter); |
|---|
| 855 | return 0; |
|---|
| 856 | } |
|---|
| 857 | |
|---|
| 858 | GB_ERROR gbl_diff(GBDATA *gb_spec, char *com, |
|---|
| 859 | int argcinput, GBL *argvinput, |
|---|
| 860 | int argcparam,GBL *argvparam, |
|---|
| 861 | int *argcout, GBL **argvout) { |
|---|
| 862 | int i; |
|---|
| 863 | GBDATA *gb_species; |
|---|
| 864 | GBDATA *gb_seq; |
|---|
| 865 | char *filter = 0; |
|---|
| 866 | long flen; |
|---|
| 867 | char *use; |
|---|
| 868 | GBL_BEGIN_PARAMS; |
|---|
| 869 | GBL_PARAM_STRING(sai,"SAI=",0,"Use default Sequence of SAI as a filter"); |
|---|
| 870 | GBL_PARAM_STRING(species,"species=",0,"Use default Sequence of species as a filter"); |
|---|
| 871 | GBL_PARAM_STRING(equal,"equal=",".","symbol for equal characters"); |
|---|
| 872 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 873 | GBL_END_PARAMS; |
|---|
| 874 | GBL_CHECK_FREE_PARAM(*argcout,argcinput); |
|---|
| 875 | GBUSE(gb_spec); |
|---|
| 876 | if (argcinput==0) return "No input stream"; |
|---|
| 877 | if (!sai && !species) return "Missing parameter: 'SAI=' or 'species='"; |
|---|
| 878 | gb_species = gbl_search_sai_species(species,sai); |
|---|
| 879 | if (!gb_species) return GB_export_error("SAI/Species '%s/%s' not found",sai,species); |
|---|
| 880 | |
|---|
| 881 | use = GBT_get_default_alignment(gb_local->gbl.gb_main); |
|---|
| 882 | gb_seq = GBT_read_sequence(gb_species,use); |
|---|
| 883 | if (!gb_seq) { |
|---|
| 884 | GB_ERROR error = GB_export_error("SAI/Species '%s/%s' has no sequence '%s'",sai,species,use); |
|---|
| 885 | free(use); |
|---|
| 886 | return error; |
|---|
| 887 | } |
|---|
| 888 | free(use); |
|---|
| 889 | |
|---|
| 890 | filter = GB_read_string(gb_seq); |
|---|
| 891 | flen = strlen(filter); |
|---|
| 892 | |
|---|
| 893 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 894 | char *dest = GB_STRDUP(argvinput[i].str); |
|---|
| 895 | char *source = dest; |
|---|
| 896 | char *org = source; |
|---|
| 897 | int j; |
|---|
| 898 | int c =0; |
|---|
| 899 | for (j = 0; j <flen && (c= *(source++));j++){ |
|---|
| 900 | if ( c== filter[j] ) *(dest++) = equal[0]; |
|---|
| 901 | else *(dest++) = c; |
|---|
| 902 | } |
|---|
| 903 | if (c) while ( (c= *(source++)) ) { *(dest++) = c; }; |
|---|
| 904 | *(dest) = 0; |
|---|
| 905 | (*argvout)[(*argcout)++].str = org; |
|---|
| 906 | } |
|---|
| 907 | free(filter); |
|---|
| 908 | return 0; |
|---|
| 909 | } |
|---|
| 910 | |
|---|
| 911 | GB_ERROR gbl_change_gc(GBDATA *gb_species, char *com, |
|---|
| 912 | int argcinput, GBL *argvinput, |
|---|
| 913 | int argcparam,GBL *argvparam, |
|---|
| 914 | int *argcout, GBL **argvout) { |
|---|
| 915 | int i; |
|---|
| 916 | GBDATA *gb_seq = 0; |
|---|
| 917 | char *filter = 0; |
|---|
| 918 | long flen; |
|---|
| 919 | char *use; |
|---|
| 920 | int ctl; |
|---|
| 921 | GBL_BEGIN_PARAMS; |
|---|
| 922 | GBL_PARAM_STRING(sai, "SAI=", 0, "Name of filter SAI"); |
|---|
| 923 | GBL_PARAM_STRING(species, "species=", 0, "Name of filter species"); |
|---|
| 924 | GBL_PARAM_STRING(exclude, "exclude=", 0, "Exclude colums"); |
|---|
| 925 | GBL_PARAM_STRING(include, "include=", 0, "Use only those columns whose filter occur in include"); |
|---|
| 926 | GBL_PARAM_INT(change, "change=", 0, "percent of coloums changed"); |
|---|
| 927 | GBL_PARAM_STRING(change_to, "to=", "GC", "to one of this"); |
|---|
| 928 | GBL_TRACE_PARAMS(argcparam,argvparam); |
|---|
| 929 | GBL_END_PARAMS; |
|---|
| 930 | GBL_CHECK_FREE_PARAM(*argcout,0); |
|---|
| 931 | if (argcinput==0) return "No input stream"; |
|---|
| 932 | if (sai || species){ |
|---|
| 933 | if (!exclude && ! include) return "Missing parameter: 'exclude=' or 'include='"; |
|---|
| 934 | gb_species = gbl_search_sai_species(species,sai); |
|---|
| 935 | if (!gb_species) return GB_export_error("SAI/Species '%s/%s' not found",sai,species); |
|---|
| 936 | use = GBT_get_default_alignment(gb_local->gbl.gb_main); |
|---|
| 937 | gb_seq = GBT_read_sequence(gb_species,use); |
|---|
| 938 | if (!gb_seq) { |
|---|
| 939 | GB_ERROR error = GB_export_error("SAI/Species '%s/%s' has no sequence '%s'",sai,species,use); |
|---|
| 940 | free(use); |
|---|
| 941 | return error; |
|---|
| 942 | } |
|---|
| 943 | free(use); |
|---|
| 944 | } |
|---|
| 945 | if (gb_seq) { |
|---|
| 946 | filter = GB_read_string(gb_seq); |
|---|
| 947 | }else{ |
|---|
| 948 | filter = strdup(""); |
|---|
| 949 | } |
|---|
| 950 | flen = strlen(filter); |
|---|
| 951 | ctl = strlen(change_to); |
|---|
| 952 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 953 | char *dest = GB_STRDUP(argvinput[i].str); |
|---|
| 954 | char *source = dest; |
|---|
| 955 | char *org = source; |
|---|
| 956 | int j; |
|---|
| 957 | int c = 1; |
|---|
| 958 | if (exclude) { |
|---|
| 959 | for (j = 0; j <flen && (c= *(source++));j++){ |
|---|
| 960 | if (!strchr(exclude, filter[j])){ |
|---|
| 961 | if ( isalpha(c) && (rand() * (100.0 / (double)0xffff)) < change){ |
|---|
| 962 | c = change_to[ (rand()>>6)% ctl]; |
|---|
| 963 | } |
|---|
| 964 | } |
|---|
| 965 | *(dest++) = c; |
|---|
| 966 | } |
|---|
| 967 | }else{ |
|---|
| 968 | for (j = 0; j <flen && (c= *(source++));j++){ |
|---|
| 969 | if (strchr(include, filter[j])){ |
|---|
| 970 | if ( isalpha(c) && (rand() * (100.0 / (double)0xffff)) < change){ |
|---|
| 971 | c = change_to[ (rand()>>6)% ctl]; |
|---|
| 972 | } |
|---|
| 973 | } |
|---|
| 974 | *(dest++) = c; |
|---|
| 975 | } |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | if (c) while ( (c= *(source++)) ) { |
|---|
| 979 | if ( isalpha(c) && (rand() * (100.0 / (double)0xffff)) < change){ |
|---|
| 980 | c = change_to[ (rand()>>6)% ctl]; |
|---|
| 981 | } |
|---|
| 982 | *(dest++) = c; |
|---|
| 983 | }; |
|---|
| 984 | *(dest) = 0; |
|---|
| 985 | (*argvout)[(*argcout)++].str = org; |
|---|
| 986 | } |
|---|
| 987 | free(filter); |
|---|
| 988 | return 0; |
|---|
| 989 | } |
|---|
| 990 | /******************************************************************************************** |
|---|
| 991 | Exec Functions |
|---|
| 992 | ********************************************************************************************/ |
|---|
| 993 | |
|---|
| 994 | GB_ERROR gbl_exec(GBDATA *gb_species, char *com, |
|---|
| 995 | int argcinput, GBL *argvinput, |
|---|
| 996 | int argcparam,GBL *argvparam, |
|---|
| 997 | int *argcout, GBL **argvout) { |
|---|
| 998 | int i; |
|---|
| 999 | char fname[1024]; |
|---|
| 1000 | void *str; |
|---|
| 1001 | FILE *out; |
|---|
| 1002 | FILE *in; |
|---|
| 1003 | char *sys; |
|---|
| 1004 | |
|---|
| 1005 | sprintf(fname,"/tmp/arb_tmp_%s_%i",GB_getenv("USER"),getpid()); |
|---|
| 1006 | if (argcparam==0) return "exec needs parameters:\nexec(command,...)"; |
|---|
| 1007 | GBUSE(gb_species);GBUSE(com); |
|---|
| 1008 | out = fopen(fname,"w"); |
|---|
| 1009 | if (!out) return GB_export_error("Cannot open temporary file '%s'",fname); |
|---|
| 1010 | for (i=0;i<argcinput;i++) { /* go through all in streams */ |
|---|
| 1011 | fprintf(out,"%s\n",argvinput[i].str); |
|---|
| 1012 | } |
|---|
| 1013 | fclose(out); |
|---|
| 1014 | str = GBS_stropen(1000); |
|---|
| 1015 | for (i=0;i<argcparam;i++) { /* go through all in params */ |
|---|
| 1016 | if (i) GBS_chrcat(str,'\''); |
|---|
| 1017 | GBS_strcat(str, argvparam[i].str); |
|---|
| 1018 | if (i) GBS_chrcat(str,'\''); |
|---|
| 1019 | GBS_chrcat(str, ' '); |
|---|
| 1020 | } |
|---|
| 1021 | GBS_strcat(str, "<"); |
|---|
| 1022 | GBS_strcat(str, fname); |
|---|
| 1023 | sys = GBS_strclose(str,0); |
|---|
| 1024 | if (! (in = popen(sys,"r"))) { |
|---|
| 1025 | free(com); |
|---|
| 1026 | unlink(fname); |
|---|
| 1027 | return GB_export_error("Cannot execute '%s'",com); |
|---|
| 1028 | } |
|---|
| 1029 | free(sys); |
|---|
| 1030 | str = GBS_stropen(1000); |
|---|
| 1031 | while ( (i=getc(in)) != EOF ) GBS_chrcat(str,i); |
|---|
| 1032 | pclose(in); |
|---|
| 1033 | (*argvout)[(*argcout)++].str = GBS_strclose(str,1); |
|---|
| 1034 | return 0; |
|---|
| 1035 | } |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | struct GBL_command_table gbl_command_table[] = { |
|---|
| 1039 | {"dd", gbl_dd } , |
|---|
| 1040 | {"head", gbl_head } , |
|---|
| 1041 | {"tail", gbl_tail } , |
|---|
| 1042 | {"mid", gbl_mid } , |
|---|
| 1043 | {"mid0", gbl_mid0 } , |
|---|
| 1044 | {"echo", gbl_echo } , |
|---|
| 1045 | {"count", gbl_count } , |
|---|
| 1046 | {"tab", gbl_tab } , |
|---|
| 1047 | {"len", gbl_len } , |
|---|
| 1048 | {"cut", gbl_cut } , |
|---|
| 1049 | {"remove", gbl_remove } , |
|---|
| 1050 | {"keep", gbl_keep } , |
|---|
| 1051 | {"plus", gbl_calculator } , |
|---|
| 1052 | {"minus", gbl_calculator } , |
|---|
| 1053 | {"mult", gbl_calculator } , |
|---|
| 1054 | {"div", gbl_calculator } , |
|---|
| 1055 | {"per_cent", gbl_calculator } , |
|---|
| 1056 | {"readdb", gbl_readdb } , |
|---|
| 1057 | {"sequence", gbl_sequence } , |
|---|
| 1058 | {"format_sequence", gbl_format_sequence } , |
|---|
| 1059 | {"format", gbl_format_sequence } , |
|---|
| 1060 | {"sequence_type", gbl_sequence_type } , |
|---|
| 1061 | {"extract_sequence", gbl_extract_sequence } , |
|---|
| 1062 | {"checksum", gbl_check } , |
|---|
| 1063 | {"gcgchecksum", gbl_gcgcheck } , |
|---|
| 1064 | {"extract_words", gbl_extract_words } , |
|---|
| 1065 | {"exec", gbl_exec } , |
|---|
| 1066 | {"filter", gbl_filter }, |
|---|
| 1067 | {"change", gbl_change_gc }, |
|---|
| 1068 | {"diff", gbl_diff }, |
|---|
| 1069 | {"srt", gbl_srt }, |
|---|
| 1070 | {0,0} |
|---|
| 1071 | |
|---|
| 1072 | }; |
|---|
| 1073 | |
|---|
| 1074 | void gbl_install_standard_commands(GBDATA *gb_main) |
|---|
| 1075 | { |
|---|
| 1076 | GB_install_command_table(gb_main,gbl_command_table); |
|---|
| 1077 | } |
|---|