vim 中插入utf-8的bom头

VIM和BOM

最近发现个怪现象, 从Vim中复制的文本(xsel的方式)和实际文本有点点区别, 会在头部加几个不可见的字节(0xEF 0xBB 0xBF), 粘贴到别的程序里有时会引发错误.

查了下, 原来这几个字节是Unicode的BOM(Byte order mark),  用来标记UTF-16和UTF-32编码文件的字节序, UTF-8并不需要. 但因为这个文件我曾经用Windows的记事本保存过,  它自作聪明地加了个BOM,  而Vim识别到文件有BOM就会自动打开bomb这个选项(具体解释请看Vim中bomb和fileencodings的帮助), so…

于是, 这样, 然后保存就好了:

小提示, 查看当前bomb是否打开可以这样:

——————————–

创建一个文件,输入一行中文,执行
:set fileencoding=utf-8
设置文件编码,并执行
:w
保存,然后执行
:e
重新加载,接着插入BOM头
:1s/^/\="\xef\xbb\xbf"
最后保存并退出:
😡
成功了!
xialulee@xialulee-pc3 ~/lab
$ xxd bom1.txt
0000000: efbb bfe6 b58b e8af 95e4 b880 e4b8 8b42

……………B
0000010: 4f4d 0d0a OM..
前三个字节正是ef bb bf。
他:现在好了,可以用你以前交给我的方法将大量的cue文件批量转换了。
我:什么方法?
他:就是那个-c参数啊!以非交互的方式运行Vim,如:
xialulee@xialulee-pc3 ~/lab
$ vim -c ‘set encoding=shift_jis | set fileencoding=utf-8 | w | e | 1s/^/\="\xef\xbb\xbf"/ | x’ CDImage.ape.cue
可以以非交互的方式使用,配合bash或者cmd的for循环,就可以搞定一堆cue文件了。
我:那是。要说现在cmd的for循环也蛮强大的。而且还有PowerShell可以用。在没有msys或者GnuWin32的情况下,将cmd或PowerShell与非交互的Vim结合起来进行批量处理,应该可行的。

后台使用top命令需注意使用-b选项

通过其他程序或脚本在非交互式模式下调用top命令,经常会出现:
   top: failed tty get 错误

解决办法:加个-b 选项皆可

-b : Batch mode operation
   Starts top in <A1><AF>Batch mode<A1><AF>, which could be useful for sending output from top to other programs or to a file. In this mode, top will not accept input and runs until the iterations limit you<A1><AF>ve set with the <A1><AF>-n<A1><AF> command-line option or until killed.

例如执行:
top -bn 1

监控某个进程的状态:
top -bn 1 -p 14370 | grep cmdname>>stats.log

恶心的绊脚石

我在浏览器里面测试通过apache访问的脚本test.php 能不能读取 a.php 里面的内容,结果怎么也看不到a.php 的内容,但是也不报错,源代码为:

<?php
// test.php
echo file_get_contents("a.php");
?>

其实, a.php的内容和test.php 内容一样。

页面上没有显示a.php的内容,也没有报错信息,apache错误日志里面也没有任何报错信息。

我很奇怪,我只好去阅读file_get_contents的具体实现了,当我阅读到一半的时候,我突然想到:
不是读取失败,只是没有显示而已,因为a.php的内容是以 ‘<‘ 开头的,被解释成html标签了,所以页面什么也没有显示,查看页面源代码,果然如此

关于js正则表达式的一点注意

js中创建正则表达式对象有两种方法:

  1.  var reg = /pattern/;
  2.  var reg = new RegExp(‘pattern’)

如果 pattern是动态构造出来的,则第一种方式就不行了,如:
var reg = "/" + a_value + "/";
这个reg是字符串,不是正则表达式对象。这时候就需要使用第二种方式了。

使用第二种方式的时候也要注意,不要收第一种方式的影响,也不要受PHP中写法的影响,注意:
第二种方式创建的时候是不能有前后的反斜线的; // 这篇文章就说了这一句有用的话
参考资料:
http://www.cainiao8.com/web/js_note/js_regular_expression.html

将html中的图片和html保存在一个文件里

html是超文本标记语言,里面显示的图片一般是指向另一个url的,但是,在我使用word的时候,我发现word文件里面是可以包含图片的,而且图片是保存在word文件里面的,这个就比html方便多了,当我保存html的时候,一般需要把相应的资源文件(图片、css、js)保存到一个目录里面,这个感觉麻烦。
css、js是可以直接写在html里面的,那么,能不能将图片数据也直接写到html里面的,去google百度了一下,还真有:
http://hi.baidu.com/guodong828/blog/item/7d86960acf23052bb0351d7c.html

