libisoburn  1.5.4
About: libisoburn is a frontend for the libraries libburn and libisofs which enables creation and expansion of ISO-9660 filesystems on all media and file types supported by libburn. It implements the API and command interpreter of program xorriso, and installs this program as small dynamically linked binary. xorriso is suitable for incremental data backup and for production of bootable ISO 9660 images. A statically linked version is available as GNU xorriso.
  Fossies Dox: libisoburn-1.5.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

match.c File Reference
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <fcntl.h>
#include <errno.h>
#include "xorriso.h"
#include "xorriso_private.h"
#include "xorrisoburn.h"
Include dependency graph for match.c:

Go to the source code of this file.

Functions

int Xorriso_prepare_regex (struct XorrisO *xorriso, char *adr, int flag)
 
int Xorriso_regexec (struct XorrisO *xorriso, char *to_match, int *failed_at, int flag)
 
int Xorriso_is_in_patternlist (struct XorrisO *xorriso, struct Xorriso_lsT *patternlist, char *path, int flag)
 
char * Xorriso_get_pattern (struct XorrisO *xorriso, struct Xorriso_lsT *patternlist, int index, int flag)
 
int Xorriso_prepare_expansion_pattern (struct XorrisO *xorriso, char *pattern, int flag)
 
int Xorriso_check_for_root_pattern (struct XorrisO *xorriso, int *filec, char **filev, int count_limit, off_t *mem, int flag)
 
int Xorriso_register_matched_adr (struct XorrisO *xorriso, char *adr, int count_limit, int *filec, char **filev, off_t *mem, int flag)
 
int Xorriso_obtain_pattern_files_x (struct XorrisO *xorriso, char *wd, char *dir_adr, int *filec, char **filev, int count_limit, off_t *mem, int *dive_count, int flag)
 
int Xorriso_eval_nonmatch (struct XorrisO *xorriso, char *pattern, int *nonconst_mismatches, off_t *mem, int flag)
 
int Xorriso_check_matchcount (struct XorrisO *xorriso, int count, int nonconst_mismatches, int num_patterns, char **patterns, int flag)
 
int Xorriso_no_pattern_memory (struct XorrisO *xorriso, off_t mem, int flag)
 
int Xorriso_alloc_pattern_mem (struct XorrisO *xorriso, off_t mem, int count, char ***filev, int flag)
 
int Xorriso_expand_disk_pattern (struct XorrisO *xorriso, int num_patterns, char **patterns, int extra_filec, int *filec, char ***filev, off_t *mem, int flag)
 
int Xorriso_warn_of_wildcards (struct XorrisO *xorriso, char *path, int flag)
 

Function Documentation

◆ Xorriso_alloc_pattern_mem()

int Xorriso_alloc_pattern_mem ( struct XorrisO xorriso,
off_t  mem,
int  count,
char ***  filev,
int  flag 
)

Definition at line 609 of file match.c.

611 {
612  char mem_text[80], limit_text[80];
613 
614  Sfile_scale((double) mem, mem_text,5,1e4,0);
615  sprintf(xorriso->info_text,
616  "Temporary memory needed for pattern expansion : %s", mem_text);
617  if(!(flag&1))
618  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "DEBUG", 0);
619  if(mem > xorriso->temp_mem_limit) {
620  Sfile_scale((double) xorriso->temp_mem_limit, limit_text,5,1e4,1);
621  sprintf(xorriso->info_text,
622  "List of matching file addresses exceeds -temp_mem_limit (%s > %s)",
623  mem_text, limit_text);
624  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
625  return(0);
626  }
627 
628  (*filev)= (char **) calloc(count, sizeof(char *));
629  if(*filev==NULL) {
630  Xorriso_no_pattern_memory(xorriso, mem, 0);
631  return(-1);
632  }
633  return(1);
634 }
int Xorriso_no_pattern_memory(struct XorrisO *xorriso, off_t mem, int flag)
Definition: match.c:596
int Sfile_scale(double value, char *result, int siz, double thresh, int flag)
Definition: sfile.c:331
char info_text[10 *4096]
int temp_mem_limit
int Xorriso_msgs_submit(struct XorrisO *xorriso, int error_code, char msg_text[], int os_errno, char severity[], int flag)
Submit a problem message to the xorriso problem reporting and handling system.
Definition: text_io.c:2504

References XorrisO::info_text, Sfile_scale(), XorrisO::temp_mem_limit, Xorriso_msgs_submit(), and Xorriso_no_pattern_memory().

Referenced by Xorriso_expand_disk_pattern(), and Xorriso_expand_pattern().

◆ Xorriso_check_for_root_pattern()

int Xorriso_check_for_root_pattern ( struct XorrisO xorriso,
int *  filec,
char **  filev,
int  count_limit,
off_t *  mem,
int  flag 
)

Definition at line 364 of file match.c.

