Globbing with MinGW on Windows - c++

I have an application built with the MinGW C++ compiler that works something like grep - acommand looks something like this:
myapp -e '.*' *.txt
where the thing that comes after the -e switch is a regex, and the thing after that is file name pattern. It seems that MinGW automatically expands (globs in UNIX terms) the command line so my regex gets mangled. I can turn this behaviour off, I discovered, by setting the global variable _CRT_glob to zero. This will be fine for bash and other sensible shell users, as the shell will expand the file pattern. For MS cmd.exe users however, it looks like I will have to expand the file pattern myself.
So my question - does anyone know of a globbing library (or facility in MinGW) to do partial command line expansion? I'm aware of the _setargv feature of the Windows CRT, but that expands the full command line. Please note I've seen this question, but it really does not address partial expansion.
I've ended up using conditional compilation to write my own globbing code for the Windows version of my app. This was pretty easy as I have my own CommandLine class which encapsulates argc and argv from main(). Still, I'd be interested to hear of other solutions.

<glob.h> has glob and globfree and lots of flags for glob.

I'm not sure if I fully understand your problem here, but in Windows you should be able to glob using FindFirstFile / FindNextFile functions from the WIN32 API. Honestly I don't know if their globing capability is comparable to glob() but you could give them a try

Related

Emacs recursive search/replace on Windows without Cygwin

