📄 config.c
字号:
/*
* Tcpdump .INI-file parser
*/
#include <stdlib.h>
#include <dos.h>
#include <io.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>
#include "interfac.h"
#include "a2name.h"
#ifdef USE_SECTION_LOCKING
extern int using_section_locking;
void *config_dummy = &using_section_locking;
#endif
/* Function internal to Watt-32 are used here too
*/
extern char *ExpandVarStr (const char *str);
static void parse_config (FILE *fil);
static void set_values (char *name, char *value, int line);
static int get_colour (char *value, int *col);
int tcpdump_init (const char *ini_file)
{
FILE *fil = fopen (ini_file, "rt");
if (!fil)
{
PERROR (("`%s' not found\n", ini_file));
return (0);
}
parse_config (fil);
fclose (fil);
return (1);
}
/*
* Read the ini-file one character at a time building up name/value
* pair that's further parsed in set_values().
*/
static void parse_config (FILE *fil)
{
char name[_MAX_PATH], value[80], buf[2];
int quotemode, mode;
int ch, line = 1;
buf[1] = '\0';
*name = *value = mode = 0;
quotemode = 0;
while ((ch = fgetc(fil)) != EOF)
{
switch (ch)
{
case '\"': quotemode ^= 1;
break;
case ' ' :
case '\t': if (quotemode)
goto addit;
break;
case '=' : if (quotemode)
goto addit;
if (!mode)
mode = 1;
break;
case '#' :
case ';' : if (quotemode)
goto addit;
mode = 2; /* comment */
break;
case '\n': line++;
case '\r':
if (*name && *value)
{
char *var = ExpandVarStr (value);
if (var != value)
strcpy (value, var);
strupr (name);
set_values (name, value, line);
}
*name = *value = quotemode = 0;
mode = 0;
break;
default:
addit: switch (mode)
{
case 0: buf[0] = ch;
strcat (name, buf);
break;
case 1: buf[0] = ch;
strcat (value, buf);
break;
}
break;
}
}
}
/*
* Set value for right-side (name) to value
*/
static void set_values (char *name, char *value, int line)
{
if (!strcmp(name,"INTERFACE"))
{
if (!pcap_device && strnicmp(value,"auto",4)) /* don't override cmd-line */
pcap_device = savestr (value);
return;
}
#if 0
if (!strcmp(name,"LOCALNET"))
{
localnet = inet_aton (value,NULL); /* overridden by pcap_lookupnet() */
return;
}
if (!strcmp(name,"NETMASK"))
{
netmask = inet_aton (value,NULL); /* overridden by pcap_lookupnet() */
return;
}
#endif
if (!strcmp(name,"ETHERS"))
{
ether_file = savestr (path_program(value));
return;
}
if (!strcmp(name,"ATALK"))
{
atalk_file = savestr (path_program(value));
return;
}
if (!strcmp(name,"HOSTS") || !strcmp(name,"SERVICES") ||
!strcmp(name,"PROTOCOLS") || !strcmp(name,"NETWORKS"))
return;
if (!strcmp(name,"COLOUR.MAIN"))
{
get_colour (value, &colour_main);
return;
}
if (!strcmp(name,"COLOUR.STATUS"))
{
get_colour (value, &colour_stat);
return;
}
if (!strcmp(name,"COLOUR.DEBUG"))
{
get_colour (value, &colour_debug);
return;
}
if (!strcmp(name,"BREAK_MODE"))
{
break_mode = atoi (value);
return;
}
if (!pcap_config_hook(name,value))
{
#if 0
/* ignore key/values used by 32-bit drivers
*/
WARNING (("Unknown INI-line at line %d: \"%s = %s\"\n",
line-1, name, value));
sleep (1);
#endif
}
}
/*
* Extract "fg/bg" colour from value. Set result into '*col'
*/
static int get_colour (char *value, int *col)
{
static char *fg_tab[] = {
"black", "blue", "green", "cyan",
"red", "magenta", "brown", "lightgray",
"darkgray","lightblue", "lightgreen","lightcyan",
"lightred","lightmagenta","yellow", "white"
};
static char *bg_tab[] = {
"black", "blue", "green", "cyan",
"red", "magenta", "brown", "lightgray"
};
char fg[30], bg[30];
int fg_col, bg_col;
if (!value || !strchr(value,'/') ||
strlen(value) > sizeof(fg)+sizeof(bg))
return (0);
if (sscanf(value,"%[^/]/%s", fg, bg) < 2)
return (0);
for (fg_col = 0; fg_col < DIM(fg_tab); fg_col++)
if (!stricmp(fg_tab[fg_col],fg))
break;
for (bg_col = 0; bg_col < DIM(bg_tab); bg_col++)
if (!stricmp(bg_tab[bg_col],bg))
break;
if (fg_col >= DIM(fg_tab) || bg_col >= DIM(bg_tab))
return (0);
*col = fg_col + 16*bg_col;
return (1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -