File: parse.c

package info (click to toggle)
flin 0.5.1-9.2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 276 kB
  • ctags: 164
  • sloc: ansic: 1,642; makefile: 105; sh: 71
file content (186 lines) | stat: -rw-r--r-- 4,085 bytes parent folder | download | duplicates (4)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
Parses menu file into a linked list
Copyright (C) 1995  Brian Cully

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.

This program 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 General Public License for more
details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 675 Mass
Ave, Cambridge, MA 02139, USA.

please send patches or advice to: `shmit@kublai.com'
*/
#ifdef HAVE_CONFIG_H
#   include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "string.h"
#include "parse.h"
#include "menu.h"
#include "misc.h"

char delim=DELIM;
char comment=COMMENT;

extern char draw_box;

char *set_nobox(struct menu **, char *, char *, int *);

struct directive {
   char *str;
   char *(*func)(struct menu **, char *, char *, int *);
};

struct directive menu_directives[] = {
   { MENU_BEGIN, define_menu },
   { DIREC_NOBOX, set_nobox },
   { NULL, NULL }
};

static int get_directive(char *name, char *(**func)(struct menu **, char *, char *, int *)) {
   int i=0, found=0;

   while (menu_directives[i].str) {
      if (!strcasecmp(name, menu_directives[i].str)) {
	 found = 1;
	 break;
      }
      i++;
   }

   if (!found)
      return NO_TOKEN;

   *func = menu_directives[i].func;

#ifdef DEBUG
   printf("Directive: `%s'\n", menu_directives[i].str);
#endif
   
   return 0;
}

char *set_nobox(struct menu **menu_ptr, char *menufile, char *line, int *line_no) {
   draw_box = 0;

   return menufile;
}

/* Parses the file into a linked menu list
** On entry:
**    menuptr == empty pointer to list
**    menufile == loaded menu file
** On exit:
**    menuptr == full menu, in linked list
**    returns -1 on error, 0 otherwise
*/

int parsefile(struct menu **menuptr,char *menufile) {
   char Line[MAX_LINE], Word[MAX_WORD];
   char *line = Line, *word = Word;
   int index,i=0;

   *menuptr = (struct menu *)chk_alloc(sizeof(struct menu));
   (*menuptr)->prev = NULL;
   (*menuptr)->next = NULL;
   (*menuptr)->data = NULL;

   while ((index = getline(menufile,line)) != -1) {
      int worddex;
      char *(*func)(struct menu **, char *, char *, int *);
      
      i++;
      menufile += index+1;

      /* Skip past white space */
      strwhite(line);

      /* comment -> `\0' */
      if (*line == comment)
	 continue;

      /* Ignore blank lines */
      if (*line == '\0')
	 continue;

      if ((worddex = substr(line, word, ' ')) == 0)
	 error("Premature end-of-line\n", i);
      
      line += worddex + 1;

      if (get_directive(word, &func) == NO_TOKEN) {
	 char mess[MAX_WORD];
	 
	 sprintf(mess, "Unrecognized directive: `%s'\n", word);
	 error(mess, i);
      }

#ifdef DEBUG
      printf("calling func()\n");
#endif
      
      menufile = func(menuptr, menufile, line, &i);
   }

   /* Free last link */

   if (!(*menuptr)->prev) {
      free(*menuptr);
      *menuptr = NULL;
   } else {
      *menuptr = (*menuptr)->prev;
      free((*menuptr)->next);
      (*menuptr)->next = NULL;

      /* rewind linked list */

      while ((*menuptr)->prev)
	 *menuptr = (*menuptr)->prev;
   }

   return 0;
}

/* frees up allocated memory
** On entry:
**    menuptr == linked menu list
**    menufile == loaded menu file
** On Exit:
**    nothing, DONT USE menuptr OR menufile without allocating them again
*/

void freemem(struct menu *menuptr, char *menufile) {
   free(menufile);

   while (menuptr) {
      struct menu *p;

      p = menuptr;
      menuptr = menuptr->next;
      while (p->data) {
	 struct menu_items *q;
	 
	 q = p->data;
	 p->data = p->data->next;
	 if (q->name)
	    free(q->name);
	 if (q->args)
	    free(q->args);
	 free(q);
      }
      free(p->name);
      free(p);
   }
}