Permalink
Browse files

start_buffer, catches messages produced before bufs initialised

  • Loading branch information...
1 parent 539ff04 commit 7cb81a20bfd32a77987615565d11ae2bdbd18d28 ec429 committed Dec 28, 2010
Showing with 182 additions and 50 deletions.
  1. +111 −4 buffer.c
  2. +19 −3 buffer.h
  3. +18 −10 config.c
  4. +4 −3 config.h
  5. +0 −2 plans
  6. +30 −28 quirc.c
View
115 buffer.c
@@ -1,13 +1,85 @@
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
buffer: multiple-buffer control
*/
#include "buffer.h"
+int init_start_buffer(void)
+{
+ s_buf.nlines=0;
+ s_buf.lt=NULL;
+ s_buf.lc=NULL;
+ s_buf.ts=NULL;
+ s_buf.errs=0;
+ return(0);
+}
+
+int add_to_start_buffer(colour lc, char *lt)
+{
+ s_buf.nlines++;
+ char **nlt;colour *nlc;time_t *nts;
+ nlt=(char **)realloc(s_buf.lt, s_buf.nlines*sizeof(char *));
+ if(!nlt)
+ {
+ s_buf.nlines--;
+ s_buf.errs++;
+ return(1);
+ }
+ s_buf.lt=nlt;
+ s_buf.lt[s_buf.nlines-1]=strdup(lt);
+ nlc=(colour *)realloc(s_buf.lc, s_buf.nlines*sizeof(colour));
+ if(!nlc)
+ {
+ s_buf.nlines--;
+ s_buf.errs++;
+ return(1);
+ }
+ s_buf.lc=nlc;
+ s_buf.lc[s_buf.nlines-1]=lc;
+ nts=(time_t *)realloc(s_buf.ts, s_buf.nlines*sizeof(time_t));
+ if(!nts)
+ {
+ s_buf.nlines--;
+ s_buf.errs++;
+ return(1);
+ }
+ s_buf.ts=nts;
+ s_buf.ts[s_buf.nlines-1]=time(NULL);
+ return(0);
+}
+
+int asb_failsafe(colour lc, char *lt)
+{
+ int e=0;
+ if((e=add_to_start_buffer(lc, lt)))
+ {
+ fprintf(stderr, "init[%d]: %s\n", e, lt);
+ }
+ return(e);
+}
+
+int free_start_buffer(void)
+{
+ int i;
+ if(s_buf.lt)
+ {
+ for(i=0;i<s_buf.nlines;i++)
+ {
+ if(s_buf.lt[i]) free(s_buf.lt[i]);
+ }
+ free(s_buf.lt);
+ s_buf.lt=NULL;
+ }
+ if(s_buf.lc) free(s_buf.lc);
+ if(s_buf.ts) free(s_buf.ts);
+ s_buf.nlines=0;
+ return(0);
+}
+
int initialise_buffers(int buflines)
{
bufs=(buffer *)malloc(sizeof(buffer));
@@ -114,7 +186,7 @@ int add_to_buffer(int buf, colour lc, char *lt)
{
if(buf>=nbufs)
{
- w_buf_print(0, c_err, "Line was written to bad buffer! Contents below.", "add_to_buffer()");
+ w_buf_print(0, c_err, "Line was written to bad buffer! Contents below.", "add_to_buffer(): ");
w_buf_print(0, lc, lt, "");
return(1);
}
@@ -470,6 +542,41 @@ int e_buf_print(int buf, colour lc, message pkt, char *lead)
return(w_buf_print(buf, lc, text, lead));
}
+int transfer_start_buffer(void)
+{
+ int i,e=0;
+ for(i=0;i<s_buf.nlines;i++)
+ {
+ e|=w_buf_print(0, s_buf.lc[i], s_buf.lt[i], "init: ");
+ }
+ return(e);
+}
+
+int push_buffer(void)
+{
+ if(transfer_start_buffer())
+ {
+ w_buf_print(0, c_err, "Failed to transfer start_buffer", "init[xsb]: ");
+ int i;
+ for(i=0;i<s_buf.nlines;i++)
+ {
+ fprintf(stderr, "init[xsb]: %s\n", s_buf.lt[i]);
+ }
+ char msg[32];
+ sprintf(msg, "%d messages written to stderr", s_buf.nlines);
+ w_buf_print(0, c_status, msg, "init[xsb]: ");
+ }
+
+ if(s_buf.errs)
+ {
+ char msg[80];
+ sprintf(msg, "%d messages were written to stderr due to start_buffer errors", s_buf.errs);
+ w_buf_print(0, c_err, msg, "init[errs]: ");
+ }
+
+ return(free_start_buffer());
+}
+
void titlebar(void)
{
printf(LOCATE CLA, 1, 1);
View
22 buffer.h
@@ -1,9 +1,9 @@
#pragma once
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
buffer: multiple-buffer control
*/
@@ -74,6 +74,20 @@ int nbufs;
int cbuf;
buffer *bufs;
+struct
+{
+ int nlines;
+ char **lt;
+ colour *lc;
+ time_t *ts;
+ int errs;
+}
+s_buf;
+
+int init_start_buffer(void);
+int add_to_start_buffer(colour lc, char *lt);
+int asb_failsafe(colour lc, char *lt);
+int free_start_buffer(void);
int initialise_buffers(int buflines);
int init_buffer(int buf, btype type, char *bname, int nlines);
int free_buffer(int buf);
@@ -82,6 +96,8 @@ int redraw_buffer(void);
int buf_print(int buf, colour lc, char *lt); // don't include trailing \n, because buf_print appends CLR \n
int w_buf_print(int buf, colour lc, char *lt, char *lead);
int e_buf_print(int buf, colour lc, message pkt, char *lead);
+int transfer_start_buffer(void);
+int push_buffer(void);
void in_update(iline inp);
char *highlight(char *src); // use ANSI-colours to highlight \escapes. Returns a malloc-like pointer
void titlebar(void);
View
28 config.c
@@ -131,7 +131,9 @@ int rcread(FILE *rcfp)
}
else
{
- fprintf(stderr, "Unrecognised ident in %%colour (%s)\n", cmd);
+ char msg[48+strlen(cmd)];
+ sprintf(msg, "rc: Unrecognised ident in %%colour (%s)", cmd);
+ asb_failsafe(c_err, msg);
nerrors++;
}
}
@@ -140,7 +142,9 @@ int rcread(FILE *rcfp)
char *rest=strtok(NULL, "");
if(!rest)
{
- fprintf(stderr, "Command (%s) without argument!\n", cmd);
+ char msg[40+strlen(cmd)];
+ sprintf(msg, "rc: Command (%s) without argument", cmd);
+ asb_failsafe(c_err, msg);
nerrors++;
}
else if(strcmp(cmd, "server")==0)
@@ -178,15 +182,15 @@ int rcread(FILE *rcfp)
char *sw=strtok(rest, " \t");
if(*sw!='-')
{
- fprintf(stderr, "ignore: need options (use '-' for no options)\n");
+ asb_failsafe(c_err, "rc: ignore: need options (use '-' for no options)");
nerrors++;
}
else
{
rest=strtok(NULL, "");
if(!rest)
{
- fprintf(stderr, "ignore: need options (use '-' for no options)\n");
+ asb_failsafe(c_err, "rc: ignore: need options (use '-' for no options)");
nerrors++;
}
else
@@ -228,15 +232,15 @@ int rcread(FILE *rcfp)
char *sw=strtok(rest, " \t");
if(*sw!='-')
{
- fprintf(stderr, "*ignore: need options (use '-' for no options)\n");
+ asb_failsafe(c_err, "rc: *ignore: need options (use '-' for no options)");
nerrors++;
}
else
{
rest=strtok(NULL, "");
if(!rest)
{
- fprintf(stderr, "*ignore: need options (use '-' for no options)\n");
+ asb_failsafe(c_err, "rc: *ignore: need options (use '-' for no options)");
nerrors++;
}
else
@@ -322,7 +326,9 @@ int rcread(FILE *rcfp)
#endif // HAVE_DEBUG
else
{
- fprintf(stderr, "Unrecognised cmd %s in .quirc (ignoring)\n", cmd);
+ char msg[48+strlen(cmd)];
+ sprintf(msg, "rc: Unrecognised cmd %s in .quirc (ignoring)", cmd);
+ asb_failsafe(c_err, msg);
nerrors++;
}
}
@@ -353,7 +359,7 @@ signed int pargs(int argc, char *argv[])
if(width<30)
{
width=30;
- printf("width set to minimum 30\n");
+ asb_failsafe(c_status, "pargs: width set to minimum 30");
}
}
else if(strncmp(argv[arg], "--height=", 9)==0)
@@ -362,7 +368,7 @@ signed int pargs(int argc, char *argv[])
if(height<5)
{
height=5;
- printf("height set to minimum 5\n");
+ asb_failsafe(c_status, "pargs: height set to minimum 5");
}
}
else if(strncmp(argv[arg], "--maxnicklen=", 13)==0)
@@ -471,7 +477,9 @@ signed int pargs(int argc, char *argv[])
#endif // HAVE_DEBUG
else
{
- fprintf(stderr, "Unrecognised argument '%s'\n", argv[arg]);
+ char msg[40+strlen(argv[arg])];
+ sprintf(msg, "pargs: Unrecognised argument '%s'", argv[arg]);
+ asb_failsafe(c_err, msg);
}
}
if(check) return(0);
View
7 config.h
@@ -1,9 +1,9 @@
#pragma once
/*
- quIRC - simple terminal-based IRC client
- Copyright (C) 2010 Edward Cree
-
+ quIRC - simple terminal-based IRC client
+ Copyright (C) 2010 Edward Cree
+
See quirc.c for license information
config: rc file and option parsing
*/
@@ -38,6 +38,7 @@ servlist;
#include "colour.h"
#include "text.h"
#include "version.h"
+#include "buffer.h"
// global settings & state
int width, height; // term size
View
2 plans
@@ -1,7 +1,5 @@
==quIRC:future plans==
-Init-buffer, to catch messages before initialise_buffers called
-
Scripting language. Under development in branch 'script'.
Quiet mode. Don't emit diagnostics for eg. unrecognised IRC traffic (such as numerics).
View
58 quirc.c
@@ -24,29 +24,35 @@ int main(int argc, char *argv[])
mtrace();
#endif // USE_MTRACE
+ init_start_buffer();
+
sigpipe=0;
struct sigaction sa;
sa.sa_handler = handle_sigpipe;
sa.sa_flags=0;
sigemptyset(&sa.sa_mask);
-
- if(sigaction(SIGPIPE, &sa, NULL)==-1)
- {
- fprintf(stderr, "Failed to set SIGPIPE handler\n");
- perror("sigaction");
- return(1);
- }
-
+
+ if(sigaction(SIGPIPE, &sa, NULL)==-1)
+ {
+ fprintf(stderr, "Failed to set SIGPIPE handler\n");
+ perror("sigaction");
+ return(1);
+ }
+
int infc=fcntl(STDIN_FILENO, F_GETFD);
- char *fcfail=NULL;
if(infc>=0)
{
if(fcntl(STDIN_FILENO, F_SETFD, infc|O_NONBLOCK)==-1)
- fcfail=strerror(errno);
+ {
+ char *err=strerror(errno);
+ char msg[48+strlen(err)];
+ sprintf(msg, "Failed to mark stdin non-blocking: fcntl: %s", err);
+ asb_failsafe(c_status, msg);
+ }
}
- if(c_init())
+ if(c_init()!=0) // should be impossible
{
- fprintf(stderr, "Failed to initialise colours\n"); // should be impossible
+ fprintf(stderr, "Failed to initialise colours\n");
return(1);
}
if(def_config())
@@ -66,11 +72,22 @@ int main(int argc, char *argv[])
{
rc_err=rcread(rcfp);
fclose(rcfp);
+ if(rc_err)
+ {
+ char msg[32];
+ sprintf(msg, "%d errors in ~/.quirc", rc_err);
+ asb_failsafe(c_status, msg);
+ }
+ }
+ else
+ {
+ asb_failsafe(c_status, "no config file found. Install one at ~/.quirc");
}
signed int e=pargs(argc, argv);
if(e>=0)
{
+ push_buffer();
return(e);
}
@@ -93,22 +110,7 @@ int main(int argc, char *argv[])
return(1);
}
- if(fcfail)
- {
- w_buf_print(0, c_status, fcfail, "fcntl: ");
- }
-
- if(!rcfp)
- {
- w_buf_print(0, c_status, "no config file found. Install one at ~/.quirc", "rc: ");
- }
-
- if(rc_err)
- {
- char msg[32];
- sprintf(msg, "%d errors in ~/.quirc", rc_err);
- w_buf_print(0, c_status, msg, "rc: ");
- }
+ push_buffer();
fd_set master, readfds;
FD_ZERO(&master);

0 comments on commit 7cb81a2

Please sign in to comment.