-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathoptions.cpp
More file actions
136 lines (123 loc) · 3.85 KB
/
options.cpp
File metadata and controls
136 lines (123 loc) · 3.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/***************************************************************************
options.cpp - description
-------------------
begin : Fri Jan 11 2002
copyright : (C) 2002 by Igor V. Youdytsky
email : Pitcher@gw.tander.ru
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <string.h>
#include <stdlib.h>
#include "drvfr.h"
#include "options.h"
const char *LineSpeed[] =
{
"2400",
"4800",
"9600",
"19200",
"38400",
"57600",
"115200"
};
/**********************************************************
* Local functions & procedures *
**********************************************************/
int readrcfile(char*);
void parse(char*);
void setdrvconf(char*, char*);
/**********************************************************
* Local static variables *
**********************************************************/
static int t_devnum = 0;
static int t_passwd = 0;
static int t_timeout = 0;
static int t_linespeed = 0;
static fr_func *fr;
/**********************************************************
* Implementation of procedures *
**********************************************************/
int readrcfile(char *fname)
{
int rcfile;
ssize_t bytesread;
char *filebuff;
filebuff = (char*)malloc(MAX_LEN);
if((rcfile = open(fname, O_RDONLY)) == -1) return -1;
if((bytesread = read(rcfile,filebuff,MAX_LEN)) > 0) parse(filebuff);
close(rcfile);
free(filebuff);
return 1;
}
//-----------------------------------------------------------------------------
void setdrvconf(char *param, char *val)
{
int tmp;
if(strcmp(param,"portnum") == 0)
{
if((tmp = strtol(val,NULL,10)) > 0) t_devnum = tmp;
return;
};
if(strcmp(param,"passwd") == 0)
{
if((tmp = strtol(val,NULL,10)) > 0) t_passwd = tmp;
return;
};
if(strcmp(param,"timeout") == 0)
{
if((tmp = strtol(val,NULL,10)) > 0 && tmp < 256) t_timeout = tmp;
return;
};
if(strcmp(param,"boudrate") == 0)
{
for(int tmp=0; tmp<7; tmp++)
{
if(strcmp(val,LineSpeed[tmp]) == 0)
{
t_linespeed = tmp;
break;
};
};
return;
};
}
//-----------------------------------------------------------------------------
void parse(char *str)
{
char *param, *val, *word, *tmp, *tmp1;
char delim[] = " .,;:!-\n";
tmp = strdup(str);
word = strtok(tmp,delim);
do
{
tmp1 = strdup(word);
param = strsep(&tmp1,"=");
val = strsep(&tmp1,"=");
setdrvconf(param, val);
} while ((word = strtok(NULL,delim)) != NULL);
}
//-----------------------------------------------------------------------------
int readoptions (void)
{
char *homedir;
int etc_res;
int home_res;
fr = drvfrInitialize();
homedir = getenv("HOME");
etc_res = readrcfile("/etc/drvfrrc");
home_res = readrcfile(strcat(homedir,"/.drvfrrc"));
if(etc_res == -1 && home_res == -1) return -1;
fr->prop->ComPortNumber = t_devnum;
fr->prop->Password = t_passwd;
fr->prop->Timeout = t_timeout;
fr->prop->BaudRate = t_linespeed;
return 1;
}
//-----------------------------------------------------------------------------