File: gom_file.c

package info (click to toggle)
gom 0.29.10-3
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 416 kB
  • ctags: 290
  • sloc: ansic: 3,183; sh: 529; makefile: 68
file content (405 lines) | stat: -rw-r--r-- 10,202 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * gom_file.c: loading and saving files
 */

/* 
 * This file is part of the package
 *
 * gom, Gom is nOt yet another Mixer
 *
 * (c) Stephan Suerken <suerken@fh-worms.de> 1996, 1997
 */

/*
 *  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.
 */

/*
 * INCLUDES
 */
#include "gom.h"

/*
 * MACROS
 */

#define GOM_FILE_OPT_SIZE         100
#define GOM_FILE_OPT_SIZE_QUOTED "100"

/*
 * DECLARATIONS/DEFINTIONS
 */

char gom_file_option[GOM_FILE_OPT_SIZE];

/*
 * FUNCTION PROTOTYPES
 */

/*****************/

/*
 * INTERN HELPERS
 */

char * 
gom_file_fncat(const char * ct)
{
  if (strlen(ct) > FILENAME_MAX-strlen(gom_file_fn)-1)
    gom_info(GOM_INFO_ERROR, "Bug: File name too big (>%i characters).", FILENAME_MAX-1);
  return strncat(gom_file_fn, ct, FILENAME_MAX-strlen(gom_file_fn)-1);
}

int
gom_file_systemconf()
{
  return (geteuid() == 0) || (getuid() == 0);
}

int 
gom_file_fn_make(enum gom_info_types verb,
		 const char * fname)
{
  gom_file_fn[0] = '\0';
  if (fname != NULL && (fname[0] == '/' || (fname[0] == '.' && fname[1] == '/')))
    {
      /* /dir/file or ./dir/file: no overwrite */
      gom_file_fncat(fname);
      return 0;
    }
  else                /* filename relative to goms config dir; overwrite */
    {
      if (gom_file_systemconf())
	{
	  /* superuser privileges: write to system directory */
	  gom_file_fncat(GOM_CONFIG_DIR_SYSTEM);
	}
      else if (getenv("HOME") != NULL)
	{
	  /* normal user; write to ~/.gom */
	  gom_file_fncat(getenv("HOME"));
	  gom_file_fncat("/" GOM_CONFIG_DIR_USER);
	}
      else
        {
	  gom_info(GOM_INFO_QUIET, "Warning: No environment variable HOME found (using current directory).");
	  gom_file_fncat("./" GOM_CONFIG_DIR_USER);
	}
      gom_file_fncat((fname == NULL) || (strlen(fname) == 0) ? GOM_OPTIONS_FILE_DEFAULT : fname);
      return 1;           /* ok to overwrite */
    }
}


/*
 * LOADING
 */


/* intern */
char *
gom_file_optarg_get()
{
  static char nullstr[] = "";

  if (strlen(gom_file_option) < 2)
    /* gom_file_option == "d" or worse */
    return nullstr;
  else
    /* seems to be valid */
    return (char *) &gom_file_option[1];
}


char
gom_file_fprereadc(FILE * f)
{
  char c;
  if (1 == fscanf(f, "%c", &c))
    fseek(f, -1L, SEEK_CUR);
  else
    c = EOF;
  return c;
}


