Chinaunix首页 | 论坛 | 博客
  • 博客访问: 624364
  • 博文数量: 144
  • 博客积分: 5037
  • 博客等级: 大校
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-30 21:49
文章存档

2010年(16)

2009年(128)

分类: LINUX

2009-06-23 09:45:19

//my_alias.h

/*********************************************************************************
  * 文件名称:alias.h
  * 功能描述:提供alias unalias功能
  * 维护记录:2009-3-25     V1.01    by skyily
 *********************************************************************************/


#ifndef __MY_ALIAS_H_
#define __MY_ALIAS_H_

typedef struct my_alias
{
    char by_name[100];
    char ture_name[200];
    struct my_alias *next;
}MY_ALIAS;

extern MY_ALIAS *alias_head;

extern void alias_init();
extern char *alias_check(char *buf, int c_size);    //

extern MY_ALIAS *alias_cmd(int c_size);
extern void alias_display();
extern void unalias_cmd();

#endif

my_alias.c

/*********************************************************************************
  * 文件名称:my_alias.c
  * 功能描述:alias and unalias cmd
  * 维护记录:2009-3-25 V1.01   by skyily
 *********************************************************************************/

#include "my_alias.h"
#include "main.h"

MY_ALIAS *alias_head;

/*********************************************************************************
 * 语法格式: void alias_init(void)
 * 实现功能: initialize alias environment
 * 入口参数: 无
 * 出口参数: 无
 *********************************************************************************/

void alias_init()
{
 FILE *fp;
 char tmp_buf[1024] = {""};
 char tmp_by[1024] = {""};
 char tmp_ture[1024] = {""};
 MY_ALIAS *node = NULL;
 MY_ALIAS *bp = NULL;
 int i = 0;
 int j = 0;
 
 if((fp = fopen("/home/skyily/temp/shellprograme/.aliasrc", "r")) == NULL)
 {
  perror("open .aliasrc error!");
  return ;
 }
 while(fgets(tmp_buf, 1024, fp) != NULL)
 {
  if(strncmp(tmp_buf, "alias", 5) == 0)
  {
   node = (MY_ALIAS *)malloc(sizeof(MY_ALIAS));
   
   i = 6;
   j = 0;
   while(tmp_buf[i] != '=')
   {
    tmp_by[j++] = tmp_buf[i++];
   }
   i += 2;
   j = 0;
   while(tmp_buf[i] != '\'')
   {
    tmp_ture[j++] = tmp_buf[i++];
   }
  // sscanf(tmp_buf, "alias %s='%s'\n", node->by_name, node->ture_name);

   strcpy(node->by_name, tmp_by);
   strcpy(node->ture_name, tmp_ture);
  
  // printf("%s\n", tmp_by);

  // printf("%s\n", tmp_ture);

   memset(tmp_by, 0, 1024);
   memset(tmp_ture, 0, 1024);
   
   if(alias_head == NULL)
    bp = alias_head = node;
   else
    bp->next = node;
   bp = node;
   bp->next = NULL;
   node = NULL;
  }
  memset(tmp_buf, 0, 1024);
 }
// close(fp);

 return ;
}

/*********************************************************************************
 * 语法格式: char *alias_check(char *buf, int c_size)
 * 实现功能: check alias in the alias environment
 * 入口参数: buf is cmd_buf, operate cmd_buf; c_size is size of cmd burch
 * 出口参数: alias_head
 *********************************************************************************/

