?
Solved

Redirection of DOS output

Posted on 1997-04-14
6
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 450 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

649 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