Fixed crash on completion of long url.
[vimprobable/e.git] / utilities.c
blob2754a74b31e407c671ff00120095707082338d7d
1 /*
2 (c) 2009 by Leon Winter
3 (c) 2009-2011 by Hannes Schueller
4 (c) 2009-2010 by Matto Fransen
5 (c) 2010-2011 by Hans-Peter Deifel
6 (c) 2010-2011 by Thomas Adam
7 see LICENSE file
8 */
10 #include "includes.h"
11 #include "vimprobable.h"
12 #include "main.h"
13 #include "utilities.h"
15 extern char commandhistory[COMMANDHISTSIZE][255];
16 extern Command commands[COMMANDSIZE];
17 extern int lastcommand, maxcommands, commandpointer;
18 extern KeyList *keylistroot;
19 extern Key keys[];
20 extern char *error_msg;
21 extern gboolean complete_case_sensitive;
22 extern char *config_base;
23 static GList *dynamic_searchengines = NULL;
25 gboolean read_rcfile(const char *config)
27 int t;
28 char s[255];
29 FILE *fpin;
30 gboolean returnval = TRUE;
32 if ((fpin = fopen(config, "r")) == NULL)
33 return FALSE;
34 while (fgets(s, 254, fpin)) {
36 * ignore lines that begin with #, / and such
38 if (!isalpha(s[0]))
39 continue;
40 t = strlen(s);
41 s[t - 1] = '\0';
42 if (!process_line(s))
43 returnval = FALSE;
45 fclose(fpin);
46 return returnval;
49 void save_command_history(char *line)
51 char *c;
53 c = line;
54 while (isspace(*c) && *c)
55 c++;
56 if (!strlen(c))
57 return;
58 strncpy(commandhistory[lastcommand], ":", 1);
59 strncat(commandhistory[lastcommand], c, 254);
60 lastcommand++;
61 if (maxcommands < COMMANDHISTSIZE - 1)
62 maxcommands++;
63 if (lastcommand == COMMANDHISTSIZE)
64 lastcommand = 0;
65 commandpointer = lastcommand;
68 gboolean
69 process_save_qmark(const char *bm, WebKitWebView *webview)
71 FILE *fp;
72 const char *filename;
73 const char *uri = webkit_web_view_get_uri(webview);
74 char qmarks[10][101];
75 char buf[100];
76 int i, mark, l=0;
77 Arg a;
78 mark = -1;
79 mark = atoi(bm);
80 if ( mark < 1 || mark > 9 )
82 a.i = Error;
83 a.s = g_strdup_printf("Invalid quickmark, only 1-9");
84 echo(&a);
85 g_free(a.s);
86 return TRUE;
88 if ( uri == NULL ) return FALSE;
89 for( i=0; i < 9; ++i ) strcpy( qmarks[i], "");
91 filename = g_strdup_printf(QUICKMARK_FILE);
93 /* get current quickmarks */
95 fp = fopen(filename, "r");
96 if (fp != NULL){
97 for( i=0; i < 10; ++i ) {
98 if (feof(fp)) {
99 break;
101 fgets(buf, 100, fp);
102 l = 0;
103 while (buf[l] && l < 100 && buf[l] != '\n') {
104 qmarks[i][l]=buf[l];
105 l++;
107 qmarks[i][l]='\0';
109 fclose(fp);
112 /* save quickmarks */
113 strcpy( qmarks[mark-1], uri );
114 fp = fopen(filename, "w");
115 if (fp == NULL) return FALSE;
116 for( i=0; i < 10; ++i )
117 fprintf(fp, "%s\n", qmarks[i]);
118 fclose(fp);
119 a.i = Error;
120 a.s = g_strdup_printf("Saved as quickmark %d: %s", mark, uri);
121 echo(&a);
122 g_free(a.s);
124 return TRUE;
127 void
128 make_keyslist(void)
130 int i;
131 KeyList *ptr, *current;
133 ptr = NULL;
134 current = NULL;
135 i = 0;
136 while ( keys[i].key != 0 )
138 current = malloc(sizeof(KeyList));
139 if (current == NULL) {
140 printf("Not enough memory\n");
141 exit(-1);
143 current->Element = keys[i];
144 current->next = NULL;
145 if (keylistroot == NULL) keylistroot = current;
146 if (ptr != NULL) ptr->next = current;
147 ptr = current;
148 i++;
152 gboolean
153 parse_colour(char *color) {
154 char goodcolor[8];
155 int colorlen;
157 colorlen = (int)strlen(color);
159 goodcolor[0] = '#';
160 goodcolor[7] = '\0';
162 /* help the user a bit by making string like
163 #a10 and strings like ffffff full 6digit
164 strings with # in front :)
167 if (color[0] == '#') {
168 switch (colorlen) {
169 case 7:
170 strncpy(goodcolor, color, 7);
171 break;
172 case 4:
173 goodcolor[1] = color[1];
174 goodcolor[2] = color[1];
175 goodcolor[3] = color[2];
176 goodcolor[4] = color[2];
177 goodcolor[5] = color[3];
178 goodcolor[6] = color[3];
179 break;
180 case 2:
181 goodcolor[1] = color[1];
182 goodcolor[2] = color[1];
183 goodcolor[3] = color[1];
184 goodcolor[4] = color[1];
185 goodcolor[5] = color[1];
186 goodcolor[6] = color[1];
187 break;
189 } else {
190 switch (colorlen) {
191 case 6:
192 strncpy(&goodcolor[1], color, 6);
193 break;
194 case 3:
195 goodcolor[1] = color[0];
196 goodcolor[2] = color[0];
197 goodcolor[3] = color[1];
198 goodcolor[4] = color[1];
199 goodcolor[5] = color[2];
200 goodcolor[6] = color[2];
201 break;
202 case 1:
203 goodcolor[1] = color[0];
204 goodcolor[2] = color[0];
205 goodcolor[3] = color[0];
206 goodcolor[4] = color[0];
207 goodcolor[5] = color[0];
208 goodcolor[6] = color[0];
209 break;
213 if (strlen (goodcolor) != 7) {
214 return FALSE;
215 } else {
216 strncpy(color, goodcolor, 8);
217 return TRUE;
221 gboolean
222 process_line_arg(const Arg *arg) {
223 return process_line(arg->s);
226 gboolean
227 changemapping(Key *search_key, int maprecord, char *cmd) {
228 KeyList *current, *newkey;
229 Arg a = { .s = cmd };
231 /* sanity check */
232 if (maprecord < 0 && cmd == NULL) {
233 /* possible states:
234 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
235 * - maprecord < 0 && cmd != NULL: mapping to command line
236 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
237 * - anything else (gets in here): an error, hence we return FALSE */
238 return FALSE;
241 current = keylistroot;
243 if (current)
244 while (current->next != NULL) {
245 if (
246 current->Element.mask == search_key->mask &&
247 current->Element.modkey == search_key->modkey &&
248 current->Element.key == search_key->key
250 if (maprecord >= 0) {
251 /* mapping to an internal signal */
252 current->Element.func = commands[maprecord].func;
253 current->Element.arg = commands[maprecord].arg;
254 } else {
255 /* mapping to a command line */
256 current->Element.func = process_line_arg;
257 current->Element.arg = a;
259 return TRUE;
261 current = current->next;
263 newkey = malloc(sizeof(KeyList));
264 if (newkey == NULL) {
265 printf("Not enough memory\n");
266 exit (-1);
268 newkey->Element.mask = search_key->mask;
269 newkey->Element.modkey = search_key->modkey;
270 newkey->Element.key = search_key->key;
271 if (maprecord >= 0) {
272 /* mapping to an internal signal */
273 newkey->Element.func = commands[maprecord].func;
274 newkey->Element.arg = commands[maprecord].arg;
275 } else {
276 /* mapping to a command line */
277 newkey->Element.func = process_line_arg;
278 newkey->Element.arg = a;
280 newkey->next = NULL;
282 if (keylistroot == NULL) keylistroot = newkey;
284 if (current != NULL) current->next = newkey;
286 return TRUE;
289 gboolean
290 mappings(const Arg *arg) {
291 char line[255];
293 if (!arg->s) {
294 set_error("Missing argument.");
295 return FALSE;
297 strncpy(line, arg->s, 254);
298 if (process_map_line(line))
299 return TRUE;
300 else {
301 set_error("Invalid mapping.");
302 return FALSE;
306 int
307 get_modkey(char key) {
308 switch (key) {
309 case '1':
310 return GDK_MOD1_MASK;
311 case '2':
312 return GDK_MOD2_MASK;
313 case '3':
314 return GDK_MOD3_MASK;
315 case '4':
316 return GDK_MOD4_MASK;
317 case '5':
318 return GDK_MOD5_MASK;
319 default:
320 return FALSE;
324 gboolean
325 process_mapping(char *keystring, int maprecord, char *cmd) {
326 Key search_key;
328 search_key.mask = 0;
329 search_key.modkey = 0;
330 search_key.key = 0;
332 if (strlen(keystring) == 1) {
333 search_key.key = keystring[0];
336 if (strlen(keystring) == 2) {
337 search_key.modkey= keystring[0];
338 search_key.key = keystring[1];
341 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
342 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
343 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
344 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
346 if (
347 ((strlen(keystring) == 5 || strlen(keystring) == 6) && keystring[0] == '<' && keystring[4] == '>') ||
348 ((strlen(keystring) == 6 || strlen(keystring) == 7) && keystring[0] == '<' && keystring[5] == '>')
350 switch (toupper(keystring[1])) {
351 case 'S':
352 search_key.mask = GDK_SHIFT_MASK;
353 if (strlen(keystring) == 5) {
354 keystring[3] = toupper(keystring[3]);
355 } else {
356 keystring[3] = tolower(keystring[3]);
357 keystring[5] = toupper(keystring[5]);
359 break;
360 case 'C':
361 search_key.mask = GDK_CONTROL_MASK;
362 break;
363 case 'M':
364 search_key.mask = get_modkey(keystring[2]);
365 break;
367 if (!search_key.mask)
368 return FALSE;
369 if (strlen(keystring) == 5) {
370 search_key.key = keystring[3];
371 } else if (strlen(keystring) == 7) {
372 search_key.modkey = keystring[4];
373 search_key.key = keystring[6];
374 } else {
375 if (search_key.mask == 'S' || search_key.mask == 'C') {
376 search_key.modkey = keystring[3];
377 search_key.key = keystring[5];
378 } else {
379 search_key.key = keystring[4];
384 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
385 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
387 if (
388 (strlen(keystring) == 6 && keystring[1] == '<' && keystring[5] == '>') ||
389 (strlen(keystring) == 7 && keystring[1] == '<' && keystring[6] == '>')
391 switch (toupper(keystring[2])) {
392 case 'S':
393 search_key.mask = GDK_SHIFT_MASK;
394 keystring[4] = toupper(keystring[4]);
395 break;
396 case 'C':
397 search_key.mask = GDK_CONTROL_MASK;
398 break;
399 case 'M':
400 search_key.mask = get_modkey(keystring[3]);
401 break;
403 if (!search_key.mask)
404 return FALSE;
405 search_key.modkey= keystring[0];
406 if (strlen(keystring) == 6) {
407 search_key.key = keystring[4];
408 } else {
409 search_key.key = keystring[5];
412 return (changemapping(&search_key, maprecord, cmd));
415 gboolean
416 process_map_line(char *line) {
417 int listlen, i;
418 char *c, *cmd;
419 my_pair.line = line;
420 c = search_word(0);
422 if (!strlen(my_pair.what))
423 return FALSE;
424 while (isspace(*c) && *c)
425 c++;
427 if (*c == ':' || *c == '=')
428 c++;
429 my_pair.line = c;
430 c = search_word(1);
431 if (!strlen(my_pair.value))
432 return FALSE;
433 listlen = LENGTH(commands);
434 for (i = 0; i < listlen; i++) {
435 /* commands is fixed size */
436 if (commands[i].cmd == NULL)
437 break;
438 if (strlen(commands[i].cmd) == strlen(my_pair.value) && strncmp(commands[i].cmd, my_pair.value, strlen(my_pair.value)) == 0) {
439 /* map to an internal symbol */
440 return process_mapping(my_pair.what, i, NULL);
443 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
444 if (strlen(my_pair.value) > 1 && strncmp(my_pair.value, ":", 1) == 0) {
445 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
446 * i.e. increasing the pointer by one will not go 'out of bounds'.
447 * We don't actually check that the command line after the = is valid.
448 * This is user responsibility, the worst case is the new mapping simply doing nothing.
449 * Since we will pass the command to the same function which also handles the config file lines,
450 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
451 * Last, but not least, the second argument being < 0 signifies to the function that this is a
452 * command line mapping, not a mapping to an existing internal symbol. */
453 cmd = (char *)malloc(sizeof(char) * strlen(my_pair.value));
454 strncpy(cmd, (my_pair.value + 1), strlen(my_pair.value) - 1);
455 cmd[strlen(cmd)] = '\0';
456 return process_mapping(my_pair.what, -1, cmd);
458 return FALSE;
461 gboolean
462 build_taglist(const Arg *arg, FILE *f) {
463 int k = 0, in_tag = 0;
464 int t = 0, marker = 0;
465 char foundtab[MAXTAGSIZE+1];
466 while (arg->s[k]) {
467 if (!isspace(arg->s[k]) && !in_tag) {
468 in_tag = 1;
469 marker = k;
471 if (isspace(arg->s[k]) && in_tag) {
472 /* found a tag */
473 t = 0;
474 while (marker < k && t < MAXTAGSIZE) foundtab[t++] = arg->s[marker++];
475 foundtab[t] = '\0';
476 fprintf(f, " [%s]", foundtab);
477 in_tag = 0;
479 k++;
481 if (in_tag) {
482 t = 0;
483 while (marker < strlen(arg->s) && t < MAXTAGSIZE) foundtab[t++] = arg->s[marker++];
484 foundtab[t] = '\0';
485 fprintf(f, " [%s]", foundtab );
487 return TRUE;
490 void
491 set_error(const char *error) {
492 /* it should never happen that set_error is called more than once,
493 * but to avoid any potential memory leaks, we ignore any subsequent
494 * error if the current one has not been shown */
495 if (error_msg == NULL) {
496 error_msg = g_strdup_printf("%s", error);
500 void
501 give_feedback(const char *feedback)
503 Arg a = { .i = Info };
505 a.s = g_strdup_printf("%s", feedback);
506 echo(&a);
507 g_free(a.s);
510 Listelement *
511 complete_list(const char *searchfor, const int mode, Listelement *elementlist)
513 FILE *f;
514 const char *filename;
515 Listelement *candidatelist = NULL, *candidatepointer = NULL;
516 char s[255] = "", readelement[MAXTAGSIZE + 1] = "";
517 int i, t, n = 0;
519 if (mode == 2) {
520 /* open in history file */
521 filename = g_strdup_printf(HISTORY_STORAGE_FILENAME);
522 } else {
523 /* open in bookmark file (for tags and bookmarks) */
524 filename = g_strdup_printf(BOOKMARKS_STORAGE_FILENAME);
526 f = fopen(filename, "r");
527 if (f == NULL) {
528 g_free((gpointer)filename);
529 return (elementlist);
532 while (fgets(s, 254, f)) {
533 if (mode == 1) {
534 /* just tags (could be more than one per line) */
535 i = 0;
536 while (s[i] && i < 254) {
537 while (s[i] != '[' && s[i])
538 i++;
539 if (s[i] != '[')
540 continue;
541 i++;
542 t = 0;
543 while (s[i] != ']' && s[i] && t < MAXTAGSIZE)
544 readelement[t++] = s[i++];
545 readelement[t] = '\0';
546 candidatelist = add_list(readelement, candidatelist);
547 i++;
549 } else {
550 /* complete string (bookmarks & history) */
551 candidatelist = add_list(s, candidatelist);
553 candidatepointer = candidatelist;
554 while (candidatepointer != NULL) {
555 if (!complete_case_sensitive) {
556 g_strdown(candidatepointer->element);
558 if (!strlen(searchfor) || strstr(candidatepointer->element, searchfor) != NULL) {
559 /* only use string up to the first space */
560 memset(readelement, 0, MAXTAGSIZE + 1);
561 if (strchr(candidatepointer->element, ' ') != NULL) {
562 i = strcspn(candidatepointer->element, " ");
563 if (i > MAXTAGSIZE)
564 i = MAXTAGSIZE;
565 strncpy(readelement, candidatepointer->element, i);
566 } else {
567 strncpy(readelement, candidatepointer->element, MAXTAGSIZE);
569 /* in the case of URLs without title, remove the line break */
570 if (readelement[strlen(readelement) - 1] == '\n') {
571 readelement[strlen(readelement) - 1] = '\0';
573 elementlist = add_list(readelement, elementlist);
574 n = count_list(elementlist);
576 if (n >= MAX_LIST_SIZE)
577 break;
578 candidatepointer = candidatepointer->next;
580 free_list(candidatelist);
581 candidatelist = NULL;
582 if (n >= MAX_LIST_SIZE)
583 break;
585 g_free((gpointer)filename);
586 return (elementlist);
589 Listelement *
590 add_list(const char *element, Listelement *elementlist)
592 int n, i = 0;
593 Listelement *newelement, *elementpointer, *lastelement;
595 if (elementlist == NULL) { /* first element */
596 newelement = malloc(sizeof(Listelement));
597 if (newelement == NULL)
598 return (elementlist);
599 strncpy(newelement->element, element, 254);
600 newelement->next = NULL;
601 return newelement;
603 elementpointer = elementlist;
604 n = strlen(element);
606 /* check if element is already in list */
607 while (elementpointer != NULL) {
608 if (strlen(elementpointer->element) == n &&
609 strncmp(elementpointer->element, element, n) == 0)
610 return (elementlist);
611 lastelement = elementpointer;
612 elementpointer = elementpointer->next;
613 i++;
615 /* add to list */
616 newelement = malloc(sizeof(Listelement));
617 if (newelement == NULL)
618 return (elementlist);
619 lastelement->next = newelement;
620 strncpy(newelement->element, element, 254);
621 newelement->next = NULL;
622 return elementlist;
625 void
626 free_list(Listelement *elementlist)
628 Listelement *elementpointer;
630 while (elementlist != NULL) {
631 elementpointer = elementlist->next;
632 free(elementlist);
633 elementlist = elementpointer;
638 count_list(Listelement *elementlist)
640 Listelement *elementpointer = elementlist;
641 int n = 0;
643 while (elementpointer != NULL) {
644 n++;
645 elementpointer = elementpointer->next;
648 return n;
651 /* split the string at the first occurence of whitespace and return the
652 * position of the second half.
653 * Unlike strtok, the substrings can be empty and the second string is
654 * stripped of trailing and leading whitespace.
655 * Return -1 if `string' contains no whitespace */
656 static int split_string_at_whitespace(char *string)
658 int index = strcspn(string, "\n\t ");
659 if (string[index] != '\0') {
660 string[index++] = 0;
661 g_strstrip(string+index);
662 return index;
664 return -1;
667 /* return TRUE, if the string contains exactly one unescaped %s and no other
668 * printf directives */
669 static gboolean sanity_check_search_url(const char *string)
671 int was_percent_char = 0, percent_s_count = 0;
673 for (; *string; string++) {
674 switch (*string) {
675 case '%':
676 was_percent_char = !was_percent_char;
677 break;
678 case 's':
679 if (was_percent_char)
680 percent_s_count++;
681 was_percent_char = 0;
682 break;
683 default:
684 if (was_percent_char)
685 return FALSE;
686 was_percent_char = 0;
687 break;
691 return !was_percent_char && percent_s_count == 1;
694 enum ConfigFileError
695 read_searchengines(const char *filename)
697 FILE *file;
698 char buffer[BUFFERSIZE], c;
699 int linum = 0, index;
700 gboolean found_malformed_lines = FALSE;
701 Searchengine *new;
703 if (access(filename, F_OK) != 0)
704 return FILE_NOT_FOUND;
706 file = fopen(filename, "r");
707 if (file == NULL)
708 return READING_FAILED;
710 while (fgets(buffer, BUFFERSIZE, file)) {
711 linum++;
713 /* skip empty lines */
714 if (!strcmp(buffer, "\n")) continue;
716 /* skip too long lines */
717 if (buffer[strlen(buffer)-1] != '\n') {
718 c = getc(file);
719 if (c != EOF) { /* this is not the last line */
720 while ((c=getc(file)) != EOF && c != '\n');
721 fprintf(stderr, "searchengines: syntax error on line %d\n", linum);
722 found_malformed_lines = TRUE;
723 continue;
727 /* split line at whitespace */
728 index = split_string_at_whitespace(buffer);
730 if (index < 0 || buffer[0] == '\0' || buffer[index] == '\0'
731 || !sanity_check_search_url(buffer+index)) {
732 fprintf(stderr, "searchengines: syntax error on line %d\n", linum);
733 found_malformed_lines = TRUE;
734 continue;
737 new = malloc(sizeof(Searchengine));
738 if (new == NULL) {
739 fprintf(stderr, "Memory exhausted while loading search engines.\n");
740 exit(EXIT_FAILURE);
743 new->handle = g_strdup(buffer);
744 new->uri = g_strdup(buffer+index);
746 dynamic_searchengines = g_list_prepend(dynamic_searchengines, new);
749 if (ferror(file)) {
750 fclose(file);
751 return READING_FAILED;
754 fclose(file);
756 return found_malformed_lines ? SYNTAX_ERROR : SUCCESS;
759 void make_searchengines_list(Searchengine *searchengines, int length)
761 int i;
762 for (i = 0; i < length; i++, searchengines++) {
763 dynamic_searchengines = g_list_prepend(dynamic_searchengines, searchengines);
767 /* find a searchengine with a given handle and return its URI or NULL if
768 * nothing is found.
769 * The returned string is internal and must not be freed or modified. */
770 char *find_uri_for_searchengine(const char *handle)
772 GList *l;
774 if (dynamic_searchengines != NULL) {
775 for (l = dynamic_searchengines; l; l = g_list_next(l)) {
776 Searchengine *s = (Searchengine*)l->data;
777 if (!strcmp(s->handle, handle)) {
778 return s->uri;
783 return NULL;