summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorrcyeske <rcyeske>2001-03-02 05:25:02 +0000
committerrcyeske <rcyeske>2001-03-02 05:25:02 +0000
commit11932954e0122c115be23862aabfacb565a4cbed (patch)
tree5d624b34129748104728d697a9e24e9b54de892a /src/main.c
parent9d783e1994f521940955a29b5a7df33b50fb0c9d (diff)
downloadratpoison-11932954e0122c115be23862aabfacb565a4cbed.zip
* actions.c (cmd_source): Open the file. Error report as
appropriate. * main.c (read_rc_file): Take a file pointer rather than a filename. (read_startup_files): If ~/.ratpoisonrc is not readable try /etc/ratpoisonrc. * actions.c: Use PRINT_DEBUG instead of fprintf. Put useful error text in calls to message(). (cmd_select): Show the window list if there is no such window number.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c94
1 files changed, 58 insertions, 36 deletions
diff --git a/src/main.c b/src/main.c
index 276df1d..802d37f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -185,66 +185,88 @@ print_help ()
}
void
-read_rc_file (char *filename)
+read_rc_file (FILE *file)
{
- FILE *rcfile;
+ size_t n = 256;
+ char *partial;
+ char *line;
+ int linesize = n;
- if ((rcfile = fopen (filename, "r")) == NULL)
- {
- PRINT_DEBUG ("ratpoison: could not open %s\n", filename);
- }
- else
+ partial = (char*)malloc(n);
+ line = (char*)malloc(linesize);
+
+ *line = '\0';
+ while (fgets (partial, n, file) != NULL)
{
- size_t n = 256;
- char *partial;
- char *line;
- int linesize = n;
+ if ((strlen (line) + strlen (partial)) <= linesize)
+ {
+ linesize *= 2;
+ line = (char*) realloc (line, linesize);
+ }
- partial = (char*)malloc(n);
- line = (char*)malloc(linesize);
+ strcat (line, partial);
- *line = '\0';
- while (fgets (partial, n, rcfile) != NULL)
+ if (feof(file) || (*(line + strlen(line) - 1) == '\n'))
{
- if ((strlen (line) + strlen (partial)) <= linesize)
- {
- linesize *= 2;
- line = (char*) realloc (line, linesize);
- }
+ /* FIXME: this is a hack, command() should properly parse
+ the command and args (ie strip whitespace, etc)
- strcat (line, partial);
+ We should not care if there is a newline (or vertical
+ tabs or linefeeds for that matter) at the end of the
+ command (or anywhere between tokens). */
+ if (*(line + strlen(line) - 1) == '\n')
+ *(line + strlen(line) - 1) = '\0';
- if (feof(rcfile) || (*(line + strlen(line) - 1) == '\n'))
- {
- PRINT_DEBUG ("rcfile line: %s\n", line);
+ PRINT_DEBUG ("rcfile line: %s\n", line);
- /* do it */
- command (line);
+ /* do it */
+ command (line);
- *line = '\0';
- }
+ *line = '\0';
}
+ }
- free (line);
- free (partial);
- }
+ free (line);
+ free (partial);
}
static void
read_startup_files ()
{
char *homedir;
+ FILE *fileptr;
- homedir = getenv ("HOME");
+ /* first check $HOME/.ratpoisonrc and if that does not exist then try
+ /etc/ratpoisonrc */
+ homedir = getenv ("HOME");
if (!homedir)
- fprintf (stderr, "$HOME not set!?\n");
+ fprintf (stderr, "ratpoison: $HOME not set!?\n");
else
{
- char *rcfile = (char*)malloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1);
- sprintf (rcfile, "%s/.ratpoisonrc", homedir);
- read_rc_file (rcfile);
+ char *filename = (char*)malloc (strlen (homedir) + strlen ("/.ratpoisonrc") + 1);
+ sprintf (filename, "%s/.ratpoisonrc", homedir);
+
+ if ((fileptr = fopen (filename, "r")) == NULL)
+ {
+ /* we probably don't need to report this, its not an error */
+ fprintf (stderr, "ratpoison: could not open %s\n", filename);
+
+ if ((fileptr = fopen ("/etc/ratpoisonrc", "r")) == NULL)
+ {
+ /* neither is this */
+ fprintf (stderr, "ratpoison: could not open /etc/ratpoisonrc\n");
+ }
+ }
+
+ if (fileptr)
+ {
+ read_rc_file (fileptr);
+ fclose (fileptr);
+ }
+
+ free (filename);
}
}