[asterisk-users] [AGI 1.4] C sample?

Lee Jenkins lee at datatrakpos.com
Sun Jan 27 08:09:59 CST 2008


Vincent wrote:
> Hello
> 
> 	I'm pretty much a newbie when it comes to C, but I have to use
> this language to write a couple of AGI proggies because I need them to
> be statically compiled.
> 
> Strangely enough, Google didn't return much when looking for the
> "Hello, world!" of AGI in C.
> 
> The following doesn't work: The file never gets written:
> ===========
> //check_cid.c
> #include <stdio.h>
> #include <stdlib.h>
> #include <syslog.h>
> #include <string.h>
> 
> int main(int argc, char *argv[])
> {
>    char line[80];
>    int i;
> 
>    setlinebuf(stdout);
>    setlinebuf(stderr);
> 
>    FILE *file;
>    file = fopen("file.txt","a+");
>    while (1) {
>       fgets(line,80,stdin);
>       fprintf(file,"%s",line);
>       if (strlen(line) <= 1) break;
>    }
>    fclose(file);
> 
>    return(EXIT_SUCCESS);
> }
> ===========
> 
> This is how it's called in extensions.conf:
> ===========
> [inside]
> exten => 9999,1,Verbose(Yes!)
> exten => 9999,n,AGI(check_cid.exe|123)
> ===========
> 
> And this is the output of "agi debug" in CLI:
> ===========
> *CLI>     -- Executing [9999 at inside:1] Verbose("SIP/2000-0904bee0",
> "Yes!") in new stack
> Yes!
>     -- Executing [9999 at inside:2] AGI("SIP/2000-0904bee0",
> "check_cid.exe|123") in new stack
>     -- Launched AGI Script /var/lib/asterisk/agi-bin/check_cid.exe
> AGI Tx >> agi_request: check_cid.exe
> AGI Tx >> agi_channel: SIP/2000-0904bee0
> AGI Tx >> agi_language: en
> AGI Tx >> agi_type: SIP
> AGI Tx >> agi_uniqueid: 1201412176.3
> AGI Tx >> agi_callerid: 2000
> AGI Tx >> agi_calleridname: Fred
> AGI Tx >> agi_callingpres: 0
> AGI Tx >> agi_callingani2: 0
> AGI Tx >> agi_callington: 0
> AGI Tx >> agi_callingtns: 0
> AGI Tx >> agi_dnid: 9999
> AGI Tx >> agi_rdnis: unknown
> AGI Tx >> agi_context: inside
> AGI Tx >> agi_extension: 9999
> AGI Tx >> agi_priority: 2
> AGI Tx >> agi_enhanced: 0.0
> AGI Tx >> agi_accountcode:
> AGI Tx >>
>     -- AGI Script check_cid.exe completed, returning 0
>   == Auto fallthrough, channel 'SIP/2000-0904bee0' status is 'UNKNOWN'
> ===========
> 
> If someone has a very basic example in C that shows how to read the
> CID #, and rewrite the CID name, I'm interested.
> 
> Thank you.
> 
> 

Sorry, I don't have a sample for you as I write mostly in Freepascal/Lazarus 
these days and use my own library for AGI/FastAGI.  That said, did you try 
saving the file to a fully qualified path?

I say that because in pascal, usually you can do this on Windows:

var
   sFile: string
   AStringList: TStringList;
begin
   sFile := ExtractFilePath(ParamStr(0)) + 'myfile.txt';

   AStringList := TStringList.create;
   try
     AStringList.LoadFromFile(sFile);
     Write(AStringList.Text);
   finally;
      AStringList.free;
      end;
end;

Normally ExtractFilePath would return the directory path that the executable is 
locate in.  My understanding is that that is not necessarily the case on linux 
which takes into account the directory from which the call to the executable is 
being made, which might not be the same directory that the executable is located on.

Could be the same thing you are experiencing.  Try using a fully qualified path 
/usr/mydirect/myfile.txt


-- 
Warm Regards,

Lee

"Everything I needed to learn in life, I learned selling encyclopedias door to 
door."



More information about the asterisk-users mailing list