Need a bit of help with reading in characters.

A

Advocated

Hey all, thanks for taking the time to read this in the first place.

Anyway, ill try and keep it simple. In my program, if i type $ man something
it should read in the 2 words, man and something
it should look up man - find that its a function, run the man function, and
then, with that second word "something" it should then execute that as its
parameters, if that make sense.

At the moment, it semi works, i.e If i type man it will run the function i
want, but i cant work out where to go next, im not even sure if the method
is right.. Any ideas?

The way i see it is that the first word/token is the command or function,
the 2nd, maybe third and so on, are its paremeters.
A way to describe it could be if i were to type: dir /p /w so it would run
the dir function, use /p as its first param, and then so on. Hope this makes
sense

Thanks for any advice or ideas.

[Code has been uploaded]

http://tinyurl.com/29u5z

(this code is not my original code and has been provided by a Lecturer with
permission to modify)
 
L

lcustoc611

I am not sure I understand what you're trying to say (yeh....computer
overdose) but from what I see you're having problems evaluating the
parameters of your program. (i.e. you're getting the "dir", but not being
able to analyze "/p").

The problem that you are having might (hehe) be caused by the ~ scanf("%s",
string); ~ line, where you should enter the parameters. Using scanf, if you
type "dir /p" it would first assign "dir" to string, analyze it, run the
block (if any) and then go back with "/p" into string again.

If your problem is simply with how to analyse the parameters, you have to
first check what the user has entered (on the example above, "dir") then
from within that analyze the parameters and possible choices for each (you
might wanna use an dynamic array for that). So if the user has entered "/p"
it would do that, if the user has entered "/y" it would do this, and so
forth.

Hope I've helped you out,
Leo C.
(e-mail address removed)
 
D

donLouis

Hey all, thanks for taking the time to read this in the first place.

Anyway, ill try and keep it simple. In my program, if i type $ man > something
it should read in the 2 words, man and something
it should look up man - find that its a function, run the man > function, and
then, with that second word "something" it should then execute that as > its
parameters, if that make sense.

at first i thought you were confusing command-line parameters (argv[])
with function parameters. after looking at the code, it looks more
like a proto-shell of sorts. if that is the case, then look into
strtok(3) to "tokenize" the contents of string.

after that, you should declare main() as type int, lose the infinite
loop, return from main(), and ditch the c++ style comments.
 
A

Advocated

lcustoc611 said:
I am not sure I understand what you're trying to say (yeh....computer
overdose) but from what I see you're having problems evaluating the
parameters of your program. (i.e. you're getting the "dir", but not being
able to analyze "/p").

The problem that you are having might (hehe) be caused by the ~ scanf("%s",
string); ~ line, where you should enter the parameters. Using scanf, if you
type "dir /p" it would first assign "dir" to string, analyze it, run the
block (if any) and then go back with "/p" into string again.

If your problem is simply with how to analyse the parameters, you have to
first check what the user has entered (on the example above, "dir") then
from within that analyze the parameters and possible choices for each (you
might wanna use an dynamic array for that). So if the user has entered "/p"
it would do that, if the user has entered "/y" it would do this, and so
forth.

Hope I've helped you out,
Leo C.
(e-mail address removed)
<snip>

What i think you are saying is create something which recognises the
parameters, i.e make something for each possible parameter. The problem with
this is, my program will be running external commands too, so it would be
impossible to tell what the user will type? Is this what you mean?

Cheers for the reply too
 
A

Advocated

Hey all, thanks for taking the time to read this in the first place.
Anyway, ill try and keep it simple. In my program, if i type $ man > something
it should read in the 2 words, man and something
it should look up man - find that its a function, run the man > function, and
then, with that second word "something" it should then execute that as > its
parameters, if that make sense.

at first i thought you were confusing command-line parameters (argv[])
with function parameters. after looking at the code, it looks more
like a proto-shell of sorts. if that is the case, then look into
strtok(3) to "tokenize" the contents of string.

after that, you should declare main() as type int, lose the infinite
loop, return from main(), and ditch the c++ style comments.

Well i think i get what you mean, this is the original code i was looking
at:
http://tinyurl.com/39nfe

