]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/locale/keymap-util.c
basic: add new helper call empty_or_dash_to_null()
[thirdparty/systemd.git] / src / locale / keymap-util.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <stdio_ext.h>
5 #include <string.h>
6 #include <sys/stat.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 #include "bus-util.h"
11 #include "env-file-label.h"
12 #include "env-file.h"
13 #include "env-util.h"
14 #include "fd-util.h"
15 #include "fileio-label.h"
16 #include "fileio.h"
17 #include "kbd-util.h"
18 #include "keymap-util.h"
19 #include "locale-util.h"
20 #include "macro.h"
21 #include "mkdir.h"
22 #include "nulstr-util.h"
23 #include "string-util.h"
24 #include "strv.h"
25 #include "tmpfile-util.h"
26
27 static bool startswith_comma(const char *s, const char *prefix) {
28 s = startswith(s, prefix);
29 if (!s)
30 return false;
31
32 return IN_SET(*s, ',', '\0');
33 }
34
35 static const char* systemd_kbd_model_map(void) {
36 const char* s;
37
38 s = getenv("SYSTEMD_KBD_MODEL_MAP");
39 if (s)
40 return s;
41
42 return SYSTEMD_KBD_MODEL_MAP;
43 }
44
45 static const char* systemd_language_fallback_map(void) {
46 const char* s;
47
48 s = getenv("SYSTEMD_LANGUAGE_FALLBACK_MAP");
49 if (s)
50 return s;
51
52 return SYSTEMD_LANGUAGE_FALLBACK_MAP;
53 }
54
55 static void context_free_x11(Context *c) {
56 c->x11_layout = mfree(c->x11_layout);
57 c->x11_options = mfree(c->x11_options);
58 c->x11_model = mfree(c->x11_model);
59 c->x11_variant = mfree(c->x11_variant);
60 }
61
62 static void context_free_vconsole(Context *c) {
63 c->vc_keymap = mfree(c->vc_keymap);
64 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
65 }
66
67 static void context_free_locale(Context *c) {
68 int p;
69
70 for (p = 0; p < _VARIABLE_LC_MAX; p++)
71 c->locale[p] = mfree(c->locale[p]);
72 }
73
74 void context_clear(Context *c) {
75 context_free_locale(c);
76 context_free_x11(c);
77 context_free_vconsole(c);
78
79 sd_bus_message_unref(c->locale_cache);
80 sd_bus_message_unref(c->x11_cache);
81 sd_bus_message_unref(c->vc_cache);
82
83 bus_verify_polkit_async_registry_free(c->polkit_registry);
84 };
85
86 void locale_simplify(char *locale[_VARIABLE_LC_MAX]) {
87 int p;
88
89 for (p = VARIABLE_LANG+1; p < _VARIABLE_LC_MAX; p++)
90 if (isempty(locale[p]) || streq_ptr(locale[VARIABLE_LANG], locale[p]))
91 locale[p] = mfree(locale[p]);
92 }
93
94 int locale_read_data(Context *c, sd_bus_message *m) {
95 struct stat st;
96 int r;
97
98 /* Do not try to re-read the file within single bus operation. */
99 if (m) {
100 if (m == c->locale_cache)
101 return 0;
102
103 sd_bus_message_unref(c->locale_cache);
104 c->locale_cache = sd_bus_message_ref(m);
105 }
106
107 r = stat("/etc/locale.conf", &st);
108 if (r < 0 && errno != ENOENT)
109 return -errno;
110
111 if (r >= 0) {
112 usec_t t;
113
114 /* If mtime is not changed, then we do not need to re-read the file. */
115 t = timespec_load(&st.st_mtim);
116 if (c->locale_mtime != USEC_INFINITY && t == c->locale_mtime)
117 return 0;
118
119 c->locale_mtime = t;
120 context_free_locale(c);
121
122 r = parse_env_file(NULL, "/etc/locale.conf",
123 "LANG", &c->locale[VARIABLE_LANG],
124 "LANGUAGE", &c->locale[VARIABLE_LANGUAGE],
125 "LC_CTYPE", &c->locale[VARIABLE_LC_CTYPE],
126 "LC_NUMERIC", &c->locale[VARIABLE_LC_NUMERIC],
127 "LC_TIME", &c->locale[VARIABLE_LC_TIME],
128 "LC_COLLATE", &c->locale[VARIABLE_LC_COLLATE],
129 "LC_MONETARY", &c->locale[VARIABLE_LC_MONETARY],
130 "LC_MESSAGES", &c->locale[VARIABLE_LC_MESSAGES],
131 "LC_PAPER", &c->locale[VARIABLE_LC_PAPER],
132 "LC_NAME", &c->locale[VARIABLE_LC_NAME],
133 "LC_ADDRESS", &c->locale[VARIABLE_LC_ADDRESS],
134 "LC_TELEPHONE", &c->locale[VARIABLE_LC_TELEPHONE],
135 "LC_MEASUREMENT", &c->locale[VARIABLE_LC_MEASUREMENT],
136 "LC_IDENTIFICATION", &c->locale[VARIABLE_LC_IDENTIFICATION]);
137 if (r < 0)
138 return r;
139 } else {
140 int p;
141
142 c->locale_mtime = USEC_INFINITY;
143 context_free_locale(c);
144
145 /* Fill in what we got passed from systemd. */
146 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
147 const char *name;
148
149 name = locale_variable_to_string(p);
150 assert(name);
151
152 r = free_and_strdup(&c->locale[p], empty_to_null(getenv(name)));
153 if (r < 0)
154 return r;
155 }
156 }
157
158 locale_simplify(c->locale);
159 return 0;
160 }
161
162 int vconsole_read_data(Context *c, sd_bus_message *m) {
163 struct stat st;
164 usec_t t;
165 int r;
166
167 /* Do not try to re-read the file within single bus operation. */
168 if (m) {
169 if (m == c->vc_cache)
170 return 0;
171
172 sd_bus_message_unref(c->vc_cache);
173 c->vc_cache = sd_bus_message_ref(m);
174 }
175
176 if (stat("/etc/vconsole.conf", &st) < 0) {
177 if (errno != ENOENT)
178 return -errno;
179
180 c->vc_mtime = USEC_INFINITY;
181 context_free_vconsole(c);
182 return 0;
183 }
184
185 /* If mtime is not changed, then we do not need to re-read */
186 t = timespec_load(&st.st_mtim);
187 if (c->vc_mtime != USEC_INFINITY && t == c->vc_mtime)
188 return 0;
189
190 c->vc_mtime = t;
191 context_free_vconsole(c);
192
193 r = parse_env_file(NULL, "/etc/vconsole.conf",
194 "KEYMAP", &c->vc_keymap,
195 "KEYMAP_TOGGLE", &c->vc_keymap_toggle);
196 if (r < 0)
197 return r;
198
199 return 0;
200 }
201
202 int x11_read_data(Context *c, sd_bus_message *m) {
203 _cleanup_fclose_ FILE *f = NULL;
204 bool in_section = false;
205 struct stat st;
206 usec_t t;
207 int r;
208
209 /* Do not try to re-read the file within single bus operation. */
210 if (m) {
211 if (m == c->x11_cache)
212 return 0;
213
214 sd_bus_message_unref(c->x11_cache);
215 c->x11_cache = sd_bus_message_ref(m);
216 }
217
218 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) < 0) {
219 if (errno != ENOENT)
220 return -errno;
221
222 c->x11_mtime = USEC_INFINITY;
223 context_free_x11(c);
224 return 0;
225 }
226
227 /* If mtime is not changed, then we do not need to re-read */
228 t = timespec_load(&st.st_mtim);
229 if (c->x11_mtime != USEC_INFINITY && t == c->x11_mtime)
230 return 0;
231
232 c->x11_mtime = t;
233 context_free_x11(c);
234
235 f = fopen("/etc/X11/xorg.conf.d/00-keyboard.conf", "re");
236 if (!f)
237 return -errno;
238
239 for (;;) {
240 _cleanup_free_ char *line = NULL;
241 char *l;
242
243 r = read_line(f, LONG_LINE_MAX, &line);
244 if (r < 0)
245 return r;
246 if (r == 0)
247 break;
248
249 l = strstrip(line);
250 if (IN_SET(l[0], 0, '#'))
251 continue;
252
253 if (in_section && first_word(l, "Option")) {
254 _cleanup_strv_free_ char **a = NULL;
255
256 r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
257 if (r < 0)
258 return r;
259
260 if (strv_length(a) == 3) {
261 char **p = NULL;
262
263 if (streq(a[1], "XkbLayout"))
264 p = &c->x11_layout;
265 else if (streq(a[1], "XkbModel"))
266 p = &c->x11_model;
267 else if (streq(a[1], "XkbVariant"))
268 p = &c->x11_variant;
269 else if (streq(a[1], "XkbOptions"))
270 p = &c->x11_options;
271
272 if (p) {
273 free_and_replace(*p, a[2]);
274 }
275 }
276
277 } else if (!in_section && first_word(l, "Section")) {
278 _cleanup_strv_free_ char **a = NULL;
279
280 r = strv_split_extract(&a, l, WHITESPACE, EXTRACT_QUOTES);
281 if (r < 0)
282 return -ENOMEM;
283
284 if (strv_length(a) == 2 && streq(a[1], "InputClass"))
285 in_section = true;
286
287 } else if (in_section && first_word(l, "EndSection"))
288 in_section = false;
289 }
290
291 return 0;
292 }
293
294 int locale_write_data(Context *c, char ***settings) {
295 _cleanup_strv_free_ char **l = NULL;
296 struct stat st;
297 int r, p;
298
299 /* Set values will be returned as strv in *settings on success. */
300
301 for (p = 0; p < _VARIABLE_LC_MAX; p++) {
302 _cleanup_free_ char *t = NULL;
303 char **u;
304 const char *name;
305
306 name = locale_variable_to_string(p);
307 assert(name);
308
309 if (isempty(c->locale[p]))
310 continue;
311
312 if (asprintf(&t, "%s=%s", name, c->locale[p]) < 0)
313 return -ENOMEM;
314
315 u = strv_env_set(l, t);
316 if (!u)
317 return -ENOMEM;
318
319 strv_free_and_replace(l, u);
320 }
321
322 if (strv_isempty(l)) {
323 if (unlink("/etc/locale.conf") < 0)
324 return errno == ENOENT ? 0 : -errno;
325
326 c->locale_mtime = USEC_INFINITY;
327 return 0;
328 }
329
330 r = write_env_file_label("/etc/locale.conf", l);
331 if (r < 0)
332 return r;
333
334 *settings = TAKE_PTR(l);
335
336 if (stat("/etc/locale.conf", &st) >= 0)
337 c->locale_mtime = timespec_load(&st.st_mtim);
338
339 return 0;
340 }
341
342 int vconsole_write_data(Context *c) {
343 _cleanup_strv_free_ char **l = NULL;
344 struct stat st;
345 int r;
346
347 r = load_env_file(NULL, "/etc/vconsole.conf", &l);
348 if (r < 0 && r != -ENOENT)
349 return r;
350
351 if (isempty(c->vc_keymap))
352 l = strv_env_unset(l, "KEYMAP");
353 else {
354 _cleanup_free_ char *s = NULL;
355 char **u;
356
357 s = strappend("KEYMAP=", c->vc_keymap);
358 if (!s)
359 return -ENOMEM;
360
361 u = strv_env_set(l, s);
362 if (!u)
363 return -ENOMEM;
364
365 strv_free_and_replace(l, u);
366 }
367
368 if (isempty(c->vc_keymap_toggle))
369 l = strv_env_unset(l, "KEYMAP_TOGGLE");
370 else {
371 _cleanup_free_ char *s = NULL;
372 char **u;
373
374 s = strappend("KEYMAP_TOGGLE=", c->vc_keymap_toggle);
375 if (!s)
376 return -ENOMEM;
377
378 u = strv_env_set(l, s);
379 if (!u)
380 return -ENOMEM;
381
382 strv_free_and_replace(l, u);
383 }
384
385 if (strv_isempty(l)) {
386 if (unlink("/etc/vconsole.conf") < 0)
387 return errno == ENOENT ? 0 : -errno;
388
389 c->vc_mtime = USEC_INFINITY;
390 return 0;
391 }
392
393 r = write_env_file_label("/etc/vconsole.conf", l);
394 if (r < 0)
395 return r;
396
397 if (stat("/etc/vconsole.conf", &st) >= 0)
398 c->vc_mtime = timespec_load(&st.st_mtim);
399
400 return 0;
401 }
402
403 int x11_write_data(Context *c) {
404 _cleanup_fclose_ FILE *f = NULL;
405 _cleanup_free_ char *temp_path = NULL;
406 struct stat st;
407 int r;
408
409 if (isempty(c->x11_layout) &&
410 isempty(c->x11_model) &&
411 isempty(c->x11_variant) &&
412 isempty(c->x11_options)) {
413
414 if (unlink("/etc/X11/xorg.conf.d/00-keyboard.conf") < 0)
415 return errno == ENOENT ? 0 : -errno;
416
417 c->vc_mtime = USEC_INFINITY;
418 return 0;
419 }
420
421 (void) mkdir_p_label("/etc/X11/xorg.conf.d", 0755);
422 r = fopen_temporary("/etc/X11/xorg.conf.d/00-keyboard.conf", &f, &temp_path);
423 if (r < 0)
424 return r;
425
426 (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
427 (void) fchmod(fileno(f), 0644);
428
429 fputs("# Written by systemd-localed(8), read by systemd-localed and Xorg. It's\n"
430 "# probably wise not to edit this file manually. Use localectl(1) to\n"
431 "# instruct systemd-localed to update it.\n"
432 "Section \"InputClass\"\n"
433 " Identifier \"system-keyboard\"\n"
434 " MatchIsKeyboard \"on\"\n", f);
435
436 if (!isempty(c->x11_layout))
437 fprintf(f, " Option \"XkbLayout\" \"%s\"\n", c->x11_layout);
438
439 if (!isempty(c->x11_model))
440 fprintf(f, " Option \"XkbModel\" \"%s\"\n", c->x11_model);
441
442 if (!isempty(c->x11_variant))
443 fprintf(f, " Option \"XkbVariant\" \"%s\"\n", c->x11_variant);
444
445 if (!isempty(c->x11_options))
446 fprintf(f, " Option \"XkbOptions\" \"%s\"\n", c->x11_options);
447
448 fputs("EndSection\n", f);
449
450 r = fflush_sync_and_check(f);
451 if (r < 0)
452 goto fail;
453
454 if (rename(temp_path, "/etc/X11/xorg.conf.d/00-keyboard.conf") < 0) {
455 r = -errno;
456 goto fail;
457 }
458
459 if (stat("/etc/X11/xorg.conf.d/00-keyboard.conf", &st) >= 0)
460 c->x11_mtime = timespec_load(&st.st_mtim);
461
462 return 0;
463
464 fail:
465 if (temp_path)
466 (void) unlink(temp_path);
467
468 return r;
469 }
470
471 static int read_next_mapping(const char* filename,
472 unsigned min_fields, unsigned max_fields,
473 FILE *f, unsigned *n, char ***a) {
474 assert(f);
475 assert(n);
476 assert(a);
477
478 for (;;) {
479 _cleanup_free_ char *line = NULL;
480 size_t length;
481 char *l, **b;
482 int r;
483
484 r = read_line(f, LONG_LINE_MAX, &line);
485 if (r < 0)
486 return r;
487 if (r == 0)
488 break;
489
490 (*n)++;
491
492 l = strstrip(line);
493 if (IN_SET(l[0], 0, '#'))
494 continue;
495
496 r = strv_split_extract(&b, l, WHITESPACE, EXTRACT_QUOTES);
497 if (r < 0)
498 return r;
499
500 length = strv_length(b);
501 if (length < min_fields || length > max_fields) {
502 log_error("Invalid line %s:%u, ignoring.", filename, *n);
503 strv_free(b);
504 continue;
505
506 }
507
508 *a = b;
509 return 1;
510 }
511
512 return 0;
513 }
514
515 int vconsole_convert_to_x11(Context *c) {
516 const char *map;
517 int modified = -1;
518
519 map = systemd_kbd_model_map();
520
521 if (isempty(c->vc_keymap)) {
522 modified =
523 !isempty(c->x11_layout) ||
524 !isempty(c->x11_model) ||
525 !isempty(c->x11_variant) ||
526 !isempty(c->x11_options);
527
528 context_free_x11(c);
529 } else {
530 _cleanup_fclose_ FILE *f = NULL;
531 unsigned n = 0;
532
533 f = fopen(map, "re");
534 if (!f)
535 return -errno;
536
537 for (;;) {
538 _cleanup_strv_free_ char **a = NULL;
539 int r;
540
541 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
542 if (r < 0)
543 return r;
544 if (r == 0)
545 break;
546
547 if (!streq(c->vc_keymap, a[0]))
548 continue;
549
550 if (!streq_ptr(c->x11_layout, empty_or_dash_to_null(a[1])) ||
551 !streq_ptr(c->x11_model, empty_or_dash_to_null(a[2])) ||
552 !streq_ptr(c->x11_variant, empty_or_dash_to_null(a[3])) ||
553 !streq_ptr(c->x11_options, empty_or_dash_to_null(a[4]))) {
554
555 if (free_and_strdup(&c->x11_layout, empty_or_dash_to_null(a[1])) < 0 ||
556 free_and_strdup(&c->x11_model, empty_or_dash_to_null(a[2])) < 0 ||
557 free_and_strdup(&c->x11_variant, empty_or_dash_to_null(a[3])) < 0 ||
558 free_and_strdup(&c->x11_options, empty_or_dash_to_null(a[4])) < 0)
559 return -ENOMEM;
560
561 modified = true;
562 }
563
564 break;
565 }
566 }
567
568 if (modified > 0)
569 log_info("Changing X11 keyboard layout to '%s' model '%s' variant '%s' options '%s'",
570 strempty(c->x11_layout),
571 strempty(c->x11_model),
572 strempty(c->x11_variant),
573 strempty(c->x11_options));
574 else if (modified < 0)
575 log_notice("X11 keyboard layout was not modified: no conversion found for \"%s\".",
576 c->vc_keymap);
577 else
578 log_debug("X11 keyboard layout did not need to be modified.");
579
580 return modified > 0;
581 }
582
583 int find_converted_keymap(const char *x11_layout, const char *x11_variant, char **new_keymap) {
584 const char *dir;
585 _cleanup_free_ char *n;
586
587 if (x11_variant)
588 n = strjoin(x11_layout, "-", x11_variant);
589 else
590 n = strdup(x11_layout);
591 if (!n)
592 return -ENOMEM;
593
594 NULSTR_FOREACH(dir, KBD_KEYMAP_DIRS) {
595 _cleanup_free_ char *p = NULL, *pz = NULL;
596 bool uncompressed;
597
598 p = strjoin(dir, "xkb/", n, ".map");
599 pz = strjoin(dir, "xkb/", n, ".map.gz");
600 if (!p || !pz)
601 return -ENOMEM;
602
603 uncompressed = access(p, F_OK) == 0;
604 if (uncompressed || access(pz, F_OK) == 0) {
605 log_debug("Found converted keymap %s at %s",
606 n, uncompressed ? p : pz);
607
608 *new_keymap = TAKE_PTR(n);
609 return 1;
610 }
611 }
612
613 return 0;
614 }
615
616 int find_legacy_keymap(Context *c, char **ret) {
617 const char *map;
618 _cleanup_fclose_ FILE *f = NULL;
619 _cleanup_free_ char *new_keymap = NULL;
620 unsigned n = 0;
621 unsigned best_matching = 0;
622 int r;
623
624 assert(!isempty(c->x11_layout));
625
626 map = systemd_kbd_model_map();
627
628 f = fopen(map, "re");
629 if (!f)
630 return -errno;
631
632 for (;;) {
633 _cleanup_strv_free_ char **a = NULL;
634 unsigned matching = 0;
635
636 r = read_next_mapping(map, 5, UINT_MAX, f, &n, &a);
637 if (r < 0)
638 return r;
639 if (r == 0)
640 break;
641
642 /* Determine how well matching this entry is */
643 if (streq(c->x11_layout, a[1]))
644 /* If we got an exact match, this is best */
645 matching = 10;
646 else {
647 /* We have multiple X layouts, look for an
648 * entry that matches our key with everything
649 * but the first layout stripped off. */
650 if (startswith_comma(c->x11_layout, a[1]))
651 matching = 5;
652 else {
653 char *x;
654
655 /* If that didn't work, strip off the
656 * other layouts from the entry, too */
657 x = strndupa(a[1], strcspn(a[1], ","));
658 if (startswith_comma(c->x11_layout, x))
659 matching = 1;
660 }
661 }
662
663 if (matching > 0) {
664 if (isempty(c->x11_model) || streq_ptr(c->x11_model, a[2])) {
665 matching++;
666
667 if (streq_ptr(c->x11_variant, a[3])) {
668 matching++;
669
670 if (streq_ptr(c->x11_options, a[4]))
671 matching++;
672 }
673 }
674 }
675
676 /* The best matching entry so far, then let's save that */
677 if (matching >= MAX(best_matching, 1u)) {
678 log_debug("Found legacy keymap %s with score %u",
679 a[0], matching);
680
681 if (matching > best_matching) {
682 best_matching = matching;
683
684 r = free_and_strdup(&new_keymap, a[0]);
685 if (r < 0)
686 return r;
687 }
688 }
689 }
690
691 if (best_matching < 10 && c->x11_layout) {
692 /* The best match is only the first part of the X11
693 * keymap. Check if we have a converted map which
694 * matches just the first layout.
695 */
696 char *l, *v = NULL, *converted;
697
698 l = strndupa(c->x11_layout, strcspn(c->x11_layout, ","));
699 if (c->x11_variant)
700 v = strndupa(c->x11_variant, strcspn(c->x11_variant, ","));
701 r = find_converted_keymap(l, v, &converted);
702 if (r < 0)
703 return r;
704 if (r > 0)
705 free_and_replace(new_keymap, converted);
706 }
707
708 *ret = TAKE_PTR(new_keymap);
709 return (bool) *ret;
710 }
711
712 int find_language_fallback(const char *lang, char **language) {
713 const char *map;
714 _cleanup_fclose_ FILE *f = NULL;
715 unsigned n = 0;
716
717 assert(lang);
718 assert(language);
719
720 map = systemd_language_fallback_map();
721
722 f = fopen(map, "re");
723 if (!f)
724 return -errno;
725
726 for (;;) {
727 _cleanup_strv_free_ char **a = NULL;
728 int r;
729
730 r = read_next_mapping(map, 2, 2, f, &n, &a);
731 if (r <= 0)
732 return r;
733
734 if (streq(lang, a[0])) {
735 assert(strv_length(a) == 2);
736 *language = TAKE_PTR(a[1]);
737 return 1;
738 }
739 }
740
741 assert_not_reached("should not be here");
742 }
743
744 int x11_convert_to_vconsole(Context *c) {
745 bool modified = false;
746
747 if (isempty(c->x11_layout)) {
748 modified =
749 !isempty(c->vc_keymap) ||
750 !isempty(c->vc_keymap_toggle);
751
752 context_free_vconsole(c);
753 } else {
754 _cleanup_free_ char *new_keymap = NULL;
755 int r;
756
757 r = find_converted_keymap(c->x11_layout, c->x11_variant, &new_keymap);
758 if (r < 0)
759 return r;
760 else if (r == 0) {
761 r = find_legacy_keymap(c, &new_keymap);
762 if (r < 0)
763 return r;
764 }
765 if (r == 0)
766 /* We search for layout-variant match first, but then we also look
767 * for anything which matches just the layout. So it's accurate to say
768 * that we couldn't find anything which matches the layout. */
769 log_notice("No conversion to virtual console map found for \"%s\".",
770 c->x11_layout);
771
772 if (!streq_ptr(c->vc_keymap, new_keymap)) {
773 free_and_replace(c->vc_keymap, new_keymap);
774 c->vc_keymap_toggle = mfree(c->vc_keymap_toggle);
775 modified = true;
776 }
777 }
778
779 if (modified)
780 log_info("Changing virtual console keymap to '%s' toggle '%s'",
781 strempty(c->vc_keymap), strempty(c->vc_keymap_toggle));
782 else
783 log_debug("Virtual console keymap was not modified.");
784
785 return modified;
786 }