* allow default path expanding in path/cpath and added tests
* added revim script to automatically insert vim modeline to sources * added retab script to reexpand tabs for given files * format sources and tests
This commit is contained in:
Родитель
9f206095f0
Коммит
75b3362d5a
1
TODO
1
TODO
|
@ -1,5 +1,4 @@
|
|||
Current:
|
||||
* include original path/cpath instead of override them all for lua_package_path/cpath
|
||||
* add directives to run lua codes when nginx stops/reloads
|
||||
* implement ngx.exec() functionality
|
||||
* deal with TCP 3-second delay problem under great connection harness
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef DDEBUG_H
|
||||
#define DDEBUG_H
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#define DDEBUG 0
|
||||
#include "ddebug.h"
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_CACHE_H
|
||||
#define NGX_HTTP_LUA_CACHE_H
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#include "ngx_http_lua_clfactory.h"
|
||||
|
||||
typedef struct {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_CLFACTORY_H__
|
||||
#define NGX_HTTP_LUA_CLFACTORY_H__
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_COMMON_H
|
||||
#define NGX_HTTP_LUA_COMMON_H
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#include "ngx_http_lua_conf.h"
|
||||
#include "ngx_http_lua_util.h"
|
||||
|
||||
|
@ -108,5 +109,4 @@ ngx_http_lua_init_vm(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
|
|||
return NGX_CONF_OK;
|
||||
}
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_CONF_H__
|
||||
#define NGX_HTTP_LUA_CONF_H__
|
||||
|
||||
|
@ -11,5 +12,4 @@ extern char* ngx_http_lua_merge_loc_conf(ngx_conf_t *cf, void *parent, void *chi
|
|||
|
||||
#endif
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#define DDEBUG 0
|
||||
|
||||
#include "ngx_http_lua_contentby.h"
|
||||
|
@ -325,6 +326,3 @@ error:
|
|||
return;
|
||||
}
|
||||
|
||||
/* vi:ts=4 sw=4 fdm=marker
|
||||
*/
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_CONTENT_BY_H__
|
||||
#define NGX_HTTP_LUA_CONTENT_BY_H__
|
||||
|
||||
|
@ -11,5 +12,4 @@ extern void ngx_http_lua_wev_handler(ngx_http_request_t *r);
|
|||
|
||||
#endif
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#define DDEBUG 0
|
||||
|
||||
#include "ngx_http_lua_directive.h"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_DIRECTIVE_H
|
||||
#define NGX_HTTP_LUA_DIRECTIVE_H
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#include "ngx_http_lua_filter.h"
|
||||
#include "ngx_http_lua_util.h"
|
||||
|
||||
|
@ -56,5 +57,4 @@ ngx_http_lua_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
|
|||
return NGX_OK;
|
||||
}
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_FILTER_H__
|
||||
#define NGX_HTTP_LUA_FILTER_H__
|
||||
|
||||
|
@ -7,5 +8,4 @@ extern ngx_int_t ngx_http_lua_filter_init(ngx_conf_t *cf);
|
|||
|
||||
#endif
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#include "ngx_http_lua_hook.h"
|
||||
#include "ngx_http_lua_util.h"
|
||||
#include "ngx_http_lua_contentby.h"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_HOOK_H
|
||||
#define NGX_HTTP_LUA_HOOK_H
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#include "ngx_http_lua_directive.h"
|
||||
#include "ngx_http_lua_conf.h"
|
||||
#include "ngx_http_lua_filter.h"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#define DDEBUG 0
|
||||
#include "ngx_http_lua_setby.h"
|
||||
#include "ngx_http_lua_hook.h"
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_SET_BY_H__
|
||||
#define NGX_HTTP_LUA_SET_BY_H__
|
||||
|
||||
|
@ -13,5 +14,4 @@ extern ngx_int_t ngx_http_lua_set_by_chunk(
|
|||
|
||||
#endif
|
||||
|
||||
// vi:ts=4 sw=4 fdm=marker
|
||||
|
||||
|
|
|
@ -1,13 +1,30 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#define DDEBUG 0
|
||||
#include "ngx_http_lua_util.h"
|
||||
#include "ngx_http_lua_hook.h"
|
||||
|
||||
#define LUA_PATH_SEP ";"
|
||||
#define AUX_MARK "\1"
|
||||
|
||||
static void init_ngx_lua_registry(lua_State *L);
|
||||
static void init_ngx_lua_globals(lua_State *L);
|
||||
static void inject_http_consts(lua_State *L);
|
||||
static void inject_log_consts(lua_State *L);
|
||||
static void setpath(lua_State *L, int tab_idx, const char *fieldname, const char *path, const char *def);
|
||||
|
||||
static void
|
||||
setpath(lua_State *L, int tab_idx, const char *fieldname, const char *path, const char *def)
|
||||
{
|
||||
const char *tmp_path;
|
||||
|
||||
tmp_path = luaL_gsub(L, path, LUA_PATH_SEP LUA_PATH_SEP, LUA_PATH_SEP AUX_MARK LUA_PATH_SEP);
|
||||
luaL_gsub(L, tmp_path, AUX_MARK, def);
|
||||
lua_remove(L, -2);
|
||||
|
||||
/* fix negative index as there's new data on stack */
|
||||
tab_idx = (tab_idx < 0) ? (tab_idx - 1) : tab_idx;
|
||||
lua_setfield(L, tab_idx, fieldname);
|
||||
}
|
||||
|
||||
lua_State *
|
||||
ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
|
||||
|
@ -28,15 +45,33 @@ ngx_http_lua_new_state(ngx_conf_t *cf, ngx_http_lua_main_conf_t *lmcf)
|
|||
}
|
||||
|
||||
if (lmcf->lua_path.len != 0) {
|
||||
lua_pushlstring(L, (char *) lmcf->lua_path.data, lmcf->lua_path.len);
|
||||
const char *old_path;
|
||||
const char *new_path;
|
||||
|
||||
lua_setfield(L, -2, "path");
|
||||
lua_getfield(L, -1, "path"); /* get original package.path */
|
||||
old_path = lua_tostring(L, -1);
|
||||
|
||||
lua_pushlstring(L, (char *) lmcf->lua_path.data, lmcf->lua_path.len);
|
||||
new_path = lua_tostring(L, -1);
|
||||
|
||||
setpath(L, -3, "path", new_path, old_path);
|
||||
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
|
||||
if (lmcf->lua_cpath.len != 0) {
|
||||
lua_pushlstring(L, (char *) lmcf->lua_cpath.data, lmcf->lua_cpath.len);
|
||||
const char *old_cpath;
|
||||
const char *new_cpath;
|
||||
|
||||
lua_setfield(L, -2, "cpath");
|
||||
lua_getfield(L, -1, "cpath"); /* get original package.cpath */
|
||||
old_cpath = lua_tostring(L, -1);
|
||||
|
||||
lua_pushlstring(L, (char *) lmcf->lua_cpath.data, lmcf->lua_cpath.len);
|
||||
new_cpath = lua_tostring(L, -1);
|
||||
|
||||
setpath(L, -3, "cpath", new_cpath, old_cpath);
|
||||
|
||||
lua_pop(L, 2);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
/* vim:set ft=c ts=4 sw=4 et fdm=marker: */
|
||||
#ifndef NGX_HTTP_LUA_UTIL_H
|
||||
#define NGX_HTTP_LUA_UTIL_H
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:filetype=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
@ -34,9 +34,10 @@ __DATA__
|
|||
|
||||
location /load {
|
||||
content_by_lua '
|
||||
package.loaded.foo = null;
|
||||
package.loaded.foo = nil;
|
||||
local foo = require "foo";
|
||||
foo.hi()';
|
||||
foo.hi()
|
||||
';
|
||||
}
|
||||
|
||||
location /check {
|
||||
|
@ -85,3 +86,63 @@ GET /main
|
|||
--- user_files
|
||||
--- response_body_like: ^[^;]+/servroot/html/\?.so$
|
||||
|
||||
|
||||
|
||||
=== TEST 3: expand default path (after)
|
||||
--- http_config eval
|
||||
"lua_package_path '$::HtmlDir/?.lua;;';"
|
||||
--- config
|
||||
location /main {
|
||||
content_by_lua '
|
||||
ngx.print(package.path);
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /main
|
||||
--- response_body_like: ^[^;]+/servroot/html/\?.lua;.+\.lua;$
|
||||
|
||||
|
||||
|
||||
=== TEST 4: expand default cpath (after)
|
||||
--- http_config eval
|
||||
"lua_package_cpath '$::HtmlDir/?.so;;';"
|
||||
--- config
|
||||
location /main {
|
||||
content_by_lua '
|
||||
ngx.print(package.cpath);
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /main
|
||||
--- response_body_like: ^[^;]+/servroot/html/\?.so;.+\.so;$
|
||||
|
||||
|
||||
|
||||
=== TEST 5: expand default path (before)
|
||||
--- http_config eval
|
||||
"lua_package_path ';;$::HtmlDir/?.lua';"
|
||||
--- config
|
||||
location /main {
|
||||
content_by_lua '
|
||||
ngx.print(package.path);
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /main
|
||||
--- response_body_like: ^.+\.lua;[^;]+/servroot/html/\?.lua$
|
||||
|
||||
|
||||
|
||||
=== TEST 6: expand default cpath (before)
|
||||
--- http_config eval
|
||||
"lua_package_cpath ';;$::HtmlDir/?.so';"
|
||||
--- config
|
||||
location /main {
|
||||
content_by_lua '
|
||||
ngx.print(package.cpath);
|
||||
';
|
||||
}
|
||||
--- request
|
||||
GET /main
|
||||
--- response_body_like: ^.+\.so;[^;]+/servroot/html/\?.so$
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
# vi:ft=perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
use lib 'lib';
|
||||
use Test::Nginx::Socket;
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
#!/bin/bash
|
||||
|
||||
for f in $*; do
|
||||
if [ -f "$f" ]; then
|
||||
vim -c retab -c x $f
|
||||
fi
|
||||
done
|
||||
|
|
@ -0,0 +1,102 @@
|
|||
#!/usr/bin/perl
|
||||
# vim:set ft=perl ts=4 sw=4 et fdm=marker:
|
||||
#
|
||||
# revim - add customized vim modeline for given files
|
||||
# Copyright (c) 2010 chaoslawful
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use Getopt::Std;
|
||||
|
||||
my %opts;
|
||||
|
||||
getopts('hm:', \%opts);
|
||||
|
||||
if($opts{h} or !@ARGV) {
|
||||
die <<EOT;
|
||||
Usage: revim [-m <vim modeline>] src/*
|
||||
|
||||
In <vim modeline>, the following placeholder(s) can be used:
|
||||
|
||||
* %t - Expands to vim file type. E.g. 'c' for .c files, 'perl' for .pl files.
|
||||
EOT
|
||||
}
|
||||
|
||||
my $vim_ml = $opts{m} || "vim:set ft=%t ts=4 sw=4 et fdm=marker:";
|
||||
my @files = map glob, @ARGV;
|
||||
for my $file (@files) {
|
||||
next if -d $file;
|
||||
my ($ft, $ml) = detect_filetype($file, $vim_ml);
|
||||
next if !defined($ft);
|
||||
revim($file, $ml);
|
||||
}
|
||||
|
||||
sub detect_filetype
|
||||
{
|
||||
my ($f, $tmpl) = @_;
|
||||
my ($ft, $lcmt, $rcmt);
|
||||
my %phs;
|
||||
|
||||
if($f =~ /.([cC]|[hH])$/) {
|
||||
$ft = "c";
|
||||
($lcmt, $rcmt) = ("/* ", " */");
|
||||
} elsif($f =~ /.(pl|pm)$/) {
|
||||
$ft = "perl";
|
||||
($lcmt, $rcmt) = ("# ", "");
|
||||
} elsif($f =~ /.t_?$/) {
|
||||
# assuming tests are written in perl
|
||||
$ft = "perl";
|
||||
($lcmt, $rcmt) = ("# ", "");
|
||||
} else {
|
||||
$ft = undef;
|
||||
}
|
||||
|
||||
if(defined($ft)) {
|
||||
%phs = (
|
||||
"%t" => $ft,
|
||||
);
|
||||
|
||||
$tmpl =~ s/(%[a-z])/$phs{$1}/ge;
|
||||
$tmpl =~ s/^/$lcmt/;
|
||||
$tmpl =~ s/$/$rcmt/;
|
||||
|
||||
return ($ft, $tmpl);
|
||||
}
|
||||
|
||||
return (undef, undef);
|
||||
}
|
||||
|
||||
sub revim
|
||||
{
|
||||
my ($f, $ml) = @_;
|
||||
my @lines;
|
||||
|
||||
open my $in, $f
|
||||
or die "Can't open $f for reading: $!";
|
||||
while(<$in>) {
|
||||
push(@lines, $_);
|
||||
}
|
||||
close $in;
|
||||
|
||||
my @nlines = grep {!/\bvim?:/} @lines;
|
||||
warn "revim: $f:\tremoved existing vim modeline.\n"
|
||||
if(@nlines != @lines);
|
||||
|
||||
if($nlines[0] =~ /^#!/) { # has shebang line
|
||||
my $shebang = shift @nlines;
|
||||
unshift(@nlines, $shebang, "$ml\n");
|
||||
} else {
|
||||
unshift(@nlines, "$ml\n");
|
||||
}
|
||||
|
||||
my $text = join '', @nlines;
|
||||
|
||||
open my $out, "> $f"
|
||||
or die "Can't open $f for writing: $!";
|
||||
binmode $out;
|
||||
print $out $text;
|
||||
close $out;
|
||||
|
||||
warn "revim: $f:\tdone.\n";
|
||||
}
|
||||
|
Загрузка…
Ссылка в новой задаче