/* loading options from stream: */
void 
gom_file_options_read(enum gom_info_types std_verb,
		      FILE * f, int action_ignore)
{
  char ind, action;
  int count, line = 1, line_valid;

  do
    {
      /* valid lines MUST start with a dash; pre-read first char of line to check */
      line_valid = ((ind = gom_file_fprereadc(f)) == '-');

      if (line_valid)
	{
	  gom_info(std_verb+2, "Line %d: significant, processing...", line);
	  
	  while (1 == (count = fscanf(f,
				      "-%" GOM_FILE_OPT_SIZE_QUOTED "s",
				      (char *) &gom_file_option)))
	    {
	      /* validity check? */

	      action = gom_file_option[0];

	      gom_info(std_verb+2, "Line %d: Option found: %s", line, gom_file_option);

	      if (action != action_ignore)
		gom_action(std_verb, action, 1, gom_file_optarg_get, NULL, NULL);
	      else
		gom_info(std_verb,
			 "Option \"-%s\" ignored.",
			 &gom_file_option);
	      
	      /* a new following space indicates more options */
	      if (isspace(ind = gom_file_fprereadc(f)) && (ind != '\n'))
		/* trennzeichen, aber kein newline */
		fseek(f, +1L, SEEK_CUR);
	      else
		/* kein trennzeichen, evt. newline */
		break;
	    }
	}
      else
	{
	  /* this is only for debugging config files... */
	  if (ind == '#')
	    gom_info(std_verb+2, "Line %d: comment, skipping...", line);
	  else if (ind == '\n')
	    gom_info(std_verb+2, "Line %d: empty, skipping...", line);
	  else
	    gom_info(std_verb+2, "Line %d: ambigious, skipping...", line);
	}
      
      /* skipping to the end of this line */
      while ((1 == fscanf(f, "%c", &ind)) && (ind != EOF) && (ind != '\n'))
	{
	  if (line_valid)
	    gom_info(std_verb+2, "Line %d: Skipping suspicious trailing garbage (%c).", line, ind);
	};

      line++;
    }
  while (feof(f) == 0);
}

/* loading options from file: */
void 
gom_file_options_load(enum gom_info_types std_verb,
		      char * fname,
		      enum gom_info_types open_err_verb, int action_ignore)
{
  gom_file_fn_make(std_verb, fname);
  if ((gom_file_f = fopen(gom_file_fn, "r")) != NULL)
    {
      gom_info(std_verb, "Loading option file from \"%s\"...", gom_file_fn);
      gom_file_options_read(std_verb+1, gom_file_f, action_ignore);
      gom_info(std_verb, "Option file load from \"%s\" completed.", gom_file_fn);
      
      /* ok, no error check here... */
      fclose(gom_file_f);
    }
  else
    {
      gom_info(open_err_verb,
	       "Can't open file \"%s\" for reading: %s.",
	       gom_file_fn, strerror(errno));
    }
}

/* loading gom's configuration: */
/* - either loads user, system or no config */
/* - no errors */
void
gom_file_config_load(enum gom_info_types verb, int action_ignore)
{
  /* check if user config file exists */
  gom_file_fn_make (verb, GOM_CONFIG_FILE);
  if (access (gom_file_fn, F_OK) == 0)
    {
      /* try to load user config */
      gom_info(verb+1, "User configuration file (%s) found.", gom_file_fn);
      gom_file_options_load (verb, GOM_CONFIG_FILE, verb, action_ignore);
    }
  else
    {
      gom_file_fn_make (verb, GOM_CONFIG_DIR_SYSTEM GOM_CONFIG_FILE);
      if (access (gom_file_fn, F_OK) == 0)
	{
	  /* try to load system config */
	  gom_info(verb+1, "System configuration file (%s) found.", gom_file_fn);
	  gom_file_options_load (verb, GOM_CONFIG_DIR_SYSTEM GOM_CONFIG_FILE, verb, action_ignore);
	}
      else
	{
	  gom_info(verb,
		   "Neither user (~/" GOM_CONFIG_DIR_USER GOM_CONFIG_FILE ") nor system ("
		   GOM_CONFIG_DIR_SYSTEM GOM_CONFIG_FILE ") configuration file found.");
	}

    }
}


/*
 * SAVING
 */