366 {
367  if(xorriso->re_fill!=0)
368  return(2);
369  /* This is the empty pattern representing root */
370  if(flag&1) {
371  (*filec)++;
372  (*mem)+= 8;
373  } else {
374  if(*filec >= count_limit) {
375  sprintf(xorriso->info_text,
376  "Number of matching files changed unexpectedly (> %d)",
377  count_limit);
378  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
379  (flag&2 ? "FATAL" : "WARNING"), 0);
380  return(flag&2 ? -1 : 0);
381  }
382  filev[*filec]= strdup("/");
383  if(filev[*filec]==NULL) {
384  Xorriso_no_pattern_memory(xorriso, (off_t) 2, 0);
385  return(-1);
386  }
387  (*filec)++;
388  }
389  return(1);
390 }

References XorrisO::info_text, XorrisO::re_fill, Xorriso_msgs_submit(), and Xorriso_no_pattern_memory().

Referenced by Xorriso_obtain_pattern_files_i(), and Xorriso_obtain_pattern_files_x().

◆ Xorriso_check_matchcount()

int Xorriso_check_matchcount ( struct XorrisO xorriso,
int  count,
int  nonconst_mismatches,
int  num_patterns,
char **  patterns,
int  flag 
)

Definition at line 574 of file match.c.

577 {
578 
579  if((flag&1) && (count!=1 || nonconst_mismatches)){
580  if(count-nonconst_mismatches>0)
581  sprintf(xorriso->info_text,
582  "Pattern match with more than one file object");
583  else
584  sprintf(xorriso->info_text, "No pattern match with any file object");
585  if(num_patterns==1)
586  sprintf(xorriso->info_text+strlen(xorriso->info_text), ": ");
587  Text_shellsafe(patterns[0], xorriso->info_text, 1);
588  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
589  (flag&2 ? "FAILURE" : "SORRY"), 0);
590  return(0);
591  }
592  return(1);
593 }
char * Text_shellsafe(char *in_text, char *out_text, int flag)
Definition: misc_funct.c:1044

References XorrisO::info_text, Text_shellsafe(), and Xorriso_msgs_submit().

Referenced by Xorriso_expand_disk_pattern(), and Xorriso_expand_pattern().

◆ Xorriso_eval_nonmatch()

int Xorriso_eval_nonmatch ( struct XorrisO xorriso,
char *  pattern,
int *  nonconst_mismatches,
off_t *  mem,
int  flag 
)

Definition at line 548 of file match.c.

550 {
551  int k,l;
552 
553  /* Is this a constant pattern ? */
554  for(k= 0; k<xorriso->re_fill; k++) {
555  if(xorriso->re_constants[k]==NULL)
556  break;
557  if(xorriso->re_constants[k][0]==0)
558  break;
559  }
560  if(k<xorriso->re_fill)
561  (*nonconst_mismatches)++; /* it is not */
562 
563  l= strlen(pattern)+1;
564  (*mem)+= sizeof(char *)+l;
565  if(l % sizeof(char *))
566  (*mem)+= sizeof(char *)-(l % sizeof(char *));
567  return(1);
568 }
char ** re_constants

References XorrisO::re_constants, and XorrisO::re_fill.

Referenced by Xorriso_expand_disk_pattern(), and Xorriso_expand_pattern().

◆ Xorriso_expand_disk_pattern()

int Xorriso_expand_disk_pattern ( struct XorrisO xorriso,
int  num_patterns,
char **  patterns,
int  extra_filec,
int *  filec,
char ***  filev,
off_t *  mem,
int  flag 
)

Definition at line 641 of file match.c.

