⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 handlers.c

📁 这是有名的<<嵌入式系统TCP/IP应用层协议>>,M.Tim Jones 著
💻 C
字号:
/* *  Embedded CLI Command Handler Functions * *  ./software/ch9/emcli/emclid/handlers.c * *  mtj@cogitollc.com * */#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>extern int getArgCount( void );extern char *getArg( int );#define MAX_STACK	20static int stack[MAX_STACK];static int stackPtr = 0;int doHelp ( int fd ){  int i;  char *helptext[]={         "\nEmbedded CLI Help\n\n",         "\thelp            - This help\n",         "\tverbose         - Toggle verbose mode (more output)\n",         "\thexof <dec num> - Display the hex value of the dec arg\n",         "\tdecof <hex num> - Display the dec value of the hex arg\n",         "\tuptime          - Display the number of seconds of uptime\n",         "\twho             - Display peer IP address and port\n",         "\tlast            - Display time of last connection\n",         "\tpush <num>      - Push <num> on the stack\n",         "\tpop             - Pop and display the top of the stack\n",         "\tadd             - Pop top two elements, push the sum\n",         "\tsub             - Pop top two elements, push the difference\n",         "\tmul             - Pop top two elements, push the product\n",         "\tdiv             - Pop top two elements, push the quotient\n"        };  for (i = 0 ; i < (sizeof(helptext)/sizeof(char *)) ; i++) {    write(fd, helptext[i], strlen(helptext[i]));  }  return 0;}int convdec ( int fd ){  char response[81];  if (getArgCount() < 1) return -1;  sprintf(response, "0x%x\n", atoi(getArg(0)));  write(fd, response, strlen(response));  return 0;}int convhex ( int fd ){  char response[81];  int  value;  if (getArgCount() < 1) return -1;  sscanf(getArg(0), "%x", &value);  sprintf(response, "%d\n", value);  write(fd, response, strlen(response));  return 0;}int getpeer ( int fd ){  char response[81];  struct sockaddr_in sockinfo;  int  len;  if (getpeername( fd, (struct sockaddr *)&sockinfo, &len ) < 0) {    return -1;  } else {    sprintf(response, "Peer address %s port %d\n",              inet_ntoa(sockinfo.sin_addr), ntohs(sockinfo.sin_port) );    write(fd, response, strlen(response));  }    return 0;}int push ( int fd ){  if ((getArgCount() != 1) || (stackPtr == MAX_STACK)) return -1;  stack[stackPtr++] = atoi(getArg(0));  return 0;}int pop ( int fd ){  char response[81];  if (stackPtr == 0) return -1;  sprintf(response, "%d\n", stack[--stackPtr]);  write(fd, response, strlen(response));  return 0;}int mathop ( int fd, char opcode ){  int  value1, value2, result;  if (stackPtr < 2) return -1;  value2 = stack[--stackPtr];  value1 = stack[--stackPtr];  if (value2 == 0) return -1;  switch(opcode) {    case 'a':      result = value1 + value2;      break;    case 's':      result = value1 - value2;      break;    case 'm':      result = value1 * value2;      break;    case 'd':      result = value1 / value2;      break;  }  stack[stackPtr++] = result;  return 0;}/* *  Copyright (c) 2002 Charles River Media.  All rights reserved. *  *  Redistribution and use in source and binary forms, with or  *  without modification, is hereby granted without fee provided  *  that the following conditions are met: *  *    1.  Redistributions of source code must retain the above  *        copyright notice, this list of conditions and the  *        following disclaimer. *    2.  Redistributions in binary form must reproduce the above *        copyright notice, this list of conditions and the  *        following disclaimer in the documentation and/or other  *        materials provided with the distribution. *    3.  Neither the name of Charles River Media nor the names of  *        its contributors may be used to endorse or promote products  *        derived from this software without specific prior  *        written permission. *  * THIS SOFTWARE IS PROVIDED BY CHARLES RIVER MEDIA AND CONTRIBUTERS  * 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CHARLES * RIVER MEDIA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,  * INCIDENTAL, SPECIAL, EXEMPLARAY, OR CONSEQUENTIAL DAMAGES (INCLUDING,  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  * POSSIBILITY OF SUCH DAMAGE. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -