Permalink
Browse files

More refactoring; chopping up quirc.c. '#pragma once'-ing headers

  • Loading branch information...
Sound and Fury
Sound and Fury committed Aug 15, 2010
1 parent c819805 commit 29252b6b579e9d1d0f9e4ee10c1a39d1414fdf5e
Showing with 169 additions and 106 deletions.
  1. +3 −3 Makefile
  2. +3 −5 bits.h
  3. +15 −0 buffer.c
  4. +4 −0 buffer.h
  5. +42 −20 config.c
  6. +15 −2 config.h
  7. +29 −4 irc.c
  8. +7 −0 irc.h
  9. +2 −0 names.h
  10. +23 −72 quirc.c
  11. +26 −0 quirc.h
View
@@ -21,13 +21,13 @@ quirc: quirc.c $(LIBS) $(INCLUDE)
%.o: %.c %.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
-irc.o: irc.c irc.h bits.h
+irc.o: irc.c irc.h bits.h buffer.h colour.h config.h
-bits.o: bits.c bits.h ttyesc.h colour.h
+bits.o: bits.c bits.h ttyesc.h colour.h config.h
colour.o: colour.c colour.h c_init.c ttyesc.h
-buffer.o: buffer.c buffer.h ttyesc.h colour.h bits.h names.h
+buffer.o: buffer.c buffer.h ttyesc.h colour.h bits.h names.h text.h
config.o: config.c config.h bits.h colour.h text.h version.h
View
8 bits.h
@@ -1,3 +1,5 @@
+#pragma once
+
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
@@ -11,15 +13,11 @@
#include <string.h>
#include "ttyesc.h"
#include "colour.h"
+#include "config.h"
// helper fn macros
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
-// global settings & state (set in quirc.c)
-int width, height; // term size
-int mirc_colour_compat;
-int force_redraw;
-
char * fgetl(FILE *); // gets a line of string data; returns a malloc-like pointer (preserves trailing \n)
int wordline(char *, int x, char **); // prepares a string for printing, breaking lines in between words
View
@@ -8,6 +8,21 @@
#include "buffer.h"
+int initialise_buffers(int buflines, char *nick)
+{
+ bufs=(buffer *)malloc(sizeof(buffer));
+ if(!bufs)
+ return(1);
+ init_buffer(0, STATUS, "status", buflines); // buf 0 is always STATUS
+ nbufs=1;
+ cbuf=0;
+ bufs[0].nick=strdup(nick);
+ if(!bufs[0].nick)
+ return(1);
+ buf_print(0, c_status, GPL_MSG, false);
+ return(0);
+}
+
int init_buffer(int buf, btype type, char *bname, int nlines)
{
bufs[buf].type=type;
View
@@ -1,3 +1,5 @@
+#pragma once
+
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
@@ -17,6 +19,7 @@
#include "colour.h"
#include "bits.h"
#include "names.h"
+#include "text.h"
typedef enum
{
@@ -50,6 +53,7 @@ int nbufs;
int cbuf;
buffer *bufs;
+int initialise_buffers(int buflines, char *nick);
int init_buffer(int buf, btype type, char *bname, int nlines);
int free_buffer(int buf);
int add_to_buffer(int buf, colour lc, char *lt);
View
@@ -8,7 +8,29 @@
#include "config.h"
-int rcread(FILE *rcfp, char **server, char **portno, char **uname, char **fname, char **nick, char **chan, int *maxnlen, int *buflines)
+int def_config(void)
+{
+ buflines=256;
+ mirc_colour_compat=1; // silently strip
+ force_redraw=1; // redraw the whole screen whenever anything happens
+ char *cols=getenv("COLUMNS"), *rows=getenv("LINES");
+ if(cols) sscanf(cols, "%u", &width);
+ if(rows) sscanf(rows, "%u", &height);
+ if(!width) width=80;
+ if(!height) height=24;
+ maxnlen=16;
+ server=NULL;
+ portno="6667";
+ username="quirc";
+ fname=(char *)malloc(20+strlen(VERSION_TXT));
+ nick=strdup("ac");
+ chan=NULL;
+ sprintf(fname, "quIRC %hhu.%hhu.%hhu%s%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT[0]?"-":"", VERSION_TXT);
+ sprintf(version, "%hhu.%hhu.%hhu%s%s", VERSION_MAJ, VERSION_MIN, VERSION_REV, VERSION_TXT[0]?"-":"", VERSION_TXT);
+ return(0);
+}
+
+int rcread(FILE *rcfp)
{
while(!feof(rcfp))
{
@@ -93,25 +115,25 @@ int rcread(FILE *rcfp, char **server, char **portno, char **uname, char **fname,
{
char *rest=strtok(NULL, "\n");
if(strcmp(cmd, "server")==0)
- *server=strdup(rest);
+ server=strdup(rest);
else if(strcmp(cmd, "port")==0)
- *portno=strdup(rest);
+ portno=strdup(rest);
else if(strcmp(cmd, "uname")==0)
- *uname=strdup(rest);
+ username=strdup(rest);
else if(strcmp(cmd, "fname")==0)
- *fname=strdup(rest);
+ fname=strdup(rest);
else if(strcmp(cmd, "nick")==0)
- *nick=strdup(rest);
+ nick=strdup(rest);
else if(strcmp(cmd, "chan")==0)
- *chan=strdup(rest);
+ chan=strdup(rest);
else if(strcmp(cmd, "mnln")==0)
- sscanf(rest, "%u", maxnlen);
+ sscanf(rest, "%u", &maxnlen);
else if(strcmp(cmd, "mcc")==0)
sscanf(rest, "%u", &mirc_colour_compat);
else if(strcmp(cmd, "fred")==0)
sscanf(rest, "%u", &force_redraw);
else if(strcmp(cmd, "buf")==0)
- sscanf(rest, "%u", buflines);
+ sscanf(rest, "%u", &buflines);
else
{
fprintf(stderr, "Unrecognised cmd %s in .quirc (ignoring)\n", cmd);
@@ -122,7 +144,7 @@ int rcread(FILE *rcfp, char **server, char **portno, char **uname, char **fname,
return(0);
}
-signed int pargs(int argc, char *argv[], char **server, char **portno, char **uname, char **fname, char **nick, char **chan, int *maxnlen, int *buflines)
+signed int pargs(int argc, char *argv[])
{
bool check=false;
int arg;
@@ -148,7 +170,7 @@ signed int pargs(int argc, char *argv[], char **server, char **portno, char **un
}
else if(strncmp(argv[arg], "--maxnicklen=", 13)==0)
{
- sscanf(argv[arg]+13, "%u", maxnlen);
+ sscanf(argv[arg]+13, "%u", &maxnlen);
}
else if(strncmp(argv[arg], "--mcc=", 6)==0)
{
@@ -160,32 +182,32 @@ signed int pargs(int argc, char *argv[], char **server, char **portno, char **un
}
else if(strncmp(argv[arg], "--buf-lines=", 12)==0)
{
- sscanf(argv[arg]+12, "%u", buflines);
+ sscanf(argv[arg]+12, "%u", &buflines);
}
else if((strcmp(argv[arg], "--no-server")==0)||(strcmp(argv[arg], "--no-auto-connect")==0)) // the "-auto" forms are from older versions; depr
{
- *server=NULL;
+ server=NULL;
}
else if((strcmp(argv[arg], "--no-chan")==0)||(strcmp(argv[arg], "--no-auto-join")==0))
{
- *chan=NULL;
+ chan=NULL;
}
else if((strcmp(argv[arg], "--check")==0)||(strcmp(argv[arg], "--lint")==0))
{
check=true;
}
else if(strncmp(argv[arg], "--server=", 9)==0)
- *server=argv[arg]+9;
+ server=argv[arg]+9;
else if(strncmp(argv[arg], "--port=", 7)==0)
- *portno=argv[arg]+7;
+ portno=argv[arg]+7;
else if(strncmp(argv[arg], "--uname=", 8)==0)
- *uname=argv[arg]+8;
+ username=argv[arg]+8;
else if(strncmp(argv[arg], "--fname=", 8)==0)
- *fname=argv[arg]+8;
+ fname=argv[arg]+8;
else if(strncmp(argv[arg], "--nick=", 7)==0)
- *nick=strdup(argv[arg]+7);
+ nick=strdup(argv[arg]+7);
else if(strncmp(argv[arg], "--chan=", 7)==0)
- *chan=argv[arg]+7;
+ chan=argv[arg]+7;
else
{
fprintf(stderr, "Unrecognised argument '%s'\n", argv[arg]);
View
@@ -1,3 +1,5 @@
+#pragma once
+
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
@@ -8,10 +10,21 @@
#include <stdio.h>
#include <stdbool.h>
+#include <string.h>
#include "bits.h"
#include "colour.h"
#include "text.h"
#include "version.h"
-int rcread(FILE *rcfp, char **server, char **portno, char **uname, char **fname, char **nick, char **chan, int *maxnlen, int *buflines); // read & parse the rc file
-signed int pargs(int argc, char *argv[], char **server, char **portno, char **uname, char **fname, char **nick, char **chan, int *maxnlen, int *buflines); // parse the cmdline args. If return is >=0, main should return it also
+// global settings & state
+int width, height; // term size
+int mirc_colour_compat;
+int force_redraw;
+int buflines;
+int maxnlen;
+char *server, *portno, *username, *fname, *nick, *chan;
+char version[16+strlen(VERSION_TXT)];
+
+int def_config(void); // set these to their defaults
+int rcread(FILE *rcfp); // read & parse the rc file.
+signed int pargs(int argc, char *argv[]); // parse the cmdline args. If return is >=0, main should return it also
View
33 irc.c
@@ -12,7 +12,7 @@ int irc_connect(char *server, char *portno, char *nick, char *username, char *fu
{
int serverhandle;
struct addrinfo hints, *servinfo;
- //printf("Looking up server: %s:%s\n", server, portno);
+ // Look up server
memset(&hints, 0, sizeof(hints));
hints.ai_family=AF_INET;
hints.ai_socktype = SOCK_STREAM; // TCP stream sockets
@@ -23,13 +23,11 @@ int irc_connect(char *server, char *portno, char *nick, char *username, char *fu
return(0); // 0 indicates failure as rv is new serverhandle value
}
char sip[INET_ADDRSTRLEN];
- //printf("running, connecting to server...\n");
struct addrinfo *p;
// loop through all the results and connect to the first we can
for(p = servinfo; p != NULL; p = p->ai_next)
{
inet_ntop(p->ai_family, &(((struct sockaddr_in*)p->ai_addr)->sin_addr), sip, sizeof(sip));
- //printf("connecting to %s\n", sip);
if((serverhandle = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
perror("socket");
@@ -50,7 +48,7 @@ int irc_connect(char *server, char *portno, char *nick, char *username, char *fu
fprintf(stderr, "failed to connect\n\n");
return(0); // 0 indicates failure as rv is new serverhandle value
}
- freeaddrinfo(servinfo); // all done with this structure
+ freeaddrinfo(servinfo); // don't need this any more
FD_SET(serverhandle, master);
*fdmax=max(*fdmax, serverhandle);
@@ -66,6 +64,33 @@ int irc_connect(char *server, char *portno, char *nick, char *username, char *fu
return(serverhandle);
}
+int autoconnect(fd_set *master, int *fdmax)
+{
+ char cstr[36+strlen(server)+strlen(portno)];
+ sprintf(cstr, "quIRC - connecting to %s", server);
+ settitle(cstr);
+ sprintf(cstr, "Connecting to %s on port %s...", server, portno);
+ setcolour(c_status);
+ printf(CLA "\n");
+ printf(LOCATE, height-2, 1);
+ printf("%s" CLR "\n", cstr);
+ resetcol();
+ printf(CLA "\n");
+ int serverhandle=irc_connect(server, portno, nick, username, fname, master, fdmax);
+ if(serverhandle)
+ {
+ bufs=(buffer *)realloc(bufs, ++nbufs*sizeof(buffer));
+ init_buffer(1, SERVER, server, buflines);
+ cbuf=1;
+ bufs[cbuf].handle=serverhandle;
+ bufs[cbuf].nick=strdup(nick);
+ bufs[cbuf].server=1;
+ add_to_buffer(1, c_status, cstr);
+ sprintf(cstr, "quIRC - connected to %s", server);
+ settitle(cstr);
+ }
+ return(serverhandle);
+}
int irc_tx(int fd, char * packet)
{
View
7 irc.h
@@ -1,3 +1,5 @@
+#pragma once
+
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
@@ -16,9 +18,14 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
+
#include "bits.h"
+#include "buffer.h"
+#include "colour.h"
+#include "config.h"
int irc_connect(char *server, char *portno, char *nick, char *username, char *fullname, fd_set *master, int *fdmax);
+int autoconnect(fd_set *master, int *fdmax);
int irc_tx(int fd, char * packet);
int irc_rx(int fd, char ** data);
View
@@ -1,3 +1,5 @@
+#pragma once
+
/*
quIRC - simple terminal-based IRC client
Copyright (C) 2010 Edward Cree
Oops, something went wrong.

0 comments on commit 29252b6

Please sign in to comment.