644 {
645  int ret, count= 0, abs_adr= 0, i, was_count, was_filec;
646  int nonconst_mismatches= 0, dive_count= 0;
647  char *dir_adr= NULL;
648 
649  Xorriso_alloc_meM(dir_adr, char, SfileadrL);
650 
651  *filec= 0;
652  *filev= NULL;
653 
654  xorriso->search_mode= 3;
655  xorriso->structured_search= 1;
656 
657  for(i= 0; i<num_patterns; i++) {
658  abs_adr= 0;
659  ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4);
660  if(ret<=0)
661  goto ex;
662  if(ret==2)
663  abs_adr= 4;
664 
665  if(patterns[i][0]=='/' || abs_adr) {
666  strcpy(dir_adr, "/");
667  abs_adr= 4;
668  } else {
669  strcpy(dir_adr, xorriso->wdx);
670  if(dir_adr[0]==0)
671  strcpy(dir_adr, "/");
672  ret= Sfile_type(dir_adr, 1|4);
673  if(ret!=2) {
674  Xorriso_msgs_submit(xorriso, 0, dir_adr, 0, "ERRFILE", 0);
675  sprintf(xorriso->info_text, "Address set by -cdx is not a directory: ");
676  Text_shellsafe(dir_adr, xorriso->info_text, 1);
677  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
678  ret= 0; goto ex;
679  }
680  }
681 
682  /* count the matches */
683  was_count= count;
684  ret= Xorriso_obtain_pattern_files_x(xorriso, "", dir_adr, &count, NULL, 0,
685  mem, &dive_count, 1 | abs_adr);
686  if(ret<=0)
687  goto ex;
688  if(was_count==count && strcmp(patterns[i],"*")!=0 && (flag&3)!=1 &&
689  !(flag & 8)) {
690  count++;
691  ret= Xorriso_eval_nonmatch(xorriso, patterns[i],
692  &nonconst_mismatches, mem, 0);
693  if(ret<=0)
694  goto ex;
695  }
696  }
697 
698  ret= Xorriso_check_matchcount(xorriso, count, nonconst_mismatches,
699  num_patterns, patterns, (flag&1)|2);
700  if(ret<=0)
701  goto ex;
702 
703  count+= extra_filec;
704  (*mem)+= extra_filec * sizeof(char *);
705 
706  if(count<=0)
707  {ret= !(flag & 8); goto ex;}
708 
709  ret= Xorriso_alloc_pattern_mem(xorriso, *mem, count, filev, 0);
710  if(ret<=0)
711  goto ex;
712 
713  /* now store addresses */
714  for(i= 0; i<num_patterns; i++) {
715  abs_adr= 0;
716  ret= Xorriso_prepare_expansion_pattern(xorriso, patterns[i], 4);
717  if(ret<=0)
718  goto ex;
719  if(ret==2)
720  abs_adr= 4;
721 
722  if(patterns[i][0]=='/' || abs_adr) {
723  strcpy(dir_adr, "/");
724  abs_adr= 4;
725  } else {
726  strcpy(dir_adr, xorriso->wdx);
727  if(dir_adr[0]==0)
728  strcpy(dir_adr, "/");
729  }
730 
731  was_filec= *filec;
732  ret= Xorriso_obtain_pattern_files_x(xorriso, "", dir_adr, filec, *filev,
733  count, mem, &dive_count, abs_adr);
734  if(ret<=0)
735  goto ex;
736 
737  if(was_filec == *filec && strcmp(patterns[i],"*")!=0 && (flag & 3) != 1 &&
738  !(flag & 8)) {
739  (*filev)[*filec]= strdup(patterns[i]);
740  if((*filev)[*filec]==NULL) {
741  (*mem)= strlen(patterns[i])+1;
742  Xorriso_no_pattern_memory(xorriso, *mem, 0);
743  ret= -1; goto ex;
744  }
745  (*filec)++;
746  }
747  }
748 
749  ret= 1;
750 ex:;
751  if(ret<=0) {
752  if(filev!=NULL)
753  Sfile_destroy_argv(&count, filev, 0);
754  *filec= 0;
755  }
756  Xorriso_free_meM(dir_adr);
757  return(ret);
758 }
static int Sfile_destroy_argv(int *argc, char ***argv, int flag)
#define SfileadrL
int Xorriso_check_matchcount(struct XorrisO *xorriso, int count, int nonconst_mismatches, int num_patterns, char **patterns, int flag)
Definition: match.c:574
int Xorriso_obtain_pattern_files_x(struct XorrisO *xorriso, char *wd, char *dir_adr, int *filec, char **filev, int count_limit, off_t *mem, int *dive_count, int flag)
Definition: match.c:433
int Xorriso_alloc_pattern_mem(struct XorrisO *xorriso, off_t mem, int count, char ***filev, int flag)
Definition: match.c:609
int Xorriso_prepare_expansion_pattern(struct XorrisO *xorriso, char *pattern, int flag)
Definition: match.c:339
int Xorriso_eval_nonmatch(struct XorrisO *xorriso, char *pattern, int *nonconst_mismatches, off_t *mem, int flag)
Definition: match.c:548
int Sfile_type(char *filename, int flag)
Definition: sfile.c:225
#define Xorriso_free_meM(pt)
Definition: sfile.h:27
#define Xorriso_alloc_meM(pt, typ, count)
Definition: sfile.h:19
int structured_search
char wdx[4096]

References XorrisO::info_text, XorrisO::search_mode, Sfile_destroy_argv(), Sfile_type(), SfileadrL, XorrisO::structured_search, Text_shellsafe(), XorrisO::wdx, Xorriso_alloc_meM, Xorriso_alloc_pattern_mem(), Xorriso_check_matchcount(), Xorriso_eval_nonmatch(), Xorriso_free_meM, Xorriso_msgs_submit(), Xorriso_no_pattern_memory(), Xorriso_obtain_pattern_files_x(), and Xorriso_prepare_expansion_pattern().

Referenced by Xorriso_opt_args(), and Xorriso_option_lsx().

◆ Xorriso_get_pattern()

char* Xorriso_get_pattern ( struct XorrisO xorriso,
struct Xorriso_lsT patternlist,
int  index,
int  flag 
)

Definition at line 321 of file match.c.