需要注意的是:
1. IE到IE8都还不支持(IE9没试)
2. base64的数据膨胀系数为 1/7,即原来的7位的数据被编码成了1个字节

避免变成死链接,这里copy一份:
—————————————————————————

        所谓"data"类型的Url格式,是在RFC2397中提出的,目的对于一些“小”的数据,可以在网页中直接嵌入,而不是从外部文件载入。例如对于img这个Tag,哪怕这个图片非常非常的小,小到只有一个点,也是要从另外一个外部的图片文件例如gif文件中读入的,如果浏览器实现了data类型的Url格式,这个文件就可以直接从页面文件内部读入了。

  data类型的Url格式早在1998年就提出了,时至今日,Firfox、Opera、Safari和Konqueror这些浏览器都已经支持,但是IE直到7.0版本都还没有支持,IE不支持的东西太多了,也不差这一个。

[小例子]

下面这个html代码可以在支持data类型Url的浏览器中运行,例如Firefox。运行后会看到一条蓝色渐变底色的标题。

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml" >

      <head>

      <style type="text/css">

            .title {  background-image:url(data:image/gif;base64,R0lGODlhAQAcALMAAMXh96HR97XZ98Hf98Xg97DX97nb98Lf97vc98Tg973d96rU97ba97%2Fe96XS9
wAAACH5BAAAAAAALAAAAAABABwAAAQVMLhVBDNItXESAURyDI2CGIxQLE4EADs%3D);

            background-repeat:repeat-x;

            height:28px;

            line-height: 28px;

            text-align:center;

      }

      </style>

      </head>

      <body>

            <div class="title">Hello, world!</div>

      </body>

      </html>

  这个渐变的蓝色底色实际上是用一个1×28的小图片通过横行重复(repeat-x)形成的。这个图片很小,不过104个字节,直接嵌入到html或css文件还是很合适的。

  data格式的Url最直接的好处是,这些Url原本会引起一个新的网络访问,因为那里是一个网页的地址,现在不会有新的网络访问了,因为现在这里是网页的内容。这样做,会减少服务器的负载,当然同时也增加了当前网页的大小。所以对“小”数据特别有好处。

  data类型Url的形式既然是Url,当然也可以直接在浏览器的地址栏中输入。

  data:text/html,<html><body><p><b>Hello, world!</b></p></body></html>在浏览器中输入以上的Url,会得到一个加粗的"Hello, world!"。也就是说,data:后面的数据直接用做网页的内容,而不是网页的地址。

简单的说,data类型的Url大致有下面几种形式。

      data:,<文本数据>data:text/plain,<文本数据>

      data:text/html,<HTML代码>

      data:text/html;base64,<base64编码的HTML代码>

      data:text/css,<CSS代码>

      data:text/css;base64,<base64编码的CSS代码>

      data:text/javascript,<Javascript代码>

      data:text/javascript;base64,<base64编码的Javascript代码>

      data:image/gif;base64,base64编码的gif图片数据

      data:image/png;base64,base64编码的png图片数据

      data:image/jpeg;base64,base64编码的jpeg图片数据

      data:image/x-icon;base64,base64编码的icon图片数据

  因为Url是一种基于文本的协议,所以gif/png/jpeg这种二进制属于需要用base64进行编码。换句话说,引入base64以后,就可以支持任意形式的数据格式。下面是个png图片的例子,会在浏览器中显示一个Mozilla的图标。

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHd

hcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAHWSURBVHjaYvz//z8DJQAggJiQOe/fv2fv7Oz8rays/N+VkfG/iYnJfyD/1+rVq7ffu3dP

FpsBAAHEAHIBCJ85c8bN2Nj4vwsDw/8zQLwKiO8CcRoQu0DxqlWrdsHUwzBAAIGJmTNnPgYa9j8UqhFElwPxf2MIDeIrKSn9FwSJoRkAE

EAM0DD4DzMAyPi/G+QKY4hh5WAXGf8PDQ0FGwJ22d27CjADAAIIrLmjo+MXA9R2kAHvGBA2wwx6B8W7od6CeQcggKCmCEL8bgwxYCbUIG