char *alias_check(char *buf, int c_size)
{
 MY_ALIAS *p = alias_head;
 char tmp_buf[CMD_SIZE] = {""};
 int i = 0;
 
 //char *tmp_ar[CMD_BUNCH_SIZE];

 
 if(argv[0] != NULL)
 {
  while(p != NULL)
  {
   if(strcmp(argv[0], p->by_name) == 0)
   {
   // printf("%s\n", p->ture_name);   //test

   // strcpy(argv[0], p->ture_name); //wrong... 

    argv[0] = NULL;
    strcpy(tmp_buf, p->ture_name);
   // printf("%s\n", argv[0]);      //test

    for(i = 1; i < c_size; i++)
    {
   // printf("%s\n", argv[i]);     //test

     strcat(tmp_buf, " ");
     strcat(tmp_buf, argv[i]);
     argv[i] = NULL;
    }
   // printf("%s\n", tmp_buf);     //test

    strcpy(buf, tmp_buf); //

    return buf;
   // break;

   }
   p = p->next;
  }
 }
 return NULL;
}

/*********************************************************************************
 * 语法格式: void alias_display(void)
 * 实现功能: display alias environment
 * 入口参数: 无
 * 出口参数: 无
 *********************************************************************************/

void alias_display()
{
 MY_ALIAS *p = alias_head;
 char tmp_buf[1024] = {""};
 
 while(p != NULL)
 {
  sprintf(tmp_buf, "alias %s='%s'", p->by_name, p->ture_name);
  printf("%s\n", tmp_buf);
  memset(tmp_buf, 0, 1024);
  p = p->next;
 }
 return ;
}

/*********************************************************************************
 * 语法格式: MY_ALIAS *alias_cmd(int c_size)
 * 实现功能: implement alias cmd
 * 入口参数: c_size is size of cmd burch
 * 出口参数: alias_head
 *********************************************************************************/

MY_ALIAS *alias_cmd(int c_size)
{
 MY_ALIAS *tmp = alias_head;
 MY_ALIAS *p = NULL;
 MY_ALIAS *node = NULL;
 char tmp_buf[1024] = {""};
 char tmp_by[1024] = {""};
 char tmp_ture[1024] = {""};
 char end[CMD_SIZE] = {""};
 int signal = 0;
 int i = 0;
 int j = 0;
 char *ptr;
 
 if(argv[1] != NULL)
 {
  strcpy(tmp_buf, argv[1]);
  if(c_size > 2)
  {
   for(i = 2; i < c_size; i++)
   {
    strcat(tmp_buf, " ");
    strcat(tmp_buf, argv[i]);
   }
  }
  
  i = 0;
  if((ptr = strchr(tmp_buf, '=')) && (*ptr != tmp_buf[0]))
  {
   
   while(tmp_buf[i] != '=')
   {
    tmp_by[j++] = tmp_buf[i++];
   }
   i++;
   if(tmp_buf[i] != '\0')
   {
    j = 0;
    if(tmp_buf[i] != '\'' ) //|| tmp_buf[i] == '\"')

    {
     printf("alias %s format is not right\n", tmp_buf);
     printf("example is ll=\'ls -l\'\n");
     return alias_head;
    }
    i++;
    while(tmp_buf[i] != '\'')
    {
     if(tmp_buf[i] == '\0')
     {
      while(1)
      {
       printf("> ");
       fflush(stdout);
       fgets(end, CMD_SIZE, stdin);
       if((end != NULL) && (strcmp(end, "\'\n") == 0))
       {
        break;
       }
       memset(end, 0, CMD_SIZE);
      }
      break;
     }
     tmp_ture[j++] = tmp_buf[i++];
    }
  
    while(tmp != NULL)
    {
     p = tmp;
     if(strcmp(tmp_by, p->by_name) == 0)
     {
      signal = 1;
      break;
     }
     
     tmp = tmp->next;
    }
    if(signal == 1)
    {
     memset(p->ture_name, 0, 200);
     strcpy(p->ture_name, tmp_ture);
     //printf("%s\n", tmp_ture);

    }
    else
    {
     node = (MY_ALIAS *)malloc(sizeof(MY_ALIAS));
     node->next = alias_head;
     alias_head = node;
     strcpy(node->by_name, tmp_by);
     strcpy(node->ture_name, tmp_ture);
     node = NULL;
    }
   }
   else
   {
    printf("input error\n");
   }
  }
  else
  {
   printf("alias %s not found\n", tmp_buf);
  }
 }
 return alias_head;
}