323 {
324  int i= 0;
325  struct Xorriso_lsT *s;
326 
327  for(s= patternlist; s != NULL; s= Xorriso_lst_get_next(s, 0)) {
328  if(i == index)
329  return(Xorriso_lst_get_text(s, 0));
330  i++;
331  }
332  return(NULL);
333 }
struct Xorriso_lsT * Xorriso_lst_get_next(struct Xorriso_lsT *entry, int flag)
Obtain the address of the next item in the chain of messages.
Definition: aux_objects.c:624
char * Xorriso_lst_get_text(struct Xorriso_lsT *entry, int flag)
Obtain the text message from the current list item.
Definition: aux_objects.c:636

References Xorriso_lst_get_next(), and Xorriso_lst_get_text().

Referenced by Xorriso_auto_driveadr(), and Xorriso_status().

◆ Xorriso_is_in_patternlist()

int Xorriso_is_in_patternlist ( struct XorrisO xorriso,
struct Xorriso_lsT patternlist,
char *  path,
int  flag 
)

Definition at line 297 of file match.c.

300 {
301  int ret, failed_at, i= 0;
302  struct Xorriso_lsT *s;
303 
304  xorriso->search_mode= 3;
305  xorriso->structured_search= 1;
306 
307  for(s= patternlist; s != NULL; s= Xorriso_lst_get_next(s, 0)) {
308  ret= Xorriso_prepare_regex(xorriso, Xorriso_lst_get_text(s, 0), 0);
309  if(ret <= 0)
310  return(-1);
311  /* Match path or parent of path */
312  ret= Xorriso_regexec(xorriso, path, &failed_at, 2);
313  if(ret == 0)
314  return(i + 1);
315  i++;
316  }
317  return(0);
318 }
int Xorriso_prepare_regex(struct XorrisO *xorriso, char *adr, int flag)
Definition: match.c:38
int Xorriso_regexec(struct XorrisO *xorriso, char *to_match, int *failed_at, int flag)
Definition: match.c:225

References XorrisO::search_mode, XorrisO::structured_search, Xorriso_lst_get_next(), Xorriso_lst_get_text(), Xorriso_prepare_regex(), and Xorriso_regexec().

Referenced by Xorriso_auto_driveadr().

◆ Xorriso_no_pattern_memory()

int Xorriso_no_pattern_memory ( struct XorrisO xorriso,
off_t  mem,
int  flag 
)

Definition at line 596 of file match.c.

597 {
598  char mem_text[80];
599 
600  Sfile_scale((double) mem, mem_text,5,1e4,1);
601  sprintf(xorriso->info_text,
602  "Cannot allocate enough memory (%s) for pattern expansion",
603  mem_text);
604  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
605  return(1);
606 }

References XorrisO::info_text, Sfile_scale(), and Xorriso_msgs_submit().

Referenced by Xorriso_alloc_pattern_mem(), Xorriso_check_for_root_pattern(), Xorriso_expand_disk_pattern(), Xorriso_expand_pattern(), Xorriso_option_not_paths(), and Xorriso_register_matched_adr().

◆ Xorriso_obtain_pattern_files_x()

int Xorriso_obtain_pattern_files_x ( struct XorrisO xorriso,
char *  wd,
char *  dir_adr,
int *  filec,
char **  filev,
int  count_limit,
off_t *  mem,
int *  dive_count,
int  flag 
)

Definition at line 433 of file match.c.