TDVkHDBia+CuotgACCueD3TDQN75D4xmAvCoK9ARMHBzAw0AECiBHkAlC0Mdy7x9ABNA3obAZXIAa6iKEcGlMVQHwWyjYuL2d4v2cPg8v

Zswx7gHyAAAK7AOif7SAbOqCmn4Ha3AHFsIDtgPq/vLz8P4MSkJ2W9h8ggBjevXvHDo4FQUQg/kdypqCg4H8lUIACnQ/SOBMYI8bAsAJF

Pcj1AAEEjwVQqLpAbXmH5BJjqI0gi9DTAAgDBBCcAVLkgmQ7yKCZxpCQxqUZhAECCJ4XgMl493ug21ZD+aDAXH0WLM4A9MZPXJkJIIAwT

AR5pQMalaCABQUULttBGCCAGCnNzgABBgAMJ5THwGvJLAAAAABJRU5ErkJggg==

 可以在Html的Img对象中使用,例如:

<img src="data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAF…" />

 可以在Css的background-image属性中使用,例如:

div.image {  width:100px;  height:100px;  background-image:url(data:image/x-icon;base64,AAABAAEAEBAAAAAAAABoBQAAF…);}

 可以在Html的Css链接处使用,例如:

<link rel="stylesheet" type="text/css"  href="data:text/css;base64,LyogKioqKiogVGVtcGxhdGUgKioq…" _fcksavedurl=""data:text/css;base64,LyogKioqKiogVGVtcGxhdGUgKioq…"" />

 可以在Html的Javascript链接处使用,例如:

<script type="text/javascript"  href="data:text/javascript;base64,dmFyIHNjT2JqMSA9IG5ldyBzY3Jv…"></script>

  完整的语法定义在RFC中,完整的语法定义如下:

dataurl:= "data:" [mediatype] [";base64"] "," datamediatype  := [type"/"subtype] *( ";" parameter )data:= *urlcharparameter  := attribute "=" valueurlchar指的就是一般url中允许的字符,有些字符需要转义,例如"="要转义为"%3D",不过我测试下来,至少在Firefox里面,不转义也是可以的。

  parameter可以对mediatype进行属性的扩展,常见的是charset,用来定义编码格式,在多语言情况下需要用到。例如下面的例子。

  data:text/plain;charset=UTF-8;base64,5L2g5aW977yM5Lit5paH77yB这个例子会显示出"你好,中文!"。如果吧charset部分去掉,就会显示乱码,因为我用的是UTF-8编码。

  Firefox有一个data类型Url的测试页面,列出了各种格式的data类型Url的测试Url,和测试结果说明。

base64编码和内容的隐秘

  把二进制数据转换成为Base64不是什么难事,比如Total Commander就有这样的功能。还有一些在线资源:

  http://www.greywyvern.com/code/php/binary2base64

  http://www.kawa.net/works/js/data-scheme/base64-e.html

  有些在线转换把base64里面的“=”转换成为%3D,这个在Url中和“=”是一样的,不转换也没什么问题。当然,这种Url还有一种隐秘的好处,就是将一些道貌岸然者不喜欢的东西,堂而皇之的放在页面上。


php 设置中的 auto_append_file 的一点注意

php 的auto_append_file设置允许在PHP执行退出时自动执行一个文件,我们就可以在这个文件中做一些自己想做的事了, 但是你要注意:
1. 该设置不能通过ini_set来设置,可在 php.ini, .htaccesshttpd.conf 中设置; 参考: http://php.net/ini
2.  如果主脚本中执行了exit,则auto_append_file 指定的文件将不被执行

参考资料:
http://cn.php.net/manual/zh/ini.php
http://cn.php.net/manual/zh/ini.sections.php

C语言中宏的几种定义方式

C语言中宏的几种定义方式:


#include
#include
#define ECHO(str) printf(“%s: %s”, #str,str)
#define ECHO2(str,num) printf(“%s”, str##num)
#define MULTILINE_ECHO(str,str2,str3) do {\
printf(“multiline echo:\n”); \
printf(“%s”, str); \
printf(“%s”, str2); \
printf(“%s”, str3); \
}while(0)

int main(int ac, char **av) {

char str[10] = “str1\n\0”;
char str2[10] = “str2\n\0”;
char str3[10] = “str3\n\0”;
ECHO(str);
ECHO2(str,2);
MULTILINE_ECHO(str, str2, str3);
return 0;
}

str: str1
str2
multiline echo:
str1
str2
str3

vim阅读apache的mod_php

