Solved

Redirection of DOS output

Posted on 1997-04-14
6
320 Views
Last Modified: 2010-04-06
I am writing a Delphi 1 shell for a 16-bit dos grep.

How do I, without calling a dos batch file, use delphi code to redirect the output of a call to a dos command to a file?

An equivalent dos command line would be
'grep -flc foo bar.txt > baz.txt'

It is the '> baz.txt' portion for which I seek an answer.

Thanks.
0
Comment
Question by:laowai
6 Comments
 
LVL 3

Expert Comment

by:mheacock
ID: 1335389
Is the DOS GREP your own creation?  I'm guessing no, else the solution would be fairly simple.
0
 

Author Comment

by:laowai
ID: 1335390
No, the 16 bit grep is not my own.  However, sample code for an *equally fast* grep would be a handsome substitute for an answer to the first question.  

I have found two problems with the following algorithm, implemented in 16 bit Delphi:
    1) Take search string
    2) Take line from file searched.
    3) If search string is in searched line, return searched line
    4) Repeat until no more lines in searched file.

Problem 1: It is not as quick as grep.

Problem 2: It bails on large files.  (I am specifically working with dictionaries, 9mbs etc.)  16 bit Grep, of course, doesn't bail.  What's it doing differently?

Related problem: The grep delphi components going around all only return whether the search file contains the string; they are not as versatile as grep.  The majority of these components are wrappers for the old regexp.dll.

aloha.
0
 
LVL 1

Expert Comment

by:flisakow
ID: 1335391

I suspect the fast versions of grep are constructing a
finite state machine to check a line for the pattern, rather
than just checking it in a naive way.

They might be reading the file in large blocks also, checking the entire block for the pattern, then retro-actively figuring out where the line was when the pattern is found.

You should be able to get a free copy of a grep code from Gnu or
a Linux site.  Also, DJGPP includes egrep and fgrep, but they
are 32-bit, even though they run on top of DOS.

Shaun
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Expert Comment

by:kolarb
ID: 1335392
If you need grep you might just have it. The source code for grep was included with Pascal7 as a Turbo Vision example.
0
 
LVL 4

Accepted Solution

by:
erajoj earned 150 total points
ID: 1335393
This is a really simple problem:
Just use command.com something like this:
  WinExec('command /c grep -flc foo bar.txt > baz.txt'...

You can also use CreateProcess if you want to wait for
the grep to finish before your shell continues.

Hope this helps.
0
 
LVL 1

Expert Comment

by:hustch
ID: 1335394
I assume you are creating a dos program.

You have 2 options :

a) Use command.com to execute your program and redirect the output. This is simple, but uses memory for additional command.com, and you can't control output to STDERR (see below).

b) Pipe the output our self :
Piping output from at dos program (using > tofile.txt) works,
because the dos program writes output to standard output (STDOUT) and/or standard error (SDTERR). When a program is invoked, these files has already been opened by command.com, so all you have to do is create a new file (or two) for the output, and then set then handle of STDOUT (and optionally STDERR) to the new handle(s).
Here is an example from a C program of mine (in Pascal notation):

const
  STDOUT = 1
  STDERR = 2

// preserve old stdout and stderr
OldOutHandle := dup(STDOUT);
OldErrHandle := dup(STDERR);

// create file for output
f = filecreate ('MyFile.TXT');

{ redirect standard files to output file
  by duplicating the file handle onto
  the file handle for standard files. }

dup2(f, STDOUT);
dup2(f, STDERR);

// close the handle for output file
fileclose(f);

 Here, you execute the program

// restore original standard handles
dup2(OldOutHandle, STDOUT);
dup2(OldErrHandle, STDERR);

// close duplicate handle for standard files
fileclose(OldOutHandle);
fileclose(OldErrHandle);

The C Dup function can be replaced by a DOS call (INT 21H)
with ah = $45, bx = handle.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).
If OK, ax is the new handle (result of dup)

The C Dup2 function can be replaced by a DOS call (INT 21H)
with ah = $46, bx = handle of your file, cx = handle to duplicate.
At return, carry is set if error, AX = error code (4 = to many files open, 6 = invalid handle).

Let me know, if you need further help
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 89
Delphi application Soap connection 5 79
Printing problem 2 72
Delphi Form ownership 4 54
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now