http://www.azstarnet.com/~ymg/files/slsnif-0.4.0.tar.gz
[slsnif.git] / src / rcfile.c
1 /*  rcfile.c
2  *  Copyright (C) 2001 Yan "Warrior" Gurtovoy (ymg@azstarnet.com)
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <ctype.h>
22 #include "common.h"
23 #include "rcfile.h"
24
25 void str2upper(char *str) {
26 /* converts given string to upper case */
27     int i = 0;
28     while (str[i]) toupper(str[i++]);
29     return;
30 }
31
32 void str2lower(char *str) {
33 /* converts given string to lower case */
34     int i = 0;
35     while (str[i]) tolower(str[i++]);
36     return;
37 }
38
39 /* callback functions */
40
41 void rc_get_bytes(tty_struct *ptr, char *value) {
42     str2upper(value);
43     ptr->dspbytes = strcmp(ON, value) ? FALSE : TRUE;
44     return;
45 }
46
47 void rc_get_tstamp(tty_struct *ptr, char *value) {
48     str2upper(value);
49     ptr->tstamp = strcmp(ON, value) ? FALSE : TRUE;
50     return;
51 }
52
53 void rc_get_nolock(tty_struct *ptr, char *value) {
54     str2upper(value);
55     ptr->nolock = strcmp(ON, value) ? FALSE : TRUE;
56     return;
57 }
58
59 void rc_get_color(tty_struct *ptr, char *value) {
60     char *clr;
61
62     str2lower(value);
63     clr = getColor(value);
64     if (clr) strcat(ptr->clr, clr);
65     return;
66 }
67
68 void rc_get_timecolor(tty_struct *ptr, char *value) {
69     char *clr;
70
71     str2lower(value);
72     clr = getColor(value);
73     if (clr) strcat(ptr->tclr, clr);
74     return;
75 }
76
77 void rc_get_bytescolor(tty_struct *ptr, char *value) {
78     char *clr;
79
80     str2lower(value);
81     clr = getColor(value);
82     if (clr) strcat(ptr->bclr, clr);
83     return;
84 }
85
86 /* end of callback functions */
87
88 void readRC(tty_struct *ptr) {
89 /* read settings from rc-file .slsnifrc */
90
91     char buffer[256];
92     char name[NAMELEN + 1];
93     char value[VALUELEN + 1];
94     int  i;
95     FILE *fp;
96
97     /* get path to the rc-file */
98     sprintf(buffer, "%s/%s", getenv("HOME"), RCFNAME);
99     /* try to open rc-file */
100     if (!(fp = fopen(buffer, "r"))) {
101         return;
102     } else {
103         /* read rc-file line by line */
104         while (fgets(buffer, 256, fp)) {
105             /* check for comments */
106             if (buffer[0] != '#') {
107                 /* get data */
108                 sscanf(buffer, "%s %[^\n]", name, value);
109                 i = 0;
110                 /* if identificator is valid, process data */
111                 while (rc_data[i].name && strcmp(rc_data[i].name, name)) i++;
112                 if (rc_data[i].name) rc_data[i].fn(ptr, value);
113             }
114         }
115         fclose(fp);
116     }    
117 }