📄 parser.c
字号:
/* * Embedded CLI Command Parser (Tokenizer) * * ./software/ch9/emcli/emclid/parser.c * * mtj@cogitollc.com * */#include <stdio.h>#define MAX_ARGS 20static char vector[MAX_ARGS];static char *ibuf;static int numArgs;/* * parseCommand() * * Identifies and logs the independent arguments of the command * line and puts their index into the vector indexed by the * particular arguments. * * Returns the number of arguments parsed for the particular * command. If the return is -1, then an error occured, otherwise * a return of 0 represents no arguments, > 0 is the number of args. * * NOTE: This module keeps user-local information local to this * module. Therefore, the user should not modify the command * buffer after it has been passed to parseCommand. * */int parseCommand( char *buf ) { int i=0; int len = strlen(buf); ibuf = buf; numArgs = 0; /* First, skip the command */ while ((i < len) && (buf[i] != ' ')) i++; buf[i++] = 0; while ((i < len) && (buf[i] == ' ')) i++; if (i >= len) return 0; while (1) { if (numArgs >= MAX_ARGS) return -1; vector[numArgs] = i; if (i >= len) break; while ((i < len) && (buf[i] != ' ')) i++; buf[i++] = 0; while ((i < len) && (buf[i] == ' ')) i++; numArgs++; } return numArgs;}char *getArg( int arg ){ if (ibuf) { return (char *)(&ibuf[vector[arg]]); } else { return NULL; }}int getArgCount ( void ){ return numArgs;}/* * 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 + -