?? parserc.c
字號:
/* -------------------------------------------------------------------- *//* SMS Client, send messages to mobile phones and pagers *//* *//* parserc.c *//* *//* Copyright (C) 1997,1998 Angelo Masci *//* *//* This library is free software; you can redistribute it and/or *//* modify it under the terms of the GNU Library General Public *//* License as published by the Free Software Foundation; either *//* version 2 of the License, or (at your option) any later version. *//* *//* This library is distributed in the hope that it will be useful, *//* but WITHOUT ANY WARRANTY; without even the implied warranty of *//* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *//* Library General Public License for more details. *//* *//* You should have received a copy of the GNU Library General Public *//* License along with this library; if not, write to the Free *//* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *//* You can contact the author at this e-mail address: *//* *//* angelo@styx.demon.co.uk *//* *//* -------------------------------------------------------------------- *//* $Id: parserc.c,v 5.1 1998/02/01 07:10:39 root Exp root $ -------------------------------------------------------------------- */#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <sys/types.h>#include <pwd.h>#include <unistd.h>#include <stddef.h>#if defined(NEXT)#include <sys/dir.h>#include <sys/dirent.h>#define NAME_MAX 255#define PATH_MAX 1024#else#include <dirent.h>#endif#include "sms_error.h"#include "logfile.h"#include "parserc.h"#include "sms_list.h"#include "sms_resource.h"#include "driver/driver.h"#include "token.h"#include "common.h"/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */#define MAXRCLINELEN 80#define DELIMETER '='#if !defined(MLOCALSMSRC)#error "MLOCALSMSRC undefined"#else#define LOCALSMSRC MLOCALSMSRC#endif#if !defined(MGLOBALSMSRC)#error "MGLOBALSMSRC undefined"#else#define GLOBALSMSRC MGLOBALSMSRC#endif#if !defined(MSERVICEDIR)#error "MSERVICEDIR undefined"#else#define SERVICEDIR MSERVICEDIR#endif/* -------------------------------------------------------------------- */static RESOURCE *SMS_services_list = NULL;SMS_list *search_list;/* -------------------------------------------------------------------- */static FILE *SMS_open_global_smsrc(void);static FILE *SMS_open_local_smsrc(void);static void SMS_close_smsrc(FILE *fp);static void read_services(void);static SMS_list *expandnumber(FILE *fp[2], char *id, char *str, char *default_service);/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static FILE *SMS_open_global_smsrc(void){ lprintf(LOG_VERBOSE, "Opening Global Addressbook File: %s\n", GLOBALSMSRC); return fopen(GLOBALSMSRC, "r");}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */#if defined(SOLARIS)#define NAME_MAX FILENAME_MAX#endif#if defined(AIX)#define NAME_MAX 512#endifstatic FILE *SMS_open_local_smsrc(void){ struct passwd *pentry; char filename[PATH_MAX + NAME_MAX +1]; pentry = getpwuid(getuid()); sms_strcpy(filename, pentry->pw_dir); sms_strcat(filename, "/"); sms_strcat(filename, LOCALSMSRC); lprintf(LOG_VERBOSE, "Opening Local Addressbook File: %s\n", filename); return fopen(filename, "r");}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static void SMS_close_smsrc(FILE *fp){ if (fp != NULL) { fclose(fp); }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int is_name(char *name){ char *ptr; ptr = name; if (isdigit(*ptr)) /* First character NUMERIC */ { /* This is NOT a NAME */ return FALSE; } while (*ptr != '\0') { if (*ptr == ':') /* Contains ':' this */ { /* this is NOT a NAME */ /* */ /* SERVICE:NUMBER */ return FALSE; } ptr++; } return TRUE;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *SMS_get_smsrc_value(FILE *fp, char *name){ char line[MAXRCLINELEN], SMS_name[MAXRCLINELEN], token[MAXRCLINELEN], *SMS_value, *src; int type, line_count; if (fp == NULL) { return NULL; } rewind(fp); SMS_value = (char *)malloc(sizeof(char) * MAXRCLINELEN); if (SMS_value == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(EMALLOC); } line_count = 0; while (get_line(line, MAXRCLINELEN, &line_count, fp) != NULL) { src = line; type = get_token(SMS_name, MAXRCLINELEN, src, &src); if (type != STRING_TOKEN) { if (type == COMMENT_TOKEN) { continue; } lprintf(LOG_VERBOSE, "Syntax Error: Name expected at line %d\n", line_count); return NULL; } type = get_token(token, MAXRCLINELEN, src, &src); if (type != ASSIGNMENT_TOKEN) { lprintf(LOG_VERBOSE, "Syntax Error: Assignment expected at line %d\n", line_count); return NULL; } type = get_token(SMS_value, MAXRCLINELEN, src, &src); if ((type != STRING_TOKEN) && (type != QUOTED_STRING_TOKEN) && (type != SINGLE_QUOTED_STRING_TOKEN) && (type != NULL_TOKEN)) { lprintf(LOG_VERBOSE, "Syntax Error: String expected at line %d\n", line_count); return NULL; } type = get_token(token, MAXRCLINELEN, src, &src); if ((type != NULL_TOKEN) && (type != COMMENT_TOKEN)) { lprintf(LOG_VERBOSE, "Syntax Error: EOL or comment expected at line %d\n", line_count); return NULL; } if (strcmp(name, SMS_name) == 0) { lprintf(LOG_VERBOSE, "Name Found: %s = %s\n", SMS_name, SMS_value); return SMS_value; } } lprintf(LOG_VERBOSE, "Name NOT Found: %s\n", name); free(SMS_value); return NULL;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */char *SMS_dual_get_smsrc_value(FILE **fp, char *name){ char *ptr; ptr = SMS_get_smsrc_value(fp[0], name); if (ptr != NULL) { return ptr; } ptr = SMS_get_smsrc_value(fp[1], name); if (ptr != NULL) { return ptr; } return NULL;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int SMS_getnamevalue_numeric(FILE *fp, char *name, long *value){ char *str, *ptr; str = SMS_get_smsrc_value(fp, name); if (str == NULL) { return -1; } *value = strtol(str, &ptr, 10); if (ptr == str) { return -1; } return 0;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */int SMS_open_dual_getnamevalue_numeric(char *name, long *value){ FILE *fp[2]; SMS_dual_openrc(fp); if (SMS_getnamevalue_numeric(fp[0], name, value) == 0) { SMS_dual_closerc(fp); return 0; } if (SMS_getnamevalue_numeric(fp[1], name, value) == 0) { SMS_dual_closerc(fp); return 0; } SMS_dual_closerc(fp); return -1;}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void SMS_dual_closerc(FILE **fp){ SMS_close_smsrc(fp[0]); SMS_close_smsrc(fp[1]);}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */void SMS_dual_openrc(FILE **fp){ fp[0] = SMS_open_local_smsrc(); if (fp[0] == NULL) { lprintf(LOG_VERBOSE, "Failed to open local smsrc file\n"); } fp[1] = SMS_open_global_smsrc(); if (fp[1] == NULL) { lprintf(LOG_WARNING, "Failed to open global smsrc file\n"); }}/* -------------------------------------------------------------------- *//* -------------------------------------------------------------------- */static char *strdup_service(char *str){ char *ptr, *dst, *service; service = (char *)malloc(sizeof(char) * (sms_strlen(str) +1)); if (service == NULL) { lprintf(LOG_ERROR, "Allocating memory\n"); exit(EMALLOC); } dst = service; ptr = str; while(*ptr != '\0') { if (*ptr == ':') { *dst = '\0'; break; } else { *dst = *ptr; } dst++; ptr++; } if (*ptr == '\0') { *service = '\0'; } return service; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -