Damien George | 9f04dfb | 2017-01-21 23:17:51 +1100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013-2016 Damien P. George |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | * THE SOFTWARE. |
| 25 | */ |
| 26 | |
| 27 | #include <stdio.h> |
Damien George | f5172af | 2017-01-22 12:12:54 +1100 | [diff] [blame] | 28 | #include <string.h> |
Damien George | 9f04dfb | 2017-01-21 23:17:51 +1100 | [diff] [blame] | 29 | |
| 30 | #include "py/builtin.h" |
Damien George | f5172af | 2017-01-22 12:12:54 +1100 | [diff] [blame] | 31 | #include "py/objmodule.h" |
Damien George | 9f04dfb | 2017-01-21 23:17:51 +1100 | [diff] [blame] | 32 | |
| 33 | #if MICROPY_PY_BUILTINS_HELP |
| 34 | |
| 35 | const char *mp_help_default_text = |
| 36 | "Welcome to MicroPython!\n" |
| 37 | "\n" |
| 38 | "For online docs please visit http://docs.micropython.org/\n" |
| 39 | "\n" |
| 40 | "Control commands:\n" |
| 41 | " CTRL-A -- on a blank line, enter raw REPL mode\n" |
| 42 | " CTRL-B -- on a blank line, enter normal REPL mode\n" |
| 43 | " CTRL-C -- interrupt a running program\n" |
| 44 | " CTRL-D -- on a blank line, exit or do a soft reset\n" |
| 45 | " CTRL-E -- on a blank line, enter paste mode\n" |
| 46 | "\n" |
| 47 | "For further help on a specific object, type help(obj)\n" |
| 48 | ; |
| 49 | |
| 50 | STATIC void mp_help_print_info_about_object(mp_obj_t name_o, mp_obj_t value) { |
| 51 | mp_print_str(MP_PYTHON_PRINTER, " "); |
| 52 | mp_obj_print(name_o, PRINT_STR); |
| 53 | mp_print_str(MP_PYTHON_PRINTER, " -- "); |
| 54 | mp_obj_print(value, PRINT_STR); |
| 55 | mp_print_str(MP_PYTHON_PRINTER, "\n"); |
| 56 | } |
| 57 | |
Damien George | f5172af | 2017-01-22 12:12:54 +1100 | [diff] [blame] | 58 | #if MICROPY_PY_BUILTINS_HELP_MODULES |
| 59 | STATIC void mp_help_add_from_map(mp_obj_t list, const mp_map_t *map) { |
| 60 | for (size_t i = 0; i < map->alloc; i++) { |
| 61 | if (MP_MAP_SLOT_IS_FILLED(map, i)) { |
| 62 | mp_obj_list_append(list, map->table[i].key); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | #if MICROPY_MODULE_FROZEN |
| 68 | STATIC void mp_help_add_from_names(mp_obj_t list, const char *name) { |
| 69 | while (*name) { |
| 70 | size_t l = strlen(name); |
| 71 | // name should end in '.py' and we strip it off |
| 72 | mp_obj_list_append(list, mp_obj_new_str(name, l - 3, false)); |
| 73 | name += l + 1; |
| 74 | } |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | STATIC void mp_help_print_modules(void) { |
| 79 | mp_obj_t list = mp_obj_new_list(0, NULL); |
| 80 | |
| 81 | mp_help_add_from_map(list, &mp_builtin_module_map); |
| 82 | |
| 83 | #if MICROPY_MODULE_WEAK_LINKS |
| 84 | mp_help_add_from_map(list, &mp_builtin_module_weak_links_map); |
| 85 | #endif |
| 86 | |
| 87 | #if MICROPY_MODULE_FROZEN_STR |
| 88 | extern const char mp_frozen_str_names[]; |
| 89 | mp_help_add_from_names(list, mp_frozen_str_names); |
| 90 | #endif |
| 91 | |
| 92 | #if MICROPY_MODULE_FROZEN_MPY |
| 93 | extern const char mp_frozen_mpy_names[]; |
| 94 | mp_help_add_from_names(list, mp_frozen_mpy_names); |
| 95 | #endif |
| 96 | |
| 97 | // sort the list so it's printed in alphabetical order |
| 98 | mp_obj_list_sort(1, &list, (mp_map_t*)&mp_const_empty_map); |
| 99 | |
| 100 | // print the list of modules in a column-first order |
| 101 | #define NUM_COLUMNS (4) |
| 102 | #define COLUMN_WIDTH (18) |
| 103 | mp_uint_t len; |
| 104 | mp_obj_t *items; |
| 105 | mp_obj_list_get(list, &len, &items); |
| 106 | unsigned int num_rows = (len + NUM_COLUMNS - 1) / NUM_COLUMNS; |
| 107 | for (unsigned int i = 0; i < num_rows; ++i) { |
| 108 | unsigned int j = i; |
| 109 | for (;;) { |
| 110 | int l = mp_print_str(MP_PYTHON_PRINTER, mp_obj_str_get_str(items[j])); |
| 111 | j += num_rows; |
| 112 | if (j >= len) { |
| 113 | break; |
| 114 | } |
| 115 | int gap = COLUMN_WIDTH - l; |
| 116 | while (gap < 1) { |
| 117 | gap += COLUMN_WIDTH; |
| 118 | } |
| 119 | while (gap--) { |
| 120 | mp_print_str(MP_PYTHON_PRINTER, " "); |
| 121 | } |
| 122 | } |
| 123 | mp_print_str(MP_PYTHON_PRINTER, "\n"); |
| 124 | } |
| 125 | |
| 126 | // let the user know there may be other modules available from the filesystem |
| 127 | mp_print_str(MP_PYTHON_PRINTER, "Plus any modules on the filesystem\n"); |
| 128 | } |
| 129 | #endif |
| 130 | |
Damien George | 9f04dfb | 2017-01-21 23:17:51 +1100 | [diff] [blame] | 131 | STATIC void mp_help_print_obj(const mp_obj_t obj) { |
Damien George | f5172af | 2017-01-22 12:12:54 +1100 | [diff] [blame] | 132 | #if MICROPY_PY_BUILTINS_HELP_MODULES |
| 133 | if (obj == MP_OBJ_NEW_QSTR(MP_QSTR_modules)) { |
| 134 | mp_help_print_modules(); |
| 135 | return; |
| 136 | } |
| 137 | #endif |
| 138 | |
Damien George | 9f04dfb | 2017-01-21 23:17:51 +1100 | [diff] [blame] | 139 | // try to print something sensible about the given object |
| 140 | mp_print_str(MP_PYTHON_PRINTER, "object "); |
| 141 | mp_obj_print(obj, PRINT_STR); |
| 142 | mp_printf(MP_PYTHON_PRINTER, " is of type %s\n", mp_obj_get_type_str(obj)); |
| 143 | |
| 144 | mp_map_t *map = NULL; |
| 145 | if (MP_OBJ_IS_TYPE(obj, &mp_type_module)) { |
| 146 | map = mp_obj_dict_get_map(mp_obj_module_get_globals(obj)); |
| 147 | } else { |
| 148 | mp_obj_type_t *type; |
| 149 | if (MP_OBJ_IS_TYPE(obj, &mp_type_type)) { |
| 150 | type = obj; |
| 151 | } else { |
| 152 | type = mp_obj_get_type(obj); |
| 153 | } |
| 154 | if (type->locals_dict != MP_OBJ_NULL && MP_OBJ_IS_TYPE(type->locals_dict, &mp_type_dict)) { |
| 155 | map = mp_obj_dict_get_map(type->locals_dict); |
| 156 | } |
| 157 | } |
| 158 | if (map != NULL) { |
| 159 | for (uint i = 0; i < map->alloc; i++) { |
| 160 | if (map->table[i].key != MP_OBJ_NULL) { |
| 161 | mp_help_print_info_about_object(map->table[i].key, map->table[i].value); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | STATIC mp_obj_t mp_builtin_help(size_t n_args, const mp_obj_t *args) { |
| 168 | if (n_args == 0) { |
| 169 | // print a general help message |
| 170 | mp_print_str(MP_PYTHON_PRINTER, MICROPY_PY_BUILTINS_HELP_TEXT); |
| 171 | } else { |
| 172 | // try to print something sensible about the given object |
| 173 | mp_help_print_obj(args[0]); |
| 174 | } |
| 175 | |
| 176 | return mp_const_none; |
| 177 | } |
| 178 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, mp_builtin_help); |
| 179 | |
| 180 | #endif // MICROPY_PY_BUILTINS_HELP |