3 * UCC (University [of WA] Computer Club) Electronic Accounting System
6 * main.c - Core and Initialisation
8 * This file is licenced under the 3-clause BSD Licence. See the file
9 * COPYING for full details.
14 #include <ctype.h> // isspace
20 #include <unistd.h> // close
21 #include <netdb.h> // gethostbyname
22 #include <pwd.h> // getpwuids
23 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <openssl/sha.h> // SHA1
28 #define USE_NCURSES_INTERFACE 0
29 #define DEBUG_TRACE_SERVER 0
32 typedef struct sItem {
39 int main(int argc, char *argv[]);
42 int ShowNCursesUI(void);
43 void ShowItemAt(int Row, int Col, int Width, int Index);
44 void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1, const char *Mid, char Pad2, const char *Right, ...);
45 // --- Coke Server Communication ---
46 int OpenConnection(const char *Host, int Port);
47 int Authenticate(int Socket);
48 void PopulateItemList(int Socket);
49 int DispenseItem(int Socket, int ItemID);
50 int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason);
51 int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason);
52 int Dispense_EnumUsers(int Socket);
53 int Dispense_ShowUser(int Socket, const char *Username);
54 void _PrintUserLine(const char *Line);
55 int Dispense_AddUser(int Socket, const char *Username);
56 int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString);
58 char *ReadLine(int Socket);
59 int sendf(int Socket, const char *Format, ...);
60 char *trim(char *string);
61 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage);
62 void CompileRegex(regex_t *regex, const char *pattern, int flags);
65 char *gsDispenseServer = "localhost";
66 int giDispensePort = 11020;
70 regex_t gArrayRegex, gItemRegex, gSaltRegex, gUserInfoRegex;
71 int gbIsAuthenticated = 0;
73 char *gsItemPattern; //!< Item pattern
74 char *gsEffectiveUser; //!< '-u' Dispense as another user
75 int gbUseNCurses = 0; //!< '-G' Use the NCurses GUI?
76 int giMinimumBalance = INT_MIN; //!< '-m' Minumum balance for `dispense acct`
77 int giMaximumBalance = INT_MAX; //!< '-M' Maximum balance for `dispense acct`
80 int main(int argc, char *argv[])
86 // -- Create regular expressions
87 // > Code Type Count ...
88 CompileRegex(&gArrayRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([0-9]+)", REG_EXTENDED); //
89 // > Code Type Ident Price Desc
90 CompileRegex(&gItemRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([A-Za-z0-9:]+?)\\s+([0-9]+)\\s+(.+)$", REG_EXTENDED);
92 CompileRegex(&gSaltRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+(.+)$", REG_EXTENDED);
93 // > Code 'User' Username Balance Flags
94 CompileRegex(&gUserInfoRegex, "^([0-9]{3})\\s+([A-Za-z]+)\\s+([^ ]+)\\s+(-?[0-9]+)\\s+(.+)$", REG_EXTENDED);
97 for( i = 1; i < argc; i ++ )
110 case 'm': // Minimum balance
111 giMinimumBalance = atoi(argv[++i]);
113 case 'M': // Maximum balance
114 giMaximumBalance = atoi(argv[++i]);
117 case 'u': // Override User
118 gsEffectiveUser = argv[++i];
129 if( strcmp(arg, "acct") == 0 )
132 sock = OpenConnection(gsDispenseServer, giDispensePort);
133 if( sock < 0 ) return -1;
136 if( i + 1 == argc ) {
137 Dispense_EnumUsers(sock);
141 // argv[i+1]: Username
146 if( i + 3 >= argc ) {
147 fprintf(stderr, "Error: `dispense acct' needs a reason\n");
151 // Authentication required
152 if( Authenticate(sock) )
155 // argv[i+1]: Username
156 // argv[i+2]: Ammount
159 if( argv[i+2][0] == '=' ) {
161 Dispense_SetBalance(sock, argv[i+1], atoi(argv[i+2] + 1), argv[i+3]);
165 Dispense_AlterBalance(sock, argv[i+1], atoi(argv[i+2]), argv[i+3]);
169 Dispense_ShowUser(sock, argv[i+1]);
174 else if( strcmp(arg, "user") == 0 )
176 // Check argument count
177 if( i + 1 >= argc ) {
178 fprintf(stderr, "Error: `dispense user` requires arguments\n");
184 sock = OpenConnection(gsDispenseServer, giDispensePort);
185 if( sock < 0 ) return -1;
187 // Attempt authentication
188 if( Authenticate(sock) )
192 if( strcmp(argv[i+1], "add") == 0 )
194 if( i + 2 >= argc ) {
195 fprintf(stderr, "Error: `dispense user add` requires an argument\n");
200 Dispense_AddUser(sock, argv[i+2]);
203 else if( strcmp(argv[i+1], "type") == 0 )
205 if( i + 3 >= argc ) {
206 fprintf(stderr, "Error: `dispense user type` requires two arguments\n");
211 Dispense_SetUserType(sock, argv[i+2], argv[i+3]);
215 fprintf(stderr, "Error: Unknown sub-command for `dispense user`\n");
222 // Item name / pattern
229 sock = OpenConnection(gsDispenseServer, giDispensePort);
230 if( sock < 0 ) return -1;
236 PopulateItemList(sock);
242 else if( gbUseNCurses )
248 for( i = 0; i < giNumItems; i ++ ) {
249 printf("%2i %s\t%3i %s\n", i, gaItems[i].Ident, gaItems[i].Price, gaItems[i].Desc);
258 fgets(buffer, BUFSIZ, stdin);
262 if( buf[0] == 'q' ) break;
266 if( i != 0 || buf[0] == '0' )
268 if( i < 0 || i >= giNumItems ) {
269 printf("Bad item %i (should be between 0 and %i)\n", i, giNumItems);
277 // Check for a valid item ID
279 DispenseItem(sock, i);
291 " Show interactive list\n"
293 " Dispense named item\n"
294 " dispense give <user> <ammount> \"<reason>\"\n"
295 " Give some of your money away\n"
296 " dispense acct [<user>]\n"
297 " Show user balances\n"
298 " dispense acct <user> [+-=]<ammount> \"<reason>\"\n"
299 " Alter a account value (Coke members only)\n"
300 " dispense user add <user>\n"
301 " Create new coke account (Wheel members only)\n"
302 " dispense user type <flags>\n"
303 " Alter a user's flags\n"
304 " <flags> is a comma-separated list of user,coke,wheel,disabled\n"
305 " Flags are removed by preceding the name with '-' or '!'\n"
309 " Set a different user (Coke members only)\n"
313 " Use alternate GUI\n"
314 " -m <min balance>\n"
315 " -M <max balance>\n"
316 " Set the Maximum/Minimum balances shown in `dispense acct`\n"
320 // -------------------
321 // --- NCurses GUI ---
322 // -------------------
324 * \brief Render the NCurses UI
326 int ShowNCursesUI(void)
328 // TODO: ncurses interface (with separation between item classes)
329 // - Hmm... that would require standardising the item ID to be <class>:<index>
334 const int displayMinWidth = 40;
335 const int displayMinItems = 8;
336 char *titleString = "Dispense";
337 int itemCount = displayMinItems;
340 int ret = -2; // -2: Used for marking "no return yet"
349 // - 6: randomly chosen (Need at least 3)
350 itemCount = LINES - 6;
351 if( itemCount > giNumItems )
352 itemCount = giNumItems;
355 height = itemCount + 3;
356 width = displayMinWidth;
359 xBase = COLS/2 - width/2;
360 yBase = LINES/2 - height/2;
365 PrintAlign(yBase, xBase, width, "/", '-', titleString, '-', "\\");
368 for( i = 0; i < itemCount; i ++ )
370 move( yBase + 1 + i, xBase );
372 if( currentItem == itemBase + i ) {
380 // - Oh god, magic numbers!
381 if( i == 0 && itemBase > 0 ) {
383 times = width-1 - 8 - 3;
384 while(times--) addch(' ');
386 else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) {
388 times = width-1 - 8 - 3;
389 while(times--) addch(' ');
393 ShowItemAt( yBase + 1 + i, xBase + 5, width - 7, itemBase + i);
397 // Scrollbar (if needed)
398 if( giNumItems > itemCount ) {
402 else if( i == itemCount - 1 ) {
406 int percentage = itemBase * 100 / (giNumItems-itemCount);
407 if( i-1 == percentage*(itemCount-3)/100 ) {
421 PrintAlign(yBase+height-2, xBase, width, "\\", '-', "", '-', "/");
434 //if( itemBase < giNumItems - (itemCount) )
436 if( currentItem < giNumItems - 1 )
438 if( itemBase + itemCount - 1 <= currentItem && itemBase + itemCount < giNumItems )
444 if( currentItem > 0 )
446 if( itemBase + 1 > currentItem && itemBase > 0 )
462 ret = -1; // -1: Return with no dispense
466 // Check if the return value was changed
467 if( ret != -2 ) break;
479 * \brief Show item \a Index at (\a Col, \a Row)
480 * \note Part of the NCurses UI
482 void ShowItemAt(int Row, int Col, int Width, int Index)
490 if( Index < 0 || Index >= giNumItems ) {
495 name = gaItems[Index].Desc;
496 price = gaItems[Index].Price;
499 printw("%02i %s", Index, name);
501 getyx(stdscr, _y, _x);
502 // Assumes max 4 digit prices
503 times = Width - 4 - (_x - Col); // TODO: Better handling for large prices
504 while(times--) addch(' ');
505 printw("%4i", price);
509 * \brief Print a three-part string at the specified position (formatted)
510 * \note NCurses UI Helper
512 * Prints \a Left on the left of the area, \a Right on the righthand side
513 * and \a Mid in the middle of the area. These are padded with \a Pad1
514 * between \a Left and \a Mid, and \a Pad2 between \a Mid and \a Right.
516 * ::printf style format codes are allowed in \a Left, \a Mid and \a Right,
517 * and the arguments to these are read in that order.
519 void PrintAlign(int Row, int Col, int Width, const char *Left, char Pad1,
520 const char *Mid, char Pad2, const char *Right, ...)
522 int lLen, mLen, rLen;
527 // Get the length of the strings
528 va_start(args, Right);
529 lLen = vsnprintf(NULL, 0, Left, args);
530 mLen = vsnprintf(NULL, 0, Mid, args);
531 rLen = vsnprintf(NULL, 0, Right, args);
535 if( lLen + mLen/2 > Width/2 || mLen/2 + rLen > Width/2 ) {
536 return ; // TODO: What to do?
542 va_start(args, Right);
546 vsnprintf(tmp, lLen+1, Left, args);
550 times = Width/2 - mLen/2 - lLen;
551 while(times--) addch(Pad1);
555 vsnprintf(tmp, mLen+1, Mid, args);
559 times = Width/2 - mLen/2 - rLen;
560 while(times--) addch(Pad2);
564 vsnprintf(tmp, rLen+1, Right, args);
569 // ---------------------
570 // --- Coke Protocol ---
571 // ---------------------
572 int OpenConnection(const char *Host, int Port)
574 struct hostent *host;
575 struct sockaddr_in serverAddr;
578 host = gethostbyname(Host);
580 fprintf(stderr, "Unable to look up '%s'\n", Host);
584 memset(&serverAddr, 0, sizeof(serverAddr));
586 serverAddr.sin_family = AF_INET; // IPv4
587 // NOTE: I have a suspicion that IPv6 will play sillybuggers with this :)
588 serverAddr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]);
589 serverAddr.sin_port = htons(Port);
591 sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
593 fprintf(stderr, "Failed to create socket\n");
599 struct sockaddr_in localAddr;
600 memset(&localAddr, 0, sizeof(localAddr));
601 localAddr.sin_family = AF_INET; // IPv4
602 localAddr.sin_port = 1023; // IPv4
603 // Attempt to bind to low port for autoauth
604 bind(sock, &localAddr, sizeof(localAddr));
608 if( connect(sock, (struct sockaddr *) &serverAddr, sizeof(serverAddr)) < 0 ) {
609 fprintf(stderr, "Failed to connect to server\n");
617 * \brief Authenticate with the server
618 * \return Boolean Failure
620 int Authenticate(int Socket)
627 regmatch_t matches[4];
629 if( gbIsAuthenticated ) return 0;
632 pwd = getpwuid( getuid() );
634 // Attempt automatic authentication
635 sendf(Socket, "AUTOAUTH %s\n", pwd->pw_name);
637 // Check if it worked
638 buf = ReadLine(Socket);
640 responseCode = atoi(buf);
641 switch( responseCode )
644 case 200: // Authenticated, return :)
648 case 401: // Untrusted, attempt password authentication
651 sendf(Socket, "USER %s\n", pwd->pw_name);
652 printf("Using username %s\n", pwd->pw_name);
654 buf = ReadLine(Socket);
657 // Expected format: 100 SALT <something> ...
659 RunRegex(&gSaltRegex, buf, 4, matches, "Malformed server response");
660 responseCode = atoi(buf);
661 if( responseCode != 100 ) {
662 fprintf(stderr, "Unknown repsonse code %i from server\n%s\n", responseCode, buf);
668 if( memcmp( buf+matches[2].rm_so, "SALT", matches[2].rm_eo - matches[2].rm_so) == 0) {
669 // Store it for later
670 memcpy( salt, buf + matches[3].rm_so, matches[3].rm_eo - matches[3].rm_so );
671 salt[ matches[3].rm_eo - matches[3].rm_so ] = 0;
675 // Give three attempts
676 for( i = 0; i < 3; i ++ )
678 int ofs = strlen(pwd->pw_name)+strlen(salt);
681 char *pass = getpass("Password: ");
684 // Create hash string
685 // <username><salt><hash>
686 strcpy(tmp, pwd->pw_name);
688 SHA1( (unsigned char*)pass, strlen(pass), h );
689 memcpy(tmp+ofs, h, 20);
692 SHA1( (unsigned char*)tmp, ofs+20, h );
693 sprintf(tmpBuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
694 h[ 0], h[ 1], h[ 2], h[ 3], h[ 4], h[ 5], h[ 6], h[ 7], h[ 8], h[ 9],
695 h[10], h[11], h[12], h[13], h[14], h[15], h[16], h[17], h[18], h[19]
699 sendf(Socket, "PASS %s\n", tmpBuf);
700 buf = ReadLine(Socket);
702 responseCode = atoi(buf);
704 if( responseCode == 200 ) break;
705 // Bad username/password
706 if( responseCode == 401 ) continue;
708 fprintf(stderr, "Unknown repsonse code %i from server\n%s\n", responseCode, buf);
714 return 2; // 2 = Bad Password
717 case 404: // Bad Username
718 fprintf(stderr, "Bad Username '%s'\n", pwd->pw_name);
723 fprintf(stderr, "Unkown response code %i from server\n", responseCode);
729 // Set effective user
730 if( gsEffectiveUser ) {
731 sendf(Socket, "SETEUSER %s\n", gsEffectiveUser);
733 buf = ReadLine(Socket);
734 responseCode = atoi(buf);
739 printf("Running as '%s' by '%s'\n", gsEffectiveUser, pwd->pw_name);
743 printf("Only coke members can use `dispense -u`\n");
748 printf("Invalid user selected\n");
753 fprintf(stderr, "Unkown response code %i from server\n", responseCode);
762 gbIsAuthenticated = 1;
769 * \brief Fill the item information structure
770 * \return Boolean Failure
772 void PopulateItemList(int Socket)
777 char *itemType, *itemStart;
779 regmatch_t matches[4];
781 // Ask server for stock list
782 send(Socket, "ENUM_ITEMS\n", 11, 0);
783 buf = ReadLine(Socket);
785 //printf("Output: %s\n", buf);
787 responseCode = atoi(buf);
788 if( responseCode != 201 ) {
789 fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
798 RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
800 itemType = &buf[ matches[2].rm_so ]; buf[ matches[2].rm_eo ] = '\0';
801 count = atoi( &buf[ matches[3].rm_so ] );
804 if( strcmp(itemType, "Items") != 0 ) {
806 fprintf(stderr, "Unexpected array type, expected 'Items', got '%s'\n",
811 itemStart = &buf[ matches[3].rm_eo ];
816 gaItems = malloc( giNumItems * sizeof(tItem) );
818 // Fetch item information
819 for( i = 0; i < giNumItems; i ++ )
821 regmatch_t matches[6];
824 buf = ReadLine(Socket);
825 responseCode = atoi(buf);
827 if( responseCode != 202 ) {
828 fprintf(stderr, "Unknown response from dispense server (Response Code %i)\n", responseCode);
832 RunRegex(&gItemRegex, buf, 6, matches, "Malformed server response");
834 buf[ matches[3].rm_eo ] = '\0';
836 gaItems[i].Ident = strdup( buf + matches[3].rm_so );
837 gaItems[i].Price = atoi( buf + matches[4].rm_so );
838 gaItems[i].Desc = strdup( buf + matches[5].rm_so );
844 buf = ReadLine(Socket);
845 responseCode = atoi(buf);
847 if( responseCode != 200 ) {
848 fprintf(stderr, "Unknown response from dispense server %i\n'%s'",
858 * \brief Dispense an item
859 * \return Boolean Failure
861 int DispenseItem(int Socket, int ItemID)
863 int ret, responseCode;
866 if( ItemID < 0 || ItemID > giNumItems ) return -1;
869 sendf(Socket, "DISPENSE %s\n", gaItems[ItemID].Ident);
870 buf = ReadLine(Socket);
872 responseCode = atoi(buf);
873 switch( responseCode )
876 printf("Dispense OK\n");
880 printf("Not authenticated\n");
884 printf("Insufficient balance\n");
888 printf("Bad item name, bug report\n");
892 printf("Item failed to dispense, is the slot empty?\n");
896 printf("Dispense not possible (slot empty/permissions)\n");
900 printf("Unknown response code %i ('%s')\n", responseCode, buf);
910 * \brief Alter a user's balance
912 int Dispense_AlterBalance(int Socket, const char *Username, int Ammount, const char *Reason)
917 sendf(Socket, "ADD %s %i %s\n", Username, Ammount, Reason);
918 buf = ReadLine(Socket);
920 responseCode = atoi(buf);
925 case 200: return 0; // OK
926 case 403: // Not in coke
927 fprintf(stderr, "You are not in coke (sucker)\n");
929 case 404: // Unknown user
930 fprintf(stderr, "Unknown user '%s'\n", Username);
933 fprintf(stderr, "Unknown response code %i\n", responseCode);
941 * \brief Alter a user's balance
943 int Dispense_SetBalance(int Socket, const char *Username, int Ammount, const char *Reason)
948 sendf(Socket, "SET %s %i %s\n", Username, Ammount, Reason);
949 buf = ReadLine(Socket);
951 responseCode = atoi(buf);
956 case 200: return 0; // OK
957 case 403: // Not in coke
958 fprintf(stderr, "You are not in coke (sucker)\n");
960 case 404: // Unknown user
961 fprintf(stderr, "Unknown user '%s'\n", Username);
964 fprintf(stderr, "Unknown response code %i\n", responseCode);
972 * \brief Enumerate users
974 int Dispense_EnumUsers(int Socket)
979 regmatch_t matches[4];
981 if( giMinimumBalance != INT_MIN ) {
982 if( giMaximumBalance != INT_MAX ) {
983 sendf(Socket, "ENUM_USERS %i %i\n", giMinimumBalance, giMaximumBalance);
986 sendf(Socket, "ENUM_USERS %i\n", giMinimumBalance);
990 if( giMaximumBalance != INT_MAX ) {
991 sendf(Socket, "ENUM_USERS - %i\n", giMaximumBalance);
994 sendf(Socket, "ENUM_USERS\n");
997 buf = ReadLine(Socket);
998 responseCode = atoi(buf);
1000 switch(responseCode)
1002 case 201: break; // Ok, length follows
1005 fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
1010 // Get count (not actually used)
1011 RunRegex(&gArrayRegex, buf, 4, matches, "Malformed server response");
1012 nUsers = atoi( buf + matches[3].rm_so );
1013 printf("%i users returned\n", nUsers);
1018 // Read returned users
1020 buf = ReadLine(Socket);
1021 responseCode = atoi(buf);
1023 if( responseCode != 202 ) break;
1025 _PrintUserLine(buf);
1027 } while(responseCode == 202);
1029 // Check final response
1030 if( responseCode != 200 ) {
1031 fprintf(stderr, "Unknown response code %i\n%s\n", responseCode, buf);
1041 int Dispense_ShowUser(int Socket, const char *Username)
1044 int responseCode, ret;
1046 sendf(Socket, "USER_INFO %s\n", Username);
1047 buf = ReadLine(Socket);
1049 responseCode = atoi(buf);
1051 switch(responseCode)
1054 _PrintUserLine(buf);
1059 printf("Unknown user '%s'\n", Username);
1064 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1074 void _PrintUserLine(const char *Line)
1076 regmatch_t matches[6];
1079 RunRegex(&gUserInfoRegex, Line, 6, matches, "Malformed server response");
1084 int usernameLen = matches[3].rm_eo - matches[3].rm_so;
1085 char username[usernameLen + 1];
1086 int flagsLen = matches[5].rm_eo - matches[5].rm_so;
1087 char flags[flagsLen + 1];
1089 memcpy(username, Line + matches[3].rm_so, usernameLen);
1090 username[usernameLen] = '\0';
1091 memcpy(flags, Line + matches[5].rm_so, flagsLen);
1092 flags[flagsLen] = '\0';
1094 bal = atoi(Line + matches[4].rm_so);
1095 printf("%-15s: $%4i.%02i (%s)\n", username, bal/100, bal%100, flags);
1099 int Dispense_AddUser(int Socket, const char *Username)
1102 int responseCode, ret;
1104 sendf(Socket, "USER_ADD %s\n", Username);
1106 buf = ReadLine(Socket);
1107 responseCode = atoi(buf);
1109 switch(responseCode)
1112 printf("User '%s' added\n", Username);
1117 printf("Only wheel can add users\n");
1122 printf("User '%s' already exists\n", Username);
1127 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1137 int Dispense_SetUserType(int Socket, const char *Username, const char *TypeString)
1140 int responseCode, ret;
1142 // TODO: Pre-validate the string
1144 sendf(Socket, "USER_FLAGS %s %s\n", Username, TypeString);
1146 buf = ReadLine(Socket);
1147 responseCode = atoi(buf);
1149 switch(responseCode)
1152 printf("User '%s' updated\n", Username);
1157 printf("Only wheel can modify users\n");
1162 printf("User '%s' does not exist\n", Username);
1167 printf("Flag string is invalid\n");
1172 fprintf(stderr, "Unknown response code %i '%s'\n", responseCode, buf);
1185 char *ReadLine(int Socket)
1187 static char buf[BUFSIZ];
1188 static int bufPos = 0;
1189 static int bufValid = 0;
1191 char *newline = NULL;
1193 char *ret = malloc(10);
1195 #if DEBUG_TRACE_SERVER
1196 printf("ReadLine: ");
1208 len = recv(Socket, buf+bufPos, BUFSIZ-1-bufPos, 0);
1209 buf[bufPos+len] = '\0';
1212 newline = strchr( buf+bufPos, '\n' );
1217 retLen += strlen(buf+bufPos);
1218 ret = realloc(ret, retLen + 1);
1219 strcat( ret, buf+bufPos );
1222 int newLen = newline - (buf+bufPos) + 1;
1223 bufValid = len - newLen;
1226 if( len + bufPos == BUFSIZ - 1 ) bufPos = 0;
1229 #if DEBUG_TRACE_SERVER
1230 printf("%i '%s'\n", retLen, ret);
1236 int sendf(int Socket, const char *Format, ...)
1241 va_start(args, Format);
1242 len = vsnprintf(NULL, 0, Format, args);
1247 va_start(args, Format);
1248 vsnprintf(buf, len+1, Format, args);
1251 #if DEBUG_TRACE_SERVER
1252 printf("sendf: %s", buf);
1255 return send(Socket, buf, len, 0);
1259 char *trim(char *string)
1263 while( isspace(*string) )
1266 for( i = strlen(string); i--; )
1268 if( isspace(string[i]) )
1277 int RunRegex(regex_t *regex, const char *string, int nMatches, regmatch_t *matches, const char *errorMessage)
1281 ret = regexec(regex, string, nMatches, matches, 0);
1283 size_t len = regerror(ret, regex, NULL, 0);
1285 regerror(ret, regex, errorStr, len);
1286 printf("string = '%s'\n", string);
1287 fprintf(stderr, "%s\n%s", errorMessage, errorStr);
1294 void CompileRegex(regex_t *regex, const char *pattern, int flags)
1296 int ret = regcomp(regex, pattern, flags);
1298 size_t len = regerror(ret, regex, NULL, 0);
1300 regerror(ret, regex, errorStr, len);
1301 fprintf(stderr, "Regex compilation failed - %s\n", errorStr);