BRL-CAD
expand.c
Go to the documentation of this file.
1 /* E X P A N D . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2008-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file libged/expand.c
21  *
22  * The expand command.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <string.h>
29 
30 #include "bu/cmd.h"
31 
32 #include "./ged_private.h"
33 
34 
35 static void
36 expand_scrape_escapes(struct bu_vls *result, const char *str)
37 {
38  char buf[2];
39  buf[1] = '\0';
40 
41  while (*str) {
42  buf[0] = *str;
43  if (*str != '\\') {
44  bu_vls_printf(result, "%s", buf);
45  } else if (*(str+1) == '\\') {
46  bu_vls_printf(result, "%s", buf);
47  ++str;
48  }
49  if (*str == '\0')
50  break;
51  ++str;
52  }
53 }
54 
55 
56 int
57 ged_expand(struct ged *gedp, int argc, const char *argv[])
58 {
59  char *pattern;
60  struct directory *dp;
61  int i, whicharg;
62  int regexp, nummatch, thismatch, backslashed;
63  static const char *usage = "expression";
64 
66  GED_CHECK_ARGC_GT_0(gedp, argc, GED_ERROR);
67 
68  /* initialize result */
69  bu_vls_trunc(gedp->ged_result_str, 0);
70 
71  /* must be wanting help */
72  if (argc == 1) {
73  bu_vls_printf(gedp->ged_result_str, "Usage: %s %s", argv[0], usage);
74  return GED_HELP;
75  }
76 
77  nummatch = 0;
78  backslashed = 0;
79  for (whicharg = 1; whicharg < argc; whicharg++) {
80  /* If * ? or [ are present, this is a regular expression */
81  pattern = (char *)argv[whicharg];
82  regexp = 0;
83  do {
84  if ((*pattern == '*' || *pattern == '?' || *pattern == '[') &&
85  !backslashed) {
86  regexp = 1;
87  break;
88  }
89  if (*pattern == '\\' && !backslashed)
90  backslashed = 1;
91  else
92  backslashed = 0;
93  } while (*pattern++);
94 
95  /* If it isn't a regexp, copy directly and continue */
96  if (regexp == 0) {
97  if (db_lookup(gedp->ged_wdbp->dbip, argv[whicharg], LOOKUP_QUIET) != RT_DIR_NULL) {
98  if (nummatch > 0)
99  bu_vls_printf(gedp->ged_result_str, " ");
100  expand_scrape_escapes(gedp->ged_result_str, argv[whicharg]);
101  ++nummatch;
102  }
103  continue;
104  }
105 
106  /* Search for pattern matches.
107  * If any matches are found, we do not have to worry about
108  * '\' escapes since the match coming from dp->d_namep will be
109  * clean. In the case of no matches, just copy the argument
110  * directly.
111  */
112 
113  pattern = (char *)argv[whicharg];
114  thismatch = 0;
115  for (i = 0; i < RT_DBNHASH; i++) {
116  for (dp = gedp->ged_wdbp->dbip->dbi_Head[i]; dp != RT_DIR_NULL; dp = dp->d_forw) {
117  if (bu_fnmatch(pattern, dp->d_namep, 0) != 0)
118  continue;
119  /* Successful match */
120  if (nummatch == 0)
121  bu_vls_printf(gedp->ged_result_str, "%s", dp->d_namep);
122  else
123  bu_vls_printf(gedp->ged_result_str, " %s", dp->d_namep);
124  ++nummatch;
125  ++thismatch;
126  }
127  }
128  }
129 
130  return GED_OK;
131 }
132 
133 
134 /*
135  * Local Variables:
136  * tab-width: 8
137  * mode: C
138  * indent-tabs-mode: t
139  * c-file-style: "stroustrup"
140  * End:
141  * ex: shiftwidth=4 tabstop=8
142  */
void usage(struct ged *gedp)
Definition: coil.c:315
#define GED_OK
Definition: ged.h:55
char * d_namep
pointer to name string
Definition: raytrace.h:859
#define RT_DBNHASH
hash table is an array of linked lists with this many array pointer elements (Memory use for 32-bit: ...
Definition: raytrace.h:755
Definition: ged.h:338
struct db_i * dbip
Definition: raytrace.h:1266
void bu_vls_trunc(struct bu_vls *vp, int len)
Definition: vls.c:198
#define GED_CHECK_ARGC_GT_0(_gedp, _argc, _flags)
Definition: ged.h:202
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
struct rt_wdb * ged_wdbp
Definition: ged.h:340
Header file for the BRL-CAD common definitions.
struct directory * d_forw
link to next dir entry
Definition: raytrace.h:864
#define GED_ERROR
Definition: ged.h:61
#define GED_CHECK_DATABASE_OPEN(_gedp, _flags)
Definition: ged.h:114
#define LOOKUP_QUIET
Definition: raytrace.h:893
int bu_fnmatch(const char *pattern, const char *pathname, int flags)
Definition: fnmatch.c:311
struct bu_vls * ged_result_str
Definition: ged.h:357
int ged_expand(struct ged *gedp, int argc, const char *argv[])
Definition: expand.c:57
struct directory * dbi_Head[RT_DBNHASH]
Definition: raytrace.h:814
void bu_vls_printf(struct bu_vls *vls, const char *fmt,...) _BU_ATTR_PRINTF23
Definition: vls.c:694
#define RT_DIR_NULL
Definition: raytrace.h:875
#define GED_HELP
Definition: ged.h:62
Definition: vls.h:56