1 /*
2   Created: 03.19.05 11:34:57 by Attila Nagyidai
3 
4   $Id: C\040Console.c,v 1.1.2.1 2003/08/13 00:38:46 neum Exp $
5 
6   This file is part of IBSH (Iron Bars Shell) , a restricted Unix shell
7   Copyright (C) 2005  Attila Nagyidai
8 
9   This program is free software; you can redistribute it and/or
10   modify it under the terms of the GNU General Public License
11   as published by the Free Software Foundation; either version 2
12   of the License, or (at your option) any later version.
13 
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18 
19   You should have received a copy of the GNU General Public License
20   along with this program; if not, write to the Free Software
21   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22 
23   Author: Attila Nagyidai
24   Email: na@ent.hu
25 
26   Co-Author: Shy
27   Email: shy@cpan.org
28 
29   Co-Author: Witzy
30   Email: stazzz@altern.org
31 
32   URL: http://ibsh.sourceforge.net
33   IRC: irc.freenode.net #ibsh
34   RSS, Statistics, etc: http://sourceforge.net/projects/ibsh/
35 
36 */
37 
38 /* Header files */
39 #include "ibsh.h"
40 
41 extern Strng commands[MAX_ITEMS];
42 extern Strng extensions[MAX_ITEMS];
43 
44 /* Shy's improved version of the original (and not well working) loadconfig. */
45 /* Reads both config files, and parses the contents into arrays. */
46 /* This one effectively dismisses every comment from the files. */
47 /* Technical Description: */
48 /* Variables: file pointer, counters, temporary string arrays. */
49 /* The method is the same for both files. First open the file, catch */
50 /* any errors. Read file 'til eof. Not read comments (starting with '#'), */
51 /* remove trailing newline character. Copy the finished item to the */
52 /* pass-by-address arguments. */
LoadConfig(void)53 int LoadConfig( void )
54 {
55   FILE *fp;
56   int i = 0;
57   char *file_user;
58 
59   Strng tmp[MAX_ITEMS];
60   Strng tmp2[MAX_ITEMS];
61 
62   /* COMMAND CONFIG !!!! */
63   file_user = (char *)malloc(strlen(loggedin.uname) + strlen(COMMANDS_DIR) + strlen(".cmds") + 2);
64 
65   if(loggedin.uname != NULL)
66   	sprintf(file_user, "%s/%s.cmds", COMMANDS_DIR, loggedin.uname);
67   else{
68 	  free(file_user);
69 	  return -1;
70   }
71 
72   /* Open global config,if not present go out !!! */
73   if((fp = fopen(COMMANDS_FILE,"r")) == NULL) {
74       OPENLOG;
75       syslog(LOG_ERR, "ibsh panic! Global commands file %s can not be read.", COMMANDS_FILE);
76       CLOSELOG;
77       exit(0);
78   }
79 
80   while (!feof(fp) && (i<MAX_ITEMS)) {
81         fgets(tmp[i],STRING_SIZE,fp);
82         if ( tmp[i][0] != '#' ) {
83                 /* Delete '\n' */
84                 tmp[i][strlen(tmp[i]) - 1] = '\0';
85                 strncpy(commands[i],tmp[i],strlen(tmp[i]));
86 
87 #ifdef DEBUG
88                 printf("COMMANDS %s\n",commands[i]);
89 #endif
90 		i++;
91       }
92   }
93   fclose(fp);
94 
95 
96 #ifdef DEBUG
97   printf("FILE USER %s\n",file_user);
98 #endif
99   /* Add the user command */
100   if ((fp = fopen(file_user,"r")) == NULL) {
101  	      free(file_user);
102 	}
103   else {
104   i--;
105 
106   while (!feof(fp) && (i<MAX_ITEMS)) {
107         fgets(tmp[i],STRING_SIZE,fp);
108         if ( tmp[i][0] != '#' ) {
109                 // Delete '\n'
110                 tmp[i][strlen(tmp[i]) - 1] = '\0';
111                 strncpy(commands[i],tmp[i],strlen(tmp[i]));
112 #ifdef DEBUG
113                 printf("COMMANDS %s\n",commands[i]);
114 #endif
115 
116 		i++;
117       }
118   }
119   fclose(fp);
120   free(file_user);
121   }
122 
123   i = 0;
124 
125   /* EXTENSIONS CONFIG !!!!*/
126 
127   file_user = (char *)malloc(strlen(loggedin.uname) + strlen(EXTENSIONS_DIR) + strlen(".xtns") + 2);
128 
129   sprintf(file_user, "%s/%s.xtns", EXTENSIONS_DIR, loggedin.uname);
130 
131     /* Open global config,if not present go out !!! */
132   if((fp = fopen(EXTENSIONS_FILE,"r")) == NULL) {
133       OPENLOG;
134       syslog(LOG_ERR, "ibsh panic! Global extensions file %s can not be read.", EXTENSIONS_FILE);
135       CLOSELOG;
136       printf("heyxtns");
137       exit(0);
138   }
139 
140   while (!feof(fp) && (i<MAX_ITEMS)) {
141     fgets(tmp2[i],STRING_SIZE,fp);
142         if ( tmp2[i][0] != '#' ) {
143                 /* Delete '\n' */
144                 tmp2[i][strlen(tmp2[i]) - 1] = '\0';
145                 strncpy(extensions[i],tmp2[i],strlen(tmp2[i]));
146 #ifdef DEBUG
147                 printf("EXTENSIONS %s\n",extensions[i]);
148 #endif
149 		i++;
150       }
151   }
152   fclose(fp);
153 
154 
155   /* Add the user extensions */
156   if ((fp = fopen(file_user,"r")) == NULL) {
157  	      free(file_user);
158 	      return 0;
159 	}
160 
161   i--;
162 
163   while (!feof(fp) && (i<MAX_ITEMS)) {
164         fgets(tmp2[i],STRING_SIZE,fp);
165         if ( tmp2[i][0] != '#' ) {
166                 // Delete '\n'
167                 tmp2[i][strlen(tmp2[i]) - 1] = '\0';
168                 strncpy(extensions[i],tmp2[i],strlen(tmp2[i]));
169                 printf("EXTENSIONS %s\n",extensions[i]);
170 		i++;
171       }
172   }
173   fclose(fp);
174   free(file_user);
175 
176   return 0;
177 }
178 
179