To answer "where did I also use this identifier?" -questions, I run on macOS and the linuxes
dired-maybe-insert-subdir
dired-mark-files-regexp
followed by either of:
dired-do-find-regexp
dired-do-find-regexp-and-replace
On Windows I can get by using Emacs as my IDE without Cygwin, except for dired recursive search/replace.
Does Projectile
offer commands that will search/replace an identifier recursively without requiring Cygwin?
Does any other package make recursive search/replace possible on Windows without Cygwin?
I'm not concerned about the speed, because even after installing Cygwin, the recursive invocation of Cygwin's grep from within Emacs is painfully slow.
Update:
Recursive search/replace seems to be available in both
Helm
and
Projectile.
If true, then my question is:
Is the feature available on Windows without Cygwin?
The list of requires in my .emacs is already excessive. What is a light package that will do recursive search/replace (without Cygwin on Windows)?
Clarification:
OK. Success. [Thanks to Drew] With a .emacs containing nothing but (require 'dired+), I can search-and-replace in marked files using M-+ Q on Windows without having Cygwin installed. I'm guessing that this will also work on linux/macOS, although perhaps not quite as quickly as delegating to grep. (The "Act on ALL files [] in and UNDER this dir?" confirmation message will start to get tedious, but that's a separate question.)
For the present question one issue needs clarification. dired+ augments the built-in dired family of commands. Is there a way for it to take over ordinary dired-do-find-regexp-and-replace? That's because with the one-liner .emacs, and with a few marked files in a directory listing, I get
File not found - GREP
File not found - -I
...
File not found - NUL
File not found - ;
indicating that grep is still being invoked. How can tell dired+ "I'm on Windows and I won't install Cygwin; please take over A and Q?" (mapped by default dired-do-find-regexp and the aforementioned command).
Editorial: dired+ seems a bit overwhelming, but if it solves this one problem (eliminate the need for Cygwin on Windows), it will be well-worth figuring out how to move from the usual dired commands to dired+.
I think you're looking for a way to search files and get a list of those that match a regexp. If tags-query-replace works for you on MS Windows (without Cygwin), and I think it should, then you can use command diredp-do-query-replace-regexp-recursive, bound to M-+ Q by default, available from Dired+.
That acts on all marked files in the current Dired buffer, and on all marked files in all of the buffer's marked subdirs, and so on, recursively.
With a non-negative prefix arg it acts on all files in the current buffer and all files in all subdirs, and so on, recursively. (That is, any marks are ignored, and the effect is as if everything were marked.)
If, instead of finding files that match a regexp, you want to search through files then you can use command diredp-do-isearch-regexp-recursive, bound to M-+ M-s a C-M- by default, also available from Dired+. The files to access are defined similarly (all Dired+ dired[p]-do...-recursive commands act similarly wrt which files are identified to act on). That definitely does not require any Cygwin etc. commands - it's just Isearch.

Wildcards in vera++

I feel really really dumb but how do I use wildcards with vera++?
I would think I could do:
vera++ --root "C:\Program Files (x86)\vera++\lib\vera++" -R L001 *.cpp
But I get:
error: cannot open source file *.cpp
while executing
"GetAllLines $f"
...
Thanks,
Daniel Dekkers
To summarize the comments:
It would appear that Vera++ does not support this. (On Windows, handling wildcards is the responsibility of individual programs; on other platforms, it's handled by the shell. If Vera++ is primarily developed by non-Windows users, that may explain the oversight.)
You're not the first person to run into this.
There are several workarounds: pass - as a filename and pipe in a list of files on stdin, or use a Makefile or similar script or tool runner, or use PowerShell or a for loop, or use another shell (like MinGW or Cygwin).

C++ Passing multiple files using * character via command line

Under linux, I do the following
COMMAND /home/directory/*.txt
and all the files in that directory get passed as separate parameters (20 files in the directory results in 20 parameters in the argv variable)
Under windows, the same command results in one parameter (that string exactly).
Is this a compiler issue (VisualC++ 2008) or a windows thing or what?
In the past, I've written batch files to parse the files into multiple parameters, but I'm hoping there's a better way.
Any help would be appreciated
It's somewhat more limited than most Unix shells, but VC++ includes a file named setargv.obj that you can link with to add globbing to your application. It supports * and ?, which covers most of what most people care about.
To use it, just include setargv.obj when you link your file. In most cases, this just means adding the file name to the command line, something like this:
cl myfile.c myotherfile.c setargv.obj
Indeed that is a shell globbing feature. In PowerShell you would handle wildcard expansion inside your function using Convert-Path (or Resolve-Path) e.g.:
function ITakeWildcards([string]$Path) {
$paths = Convert-Path $path
foreach ($aPath in $paths) {
"Processing path $aPath"
}
}

How can I quickly search all included header files in a project for a specific symbol?

Here's a problem I've had recently that just HAS to be a common pain to others here.
I'm working with someone else's legacy C code and need to find where a function or macro was defined. The code #includes a bunch of different standard system libraries in addition to those from the specific project.
Is there a tool or technique to quickly find where a specific function, macro, (or other global for that matter) was defined?
I tried:
grep -R 'function' /usr/lib
and other similar *nix/bash-fu with only limited success and lots of annoying chaff to cull. One of you sage coders out there must have a good solution to this seemingly common scenario.
I was very surprised to not find another question on this particular pain here or in my searches of the interwebs. (I'm sure there will be angry comments if I missed one... ;-))
Thanks in advance for any tips!
Use etags/ctags from the exuberant ctags project in conjunction with an editor (emacs, vim) that understands them, or GNU GLOBAL.
Oh, and if you happen to use automake it generates a target TAGS. So no need for complicated manual calls to {c,e}tags.
Use ctags/cscope + vim/emacs
you can google for their detail use.
if you use ctags + vim, you can :
1.go to the /usr/include directory, excute ctags -f tags1 -R . generate the tags
2.generate tags for your code in your code directory ctags -f tags2 -R.
3.run :set path+=tags1,tags2 in your vim
4.under a function or marco try CTRL+]
Here is what you can do, assuming you use gcc, if not just modify it accordingly.
gcc -E myfile.c | grep '^#' | cut -f 3 -d ' ' | sort |uniq | xargs -n 1 grep -l "MYMACROORFUNCTIONNAME"
You can use Eclipse CDT. For example here is described how to setup CDT project to navigate Linux kernel source - HowTo use the CDT to navigate Linux kernel source.
vim + ctags is the way to go. You can jump to and definition of functions, global variables, macros, etc. etc.
FYI, browsing programs with tags
Also, if you want to quickly switch between .c and .h files, please refer to this blog
you can use cscope or emacs/vim + xcscope.el to do that easily. I think it's batter than ctage and etage.
Provided the correct headers are included that directly or indirectly define what you look for, most IDEs have a jump-to-definition-functionality that works.
The tags-approaches are of course nice because they don't depend on correctly included headers.

Using Eclipse CDT from command line

I need to have some of my C++ classes, functions and namespaces renamed as a part of my build script, which is runned by my CI system.
Unfortunatly a simple sad/awk/gsar/... technique is not enough, and I need smart rename refactoring, that carefully analyses my code.
Actually I found out, that CDT C/C++ rename refactoring does, what I need. But it does it from Eclipse IDE. So I need to find a way to start it from command line, and to make it a part of my CI build script.
I know that Eclipse has eclipsec executable, that allowes running some Eclipse functions from command line (see e.g. here).
But I can't find any suitable documentation for functions, CDT exports to command line. The only thing, I found is the this. But it doesn't solve my problem.
So, I need help to run CDT rename refactoring from command line (or someway like that). If it is not possible, may be someone will advice another tool, that can do rename refactoring for C++ from command line ?
Pragmatic Approach
"I need to have renamed as a part of my build script"
This sounds a bit like a design problem. However, I remember having been guilty of the same sin once writing a C++ application on AIX/Win32: most notably, I wanted to be able to link 'conflicting' versions of shared objects. I solved it using a simple preprocessor hack like this:
# makefile
#if($(ALTERNATIVE))
CPPFLAGS+=-DLIBNAMESPACE=MYLIB_ALTERNATIVE
#else
CPPFLAGS+=-DLIBNAMESPACE=MYLIB
#endif
./obj64/%.o: %cpp
xlC++ $(CPPFLAGS) $^ -o %#
Sample source/header file:
namespace MYLIB
{
class LibService :
{
};
}
As you can see, this required only a single
find -iname '*.[hc]pp' -o -iname '*.[hc]' -print0 |
xargs -0 sed -i 's/OldNamespace/MYLIB/g'
Eclipse Automation
You could have a look at eclim, which does most, if not all, of what you describe, however it targets the vim editor.
What eclim boasts, is full eclipse intergration (completion, refactoring, usage search etc.) from an external program. I'm not fully up to speed with the backend of eclim, but I do know that it works with a eclimd server process that exposes the service interface used by the vim plugin.
I suspect you should be able to reuse the code from eclimd if not just use eclim for your purposes.
We are completing a command-line rename tool for C++, that uses compiler accurate parsing and name resolution, including handling of shadowed names. Contact me (see bio) for further details or if you might be interested in a beta.