437 {
438  int ret, failed_at, follow_mount, follow_links;
439  struct DirseQ *dirseq= NULL;
440  struct stat stbuf;
441  dev_t dir_dev;
442  char *path;
443  char *adr= NULL, *name= NULL, *path_data= NULL;
444 
445  adr= malloc(SfileadrL);
446  name= malloc(SfileadrL);
447  path_data= malloc(SfileadrL);
448  if(adr==NULL || name==NULL || path_data==NULL) {
449  Xorriso_no_malloc_memory(xorriso, &adr, 0);
450  {ret= -1; goto ex;}
451  }
452  follow_mount= (xorriso->do_follow_mount || xorriso->do_follow_pattern);
453  follow_links= (xorriso->do_follow_links || xorriso->do_follow_pattern);
454  if(!(flag&2))
455  *dive_count= 0;
456  else
457  (*dive_count)++;
458 
459  ret= Xorriso_check_for_root_pattern(xorriso, filec, filev, count_limit,
460  mem, flag&1);
461  if(ret!=2)
462  goto ex;
463 
464  if(lstat(dir_adr, &stbuf)==-1)
465  {ret= 2; goto ex;}
466  dir_dev= stbuf.st_dev;
467  if(S_ISLNK(stbuf.st_mode)) {
468  if(stat(dir_adr, &stbuf)==-1)
469  {ret= 2; goto ex;}
470  if(dir_dev != stbuf.st_dev && !follow_mount)
471  {ret= 2; goto ex;}
472  }
473  ret= Dirseq_new(&dirseq, dir_adr, 1);
474  if(ret<0) {
475  sprintf(xorriso->info_text, "Cannot obtain disk directory iterator");
476  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
477  {ret= -1; goto ex;}
478  }
479  if(ret==0)
480  {ret= 2; goto ex;}
481 
482  while(1) {
483  ret= Dirseq_next_adr(dirseq,name,0);
484  if(ret==0)
485  break;
486  if(ret<0) {
487  sprintf(xorriso->info_text,"Failed to obtain next directory entry");
488  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FATAL", 0);
489  {ret= -1; goto ex;}
490  }
491 
492  ret= Xorriso_make_abs_adr(xorriso, wd, name, adr, flag&4);
493  if(ret<=0)
494  goto ex;
495 
496  ret= Xorriso_regexec(xorriso, adr, &failed_at, 1);
497  if(ret>0) { /* no match */
498  if(failed_at <= *dive_count) /* no hope for a match */
499  continue;
500  path= adr;
501  if(adr[0]!='/') {
502  path= path_data;
503  ret= Xorriso_make_abs_adr(xorriso, xorriso->wdx, adr, path, 1|4);
504  if(ret<=0)
505  goto ex;
506  }
507 
508  if(follow_links)
509  ret= stat(path,&stbuf);
510  else
511  ret= lstat(path,&stbuf);
512  if(ret==-1)
513  continue;
514  if(!S_ISDIR(stbuf.st_mode))
515  continue;
516  if(dir_dev != stbuf.st_dev && !follow_mount)
517  continue;
518 
519  /* dive deeper */
520  ret= Xorriso_obtain_pattern_files_x(xorriso, adr, path,
521  filec, filev, count_limit, mem, dive_count, flag|2);
522  if(ret<=0)
523  goto ex;
524  } else {
525  ret= Xorriso_register_matched_adr(xorriso, adr, count_limit,
526  filec, filev, mem, flag&1);
527  if(ret<0)
528  goto ex;
529  if(ret==0)
530  break;
531  }
532  }
533  ret= 1;
534 ex:;
535  if(adr!=NULL)
536  free(adr);
537  if(name!=NULL)
538  free(name);
539  if(path_data!=NULL)
540  free(path_data);
541  Dirseq_destroy(&dirseq,0);
542  if(flag&2)
543  (*dive_count)--;
544  return(ret);
545 }
int Dirseq_destroy(struct DirseQ **o, int flag)
Definition: aux_objects.c:336
int Dirseq_next_adr(struct DirseQ *o, char reply[4096], int flag)
Definition: aux_objects.c:384
int Dirseq_new(struct DirseQ **o, char *adr, int flag)
Definition: aux_objects.c:291
int Xorriso_register_matched_adr(struct XorrisO *xorriso, char *adr, int count_limit, int *filec, char **filev, off_t *mem, int flag)
Definition: match.c:396
int Xorriso_check_for_root_pattern(struct XorrisO *xorriso, int *filec, char **filev, int count_limit, off_t *mem, int flag)
Definition: match.c:364
int Xorriso_make_abs_adr(struct XorrisO *xorriso, char *wd, char *name, char adr[], int flag)
Definition: parse_exec.c:2812
int do_follow_mount
int do_follow_pattern
int do_follow_links
int Xorriso_no_malloc_memory(struct XorrisO *xorriso, char **to_free, int flag)
Definition: text_io.c:4077

References Dirseq_destroy(), Dirseq_new(), Dirseq_next_adr(), XorrisO::do_follow_links, XorrisO::do_follow_mount, XorrisO::do_follow_pattern, XorrisO::info_text, SfileadrL, XorrisO::wdx, Xorriso_check_for_root_pattern(), Xorriso_make_abs_adr(), Xorriso_msgs_submit(), Xorriso_no_malloc_memory(), Xorriso_regexec(), and Xorriso_register_matched_adr().

Referenced by Xorriso_expand_disk_pattern().

◆ Xorriso_prepare_expansion_pattern()

int Xorriso_prepare_expansion_pattern ( struct XorrisO xorriso,
char *  pattern,
int  flag 
)

Definition at line 339 of file match.c.

341 {
342  int ret, prepwd= 0;
343 
344  ret= Xorriso_prepare_regex(xorriso, pattern, 1|2|(flag&4));
345  if(ret==2) {
346  ret= Xorriso_prepare_regex(xorriso, pattern, flag&4);
347  prepwd= 1;
348  }
349  if(ret<=0) {
350  sprintf(xorriso->info_text,
351  "Cannot compile pattern to regular expression: %s", pattern);
352  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
353  return(0);
354  }
355  return(1+prepwd);
356 }

References XorrisO::info_text, Xorriso_msgs_submit(), and Xorriso_prepare_regex().

Referenced by Xorriso_expand_disk_pattern(), and Xorriso_expand_pattern().

◆ Xorriso_prepare_regex()

int Xorriso_prepare_regex ( struct XorrisO xorriso,
char *  adr,
int  flag 
)