创建tags 文件:
php-5.3.3 目录下:
ctags -R .
ctags -R -a -f tags /path/to/httpd-2.2.16
如果需要tags使用绝对路径,则可以使用参数:
–tag-relative=no

1. mod_php.c的写法,主要是定义一些函数,然后通过模块结构体变量挂到apache的钩子上:

php-5.3.3/sapi/apache/mod_php5.c
  1.     
    //…

        

  2.     
    // 1. 这个结构的名字php5_module是有严格要求的

        

  3.     
    module MODULE_VAR_EXPORT php5_module =

        

  4.     
    {

        

  5.     
        STANDARD_MODULE_STUFF,

        

  6.     
        php_init_handler,           /* initializer */

        

  7.     
        php_create_dir,             /* per-directory config creator */

        

  8.     
        php_merge_dir,              /* dir merger */

        

  9.     
        NULL,                       /* per-server config creator */

        

  10.     
        NULL,                       /* merge server config */

        

  11.     
        php_commands,               /* command table */

        

  12.     
        php_handlers,               /* handlers */

        

  13.     
        NULL,                       /* filename translation */

        

  14.     
        NULL,                       /* check_user_id */

        

  15.     
        NULL,                       /* check auth */

        

  16.     
        NULL,                       /* check access */

        

  17.     
        NULL,                       /* type_checker */

        

  18.     
        NULL,                       /* fixups */

        

  19.     
        NULL                        /* logger */

        

  20.     
    #if MODULE_MAGIC_NUMBER >= 19970103

        

  21.     
        , NULL                      /* header parser */

        

  22.     
    #endif

        

  23.     
    #if MODULE_MAGIC_NUMBER >= 19970719

        

  24.     
        , NULL                      /* child_init */

        

  25.     
    #endif

        

  26.     
    #if MODULE_MAGIC_NUMBER >= 19970728

        

  27.     
        , php_child_exit_handler        /* child_exit */

        

  28.     
    #endif

        

  29.     
    #if MODULE_MAGIC_NUMBER >= 19970902

        

  30.     
        , NULL                      /* post read-request *

        

  31.     
    #endif

        

  32.     
    };

        

2. apache模块结构的定义:

http-2.216/include/http_config.h
  1.     
    typedef struct module_struct module;

        

  2.     
    struct module_struct {          

        

  3.     
        /** API version, *not* module version; check that module is

        

  4.     
         * compatible with this version of the server.

        

  5.     
         */

        

  6.     
        int version;                

        

  7.     
        /** API minor version. Provides API feature milestones. Not checked

        

  8.     
         *  during module init */  

        

  9.     
        int minor_version;

        

  10.     
        /** Index to this modules structures in config vectors.  default -1*/

        

  11.     
        int module_index;

        

  12.     
     

        

  13.     
        /** The name of the module’s C file */

        

  14.     
        const char *name;

        

  15.     
        /** The handle for the DSO.  Internal use only , default null*/

        

  16.     
        void *dynamic_load_handle;

        

  17.     
                                                                                                         

        

  18.     
        /** A pointer to the next module in the list                                                    

        

  19.     
         *  @defvar module_struct *next , default null*/                                                              

        

  20.     
        struct module_struct *next;                                                                      

        

  21.     
                                                                                                         

        

  22.     
        /** Magic Cookie to identify a module structure;  It’s mainly                                    

        

  23.     
         *  important for the DSO facility (see also mod_so).  */                                        

        

  24.     
        unsigned long magic;                                                                            

        

  25.     
                                                                                                         

        

  26.     
        /** Function to allow MPMs to re-write command line arguments.  This                            

        

  27.     
         *  hook is only available to MPMs.                                                              

        

  28.     
         *  @param The process that the server is running in.                                            

        

  29.     
                                                                   

        

  30.     
         */

        

  31.     
        void (*rewrite_args) (process_rec *process);

        

  32.     
        /** Function to allow all modules to create per directory configuration

        

  33.     
         *  structures.

        

  34.     
         *  @param p The pool to use for all allocations.

        

  35.     
         *  @param dir The directory currently being processed.

        

  36.     
         *  @return The per-directory structure created

        

  37.     
         */

        

  38.     
        void *(*create_dir_config) (apr_pool_t *p, char *dir);

        

  39.     
        /** Function to allow all modules to merge the per directory configuration

        

  40.     
         *  structures for two directories.

        

  41.     
         *  @param p The pool to use for all allocations.

        

  42.     
         *  @param base_conf The directory structure created for the parent directory.

        

  43.     
         *  @param new_conf The directory structure currently being processed.

        

  44.     
         *  @return The new per-directory structure created

        

  45.     
         */                                                                                              

        

  46.     
        void *(*merge_dir_config) (apr_pool_t *p, void *base_conf, void *new_conf);                      

        

  47.     
        /** Function to allow all modules to create per server configuration                            

        

  48.     
         *  structures.                                                                                  

        

  49.     
         *  @param p The pool to use for all allocations.                                                

        

  50.     
         *  @param s The server currently being processed.                                              

        

  51.     
         *  @return The per-server structure created                                                    

        

  52.     
         */                                                                                              

        

  53.     
        void *(*create_server_config) (apr_pool_t *p, server_rec *s);                                    

        

  54.     
        /** Function to allow all modules to merge the per server configuration                          

        

  55.     
         *  structures for two servers.                                                        

        

  56.     
         *  @param p The pool to use for all allocations.

        

  57.     
         *  @param base_conf The directory structure created for the parent directory.

        

  58.     
         *  @param new_conf The directory structure currently being processed.

        

  59.     
         *  @return The new per-directory structure created                                              

        

  60.     
         */                                                                                              

        

  61.     
        void *(*merge_server_config) (apr_pool_t *p, void *base_conf,                                    

        

  62.     
                                      void *new_conf);                                                  

        

  63.     
                                                                                                         

        

  64.     
        /** A command_rec table that describes all of the directives this module                        

        

  65.     
         * defines. */                                                                                  

        

  66.     
        const command_rec *cmds;                                                                        

        

  67.     
                                                                                                         

        

  68.     
        /** A hook to allow modules to hook other points in the request processing.                      

        

  69.     
         *  In this function, modules should call the ap_hook_*() functions to                          

        

  70.     
         *  register an interest in a specific step in processing the current                            

        

  71.     
         *  request.                                                                                    

        

  72.     
         *  @param p the pool to use for all allocations                                                

        

  73.     
         */                                                                                              

        

  74.     
        void (*register_hooks) (apr_pool_t *p);                                                          

        

  75.     
    };                                        

        

