lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jun 27, 2014, at 9:55 PM, Austin Einter <austin.einter@gmail.com> wrote:

> Hi All
> Thanks a lot. Using your recommended means (-DLUA_USE_APICHECK and checking return values of luaL_loadstring, lua_pcall ) , I found the errors.
> 
> I am trying to resolve the errors.
> 
> Lets say I have a simple function as below.
> 
> const char *c_function = "function modify_content_length(msg, length) "
>                          "local offset = string.find(msg, \"\r\n\r\n\", 1, true) "
>                          "return 0 "
>                          "end";
> 
> Looks I am getting error for line "local offset = string.find(msg, \"\r\n\r\n\", 1, true) "
>  
> If that line I change to "local offset = string.find(msg, \"someword\", 1, true) " then I do not get error (do not crash, execution completes).
> 
> Ideally both statements are similar, in one case I have \r\n\r\n and other case I have someward. 
> 
> In 1st case I get error "COMPILE: [string "function modify_content_length(msg, length) l..."]:1: unfinished string near '"'"
> 
> and other case it executes and not crashed.
> 
> 
> Kindly help me to understand why this error.
> 
> BR
> Austin

You need to use double-backslashes in your quoted string on the newline characters...

"local offset = string.find(msg, \"\\r\\n\\r\\n\", 1, true)"

Without the double backslashes C will convert the \r and \n to carriage return and linefeed characters before Lua sees anything, hence the "unfinished string" error you are getting.

With the double backslashes only the backslashes will be escaped and so string.find will get the literal string "\r\n\r\n", which is what you desire.

Why are you hardcoding a Lua function in C instead of loading it from a file? If you are hardcoding a script then why not just use C itself to perform this action?

~pmd