/* intern */
int
gom_file_open_save(enum gom_info_types verb,
		   const char * file)
{
  int overwrite;


  overwrite = gom_file_fn_make(verb, file);
  
  /* NEVER write the config file as setting */
  if ((file != NULL) && (strncmp(file, GOM_CONFIG_FILE, strlen(GOM_CONFIG_FILE)) == 0))
    {
      gom_info(GOM_INFO_ERROR,
	       "I won't save files starting with \"" GOM_CONFIG_FILE "\" (like \"%s\") to the configuration dir.",
	       gom_file_fn);
    }
  /* try open file */
  else if (!overwrite && (gom_file_f = fopen(gom_file_fn, "r")) != NULL)  /* open absolute given file */
    {
      fclose(gom_file_f); gom_file_f = NULL;
      gom_info(GOM_INFO_ERROR,
	       "File \"%s\" exists (I refuse to overwrite files elsewhere than in $HOME/.gom).",
	       gom_file_fn);
    }
  else if ((gom_file_f = fopen(gom_file_fn, "w")) == NULL)  /* open relative given file */
    {
      gom_info(GOM_INFO_ERROR,
	       "Can't open file \"%s\" for save: %s.",
	       gom_file_fn, strerror(errno));     
    }
  else
    {
      /* ok to overwrite */
      if (gom_file_systemconf())
	gom_info(GOM_INFO_QUIET, "Warning: Running as root, writing to \"" GOM_CONFIG_DIR_SYSTEM "\".");
      return 1;
    }
  return 0;
}

/* intern helps to stream */
int
gom_file_stropt_write(FILE * f, char option, char * arg)
{
  return fprintf(f, "-%c%s ", option, arg);
}

int 
gom_file_intopt_write(FILE * f, char option, int arg)
{
  return fprintf(f, "-%c%i ", option, arg);
}

/* write ONE CHANNEL */
void 
gom_file_options_c_write(FILE * f, int c)
{
  int cc;

  /* select channel, write record */
  gom_file_intopt_write(f, 'c', c);
  if (gom_driver_c_r(c) > -1)
    gom_file_intopt_write(f, 'r', gom_driver_c_r(c));
  
  /* unlock channel to get the different volumes right when *loading* */
  gom_file_intopt_write(f, 'k', 0);
  
  /* write all channel volumes */
  for (cc=gom_driver_c_c_first(c); cc > -1; cc=gom_driver_c_c_next(c, cc, 1))
    {
      gom_file_intopt_write(f, 'C', cc);
      gom_file_intopt_write(f, 'l', gom_driver_c_c_v(c, cc));
    }
  /* _lock_ channel as bulls neck default when *loading* */
  gom_file_intopt_write(f, 'k', 1);
}

/* settings file to stream */
void 
gom_file_settings_write(FILE * f, int verbose)
{
  int c;

  if (verbose)
    {
      fprintf(f,
	      "# This is a mixer settings file, automatically produced by " GOM_VERSION_STRING ".\n"
	      "# Lines starting with \"-\" are read (must be one-letter style gom options).\n"
	      "# Other lines are comments.\n"
	      "#\n"
	      "# Channels with recording source == 1 first (avoids \"last rec. source error\" when loading):\n");
    }

  for (c=gom_driver_c_first(); c > -1; c=gom_driver_c_next(c, 1))
    {
      if (gom_driver_c_r(c) > 0)
	{
	  gom_file_options_c_write(f, c);
	  if (verbose)
	    fprintf(f, "\n");
	}
    }
  
  if (verbose)
    fprintf(f,
	    "# Other channels:\n");
  for (c=gom_driver_c_first(); c > -1; c=gom_driver_c_next(c, 1))
    {
      if (gom_driver_c_r(c) <= 0)
	{
	  gom_file_options_c_write(f, c);
	  if (verbose)
	    fprintf(f, "\n");
	}
    }
  if (verbose)
    fprintf(f,
	    "# Resetting current channel to first.\n");
  gom_file_intopt_write(f, 'c', gom_driver_c_first());
}

/* settings file to file: */
void 
gom_file_settings_save(enum gom_info_types std_verb,
		       char * fname)
{
  /* try open file */
  if (gom_file_open_save(std_verb, fname))
    {
      /* write a comment header */
      gom_file_settings_write(gom_file_f, 1);

      if (fclose(gom_file_f) == 0)
	gom_info(std_verb, "Settings saved to \"%s\".", gom_file_fn);
      else
	gom_info(GOM_INFO_ERROR, "Problems closing file \"%s\": %s.",
		 gom_file_fn, strerror(errno));
    }
}