Definition at line 38 of file match.c.

39 {
40  int l,ret,i,count,bonked= 0,is_constant,is_still_relative= 0, adr_size;
41  char *cpt,*npt,*adr_part= NULL, *absolute_adr= NULL, *adr_start,*wd;
42 
43  adr_size= 2 * SfileadrL;
44  Xorriso_alloc_meM(adr_part, char, adr_size);
45  Xorriso_alloc_meM(absolute_adr, char, adr_size);
46 
47  if(flag&4)
48  wd= xorriso->wdx;
49  else
50  wd= xorriso->wdi;
51 
52  if(xorriso->search_mode>=2 && xorriso->search_mode<=4) {
53  if(xorriso->search_mode==3 || xorriso->search_mode==4) {
54  l= strlen(adr)+strlen(wd)+1;
55  if(l * 2 + 2 > ((int) sizeof(xorriso->reg_expr)) || l * 2 + 2 > adr_size){
56  sprintf(xorriso->info_text,"Search pattern too long");
57  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE", 0);
58  {ret= 0; goto ex;}
59  }
60  }
61  Xorriso_destroy_re(xorriso,0);
62  if(xorriso->structured_search && xorriso->search_mode==3) {
63  if(adr[0]!='/')
64  is_still_relative= 1;
65  if(is_still_relative && !(flag&1)) {
66  /* relative expression : prepend working directory */
67  sprintf(absolute_adr,"%s/%s",wd,adr);
68  adr_start= absolute_adr;
69  xorriso->prepended_wd= 1;
70  is_still_relative= 0;
71  } else
72  adr_start= adr;
73  /* count slashes */;
74  cpt= adr_start;
75  while(*cpt=='/')
76  cpt++;
77  for(i= 0;1;i++) {
78  cpt= strchr(cpt,'/');
79  if(cpt==NULL)
80  break;
81  while(*cpt=='/')
82  cpt++;
83  }
84  count= i+1;
85  xorriso->re= TSOB_FELD(regex_t,count);
86  if(xorriso->re==NULL)
87  {ret= -1; goto ex;}
88  xorriso->re_constants= TSOB_FELD(char *,count);
89  if(xorriso->re_constants==NULL)
90  {ret= -1; goto ex;}
91  for(i= 0;i<count;i++)
92  xorriso->re_constants[i]= NULL;
93  xorriso->re_count= count;
94  xorriso->re_fill= 0;
95 
96  /* loop over slash chunks*/;
97  cpt= adr_start;
98  xorriso->re_fill= 0;
99  while(*cpt=='/')
100  cpt++;
101  for(i= 0;i<count;i++) {
102  npt= strchr(cpt,'/');
103  if(npt==NULL) {
104  if((int) strlen(cpt) >= adr_size)
105  {ret= -1; goto ex;}
106  strcpy(adr_part,cpt);
107  } else {
108  if(npt-cpt >= adr_size)
109  {ret= -1; goto ex;}
110  strncpy(adr_part,cpt,npt-cpt);
111  adr_part[npt-cpt]= 0;
112  }
113 
114  if(adr_part[0]==0)
115  goto next_adr_part;
116  if(adr_part[0] == '.' && adr_part[1] == 0)
117  goto next_adr_part;
118  if(adr_part[0]=='.' && adr_part[1]=='.' && adr_part[2]==0) {
119  /* delete previous part */
120  if(xorriso->re_fill <= 0) {
121  bonked= 1;
122  goto next_adr_part;
123  }
124  if(xorriso->re_constants[xorriso->re_fill-1]!=NULL) {
125  free(xorriso->re_constants[xorriso->re_fill-1]);
126  xorriso->re_constants[xorriso->re_fill-1]= NULL;
127  } else
128  regfree(&(xorriso->re[xorriso->re_fill-1]));
129  (xorriso->re_fill)--;
130  goto next_adr_part;
131  }
132  if(strcmp(adr_part,"*")==0) {
133  adr_part[0]= 0;
134  ret= 2;
135  } else
136  ret= Xorriso__bourne_to_reg(adr_part,xorriso->reg_expr,0);
137  if(ret==2) {
138  if(Sregex_string(&(xorriso->re_constants[xorriso->re_fill]),adr_part,0)
139  <=0)
140  {ret= -1; goto ex;}
141  } else {
142  if(regcomp(&(xorriso->re[xorriso->re_fill]),xorriso->reg_expr,0)!=0)
143  goto cannot_compile;
144  }
145  xorriso->re_fill++;
146 next_adr_part:;
147  if(i==count-1)
148  break;
149  cpt= npt+1;
150  while(*cpt=='/')
151  cpt++;
152  }
153  if(bonked) {
154  if(flag&2)
155  {ret= 2; goto ex;}
156  sprintf(xorriso->info_text, "Your '..' bonked at the %s directory.",
157  is_still_relative ? "working" : "root");
158  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0);
159  {ret= 0; goto ex;}
160  }
161  if(xorriso->re_fill == 0 && is_still_relative) {
162  /* "." and its equivalents end up here */
163  if(Sregex_string(&(xorriso->re_constants[0]), ".", 0) <=0)
164  {ret= -1; goto ex;}
165  xorriso->re_fill= 1;
166  }
167 
168  Xorriso__bourne_to_reg(adr_start,xorriso->reg_expr,0); /* just for show */
169 
170  } else {
171  is_constant= 0;
172  if(strcmp(adr,"*")==0 || adr[0]==0) {
173  is_constant= 1;
174  } else if(xorriso->search_mode==3 || xorriso->search_mode==4) {
175  ret= Xorriso__bourne_to_reg(adr,xorriso->reg_expr,0);
176  is_constant= (ret==2);
177  } else {
178  if(strlen(adr)>=sizeof(xorriso->reg_expr))
179  {ret= -1; goto ex;}
180  strcpy(xorriso->reg_expr,adr);
181  }
182  xorriso->re_count= 0; /* tells matcher that this is not structured */
183  xorriso->re_constants= TSOB_FELD(char *,1);
184  if(xorriso->re_constants==NULL)
185  {ret= -1; goto ex;}
186  xorriso->re_constants[0]= NULL;
187  if(is_constant) {
188  if(strcmp(adr,"*")==0) {
189  if(Sregex_string(&(xorriso->re_constants[0]),"",0)<=0)
190  {ret= -1; goto ex;}
191  } else {
192  if(Sregex_string(&(xorriso->re_constants[0]),adr,0)<=0)
193  {ret= -1; goto ex;}
194  }
195  xorriso->re_fill= 1;
196  } else {
197  xorriso->re= TSOB_FELD(regex_t,1);
198  if(xorriso->re==NULL)
199  {ret= -1; goto ex;}
200  if(regcomp(&(xorriso->re[0]),xorriso->reg_expr,0)!=0) {
201 cannot_compile:;
202  sprintf(xorriso->info_text, "Cannot compile regular expression : %s",
203  xorriso->reg_expr);
204  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "FAILURE",0);
205  {ret= 0; goto ex;}
206  }
207  }
208 
209  }
210  }
211  ret= 1;
212 ex:;
213  Xorriso_free_meM(adr_part);
214  Xorriso_free_meM(absolute_adr);
215  return(ret);
216 }
int Xorriso_destroy_re(struct XorrisO *m, int flag)
Definition: base_obj.c:563
int Xorriso__bourne_to_reg(char bourne_expr[], char reg_expr[], int flag)
Definition: misc_funct.c:1162
int Sregex_string(char **handle, char *text, int flag)
Definition: misc_funct.c:865
#define TSOB_FELD(typ, count)
Definition: sfile.h:15
regex_t * re
char reg_expr[2 *4096]
int prepended_wd
char wdi[4096]