I think this uses a kind of tokeniser, but the problem is, the input is
predifined if that makes any sense, i.e
strcpy(InputString," Test\t \t of the parser \t with its problems....
a");
is already the user input, instead of getting the actual user input.

The idea is, it will actually be a kind of shell, if i can get this first
part to function. Any more ideas?

Thanks
 
D

donLouis

I think this uses a kind of tokeniser, but the problem is, the input >is
predifined if that makes any sense, i.e
strcpy(InputString," Test\t \t of the parser \t with its >problems....
a");
is already the user input, instead of getting the actual user input.

The idea is, it will actually be a kind of shell, if i can get this first
part to function. Any more ideas?

back up to the scanf of string. the program _immediately_ runs string
through findCmd. if i type in man or dir, it indexes the function
properly. if i type in foobar, i get an "Unknown command" as expected.
if i type in man foobar, i get the output for man _and_ the output for
an unknown command.

_if_, what you are looking for is something like "running man w/ foobar
parameters", _then_, you need to break up string into an array of chars
similar to argv[] _before_ it runs through findCmd or any other parsers,
i.e. first you lex it, second you yacc it, then you run it.

if this is not what you are looking for, then i am babbling incoherently
and i apologize for wasting your time.
 
L

Leo Custodio

I still don't understand what you're trying to do, but let's see if with a
few more examples we all get it.
Are you trying to create a program that, when running, would accept
arguments and would pass them to the shell?
Because you've mentioned something earlier about your program being able to
accept external arguments as well (ie. myprogram -a -x -kill) but it does
not do that so far.

If your program will take external parameters, you might wanna have a look
at the main function and change it to something like ~ int main(int argc,
char * argv[]) ~ and analyze all the parameters from it and pass them back
to the shell.

If what you're trying to do is simply get parameters at runtime (ie. your
software prompts for user imput and you will then analyze them), you have to
change that scanf input type ~ scanf("%s", string); ~ as i've said before
and make it accept the whole line. Afterwards, you separate them and check
any arguments, enabling flags for each parameter and then running your
program according to the flags.



Leo
(e-mail address removed)



Advocated said:
Hey all, thanks for taking the time to read this in the first place.

Anyway, ill try and keep it simple. In my program, if i type $ man > something
it should read in the 2 words, man and something
it should look up man - find that its a function, run the man > function, and
then, with that second word "something" it should then execute that as
its
parameters, if that make sense.

at first i thought you were confusing command-line parameters (argv[])
with function parameters. after looking at the code, it looks more
like a proto-shell of sorts. if that is the case, then look into
strtok(3) to "tokenize" the contents of string.

after that, you should declare main() as type int, lose the infinite
loop, return from main(), and ditch the c++ style comments.

Well i think i get what you mean, this is the original code i was looking
at:
http://tinyurl.com/39nfe

I think this uses a kind of tokeniser, but the problem is, the input is
predifined if that makes any sense, i.e
strcpy(InputString," Test\t \t of the parser \t with its problems....
a");
is already the user input, instead of getting the actual user input.

The idea is, it will actually be a kind of shell, if i can get this first
part to function. Any more ideas?

Thanks
 
A

Advocated

Leo Custodio said:
I still don't understand what you're trying to do, but let's see if with a
few more examples we all get it.
Are you trying to create a program that, when running, would accept
arguments and would pass them to the shell?
Because you've mentioned something earlier about your program being able to
accept external arguments as well (ie. myprogram -a -x -kill) but it does
not do that so far.

If your program will take external parameters, you might wanna have a look
at the main function and change it to something like ~ int main(int argc,
char * argv[]) ~ and analyze all the parameters from it and pass them back
to the shell.

If what you're trying to do is simply get parameters at runtime (ie. your
software prompts for user imput and you will then analyze them), you have to
change that scanf input type ~ scanf("%s", string); ~ as i've said before
and make it accept the whole line. Afterwards, you separate them and check
any arguments, enabling flags for each parameter and then running your
program according to the flags.



Leo
(e-mail address removed)



Advocated said:
Hey all, thanks for taking the time to read this in the first place.

Anyway, ill try and keep it simple. In my program, if i type $ man > something
it should read in the 2 words, man and something
it should look up man - find that its a function, run the man > function, and
then, with that second word "something" it should then execute that
as

its
parameters, if that make sense.

at first i thought you were confusing command-line parameters (argv[])
with function parameters. after looking at the code, it looks more
like a proto-shell of sorts. if that is the case, then look into
strtok(3) to "tokenize" the contents of string.

after that, you should declare main() as type int, lose the infinite
loop, return from main(), and ditch the c++ style comments.

Well i think i get what you mean, this is the original code i was looking
at:
http://tinyurl.com/39nfe

I think this uses a kind of tokeniser, but the problem is, the input is
predifined if that makes any sense, i.e
strcpy(InputString," Test\t \t of the parser \t with its problems....
a");
is already the user input, instead of getting the actual user input.

The idea is, it will actually be a kind of shell, if i can get this first
part to function. Any more ideas?

Thanks

Thanks for all this help guys. Ive made the changes stated
http://tinyurl.com/2hkpx
and also added something to get the commands, this is working ok at the
moment, in the means that, if i type: dir *.exe it will print token 0 is
dir, token 1 is *.exe so i think im kind of getting the idea?

Im having a silly problem with it though, in the LeftTrim function it uses:
while ((*Input == ' ') || (*Input == '\t')) This is supposed to remove all
spaces and tabs from the input, but its only semi working and i cannot see
why. If i type hello<space>world it works fine, if i do
hello<space><space>world etc, it works fine, and both times prints token 0 =
hello token 1 = world.
The problem is with the tabs. If i type: <tab>hello world it prints token 0
= hello token 1 = world. but if i type: hello<tab>world it prints: token 0
= hello world.

Not sure why its not working, but in my previous examples where i wasnt
waiting for user input(using a predefined string: strcpy(InputString,"
Test\t \t of the parser \t with its problems.... a"); ) it worked
perfectly, Can anyone see where im going wrong? Ive tried running in Visual
Studio debugger, but for some reason its not working 100%, i.e i run the
debugger, it gets to say a printf statement and asks me where printf.c is
located :s

Cheers though all!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top