3. apache动态加载模块的过程。 动态加载模块时,严格检查模块名,包括httpd.conf中的module name(如: php5_module);对应的apache模块定义所在的文件的文件名(如: mod_php5.c)也是严格检查的。

httpd-2.2.16/modules/mappers/mod_so.c
  1.     
    static const char *load_module(cmd_parms *cmd, void *dummy,

        

  2.     
                                   const char *modname, const char *filename)

        

  3.     
    {

        

  4.     
        apr_dso_handle_t *modhandle;

        

  5.     
        apr_dso_handle_sym_t modsym;

        

  6.     
        module *modp;

        

  7.     
        const char *szModuleFile = ap_server_root_relative(cmd->pool, filename);

        

  8.     
        so_server_conf *sconf;

        

  9.     
        ap_module_symbol_t *modi;

        

  10.     
    //…

        

4. Apache conf 中的配置:
LoadModule php5_module        modules/libphp5.so

5. apr 动态加载模块并获取指定符号(如:php5_module):

apr-1.4.2/dso/beos/dso.c
  1.     
     

        

  2.     
    APR_DECLARE(apr_status_t) apr_dso_sym(apr_dso_handle_sym_t *ressym, apr_dso_handle_t *handle,

        

  3.     
                   const char *symname)

        

  4.     
    {

        

  5.     
        int err;

        

  6.     
     

        

  7.     
        if (symname == NULL)

        

  8.     
            return APR_ESYMNOTFOUND;

        

  9.     
     

        

  10.     
        err = get_image_symbol(handle->handle, symname, B_SYMBOL_TYPE_ANY,

        

  11.     
                 ressym);

        

  12.     
     

        

  13.     
        if(err != B_OK)

        

  14.     
            return APR_ESYMNOTFOUND;

        

  15.     
     

        

  16.     
        return APR_SUCCESS;

        

  17.     
    }

        

6. 一切准备完毕后:
   将模块写入已加载的模块列表中: ap_add_loaded_module(modp, cmd->pool);
   注册相关钩子函数:
     apr_pool_cleanup_register(cmd->pool, modi, unload_module, apr_pool_cleanup_null);
   Finally we need to run the configuration process for the module
     ap_single_module_configure(cmd->pool, cmd->server, modp); 

雁南飞


=========================================