parserc.c
上传用户:mei_mei897
上传日期:2007-01-05
资源大小:82k
文件大小:16k
- /* -------------------------------------------------------------------- */
- /* 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: %sn", GLOBALSMSRC);
- return fopen(GLOBALSMSRC, "r");
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- #if defined(SOLARIS)
- #define NAME_MAX FILENAME_MAX
- #endif
- #if defined(AIX)
- #define NAME_MAX 512
- #endif
- static 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: %sn", 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 != ' ')
- {
- 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 memoryn");
- 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 %dn", line_count);
- return NULL;
- }
- type = get_token(token, MAXRCLINELEN, src, &src);
- if (type != ASSIGNMENT_TOKEN)
- {
- lprintf(LOG_VERBOSE, "Syntax Error: Assignment expected at line %dn", 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 %dn", 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 %dn", line_count);
- return NULL;
- }
- if (strcmp(name, SMS_name) == 0)
- {
- lprintf(LOG_VERBOSE, "Name Found: %s = %sn", SMS_name, SMS_value);
- return SMS_value;
- }
- }
- lprintf(LOG_VERBOSE, "Name NOT Found: %sn", 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 filen");
- }
- fp[1] = SMS_open_global_smsrc();
- if (fp[1] == NULL)
- { lprintf(LOG_WARNING, "Failed to open global smsrc filen");
- }
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- 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 memoryn");
- exit(EMALLOC);
- }
-
- dst = service;
- ptr = str;
- while(*ptr != ' ')
- {
- if (*ptr == ':')
- { *dst = ' ';
- break;
- }
- else
- { *dst = *ptr;
- }
-
- dst++;
- ptr++;
- }
- if (*ptr == ' ')
- {
- *service = ' ';
- }
- return service;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static char *strdup_number(char *str)
- {
- char *ptr,
- *dst,
- *number;
-
- number = (char *)malloc(sizeof(char) * (sms_strlen(str) +1));
- if (number == NULL)
- {
- lprintf(LOG_ERROR, "Allocating memoryn");
- exit(EMALLOC);
- }
-
- dst = number;
- ptr = str;
- while(*ptr != ' ')
- {
- if (*ptr == ':')
- { dst = number;
- }
- else
- { *dst = *ptr;
- dst++;
- }
-
- ptr++;
- }
- *dst = ' ';
-
- return number;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- SMS_list *add_number(SMS_list *list, char *id, char *number, char *default_service)
- {
- char *mnumber,
- *mservice;
- mnumber = strdup_number(number);
- mservice = strdup_service(number);
- if (strcmp(mservice, "") == 0)
- {
- if (mnumber[0] != '_')
- {
- list = add_item(list, id, default_service, mnumber);
- }
- else
- {
- list = add_item(list, id, default_service, &mnumber[1]);
- }
- }
- else
- {
- if (mnumber[0] != '_')
- {
- list = add_item(list, id, mservice, mnumber);
- }
- else
- {
- list = add_item(list, id, default_service, &mnumber[1]);
- }
- }
- free(mnumber);
- free(mservice);
- return list;
- }
-
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static SMS_list *expandnumber(FILE *fp[2], char *id, char *str, char *default_service)
- {
- char *src,
- *dst,
- *number,
- *value;
-
- int len, num;
- SMS_list
- *list,
- *sub_list;
-
-
- if (str == NULL)
- { return NULL;
- }
-
- len = sms_strlen(str);
-
- number = (char *)malloc(sizeof(char) * (len +1));
- if (number == NULL)
- {
- lprintf(LOG_ERROR, "Allocating memoryn");
- exit(EMALLOC);
- }
- list = NULL;
- dst = number;
- src = str;
- num = 1;
- while (*src != ' ')
- {
- if (*src == ',')
- {
- *dst = ' ';
- if (is_name(number))
- {
- /* Expand number */
- if (find_number(search_list, number) != NULL)
- {
- lprintf(LOG_WARNING, "Circular list detectedn");
- }
- else
- { search_list = add_item(search_list, "", "", number);
-
- value = SMS_dual_get_smsrc_value(fp, number);
- if (value != NULL)
- {
- sub_list = expandnumber(fp, number, value, default_service);
- list = insert_list(list, sub_list);
- }
- else
- { list = add_number(list, id, number, default_service);
- }
- }
- }
- else
- { list = add_number(list, id, number, default_service);
- }
- dst = number;
- num++;
- }
- else
- { *dst = *src;
- dst++;
- }
- src++;
- }
- *dst = ' ';
- if (is_name(number))
- {
- /* Expand number */
- if (find_number(search_list, number) != NULL)
- {
- lprintf(LOG_WARNING, "Circular list detectedn");
- }
- else
- { search_list = add_item(search_list, "", "", number);
-
- value = SMS_dual_get_smsrc_value(fp, number);
- if (value != NULL)
- {
- sub_list = expandnumber(fp, number, value, default_service);
- list = insert_list(list, sub_list);
- }
- else
- { list = add_number(list, id, number, default_service);
- }
- }
- }
- else
- { list = add_number(list, id, number, default_service);
- }
- free(number);
-
- return list;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- SMS_list *SMS_expandnumber(FILE *fp[2], char *id, char *number, char *default_service)
- {
- SMS_list
- *list;
-
- search_list = NULL;
- list = expandnumber(fp, id, number, default_service);
- free_list(search_list);
- return list;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- static void read_services(void)
- {
- char fname[1024],
- dname[1024];
- int num,
- i;
- DIR *dp;
- struct dirent
- *ep;
- sms_strcpy(dname, SERVICEDIR);
- sms_strcat(dname, "/services");
- dp = opendir (dname);
- if (dp == NULL)
- { lprintf(LOG_ERROR, "Opening Services directory %sn", dname);
- exit(-1);
- }
- num = 0;
- while ((ep = readdir(dp)))
- {
- if (ep->d_name[0] == '.')
- { continue;
- }
- num++;
- }
- SMS_services_list = (RESOURCE *)malloc(sizeof(RESOURCE) * (num +1));
- if (SMS_services_list == NULL)
- { lprintf(LOG_ERROR, "Allocating memory for SMS_services_listn");
- exit(-1);
- }
- i = 0;
- rewinddir(dp);
- while ((ep = readdir(dp)))
- {
- if (ep->d_name[0] == '.')
- { continue;
- }
- SMS_services_list[i].type = RESOURCE_STRING;
- SMS_services_list[i].name = malloc(sms_strlen("SMS_protocol_") + sms_strlen(ep->d_name) +1);
- if (SMS_services_list[i].name == NULL)
- { lprintf(LOG_ERROR, "Allocating memory for SMS_services_list[%d].namen", i);
- exit(-1);
- }
- sms_strcpy(SMS_services_list[i].name, "SMS_protocol_");
- sms_strcat(SMS_services_list[i].name, ep->d_name);
- SMS_services_list[i].found = 0;
- SMS_services_list[i].default_warning = 1;
- SMS_services_list[i].str_value = NULL;
- SMS_services_list[i].int_value = 0;
- SMS_services_list[i].default_str_value = NULL;
- SMS_services_list[i].default_int_value = 0;
- SMS_services_list[i].var = NULL;
- i++;
- }
- SMS_services_list[i].type = RESOURCE_NULL;
- SMS_services_list[i].name = NULL;
- SMS_services_list[i].found = 0;
- SMS_services_list[i].default_warning = 1;
- SMS_services_list[i].str_value = NULL;
- SMS_services_list[i].int_value = 0;
- SMS_services_list[i].default_str_value = NULL;
- SMS_services_list[i].default_int_value = 0;
- SMS_services_list[i].var = NULL;
- sms_strcpy(fname, SERVICEDIR);
- sms_strcat(fname, "/sms_services");
- if (read_resource_file(fname, SMS_services_list, FALSE) != RESOURCE_FILE_OK)
- { lprintf(LOG_ERROR, "Unrecoverable Failure Parsing service file %sn", fname);
- exit(-1);
- }
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- char *get_protocol(char *service)
- {
- char name[512];
- RESOURCE
- *resource;
- if (SMS_services_list == NULL)
- { read_services();
- }
- sms_strcpy(name, "SMS_protocol_");
- sms_strcat(name, service);
- resource = SMS_services_list;
- while (resource->type != RESOURCE_NULL)
- {
- if (strcmp(name, resource->name) == 0)
- { lprintf(LOG_VERBOSE, "Found %s = %sn", resource->name, resource->str_value);
- return resource->str_value;
- }
- resource++;
- }
- return NULL;
- }
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- int validate_expanded_numbers(SMS_list *numbers)
- {
- DEVICE_ENTRY
- *device;
- char *id,
- *service,
- *protocol;
- int count;
- SMS_list
- *node;
- count = 0;
-
- node = get_first(numbers);
- while (node != NULL)
- {
- service = get_service(node);
- protocol = get_protocol(service);
- device = get_device(protocol);
- if (device == NULL)
- { lprintf(LOG_ERROR, "Driver for service %s NOT foundn", service);
- count++;
- }
- else
- { id = get_number(node);
- if (!((*device->validate_id)(id)))
- {
- lprintf(LOG_WARNING, "Invalid id: %sn", id);
- count++;
- }
- }
- node = get_next(node);
- }
- return count;
- }
-
- /* -------------------------------------------------------------------- */
- /* -------------------------------------------------------------------- */
- int is_numeric(char *ptr)
- {
- while (*ptr != ' ')
- {
- if (!isdigit(*ptr))
- { return FALSE;
- }
- ptr++;
- }
- return TRUE;
- }