2 (c) 2009 by Leon Winter
3 (c) 2009-2012 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
11 #include "vimprobable.h"
13 #include "utilities.h"
15 extern GList
*commandhistory
;
16 extern int commandpointer
;
17 extern Command commands
[COMMANDSIZE
];
18 extern KeyList
*keylistroot
;
20 extern char *error_msg
;
21 extern gboolean complete_case_sensitive
;
22 extern char *config_base
;
23 static GList
*dynamic_searchengines
= NULL
, *dynamic_uri_handlers
= NULL
;
25 void add_modkeys(char key
);
27 void save_command_history(char *line
)
31 while (isspace(*c
) && *c
)
36 if (COMMANDHISTSIZE
<= g_list_length(commandhistory
)) {
37 /* if list is too long - remove items from beginning */
38 commandhistory
= g_list_delete_link(commandhistory
, g_list_first(commandhistory
));
40 commandhistory
= g_list_append(commandhistory
, g_strdup(c
));
44 process_save_qmark(const char *bm
, WebKitWebView
*webview
)
48 const char *uri
= webkit_web_view_get_uri(webview
);
54 if ( mark
< 1 || mark
> 9 ) {
55 echo_message(Error
, "Invalid quickmark, only 1-9");
58 if ( uri
== NULL
) return FALSE
;
59 for( i
=0; i
< 9; ++i
) strcpy( qmarks
[i
], "");
61 filename
= g_strdup_printf(QUICKMARK_FILE
);
63 /* get current quickmarks */
65 fp
= fopen(filename
, "r");
67 for( i
=0; i
< 10; ++i
) {
73 while (buf
[l
] && l
< 100 && buf
[l
] != '\n') {
83 strcpy( qmarks
[mark
-1], uri
);
84 fp
= fopen(filename
, "w");
85 g_free((gpointer
*)filename
);
86 if (fp
== NULL
) return FALSE
;
87 for( i
=0; i
< 10; ++i
)
88 fprintf(fp
, "%s\n", qmarks
[i
]);
90 echo_message(Error
, "Saved as quickmark %d: %s", mark
, uri
);
99 KeyList
*ptr
, *current
;
104 while ( keys
[i
].key
!= 0 )
106 current
= malloc(sizeof(KeyList
));
107 if (current
== NULL
) {
108 printf("Not enough memory\n");
111 current
->Element
= keys
[i
];
112 current
->next
= NULL
;
113 if (keylistroot
== NULL
) keylistroot
= current
;
114 if (ptr
!= NULL
) ptr
->next
= current
;
121 parse_colour(char *color
) {
125 colorlen
= (int)strlen(color
);
130 /* help the user a bit by making string like
131 #a10 and strings like ffffff full 6digit
132 strings with # in front :)
135 if (color
[0] == '#') {
138 strncpy(goodcolor
, color
, 7);
141 goodcolor
[1] = color
[1];
142 goodcolor
[2] = color
[1];
143 goodcolor
[3] = color
[2];
144 goodcolor
[4] = color
[2];
145 goodcolor
[5] = color
[3];
146 goodcolor
[6] = color
[3];
149 goodcolor
[1] = color
[1];
150 goodcolor
[2] = color
[1];
151 goodcolor
[3] = color
[1];
152 goodcolor
[4] = color
[1];
153 goodcolor
[5] = color
[1];
154 goodcolor
[6] = color
[1];
160 strncpy(&goodcolor
[1], color
, 6);
163 goodcolor
[1] = color
[0];
164 goodcolor
[2] = color
[0];
165 goodcolor
[3] = color
[1];
166 goodcolor
[4] = color
[1];
167 goodcolor
[5] = color
[2];
168 goodcolor
[6] = color
[2];
171 goodcolor
[1] = color
[0];
172 goodcolor
[2] = color
[0];
173 goodcolor
[3] = color
[0];
174 goodcolor
[4] = color
[0];
175 goodcolor
[5] = color
[0];
176 goodcolor
[6] = color
[0];
181 if (strlen (goodcolor
) != 7) {
184 strncpy(color
, goodcolor
, 8);
190 process_line_arg(const Arg
*arg
) {
191 return process_line(arg
->s
);
195 changemapping(Key
*search_key
, int maprecord
, char *cmd
) {
196 KeyList
*current
, *newkey
;
197 Arg a
= { .s
= cmd
};
200 if (maprecord
< 0 && cmd
== NULL
) {
202 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
203 * - maprecord < 0 && cmd != NULL: mapping to command line
204 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
205 * - anything else (gets in here): an error, hence we return FALSE */
209 current
= keylistroot
;
212 while (current
->next
!= NULL
) {
214 current
->Element
.mask
== search_key
->mask
&&
215 current
->Element
.modkey
== search_key
->modkey
&&
216 current
->Element
.key
== search_key
->key
218 if (maprecord
>= 0) {
219 /* mapping to an internal signal */
220 current
->Element
.func
= commands
[maprecord
].func
;
221 current
->Element
.arg
= commands
[maprecord
].arg
;
223 /* mapping to a command line */
224 current
->Element
.func
= process_line_arg
;
225 current
->Element
.arg
= a
;
229 current
= current
->next
;
231 newkey
= malloc(sizeof(KeyList
));
232 if (newkey
== NULL
) {
233 printf("Not enough memory\n");
236 newkey
->Element
.mask
= search_key
->mask
;
237 newkey
->Element
.modkey
= search_key
->modkey
;
238 newkey
->Element
.key
= search_key
->key
;
239 if (maprecord
>= 0) {
240 /* mapping to an internal signal */
241 newkey
->Element
.func
= commands
[maprecord
].func
;
242 newkey
->Element
.arg
= commands
[maprecord
].arg
;
244 /* mapping to a command line */
245 newkey
->Element
.func
= process_line_arg
;
246 newkey
->Element
.arg
= a
;
248 add_modkeys(newkey
->Element
.modkey
);
252 if (keylistroot
== NULL
) keylistroot
= newkey
;
254 if (current
!= NULL
) current
->next
= newkey
;
259 void add_modkeys(char key
)
262 extern char *modkeys
;
263 len
= strlen( modkeys
);
265 if ( modkeys
[k
] == key
) return;
268 modkeys
= realloc(modkeys
, len
+ 2);
270 modkeys
[len
+1] = '\0';
274 mappings(const Arg
*arg
) {
278 set_error("Missing argument.");
281 strncpy(line
, arg
->s
, 254);
282 if (process_map_line(line
))
285 set_error("Invalid mapping.");
291 get_modkey(char key
) {
294 return GDK_MOD1_MASK
;
296 return GDK_MOD2_MASK
;
298 return GDK_MOD3_MASK
;
300 return GDK_MOD4_MASK
;
302 return GDK_MOD5_MASK
;
309 process_mapping(char *keystring
, int maprecord
, char *cmd
) {
313 search_key
.modkey
= 0;
316 if (strlen(keystring
) == 1) {
317 search_key
.key
= keystring
[0];
320 if (strlen(keystring
) == 2) {
321 search_key
.modkey
= keystring
[0];
322 search_key
.key
= keystring
[1];
325 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
326 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
327 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
328 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
331 ((strlen(keystring
) == 5 || strlen(keystring
) == 6) && keystring
[0] == '<' && keystring
[4] == '>') ||
332 ((strlen(keystring
) == 6 || strlen(keystring
) == 7) && keystring
[0] == '<' && keystring
[5] == '>')
334 switch (toupper(keystring
[1])) {
336 search_key
.mask
= GDK_SHIFT_MASK
;
337 if (strlen(keystring
) == 5) {
338 keystring
[3] = toupper(keystring
[3]);
340 keystring
[3] = tolower(keystring
[3]);
341 keystring
[5] = toupper(keystring
[5]);
345 search_key
.mask
= GDK_CONTROL_MASK
;
348 search_key
.mask
= get_modkey(keystring
[2]);
351 if (!search_key
.mask
)
353 if (strlen(keystring
) == 5) {
354 search_key
.key
= keystring
[3];
355 } else if (strlen(keystring
) == 7) {
356 search_key
.modkey
= keystring
[4];
357 search_key
.key
= keystring
[6];
359 if (search_key
.mask
== 'S' || search_key
.mask
== 'C') {
360 search_key
.modkey
= keystring
[3];
361 search_key
.key
= keystring
[5];
363 search_key
.key
= keystring
[4];
368 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
369 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
372 (strlen(keystring
) == 6 && keystring
[1] == '<' && keystring
[5] == '>') ||
373 (strlen(keystring
) == 7 && keystring
[1] == '<' && keystring
[6] == '>')
375 switch (toupper(keystring
[2])) {
377 search_key
.mask
= GDK_SHIFT_MASK
;
378 keystring
[4] = toupper(keystring
[4]);
381 search_key
.mask
= GDK_CONTROL_MASK
;
384 search_key
.mask
= get_modkey(keystring
[3]);
387 if (!search_key
.mask
)
389 search_key
.modkey
= keystring
[0];
390 if (strlen(keystring
) == 6) {
391 search_key
.key
= keystring
[4];
393 search_key
.key
= keystring
[5];
396 return (changemapping(&search_key
, maprecord
, cmd
));
400 process_map_line(char *line
) {
406 if (!strlen(my_pair
.what
))
408 while (isspace(*c
) && *c
)
411 if (*c
== ':' || *c
== '=')
415 if (!strlen(my_pair
.value
))
417 listlen
= LENGTH(commands
);
418 for (i
= 0; i
< listlen
; i
++) {
419 /* commands is fixed size */
420 if (commands
[i
].cmd
== NULL
)
422 if (strlen(commands
[i
].cmd
) == strlen(my_pair
.value
) && strncmp(commands
[i
].cmd
, my_pair
.value
, strlen(my_pair
.value
)) == 0) {
423 /* map to an internal symbol */
424 return process_mapping(my_pair
.what
, i
, NULL
);
427 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
428 if (strlen(my_pair
.value
) > 1 && strncmp(my_pair
.value
, ":", 1) == 0) {
429 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
430 * i.e. increasing the pointer by one will not go 'out of bounds'.
431 * We don't actually check that the command line after the = is valid.
432 * This is user responsibility, the worst case is the new mapping simply doing nothing.
433 * Since we will pass the command to the same function which also handles the config file lines,
434 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
435 * Last, but not least, the second argument being < 0 signifies to the function that this is a
436 * command line mapping, not a mapping to an existing internal symbol. */
437 cmd
= (char *)malloc(sizeof(char) * strlen(my_pair
.value
));
438 memset(cmd
, 0, strlen(my_pair
.value
));
439 strncpy(cmd
, (my_pair
.value
+ 1), strlen(my_pair
.value
) - 1);
440 return process_mapping(my_pair
.what
, -1, cmd
);
446 build_taglist(const Arg
*arg
, FILE *f
) {
447 int k
= 0, in_tag
= 0;
448 int t
= 0, marker
= 0;
449 char foundtab
[MAXTAGSIZE
+1];
451 if (!isspace(arg
->s
[k
]) && !in_tag
) {
455 if (isspace(arg
->s
[k
]) && in_tag
) {
458 while (marker
< k
&& t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
460 fprintf(f
, " [%s]", foundtab
);
467 while (marker
< strlen(arg
->s
) && t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
469 fprintf(f
, " [%s]", foundtab
);
475 set_error(const char *error
) {
476 /* it should never happen that set_error is called more than once,
477 * but to avoid any potential memory leaks, we ignore any subsequent
478 * error if the current one has not been shown */
479 if (error_msg
== NULL
) {
480 error_msg
= g_strdup_printf("%s", error
);
485 echo_message(const MessageType type
, const char *format
, ...)
490 va_start(ap
, format
);
492 a
.s
= g_strdup_vprintf(format
, ap
);
499 complete_list(const char *searchfor
, const int mode
, Listelement
*elementlist
)
502 const char *filename
;
503 Listelement
*candidatelist
= NULL
, *candidatepointer
= NULL
;
504 char s
[255] = "", readelement
[MAXTAGSIZE
+ 1] = "";
508 /* open in history file */
509 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
511 /* open in bookmark file (for tags and bookmarks) */
512 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
514 f
= fopen(filename
, "r");
516 g_free((gpointer
*)filename
);
517 return (elementlist
);
520 while (fgets(s
, 254, f
)) {
522 /* just tags (could be more than one per line) */
524 while (s
[i
] && i
< 254) {
525 while (s
[i
] != '[' && s
[i
])
531 while (s
[i
] != ']' && s
[i
] && t
< MAXTAGSIZE
)
532 readelement
[t
++] = s
[i
++];
533 readelement
[t
] = '\0';
534 candidatelist
= add_list(readelement
, candidatelist
);
538 /* complete string (bookmarks & history) */
539 candidatelist
= add_list(s
, candidatelist
);
541 candidatepointer
= candidatelist
;
542 while (candidatepointer
!= NULL
) {
543 strncpy(s
, candidatepointer
->element
, sizeof(s
));
544 if (!complete_case_sensitive
) {
547 if (!strlen(searchfor
) || strstr(s
, searchfor
) != NULL
) {
548 /* only use string up to the first space */
549 memset(readelement
, 0, MAXTAGSIZE
+ 1);
550 if (strchr(candidatepointer
->element
, ' ') != NULL
) {
551 i
= strcspn(candidatepointer
->element
, " ");
554 strncpy(readelement
, candidatepointer
->element
, i
);
556 strncpy(readelement
, candidatepointer
->element
, MAXTAGSIZE
);
558 /* in the case of URLs without title, remove the line break */
559 if (readelement
[strlen(readelement
) - 1] == '\n') {
560 readelement
[strlen(readelement
) - 1] = '\0';
562 elementlist
= add_list(readelement
, elementlist
);
563 n
= count_list(elementlist
);
565 if (n
>= MAX_LIST_SIZE
)
567 candidatepointer
= candidatepointer
->next
;
569 free_list(candidatelist
);
570 candidatelist
= NULL
;
571 if (n
>= MAX_LIST_SIZE
)
574 g_free((gpointer
)filename
);
575 return (elementlist
);
579 add_list(const char *element
, Listelement
*elementlist
)
582 Listelement
*newelement
, *elementpointer
, *lastelement
;
584 if (elementlist
== NULL
) { /* first element */
585 newelement
= malloc(sizeof(Listelement
));
586 if (newelement
== NULL
)
587 return (elementlist
);
588 strncpy(newelement
->element
, element
, 254);
589 newelement
->next
= NULL
;
592 elementpointer
= elementlist
;
595 /* check if element is already in list */
596 while (elementpointer
!= NULL
) {
597 if (strlen(elementpointer
->element
) == n
&&
598 strncmp(elementpointer
->element
, element
, n
) == 0)
599 return (elementlist
);
600 lastelement
= elementpointer
;
601 elementpointer
= elementpointer
->next
;
605 newelement
= malloc(sizeof(Listelement
));
606 if (newelement
== NULL
)
607 return (elementlist
);
608 lastelement
->next
= newelement
;
609 strncpy(newelement
->element
, element
, 254);
610 newelement
->next
= NULL
;
615 free_list(Listelement
*elementlist
)
617 Listelement
*elementpointer
;
619 while (elementlist
!= NULL
) {
620 elementpointer
= elementlist
->next
;
622 elementlist
= elementpointer
;
627 count_list(Listelement
*elementlist
)
629 Listelement
*elementpointer
= elementlist
;
632 while (elementpointer
!= NULL
) {
634 elementpointer
= elementpointer
->next
;
640 /* split the string at the first occurence of whitespace and return the
641 * position of the second half.
642 * Unlike strtok, the substrings can be empty and the second string is
643 * stripped of trailing and leading whitespace.
644 * Return -1 if `string' contains no whitespace */
645 static int split_string_at_whitespace(char *string
)
647 int index
= strcspn(string
, "\n\t ");
648 if (string
[index
] != '\0') {
650 g_strstrip(string
+index
);
656 /* return TRUE, if the string contains exactly one unescaped %s and no other
657 * printf directives */
658 static gboolean
sanity_check_search_url(const char *string
)
660 int was_percent_char
= 0, percent_s_count
= 0;
662 for (; *string
; string
++) {
665 was_percent_char
= !was_percent_char
;
668 if (was_percent_char
)
670 was_percent_char
= 0;
673 if (was_percent_char
)
675 was_percent_char
= 0;
680 return !was_percent_char
&& percent_s_count
== 1;
683 void make_searchengines_list(Searchengine
*searchengines
, int length
)
686 for (i
= 0; i
< length
; i
++, searchengines
++) {
687 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, searchengines
);
691 /* find a searchengine with a given handle and return its URI or NULL if
693 * The returned string is internal and must not be freed or modified. */
694 char *find_uri_for_searchengine(const char *handle
)
698 if (dynamic_searchengines
!= NULL
) {
699 for (l
= dynamic_searchengines
; l
; l
= g_list_next(l
)) {
700 Searchengine
*s
= (Searchengine
*)l
->data
;
701 if (!strcmp(s
->handle
, handle
)) {
711 read_rcfile(const char *config
)
713 int t
, linum
= 0, index
;
714 char s
[255], *buffer
;
716 gboolean found_malformed_lines
= FALSE
;
718 URIHandler
*newhandler
;
720 if (access(config
, F_OK
) != 0)
721 return FILE_NOT_FOUND
;
723 fpin
= fopen(config
, "r");
725 return READING_FAILED
;
727 while (fgets(s
, 254, fpin
)) {
730 * ignore lines that begin with #, / and such
736 if (strncmp(s
, "searchengine", 12) == 0) {
738 while (buffer
[0] == ' ')
740 /* split line at whitespace */
741 index
= split_string_at_whitespace(buffer
);
742 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
743 || !sanity_check_search_url(buffer
+index
)) {
744 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
745 found_malformed_lines
= TRUE
;
748 new = malloc(sizeof(Searchengine
));
750 fprintf(stderr
, "Memory exhausted while loading search engines.\n");
753 new->handle
= g_strdup(buffer
);
754 new->uri
= g_strdup(buffer
+index
);
755 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, new);
756 } else if (strncmp(s
, "handler", 7) == 0) {
758 while (buffer
[0] == ' ')
760 /* split line at whitespace */
761 index
= split_string_at_whitespace(buffer
);
762 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
763 || !sanity_check_search_url(buffer
+index
)) { /* this sanity check is also valid for handler definitions */
764 fprintf(stderr
, "URI handlers: syntax error on line %d\n", linum
);
765 found_malformed_lines
= TRUE
;
768 newhandler
= malloc(sizeof(URIHandler
));
769 if (newhandler
== NULL
) {
770 fprintf(stderr
, "Memory exhausted while loading uri handlers.\n");
773 newhandler
->handle
= g_strdup(buffer
);
774 newhandler
->handler
= g_strdup(buffer
+index
);
775 dynamic_uri_handlers
= g_list_prepend(dynamic_uri_handlers
, newhandler
);
777 if (!process_line(s
))
778 found_malformed_lines
= TRUE
;
782 return found_malformed_lines
? SYNTAX_ERROR
: SUCCESS
;
785 void make_uri_handlers_list(URIHandler
*uri_handlers
, int length
)
788 for (i
= 0; i
< length
; i
++, uri_handlers
++) {
789 dynamic_uri_handlers
= g_list_prepend(dynamic_uri_handlers
, uri_handlers
);
794 /* spawn a child process handling a protocol encoded in URI.
796 On success, pid will contain the pid of the spawned child.
797 If you pass NULL as child_pid, glib will reap the child. */
799 open_handler_pid(char *uri
, GPid
*child_pid
) {
801 char *p
= NULL
, *arg
, arg_temp
[MAX_SETTING_SIZE
], *temp
, temp2
[MAX_SETTING_SIZE
] = "", *temp3
;
804 GSpawnFlags flags
= G_SPAWN_SEARCH_PATH
;
807 flags
|= G_SPAWN_DO_NOT_REAP_CHILD
;
809 p
= strchr(uri
, ':');
811 if (dynamic_uri_handlers
!= NULL
) {
812 for (l
= dynamic_uri_handlers
; l
; l
= g_list_next(l
)) {
813 URIHandler
*s
= (URIHandler
*)l
->data
;
814 if (strlen(uri
) >= strlen(s
->handle
) && strncmp(s
->handle
, uri
, strlen(s
->handle
)) == 0) {
815 if (strlen(s
->handler
) > 0) {
816 arg
= (uri
+ strlen(s
->handle
));
817 strncpy(temp2
, s
->handler
, MAX_SETTING_SIZE
);
818 temp
= strtok(temp2
, " ");
820 while (temp
!= NULL
) {
821 if (strstr(temp
, "%s")) {
823 memset(arg_temp
, 0, MAX_SETTING_SIZE
);
824 while (strncmp(temp3
, "%s", 2) != 0) {
825 strncat(arg_temp
, temp3
, 1);
828 strcat(arg_temp
, arg
);
831 strcat(arg_temp
, temp3
);
836 temp
= strtok(NULL
, " ");
840 g_spawn_async(NULL
, argv
, NULL
, flags
,
841 NULL
, NULL
, child_pid
, NULL
);
853 open_handler(char *uri
) {
854 return open_handler_pid(uri
, NULL
);