References XorrisO::info_text, XorrisO::prepended_wd, XorrisO::re, XorrisO::re_constants, XorrisO::re_count, XorrisO::re_fill, XorrisO::reg_expr, XorrisO::search_mode, SfileadrL, Sregex_string(), XorrisO::structured_search, TSOB_FELD, XorrisO::wdi, XorrisO::wdx, Xorriso__bourne_to_reg(), Xorriso_alloc_meM, Xorriso_destroy_re(), Xorriso_free_meM, and Xorriso_msgs_submit().

Referenced by Xorriso_is_in_patternlist(), and Xorriso_prepare_expansion_pattern().

◆ Xorriso_regexec()

int Xorriso_regexec ( struct XorrisO xorriso,
char *  to_match,
int *  failed_at,
int  flag 
)

Definition at line 225 of file match.c.

227 {
228  int ret,i,re_start= 0,reg_nomatch= -1;
229  char *cpt,*npt, *adr_part= NULL, *mpt;
230 
231  Xorriso_alloc_meM(adr_part, char, SfileadrL);
232 
233  reg_nomatch= REG_NOMATCH;
234 
235  *failed_at= 0;
236  if(!(xorriso->structured_search && xorriso->re_count>0)) {
237  if(xorriso->re_constants!=NULL)
238  if(xorriso->re_constants[0]!=NULL) {
239  if(xorriso->re_constants[0][0]==0)
240  {ret= 0; goto ex;}
241  if(strcmp(xorriso->re_constants[0],to_match)!=0)
242  {ret= reg_nomatch; goto ex;}
243  {ret= 0; goto ex;}
244  }
245  ret= regexec(&(xorriso->re[0]),to_match,1,xorriso->match,0);
246  goto ex;
247  }
248 
249  cpt= to_match;
250  while(*cpt=='/')
251  cpt++;
252  if(flag&4)
253  re_start= xorriso->re_failed_at;
254  if(re_start<0)
255  re_start= 0;
256  for(i= re_start;i<xorriso->re_fill;i++) {
257  *failed_at= i;
258  npt= strchr(cpt,'/');
259  if(npt==NULL) {
260  if(i<xorriso->re_fill-1 && !(flag&1))
261  {ret= reg_nomatch; goto ex;} /* this must be the last expression part */
262  mpt= cpt;
263  } else {
264  strncpy(adr_part,cpt,npt-cpt);
265  adr_part[npt-cpt]= 0;
266  mpt= adr_part;
267  }
268  if(xorriso->re_constants[i]!=NULL) {
269  if(xorriso->re_constants[i][0]!=0) /* empty constant matches anything */
270  if(strcmp(xorriso->re_constants[i],mpt)!=0)
271  {ret= reg_nomatch; goto ex;}
272  } else {
273  ret= regexec(&(xorriso->re[i]),mpt,1,xorriso->match,0);
274  if(ret!=0)
275  goto ex;
276  }
277  if(npt==NULL) {
278  if(i>=xorriso->re_fill-1)
279  {ret= 0; goto ex;} /* MATCH */
280  *failed_at= i+1;
281  {ret= reg_nomatch; goto ex;}
282  }
283  cpt= npt+1;
284  while(*cpt=='/')
285  cpt++;
286  }
287  *failed_at= xorriso->re_fill;
288  if(flag & 2)
289  {ret= 0; goto ex;} /* MATCH */
290  ret= reg_nomatch;
291 ex:;
292  Xorriso_free_meM(adr_part);
293  return(ret);
294 }
int re_failed_at
regmatch_t match[1]

