Update Debian Vcs-* fields to point to git repository
[onak.git] / gpgstats-0.0.2 / parse.c
1 /*
2         parse.c - General string parsing routines.
3         Copyright 1999 Jonathan McDowell for Project Purple
4
5         19/09/1999 - Started writing.
6 */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "parse.h"
13
14 struct strll *addtoend(struct strll *current, char *newstr)
15 {
16         struct strll *new, *tmp;
17
18         if ((new=malloc(sizeof(struct strll)))==NULL) {
19                 perror("addtoend()");
20                 exit(1);
21         }
22
23         new->str=newstr;
24         new->next=NULL;
25
26         if (current==NULL) {
27                 return new;
28         } else {
29                 tmp=current;
30                 while (tmp->next!=NULL) tmp=tmp->next;
31                 tmp->next=new;
32         }
33
34         return current;
35 }
36
37 int parseline(struct cfginf commands[], const char *commandline)
38 {
39         int loop=0;
40         char *params;
41         char command[CMDLEN], *pos=NULL;
42
43         params=NULL;
44         if (commands==NULL || commandline==NULL || strlen(commandline)==0) return 0;
45
46         if ((params=strdup(commandline))==NULL) {
47                 return 0;
48         }
49
50         while (params[strlen(params)-1]<' ') params[strlen(params)-1]=0;
51
52         if ((pos=strchr(params, ' '))!=NULL) {
53                 *pos=0;
54                 if (strlen(params)>=CMDLEN) {
55                         /* Hah. No buffer overflow here. (Egg on face approaching....) */
56                         free(params);
57                         return 0;
58                 }
59                 strncpy(command, params, CMDLEN);
60                 command[CMDLEN-1]=0;
61                 memmove(params, pos+1, strlen(commandline)-strlen(params));
62         } else {
63                 if (strlen(params)>=CMDLEN) {
64                         /* Hah. No buffer overflow here. (Egg on face approaching....) */
65                         free(params);
66                         return 0;
67                 }
68                 strncpy(command, params, CMDLEN);
69                 command[CMDLEN-1]=0;
70         }
71
72         while (strlen(commands[loop].command)>0 && strcasecmp(command, commands[loop].command)!=0) {
73                 ++loop;
74         }
75
76         if (strlen(commands[loop].command)==0) {
77                 return -1;
78         } else {
79                 if (commands[loop].type==0 && params==NULL) {
80                         return loop+1;
81                 } else {
82                         switch (commands[loop].type) {
83                         case 1: *((char **) commands[loop].var) = params;
84                                 break;
85                         case 2: *((int *) commands[loop].var) = str2bool(params);
86                                 free(params);
87                                 break;
88                         case 3: *((int *) commands[loop].var) = atoi(params);
89                                 free(params);
90                                 break;
91                         case 4: *((struct strll **) commands[loop].var) = addtoend(*((struct strll **) commands[loop].var), params);
92                                 break;
93                         default:
94                                 break;
95                         }
96                         return loop+1;
97                 }
98         }
99 }
100
101 int str2bool(const char *buf)
102 {
103         if (strcasecmp("TRUE", buf) == 0 || strcmp("1", buf) == 0 ||
104                 strcasecmp("Y", buf) == 0 || strcasecmp("YES", buf) == 0 ||
105                 strcasecmp("T", buf) == 0) return 1;
106
107         if (strcasecmp("FALSE", buf) == 0 || strcmp("0", buf) == 0 ||
108                 strcasecmp("N", buf) == 0 || strcasecmp("NO", buf) == 0 ||
109                 strcasecmp("F", buf) == 0) return 0;
110
111         return -1;
112 }