/*********************************************************************************
 * 语法格式: void unalias_cmd(void)
 * 实现功能: delelt alias of environment
 * 入口参数: 无
 * 出口参数: 无
 *********************************************************************************/

void unalias_cmd()
{
 MY_ALIAS *p = alias_head;
 MY_ALIAS *tmp = NULL;
  
 if(argv[1] != NULL)
 {
  if(strcmp(argv[1], "-a") == 0)
  {
   while(alias_head != NULL)
   {
    alias_head = alias_head->next;
    free(p);
   // p = NULL;

    p = alias_head;
   }
  }
  else
  {
   //while(p != NULL)

   {
    while((p != NULL) && (strcmp(argv[1], p->by_name) != 0))
    {
     tmp = p;
     p = p->next;
    }
    if((p != NULL) && (strcmp(argv[1], p->by_name) == 0))
    {
     if(p == alias_head)
     {
      alias_head = p->next;
      free(p);
      p = NULL;
     }
     else
     {
      tmp->next = p->next;
      free(p);
      p = NULL;
     }
    }
    else
    {
     printf("unalias %s not found\n", argv[1]);
    }
   }
  }
 }
 return ;
}
基本已经实现,希望大家多多指点

[root@localhost shellprogram]# ./shell

[root@localhost shellprogram]$ alias

 alias cp='cp -i'
 alias ll='ls -l --color=tty'
 alias ls='ls --color=tty'
 alias mv='mv -i'
 alias rm='rm -i'
 alias vi='vim'

[root@localhost shellprogram]$ alias a='ls'

[root@localhost shellprogram]$ alias

 alias a='ls'

 alias cp='cp -i'
 alias ll='ls -l --color=tty'
 alias ls='ls --color=tty'
 alias mv='mv -i'
 alias rm='rm -i'
 alias vi='vim'

[root@localhost shellprogram]$ a

add_print_history.c  cmd_jobs.o          format_cmd.c    keypress.c      my_alias.c      my_redirect.bak.c  test
add_print_history.o  ctrl_z_c_q.c        format_cmd.o    keypress.h      my_alias.c.bak  my_redirect.c      test1
a.out                ctrl_z_c_q.o        get_cmd.c       keypress.h.bak  my_alias.h      my_redirect.o      test.c
cmd_differ.c         cut_cmd.c           get_cmd.o       keypress.o      my_alias.h.bak  mysh_profile
cmd_differ.o         cut_cmd.o           history.bak     main.c          my_alias.o      shell
cmd_found.c          cut_dir.c           history_init.c  main.h          my_cmd.c        shell.bak
cmd_found.o          cut_dir.o           history_init.o  main.h.bak      my_cmd.o        shell_bak1.c
cmd_jobs.c           environment_init.c  history_save.c  main.o          my_pipe.c       shell.bak2
cmd_jobs.c.bak       environment_init.o  history_save.o  Makefile        my_pipe.o       shell_bak2.c

[root@localhost shellprogram]$ unalias a

[root@localhost shellprogram]$ alias 

 alias cp='cp -i'
 alias ll='ls -l --color=tty'
 alias ls='ls --color=tty'
 alias mv='mv -i'
 alias rm='rm -i'
 alias vi='vim'

[root@localhost shellprogram]$ unalias -a  //全部去掉别名

[root@localhost shellprogram]$ alias       //什么都没有了

[root@localhost shellprogram]$ exit

byebey!

[root@localhost shellprogram]#              

阅读(2578) | 评论(1) | 转发(0) |
0

上一篇:有关likely和unlikely

下一篇:NAND Flash的寻址

给主人留下些什么吧!~~

kelciej2016-06-18 20:25:54

亲,alias的具体怎么在main函数里面写??