References XorrisO::match, XorrisO::re, XorrisO::re_constants, XorrisO::re_count, XorrisO::re_failed_at, XorrisO::re_fill, SfileadrL, XorrisO::structured_search, Xorriso_alloc_meM, and Xorriso_free_meM.

Referenced by Xorriso_is_in_patternlist(), Xorriso_ls(), Xorriso_obtain_pattern_files_i(), Xorriso_obtain_pattern_files_x(), and Xorriso_sorted_node_array().

◆ Xorriso_register_matched_adr()

int Xorriso_register_matched_adr ( struct XorrisO xorriso,
char *  adr,
int  count_limit,
int *  filec,
char **  filev,
off_t *  mem,
int  flag 
)

Definition at line 396 of file match.c.

399 {
400  int l;
401 
402  if(flag&1) {
403  (*filec)++;
404  l= strlen(adr)+1;
405  (*mem)+= sizeof(char *)+l;
406  if(l % sizeof(char *))
407  (*mem)+= sizeof(char *)-(l % sizeof(char *));
408  } else {
409  if(*filec >= count_limit) {
410  sprintf(xorriso->info_text,
411  "Number of matching files changed unexpectedly (> %d)",
412  count_limit);
413  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0,
414  (flag&2 ? "FATAL" : "WARNING"), 0);
415  return(flag&2 ? -1 : 0);
416  }
417  filev[*filec]= strdup(adr);
418  if(filev[*filec]==NULL) {
419  Xorriso_no_pattern_memory(xorriso, (off_t) (strlen(adr)+1), 0);
420  return(-1);
421  }
422  (*filec)++;
423  }
424  return(1);
425 }

References XorrisO::info_text, Xorriso_msgs_submit(), and Xorriso_no_pattern_memory().

Referenced by Xorriso_obtain_pattern_files_i(), and Xorriso_obtain_pattern_files_x().

◆ Xorriso_warn_of_wildcards()

int Xorriso_warn_of_wildcards ( struct XorrisO xorriso,
char *  path,
int  flag 
)

Definition at line 764 of file match.c.

765 {
766  static int count_iso= 0, count_disk= 0, max_iso= 3, max_disk= 3;
767 
768  if(strchr(path,'*')!=NULL || strchr(path,'?')!=NULL ||
769  strchr(path,'[')!=NULL) {
770  if(flag & 2) {
771  count_disk++;
772  if(count_disk > max_disk)
773  return(1);
774  } else {
775  count_iso++;
776  if(count_iso > max_iso)
777  return(1);
778  }
779  if(flag&1) {
780  sprintf(xorriso->info_text,
781  "Pattern expansion of wildcards \"*?[\" does not apply to this command");
782  } else {
783  sprintf(xorriso->info_text,
784  "Pattern expansion of wildcards \"*?[\" is disabled by command %s",
785  (flag&2) ? "-disk_pattern or -pathspecs" : "-iso_rr_pattern");
786  }
787  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
788  sprintf(xorriso->info_text,"Pattern seen: ");
789  Text_shellsafe(path, xorriso->info_text, 1);
790  strcat(xorriso->info_text, "\n");
791  Xorriso_msgs_submit(xorriso, 0, xorriso->info_text, 0, "WARNING", 0);
792  return(1);
793  }
794  return(0);
795 }

References XorrisO::info_text, Text_shellsafe(), and Xorriso_msgs_submit().

Referenced by Xorriso_end_idx(), Xorriso_option_cdi(), and Xorriso_option_cdx().