📄 smpd_get_opt.c
字号:
/* -*- Mode: C; c-basic-offset:4 ; -*- * * Copyright (C) 1997 University of Chicago. * See COPYRIGHT notice in top-level directory. */#include "smpd.h"#include <stdio.h>#include <stdlib.h>#ifdef HAVE_STRING_H#include <string.h>#endif#ifdef HAVE_MATH_H#include <math.h>#endif#ifdef HAVE_CTYPE_H#include <ctype.h>#endif#undef FCNAME#define FCNAME "first_token"static const char * first_token(const char *str){ smpd_enter_fn(FCNAME); if (str == NULL) { smpd_exit_fn(FCNAME); return NULL; } while (isspace(*str)) str++; if (*str == '\0') { smpd_exit_fn(FCNAME); return NULL; } smpd_exit_fn(FCNAME); return str;}#undef FCNAME#define FCNAME "next_token"static const char * next_token(const char *str){ const char *result; smpd_enter_fn(FCNAME); if (str == NULL) { smpd_exit_fn(FCNAME); return NULL; } str = first_token(str); if (str == NULL) { smpd_exit_fn(FCNAME); return NULL; } if (*str == SMPD_QUOTE_CHAR) { /* move over string */ str++; /* move over the first quote */ if (*str == '\0') { smpd_exit_fn(FCNAME); return NULL; } while (*str != SMPD_QUOTE_CHAR) { /* move until the last quote, ignoring escaped quotes */ if (*str == SMPD_ESCAPE_CHAR) { str++; if (*str == SMPD_QUOTE_CHAR) str++; } else { str++; } if (*str == '\0') { smpd_exit_fn(FCNAME); return NULL; } } str++; /* move over the last quote */ } else { if (*str == SMPD_DELIM_CHAR) { /* move over the DELIM token */ str++; } else { /* move over literal */ while (!isspace(*str) && *str != SMPD_DELIM_CHAR && *str != '\0') str++; } } result = first_token(str); smpd_exit_fn(FCNAME); return result;}#undef FCNAME#define FCNAME "compare_token"static int compare_token(const char *token, const char *str){ smpd_enter_fn(FCNAME); if (token == NULL || str == NULL) { smpd_exit_fn(FCNAME); return -1; } if (*token == SMPD_QUOTE_CHAR) { /* compare quoted strings */ token++; /* move over the first quote */ /* compare characters until reaching the end of the string or the end quote character */ for (;;) { if (*token == SMPD_ESCAPE_CHAR) { if (*(token+1) == SMPD_QUOTE_CHAR) { /* move over the escape character if the next character is a quote character */ token++; } if (*token != *str) break; } else { if (*token != *str || *token == SMPD_QUOTE_CHAR) break; } if (*str == '\0') break; token++; str++; } if (*str == '\0' && *token == SMPD_QUOTE_CHAR) { smpd_exit_fn(FCNAME); return 0; } if (*token == SMPD_QUOTE_CHAR) { smpd_exit_fn(FCNAME); return 1; } if (*str < *token) { smpd_exit_fn(FCNAME); return -1; } smpd_exit_fn(FCNAME); return 1; } /* compare DELIM token */ if (*token == SMPD_DELIM_CHAR) { if (*str == SMPD_DELIM_CHAR) { str++; if (*str == '\0') { smpd_exit_fn(FCNAME); return 0; } smpd_exit_fn(FCNAME); return 1; } if (*token < *str) { smpd_exit_fn(FCNAME); return -1; } smpd_exit_fn(FCNAME); return 1; } /* compare literals */ while (*token == *str && *str != '\0' && *token != SMPD_DELIM_CHAR && !isspace(*token)) { token++; str++; } if ( (*str == '\0') && (*token == SMPD_DELIM_CHAR || isspace(*token) || *token == '\0') ) { smpd_exit_fn(FCNAME); return 0; } if (*token == SMPD_DELIM_CHAR || isspace(*token) || *token < *str) { smpd_exit_fn(FCNAME); return -1; } smpd_exit_fn(FCNAME); return 1;}#undef FCNAME#define FCNAME "token_copy"static void token_copy(const char *token, char *str, int maxlen){ smpd_enter_fn(FCNAME); /* check parameters */ if (token == NULL || str == NULL) { smpd_exit_fn(FCNAME); return; } /* check special buffer lengths */ if (maxlen < 1) { smpd_exit_fn(FCNAME); return; } if (maxlen == 1) { *str = '\0'; smpd_exit_fn(FCNAME); return; } /* cosy up to the token */ token = first_token(token); if (token == NULL) { smpd_exit_fn(FCNAME); return; } if (*token == SMPD_DELIM_CHAR) { /* copy the special deliminator token */ str[0] = SMPD_DELIM_CHAR; str[1] = '\0'; smpd_exit_fn(FCNAME); return; } if (*token == SMPD_QUOTE_CHAR) { /* quoted copy */ token++; /* move over the first quote */ do { if (*token == SMPD_ESCAPE_CHAR) { if (*(token+1) == SMPD_QUOTE_CHAR) token++; *str = *token; } else { if (*token == SMPD_QUOTE_CHAR) { *str = '\0'; smpd_exit_fn(FCNAME); return; } *str = *token; } str++; token++; maxlen--; } while (maxlen); /* we've run out of destination characters so back up and null terminate the string */ str--; *str = '\0'; smpd_exit_fn(FCNAME); return; } /* literal copy */ while (*token != SMPD_DELIM_CHAR && !isspace(*token) && *token != '\0' && maxlen) { *str = *token; str++; token++; maxlen--; } if (maxlen) *str = '\0'; smpd_exit_fn(FCNAME);}#undef FCNAME#define FCNAME "token_hide"static void token_hide(char *token){ smpd_enter_fn(FCNAME); /* check parameters */ if (token == NULL) { smpd_exit_fn(FCNAME); return; } /* cosy up to the token */ token = (char*)first_token(token); if (token == NULL) { smpd_exit_fn(FCNAME); return; } if (*token == SMPD_DELIM_CHAR) { *token = SMPD_HIDE_CHAR; smpd_exit_fn(FCNAME); return; } /* quoted */ if (*token == SMPD_QUOTE_CHAR) { *token = SMPD_HIDE_CHAR; token++; /* move over the first quote */ while (*token != '\0') { if (*token == SMPD_ESCAPE_CHAR) { if (*(token+1) == SMPD_QUOTE_CHAR) { *token = SMPD_HIDE_CHAR; token++; } *token = SMPD_HIDE_CHAR; } else { if (*token == SMPD_QUOTE_CHAR) { *token = SMPD_HIDE_CHAR; smpd_exit_fn(FCNAME); return; } *token = SMPD_HIDE_CHAR; } token++; } smpd_exit_fn(FCNAME); return; } /* literal */ while (*token != SMPD_DELIM_CHAR && !isspace(*token) && *token != '\0') { *token = SMPD_HIDE_CHAR; token++; } smpd_exit_fn(FCNAME);}#undef FCNAME#define FCNAME "smpd_get_string_arg"int smpd_get_string_arg(const char *str, const char *flag, char *val, int maxlen){ smpd_enter_fn(FCNAME); if (maxlen < 1) { smpd_exit_fn(FCNAME); return SMPD_FALSE; } /* line up with the first token */ str = first_token(str); if (str == NULL) { smpd_exit_fn(FCNAME); return SMPD_FALSE; } /* This loop will match the first instance of "flag = value" in the string. */ do { if (compare_token(str, flag) == 0) { str = next_token(str); if (compare_token(str, SMPD_DELIM_STR) == 0) { str = next_token(str); if (str == NULL) { smpd_exit_fn(FCNAME); return SMPD_FALSE; } token_copy(str, val, maxlen); smpd_exit_fn(FCNAME); return SMPD_TRUE; } } else { str = next_token(str); } } while (str); smpd_exit_fn(FCNAME); return SMPD_FALSE;}#undef FCNAME#define FCNAME "smpd_hide_string_arg"int smpd_hide_string_arg(char *str, const char *flag){ smpd_enter_fn(FCNAME); /* line up with the first token */ str = (char*)first_token(str); if (str == NULL) { smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } do { if (compare_token(str, flag) == 0) { str = (char*)next_token(str); if (compare_token(str, SMPD_DELIM_STR) == 0) { str = (char*)next_token(str); if (str == NULL) { smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } token_hide(str); smpd_exit_fn(FCNAME); return SMPD_SUCCESS; } } else { str = (char*)next_token(str); } } while (str); smpd_exit_fn(FCNAME); return SMPD_SUCCESS;}#undef FCNAME#define FCNAME "smpd_get_int_arg"int smpd_get_int_arg(const char *str, const char *flag, int *val_ptr){ char int_str[12]; smpd_enter_fn(FCNAME); if (smpd_get_string_arg(str, flag, int_str, 12)) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -