Numworks Epsilon  1.4.1
Graphing Calculator Operating System
builtinhelp.c
Go to the documentation of this file.
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>
28 #include <string.h>
29 
30 #include "py/builtin.h"
31 #include "py/objmodule.h"
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) {
52  mp_obj_print(name_o, PRINT_STR);
54  mp_obj_print(value, PRINT_STR);
56 }
57 
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) {
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
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 (;;) {
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--) {
121  }
122  }
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 
131 STATIC void mp_help_print_obj(const mp_obj_t obj) {
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 
139  mp_obj_type_t *type = mp_obj_get_type(obj);
140 
141  // try to print something sensible about the given object
142  mp_print_str(MP_PYTHON_PRINTER, "object ");
143  mp_obj_print(obj, PRINT_STR);
144  mp_printf(MP_PYTHON_PRINTER, " is of type %q\n", type->name);
145 
146  mp_map_t *map = NULL;
147  if (type == &mp_type_module) {
149  } else {
150  if (type == &mp_type_type) {
151  type = MP_OBJ_TO_PTR(obj);
152  }
154  map = mp_obj_dict_get_map(type->locals_dict);
155  }
156  }
157  if (map != NULL) {
158  for (uint i = 0; i < map->alloc; i++) {
159  if (map->table[i].key != MP_OBJ_NULL) {
160  mp_help_print_info_about_object(map->table[i].key, map->table[i].value);
161  }
162  }
163  }
164 }
165 
166 STATIC mp_obj_t mp_builtin_help(size_t n_args, const mp_obj_t *args) {
167  if (n_args == 0) {
168  // print a general help message
170  } else {
171  // try to print something sensible about the given object
172  mp_help_print_obj(args[0]);
173  }
174 
175  return mp_const_none;
176 }
177 MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_help_obj, 0, 1, mp_builtin_help);
178 
179 #endif // MICROPY_PY_BUILTINS_HELP
const mp_map_t mp_builtin_module_map
mp_map_t * mp_obj_dict_get_map(mp_obj_t self_in)
Definition: objdict.c:608
uintptr_t mp_uint_t
Definition: mpconfigport.h:74
#define mp_const_none
Definition: obj.h:614
const mp_map_t mp_builtin_module_weak_links_map
#define MP_OBJ_IS_TYPE(o, t)
Definition: obj.h:254
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind)
Definition: obj.c:76
mp_obj_type_t * mp_obj_get_type(mp_const_obj_t o_in)
Definition: obj.c:40
int mp_print_str(const mp_print_t *print, const char *str)
Definition: mpprint.c:53
#define MP_OBJ_NEW_QSTR(qst)
Definition: obj.h:92
mp_obj_t mp_obj_new_list(size_t n, mp_obj_t *items)
Definition: objlist.c:470
mp_obj_t key
Definition: obj.h:342
size_t alloc
Definition: obj.h:361
mp_obj_dict_t * mp_obj_module_get_globals(mp_obj_t self_in)
Definition: objmodule.c:125
const mp_map_t mp_const_empty_map
Definition: map.c:38
#define STATIC
Definition: mpconfig.h:1178
mp_obj_t mp_obj_list_append(mp_obj_t self_in, mp_obj_t arg)
Definition: objlist.c:234
size_t strlen(const char *s)
Definition: strlen.c:3
#define NULL
Definition: stddef.h:4
#define MP_OBJ_NULL
Definition: obj.h:73
#define MP_PYTHON_PRINTER
Definition: mpprint.h:45
void mp_obj_list_get(mp_obj_t self_in, size_t *len, mp_obj_t **items)
Definition: objlist.c:480
mp_obj_t value
Definition: obj.h:343
mp_obj_t mp_obj_new_str(const char *data, size_t len, bool make_qstr_if_not_already)
Definition: objstr.c:2025
args
Definition: i18n.py:175
list
Definition: grammar.h:158
#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name)
Definition: obj.h:297
Definition: obj.h:356
const mp_obj_type_t mp_type_type
Definition: objtype.c:969
const char * mp_obj_str_get_str(mp_obj_t self_in)
Definition: objstr.c:2095
mp_obj_t mp_obj_list_sort(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs)
Definition: objlist.c:315
#define MP_OBJ_TO_PTR(o)
Definition: obj.h:228
Definition: obj.h:413
const mp_obj_type_t mp_type_dict
Definition: objdict.c:552
const mp_obj_type_t mp_type_module
Definition: objmodule.c:94
qstr name
Definition: obj.h:478
uint64_t mp_obj_t
Definition: obj.h:39
mp_map_elem_t * table
Definition: obj.h:362
int mp_printf(const mp_print_t *print, const char *fmt,...)
Definition: mpprint.c:380
const char MICROPY_PY_BUILTINS_HELP_TEXT[]
struct _mp_obj_dict_t * locals_dict
Definition: obj.h:536
unsigned int uint
Definition: misc.h:38