- Timestamp:
- 2015-02-20T22:50:54Z (9 years ago)
- Branches:
- master
- Children:
- 0b9daac, 3d45471, 7733b8c
- Parents:
- af359b4
- git-author:
- Indent <please@…> (19-02-15 05:47:20)
- git-committer:
- dequis <dx@…> (20-02-15 22:50:54)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
help.c
raf359b4 r5ebff60 1 1 /********************************************************************\ 2 2 * BitlBee -- An IRC to other IM-networks gateway * 3 3 * * … … 27 27 #include "bitlbee.h" 28 28 #include "help.h" 29 #undef read 29 #undef read 30 30 #undef write 31 31 32 32 #define BUFSIZE 1100 33 33 34 help_t *help_init( help_t **help, const char *helpfile)34 help_t *help_init(help_t **help, const char *helpfile) 35 35 { 36 36 int i, buflen = 0; … … 39 39 time_t mtime; 40 40 struct stat stat[1]; 41 42 *help = h = g_new0 ( help_t, 1 ); 43 44 h->fd = open( helpfile, O_RDONLY ); 45 46 if( h->fd == -1 ) 47 { 48 g_free( h ); 49 return( *help = NULL ); 50 } 51 52 if( fstat( h->fd, stat ) != 0 ) 53 { 54 g_free( h ); 55 return( *help = NULL ); 41 42 *help = h = g_new0(help_t, 1); 43 44 h->fd = open(helpfile, O_RDONLY); 45 46 if (h->fd == -1) { 47 g_free(h); 48 return(*help = NULL); 49 } 50 51 if (fstat(h->fd, stat) != 0) { 52 g_free(h); 53 return(*help = NULL); 56 54 } 57 55 mtime = stat->st_mtime; 58 59 s = g_new (char, BUFSIZE + 1);56 57 s = g_new(char, BUFSIZE + 1); 60 58 s[BUFSIZE] = 0; 61 62 while( ( ( i = read( h->fd, s + buflen, BUFSIZE - buflen ) ) > 0 ) || 63 ( i == 0 && strstr( s, "\n%\n" ) ) ) 64 { 59 60 while (((i = read(h->fd, s + buflen, BUFSIZE - buflen)) > 0) || 61 (i == 0 && strstr(s, "\n%\n"))) { 65 62 buflen += i; 66 memset( s + buflen, 0, BUFSIZE - buflen ); 67 if( !( t = strstr( s, "\n%\n" ) ) || s[0] != '?' ) 68 { 63 memset(s + buflen, 0, BUFSIZE - buflen); 64 if (!(t = strstr(s, "\n%\n")) || s[0] != '?') { 69 65 /* FIXME: Clean up */ 70 help_free( help);71 g_free( s);66 help_free(help); 67 g_free(s); 72 68 return NULL; 73 69 } 74 i = strchr( s, '\n' ) - s; 75 76 if( h->title ) 77 { 78 h = h->next = g_new0( help_t, 1 ); 79 } 80 h->title = g_new ( char, i ); 81 82 strncpy( h->title, s + 1, i - 1 ); 83 h->title[i-1] = 0; 70 i = strchr(s, '\n') - s; 71 72 if (h->title) { 73 h = h->next = g_new0(help_t, 1); 74 } 75 h->title = g_new(char, i); 76 77 strncpy(h->title, s + 1, i - 1); 78 h->title[i - 1] = 0; 84 79 h->fd = (*help)->fd; 85 h->offset.file_offset = lseek( h->fd, 0, SEEK_CUR) - buflen + i + 1;80 h->offset.file_offset = lseek(h->fd, 0, SEEK_CUR) - buflen + i + 1; 86 81 h->length = t - s - i - 1; 87 82 h->mtime = mtime; 88 89 buflen -= ( t + 3 - s);90 t = g_strdup( t + 3);91 g_free( s);92 s = g_renew( char, t, BUFSIZE + 1);83 84 buflen -= (t + 3 - s); 85 t = g_strdup(t + 3); 86 g_free(s); 87 s = g_renew(char, t, BUFSIZE + 1); 93 88 s[BUFSIZE] = 0; 94 89 } 95 96 g_free( s);97 98 return( *help);99 } 100 101 void help_free( help_t **help)90 91 g_free(s); 92 93 return(*help); 94 } 95 96 void help_free(help_t **help) 102 97 { 103 98 help_t *h, *oh; 104 99 int last_fd = -1; /* Weak de-dupe */ 105 106 if ( help == NULL || *help == NULL )100 101 if (help == NULL || *help == NULL) { 107 102 return; 108 103 } 104 109 105 h = *help; 110 while( h ) 111 { 112 if( h->fd != last_fd ) 113 { 114 close( h->fd ); 106 while (h) { 107 if (h->fd != last_fd) { 108 close(h->fd); 115 109 last_fd = h->fd; 116 110 } 117 g_free( h->title);118 h = (oh =h)->next;119 g_free( oh);120 } 121 111 g_free(h->title); 112 h = (oh = h)->next; 113 g_free(oh); 114 } 115 122 116 *help = NULL; 123 117 } 124 118 125 char *help_get( help_t **help, char *title)119 char *help_get(help_t **help, char *title) 126 120 { 127 121 time_t mtime; … … 129 123 help_t *h; 130 124 131 for( h = *help; h; h = h->next ) 132 { 133 if( h->title != NULL && g_strcasecmp( h->title, title ) == 0 ) 125 for (h = *help; h; h = h->next) { 126 if (h->title != NULL && g_strcasecmp(h->title, title) == 0) { 134 127 break; 135 }136 if( h && h->length > 0 )137 {138 char *s = g_new( char, h->length + 1);139 128 } 129 } 130 if (h && h->length > 0) { 131 char *s = g_new(char, h->length + 1); 132 140 133 s[h->length] = 0; 141 if( h->fd >= 0 ) 142 { 143 if( fstat( h->fd, stat ) != 0 ) 144 { 145 g_free( s ); 134 if (h->fd >= 0) { 135 if (fstat(h->fd, stat) != 0) { 136 g_free(s); 146 137 return NULL; 147 138 } 148 139 mtime = stat->st_mtime; 149 150 if( mtime > h->mtime ) 151 { 152 g_free( s ); 140 141 if (mtime > h->mtime) { 142 g_free(s); 153 143 return NULL; 154 144 } 155 156 if ( lseek( h->fd, h->offset.file_offset, SEEK_SET) == -1 ||157 read( h->fd, s, h->length ) != h->length )145 146 if (lseek(h->fd, h->offset.file_offset, SEEK_SET) == -1 || 147 read(h->fd, s, h->length) != h->length) { 158 148 return NULL; 159 } 160 else 161 { 162 strncpy( s, h->offset.mem_offset, h->length ); 149 } 150 } else { 151 strncpy(s, h->offset.mem_offset, h->length); 163 152 } 164 153 return s; 165 154 } 166 155 167 156 return NULL; 168 157 } 169 158 170 int help_add_mem( help_t **help, const char *title, const char *content)159 int help_add_mem(help_t **help, const char *title, const char *content) 171 160 { 172 161 help_t *h, *l = NULL; 173 174 for( h = *help; h; h = h->next ) 175 { 176 if( g_strcasecmp( h->title, title ) == 0 ) 162 163 for (h = *help; h; h = h->next) { 164 if (g_strcasecmp(h->title, title) == 0) { 177 165 return 0; 178 166 } 167 179 168 l = h; 180 169 } 181 182 if( l ) 183 h = l->next = g_new0( struct help, 1 ); 184 else 185 *help = h = g_new0( struct help, 1 ); 170 171 if (l) { 172 h = l->next = g_new0(struct help, 1); 173 } else { 174 *help = h = g_new0(struct help, 1); 175 } 186 176 h->fd = -1; 187 h->title = g_strdup( title);188 h->length = strlen( content);189 h->offset.mem_offset = g_strdup( content);190 177 h->title = g_strdup(title); 178 h->length = strlen(content); 179 h->offset.mem_offset = g_strdup(content); 180 191 181 return 1; 192 182 } 193 183 194 char *help_get_whatsnew( help_t **help, int old)184 char *help_get_whatsnew(help_t **help, int old) 195 185 { 196 186 GString *ret = NULL; 197 187 help_t *h; 198 188 int v; 199 200 for( h = *help; h; h = h->next ) 201 if( h->title != NULL && strncmp( h->title, "whatsnew", 8 ) == 0 && 202 sscanf( h->title + 8, "%x", &v ) == 1 && v > old ) 203 { 204 char *s = help_get( &h, h->title ); 205 if( ret == NULL ) 206 ret = g_string_new( s ); 207 else 208 g_string_append_printf( ret, "\n\n%s", s ); 209 g_free( s ); 210 } 211 212 return ret ? g_string_free( ret, FALSE ) : NULL; 213 } 189 190 for (h = *help; h; h = h->next) { 191 if (h->title != NULL && strncmp(h->title, "whatsnew", 8) == 0 && 192 sscanf(h->title + 8, "%x", &v) == 1 && v > old) { 193 char *s = help_get(&h, h->title); 194 if (ret == NULL) { 195 ret = g_string_new(s); 196 } else { 197 g_string_append_printf(ret, "\n\n%s", s); 198 } 199 g_free(s); 200 } 201 } 202 203 return ret ? g_string_free(ret, FALSE) : NULL; 204 }
Note: See TracChangeset
for help on using the changeset viewer.